o
    i                     @   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d	ejjd
ee	e  dee
e  dedejjf
ddZd	ejjfddZd	ejjde
e de
e f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   k/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/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u rt }|du rg }dd |D }t |}t fdd|D r=t	t|| }g }|D ]}t
 |sM|| qA|rZtdd	| | }|d
krltj j||}n|dkrztj j||}n|dkrt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_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   sL   


r7   c                 C   s   t | tjjstdt|  dg }t| ds"|tj	j
dd |  D ]\}}|jr;|tjj
d| dd q&tj| }|D ]$}d|v rW|tjj
d	|d d
|v rh|tjj
d|d qD|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_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.ZdropoutzOperator {} exists, remember to call eval() before saving the module.and call torch.utils.mobile_optimizer.optimize_for_mobile to drop dropout operator.Z
batch_normzOperator {} 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   r8   Znamed_parametersZrequires_gradr   Zexport_opnamesr   formatr   )r   Z	lint_listr8   paramZop_namesZop_namer   r   r   generate_mobile_module_lintsN   s.   
r<   c                 C   sz   g }t | dr|d |d t | dr;|d |  }|D ]}||vr,|| |d|  |d|  q!|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.   s   s   





r.   )NNr   )__doc__r(   enumr   Ztorch._Cr   ZMobileOptimizerTypetypingr   r   r   r   r   r)   r*   r   ZRecursiveScriptModuler7   r<   r.   r   r   r   r   <module>   s,    


>&%