
    ֧g                         d dl Z d dlmZmZ d dlZe j        d             Ze j        d             Zej        j	        Z
d Zd Zd Zdeeeef                  fd	ZdS )
    N)ListTuplec              #     K   t           j                                        }t           j                            |            	 dV  t           j                            |           dS # t           j                            |           w xY 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_flags     L/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/jit/_fuser.pyoptimized_executionr      sw       (7799K	H))/:::;--k:::::--k::::s   A& &!Bc              #   6  K   t           j                                        }t           j                                        }t           j                                        }t           j                                        }t           j                                        }| dk    rt           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           t           j        
                    d           t           j                            d           n8| dk    rt           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           t           j        
                    d           t           j                            d           n| dk    rt           j                            d           t           j                            d           t           j        	                    d           t           j        
                    d           t           j                            d           nt          d|  d	          	 d
V  | 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 xY w)zContext 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_modes           r   fuserr)      s      80022L80022L!H==??5577X//11Nx..t444..t444--e444))%000&&u----			!&!E!Ed!K!K"XBB4HH..t444..t444--d333))%000&&u----			..u555..u555--e444))$///&&u----			!&!E!Ed!K!K"XBB4HH..t444..u555--d333))%000&&t,,,,	..u555..u555--e444))%000&&u----CDCCCDDD7'''H001GHHHH112DEEE..|<<<..|<<<--.CDDD))*;<<<&&~66666 '''H001GHHHH112DEEE..|<<<..|<<<--.CDDD))*;<<<&&~6666s   T9 9CXc                     |                                  dk    r|                    |            d S |                                 D ])}|                                D ]}t	          ||           *d S )Nzprim::DifferentiableGraph)kindappendblocksnodes_get_differentiable_graph_node)node	diff_nodeblockns       r   r/   r/   W   s    yy{{111[[]] 	= 	=E[[]] = =.q)<<<<=	= 	=    c                 $    t          | | g|R i |S )N)_script_method_graph_for)selfargskwargss      r   
_graph_forr:   `   s"    #D$@@@@@@@r4   c                    	 |                                 }t          |j                                                  }t	          |          dk    sJ |d         j                                        }|d         j                                        }g }|	                                D ]}	t          |	|           t	          |          t	          |          k    sJ t          ||          D ]_\  }	}
t          |
j                                                  }t	          |          dk    r!|	                    d|d         j                   `|S # t          $ r  | |i | t                      cY S w xY 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)r7   parentr8   r9   dbsepsrC   	fw_states
diff_nodesr3   statefw_execution_planss               r   r6   r6   d   sz   /$$&&3&--//003xx1}}}}A!!## FKAACC	*,
 	: 	:A*1j99999~~Z0000J	22 	> 	>HAu!%e&;&B&B&D&D!E!E %&&!++Z!3A!6!<=== / / / 	df,.....	/s   E	E  E/.E/strategyc                 @    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)rQ   s    r   set_fusion_strategyrT      s    < 8,,X666r4   )
contextlibtypingr   r   r   contextmanagerr   r)   r   _last_executed_optimized_graphrI   r/   r:   r6   strintrT    r4   r   <module>r\      s                 ; ; ; =7 =7 =7@ !& G = = =A A A/ / />7$uS#X"7 7 7 7 7 7 7r4   