
    Yh                     2   d Z ddlZddlmZ ddlmZ ddlmZm	Z	  G d de          Z
	 	 	 dd	ej        j        d
eee                  deee	                  dedej        j        f
dZd	ej        j        fdZd	ej        j        dee         dee         fdZdS )zKThis module contains utility method for mobile model optimization and lint.    N)Enum)_MobileOptimizerType)OptionalAnyStrc                       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     n/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/utils/mobile_optimizer.pyr   r   	   s"        MMGIIIr   r   CPUscript_moduleoptimization_blocklistpreserved_methodsbackendreturnc                 Z    t           t          j        j                  s t	          dt                      d          |t                      }|g }d |D             }t           |          }t           fd|D                       rt          t          ||z                       } fd|D             }|r%t          dd                    |                     |                                }|d	k    r't          j                             j        ||          }nh|d
k    r't          j                             j        ||          }n;|dk    r&t          j                             j        |          }nt	          d          t          j        j                            |          S )a  
    Optimize a torch script module for mobile deployment.

    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                 ,    g | ]}t          |          S r   )str).0methods     r   
<listcomp>z'optimize_for_mobile.<locals>.<listcomp>.   s    'T'T'TF'T'T'Tr   c              3   8   K   | ]}t          |          V  d S )Nhasattrr"   r#   r   s     r   	<genexpr>z&optimize_for_mobile.<locals>.<genexpr>1   s-      
R
Rf7=&))
R
R
R
R
R
Rr   c                 4    g | ]}t          |          |S r   r&   r(   s     r   r$   z'optimize_for_mobile.<locals>.<listcomp>4   s*    jjjF7S`bhKiKijjjjr   zAThe following methods to preserve do not exist in script_module: z, cpuvulkanmetalz:Unknown backend, must be one of 'CPU', 'Vulkan' or 'Metal')
isinstancetorchjitScriptModule	TypeErrortypeset(_get_bundled_inputs_preserved_attributesalllistAttributeErrorjoinlower_C_jit_pass_optimize_for_mobile_c$_jit_pass_vulkan_optimize_for_mobile#_jit_pass_metal_optimize_for_mobile
_recursivewrap_cpp_module)r   r   r   r   preserved_methods_strbundled_inputs_attributesnon_exist_methodsoptimized_cpp_modules   `       r   optimize_for_mobilerF      s   $ mUY%;<< IG4&&GGGI I 	I %!$ 
 (U'TBS'T'T'T HXm n n

R
R
R
R8Q
R
R
RRR ] $S)>AZ)Z%[%[ \ \jjjj.Cjjj pnPTPYPYZkPlPlnnp p 	p mmooG%$xEE"! #  # 
H		$xLL"! #  # 
G		$xKKML\^sttTUUU9//0DEEEr   c                    t          | t          j        j                  s t	          dt          |            d          g }t          | d          s'|                    t          j	        j
        dd           |                                 D ]7\  }}|j        r+|                    t          j        j
        d| dd           8t          j                            |           }|D ]`}d|v r+|                    t          j        j
        d	| d
d           d|v r+|                    t          j        j
        d	| dd           a|S )z
    Generate a list of lints for a given torch script module.

    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   $_generate_bundled_inputs_for_forwardzNo 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.dropoutz	Operator z exists, remember to call eval() before saving the module.and call torch.utils.mobile_optimizer.optimize_for_mobile to drop dropout operator.
batch_normz 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/   r0   r1   r2   r3   r'   appendr   r   rI   named_parametersrequires_gradr   export_opnamesr   r   )r   	lint_listrI   paramop_namesop_names         r   generate_mobile_module_lintsrU   L   s    mUY%;<< IG4&&GGGI I 	I I="HII {("8"= Ky z z 	{ 	{ 	{ %5577 3 3e 	3h&<&A O1W[ O1 O1 O1 2 2 3 3 3 y''66H 
, 
,h&6&;**W ** ** **+ + , , , 7""h&8&=**W ** ** **+ + , , ,
 r   c                    g }t          | d          r*|                    d           |                    d           t          | d          rw|                    d           |                                 }|D ]K}||vr|                    |           |                    d|z              |                    d|z              L|S )Nget_all_bundled_inputsget_num_bundled_inputs%get_bundled_inputs_functions_and_infoget_all_bundled_inputs_for__bundled_inputs_deflated_)r'   rM   rY   )r   r   rC   all_infofunction_names        r   r5   r5   u   s     "}677 C!(()ABBB!(()ABBB }EFF Z!(()PQQQ FFHH% 	Z 	ZM$555)00???%,,-J]-Z[[[%,,-H=-XYYYY$$r   )NNr   )__doc__r/   enumr   torch._Cr   MobileOptimizerTypetypingr   r   r   r0   r1   r4   r7   r!   RecursiveScriptModulerF   rU   r5   r   r   r   <module>rd      s^   Q Q        @ @ @ @ @ @ # # # # # # # #    t    FJ48	:F :Fy-:F (-@)A B:F $DL1:F 	:F "'!@	:F :F :F :Fz'	0F ' ' ' 'R%EI<R %gklogp %uyz}u~ % % % % % %r   