
    i                        U d Z ddlZddlZddlZddl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mZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZmZmZ dd
lmZ ddl m!Z! erddl"m#Z# ddl$m%Z%  ejL                  e'      Z(ddgZ)dZ*g a+e,e	   e-d<   g a.e,e	   e-d<   g a/e,e	   e-d<   da0da1da2da3de4fdZ5de4fdZ6de4fdZ7de4fdZ8de4fdZ9 G d d      Z:dCde4fdZ;dCde4fdZ<dDdejz                  d e>dejz                  e?z  fd!Z@d" ZAdDdejz                  d e>dejz                  eBz  fd#ZCde4fd$ZDd%e4de4dz  fd&ZEde4dz  fd'ZFde4fd(ZGdaHd) ZI G d* d+      ZJ G d, d-eJ      ZK G d. d/eJ      ZL G d0 d1eJ      ZM G d2 d3eJ      ZN G d4 d5eJ      ZOd6 ZPd7eJddfd8ZQd7eJddfd9ZRd7eJde4fd:ZSej                  j                  d;d<=      d>e4ddfd?       ZVeVj                  d>e4ddfd@       ZX G dA de      ZYdeYdz  fdBZZy)Ea  
DebugMode is a debugging TorchDispatchMode that intercepts and logs runtime calls
to a hierarchical string dump. It logs real tensor, DTensor, and optionally FakeTensor
operations, with some additional handling for DTensor internals.

An example dump from an eager mode DTensor matmul:

    torch.mm(dt$0: f32[8, 8]| S(0), dt$1: f32[8, 32]| S(0))  ->  dt$6: f32[8, 32]| S(0)
      aten::mm(dt$0: f32[8, 8]| S(0), dt$1: f32[8, 32]| S(0))
        redistribute_input(1, S(0) -> R)
          redistribute_input(t$2: f32[1, 32], trace: S(0)->R)
            _c10d_functional::all_gather_into_tensor(t$2: f32[1, 32], 8, 0)  ->  t$3: f32[8, 32]
            _c10d_functional::wait_tensor(t$3: f32[8, 32])  ->  t$3: f32[8, 32]
        aten::mm(t$4: f32[1, 8], t$3: f32[8, 32])  ->  t$5: f32[1, 32]

This mode runs "under" compile, which means it hides itself during compilation, and is re-enabled
at runtime, and DebugMode-related operations won't show up in the compiled region.
DebugMode also provides some visibility into non-torch-dispatch calls (e.g. DTensor redistribute calls,
inductor-generated triton kernels), but requires special handling for these, since dispatch modes
can't intercept them by default.

The mode also provides some extensions for custom debugging (e.g. adding custom dispatch call hooks
via dispatch_hooks), or numerics debugging (e.g. tensor hashing for bitwise equivalence/closeness,
via log_tensor_hashes). These decorators allow annotating string dumps with additional per-call information,
for any region of runtime code.

Usage::

    with DebugMode() as debug_mode:
        result = some_pytorch_operation(tensor_input)
    print(debug_mode.debug_string())
    N)Callable)AnyTYPE_CHECKING)
FakeTensorFakeTensorMode)_parse_stack_trace)dtype_abbrs)_get_current_dispatch_mode _get_current_dispatch_mode_stackTorchDispatchMode)keystrtree_alltree_maptree_map_with_path)CapturedTraceback)	WeakIdRef)DeviceInterface
ModTracker	DebugModeget_active_debug_moderedistribute_input_DISPATCH_RECORD_HOOKS_DISPATCH_LOG_HOOKS_DISPATCH_PRE_LOG_HOOKSFreturnc           	      d    ddj                  | D cg c]  }t        |       c}       dS c c}w N[, ]joinstr)shapexs     Q/var/www/html/engine/venv/lib/python3.12/site-packages/torch/utils/_debug_mode.py_stringify_shaper(   V   s-    tyy%0Q#a&012!440   -
c           	      x    ddj                  | j                  D cg c]  }t        |       c}       dS c c}w )NzDM(r    ))r#   r%   r$   )meshss     r'   _stringify_device_meshr.   Z   s1    DJJ7qCF789;;7s   7
c           	      d    ddj                  | D cg c]  }t        |       c}       dS c c}w r   r"   )	placementps     r'   _stringify_placementr2   ^   s-    tyy)4Q#a&456a884r)   c           
          i }|D ]  }t        | |      st        | |      ||<     t        |      dk(  ryddj                  |j	                         D cg c]  \  }}| d|  c}}       dS c c}}w )Nr    {r    =})hasattrgetattrlenr#   items)tensor
attributespairsattrkvs         r'   _stringify_attributesrB   b   s{    E 064 !&$/E$K0 5zQ		%++-@$!QaS!:@AB"EE@s   A0c                 Z    ddl m} |j                  | j                  | j                        S )Nr   DTensorSpec)&torch.distributed.tensor._dtensor_specrE   format_shard_order_str
placementsshard_order)specrE   s     r'   _stringify_dtensor_specrK   l   s"    B--doot?O?OPP    c                        e Zd ZddZdefdZy)TensorIdTrackerr   Nc                      i | _         d| _        y Nr   )tensor_memonext_tensor_idselfs    r'   __init__zTensorIdTracker.__init__s   s    13rL   c                 T    t         j                  j                         5  t        |      d fd}t	        j
                  ||        j                  vr. j                   j                  <    xj                  dz  c_         j                     cd d d        S # 1 sw Y   y xY w)Nc                  >    j                   j                   d        y N)rQ   pop)orT   s   r'   del_memoz%TensorIdTracker._id.<locals>.del_memo{   s      $$Q-rL      r   N)torch_C_DisablePythonDispatcherr   weakreffinalizerQ   rR   )rT   r<   r[   rZ   s   `  @r'   _idzTensorIdTracker._idw   s    XX..0 
	'&!A. VX.(((&*&9&9  ###q(###A&
	' 
	' 
	's   A3BB'r]   )__name__
__module____qualname__rU   intrc    rL   r'   rN   rN   r   s     'S 'rL   rN   c                    t        | t        j                        rt        | j                      t        | j                         t        | |       }|d|j                  |        nd}t        | t        j                  j                  j                        rd| d| dt        | j                         S t        | t              rd| d| S d| d| S t        dt!        |              )	z.Convert tensor to debug string representation.$r4   dt: z| fttzUnsupported tensor type: )
isinstancer^   Tensorr	   dtyper(   r%   rB   rc   distributedr<   DTensorrK   _specr   RuntimeErrortype)r<   r=   rQ   tensor_debug_strid_strs        r'   _tensor_debug_stringry      s     &%,,')&,,789I&,,9W8XYnou  xB  ZC  YD  E2=2I1[__V,-.rfe//66>>?xr"2!326Mfll6[5\]]
+xr"2!344vhb!1 2336tF|nEFFrL   c                 P    ddl m fd}t        ||       } t        |       S )Nr   rD   c                     t        | t        j                        rt        |       S t        |       rt	        |       S | S rX   )ro   r^   rp   ry   rK   )r&   rE   r=   rQ   s    r'   to_strz_arg_to_str.<locals>.to_str   s:    a&':{CC;'*1--rL   )rF   rE   r   r$   )argr=   rQ   r|   rE   s    `` @r'   _arg_to_strr~      s"    B 63
Cs8OrL   rn   
use_scalarc                    t         j                  j                         5  | j                         s | j	                         s| j                         } | j                         } | j	                         r!| j                  t         j                        }n | j                  t         j                        }|j                  d      }|r|j                         cddd       S |cddd       S # 1 sw Y   yxY w)a.  
    from Observer. Computes a hash for a tensor by converting it to float (if needed), making it contiguous,
    replacing NaN/inf values with fixed numbers, and then computing the L1 norm in float64 or complex128.
    This is used to generate a deterministic summary value for tensor comparison.
    rq   r\   )r1   N)r^   r_   r`   is_floating_point
is_complexfloat
contiguousto
complex128float64normitem)rn   r   t_floatouts       r'   norm_hash_fnr      s     
	*	*	, ##%	ALLN<<>dd!1!1d2Gddd/GllQl88:    s   B5C)C))C2c                 f    t        | |z
        }t        t        |       t        |      d      }||z  S )Ng|=)absmax)hash1hash2	numeratordenominators       r'   _compute_rel_diffr      s1    EEM"Ic%j#e*e4K{""rL   c                    t        | t        j                  j                  j                        r| j                         } | j                         r!| j                  t        j                        }nn| j                         r>| j                  t        j                        j                  t        j                        }n | j                  t        j                        }| j                         dkD  rt        j                  |      }n0t        j                  d|j                   t        j"                        }|r|j%                         S |S )z(
    wrapper over torch.hash_tensor
    r   r   rh   )devicerq   )ro   r^   rr   r<   rs   to_localr   r   r   r   r   viewint64numelhash_tensorzerosr   uint64r   )rn   r   t_cleanr   s       r'   hash_tensor_fnr      s     !U&&--556JJL$$U]]$+	
$$U--$.33EMMB$$U[[$)wwy1}(kk"W^^5<<HxxzJrL   c                  (   ddl m}  t        j                         j	                         }|d d }|D cg c]  }|j
                   |        vs| }}t        j                  j                  |      }dj                  |j                               S c c}w )Nr   )uninteresting_filesr4   )%torch.fx.experimental.symbolic_shapesr   r   extractsummaryfilename	tracebackStackSummary	from_listr#   format)r   r   frames      r'   _get_stack_tracer      s    I'')113GcrlG"enn<O<Q&QG  $$..w7G777>>#$$	s   BBstack_trace_strc                    t         j                  j                  t        j                  t
                    fd}t        | |      }|r6d|j                   d|j                   d|j                   d|j                   S y )Nc                 ^    | j                  t        j                  j                  z          S rX   )
startswithospathsep)filenamecode	torch_dirs      r'   <lambda>z'_get_user_stack_trace.<locals>.<lambda>   s     T__Y=T-U)U rL   )	filter_fnzFile: :z in z, code: )r   r   dirnameinspectgetfiler^   r   r   linenor   r   )r   r   tracer   s      @r'   _get_user_stack_tracer      sd     67IUI)DE

|1U\\N$uzzl(5::,WWrL   c                      t         j                  j                         Jt         j                  j                         j                  j	                  d      } | rdj                  |       S y )N
traceback_r4   )r^   r_   _current_autograd_nodemetadatagetr#   )tbs    r'   _maybe_get_autograd_tracer      sL    xx&&(4XX,,.77;;LI772;rL   c                     t        | t        j                  j                        r| j                  }|S t        | d      r)t        | d      r| j                   d| j                   }|S t        |       }|S )Nre   rd   .)	ro   r^   _ops
OpOverloadrf   r8   re   rd   r$   )opop_names     r'   _get_op_namer      sj    "ejj++,//
 N	 
\	"wr:'>]]O1R[[M2 N b'NrL   c                     t         st        j                  j                  t        j
                        t        _        ddlm}  | j                  t        j                  j                  j                  j                         ddlm} ddlm  |t        j                  j                  j                        dt"        ddffd       }d	a yy)
zz
    Lazily apply dont_skip_tracing decorator to DebugMode._annotate, to avoid circular import/initialization issues.
    r   )_side_effectful_functions)register_lowering)warning_oncetagr   Nc                       t         d       y )Nz7DebugMode._annotate() is a no-op for backend="inductor")log)r   r   s    r'   _annotate_loweringz6_ensure_annotate_decorated.<locals>._annotate_lowering  s    WXrL   T)_annotate_decoratedr^   _dynamodont_skip_tracingr   	_annotatetorch.fx.noder   addopsdebug_mode_opsannotatedefaulttorch._inductor.loweringr   torch._loggingr   r$   )r   r   r   r   s      @r'   _ensure_annotate_decoratedr     s    
 #mm==i>Q>QR	 	<!%%eii&>&>&G&G&O&OP 	?/	59933<<	=	C 	D 	 
>	 ## rL   c                       e Zd ZdZ	 	 	 ddedeeef   dz  deeef   dz  deddf
dZ		 dd	e
e   d
edz  ddfdZ	 dded	e
e   d
edz  ddfdZd	e
e   defdZdefdZy)
_DebugCallz3Base class for tracking operator calls in DebugModeN
call_depthrecordr   stackr   c                 |    || _         |rt               | _        t               | _        || _        || _        d | _        y rX   )r   r   stack_tracer   fwd_stack_tracer   r   
output_str)rT   r   r   r   r   s        r'   rU   z_DebugCall.__init__   s;     %/1D#<#>D  &*rL   r=   rQ   c                     t        d      )z
        To reduce memory consumption, this method stringifies args/kwargs, stores the result, and deletes original args/kwargs.
        z9Subclasses must implement stringify_args(), even if no-opNotImplementedErrorrT   r=   rQ   s      r'   stringify_argsz_DebugCall.stringify_args1  s     "G
 	
rL   outputc                 j    t        d |      ryt        fd|      }dt        |       | _        y)z;Store stringified version of call output in self.output_strc                 
    | d u S rX   rh   r&   s    r'   r   z-_DebugCall.stringify_output.<locals>.<lambda>B  s
    a4i rL   Nc                     t        |       S rX   r~   )r&   r=   rQ   s    r'   r   z-_DebugCall.stringify_output.<locals>.<lambda>D  s    Az;(O rL   z  ->  )r   r   r$   r   )rT   r   r=   rQ   r   s     `` r'   stringify_outputz_DebugCall.stringify_output;  s4     '0OQWX
"3z?"34rL   c                     t        d      )Nz)Subclasses must implement string render()r   rT   r=   s     r'   renderz_DebugCall.renderG  s    !"MNNrL   c                 $    | j                  g       S rX   )r   rS   s    r'   __repr__z_DebugCall.__repr__J  s    {{2rL   )NNFrX   )rd   re   rf   __doc__rg   dictr$   r   boolrU   listrN   r   r   r   r   rh   rL   r'   r   r     s    =
 )-%)++ S#X%+ #s(^d"	+
 + 
+$ LP
s)
2AD2H
	
 /3	
5
5 I
5 %t+	
5
 

5Oc Os O# rL   r   c                   |     e Zd ZdZ	 ddededededdf
 fdZ	 dd	e	e
   d
edz  ddfdZd	e	e
   de
fdZd Z xZS )_OpCallzNormal operator callargskwargsr   r   r   Nc                 n    t         |   ||       || _        || _        || _        d | _        d | _        y Nr   )superrU   r   r  r  args_str
kwargs_str)rT   r   r  r  r   r   	__class__s         r'   rU   z_OpCall.__init__Q  s:     	51	$(&*rL   r=   rQ   c                    dj                  fd| j                  D              | _        | j                  r<ddj                  fd| j                  j	                         D              z   | _        nd| _        | `| `y )Nr    c              3   8   K   | ]  }t        |        y wrX   r   ).0r}   r=   rQ   s     r'   	<genexpr>z)_OpCall.stringify_args.<locals>.<genexpr>d  s      "
:=KZ5"
s   c              3   H   K   | ]  \  }}| d t        |         ywr6   Nr   r  r@   rA   r=   rQ   s      r'   r  z)_OpCall.stringify_args.<locals>.<genexpr>h  s2      /Aq #Q{1j+>?@/   "r4   )r#   r  r  r  r;   r  r   s    ``r'   r   z_OpCall.stringify_argsa  ss     		 "
AE"
 
 ;;"TYY / KK--// & DO
 !DOIKrL   c                 .   | j                   | j                   }n$dj                  fd| j                  D              }| j                  | j                  }nD| j                  r6ddj                  fd| j                  j                         D              z   }nd}t        | j                  t        j                  j                        r| j                  j                  }nqt        | j                  d      rFt        | j                  d      r0| j                  j                   d| j                  j                   }nt        | j                        }| d| | d	}| j                   r|| j                   z  }| j"                  r|d
| j"                   z  }|S )Nr    c              3   6   K   | ]  }t        |        y wrX   r   )r  r}   r=   s     r'   r  z!_OpCall.render.<locals>.<genexpr>u  s      S#S*!= Ss   c              3   F   K   | ]  \  }}| d t        |         ywr  r   )r  r@   rA   r=   s      r'   r  z!_OpCall.render.<locals>.<genexpr>{  s-      .<@Aqqc;q*567.s   !r4   re   rd   r   (r+   z  # )r  r#   r  r  r  r;   ro   r   r^   r   r   rf   r8   re   rd   r$   r   r   )rT   r=   r  r  r   base_strs    `    r'   r   z_OpCall.renderq  s?   ==$}}Hyy S SSH??&J{{!DII .DHKKDUDUDW. % 
  
dgguzz445gg**GTWWl+0L++,Adgg.>.>-?@G$''lGYaz*Q7??'H88$txxj))HrL   c              #     K   | j                   7| j                  | j                   | j                  | j                  gE d {    y | j                  | j                  | j
                  | j                  gE d {    y 7 <7 wrX   )r  r   r  r   r  r  rS   s    r'   __iter__z_OpCall.__iter__  s^     ==$QQQDKKIII RIs#   >B A=6B7A?8B?BFrX   )rd   re   rf   r   tupler   rg   r  rU   r  r$   rN   r   r   r  __classcell__r  s   @r'   r  r  N  s     + + 	+
 + + 
+" LPs)2AD2H	 c s >JrL   r  c                   j     e Zd ZdZ	 d
	 d fdZ	 ddee   dedz  ddfdZdee   defdZ	d	 Z
 xZS )_RedistributeCallz'Redistribute call from DTensor dispatchr   Nc                 n    t         |   ||       || _        || _        || _        || _        d | _        y r  )r
  rU   r}   src_placementdst_placementtransform_info_strarg_str)rT   r}   r$  r%  r&  r   r   r  s          r'   rU   z_RedistributeCall.__init__  s=     	51**"4#'rL   r=   rQ   c                 B    t        | j                  ||       | _        | `y rX   )r~   r}   r'  r   s      r'   r   z _RedistributeCall.stringify_args  s      &dhh
KHIHrL   c                 X   | j                   | j                   }nt        | j                  |       }| j                  d| j                   }n3t        | j                  |      }t        | j
                  |      }| d| }t         d| d| d}| j                  r|| j                  z  }|S )Nztrace: z -> r  r    r+   )r'  r~   r}   r&  r$  r%  REDISTRIBUTE_FUNCr   )rT   r=   r'  placement_strsrc_placement_strdst_placement_strr  s          r'   r   z_RedistributeCall.render  s    <<#llG$TXXz:;G"".%d&=&=%>?M +D,>,>
 K +D,>,>
 K016G5HIM'('"]O1E??'HrL   c              #      K   | j                   | j                   }n| j                  }t         | j                  r|| j                  g n|| j                  | j
                  g i  | j                   y wrX   )r'  r}   r*  r&  r$  r%  r   )rT   r}   s     r'   r  z_RedistributeCall.__iter__  sj     <<#,,C((C""//00**D,>,>??oos   A9A;r  r]   rX   )rd   re   rf   r   rU   r  r$   rN   r   r   r  r  r   s   @r'   r"  r"    sc    1 ( 
($ LPs)2AD2H	c s $rL   r"  c            	       v     e Zd ZdZddedededdf fdZ	 ddee   d	e	dz  ddfd
Z
dee   defdZd Z xZS )_NNModuleCallz1Designates entering an nn.Module's forward methodmodule_namer   r   r   Nc                 6    t         |   ||       || _        y r  )r
  rU   r1  )rT   r1  r   r   r  s       r'   rU   z_NNModuleCall.__init__  s    51&rL   r=   rQ   c                      y rX   rh   r   s      r'   r   z_NNModuleCall.stringify_args  s     	rL   c                      d| j                    S )N	[nn.Mod] )r1  r   s     r'   r   z_NNModuleCall.render  s    4++,--rL   c              #   Z   K   d| j                    di | j                  gE d {    y 7 w)Nr5  rh   )r1  r   rS   s    r'   r  z_NNModuleCall.__iter__  s3     (()*OO	
 	
 	
s   !+)+r  rX   )rd   re   rf   r   r$   rg   r  rU   r  rN   r   r   r  r  r   s   @r'   r0  r0    sq    ;'C 'S ' 'RV '
 LPs)2AD2H	
.c .s .
rL   r0  c                        e Zd ZdZdedeeef   def fdZ	 dde	e   de
dz  d	dfd
Zde	e   d	efdZddZd Z xZS )_TritonKernelCallz Triton kernel call from Inductorkernel_namer  r   c                 j    t         |   |       || _        || _        d | _        d | _        d | _        y rX   )r
  rU   r9  r  r  
pre_hashespost_hashes)rT   r9  r  r   r  s       r'   rU   z_TritonKernelCall.__init__  s7     	$&&*1526rL   Nr=   rQ   r   c                 b   t         x}rS| j                  j                         D ci c](  \  }}t        |t        j
                        r	| ||      * c}}| _        | j                  r9dj                  fd| j                  j                         D              | _        y d| _        y c c}}w )Nr    c              3   H   K   | ]  \  }}| d t        |         ywr  r   r  s      r'   r  z3_TritonKernelCall.stringify_args.<locals>.<genexpr>
  s2      (Aq #Q{1j+>?@(r  r4   )	_TRITON_INPUT_HASH_FNr  r;   ro   r^   rp   r;  r#   r  )rT   r=   rQ   hash_fnr@   rA   s    ``   r'   r   z _TritonKernelCall.stringify_args  s    
 ,+7+ !KK--/Aqa. 71:DO ;;"ii ( KK--/( DO
 !DOs   -B+c                    d| j                    d| j                   d}| j                  rJdj                  d | j                  j	                         D              }dd| j
                  z  z   d| d	z   }nd
}| j                  rJdj                  d | j                  j	                         D              }dd| j
                  z  z   d| d	z   }nd
}| | | dS )Nz	[triton] r  r+   r    c              3   0   K   | ]  \  }}| d |   ywrl   Nrh   r  r@   rA   s      r'   r  z+_TritonKernelCall.render.<locals>.<genexpr>  s     &Wtq!!Bqc{&W   z
    z# pre-kernel hashes: {r7   r4   c              3   0   K   | ]  \  }}| d |   ywrC  rh   rD  s      r'   r  z+_TritonKernelCall.render.<locals>.<genexpr>  s#      ( $11#Rs(rE  z# post-kernel hashes: {
)r9  r  r;  r#   r;   r   r<  )rT   r=   r  pre_hashes_strpost_hashes_strs        r'   r   z_TritonKernelCall.render  s    t//0$//1B!D??!YY&Wt?T?T?V&WWN()+N+;2>?   N"ii ((,(8(8(>(>(@( O (),_,=R@A  !ON+O+<B??rL   c           
         |j                  |j                                t        r_d| j                  j	                         D ci c]2  \  }}|t        |t        j                        r|j                         n|4 c}}i| _	        t        x}rS| j                  j	                         D ci c](  \  }}t        |t        j                        r	| ||      * c}}| _        | `y c c}}w c c}}w )Nr   )synchronizecurrent_device_RECORD_TRITON_OUTPUTSr  r;   ro   r^   rp   cloner   _TRITON_OUTPUT_HASH_FNr<  )rT   device_interfacer@   rA   r@  s        r'   rb   z_TritonKernelCall.finalize)  s     	$$%5%D%D%FG! $ 1 1 31 Jq%,,$?qwwyQFDK -,7, !KK--/ Aqa. 71: D K s   7C#*-C)c              #   h   K   | j                   d| j                  | j                  gE d {    y 7 w)Nrh   )r9  r  r   rS   s    r'   r  z_TritonKernelCall.__iter__>  s&     $$b$//4??KKKs   (202rX   )rQ  r   )rd   re   rf   r   r$   r   r   rg   rU   r  rN   r   r   rb   r  r  r   s   @r'   r8  r8    s    *77 S#X7 	7 LP!s)!2AD2H!	!(@c @s @0*LrL   r8  c                   X     e Zd ZdZ	 ddededededdf
 fdZd	ee	   de	fd
Z
d Z xZS )_AnnotateCallzCustom annotation callr   is_profiler_recordr   r   r   Nc                 D    t         |   ||       || _        || _        y r  )r
  rU   r   rU  )rT   r   rU  r   r   r  s        r'   rU   z_AnnotateCall.__init__E  s&     	51"4rL   r=   c                 V    | j                   rd| j                   S d| j                   S )N[record function] [annotate] )rU  r   r   s     r'   r   z_AnnotateCall.renderL  s-    ""'z22 
++rL   c              #      K   | j                   rd| j                   nd| j                   di | j                  gE d {    y 7 w)NrX  rY  rh   )rU  r   r   rS   s    r'   r  z_AnnotateCall.__iter__R  sK      ** %TXXJ/"488*-OO	
 		
 		
s   <AAAr  )rd   re   rf   r   r   r  rg   rU   r  r$   r   r  r  r   s   @r'   rT  rT  B  sV      RW55,05>A5JN5	5,c ,s ,

rL   rT  c                 8     | | }|t        |t              sJ |S rX   )ro   r   )hookr  r   s      r'   	_run_hookr]  _  s$    
+C;*S$///JrL   callc           	          t         rMt         D ]C  }t        ||||||       }|| j                  i | _        | j                  j                  |       E y y rX   )r   r]  r   update)r^  functypesr  r  r\  hook_outs          r'   _run_dispatch_pre_log_hooksrd  e  sT    + 	*D tUD&$GH#88#!DH)	* rL   c           	      *   t         r:i }t         D ]&  }t        ||||||      }||j                  |       ( |r|| _        t        rM| j
                  i | _        t        D ]0  }t        ||||||      }|| j
                  j                  |       2 y y rX   )r   r]  r`  r   r   r   )	r^  ra  rb  r  r  resultr   r\  rc  s	            r'   _run_dispatch_hooksrg  q  s    * 	(D tUD&&IH#h'	(  DK88DH' 	*D tUD&&IH#)	*	 rL   c                     t        | t              rt        | j                        S t        | t              r| j
                  S t        | t              r| j                  S t        | t              rt        S t        |       S )z>String identifying _DebugCall (e.g. func, kernel, module name))ro   r  r   r   r8  r9  r0  r1  r"  r*  r$   )r^  s    r'   _get_call_nameri    sc    $ DGG$$	D+	,	D-	(	D+	,  4yrL   zdebug_mode_ops::annotaterh   )mutates_argsr   c                      y rX   rh   r   s    r'   r   r     s     rL   c                      y rX   rh   rl  s    r'   _annotate_fakern    s    rL   c                   n    e Zd Zddddddddddd
	 d+ fdZd+dZd+dZedefd	       Zd+d
Z	d+dZ
d,dZd Zd Zd,dZ fdZ fdZd+dZej&                  	 d-dedz  fd       Zdedeeef   defdZd-dedz  defdZeej&                  	 	 	 d.dedz  dedz  dedz  fd              Zeej&                  d               Zeej&                  	 d/deez  ee   z  d efd!              Z eej&                  d"               Z!e"d#        Z#d$ Z$ed%eddfd&       Z%e	 d0d'ed(ed)edee   fd*       Z& xZ'S )1r   FTN)
record_torchfunctionrecord_faketensorrecord_realtensorrecord_tensor_attributesrecord_nn_modulestore_original_argsrecord_stack_tracerecord_output
record_idsrecord_profiler_contextr   c       
         F   t         |           dd l}t                d| _        || _        || _        || _        |xs g | _        || _	        d | _
        | j                  r| j                          || _        || _        || _        |	| _        |
| _        | j#                          y )Nr   T)r
  rU   torch.distributed.tensorr   supports_higher_order_operatorsrp  rq  rr  rs  rt  module_trackermodule_tracker_setupru  rv  rw  rx  ry  reset)rT   rp  rq  rr  rs  rt  ru  rv  rw  rx  ry  r^   r  s               r'   rU   zDebugMode.__init__  s     	'"$/3, %9! "3 "3 )A(FB% !115  %%' $7  #5 $1 !+ .E$

rL   c                 Z    g | _         d| _        t               | _        i | _        d| _        y rP   )	operatorsr   rN   _tensor_memo_output_infoignored_record_functionsrS   s    r'   r  zDebugMode.reset  s+    +-/1()%rL   c                 "    || j                   |<   y)z5Assign IDs to output tensors and store in output_infoN)r  )rT   op_indexrf  s      r'   _track_op_outputzDebugMode._track_op_output  s     '-(#rL   c                      y)NTrh   )clss    r'   ignore_compile_internalsz"DebugMode.ignore_compile_internals  s    rL   c                     t         ry t        |      j                  d      ry | j                  s4|j	                  | j
                  | j                  r| j                  nd        | j                  j                  |       y )Nzprofiler::_record_function)
_IN_INDUCTOR_BENCHMARKr$   r   ru  r   rs  rx  r  r  append)rT   r^  s     r'   _record_callzDebugMode._record_call  sc    !t9 <=''--%)__!!$ 	d#rL   c                     | j                   sy |j                  || j                  | j                  r| j                         y d        y rX   )rw  r   rs  rx  r  )rT   r^  r   s      r'   _record_call_outputzDebugMode._record_call_output  sA    !!))!%D	
 7;	
rL   c                 6   |i }t        |||| j                  | j                        }| j                  |       	 | xj                  dz  c_         ||i |}| j	                  ||       || xj                  dz  c_        S # | xj                  dz  c_        w xY w)Nr	  r\   )r  r   rv  r  r  rT   ra  rb  r  r  r^  rf  s          r'   __torch_function__zDebugMode.__torch_function__
  s    >F$t7N7N
 	$	!OOq O4*6*F$$T62OOq ODOOq Os   0B Bc                     t        fddD              r| xj                  dz  c_        y t        d| j                  | j                        }| j
                  j                  |       | xj                  dz  c_        y )Nc              3   @   K   | ]  }j                  |        y wrX   )r   )r  prefixr   s     r'   r  z3DebugMode._maybe_record_function.<locals>.<genexpr>  s"      	
 NN6"	
s   )zCachingAutotuner.zInductorBenchmarker.zcompile_fx.<locals>.r\   Tr	  )anyr  rT  r   rv  r  r  rT   r   r^  s    ` r'   _maybe_record_functionz DebugMode._maybe_record_function  sj     	
	
 	
 ))Q.)S$t?V?VWd#1rL   c                     | j                   dk\  sJ | j                   dkD  r| xj                   dz  c_         y | xj                  dz  c_        y )Nr   r\   )r  r   rS   s    r'   _maybe_exit_record_functionz%DebugMode._maybe_exit_record_function.  sC    ,,111((1,))Q.)OOq OrL   c                    |i }| j                   r|t        j                  j                  j                  j
                  k(  r%t        |      dk(  sJ | j                  |d          nA|t        j                  j                  j                  j                  k(  r| j                          |t        j                  j                  j                  j
                  u r%t        |      dk(  sJ | j                  |d          y d }t        j                  j                  j                   |v r;t#        |||| j$                  | j&                        }| j)                  |       t*        S t,        |v st/        t1               t2              rv| j4                  r|t        j                  j6                  j8                  j
                  k7  rt#        |||| j$                  dz   | j&                        }| j)                  |       nRt        |      dk(  rD| j:                  r8t#        |||| j$                  dz   | j&                        }| j)                  |       |rt=        |||||        ||i |}|r"| j?                  ||       tA        ||||||       |S )Nr\   r   r	  )!ry  r^   r   profiler_record_function_enter_newr   r:   r  _record_function_exit_RecordFunctionr  r   r   _handle_annotaterr   r<   rs   r  r   rv  r  NotImplementedr   ro   r
   r   rq  primr   rr  rd  r  rg  r  s          r'   __torch_dispatch__zDebugMode.__torch_dispatch__5  s,   >F ''uyy))DDLLL4yA~%~++DG4++AAQQQ002 599++44<<<t9>!>!!$q'* ##++u4dFDOO4;R;RD d#!!5 J&(.%
 %%599>>00888"!+"55D %%d+Z1_%%OOa'11 !!$'
 'dE4Ht&v&$$T62dE4HrL   c                 ~   | j                          | j                  rt        j                  j	                  |        t
        |           | j                  r| j                  j                          | j                  r@t        j                  j                  dd      | _        | j                  j                          | S )NTF)	check_nan)r  rp  r^   r_   _push_on_torch_function_stackr
  	__enter__rt  r}  rv  autogradset_detect_anomalyanomaly_for_traces)rT   r  s    r'   r  zDebugMode.__enter__t  s    

$$HH2248  ))+""&+nn&G&G 'H 'D# ##--/rL   c                 
   t        |   |  | j                  r| j                  j                          | j                  rt
        j                  j                          | j                  r | j                  j                  |  y y rX   )
r
  __exit__rt  r}  rp  r^   r_   _pop_torch_function_stackrv  r  )rT   r  r  s     r'   r  zDebugMode.__exit__  sh    $  ((*$$HH..0"",D##,,d3 #rL   c                 z     ddl m}  |        _        d fd}d fd} j                  j                  ||       y )Nr   r   c                     j                   j                  |       }j                  j                  t	        |j
                               xj
                  dz  c_        y Nr\   )r}  _get_mod_namer  r  r0  r   )moduleinputfqnrT   s      r'   pre_fw_hookz3DebugMode.module_tracker_setup.<locals>.pre_fw_hook  sD    %%33F;CNN!!-T__"EFOOq OrL   c                 0    xj                   dz  c_         y r  )r   )r  r  r   rT   s      r'   post_fw_hookz4DebugMode.module_tracker_setup.<locals>.post_fw_hook  s    OOq OrL   r]   )$torch.distributed._tools.mod_trackerr   r}  register_user_hooks)rT   r   r  r  s   `   r'   r~  zDebugMode.module_tracker_setup  s2    C(l	!	! 	//\JrL   r&  c           
   #     K   	 | j                  t        ||||| j                  dz   | j                               | xj                  dz  c_        d  | xj                  dz  c_        y # | xj                  dz  c_        w xY ww)Nr\   )r$  r%  r&  r   r   )r  r"  r   rv  )rT   r}   r$  r%  r&  s        r'   record_redistribute_callsz#DebugMode.record_redistribute_calls  sq     	!!"/"/'9#211	 OOq OOOq ODOOq Os   BAA* B*BBr9  r  c                     t        ||| j                  dz         }|j                  | j                         | j                  j                  |       |S r  )r8  r   r   rs  r  r  )rT   r9  r  r^  s       r'   record_triton_kernelzDebugMode.record_triton_kernel  sF     !fdoo6IJD99:d#rL   show_stack_tracec                     | j                   n|}t        j                  j                         5  |s/dj	                   fd j
                  D              }|cddd       S g }d} j
                  D ]  }d}t        |d      r|j                  r|j                  }n$t        |d      r|j                  r|j                  }d}|rt        |      }|rC||k7  r>|r|j                  d       d|j                  dz   z  }|j                  |d	z   |z          |}dd|j                  z  z   |j                   j                        z   }	|j                  |	        dj	                  |      cddd       S # 1 sw Y   yxY w)
a@  
        show_stack_trace: option to display one-line stack trace summaries above groups
                        of operations (similar to gm.print_readable() style).
                        Requires record_stack_trace=True.
                        if None, uses self.record_stack_trace, otherwise overrides it.
        NrH  c              3   z   K   | ]2  }d d |j                   z  z   |j                  j                        z    4 yw)rF  N)r   r   rs  )r  r   rT   s     r'   r  z)DebugMode.debug_string.<locals>.<genexpr>  sC      #  R]]*+ii = =>?#s   8;r   r   r4   rF  r\   z# )rv  r^   r_   DisableTorchFunctionr#   r  r8   r   r   r   r  r   r   rs  )
rT   r  rf  linesprev_stack_summaryr   r   stack_summaryindentlines
   `         r'   debug_stringzDebugMode.debug_string  sz    (8'?D##EU 	 XX**, *	$# # #nn	#  *	$ *	$ E!%nn #"201b6H6H"$"4"4KR/BNN"$..K $$9+$FM ]6H%HR(!R]]Q%67FLL$!>?)6& R]]*+ii = =>? 
 T"5#8 99U#U*	$ *	$ *	$s   (E/"DE//E8record_hooklog_hookpre_log_hookc              #     K   | rt         j                  |        |rt        j                  |       |rt        j                  |       	 d | rt         j	                          |rt        j	                          |rt        j	                          yy# | rt         j	                          |rt        j	                          |rt        j	                          w w xY ww)ai  
        Allows installing post-hooks on arguments to intercepted __torch_dispatch__ calls;
        hook signatures are expected as (func, types, args, kwargs, result),
        i.e. __torch_dispatch__ args + return value.

        Logging hook outputs are stored in call.log and annotate calls in debug_string(),
        while recording hook outputs are just stored in call.record.
        For now hooks are expected to return dictionaries.

        pre_log_hook signature is (func, types, args, kwargs, call) and is executed before
        the operation. It allows capturing state before in-place mutations.
        N)r   r  r   r   rY   )r  r  r  s      r'   dispatch_hookszDebugMode.dispatch_hooks  s     * "))+6&&x0#**<8	.&**,#'')'++- 	 &**,#'')'++- s    AC	B ACACCc               #      K   d } 	 t         }da t        j                  |       5  d ddd       |a y# 1 sw Y   |a yxY w# a w xY ww)zN
        Hook for storing cloned output tensors in .record["output"].
        c                 $    t        d |      }d|iS )Nc                 Z    t        | t        j                        r| j                         S | S rX   )ro   r^   rp   rO  r   s    r'   r   zADebugMode.record_outputs.<locals>.dispatch_hook.<locals>.<lambda>%  s    z!U\\'B!'')  rL   r   r   )ra  rb  r  r  rf  r   s         r'   dispatch_hookz/DebugMode.record_outputs.<locals>.dispatch_hook#  s    I6C c?"rL   T)r  N)rN  r   r  )r  _old_record_tritons     r'   record_outputszDebugMode.record_outputs  sV     	#	8!7%)"))m)D  &8" &8"%7"s9   A
A 5A A
A A A
 A AA
r@  hash_inputsc              #     	K   d }t        |       r| n\t        | t              r	 ||       nCt        | t              r| D cg c]
  } ||       c}		fdnt	        dt        |              fdfd}fd}	 rt        }at        }at        j                  |r|nd      5  d ddd       ra|ayc c}w # 1 sw Y   xY w# raaw xY ww)	au  
        Installs hook for tensor hash logging.

        hash_fn: One of:
            - Custom-defined hash function
            - String: one of ("norm", "hash_tensor")
                - "norm": uses norm_hash_fn; basically tensor's L1 norm
                - "hash_tensor": uses torch.hash_tensor (XOR sum reduction)
            - List of strings: returns tuple of hashes from above options
        hash_inputs: if True, also hashes tensors in (args, kwargs), storing them in "input_hash".
        Input hashes are captured before the operation executes, so they reflect the state before
        any in-place mutations.
        c                     t        | t              r| dv sJ t        j                  | dk(  rt        d      S t
        d      S )N)r   r   r   T)r   )ro   r$   	functoolspartialr   r   )	hash_types    r'   hash_fn_optionz3DebugMode.log_tensor_hashes.<locals>.hash_fn_optionE  sJ    i-)?V2VVV$$ )V 3TX 9GTX rL   c                 .     t         fdD              S )Nc              3   .   K   | ]  } |        y wrX   rh   )r  fnr&   s     r'   r  z@DebugMode.log_tensor_hashes.<locals>.<lambda>.<locals>.<genexpr>Q  s      52A 5s   )r  )r&   fnss   `r'   r   z-DebugMode.log_tensor_hashes.<locals>.<lambda>Q  s    5 5 55 rL   zRlog_tensor_hashes() expected hash_fn to be callable, str, or list[str], but found c                 "    t        fd|       S )Nc                 L    t        | t        j                        r |       S d S rX   )ro   r^   rp   )r&   r  s    r'   r   zADebugMode.log_tensor_hashes.<locals>._tree_hash.<locals>.<lambda>Y  s    :a#>"Q% D rL   r  )objr  s    r'   
_tree_hashz/DebugMode.log_tensor_hashes.<locals>._tree_hashW  s    H# rL   c                 v    dt        |       v sdt        |       v ryr ||f      }t        d |      sd|iS y)z:Pre-hook to capture input hashes before operation executesemptyr  Nc                 
    | d u S rX   rh   r   s    r'   r   zMDebugMode.log_tensor_hashes.<locals>._dispatch_pre_log_hook.<locals>.<lambda>d  s
    !t) rL   
input_hash)r$   r   )ra  rb  r  r  r^  r  r  r  s         r'   _dispatch_pre_log_hookz;DebugMode.log_tensor_hashes.<locals>._dispatch_pre_log_hook\  sG    #d)#zSY'>'v7
 3Z@(*55rL   c                     dt        |       v sdt        |       v ryi } |      |d<   t        d |j                               ry|S )z;Post-hook to capture output hashes after operation executesr  r  Nhashc                 
    | d u S rX   rh   r   s    r'   r   zJDebugMode.log_tensor_hashes.<locals>._dispatch_post_hook.<locals>.<lambda>p  s
    !t) rL   )r$   r   values)ra  rb  r  r  rf  r   r  s         r'   _dispatch_post_hookz8DebugMode.log_tensor_hashes.<locals>._dispatch_post_hookh  sI    #d)#zSY'>C$V,CK+SZZ\:JrL   N)r  r  )
callablero   r$   r  r   rv   r?  rP  r   r  )
r@  r  r  r  r  r  _old_input_hfn_old_output_hfnr  r  s
    ` `    @@r'   log_tensor_hasheszDebugMode.log_tensor_hashes2  s     &	 GB%(B&078">"%8C5B%deijqerdst 	

	
		5!6(*%4O%'")),7B3 *   	 (6%%4"g 9V  (6%%4"sA   ?C"C0C"-C 2C7C ?C"CC CC"c               #   ,   K   	 da d da y# da w xY ww)z
        Context manager for disabling logging during inductor benchmarking,
        so logs don't contain all kernels launched from autotuning.
        TNF)r  rh   rL   r'   _benchmarking_inductorz DebugMode._benchmarking_inductor  s      	+%)"%*"U"s    c                 ,    t        | j                        S rX   )r  r  rS   s    r'   logszDebugMode.logs  s    DNN##rL   c                 ~    t        |d| j                  | j                        }| j                  j	                  |       y)zHandles DebugMode._annotate()FN)rT  r   rv  r  r  r  s      r'   r  zDebugMode._handle_annotate  s.    S%$:Q:QRd#rL   r   c                 V    t         j                  j                  j                  |        y)z
        If an active DebugMode exists, adds an "[annotate] <tag>" entry to the logs. Useful for contextualizing logs.
        Implemented with a custom op.
        N)r^   r   r   r   rl  s    r'   r   zDebugMode._annotate  s     			  ))#.rL   logs1logs2compare_inputsc                 p   t        |       t        |      k7  r#t        dt        |        dt        |             g t        t        | |            D ]^  \  }\  }}t	        |      j
                  }t	        |      j
                  }||k7  rt        d| d| d|       |}t        |      t        |      }
}	|	|
k7  rt        d| d| d|	 d| d|
 d      |	|j                  |j                  k7  r.t        d	| d d
| d|j                   d|j                   
      t        |t              r,t        |      t        |      k7  st        d| d| d|       t        |t              r"|j                  |j                  k7  r5t        d|j                   d| d|j                   d|j                         fd}|j                  du|j                  du}}||k7  r!t        d|j                   d| d| d|       |r ||j                  |j                  d       |s|j                  du|j                  du}}||k7  r!t        d|j                   d| d| d|       |s ||j                  |j                  d       =t        |t               sOfd}|j"                  duxr d|j"                  v }|j"                  duxr d|j"                  v }||k7  rt        d| d d| d| d | 
      |r% ||j"                  d   |j"                  d   d       |s|j"                  duxr d!|j"                  v }|j"                  duxr d!|j"                  v }||k7  rt        d"| d d| d#| d$| 
      |s: ||j"                  d!   |j"                  d!   d       a S )%a  
        Compares tensor hashes between two DebugMode runs, for checking run-to-run numerical divergence.

        This first validates the two log sequences have identical structure (same operations, input shapes/dtypes, etc.),
        then compares tensor hash values, and returns a list of call outputs where mismatches were found.
        Expects input logs to have been run with log_tensor_hashes, and looks for hashes in .log["hash"] & .log["input_hash"]
        (or .post_hashes & .pre_hashes for triton kernels).

        note: skips checking log pairs where hashes aren't present, but will raise if present in one & not the other.

        Args:
            logs1: logs from the first DebugMode run (from debug_mode.logs)
            logs2: logs from the second DebugMode run
            compare_inputs: If True, also compare input tensor hashes (default: only output checking)

        Returns:
            List of dictionaries describing hash mismatches. Each dict contains:
                - call_type: "torch op" or "triton kernel"
                - call: Operator/kernel name
                - arg_name: For triton kernels, the argument name; None for torch ops
                - pytree_path: For torch ops, the pytree path to the differing tensor; None for kernels
                - hash1: Hash value from the first run
                - hash2: Hash value from the second run
                - rel_diff: Relative difference between hash values
                - is_input_hash: True if this is an input hash, False for output hash

        Raises:
            ValueError: If logs have different lengths, call types, operator names, or call depths

        Usage::

            # Run model first time
            with DebugMode() as debug_mode, DebugMode.log_tensor_hashes():
                model(x)
                logs1 = debug_mode.logs

            # Run again, in exactly the same way
            with DebugMode() as debug_mode, DebugMode.log_tensor_hashes():
                model(x)
                logs2 = debug_mode.logs

            mismatches = DebugMode.check_hash_mismatches(logs1, logs2)
            for m in mismatches:
                print(f"{m['call']}: hash diff {m['rel_diff']:.2e}")
        zLog lengths don't match: z vs z Call types don't match at index rl   zOperators don't match at index r   z] vs r!   zCall depths for z] don't match at index z(Redistribute calls don't match at index z(Triton kernel call args don't match for z
 at index z	:

log1: z

log2: c                     t        | j                               t        |j                               k(  sJ | D ]>  }| |   ||   k7  sj                  d|d | |   ||   t        | |   ||         |d       @ y )Nztriton kernel	call_typer^  arg_namepytree_pathr   r   rel_diffis_input_hash)setkeysr  r   )hashes1hashes2is_inputkeydifference_infor   s       r'   compare_triton_hashesz>DebugMode.check_hash_mismatches.<locals>.compare_triton_hashes   s    w||~.#glln2EEEE& "3<73<7+221@,30337-4S\-4S\0A(/gcl1& 6>!"rL   Nz2Triton kernel post-hash presence inconsistent for z: log1 has post_hashes=z, log2 has post_hashes=F)r  z1Triton kernel pre-hash presence inconsistent for z: log1 has pre_hashes=z, log2 has pre_hashes=Tc                 0    fd}t        || |       y )Nc                 l    ||k7  r.j                  dd t        |       ||t        ||      d       y y )Nztorch opr  )r  r   r   )keypathr   r   r	  r  r   s      r'   _helperzKDebugMode.check_hash_mismatches.<locals>.compare_op_hashes.<locals>._helper8  sE     E>+221;,30439'?-2-20A%0O5=	!" *rL   )r   )r  r  r  r  r	  r   s     ` r'   compare_op_hashesz:DebugMode.check_hash_mismatches.<locals>.compare_op_hashes7  s     'wArL   r  z4Output hash presence inconsistent for triton kernel z] at index z: log1 has hash=z, log2 has hash=r  z3Input hash presence inconsistent for triton kernel z: log1 has input_hash=z, log2 has input_hash=)r:   
ValueError	enumerateziprv   rd   ri  r   ro   r"  r  r8  r  r9  r<  r;  r  r   )r  r  r  ilog1log2
call1_type
call2_typer  op1_nameop2_namer
  
has_post_1
has_post_2	has_pre_1	has_pre_2r  	has_hash1	has_hash2r	  r   s                      @@r'   check_hash_mismatcheszDebugMode.check_hash_mismatches  s   b u:U#8UDUUVV(UE):; O	OA|dd,,Jd,,JZ' 6qcJ<tJ<X  #I "0!5~d7KhH8# 5aS9+QxjPUV_U``abjakklm  G $//1 &yk7);RSTRUUWX\XgXgWhhlmqm|m|l}~ 
 $ 12;%+-$B1#RvTRVQWX 
 D"34??doo5$B4CSCSBTT^_`^a b%%)__$5Z?PR 
* $$D0$$D0 '
 +$LTM]M]L^ _$$%3&=j\I`ak`ln 
 )(($*:*:U
 "t3t3  )I !I-(OPTP`P`Oa b(()s*@Kabkaln 
 !- OOT__t
 D'*B& !HHD0GVtxx5G	 HHD0GVtxx5G		)$NykYZ[bZc d$$%3&6ykAQR[Q\^ 
 %((!& " $ 4 Q9QI $ 4 Q9QI I-(QR[Q\\]^e]f g(()s*@Kabkaln 
 !) HH\2 HH\2%)WO	b rL   r]   )rh   NrX   )NNN)r   Fr  )(rd   re   rf   rU   r  r  classmethodr  r  r  r  r  r  r  r  r  r  r~  
contextlibcontextmanagerr$   r  r   r   r8  r  r  staticmethodr   r  r  r  r  r  propertyr  r  r   r   r  r   s   @r'   r   r     sJ    #!%!  $= 
=~*-   $
!"&!=~$4K"  *.!
  $J! !.(,S#X	5$TD[ 5$C 5$n '+$((,!._!.T/!. o!.  !.F 8  8( JOO5C$s)+O5CGO5  O5b 
+  
+ $ $$
 /s /t / / 9>EE E26E	dE ErL   c                  R    d } t               D ]  }t        |t              s|}  | S  | S rX   )r   ro   r   )
debug_modemodes     r'   r   r   m  s9    J02 dI&J	 rL   rX   r  )[r   r"  r  r   loggingr   r   ra   collections.abcr   typingr   r   r^   torch._subclasses.fake_tensorr   r   torch.fx.graphr   torch.utils._dtype_abbrsr	   torch.utils._python_dispatchr
   r   r   torch.utils._pytreer   r   r   r   torch.utils._tracebackr   torch.utils.weakr   torch._dynamo.device_interfacer   r  r   	getLoggerrd   r   __all__r*  r   r  __annotations__r   r   r  rN  rP  r?  r$   r(   r.   r2   rB   rK   rN   ry   r~   rp   r  r   r   r   rg   r   r   r   r   r   r   r   r   r  r"  r0  r8  rT  r]  rd  rg  ri  library	custom_opr   register_fakern  r   r   rh   rL   r'   <module>r:     s  B     	   $ %  D - 0 
 O N 4 & >? g!/
0 ) )+ X +&( T(^ (*, h ,      5s 5<C <9s 9F FQS Q' '&G# G"c ELL d u||e?S ,#ell  PSAS 0	%# 	%3 3: 3:    #2. .bGJj GJT9
 9x
J 
0SL
 SLl
J 
:	*j 	*PT 	**j *PT **   3"E3 4  F
    K! K\y4/ rL   