
    sg?*                     F   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	jN                  jP                  fd"       Z)ed!e	jN                  jP                  fd#       Z*ed!e	jN                  jP                  fd$       Z+ed!e	jN                  jP                  fd%       Z,e jZ                   G d& d'             Z.d!e	jN                  jP                  fd(Z/ G d) d*      Z0y)+    N)import_module)AnyListOptional)#min_cut_rematerialization_partition)_guards)config)
ts_compile   )aot_autograd)register_debug_backendc                 J    |rt         j                  d|       | j                  S )Nz&eager backend ignoring extra kwargs %s)logwarningforwardgmfake_tensor_inputskwargss      S/var/www/html/venv/lib/python3.12/site-packages/torch/_dynamo/backends/debugging.pyeagerr      s    <fE::    c                 B     |rt         j                  d|        fd}|S )Nz/eager_noexcept backend ignoring extra kwargs %sc                      	  |  S # t         $ r/}t        j                  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)   sE    	t9 	--##88I	s    	A *;A )r   r   )r   r   r   r"   s   `   r   eager_noexceptr#   "   s!    EvN Lr   c                      |rt         j                  d|       ddlm}  fd}  ||d      | }|j	                          |S )Nz3pre_dispatch_eager backend ignoring extra kwargs %sr   )make_fxc                  Z    t        j                  j                        j                  |  S Nr   fxInterpreterrun)r    r   s    r   runnable_gmz'pre_dispatch_eager.<locals>.runnable_gm;   s#    xx##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   sE    I6R:3 >gk=?QRO""$r   c                 R     |rt         j                  d|       ddlm  fd}|S )Nz,eager_debug backend ignoring extra kwargs %sr   )SchemaCheckModec                              5  t        j                  j                        j                  |  cd d d        S # 1 sw Y   y xY wr'   r(   )r    r3   r   s    r   r"   zeager_debug.<locals>.innerN   s;     	788''+//6	7 	7 	7s	   +>A)r   r   #torch._subclasses.schema_check_moder3   )r   r   r   r"   r3   s   `   @r   eager_debugr6   D   s$    BFKC
7 Lr   ts)namec                 @    t         j                  j                  |       S r'   )r   jitscript)r   r   s     r   torchscriptr<   U   s    99Br   c                        fd}d|_         |S )Nc                 `    t         j                  j                        j                  |       S r'   )r   r)   r*   	boxed_run)r    fx_gs    r   r+   zboxed_nop.<locals>.run\   s#    xx##D)33D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         j                  d|       t        j                  d      5   t	        t
        t
        d t        j                  t        d            | |      cd d d        S # 1 sw Y   y xY w)Nz;aot_eager_decomp_partition backend ignoring extra kwargs %sT)unlift_effect_tokensc                  4    t        d      j                         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   st    I6	
 
		T	: "
|!!$ #**3j
  "" " "s   8A11A:rX   )rD   aot_tsc                       e Zd Zy)ReluCompileErrorN__name__
__module____qualname__rN   r   r   r[   r[          r   r[   c                       e Zd Zy)TestingOnlyCompileErrorNr\   rN   r   r   rb   rb      r`   r   rb   r   c                     | j                   j                  D ]%  }|j                  t        j                  k(  s!t
         | S r'   )graphnodestargetr   relur[   r   rB   nodes      r   relu_compile_error_TESTING_ONLYrj      s6     #;;%**$""# Ir   c                     | j                   j                  D ]<  }|j                  t        j                  k(  s!t        j
                  |_        d|_        > | j                          | S )N)FReluRuntimeError)rd   re   rf   r   rg   _assertr    	recompilerh   s      r   relu_runtime_error_TESTING_ONLYro      sK     4;;%**$--DK3DI4 LLNIr   c                     | j                   j                  D ]K  }|j                  t        j                  k(  s!t        j
                  |_        |j                  d   df|_        M | j                          | S )Nr   r   )rd   re   rf   r   rg   addr    rn   rh   s      r    relu_accuracy_error_TESTING_ONLYrr      sX     *;;%**$))DK1q)DI* LLNIr   c                     | j                   j                  D ]  }|j                  dk(  s n | S |D ]  }|j                  rt         | S )Ncall_function)rd   re   opis_leafrb   )r   rB   ri   ts       r   #non_leaf_compile_error_TESTING_ONLYrx      sT      77o% 	 *yy))* 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y)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 ]C  \  }}|d|dz    dz  }|d	|j
                   dz  }|d
z  }|j                  D ]  }|d| dz  } E | j                  >|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    !1!1 2"5'(>(>'?rBBJt}}oR00$$!*4+=+=!> 	5CAwc22F\%8%8$9<<F))F!-!8!8 5F=/445		5 )((F%d&8&89 ,SF3q5'-- ,BRDm+F,,
 ??&o%F%doo6 .5HQqSE--DU--. )(:(:';2>>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    
 %%&&  M37M8D/0704Jgmm,-4#'M8C=' r   rz   c                 V   |j                  |        | j                  j                  D cg c]  }|j                  dk(  s|j                    }}|t        |      z  }|j                  |       | j                  j                  r|j                  | j                         | ||||fS c c}w )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.
    rt   )appendrd   re   ru   rf   lencompile_subgraph_reasongraph_break)r   r{   r   r   r~   ri   r   s          r   _explain_graph_detailr     s    ( MM"#%88>>
P4TWW5O4;;
PC
PCH	!!--R778vx== Qs
   B&B&c                   X    e Zd ZdZddZdej                  j                  fdZde	fdZ
y)	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                 T    ddl m}  ||      | _        g | _        d| _        g | _        y )Nr   )lookup_backendr   )registryr   backendr{   r   r~   )r   r   r   s      r   __init__zExplainWithBackend.__init__8  s'    ,%g.r   r   c                     t        || j                  | j                  g | j                        \  }| _        | _        }| _        | j	                  ||      S r'   )r   r{   r   r~   r   )r   r   rB   _s       r   __call__zExplainWithBackend.__call__@  sK    @UT]]B0B0BA
=DK4+= ||B//r   c                     t        | j                        }t        | j                  ||dz
  | j                  | j                        }|S )Nr   )r   r{   rz   r~   r   )r   r|   r   s      r   r   zExplainWithBackend.outputF  sB    $++&KK!OMM
 r   )r   N)r]   r^   r_   r   r   r   r)   r   r   rz   r   rN   r   r   r   r     s.    0 0588// 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  7 1   @ g!
    "      t   
 4#'	
 ky 9 ,$!  	(6S"* 	%3M 
*	- hF 3	y 		i 	 (<(<   (<(<   )=)=   EHH,@,@   - - -`>><1 1r   