a
    ßñh  ã                   @   sz   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	 G dd„ deƒZ
dee ee	 ed	œd
d„Zejjdœdd„ZdS )zM
This module contains utility method for mobile model optimization and lint.
é    N)ÚEnum)ÚMobileOptimizerType)Ú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   ú\/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/utils/mobile_optimizer.pyr   
   s   r   ÚCPU)Úoptimization_blocklistÚpreserved_methodsÚbackendc                 C   s†   t | tjjƒs td t| ƒ¡ƒ‚|du r.tƒ }|du r:g }|dkrVtj 	| j
||¡}n"|dkrpt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.
        perserved_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) or 'Vulkan').
    Returns:
        A new optimized torch script module
    ú%Got {}, but ScriptModule is expected.Nr   ZVulkanz/Unknown backend, must be one of 'CPU', 'Vulkan')Ú
isinstanceÚtorchÚjitÚScriptModuleÚ	TypeErrorÚformatÚtypeÚsetZ_CZ_jit_pass_optimize_for_mobileZ_cZ$_jit_pass_vulkan_optimize_for_mobileÚ
_recursiveZwrap_cpp_module)Úscript_moduler   r   r   Zoptimized_cpp_moduler   r   r   Úoptimize_for_mobile   s    ÿr$   )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_inputszˆNo bundled input, 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    ÚhasattrÚappendr   r   r%   Znamed_parametersZrequires_gradr   Zexport_opnamesr   r   )r#   Z	lint_listr%   ÚparamZop_namesZop_namer   r   r   Úgenerate_mobile_module_lints4   s.    ÿ
þþ
þr*   )NNr   )Ú__doc__r   Úenumr   Ztorch._Cr   Útypingr   r   r   r   Ústrr$   r   r   r*   r   r   r   r   Ú<module>   s      üü$