a
    Ž¥»ax  ã                   @   s°   d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
 G dd„ deƒZdejjeee  ee	e
  eejjd	œd
d„Zejjdœdd„Zejje	e e	e dœdd„ZdS )zM
This module contains utility method for mobile model optimization and lint.
é    N)ÚEnum)ÚMobileOptimizerType)ÚOptionalÚSetÚListÚAnyStrc                   @   s   e Zd ZdZdZdZdZdS )ÚLintCodeé   é   é   é   N)Ú__name__Ú
__module__Ú__qualname__ÚBUNDLED_INPUTÚREQUIRES_GRADÚDROPOUTÚ	BATCHNORM© r   r   úV/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/utils/mobile_optimizer.pyr   
   s   r   ÚCPU)Úscript_moduleÚoptimization_blocklistÚpreserved_methodsÚbackendÚreturnc           	         s"  t ˆ tjjƒs td tˆ ƒ¡ƒ‚|du r.tƒ }|du r:g }dd„ |D ƒ}tˆ |ƒ}t	‡ fdd„|D ƒƒrxt
t|| ƒƒ}g }|D ]}tˆ |ƒs€| |¡ q€|r²td d |¡¡ƒ‚| ¡ }|dkrÖtj ˆ j||¡}n>|d	krðtj ˆ j|¡}n$|d
k�rtj ˆ j|¡}ntdƒ‚tjj |¡S )a�  
    Args:
        script_module: An instance of torch script module with type of ScriptModule.
        optimization_blocklist: A set with type of MobileOptimizerType. When set is not passed,
            optimization method will run all the optimizer pass; otherwise, optimizer
            method will run the optimization pass that is not included inside optimization_blocklist.
        preserved_methods: A list of methods that needed to be preserved when freeze_module pass is invoked
        backend: Device type to use for running the result model ('CPU'(default), 'Vulkan' or 'Metal').
    Returns:
        A new optimized torch script module
    ú%Got {}, but ScriptModule is expected.Nc                 S   s   g | ]}t |ƒ‘qS r   )Ústr©Ú.0Úmethodr   r   r   Ú
<listcomp>-   ó    z'optimize_for_mobile.<locals>.<listcomp>c                    s   g | ]}t ˆ |ƒ‘qS r   )Úhasattrr   ©r   r   r   r!   0   r"   zCThe following methods to preserve do not exist in script_module: {}z, ÚcpuZvulkanZmetalz:Unknown backend, must be one of 'CPU', 'Vulkan' or 'Metal')Ú
isinstanceÚtorchÚjitÚScriptModuleÚ	TypeErrorÚformatÚtypeÚsetÚ(_get_bundled_inputs_preserved_attributesÚallÚlistr#   ÚappendÚAttributeErrorÚjoinÚlowerZ_CZ_jit_pass_optimize_for_mobileZ_cZ$_jit_pass_vulkan_optimize_for_mobileZ#_jit_pass_metal_optimize_for_mobileÚ
_recursiveZwrap_cpp_module)	r   r   r   r   Zpreserved_methods_strÚbundled_inputs_attributesZnon_exist_methodsr    Zoptimized_cpp_moduler   r$   r   Úoptimize_for_mobile   sF    ÿ

ÿÿý
r7   r$   c                 C   sÒ   t | tjjƒs td t| ƒ¡ƒ‚g }t| dƒsB| t	j
jddœ¡ |  ¡ D ](\}}|jrJ| t	jjd |¡dœ¡ qJtj | ¡}|D ]H}d|v rª| t	jjd |¡dœ¡ d|v r„| t	jjd	 |¡dœ¡ q„|S )
z´
    Args:
        script_module: An instance of torch script module with type of ScriptModule

    Returns:
        lint_map: A list of dictionary that contains modules lints
    r   Z$_generate_bundled_inputs_for_forwardz”No bundled input for forward, please add bundled inputs before saving the module using torch.utils.bundled_inputs.augment_model_with_bundled_inputs.)ÚnameÚmessagezParam {} requires grad, please set torch.no_grad() to reduce memory usage and improve computation speed during inference phase.Zdropoutz˜Operator {} exists, remember to call eval() before saving the module.and call torch.utils.mobile_optimizer.optimize_for_mobile to drop dropout operator.Z
batch_normz›Operator {} exists, remember to call eval() before saving the module and call torch.utils.mobile_optimizer.optimize_for_mobile to drop batch_norm operator.)r&   r'   r(   r)   r*   r+   r,   r#   r1   r   r   r8   Znamed_parametersZrequires_gradr   Zexport_opnamesr   r   )r   Z	lint_listr8   ÚparamZop_namesZop_namer   r   r   Úgenerate_mobile_module_lintsL   s.    ÿ
þþ
þr;   )r   r   r   c                 C   sz   g }t | dƒr"| d¡ | d¡ t | dƒrv| d¡ |  ¡ }|D ]2}||vrX| |¡ | d| ¡ | d| ¡ qB|S )NZget_all_bundled_inputsZget_num_bundled_inputsÚ%get_bundled_inputs_functions_and_infoZget_all_bundled_inputs_for_Z_bundled_inputs_deflated_)r#   r1   r<   )r   r   r6   Zall_infoZfunction_namer   r   r   r.   q   s    





r.   )NNr   )Ú__doc__r'   Úenumr   Ztorch._Cr   Útypingr   r   r   r   r   r(   r)   r   ZRecursiveScriptModuler7   r;   r.   r   r   r   r   Ú<module>   s       ü

ü<%