U
    •Ê¹jž  ã                   @   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   ú[/var/www/html/bwave/python/venv/lib/python3.8/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�ƒ‚|dkr0tƒ }|dkr<g }dd„ |D ƒ}tˆ |ƒ}t‡ fdd„|D ƒƒrzt	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>-   s     z'optimize_for_mobile.<locals>.<listcomp>c                 3   s   | ]}t ˆ |ƒV  qd S )N)Úhasattrr   ©r   r   r   Ú	<genexpr>0   s     z&optimize_for_mobile.<locals>.<genexpr>zAThe 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Ú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_mobileZ
_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   sJ    ÿ

ÿýý
r6   r$   c                 C   sÖ   t | tjjƒs"tdt| ƒ› d�ƒ‚g }t| dƒsD| tj	j
ddœ¡ |  ¡ D ]*\}}|jrL| tjj
d|› d�dœ¡ qLtj | ¡}|D ]H}d|kr®| tjj
d	 |¡dœ¡ d
|k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   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 zw 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#   r1   r   r   r7   Znamed_parametersZrequires_gradr   Zexport_opnamesr   Úformatr   )r   Z	lint_listr7   ÚparamZop_namesZop_namer   r   r   Úgenerate_mobile_module_lintsN   s*    ÿ
þ
þr;   )r   r   r   c                 C   sz   g }t | dƒr"| d¡ | d¡ t | dƒrv| d¡ |  ¡ }|D ]2}||k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   r5   Zall_infoZfunction_namer   r   r   r.   s   s    





r.   )NNr   )Ú__doc__r(   Úenumr   Ztorch._Cr   ZMobileOptimizerTypeÚtypingr   r   r   r   r   r)   r*   r   ZRecursiveScriptModuler6   r;   r.   r   r   r   r   Ú<module>   s"      ü

 ü>%