o
    úÇh«  ã                   @   sj   d dl Z d dlZe jdd„ ƒZe jdd„ ƒZejjZdd„ Zdd	„ Z	d
d„ Z
deeeef  fdd„ZdS )é    Nc              	   c   s@    t j ¡ }t j | ¡ zdV  W t j |¡ dS t j |¡ w )zlContext 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)Úshould_optimizeÚstored_flag© r   úd/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/jit/_fuser.pyÚoptimized_execution   s   €
r
   c              	   c   sœ   t j ¡ }t j ¡ }t j ¡ }t j ¡ }t j ¡ }| dkr=t j d¡ t j d¡ t j 	d¡ t j 
d¡ t j d¡ n¬| dkrlt j d¡}t j d¡}t j d¡ t j d¡ t j 	d¡ t j 
d¡ t j d¡ n}| dkrt j d¡ t j d¡ t j 	d¡ t j 
d¡ t j d¡ nZ| dkr¾t j d¡}t j d¡}t j d¡ t 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¡ ntd| › d	ƒ‚z4d
V  W | dv rþt j |¡ t j |¡ t j |¡ t j |¡ t j 	|¡ t j 
|¡ t j |¡ d
S | dv r/t j |¡ t j |¡ t j |¡ t j |¡ t j 	|¡ t j 
|¡ t j |¡ w )zúContext manager that facilitates switching between backend fusers.

    Valid names:
    * ``fuser0`` - enables only legacy fuser
    * ``fuser1`` - enables only NNC
    * ``fuser2`` - enables only nvFuser
    * ``fuser3`` - enables oneDNN Graph
    Úfuser0TFÚfuser1Úfuser2Úfuser3Únonez!unrecognized fuser option (name: ú)N)r   r   )r   r   Ú_jit_can_fuse_on_cpuÚ_jit_can_fuse_on_gpuÚ_jit_texpr_fuser_enabledÚ_jit_nvfuser_enabledÚ_jit_llga_enabledÚ_jit_override_can_fuse_on_cpuÚ_jit_override_can_fuse_on_gpuÚ_jit_set_texpr_fuser_enabledÚ_jit_set_nvfuser_enabledÚ_jit_set_llga_enabledÚ_jit_set_profiling_executorr   Ú	Exception)ÚnameÚold_cpu_fuseÚold_gpu_fuseÚold_texpr_fuser_stateÚold_nvfuser_stateÚold_llga_stateÚold_profiling_executorÚold_profiling_moder   r   r	   Úfuser   sv   €






ør%   c                 C   sD   |   ¡ dkr| | ¡ d S |  ¡ D ]}| ¡ D ]}t||ƒ qqd S )Nzprim::DifferentiableGraph)ÚkindÚappendÚblocksÚnodesÚ_get_differentiable_graph_node)ÚnodeÚ	diff_nodeÚblockÚnr   r   r	   r*   V   s   ÿÿr*   c                 O   s   t | | g|¢R i |¤ŽS )N)Ú_script_method_graph_for)ÚselfÚargsÚkwargsr   r   r	   Ú
_graph_for_   s   r3   c                 O   sà   z\|  ¡ }t|j ¡ ƒ}t|ƒdksJ ‚|d j ¡ }|d j ¡ }g }| 	¡ D ]}	t
|	|ƒ q(t|ƒt|ƒks:J ‚t||ƒD ]\}	}
t|
j ¡ ƒ}t|ƒdkrY|	 d|d j¡ q?|W S  tyo   | |i |¤Ž tƒ  Y S w )Né   r   ÚSubgraph)Úget_debug_stateÚlistÚexecution_plansÚvaluesÚlenÚgraphÚcopyÚcodeÚ!differentiable_op_executor_statesr)   r*   ÚzipÚg_r   Úlast_executed_optimized_graph)r0   Úparentr1   r2   ÚdbsÚepsr;   Ú	fw_statesÚ
diff_nodesr.   ÚstateÚfw_execution_plansr   r   r	   r/   c   s(   €
ür/   Ústrategyc                 C   s   t j | ¡S )a[  Set 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_strategy)rI   r   r   r	   Úset_fusion_strategy‚   s   rK   )Ú
contextlibr   Úcontextmanagerr
   r%   r   Ú_last_executed_optimized_graphrA   r*   r3   r/   r7   ÚtupleÚstrÚintrK   r   r   r   r	   Ú<module>   s   


@	