
    Vh<                        d 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 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d Zd Zed        Zed        Zed        Z ed      d        Z d Z!d Z"d8dZ#de	jH                  jJ                  de&fdZ'd Z(	 	 d9dZ) ede)        ee!d       Z* ed!e*       d" Z+ ed#e+       d$ Z, ed%e,       d& Z- ed'e-        ee(      Z. ed)e.        G d* d+e/      Z0 G d, d-e/      Z1ed.e	jd                  jf                  fd/       Z4ed.e	jd                  jf                  fd0       Z5ed.e	jd                  jf                  fd1       Z6ed.e	jd                  jf                  fd2       Z7ejp                   G d3 d4             Z9d.e	jd                  jf                  fd5Z: G d6 d7      Z;y):aD  
This module provides debugging backends for TorchDynamo to help diagnose and troubleshoot
compilation and execution issues. It includes:

Key Debugging Backends:
- eager: Simple pass-through backend that runs models in eager mode
- eager_noexcept: Similar to eager but with additional exception handling
- eager_debug: Adds schema validation checks for custom operators
- aot_eager: Uses AOT Autograd with nop compiler for debugging
- aot_eager_decomp_partition: Uses TorchInductor decompositions for debugging
- torchscript: Compiles using TorchScript for debugging JIT-related issues

Testing and Development Tools:
- Backends for inducing specific errors (compile/runtime/accuracy)
- ExplainOutput class for detailed graph compilation analysis
- Utilities for cross-referencing and mode management
- Tools for graph detail inspection and break reason analysis

These backends are primarily used for:
1. Debugging graph breaks and compilation failures
2. Testing error handling and recovery mechanisms
3. Analyzing performance bottlenecks
4. Validating operator schemas and decompositions
    N)import_module)Any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      P/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/backends/debugging.pyeagerr   /   s    <fE::    c                     t        | g      S N),make_eager_backend_with_torch_function_modesmodes    r   +make_eager_backend_with_torch_function_moder   6   s    7??r   c                 "     ddl m  fd}|S )zUsed to trace HOPs (cond and while) for eager exectution, the metadata
    TF mode mutates vars outside of the scope of the HOP, and we can't have graph breaks
    in the HOP, so we need to externally run this mode and not trace it.r   )	ExitStackc                 ~            }D ]  }|j                  |        | j                  }|j                          |S r   )enter_contextr   close)r   r   r   stackr   resultr   modess         r   fnz8make_eager_backend_with_torch_function_modes.<locals>.fn@   s@     	&D%	& r   )
contextlibr   )r$   r%   r   s   ` @r   r   r   :   s     % Ir   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>.innerS   sE    	t9 	--##88I	s    	A *;A )r   r   )r   r   r   r0   s   `   r   eager_noexceptr1   L   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 r   r*   fxInterpreterrun)r.   r   s    r   runnable_gmz'pre_dispatch_eager.<locals>.runnable_gme   s#    xx##B'++T22r   T)pre_dispatch)r   r   "torch.fx.experimental.proxy_tensorr3   print_readable)r   r   r   r3   r9   pre_dispatch_gms   `     r   pre_dispatch_eagerr>   ^   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   r5   )r.   r@   r   s    r   r0   zeager_debug.<locals>.innerx   s;     	788''+//6	7 	7 	7s	   +>A)r   r   #torch._subclasses.schema_check_moder@   )r   r   r   r0   r@   s   `   @r   eager_debugrC   n   s$    BFKC
7 Lr   ts)namec                 @    t         j                  j                  |       S r   )r*   jitscript)r   r   s     r   torchscriptrI      s    99Br   c                        fd}d|_         |S )Nc                 `    t         j                  j                        j                  |       S r   r*   r6   r7   	boxed_run)r.   fx_gs    r   r8   zboxed_nop.<locals>.run   s#    xx##D)33D99r   T_boxed_call)rN   example_inputsr8   s   `  r   	boxed_noprR      s    : COJr   c                $      fd}d|_         |S )Nc                     5  t         j                  j                        j                  |       cd d d        S # 1 sw Y   y xY wr   rL   )r.   rN   r   s    r   r8   z boxed_nop_with_mode.<locals>.run   s8     	>88''-77=	> 	> 	>s	   .<ATrO   )rN   rQ   r   r8   s   ` ` r   boxed_nop_with_moderU          > COJr   c                 $      fd}d|_         |S )Nc                     t         j                  j                        5  t         j                  j	                        j                  |       cd d d        S # 1 sw Y   y xY wr   )r*   _subclassesCrossRefFakeModer6   r7   rM   )r.   rN   ignore_op_fns    r   r8   z$fake_crossref_boxed_nop.<locals>.run   sI    //= 	>88''-77=	> 	> 	>s   .AA"TrO   )rN   rQ   r[   r8   s   ` ` r   fake_crossref_boxed_nopr\      rV   r   opreturnc                     | j                   dv S )N)atenprimsprim)	namespace)r]   s    r   ignore_builtinsrd      s    <<444r   c                  @   t         j                  j                  j                  st        S t         j                  j                  j                  dk(  rt
        S t         j                  j                  j                  dk(  sJ t        j                  t
        t              S )Nall
custom_ops)r[   )	r*   
_functorchr   fake_tensor_crossrefrR   r\   	functoolspartialrd    r   r   get_nop_funcrm      sq    ""77				 	 	5	5	>&&&&;;|KKK  !8WWr   c                 X     t        |xs t        |xs t        t        d      | |fi |S )NT)fw_compilerbw_compilerpartition_fnkeep_inference_input_mutations)r   rR   r   )r   r   ro   rp   r   s        r   	aot_eagerrs      s@    <,9,98'+	
 	
(
 !'( (r   rs   )rE   compiler_fnT)ro   rr   aot_eager_default_partitionerc                 d   |rt         j                  d|       ddlm} ddi}|j	                  d      x}r|j                  |       t        j                  |      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 %sr   )CompilerBisectorunlift_effect_tokensTaot_eager_decomp_partitionc                  4    t        d      j                         S Nztorch._inductor.compile_fxr   select_decomp_tablerl   r   r   <lambda>z,aot_eager_decomp_partition.<locals>.<lambda>   s    =,$!!# r   inductorcompilerro   rp   decompositionsrq   )r   r   !torch._inductor.compiler_bisectorrw   get_config_changeupdatefunctorch_configpatchr   rm   rj   rk   r   )r   r   r   rw   config_patchesbisect_changess         r   ry   ry      s    I6	
 C,d3N);;$ ~  	n-				/ "
|$$$ #**3j
  "" " "s   A B&&B/ry   c           
           t        t        j                  t        |      t        j                  t        |      d t        j                  t        d            | |      S )Nr   c                  4    t        d      j                         S r{   r|   rl   r   r   r~   z6aot_eager_decomp_partition_with_mode.<locals>.<lambda>   s    }( 



 r   r   r   r   )r   rj   rk   rU   r   )r   r   r   kwargs       r   $aot_eager_decomp_partition_with_moder      s\    <%%&9E%%&9E  &&/*
 	
 r   r   c                     t         j                  sdnt         j                  }t        j                  |      5  t        | |fi |cd d d        S # 1 sw Y   y xY w)Nrg   )ri   )r   ri   r   ry   )r   r   r   
config_vals       r   #aot_eager_decomp_partition_crossrefr     sY      44 	22 
 
		Z	@ L)".@KFKL L Ls   AAr   )ro   aot_tsc                       e Zd Zy)ReluCompileErrorN__name__
__module____qualname__rl   r   r   r   r   "      r   r   c                       e Zd Zy)TestingOnlyCompileErrorNr   rl   r   r   r   r   &  r   r   r   r   c                     | j                   j                  D ]%  }|j                  t        j                  k(  s!t
         | S r   )graphnodestargetr*   relur   r   rQ   nodes      r   relu_compile_error_TESTING_ONLYr   *  s6     #;;%**$""# Ir   c                     | j                   j                  D ]<  }|j                  t        j                  k(  s!t        j
                  |_        d|_        > | j                          | S )N)FReluRuntimeError)r   r   r   r*   r   _assertr.   	recompiler   s      r   relu_runtime_error_TESTING_ONLYr   2  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
   )r   r   r   r*   r   addr.   r   r   s      r    relu_accuracy_error_TESTING_ONLYr   <  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)r   r   r]   is_leafr   )r   rQ   r   ts       r   #non_leaf_compile_error_TESTING_ONLYr   G  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_timesr^   c                    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opsr]   iguards	            r   __str__zExplainOutput.__str__h  s    !1!1 2"5'(>(>'?rBBJt}}oR00$$!*4+=+=!> 	5Cay44F\%8%8$9<<F))F!-!8!8 5F=/445		5 )((F%d&8&89 ,SF37)3// ,BRDm+F,,
 ??&o%F%doo6 .5HQUG3//DU--. )(:(:';2>>Fr   )r   r   r   __doc__listr*   r6   GraphModule__annotations__intr   r   r   Noder   r   Guardr   r   r   rl   r   r   r   r   V  s    
 %%&&  M37M8D/0704Jgmm,-4#'M8C=' r   r   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.
    r   )appendr   r   r]   r   lencompile_subgraph_reasongraph_break)r   r   r   r   r   r   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__  s'    ,%g.r   r   c                     t        || j                  | j                  g | j                        \  }| _        | _        }| _        | j	                  ||      S r   )r   r   r   r   r   )r   r   rQ   _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   r   r   r   )r   r   r   s      r   r   zExplainWithBackend.output  sB    $++&KK!OMM
 r   )r^   N)r   r   r   r   r   r*   r6   r   r   r   r   rl   r   r   r   r     s.    0 0588// 0
 
r   r   r   )NN)<r   dataclassesrj   logging	importlibr   typingr   r   r*   functorch.compiler   r   torch._functorchr   r   torch._functorch.compilersr	   commonr   r   r   register_backend	getLoggerr   r   r   r   r   r1   r>   rC   rI   rR   rU   r\   _ops
OpOverloadboolrd   rm   rs   ru   ry   r   r   r   r)   r   r   r6   r   r   r   r   r   	dataclassr   r   r   rl   r   r   <module>r      sf  2    #    A  7 1   @ g!  @$  "      t   
5

-- 5$ 5X 	( ky 9 ,$!  	(6S": 	%3M 	/4
L 	.3 
*	- hF 3	y 		i 	 (<(<   (<(<   )=)=   EHH,@,@   - - -`>><1 1r   