a
    HSic=  ã                   @   sz   d dl Z d dlZd dlmZmZ e jdd„ ƒZe jdd„ ƒZejj	Z
dd„ Zd	d
„ Zdd„ Zeeeef  dœdd„ZdS )é    N)ÚListÚTuplec              	   c   s@   t j ¡ }t j | ¡ zdV  W t j |¡ nt j |¡ 0 dS )z|
    A context manager that controls whether the JIT's executor will run
    optimizations before executing a function.
    N)ÚtorchÚ_CÚ_get_graph_executor_optimizeÚ_set_graph_executor_optimize)Zshould_optimizeZstored_flag© r   úL/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/jit/_fuser.pyÚoptimized_execution   s
    
r
   c              	   c   s¬  t j ¡ }t j ¡ }t j ¡ }t j ¡ }| dkrbt j d¡ t j d¡ t j d¡ t j 	d¡ n”| dkr´t j 
d¡}t j d¡}t j d¡ t j d¡ t j d¡ t j 	d¡ nB| dkrît j d¡ t j d¡ t j d¡ t j 	d¡ ntdƒ‚z\dV  W | dkr"t j 
|¡ t j |¡ t j |¡ t j |¡ t j |¡ t j 	|¡ nT| dkrvt j 
|¡ t j |¡ t j |¡ t j |¡ t j |¡ t j 	|¡ 0 dS )zÝ
    A context manager that facilitates switching between
    backend fusers.

    Valid names:
    * ``fuser0`` - enables only legacy fuser
    * ``fuser1`` - enables only NNC
    * ``fuser2`` - enables only nvFuser
    Zfuser0TFZfuser1Zfuser2zunrecognized fuser optionN)r   r   Ú_jit_can_fuse_on_cpuÚ_jit_can_fuse_on_gpuÚ_jit_texpr_fuser_enabledÚ_jit_nvfuser_enabledÚ_jit_override_can_fuse_on_cpuÚ_jit_override_can_fuse_on_gpuÚ_jit_set_texpr_fuser_enabledÚ_jit_set_nvfuser_enabledÚ_jit_set_profiling_executorr   Ú	Exception)ÚnameZold_cpu_fuseZold_gpu_fuseZold_texpr_fuser_stateZold_nvfuser_stateZold_profiling_executorZold_profiling_moder   r   r	   Úfuser   sL    




ù
r   c                 C   sB   |   ¡ dkr| | ¡ n&|  ¡ D ]}| ¡ D ]}t||ƒ q,q d S )Nzprim::DifferentiableGraph)ÚkindÚappendÚblocksÚnodesÚ_get_differentiable_graph_node)ÚnodeZ	diff_nodeÚblockÚnr   r   r	   r   D   s
    r   c                 O   s   t | | g|¢R i |¤ŽS )N)Ú_script_method_graph_for)ÚselfÚargsÚkwargsr   r   r	   Ú
_graph_forL   s    r#   c                 O   sä   z¸|  ¡ }t|j ¡ ƒ}t|ƒdks(J ‚|d j ¡ }|d j ¡ }g }| 	¡ D ]}	t
|	|ƒ qPt|ƒt|ƒkstJ ‚t||ƒD ]4\}	}
t|
j ¡ ƒ}t|ƒdkr~|	 d|d j¡ q~|W S  tyÞ   | |i |¤Ž tƒ  Y S 0 d S )Né   r   ZSubgraph)Úget_debug_stateÚlistÚexecution_plansÚvaluesÚlenÚgraphÚcopyÚcodeÚ!differentiable_op_executor_statesr   r   ÚzipÚg_r   Úlast_executed_optimized_graph)r    Úparentr!   r"   ZdbsÚepsr*   Z	fw_statesZ
diff_nodesr   ÚstateZfw_execution_plansr   r   r	   r   O   s$    r   ©Ústrategyc                 C   s   t j | ¡S )aa  
    Sets the type and number of specializations that can occur during fusion.

    Usage: provide a list of pairs (type, depth) where type is one of "STATIC" or "DYNAMIC"
    and depth is an integer.

    Behavior - static vs dynamic:
        In STATIC fusion, fused ops are compiled to have fixed input shapes. The shape is determined
        based on some initial profiling runs.
        In DYNAMIC fusion, fused ops are compiled to have variable input shapes, so that multiple
        shapes are possible.

    In both cases, we also recompile on new striding behavior, device, or dtype.

    Behavior - fallback functions & depth:
        When an input doesn't match the format required by the specialized compiled op, it will run
        a fallback function. Fallback functions are recursively be compiled and specialized based
        on the observed tensor shapes. Since compilation can be slow, the "depth" parameter is provided to
        limit the number of specializations that can be compiled, before giving up on recompiling and
        falling back to a completely un-fused, un-specialized implementation.

    The list of (type, depth) pairs controls the type of specializations and the number of
    specializations. For example: [("STATIC", 2), ("DYNAMIC", 2)] indicates that the first
    two specializations will use static fusions, the following two specializations will use
    dynamic fusion, and any inputs that satisfy none of the 4 options will run an
    unfused implementation.

    NB: in the future, if more as more fusion backends are added there may be more granular
    apis for specific fusers.
    )r   r   Ú_jit_set_fusion_strategyr4   r   r   r	   Úset_fusion_strategym   s    r7   )Ú
contextlibr   Útypingr   r   Úcontextmanagerr
   r   r   Ú_last_executed_optimized_graphr0   r   r#   r   ÚstrÚintr7   r   r   r   r	   Ú<module>   s   

.