
    JTh                     ^   S r SSKrSSKJr  SSKJr  SSKJrJ	r	   " S S\5      r
   SS\R                  R                  S	\\\      S
\\\	      S\S\R                  R                   4
S jjrS\R                  R                  4S jrS\R                  R                  S
\\   S\\   4S jrg)zKThis module contains utility method for mobile model optimization and lint.    N)Enum)_MobileOptimizerType)OptionalAnyStrc                   $    \ rS rSrSrSrSrSrSrg)LintCode	                N)	__name__
__module____qualname____firstlineno__BUNDLED_INPUTREQUIRES_GRADDROPOUT	BATCHNORM__static_attributes__r       T/var/www/auris/envauris/lib/python3.13/site-packages/torch/utils/mobile_optimizer.pyr   r   	   s    MMGIr   r   script_moduleoptimization_blocklistpreserved_methodsbackendreturnc                   ^  [        T [        R                  R                  5      (       d  [	        S[        T 5       S35      eUc
  [        5       nUc  / nU Vs/ s H  n[        U5      PM     nn[        T U5      n[        U 4S jU 5       5      (       a  [        [        XV-   5      5      nU Vs/ s H  n[        T U5      (       a  M  UPM     nnU(       a  [        SSR                  U5       35      eUR                  5       nUS:X  a,  [        R                  R!                  T R"                  UU5      nOnUS:X  a,  [        R                  R%                  T R"                  UU5      nO<US:X  a+  [        R                  R'                  T R"                  U5      nO[	        S	5      e[        R                  R(                  R+                  U5      $ s  snf s  snf )
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.c              3   <   >#    U  H  n[        TU5      v   M     g 7f)N)hasattr).0methodr   s     r   	<genexpr>&optimize_for_mobile.<locals>.<genexpr>1   s     
R8Qf7=&))8Qs   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str(_get_bundled_inputs_preserved_attributesalllistr#   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   r%   preserved_methods_strbundled_inputs_attributesnon_exist_methodsoptimized_cpp_modules	   `        r   optimize_for_mobilerD      s   $ mUYY%;%;<<4&''FGI 	I %!$ 
 CT'TBSFBS'T HXm n

R8Q
RRR $S)>)Z%[ \.Cj.CF7S`bhKi.CjOPTPYPYZkPlOmnp 	p mmoG%$xxEE"! # 
H	$xxLL"! # 
G	$xxKKML\L\^stTUU99//0DEE7 (U ks   G0G	Gc                    [        U [        R                  R                  5      (       d  [	        S[        U 5       S35      e/ n[        U S5      (       d,  UR                  [        R                  R                  SS.5        U R                  5        HH  u  p#UR                  (       d  M  UR                  [        R                  R                  SU S3S.5        MJ     [        R                  R                  U 5      nU Hq  nSU;   a0  UR                  [        R                  R                  S	U S
3S.5        SU;   d  MA  UR                  [        R                   R                  S	U S3S.5        Ms     U$ )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,   r-   r.   r/   r0   r#   appendr   r   rG   named_parametersrequires_gradr   export_opnamesr   r   )r   	lint_listrG   paramop_namesop_names         r   generate_mobile_module_lintsrS   L   ss    mUYY%;%;<<4&''FGI 	I I="HII("8"8"="= Ky z 	{ %557h&<&<&A&APVW[V\ ]1 O1 2 3 8 yy''6Hh&6&6&;&;+4WI >* **+ , 7"h&8&8&=&=+4WI >* **+ ,  r   c                 b   / n[        U S5      (       a"  UR                  S5        UR                  S5        [        U S5      (       ah  UR                  S5        U R                  5       nU HA  nXA;  a  UR                  U5        UR                  SU-   5        UR                  SU-   5        MC     U$ )Nget_all_bundled_inputsget_num_bundled_inputs%get_bundled_inputs_functions_and_infoget_all_bundled_inputs_for__bundled_inputs_deflated_)r#   rK   rW   )r   r   rA   all_infofunction_names        r   r3   r3   u   s     "}677!(()AB!(()AB }EFF!(()PQ FFH%M5)00?%,,-J]-Z[%,,-H=-XY	 & %$r   )NNCPU)__doc__r,   enumr   torch._Cr   MobileOptimizerTypetypingr   r   r   r-   r.   r1   r5   r2   RecursiveScriptModulerD   rS   r3   r   r   r   <module>rc      s    Q   @ #t  FJ48	:Fyy--:F (-@)A B:F $DL1:F 	:F "'!@!@	:Fz'		0F0F 'R%EII<R<R %gklogp %uyz}u~ %r   