a
    –¥»aë  ã                   @   sT   d dl Z d dlZd dlmZ e jdd„ ƒZe jdd„ ƒZejjZ	dd„ Z
d	d
„ ZdS )é    N)ÚListc              	   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Ú_CZ_get_graph_executor_optimizeZ_set_graph_executor_optimize)Zshould_optimizeZstored_flag© r   úJ/home/yuhw/wc/pochoir/venv/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   Z_jit_can_fuse_on_cpuZ_jit_can_fuse_on_gpuZ_jit_texpr_fuser_enabledZ_jit_nvfuser_enabledZ_jit_override_can_fuse_on_cpuZ_jit_override_can_fuse_on_gpuZ_jit_set_texpr_fuser_enabledZ_jit_set_nvfuser_enabledZ_jit_set_profiling_executorZ_jit_set_profiling_modeÚ	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è   z¼|   ¡ }t|j ¡ ƒ}t|ƒdks(J ‚|d j ¡ }|d j ¡ }g }| 	¡ D ]}t
||ƒ qPt|ƒt|ƒkstJ ‚t||ƒD ]8\}}	t|	j ¡ ƒ}
t|
ƒdks¤J ‚| d|
d j¡ q~|W S  tyâ   | |i |¤Ž tƒ  Y S 0 d S )Né   r   ZSubgraph)Zget_debug_stateÚlistZexecution_plansÚvaluesÚlenÚgraphÚcopyÚcodeZ!differentiable_op_executor_statesr   r   ÚzipZg_r   Úlast_executed_optimized_graph)ÚselfÚargsÚkwargsZdbsZepsr   Z	fw_statesZ
diff_nodesr   ÚstateZfw_execution_plansr   r   r   Ú
_graph_forL   s$    r    )Ú
contextlibr   Útypingr   Úcontextmanagerr   r
   r   Z_last_executed_optimized_graphr   r   r    r   r   r   r   Ú<module>   s   

.