
    {Yh]                    .   d dl m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	Z	d dl
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mZmZmZmZ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#m$Z$ d dl%m&Z&  ej'        e(          Z)er
d dl*m+Z+ d dl,Z,	  ej-        d          Z. ej-        d          Z/ ed           G d d                      Z0 G d de          Z1 G d dej2                  Z3	  G d d          Z4 ej        d           G d d                      Z5 G d de          Z6e!ej         G d d                                  Z7 ed          Z8	  ej        d           G d  d!                      Z9	  ej        d           G d" d#e9                      Z:	  ej        d           G d$ d%e9                      Z;	  G d& d'ee8                   Z< G d( d)          Z= G d* d+          Z> G d, d-e<e>                   Z? G d. d/          Z@ G d0 d1e<e@                   ZA G d2 d3          ZB	  G d4 d5e<e=                   ZC G d6 d7          ZD G d8 d9eD          ZE G d: d;          ZF ejG                    ZH	  G d< d=          ZI G d> d?          ZJedMdB            ZKedNdD            ZL ej        d           G dE dF                      ZM ej        d           G dG dHeM                      ZNdOdPdKZOdL ZPdS )Q    )annotationsN)abstractmethod)defaultdict)contextmanager)	dataclass)AnyCallableGeneric
NamedTupleOptionalTYPE_CHECKINGTypeVarUnion)_pytree)dataclass_slots)CapturedTracebackformat_frame)WeakTensorKeyDictionary)CodeTypez-^(?P<frame_id>\d+)/(?P<frame_compile_id>\d+)$zQ^!(?P<compiled_autograd_id>\d+)(?:/(?P<frame_id>\d+)/(?P<frame_compile_id>\d+))?$T)frozenc                  P    e Zd ZU ded<   ded<   dZded<   d Zed
d	            ZdS )	CompileIdOptional[int]frame_idframe_compile_idNcompiled_autograd_idc                    | j         =| j        d u | j        d u k    sJ d}| j        d| j         d| j         }d| j          | S | j        | j        J | j         d| j         S )N /!)r   r   r   )self	frame_strs     _/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/_guards.py__str__zCompileId.__str__V   s    $0MT)t/D/LMMMMI}(GGG0EGG	=t0=)====,1F1R1R1Rm==d&;===    
compile_idOptional[str]c                N   |dS 	 t           t          fD ]e}|                    |          x}rL|                                }|                                D ]\  }}|t          |          ||<    | di |c S ft          # t          $ r}t          d| d          |d}~ww xY w)z
        Factory method that creates a CompileId from its string representation.
        Keep this in sync with the __str__ method.
        NzInvalid compile_id '' )COMPILE_ID_PATTERNCA_COMPILE_ID_PATTERNmatch	groupdictitemsint
ValueError	Exception)clsr&   patternr-   groupskves           r#   from_stringzCompileId.from_stringc   s     4	J.0EF ! !#MM*5555 )"__..F & / /1=(+AF1I3====((() !  	J 	J 	JAJAAABBI	Js   A2B 9B 
B$BB$)r&   r'   )__name__
__module____qualname____annotations__r   r$   classmethodr9   r*   r%   r#   r   r   G   sy         
 $### +/....
> > > J J J [J J Jr%   r   c                  *    e Zd ZU ded<   ded<   d ZdS )TraceIdr   r&   r0   attemptc                b    | j         dk    rt          | j                  S | j         d| j          S )Nr   _)rA   strr&   r!   s    r#   r$   zTraceId.__str__   s7    <1t'''o66666r%   Nr:   r;   r<   r=   r$   r*   r%   r#   r@   r@   z   s<          LLL7 7 7 7 7r%   r@   c                  t    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZddZddZddZddZd ZdS )GuardSourcer                           	   
                  returnboolc                6    | t           j        t           j        fv S N)rH   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULErE   s    r#   is_fsdp_modulezGuardSource.is_fsdp_module   s    68UVVVr%   c                    dd l mc m} |j        r.| t          j        t          j        fv p|                                 S | t          j        t          j        fv S Nr   )torch._dynamo.config_dynamoconfig_unsafe_skip_fsdp_module_guardsrH   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULEr^   )r!   rc   s     r#   is_specialized_nn_modulez$GuardSource.is_specialized_nn_module   s|    ---------1 	<; )
 &&(( 43
 
 	
r%   c                b    | t           j        t           j        t           j        t           j        fv S r[   )rH   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULErE   s    r#   is_unspecialized_nn_modulez&GuardSource.is_unspecialized_nn_module   s,    65>=	
 
 	
r%   c                6    | t           j        t           j        fv S r[   )rH   rk   rl   rE   s    r#   "is_unspecialized_builtin_nn_modulez.GuardSource.is_unspecialized_builtin_nn_module   s     >=
 
 	
r%   c                x    | t           j        t           j        t           j        t           j        t           j        fv S r[   )rH   LOCALrf   r]   rj   rl   rE   s    r#   is_localzGuardSource.is_local   s2    3)5=
 
 	
r%   NrX   rY   )r:   r;   r<   rq   GLOBALrf   re   CONSTANTRANDOM_VALUE	SHAPE_ENVr]   r\   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALrj   ri   rl   rk   r^   rg   rm   ro   rr   r*   r%   r#   rH   rH      s        EF"##$ HLINIO$&!%'",.)-/*W W W W
 
 
 
"
 
 
 

 
 
 

 
 
 
 
r%   rH   c                      e Zd ZdS )GuardBuilderBaseNr:   r;   r<   r*   r%   r#   r|   r|      s        Dr%   r|   c                  *    e Zd ZU ded<   ded<   d ZdS )SLocz,Optional[Union[traceback.FrameSummary, str]]framework_locr'   maybe_user_locc                    t          | j        t                    r| j        nt          | j                  }| j        | j         d| dS d| dS )Nz ()()
isinstancer   rD   r   r   )r!   flocs     r#   r$   zSLoc.__str__   sg     $,c222Dd011 	
 *)44T4444t;;;r%   NrF   r*   r%   r#   r   r      s=         ????!!!!	 	 	 	 	r%   r   c                  .    e Zd ZU ded<   ded<   ded<   dS )
ShapeGuardzsympy.logic.boolalg.Booleanexprr   slocrY   size_obliviousNr:   r;   r<   r=   r*   r%   r#   r   r      s3         %%%%JJJr%   r   c                     e Zd ZU ded<   ded<   dZded<   dZded<   dZd	ed
<   dZded<   dZded<   dZ	ded<   dZ
ded<   d Zd Zd Zd Zed&d            Zed'd            Zed             Zd Zd Zd(d!Zd" Zd# Zd$ Zd% ZdS ))GuardSourceoriginating_sourcez)Callable[[GuardBuilderBase, Guard], None]	create_fnNzOptional[list[str]]guard_types	code_listzOptional[object]obj_weakrefzOptional[type]guarded_class_weakrefzOptional[CapturedTraceback]stackz Optional[traceback.StackSummary]
user_stackr   _hashc                    | j         3t          | j        | j        t	          | j                  f          | _         | j         S r[   )r   hashnamesourceidr   rE   s    r#   __hash__zGuard.__hash__  s6    :ty$+r$.7I7IJKKDJzr%   c                (   t          | j        t          j                  o&| j        j        t
          j        j        j        j	        u }|| j
        r| j
        j        ndt          | j                  | j        |                                 j        j        fS )N)r   r   	functoolspartialfunctorchrb   guardsGuardBuilderDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r!   is_duplicate_inputs     r#   sort_keyzGuard.sort_key  s    
 t~y'899 Y#u}';'H'XX 	
 !%4DK"	NNI  ""+:
 	
r%   c                V    |                                  |                                 k     S r[   )r   r!   others     r#   __lt__zGuard.__lt__%  s    }}!1!111r%   c                f    t          | j        t          j                  r| j        j        S | j        S r[   )r   r   r   r   r   rE   s    r#   r   zGuard.inner_create_fn(  s,    dni&788 	">&&>!r%   rX   rD   c                4    | j                                         S r[   )r   r   rE   s    r#   r   z
Guard.name.  s    &++---r%   rH   c                4    | j                                         S r[   )r   guard_sourcerE   s    r#   r   zGuard.source2  s    &33555r%   c           
     @   t          | t          j                  rv |             }|Jdt          t	          |                      d|j        j         dt          t	          |                     dS dt          t	          |                      dS t          |           S )a  
        This is a workaround of a Python weakref bug.

        `obj_weakref` is instance returned by `weakref.ref`,
        `str(obj_weakref)` is buggy if the original obj overrides __getattr__, e.g:

            class MyConfig(dict):
                def __getattr__(self, x):
                    return self[x]

            obj = MyConfig(offset=5)
            obj_weakref = weakref.ref(obj)
            str(obj_weakref)  # raise error: KeyError: '__name__'
        Nz<weakref at z; to 'z' at >z; dead>)r   weakrefReferenceTypehexr   	__class__r:   rD   )r   objs     r#   weakref_to_strzGuard.weakref_to_str6  s      k7#899 	$+--Cnc"[//&:&:nn#-BXnn_bceficjcj_k_knnnnCc"[//&:&:CCCC{###r%   c                    d| j         r| j         j                                        nd dt          | j                   d|                                 j         d| j         d| j         d|                     | j	                   d| j
         d}|S )	Nz	
        r    z&
        {
            'guard_types': z,
            'code': z,
            'obj_weakref': z
            'guarded_class': z
        }
        )r   r   lowerreprr   r:   r   r   r   r   r   )r!   ss     r#   __repr__zGuard.__repr__O  s    %)[	8				!	!	!b ;?	?? MQMaMaMcMcMl  !,  ^	 
 !//0@AA  #8   r%   c                l   dt          | j                   d}| j        r| j        j                                        nd}|d| dz  }|d|                                 j         dz  }|d| j         dz  }|d| j         dz  }|d|                     | j	                   dz  }|d	| j
         dz  }|S )
NzName: 
r   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )r   r   r   r   r   r:   r   r   r   r   r   )r!   outputr   s      r#   r$   zGuard.__str__[  s    -$ty//----1[@!'')))b+++++M$*>*>*@*@*IMMMM:d&6::::6DN6666R)<)<T=M)N)NRRRRN0JNNNNr%   builderr|   c           	        	 |                      ||           S # t          $ r t                              dt	          |                                                      | j        r_t                              dd                    | j        	                                dd                                                                w xY w)NzError while creating guard:
%szCreated at:
%sr   )
r   r2   log	exceptionrD   rstripr   errorjoinformat)r!   r   s     r#   createzGuard.createf  s    	>>'4000 	 	 	MM;SYY=M=M=O=OPPPz Y		+RWWTZ5F5F5H5H5M-N-N-U-U-W-WXXX		s
    B,Cc                4    | j                                         S r[   )r   rg   rE   s    r#   rg   zGuard.is_specialized_nn_moduleo  s    {33555r%   c                4    | j                                         S r[   )r   r^   rE   s    r#   r^   zGuard.is_fsdp_moduler  s    {))+++r%   c                4    | j                                         S r[   )r   rr   rE   s    r#   rr   zGuard.is_localu  s    {##%%%r%   c                h   | j         sg | _         | j                             |           | j        |d fv s
J d            || _        | j        s|| _        n| j                            |           | j        |d fv s2t          | j                  r|                                 
J d            || _        d S )Nz+Guarded class id must be identical, or NonezBGuarded object must be identical, None or ephemeral (dead weakref))r   appendr   r   extendr   callable)r!   
guard_typeguarded_classr   r   s        r#   set_export_infozGuard.set_export_infox  s     	"!D
+++).
 
 
 
 9
 
 
 &3"~ 	-&DNNN!!),,, d 333()) 4  ""**O +**&r%   rX   rD   rX   rH   )r   r|   )r:   r;   r<   r=   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r$   r   rg   r^   rr   r   r*   r%   r#   r   r      s        ( 8888 (,K++++%)I))))$(K((((,00000)-E----37J7777E  

 
 
 2 2 2" " " . . . X. 6 6 6 X6 $ $ \$0
 
 
	 	 	   6 6 6, , ,& & &' ' ' ' 'r%   r   Tc                      e Zd ZdS )GuardEnvExprNr}   r*   r%   r#   r   r     s        Dr%   r   c                  *    e Zd ZU ded<   ded<   d ZdS )DuplicateInputsr   input_source_ainput_source_bc                *    | j         | j        k    sJ d S r[   )r   r   rE   s    r#   __post_init__zDuplicateInputs.__post_init__  s    "d&9999999r%   N)r:   r;   r<   r=   r   r*   r%   r#   r   r     s=         : : : : :r%   r   c                  $    e Zd ZU ded<   ded<   dS )StorageOverlapzlist[Source]overlapping_sourcesnon_overlapping_sourcesNr   r*   r%   r#   r   r     s*         %%%%))))))r%   r   c                  >    e Zd Zedd            Zedd            ZdS )	CheckpointablerX   r   c                    d S r[   r*   rE   s    r#   copy_graphstatezCheckpointable.copy_graphstate  s    $'Cr%   statec                    d S r[   r*   r!   r   s     r#   restore_graphstatez!Checkpointable.restore_graphstate  s    ,/Cr%   N)rX   r   )r   r   )r:   r;   r<   r   r   r   r*   r%   r#   r   r     s<        ''' ^'/// ^///r%   r   c                  D    e Zd ZU dZ e            Zded<   d Zd Zd Z	dS )GuardsCheckpointStatezW
    The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext
    
set[Guard]dynamo_guardsc                    || _         d S r[   )r   )r!   r   s     r#   __init__zGuardsCheckpointState.__init__  s    *r%   c                n    | j                             |j                   }t          |          dk    rdS |S )z
        Produces a delta against another GuardsCheckpointState.

        Returns None if no delta is found, otherwise, return a set() of mismatched
        Guard type objects.
        r   N)r   
differencer   r!   r   rs      r#   diffzGuardsCheckpointState.diff  s6     ))%*=>>q66Q;;4r%   c                0    |                      |          d u S r[   r   r   s     r#   __eq__zGuardsCheckpointState.__eq__      yy4''r%   N)
r:   r;   r<   __doc__setr   r=   r   r   r  r*   r%   r#   r   r     sf           !$M%%%%+ + +
 
 
( ( ( ( (r%   r   c                  0    e Zd ZU i Zded<   d Zd Zd ZdS )ModuleContextCheckpointStatezdict[str, torch.nn.Module]
nn_modulesc                    || _         d S r[   r  )r!   r  s     r#   r   z%ModuleContextCheckpointState.__init__  s    $r%   c                    t          | j                                                                      t          |j                                                            }t	          |          dk    rdS |S )z
        Produces a delta against another ModuleContextCheckpointState.

        Returns None if no delta is found, otherwise, return a set() of mismatched
        module key names.
        r   N)r  r  keysr   r   r   s      r#   r   z!ModuleContextCheckpointState.diff  s]     $$&&''223u7G7L7L7N7N3O3OPPq66Q;;4r%   c                0    |                      |          d u S r[   r  r   s     r#   r  z#ModuleContextCheckpointState.__eq__  r  r%   N)r:   r;   r<   r  r=   r   r   r  r*   r%   r#   r  r    sT         -/J////% % %
 
 
( ( ( ( (r%   r  c                  "    e Zd ZddZd Zd ZdS )ModuleContextrX   Nonec                    i | _         d S r[   r
  rE   s    r#   r   zModuleContext.__init__  s    *,r%   c                D    t          t          | j                            S r[   )r  dictr  rE   s    r#   r   zModuleContext.copy_graphstate  s    +D,A,ABBBr%   c                L    t          |t                    sJ |j        | _        d S r[   )r   r  r  r   s     r#   r   z ModuleContext.restore_graphstate  s'    %!=>>>>>*r%   NrX   r  r:   r;   r<   r   r   r   r*   r%   r#   r  r    sI        - - - -C C C+ + + + +r%   r  c                  0    e Zd ZU i Zded<   d Zd Zd ZdS )GlobalContextCheckpointStatezdict[str, tuple[Callable, ...]]global_statec                    || _         d S r[   r  )r!   global_statess     r#   r   z%GlobalContextCheckpointState.__init__  s    )r%   c                    t          | j                                                                      t          |j                                                            }t	          |          dk    rdS |S )z
        Produces a delta against another GlobalContextCheckpointState.

        Returns None if no delta is found, otherwise, return a set() of mismatched
        global key names.
        r   N)r  r  r  r   r   r   s      r#   r   z!GlobalContextCheckpointState.diff  s^     !&&(())44S9K9P9P9R9R5S5STTq66Q;;4r%   c                0    |                      |          d u S r[   r  r   s     r#   r  z#GlobalContextCheckpointState.__eq__*  r  r%   N)r:   r;   r<   r  r=   r   r   r  r*   r%   r#   r  r    sT         46L6666* * *
 
 
( ( ( ( (r%   r  c                  .    e Zd ZdZh dZd	dZd Zd ZdS )
GlobalContextzz
    This keeps track of the global torch state during tracing of a function.
    For example, torch.is_grad_enabled.
    >   grad_enabledautocast_enabledautocast_cpu_dtypeautocast_gpu_dtypeautocast_cpu_enabledautocast_cache_enabledrX   r  c                    i | _         d S r[   r  rE   s    r#   r   zGlobalContext.__init__=  s    =?r%   c                D    t          t          | j                            S r[   )r  r  r  rE   s    r#   r   zGlobalContext.copy_graphstate@  s    +D1B,C,CDDDr%   c                f   t          |t                    sJ |j        | _        t          | j                  t          | j                  k    r/t          | j                                                  | j        k    s
J d            | j                                        D ]\  }} ||           d S )NzGlobal state mismatch)r   r  r  r   _supported_global_statesr  r  values)r!   r   r   argss       r#   r   z GlobalContext.restore_graphstateC  s    %!=>>>>>!.!""c$*G&H&HHHD%**,,--1NNNN" ONN+2244 	 	JD$DJJJJ	 	r%   Nr  )r:   r;   r<   r  r*  r   r   r   r*   r%   r#   r   r   .  si         
     @ @ @ @E E E    r%   r   c                  L    e Zd ZddZd Zd Zd Zd Zddd	ddZddZ	d Z
dS )	GuardsSetNc                4    |t                      }|| _        d S r[   )r  inner)r!   r0  s     r#   r   zGuardsSet.__init__Q  s    =EEE


r%   c                *    t          | j                  S r[   )iterr0  rE   s    r#   __iter__zGuardsSet.__iter__V      DJr%   c                *    t          | j                  S r[   )r   r0  rE   s    r#   __len__zGuardsSet.__len__Y  s    4:r%   c                :    t          | j        |j        z
            S r[   )r.  r0  r   s     r#   __sub__zGuardsSet.__sub__^  s    ek1222r%   c                *    t          | j                  S r[   )rY   r0  rE   s    r#   __bool__zGuardsSet.__bool__a  r4  r%   Tr   )collect_debug_stackskipguardr   c                   || j         v rd S |rI|j        t          j        d|z             |_        |j        t
                                          |_        | j                             |           d S NrI   )r<  )r0  r   r   extractr   TracingContextextract_stackadd)r!   r=  r;  r<  s       r#   rC  zGuardsSet.addd  su    DJF 	B{"/7QXFFF'#1#?#?#A#A 
ur%   othersr   c                H    |D ]}|D ]}|                      |d           d S r?  )rC  )r!   rD  ogs       r#   updatezGuardsSet.updaten  sH     	$ 	$A $ $####$	$ 	$r%   c                H    ddl m fd| j        D             | _        dS )z.Delete all guards that contains a given sourcerI   )is_from_sourcec                6    h | ]} |j                   |S r*   )r   ).0rG  rJ  r   s     r#   	<setcomp>z6GuardsSet.remove_guards_with_source.<locals>.<setcomp>w  s=     
 
 
8Lf)U)U

 
 
r%   N)_dynamo.sourcerJ  r0  )r!   r   rJ  s    `@r#   remove_guards_with_sourcez#GuardsSet.remove_guards_with_sources  sK    222222
 
 
 
 
z
 
 



r%   r[   )r=  r   )rD  r   )r:   r;   r<   r   r3  r6  r8  r:  rC  rH  rO  r*   r%   r#   r.  r.  P  s           
       
3 3 3      8<!      $ $ $ $

 
 
 
 
r%   r.  c                  "    e Zd ZddZd Zd ZdS )GuardsContextrX   r  c                :    t                      | _        g | _        d S r[   )r.  r   aotautograd_guardsrE   s    r#   r   zGuardsContext.__init__  s    (168r%   c                N    t          t          | j        j                            S r[   )r   r  r   r0  rE   s    r#   r   zGuardsContext.copy_graphstate  s    $S);)A%B%BCCCr%   c                f    t          |t                    sJ t          |j                  | _        d S r[   )r   r   r.  r   r   s     r#   r   z GuardsContext.restore_graphstate  s2    %!677777&u':;;r%   Nr  r  r*   r%   r#   rQ  rQ    sI        9 9 9 9D D D< < < < <r%   rQ  c                      e Zd Zedd            Zedd            Zedd            Zedd            Zedd            Zedd            Z	edd            Z
edd            ZdS )HopSubgraphCachefn_idr0   
identifierrD   c                    d S r[   r*   r!   rX  rY  s      r#   add_dynamo_installed_submodulez/HopSubgraphCache.add_dynamo_installed_submodule  s    KN3r%   rX   	list[str]c                    d S r[   r*   r!   rX  s     r#   get_dynamo_installed_submodulesz0HopSubgraphCache.get_dynamo_installed_submodules      HKr%   keyr	   c                    d S r[   r*   r!   rY  rb  s      r#   add_autograd_key_entryz'HopSubgraphCache.add_autograd_key_entry  s    FIcr%   c                    d S r[   r*   r!   rY  s     r#   get_autograd_key_entryz'HopSubgraphCache.get_autograd_key_entry  s    7:sr%   c                    d S r[   r*   rd  s      r#   add_proxy_dispatch_entryz)HopSubgraphCache.add_proxy_dispatch_entry  ra  r%   c                    d S r[   r*   rg  s     r#   get_proxy_dispatch_entryz)HopSubgraphCache.get_proxy_dispatch_entry  s    9<r%   tangent_metadatatuple[object]gmodtorch.fx.GraphModulec                    d S r[   r*   )r!   rY  rm  ro  s       r#   add_lazy_bwd_entryz#HopSubgraphCache.add_lazy_bwd_entry  s	     sr%   c                    d S r[   r*   r!   rY  rm  s      r#   get_lazy_bwd_entryz#HopSubgraphCache.get_lazy_bwd_entry  s	     cr%   NrX  r0   rY  rD   rX  r0   rX   r]  rY  rD   rb  r	   rY  rD   rY  rD   rm  rn  ro  rp  )rY  rD   rm  rn  rX   r0   )r:   r;   r<   r   r\  r`  re  rh  rj  rl  rr  ru  r*   r%   r#   rW  rW    s        NNN ^NKKK ^KIII ^I::: ^:KKK ^K<<< ^<   ^    ^  r%   rW  c                  V    e Zd ZddZddZdd
ZddZddZddZddZ	ddZ
ddZdS )InvokeSubgraphCacherX   r  c                    i | _         i | _        t          t                    | _        t          t
                    | _        d S r[   )autograd_cacheproxy_dispatch_cacher   listdynamo_installed_submodulesr  lazy_bwd_cacherE   s    r#   r   zInvokeSubgraphCache.__init__  s=    359;!ALTARAR(  	r%   rX  r0   rY  rD   c                F    | j         |                             |           d S r[   )r  r   r[  s      r#   r\  z2InvokeSubgraphCache.add_dynamo_installed_submodule  s$    (/66zBBBBBr%   r]  c                8    | j                             |g           S r[   )r  getr_  s     r#   r`  z3InvokeSubgraphCache.get_dynamo_installed_submodules  s    /33E2>>>r%   rb  r	   c                    || j         |<   d S r[   )r~  rd  s      r#   re  z*InvokeSubgraphCache.add_autograd_key_entry  s    *-J'''r%   c                8    | j                             |d           S r[   )r~  r  rg  s     r#   rh  z*InvokeSubgraphCache.get_autograd_key_entry  s    "&&z4888r%   c                    || j         |<   d S r[   )r  rd  s      r#   rj  z,InvokeSubgraphCache.add_proxy_dispatch_entry  s    03!*---r%   c                8    | j                             |d           S r[   )r  r  rg  s     r#   rl  z,InvokeSubgraphCache.get_proxy_dispatch_entry  s    (,,Z>>>r%   rm  rn  ro  rp  c                ^    t          | j        |                   }||f| j        |         |<   |S r[   )r   r  )r!   rY  rm  ro  	num_gmodss        r#   rr  z&InvokeSubgraphCache.add_lazy_bwd_entry  s7     +J788	=A9<MJ'(89r%   c                Z    || j         vrdS | j         |                             |d          S )N)NN)r  r  rt  s      r#   ru  z&InvokeSubgraphCache.get_lazy_bwd_entry  s4    T000<":.223C\RRRr%   Nr  rv  rw  rx  ry  rz  )rY  rD   rm  rn  )r:   r;   r<   r   r\  r`  re  rh  rj  rl  rr  ru  r*   r%   r#   r|  r|    s           C C C C? ? ? ?. . . .9 9 9 94 4 4 4? ? ? ?	 	 	 	S S S S S Sr%   r|  c                      e Zd Zd	dZd
dZdS )HopDispatchSetCacherX   r  c                <    ddl m} |t                      i| _        d S )Nr   )invoke_subgraph)'torch._higher_order_ops.invoke_subgraphr  r|  hop_cache_map)r!   r  s     r#   r   zHopDispatchSetCache.__init__  s.    KKKKKK-/B/D/DEr%   optorch._ops.HigherOrderOperatorOptional[HopSubgraphCache]c                2    || j         vrd S | j         |         S r[   )r  )r!   r  s     r#   	get_cachezHopDispatchSetCache.get_cache  s%     T'''4!"%%r%   Nr  )r  r  rX   r  )r:   r;   r<   r   r  r*   r%   r#   r  r    s@        F F F F& & & & & &r%   r  c                  p    e Zd Zed	d            Zed
d            Zd Zed             Zed             ZdS )CompileContextrX   c                 6    t           j        J t           j        S r[   )_TLScompile_contextr*   r%   r#   r  zCompileContext.get  s    #///##r%   Optional[CompileContext]c                 .    t          t          dd           S Nr  getattrr  r*   r%   r#   try_getzCompileContext.try_get
      t.555r%   c                b    |t          |t                    sJ || _        d| _        g | _        d S r`   )r   r   r&   rA   shape_env_guards)r!   r&   s     r#   r   zCompileContext.__init__  s9    !Z
I%F%F!!!/9+-r%   c                 J    t                                           } | d S | j        S r[   )r  r  r&   rE   s    r#   current_compile_idz!CompileContext.current_compile_id  s%    %%''<4r%   c                     t                                           } | d S | j        d S t          | j        | j                  S r[   )r  r  r&   r@   rA   rE   s    r#   current_trace_idzCompileContext.current_trace_id  s>    %%''<4?"4t555r%   N)rX   r  )rX   r  )	r:   r;   r<   r   r  r  r   r  r  r*   r%   r#   r  r    s        $ $ $ \$ 6 6 6 \6. . .   \ 6 6 \6 6 6r%   r  c                  L   e Zd ZdZedd            Zedd            Zd Zd Zee	d                         Z
ed	             Zd
 Zeej	        d                         Zeej	        d                         Zeej	        d                         Zed             Zed             ZdS )rA  z
    Provides the currently installed TracingContext, or None.

    Note that it is a staticmethod, and invocations outside of `with tracing()` (see below), are valid but
    will return None.
    rX   Optional[TracingContext]c                 .    t          t          dd           S )Ntracing_contextr  r*   r%   r#   r  zTracingContext.try_get.  r  r%   c                 Z    t                                           x} r| S t          d          )Nz<TracingContext.get() must be called within an ongoing trace.)rA  r  RuntimeError)ctxs    r#   r  zTracingContext.get2  s5     ((***3 	JJ
 
 	
r%   c                   t                      | _        t                      | _        t	                      | _        t                      | _        t                      | _        || _	        g | _
        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d| _        t'                      | _        d| _        t-                      | _        g | _        d S NF)rQ  guards_contextr  module_contextr   global_contextr  previously_inlined_functionspreviously_cleaned_instructions	fake_modeframe_summary_stackloc_in_framefw_metadataaot_graph_nameparams_flatparams_flat_unwrap_subclassesparams_unwrapped_to_flat_indexoutput_strides#force_unspec_int_unbacked_size_liker   tensor_to_contextfakify_first_callr  hop_dispatch_set_cachetraced_code)r!   r  s     r#   r   zTracingContext.__init__:  s    +oo+oo+oo,0FF)/3vv,"#%  !"-1*.2+ JN 490!8!:!:
 "'&9&;&;#+-r%   c                    i | j         _        | j                                         | j                                         d S r[   )r  r  r  clearr  rE   s    r#   r  zTracingContext.cleark  s?     ,.()//111,2244444r%   c               +    K   i }t                                           }|                                 D ]}t          ||          ||<   |                                 D ]\  }}t          |||           	 d V  |                                D ]\  }}t          |||           d S # |                                D ]\  }}t          |||           w xY wr[   )rA  r  r  r  r/   setattr)kwargspriorr  rb  vals        r#   patchzTracingContext.patchr  s         "";;== 	+ 	+C c**E#JJ 	# 	#HCCc""""	'EEE!KKMM ' 'SS#&&&&' 'EKKMM ' 'SS#&&&&'s   4B% %-Cc                     t                                           } | t          j                    S | j        }| j        ||                                 gz   }t          j                            |          S r[   )rA  r  	tracebackStackSummaryr  r  _populate_loc_in_frame_summary	from_list)r!   r   s     r#   rB  zTracingContext.extract_stack  sh    %%''<)+++((T@@BBCCE%//666r%   c                Z    | j         J | j         \  }}}t          j        |||d          S )NF)lookup_line)r  r  FrameSummary)r!   filenamelineno
frame_names       r#   r  z-TracingContext._populate_loc_in_frame_summary  s;     ,,,'+'8$&*%h
PUVVVVr%   c               #    K   t                                           } t          j        j                            | dg           5  t          j        j                            | dd           5  	 d V  n*# t          $ r}t          |d          sd |_         d }~ww xY w	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr  r  
real_stack)	rA  r  unittestmockr  objectr2   hasattrr  )tcr8   s     r#   clear_framezTracingContext.clear_frame  s_      !!M&&r+@"EE	 	M&&r>4@@	 	   $ q,// (#'AL) 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sZ   'B?)B'+A0/B'0
B:BBB'B?'B+	+B?.B+	/B??CCc              #    K   t                                           }| |j                            |            |j        }d |_        	 d V  n<# t
          $ r/}t          |d          s|                                |_         d }~ww xY w	 | |j        	                                 ||_        d S # | |j        	                                 ||_        w xY w)Nr  )
rA  r  r  r   r  r2   r  rB  r  pop)frame_summaryr  oldr8   s       r#   current_framezTracingContext.current_frame  s      
 !!$"))-888o		"EEEE 	 	 	1l++ 2!//11	  (&**,,,!BOOO (&**,,,!BO!!!!s*   A B- 
B*B  BB- -$Cc               #     K   t                                           } | d V  d S | j        }g | _        	 | j        V  || _        d S # || _        w xY wr[   )rA  r  r  )r  old_output_stridess     r#   report_output_stridesz$TracingContext.report_output_strides  st       ##%%:JJJF.	3#### 2B 2B2222s   	A 	Ac                H    | ||ft                                           _        d S r[   )rA  r  r  )r  r  r  s      r#   set_current_loczTracingContext.set_current_loc  s%     .6vz,J)))r%   c                 J    t                                           } | d S | j        S r[   )rA  r  r  )r  s    r#   get_traced_codezTracingContext.get_traced_code  s%    ##%%:4~r%   N)rX   r  )rX   rA  )r:   r;   r<   r  r   r  r  r   r  r   r  rB  r  
contextlibr  r  r  r  r  r*   r%   r#   rA  rA  &  s         6 6 6 \6 
 
 
 \
/. /. /.b5 5 5 ' ' ^ \' 7 7 \7W W W    \< " "  \"& 
3 
3  \
3 K K \K
   \  r%   rA  contextr  c              #     K   t          t          dd           }| t          _        	 | V  |t          _        d S # |t          _        w xY wr  )r  r  r  )r  old_contexts     r#   r  r    sP      $ 1488K"D+*{****s	   8 Ar  c              #    K   t          t          dd          }| t          _        	 | V  n># t          $ r1}t	          |d          s| |                                 |_         d}~ww xY w	 | 1| j        *| j        j        | j        j        	                                 |t          _        dS # | 1| j        *| j        j        | j        j        	                                 |t          _        w xY w)z
    This function installs the passed in tracing context as a dynamic scoped
    global variable.

    Calls to TracingContext.get() while not under a `with tracing()` context
    will return None.
    r  Nr  )
r  r  r  r2   r  rB  r  r  	shape_envcleanup)r  r  r8   s      r#   tracingr    s      $ 1488K"D+   q,'' 	3G,?"0022AL 	 !-!+7'//111* !-!+7'//111*****s&   + B+ 
A&,A!!A&&B+ +AC,c                  F    e Zd Zd Zd Zd ZddZddZdd
ZddZ	d Z
dS )r   c                    dS r  r*   rE   s    r#   is_dict_keyzSource.is_dict_key      ur%   c                    dS r  r*   rE   s    r#   is_ephemeralzSource.is_ephemeral  r  r%   c                    t           r[   NotImplementedError)r!   codegens     r#   reconstructzSource.reconstruct      !!r%   rX   rH   c                    t           r[   r  rE   s    r#   r   zSource.guard_source  r   r%   rD   c                    t           r[   r  rE   s    r#   r   zSource.name  r   r%   r   c                p    |                                  t          j        u rt          t	          | |          S r[   )r   rH   ru   r  r   )r!   fns     r#   
make_guardzSource.make_guard  s0    +"666%%T2r%   rY   c                N    |                                                                  S r[   )r   rg   rE   s    r#   rg   zSource.is_specialized_nn_module"  s       "";;===r%   c                F    |                                  t          j        k    S )z+True if you can guard on attributes of this)r   rH   rz   rE   s    r#   subguards_allowedzSource.subguards_allowed%  s      ""k&AAAr%   Nr   r   )rX   r   rs   )r:   r;   r<   r  r  r  r   r   r  rg   r  r*   r%   r#   r   r     s            " " "" " " "" " " "   
> > > >B B B B Br%   r   c                  .    e Zd ZU ded<   d Zd ZddZdS )	ChainedSourcer   basec                4    | j                                         S r[   )r  r  rE   s    r#   r  zChainedSource.is_dict_key/  s    y$$&&&r%   c                4    | j                                         S r[   )r  r  rE   s    r#   r  zChainedSource.is_ephemeral3  s    y%%'''r%   rX   c                l    | }t          |t                    r|j        }t          |t                    |S r[   )r   r
  r  )r!   currents     r#   get_basezChainedSource.get_base6  s9    -00 	#lG -00 	#r%   N)rX   r   )r:   r;   r<   r=   r  r  r  r*   r%   r#   r
  r
  +  sQ         LLL' ' '( ( (     r%   r
  inputsr   c                   ddl m}m} g }t                                          x}r!|j        }||                    |ddf           ddlm} t          t           |                                D ]-\  }}t          ||          r|                    |d|f           .t          j        |           }	t          |	          D ]2\  }}
t          |
|          r|                    |
j        d|f           3|r[|d         \  }}}|dd         D ]B\  }}}||u s8J d	| d
| d| d| d
| d| d| d| d|j         d| d| d|j                     C|S dS )a  
    Attempts to "detect" what the current fake mode is.  If there is one ambiently
    available from TracingContext, we preferentially use that.  Otherwise, we
    heuristically detect the fake mode via the following sources, in order of
    priority:

        - Currently active fake mode on stack
        - Fake mode associated with passed in tensors (inputs does not
          have to be flattened)
    r   )
FakeTensorFakeTensorModeNztracing context _get_current_dispatch_mode_stackzactive fake modezfake tensor inputrI   zfake mode (z) from r   z doesn't match mode (z

fake mode from z allocated at:
z
fake mode from )torch._subclasses.fake_tensorr  r  rA  r  r  r   torch.utils._python_dispatchr  	enumeratereversedr   pytreetree_leavesr   )r  r  r  
fake_modesr  r  r  imflat_inputs
flat_inputdesc1i1desc2i2s                  r#   detect_fake_moder&  =  sG    IHHHHHHHJ ((***w A%	 y*;Q?@@@MMMMMM(#C#C#E#EFFGG : :1a(( 	:q"4a8999$V,,K";// N N:j*-- 	Nz35H!LMMM 
)!}	5"&qrrN 	 	LAub>>>Hi H H H H H HQR H H[` H Hce H H"'H H*,H H>GoH H"'H H*,H H>?gH H ">>>
 tr%   c                     ddl m}  ddlm} t	          t           |                                D ]\  }}t          ||           r|c S dS )z~
    Inspects the dispatch mode stack for an active fake mode and returns it.
    Returns None if no fake mode is active.
    r   )r  r  N)r  r  r  r  r  r  r   )r  r  rC   r  s       r#   active_fake_moder(  i  s|    
 =<<<<<MMMMMM(#C#C#E#EFFGG  1a(( 	HHH	 4r%   )r  r  )r  r  r[   )r  r   )Q
__future__r   r  dataclassesenumr   loggingre	threadingr  unittest.mockr  r   abcr   collectionsr   r   r   typingr   r	   r
   r   r   r   r   r   r   torch.utilsr   r  torch.utils._backport_slotsr   torch.utils._tracebackr   r   torch.utils.weakr   	getLoggerr:   r   typesr   sympycompiler+   r,   r   r@   EnumrH   r|   r   r   r   r   r   r   r   r   r   r  r  r  r   r.  rQ  rW  r|  r  localr  r  rA  r  r  r   r
  r&  r(  r*   r%   r#   <module>r=     s   " " " " " "               				                    # # # # # # % % % % % % ! ! ! ! ! !	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  ) ) ) ) ) ) 7 7 7 7 7 7 B B B B B B B B 4 4 4 4 4 4 g!!  LLL  RZ PQQ "
X  $ $/J /J /J /J /J /J /J /Jd7 7 7 7 7j 7 7 7;
 ;
 ;
 ;
 ;
$) ;
 ;
 ;
|	 	 	 	 	 	 	 	 d###       $#         a' a' a' a' a' a' a'  a'H GCLL d###	 	 	 	 	 	 	 $#	 d###: : : : :l : : $#: d###* * * * *\ * * $#*

0 0 0 0 0WQZ 0 0 0( ( ( ( ( ( ( (4( ( ( ( ( ( ( (,	+ 	+ 	+ 	+ 	+N#?@ 	+ 	+ 	+( ( ( ( ( ( ( (,    N#?@   D)
 )
 )
 )
 )
 )
 )
 )
X< < < < <N#89 < < <       B*S *S *S *S *S* *S *S *SZ& & & & & & & & y&6 6 6 6 6 6 6 6D| | | | | | | |~ + + + + + + + +8 d###B B B B B B B $#B< d###    F   $#") ) ) ) )X    r%   