
    קg?*                     Z   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	Z	d dl
mZ d dl	mZ d dlmZ d dlmZ dd	lmZ dd
lmZ  ej        e          Z	 ed             Zed             Zed             Zed             Z ed          d             Zd Z eeed          Z  ede             eed          Z! ede!           d Z" ede"            ee          Z# ede#            G d de$          Z% G d d e$          Z&ed!e	j'        j(        fd"            Z)ed!e	j'        j(        fd#            Z*ed!e	j'        j(        fd$            Z+ed!e	j'        j(        fd%            Z,e j-         G d& d'                      Z.d!e	j'        j(        fd(Z/ G d) d*          Z0dS )+    N)import_module)AnyListOptional)#min_cut_rematerialization_partition)_guards)config)
ts_compile   )aot_autograd)register_debug_backendc                 J    |rt                               d|           | j        S )Nz&eager backend ignoring extra kwargs %s)logwarningforwardgmfake_tensor_inputskwargss      \/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/_dynamo/backends/debugging.pyeagerr      s(     F<fEEE:    c                 L     |rt                               d|            fd}|S )Nz/eager_noexcept backend ignoring extra kwargs %sc                  ~    	  |  S # t           $ r*}t          j        j                            d          |d }~ww xY w)Nz7Unexpected exception when running generated GraphModule)	Exceptiontorch_dynamoexcTorchDynamoException)argser   s     r   innerzeager_noexcept.<locals>.inner)   sV    	2t9 	 	 	-#88I 	s    
<%7<)r   r   )r   r   r   r"   s   `   r   eager_noexceptr#   "   sA     OEvNNN     Lr   c                      |rt                               d|           ddlm}  fd}  ||d          | }|                                 |S )Nz3pre_dispatch_eager backend ignoring extra kwargs %sr   )make_fxc                  P    t          j                                      j        |  S Nr   fxInterpreterrun)r    r   s    r   runnable_gmz'pre_dispatch_eager.<locals>.runnable_gm;   s!    x##B''+T22r   T)pre_dispatch)r   r   "torch.fx.experimental.proxy_tensorr%   print_readable)r   r   r   r%   r,   pre_dispatch_gms   `     r   pre_dispatch_eagerr1   4   s     SI6RRR::::::3 3 3 3 3 >ggk===?QRO""$$$r   c                 \     |rt                               d|           ddlm  fd}|S )Nz,eager_debug backend ignoring extra kwargs %sr   )SchemaCheckModec                                   5  t          j                                      j        |  cd d d            S # 1 swxY w Y   d S r'   r(   )r    r3   r   s    r   r"   zeager_debug.<locals>.innerN   s    _ 	7 	78''++/6	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   &?AA)r   r   #torch._subclasses.schema_check_moder3   )r   r   r   r"   r3   s   `   @r   eager_debugr6   D   sY     LBFKKKCCCCCC
7 7 7 7 7 7 Lr   ts)namec                 @    t           j                            |           S r'   )r   jitscript)r   r   s     r   torchscriptr<   U   s    9Br   c                        fd}d|_         |S )Nc                 h    t           j                                                          |           S r'   )r   r)   r*   	boxed_run)r    fx_gs    r   r+   zboxed_nop.<locals>.run\   s'    x##D))33D999r   T)_boxed_call)r@   example_inputsr+   s   `  r   	boxed_noprC   [   s*    : : : : : COJr   T)fw_compilerpartition_fnkeep_inference_input_mutations	aot_eager)r8   compiler_fn)rD   rF   aot_eager_default_partitionerc                    |rt                               d|           t          j        d          5   t	          t
          t
          d t          j        t          d                    | |          cd d d            S # 1 swxY w Y   d S )Nz;aot_eager_decomp_partition backend ignoring extra kwargs %sT)unlift_effect_tokensc                  D    t          d                                          S )Nztorch._inductor.compile_fx)r   select_decomp_table r   r   <lambda>z,aot_eager_decomp_partition.<locals>.<lambda>   s"    =,$ $!!## r   inductor)compiler)rD   bw_compilerdecompositionsrE   )	r   r   functorch_configpatchr   rC   	functoolspartialr   r   s      r   aot_eager_decomp_partitionrX   x   s     
I6	
 	
 	
 
	T	:	:	: " "
|!!$ $ #*3j  
 
 
  " "" " " " " " " " " " " " " " " " " "s   ABBBrX   )rD   aot_tsc                       e Zd ZdS )ReluCompileErrorN__name__
__module____qualname__rN   r   r   r[   r[              Dr   r[   c                       e Zd ZdS )TestingOnlyCompileErrorNr\   rN   r   r   rb   rb      r`   r   rb   r   c                 \    | j         j        D ]}|j        t          j        k    rt
          | S r'   )graphnodestargetr   relur[   r   rB   nodes      r   relu_compile_error_TESTING_ONLYrj      s4     # #;%*$$"" %Ir   c                     | j         j        D ]/}|j        t          j        k    rt          j        |_        d|_        0|                                  | S )N)FReluRuntimeError)rd   re   rf   r   rg   _assertr    	recompilerh   s      r   relu_runtime_error_TESTING_ONLYro      sG     4 4;%*$$-DK3DILLNNNIr   c                     | j         j        D ]<}|j        t          j        k    r%t          j        |_        |j        d         df|_        =|                                  | S )Nr   r   )rd   re   rf   r   rg   addr    rn   rh   s      r    relu_accuracy_error_TESTING_ONLYrr      sR     * *;%*$$)DK1q)DILLNNNIr   c                 h    | j         j        D ]}|j        dk    r n| S |D ]}|j        st          | S )Ncall_function)rd   re   opis_leafrb   )r   rB   ri   ts       r   #non_leaf_compile_error_TESTING_ONLYrx      s^       7o%%E & 	 * *y 	*))	*Ir   c                      e Zd ZU dZeej        j                 ed<   e	ed<   e	ed<   ee
         ed<   e	ed<   dZeeej        j                          ed<   dZeeej                          ed	<   dZee         ed
<   defdZdS )ExplainOutputzu
    This is the output of :func:`torch._dynamo.explain()`
    There is no reason to create this class directly.
    graphsgraph_countgraph_break_countbreak_reasonsop_countNops_per_graph
out_guardscompile_timesreturnc                 .   d| j          d}|d| j         dz  }|d| j         dz  }|dz  }t          | j                  D ]7\  }}|d|dz    dz  }|d	|j         dz  }|d
z  }|j        D ]}|d| dz  }8| j        9|dz  }t          | j                  D ]\  }}|d|dz    dz  }|D ]}|d| dz  } | j        @|dz  }t          | j                  D ]&\  }}|d|dz    dz  }|dt          |           z  }'| j
        |d| j
         dz  }|S )NzGraph Count: 
zGraph Break Count: z
Op Count: zBreak Reasons:
z  Break Reason r   z:
z    Reason: z    User Stack:
z      zOps per Graph:
z  Ops z    zOut Guards:
z  Guard zCompile Times: )r|   r}   r   	enumerater~   reason
user_stackr   r   strr   )	selfoutputidxbreak_reasonframe_summaryopsru   iguards	            r   __str__zExplainOutput.__str__   s   5!1555B(>BBBB0t}0000$$!*4+=!>!> 	5 	5C2A2222F<\%8<<<<F))F!-!8 5 54=44445 )((F%d&899 , ,S-3q5---- , ,BmRmmm+FF, ?&o%F%do66 . .5-QqS-----U---)>(:>>>>Fr   )r]   r^   r_   __doc__r   r   r)   GraphModule__annotations__intr   r   r   Noder   r   Guardr   r   r   rN   r   r   rz   rz      s          
 %&&&&    MMM37M8D/077704Jgm,-444#'M8C='''      r   rz   c                     |                     |            d | j        j        D             }|t          |          z  }|                     |           | j        j        r|                     | j                   | ||||fS )a  
    This function is a utility which processes a torch.fx.GraphModule and
    accumulates information about its ops, graph breaks, and other details. It
    is intended to be used by the ExplainWithBackend class and
    `torch._dynamo.explain()` to provide details from Dynamo's graph capture.

    Parameters:
        gm (torch.fx.GraphModule): The GraphModule to be processed.
        graphs (list): A list that accumulates all the GraphModules processed.
        op_count (int): The total count of operations in all GraphModules processed so far.
        ops_per_graph (list): A list that accumulates the operations of each GraphModule.
        break_reasons (list): A list that accumulates the reasons for breaks in each GraphModule.

    Returns:
        tuple: A tuple containing the processed GraphModule, the updated lists of graphs,
               operations per graph, and break reasons, and the updated operation count.
    c                 2    g | ]}|j         d k    |j        S )rt   )ru   rf   ).0ri   s     r   
<listcomp>z)_explain_graph_detail.<locals>.<listcomp>  s&    
P
P
P4TW5O5O4;5O5O5Or   )appendrd   re   lencompile_subgraph_reasongraph_break)r   r{   r   r   r~   r   s         r   _explain_graph_detailr     s    ( MM"
P
P28>
P
P
PCCH	!- 9R7888vx==r   c                   F    e Zd ZdZddZdej        j        fdZde	fdZ
dS )	ExplainWithBackenda  
    This class is intended to be used as a backend for `torch.compile`. It is
    composable with other backends. When used in this way, it accumulates
    information about graph breaks, ops, and other info and provides a string
    representation summarizing this information.

    Attributes:
        backend (str): The name of the backend to use for optimization.
        graphs (list): A list of the graphs captured by TorchDynamo.
        op_count (int): The total number of operations in all optimized graphs.
        break_reasons (list): A list of graph break reasons with stack traces.

    Example Usage:
        def fn(x):
            x = torch.sigmoid(x)
            return x

        torch._dynamo.reset()
        eb = ExplainWithBackend("inductor")
        optimized_fn = torch.compile(fn, backend=eb)
        result = optimized_fn(torch.randn(5))
        print(eb.output())
    r   Nc                 \    ddl m}  ||          | _        g | _        d| _        g | _        d S )Nr   )lookup_backendr   )registryr   backendr{   r   r~   )r   r   r   s      r   __init__zExplainWithBackend.__init__8  sA    ,,,,,,%~g..r   r   c                     t          || j        | j        g | j                  \  }| _        | _        }| _        |                     ||          S r'   )r   r{   r   r~   r   )r   r   rB   _s       r   __call__zExplainWithBackend.__call__@  sM    @UT]B0BA
 A
=DK4+= ||B///r   c                 x    t          | j                  }t          | j        ||dz
  | j        | j                  }|S )Nr   )r   r{   rz   r~   r   )r   r|   r   s      r   r   zExplainWithBackend.outputF  sA    $+&&K!OM
 
 r   )r   N)r]   r^   r_   r   r   r   r)   r   r   rz   r   rN   r   r   r   r     sm         0       058/ 0 0 0 0
 
 
 
 
 
 
r   r   )1dataclassesrV   logging	importlibr   typingr   r   r   r   functorch.compiler   r   torch._functorchr	   rT   torch._functorch.compilersr
   commonr   r   r   register_backend	getLoggerr]   r   r   r#   r1   r6   r<   rC   rG   rI   rX   rY   r   r[   rb   r)   r   rj   ro   rr   rx   	dataclassrz   r   r   rN   r   r   <module>r      s            # # # # # # & & & & & & & & & &  A A A A A A       7 7 7 7 7 7 1 1 1 1 1 1             @ @ @ @ @ @ g!!
      "        t     
   L4#'  	
  ky 9 9 9 9 ,$! ! !   	(6S   " " "*  	%3M    
*	-	-	-  hF 3 3 3 3	 	 	 	 	y 	 	 		 	 	 	 	i 	 	 	 (<     (<     )=     EH,@     - - - - - - - -`>> > > ><1 1 1 1 1 1 1 1 1 1r   