o
    ei*                     @  s  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 d dlmZ d dlZd d	lmZ  d d
l!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' d dl(m)Z) e*e+Z,erd dl-m.Z.m/Z/m0Z0 d dl1m2Z2 d dl3Z3d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; 	 e<dZ=e<dZ>eddddG dd dZ?G dd deZ@G dd dejAZB	 G dd dZCejdd G d!d" d"ZDG d#d$ d$eZEejdd%G d&d' d'ZFed(ZG	 ejdd G d)d* d*ZH	 ejdd G d+d, d,eHZI	 ejdd G d-d. d.eHZJ	 G d/d0 d0eeG ZKG d1d2 d2ZLG d3d4 d4ZMG d5d6 d6eKeM ZNG d7d8 d8ZOG d9d: d:eKeO ZPG d;d< d<ZQ	 G d=d> d>eKeL ZRG d?d@ d@ZSG dAdB dBeSZTG dCdD dDZUeV ZW	 G dEdF dFZXeG dGdH dHZYG dIdJ dJZZedkdOdPZ[edldSdTZ\edmdYdZZ]e	dndod]dZZ]e 	dndpd`dZZ]e]dd G dadb dbZ^e]dd G dcdd dde^Z_dndqdgdhZ`drdidjZadS )s    )annotationsN)abstractmethod)defaultdict)contextmanager)	dataclass)AnyGeneric
NamedTupleOptionaloverloadTYPE_CHECKINGTypeVar)dataclass_transform)_pytree)
OrderedSet)is_traceable_wrapper_subclass)CapturedTracebackformat_frame)WeakTensorKeyDictionary)Callable	GeneratorIterator)CodeType)DDPOptimizerContext)	PyCodegen)ViewAndMutationMetaFakeTensorModez-^(?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)frozenkw_onlyslotsc                   @  sB   e Zd ZU ded< ded< dZded< ddd	ZedddZdS )	CompileId
int | Noneframe_idframe_compile_idNcompiled_autograd_idreturnstrc                 C  s   | j d ur4| jd u | jd u krtd| j d| j d}| jd ur,d| j d| j }d| j  | S | jd u s>| jd u rJtd| j d| j | j d| j S )NzMframe_id and frame_compile_id must both be None or both be set, got frame_id=z, frame_compile_id= /!z=frame_id and frame_compile_id must not be None, got frame_id=)r%   r#   r$   AssertionError)self	frame_str r.   W/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/_guards.py__str__S   s,   

zCompileId.__str__
compile_id
str | NoneCompileId | Nonec              
   C  s   |du rdS z1t tfD ]*}|| }r5| }| D ]\}}|dur*t|||< q| di |  W S qt tyL } z	td| d|d}~ww )z
        Factory method that creates a CompileId from its string representation.
        Keep this in sync with the __str__ method.
        NzInvalid compile_id ''r.   )COMPILE_ID_PATTERNCA_COMPILE_ID_PATTERNmatch	groupdictitemsint
ValueError	Exception)clsr1   patternr7   groupskver.   r.   r/   from_stringh   s"   zCompileId.from_stringr&   r'   )r1   r2   r&   r3   )__name__
__module____qualname____annotations__r%   r0   classmethodrC   r.   r.   r.   r/   r!   D   s   
 
r!   c                   @  (   e Zd ZU ded< ded< d
ddZd	S )TraceIdr!   r1   r:   attemptr&   r'   c                 C  s&   | j dkr
t| jS | j d| j  S )Nr   _)rL   r'   r1   r,   r.   r.   r/   r0      s   

zTraceId.__str__NrD   rE   rF   rG   rH   r0   r.   r.   r.   r/   rK      s   
 rK   c                   @  s   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ZdddZdddZdddZdddZdddZdS ) GuardSourcer                           	   
                     r&   boolc                 C     | t jt jfv S N)rP   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULErN   r.   r.   r/   is_fsdp_module      zGuardSource.is_fsdp_modulec                 C  s@   dd l m  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_guardsrP   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULErf   )r,   rj   r.   r.   r/   is_specialized_nn_module   s   z$GuardSource.is_specialized_nn_modulec                 C  s   | t jt jt jt jfv S rc   )rP   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULErN   r.   r.   r/   is_unspecialized_nn_module   s   z&GuardSource.is_unspecialized_nn_modulec                 C  rb   rc   )rP   rq   rr   rN   r.   r.   r/   "is_unspecialized_builtin_nn_module   s   z.GuardSource.is_unspecialized_builtin_nn_modulec                 C  s   | t jt jt jt jt jfv S rc   )rP   LOCALrm   re   rp   rr   rN   r.   r.   r/   is_local   s   zGuardSource.is_localNr&   ra   )rE   rF   rG   ru   GLOBALrm   rl   CONSTANTRANDOM_VALUE	SHAPE_ENVre   rd   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALrp   ro   rr   rq   
TEMP_LOCALrf   rn   rs   rt   rv   r.   r.   r.   r/   rP      s.    



rP   c                   @     e Zd ZdS )GuardBuilderBaseNrE   rF   rG   r.   r.   r.   r/   r      s    r   )r   c                   @  rJ   )SLocz#traceback.FrameSummary | str | Noneframework_locr2   maybe_user_locr&   r'   c                 C  sD   t | jtr	| jnt| j}| jd ur| j d| dS d| dS )Nz ()()
isinstancer   r'   r   r   )r,   flocr.   r.   r/   r0      s   

zSLoc.__str__NrD   rO   r.   r.   r.   r/   r         
 r   c                   @  s&   e Zd ZU ded< ded< ded< dS )
ShapeGuardzsympy.logic.boolalg.Booleanexprr   slocra   size_obliviousNrE   rF   rG   rH   r.   r.   r.   r/   r      s   
 r   )r    c                   @  s   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ed< dCddZdDddZdEddZdFd!d"ZedGd$d%ZedHd'd(ZedId*d+ZdGd,d-ZdGd.d/ZdJd3d4ZdKd5d6ZdKd7d8ZdKd9d:ZdGd;d<ZdLdAdBZdS )MGuardSourceoriginating_sourcez)Callable[[GuardBuilderBase, Guard], None]	create_fnNzlist[str] | Noneguard_types	code_listzobject | Noneobj_weakref!weakref.ReferenceType[Any] | Noneguarded_class_weakrefzCapturedTraceback | Nonestackztraceback.StackSummary | None
user_stackr"   _hashFra   _unserializabler&   r:   c                 C  s*   | j d u rt| j| jt| jf| _ | j S rc   )r   hashnamesourceidr   rN   r.   r.   r/   __hash__  s   
zGuard.__hash__tuple[bool, int, int, str, int]c                 C  sN   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torchri   guardsGuardBuilderDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r,   is_duplicate_inputr.   r.   r/   sort_key  s   
zGuard.sort_keyotherc                 C  s   |   |  k S rc   )r   r,   r   r.   r.   r/   __lt__+  rg   zGuard.__lt__(Callable[[GuardBuilderBase, Guard], Any]c                 C  s   t | jtjr| jjS | jS rc   )r   r   r   r   r   rN   r.   r.   r/   r   .  s   zGuard.inner_create_fnr'   c                 C     | j jS rc   )r   r   rN   r.   r.   r/   r   4     z
Guard.namerP   c                 C  r   rc   )r   guard_sourcerN   r.   r.   r/   r   8  r   zGuard.sourceobjectc                 C  sb   t | tjr-|  }|dur#d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__rE   r'   )r   objr.   r.   r/   weakref_to_str<  s   ,zGuard.weakref_to_strc                 C  s`   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   rE   r   r   r   r   r   )r,   sr.   r.   r/   __repr__U  s    
	zGuard.__repr__c                 C  s   dt | j d}| jr| jj nd}|d| d7 }|d|  j d7 }|d| j d7 }|d| j d7 }|d| | j	 d7 }|d	| j
 d7 }|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   rE   r   r   r   r   r   )r,   outputr   r.   r.   r/   r0   a  s   zGuard.__str__builderr   r   c                 C  s^   z|  || W S  ty.   tdt|   | jr-tdd| j	 dd     w )NzError while creating guard:
%szCreated at:
%sr(   )
r   r<   log	exceptionr'   rstripr   errorjoinformat)r,   r   r.   r.   r/   createl  s   $zGuard.createc                 C  
   | j  S rc   )r   rn   rN   r.   r.   r/   rn   u     
zGuard.is_specialized_nn_modulec                 C  r   rc   )r   rf   rN   r.   r.   r/   rf   x  r   zGuard.is_fsdp_modulec                 C  r   rc   )r   rv   rN   r.   r.   r/   rv   {  r   zGuard.is_localc                 C  s(   t | jtjr| jj}|jS | j}|jS rc   )r   r   r   r   r   rE   )r,   r   r.   r.   r/   create_fn_name~  s
   zGuard.create_fn_name
guard_typeguarded_class	list[str]Nonec                 C  s   | j sg | _ | j | | j|d fvrtd| j d| || _| js(|| _n| j| | j|d fv p?t| jo?|  d u }|sMtd| j d| || _d S )Nz1Guarded class id must be identical, or None, got z vs zHGuarded object must be identical, None or ephemeral (dead weakref), got )r   appendr   r+   r   extendr   callable)r,   r   r   r   r   is_validr.   r.   r/   set_export_info  s8   


zGuard.set_export_infor&   r:   )r&   r   )r   r   r&   ra   )r&   r   rD   r&   rP   )r   r   r&   r'   )r   r   r&   r   rw   )
r   r'   r   r   r   r   r   r   r&   r   )rE   rF   rG   rH   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r0   r   rn   rf   rv   r   r   r.   r.   r.   r/   r      s:   
 







	


r   Tc                   @  r   )GuardEnvExprNr   r.   r.   r.   r/   r     s    r   c                   @  s(   e Zd ZU ded< ded< d	ddZdS )
DuplicateInputsr   input_source_ainput_source_br&   r   c                 C  s    | j | jkrtd| j  d S )Nz9input_source_a and input_source_b must be different, got )r   r   r+   rN   r.   r.   r/   __post_init__  s   zDuplicateInputs.__post_init__Nr&   r   )rE   rF   rG   rH   r   r.   r.   r.   r/   r     r   r   c                   @  s   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   c                   @  s(   e Zd Zed
ddZedddZd	S )Checkpointabler&   r   c                 C     d S rc   r.   rN   r.   r.   r/   copy_graphstate     zCheckpointable.copy_graphstatestater   c                 C  r   rc   r.   r,   r   r.   r.   r/   restore_graphstate  r   z!Checkpointable.restore_graphstateN)r&   r   )r   r   r&   r   )rE   rF   rG   r   r   r   r.   r.   r.   r/   r     s
    r   c                   @  s8   e Zd ZU dZded< dddZdd
dZdddZdS )GuardsCheckpointStatezW
    The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext
    OrderedSet[Guard]dynamo_guardsr&   r   c                 C  
   || _ d S rc   )r   )r,   r   r.   r.   r/   __init__   r   zGuardsCheckpointState.__init__r   Optional[OrderedSet[Guard]]c                 C  s"   | 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 an OrderedSet() of mismatched
        Guard type objects.
        r   N)r   
differencer   r,   r   rr.   r.   r/   diff  s   zGuardsCheckpointState.diffr   ra   c                 C     t |tsdS | |d u S NF)r   r   r   r   r.   r.   r/   __eq__     
zGuardsCheckpointState.__eq__N)r   r   r&   r   )r   r   r&   r   r   r   r&   ra   )rE   rF   rG   __doc__rH   r   r   r   r.   r.   r.   r/   r     s   
 

r   c                   @  s8   e Zd ZU i Zded< dddZdd	d
ZdddZdS )ModuleContextCheckpointStatedict[str, torch.nn.Module]
nn_modulesr&   r   c                 C  r   rc   r  )r,   r  r.   r.   r/   r     r   z%ModuleContextCheckpointState.__init__r   set[str] | Nonec                 C  2   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)setr  keysr   r   r   r.   r.   r/   r        z!ModuleContextCheckpointState.diffr   ra   c                 C  r   r   )r   r  r   r   r.   r.   r/   r   '  r   z#ModuleContextCheckpointState.__eq__N)r  r  r&   r   )r   r  r&   r  r   )rE   rF   rG   r  rH   r   r   r   r.   r.   r.   r/   r    
   
 

r  c                   @  *   e Zd ZdddZdddZdd	d
ZdS )ModuleContextr&   r   c                 C  
   i | _ d S rc   r  rN   r.   r.   r/   r   .  r   zModuleContext.__init__r  c                 C  s   t t| jS rc   )r  dictr  rN   r.   r.   r/   r   1     zModuleContext.copy_graphstater   c                 C  s(   t |tstdt| |j| _d S )Nz+expected ModuleContextCheckpointState, got )r   r  r+   typer  r   r.   r.   r/   r   4  s
   
z ModuleContext.restore_graphstateNr   )r&   r  )r   r  r&   r   rE   rF   rG   r   r   r   r.   r.   r.   r/   r  -  s    

r  c                   @  s8   e Zd ZU i Zded< dddZdd
dZdddZdS )GlobalContextCheckpointStatedict[str, tuple[Callable, Any]]global_stateglobal_statesr&   r   c                 C  r   rc   r  )r,   r  r.   r.   r/   r   ?  r   z%GlobalContextCheckpointState.__init__r   r  c                 C  r  )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   r.   r.   r/   r   B  r
  z!GlobalContextCheckpointState.diffr   ra   c                 C  r   r   )r   r  r   r   r.   r.   r/   r   N  r   z#GlobalContextCheckpointState.__eq__N)r  r  r&   r   )r   r  r&   r  r   )rE   rF   rG   r  rH   r   r   r   r.   r.   r.   r/   r  <  r  r  c                   @  s6   e Zd ZdZh dZdddZddd	Zdd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_enabledr&   r   c                 C  r  rc   r  rN   r.   r.   r/   r   c  r   zGlobalContext.__init__r  c                 C  
   t | jS rc   )r  r  rN   r.   r.   r/   r   f  r   zGlobalContext.copy_graphstater   c                 C  s   t |tstdt| |j| _t| jt| jkr&t| j | jks6tdt| j  d| j | j	 D ]\}}|| q;d S )Nz+expected GlobalContextCheckpointState, got z Global state mismatch: got keys z, expected )
r   r  r+   r  r  r   _supported_global_statesr  r	  values)r,   r   r   argsr.   r.   r/   r   i  s   

z GlobalContext.restore_graphstateNr   )r&   r  )r   r  r&   r   )rE   rF   rG   r  r   r   r   r   r.   r.   r.   r/   r  T  s    
	
r  c                   @  sf   e Zd Zd%d&ddZd'd	d
Zd(ddZd)ddZd*ddZdddd+ddZd,dd Z	d-d#d$Z
dS ).	GuardsSetNinnerr   r&   r   c                 C  s   |d u r
t  | _d S || _d S rc   )r   r$  )r,   r$  r.   r.   r/   r   ~  s   
zGuardsSet.__init__Iterator[Guard]c                 C  r  rc   )iterr$  rN   r.   r.   r/   __iter__  r   zGuardsSet.__iter__r:   c                 C  r  rc   )r   r$  rN   r.   r.   r/   __len__  r   zGuardsSet.__len__r   c                 C  s   t | j|j S rc   )r#  r$  r   r.   r.   r/   __sub__  rg   zGuardsSet.__sub__ra   c                 C  r  rc   )ra   r$  rN   r.   r.   r/   __bool__  r   zGuardsSet.__bool__Tr   )collect_debug_stackskipguardr   r+  r,  c                C  sR   || j v rd S |r|jd u rtjd| d|_|jd u r!t |_| j | d S NrQ   )r,  )r$  r   r   extractr   TracingContextextract_stackadd)r,   r-  r+  r,  r.   r.   r/   r2    s   



zGuardsSet.addothers
set[Guard]c                 G  s&   |D ]}|D ]	}| j |dd qqd S r.  )r2  )r,   r3  ogr.   r.   r/   update  s
   zGuardsSet.updater   r   c                   s,   ddl m  t fdd| jD | _dS )z.Delete all guards that contains a given sourcerQ   )is_from_sourcec                 3  s     | ]} |j s|V  qd S rc   )r   ).0r6  r8  r   r.   r/   	<genexpr>  s    
z6GuardsSet.remove_guards_with_source.<locals>.<genexpr>N)_dynamo.sourcer8  r   r$  )r,   r   r.   r:  r/   remove_guards_with_source  s   z#GuardsSet.remove_guards_with_sourcerc   )r$  r   r&   r   )r&   r%  r   )r   r#  r&   r#  rw   )r-  r   r+  ra   r,  r:   r&   r   )r3  r4  r&   r   )r   r   r&   r   )rE   rF   rG   r   r'  r(  r)  r*  r2  r7  r=  r.   r.   r.   r/   r#  }  s    




r#  c                   @  r  )GuardsContextr&   r   c                 C  s   t  | _g | _d S rc   )r#  r   aotautograd_guardsrN   r.   r.   r/   r     s   
zGuardsContext.__init__r   c                 C  s   t t| jjS rc   )r   r   r   r$  rN   r.   r.   r/   r     rg   zGuardsContext.copy_graphstater   c                 C  s,   t |tstdt| t|j| _d S )Nz$expected GuardsCheckpointState, got )r   r   r+   r  r#  r   r   r.   r.   r/   r     s   
z GuardsContext.restore_graphstateNr   )r&   r   )r   r   r&   r   r  r.   r.   r.   r/   r>    s    

r>  c                   @  s|   e Zd Zed!ddZed"d
dZed#ddZed$ddZed#ddZed$ddZ	ed%ddZ
ed&ddZd S )'HopSubgraphCachefn_idr:   
identifierr'   r&   r   c                 C  r   rc   r.   r,   rA  rB  r.   r.   r/   add_dynamo_installed_submodule  r   z/HopSubgraphCache.add_dynamo_installed_submoduler   c                 C  r   rc   r.   r,   rA  r.   r.   r/   get_dynamo_installed_submodules  r   z0HopSubgraphCache.get_dynamo_installed_submoduleskeyr   c                 C  r   rc   r.   r,   rB  rG  r.   r.   r/   add_autograd_key_entry  r   z'HopSubgraphCache.add_autograd_key_entryCallable | Nonec                 C  r   rc   r.   r,   rB  r.   r.   r/   get_autograd_key_entry  r   z'HopSubgraphCache.get_autograd_key_entryc                 C  r   rc   r.   rH  r.   r.   r/   add_proxy_dispatch_entry  r   z)HopSubgraphCache.add_proxy_dispatch_entryc                 C  r   rc   r.   rK  r.   r.   r/   get_proxy_dispatch_entry  r   z)HopSubgraphCache.get_proxy_dispatch_entrytangent_metadatatuple[object]gmodtorch.fx.GraphModulec                 C  r   rc   r.   )r,   rB  rO  rQ  r.   r.   r/   add_lazy_bwd_entry  s   z#HopSubgraphCache.add_lazy_bwd_entry.tuple[torch.fx.GraphModule | None, int | None]c                 C  r   rc   r.   r,   rB  rO  r.   r.   r/   get_lazy_bwd_entry     z#HopSubgraphCache.get_lazy_bwd_entryNrA  r:   rB  r'   r&   r   rA  r:   r&   r   rB  r'   rG  r   r&   r   rB  r'   r&   rJ  rB  r'   rO  rP  rQ  rR  r&   r:   rB  r'   rO  rP  r&   rT  )rE   rF   rG   r   rD  rF  rI  rL  rM  rN  rS  rV  r.   r.   r.   r/   r@    s"    r@  c                   @  sz   e Zd Zd*ddZd+d	d
Zd,ddZd-ddZd.ddZd-ddZd.ddZ	d/ddZ
d0d d!Zd1d$d%Zd2d'd(Zd)S )3InvokeSubgraphCacher&   r   c                 C  s*   i | _ i | _tt| _tt| _i | _d S rc   )autograd_cacheproxy_dispatch_cacher   listdynamo_installed_submodulesr  lazy_bwd_cacheeffects_cacherN   r.   r.   r/   r     s   
zInvokeSubgraphCache.__init__rA  r:   rB  r'   c                 C  s   | j | | d S rc   )rb  r   rC  r.   r.   r/   rD    s   z2InvokeSubgraphCache.add_dynamo_installed_submoduler   c                 C  s   | j |g S rc   )rb  getrE  r.   r.   r/   rF    r  z3InvokeSubgraphCache.get_dynamo_installed_submodulesrG  r   c                 C     || j |< d S rc   )r_  rH  r.   r.   r/   rI    r  z*InvokeSubgraphCache.add_autograd_key_entryrJ  c                 C     | j |d S rc   )r_  re  rK  r.   r.   r/   rL    r  z*InvokeSubgraphCache.get_autograd_key_entryc                 C  rf  rc   )r`  rH  r.   r.   r/   rM    r  z,InvokeSubgraphCache.add_proxy_dispatch_entryc                 C  rg  rc   )r`  re  rK  r.   r.   r/   rN    r  z,InvokeSubgraphCache.get_proxy_dispatch_entryrO  rP  rQ  rR  c                 C  s$   t | j| }||f| j| |< |S rc   )r   rc  )r,   rB  rO  rQ  	num_gmodsr.   r.   r/   rS    s   z&InvokeSubgraphCache.add_lazy_bwd_entryrT  c                 C  s    || j vrdS | j | |dS )N)NN)rc  re  rU  r.   r.   r/   rV    s   
z&InvokeSubgraphCache.get_lazy_bwd_entryeffectsr  c                 C  sD   | j |d }r||krtd| d| d| d|| j |< dS )z>Store the effect types for a given invoke_subgraph identifier.NzPDifferent number of effects were found for invoke_subgraph call with identifier z,. 
Previously we had the following effects: z.
But now we have: .)rd  re  r+   )r,   rB  ri  prev_effectsr.   r.   r/   add_effects  s   zInvokeSubgraphCache.add_effects
set | Nonec                 C  s   | j |dS )zARetrieve the effect types for a given invoke_subgraph identifier.N)rd  re  rK  r.   r.   r/   get_effects!  s   zInvokeSubgraphCache.get_effectsNr   rX  rY  rZ  r[  r\  r]  )rB  r'   ri  r  r&   r   )rB  r'   r&   rm  )rE   rF   rG   r   rD  rF  rI  rL  rM  rN  rS  rV  rl  rn  r.   r.   r.   r/   r^    s    









r^  c                   @  s    e Zd ZdddZddd	Zd
S )HopDispatchSetCacher&   r   c                 C  s   ddl m} |t i| _d S )Nr   )invoke_subgraph)'torch._higher_order_ops.invoke_subgraphrp  r^  hop_cache_map)r,   rp  r.   r.   r/   r   '  s   zHopDispatchSetCache.__init__optorch._ops.HigherOrderOperatorHopSubgraphCache | Nonec                 C  s   || j vrd S | j | S rc   )rr  )r,   rs  r.   r.   r/   	get_cache-  s   

zHopDispatchSetCache.get_cacheNr   )rs  rt  r&   ru  )rE   rF   rG   r   rv  r.   r.   r.   r/   ro  &  s    
ro  c                   @  sN   e Zd ZedddZedddZdd
dZedddZedddZdS )CompileContextr&   c                   C  s   t jd u r	tdt jS )Nzcompile_context is not set)_TLScompile_contextr+   r.   r.   r.   r/   re  I  s   
zCompileContext.getCompileContext | Nonec                   C     t tdd S Nry  getattrrx  r.   r.   r.   r/   try_getO     zCompileContext.try_getr1   r3   r   c                 C  s:   |d urt |tstdt| || _d| _g | _d S )Nz*compile_id must be None or CompileId, got r   )r   r!   r+   r  r1   rL   shape_env_guards)r,   r1   r.   r.   r/   r   S  s   
zCompileContext.__init__c                  C     t  } | d u r
d S | jS rc   )rw  r  r1   rN   r.   r.   r/   current_compile_id]     z!CompileContext.current_compile_idTraceId | Nonec                  C  s0   t  } | d u r
d S | jd u rd S t| j| jS rc   )rw  r  r1   rK   rL   rN   r.   r.   r/   current_trace_idd  s   
zCompileContext.current_trace_idN)r&   rw  )r&   rz  )r1   r3   r&   r   )r&   r3   )r&   r  )	rE   rF   rG   r   re  r  r   r  r  r.   r.   r.   r/   rw  H  s    

rw  c                   @  s*   e Zd ZU dZded< ded< ded< dS )	InlinedCodeCachez8Cache for code-object-derived data used during inlining.z	list[Any]instructionszdict[Any, int]indexofdict[str, Any]code_optionsN)rE   rF   rG   r  rH   r.   r.   r.   r/   r  n  s
   
 r  c                   @  s   e Zd ZdZed.ddZed/ddZd0ddZd1ddZee	d2ddZ
ed3ddZd4ddZeej	d5ddZeej	d6ddZeej	d7d!d"Zed8d(d)Zed9d+d,Zd-S ):r0  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.
    r&   TracingContext | Nonec                   C  r{  )Ntracing_contextr}  r.   r.   r.   r/   r    r  zTracingContext.try_getc                  C  s   t   } r| S td)Nz<TracingContext.get() must be called within an ongoing trace.)r0  r  RuntimeError)ctxr.   r.   r/   re    s
   zTracingContext.get	fake_modeFakeTensorMode | Noner   c                 C  s   t  | _t | _t | _t | _t | _t | _	|| _
g | _d | _d | _d | _d | _d | _d | _d | _d | _d| _t | _d| _t | _g | _d S r   )r>  guards_contextr  module_contextr  global_contextr  previously_inlined_functionspreviously_cleaned_instructionsinlined_code_cacher  frame_summary_stackloc_in_framefw_metadataddp_optimizer_ctx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_callro  hop_dispatch_set_cachetraced_code)r,   r  r.   r.   r/   r     s*   

zTracingContext.__init__c                 C  s*   i | j _| j  | j  | j  d S rc   )r  r  r  clearr  r  rN   r.   r.   r/   r    s   

zTracingContext.clearkwargsr   Generator[None, None, None]c                  k  s    i }t  }| D ]	}t||||< q	|  D ]
\}}t||| qzd V  W | D ]
\}}t||| q+d S | D ]
\}}t||| q<w rc   )r0  re  r~  r9   setattr)r  priorr  rG  valr.   r.   r/   patch  s   zTracingContext.patchtraceback.StackSummaryc                  C  sB   t  } | d u rt S | j}| jd ur||  g }tj|S rc   )r0  r  	tracebackStackSummaryr  r  _populate_loc_in_frame_summary	from_list)r,   r   r.   r.   r/   r1    s   
zTracingContext.extract_stacktraceback.FrameSummaryc                 C  s0   | j d u r	td| j \}}}tj|||ddS )Nzloc_in_frame must not be NoneF)lookup_line)r  r+   r  FrameSummary)r,   filenamelineno
frame_namer.   r.   r/   r    s   
z-TracingContext._populate_loc_in_frame_summaryc                  c  s    t  } tjj| dg E tjj| dd # zd V  W n ty3 } z	t|ds.d |_ d }~ww W d    n1 s>w   Y  W d    d S W d    d S 1 sVw   Y  d S )Nr  r  
real_stack)	r0  re  unittestmockr  r   r<   hasattrr  )tcrB   r.   r.   r/   clear_frame  s    

PzTracingContext.clear_frameframe_summarytraceback.FrameSummary | Nonec              
   c  s    t  }| d ur|j|  |j}d |_z,zd V  W n ty2 } zt|ds-| |_ d }~ww W | d ur=|j	  ||_d S | d urK|j	  ||_w )Nr  )
r0  re  r  r   r  r<   r  r1  r  pop)r  r  oldrB   r.   r.   r/   current_frame  s*   





zTracingContext.current_frame:Generator[list[tuple[int, ...] | None] | None, None, None]c                  c  sF    t  } | d u rd V  d S | j}g | _z
| jV  W || _d S || _w rc   )r0  r  r  )r  old_output_stridesr.   r.   r/   report_output_strides"  s   
z$TracingContext.report_output_stridesr  r'   r  r:   r  c                 C  s   | ||ft  _d S rc   )r0  re  r  )r  r  r  r.   r.   r/   set_current_loc2  s   zTracingContext.set_current_loclist[CodeType] | Nonec                  C  r  rc   )r0  r  r  )r  r.   r.   r/   get_traced_code8  r  zTracingContext.get_traced_codeN)r&   r  )r&   r0  )r  r  r&   r   r   )r  r   r&   r  )r&   r  )r&   r  )r&   r  )r  r  r&   r  )r&   r  )r  r'   r  r:   r  r'   r&   r   )r&   r  )rE   rF   rG   r  r   r  re  r   r  r   r  r1  r  
contextlibr  r  r  r  r  r.   r.   r.   r/   r0  w  s6    

5
	r0  contextrz  r&   ,Generator[CompileContext | None, None, None]c                 c  s0    t tdd }| t_z	| V  W |t_d S |t_w r|  )r~  rx  ry  )r  old_contextr.   r.   r/   ry  @  s   ry  r  ,Generator[TracingContext | None, None, None]c              
   c  s    t tdd}| t_z<z| V  W n ty+ } zt|ds&| dur&|  |_ d}~ww W | durB| jdurB| jjdurB| jj	  |t_dS | dur\| jdur\| jjdur\| jj	  |t_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~  rx  r  r<   r  r1  r  r  	shape_envcleanup)r  r  rB   r.   r.   r/   tracingL  s,   




r  r=   type[T]r  r   c                 K  r   rc   r.   r=   r  r.   r.   r/   dataclass_with_cached_hashi  r   r  r   Callable[[type[T]], type[T]]c                 K  r   rc   r.   r  r.   r.   r/   r  m  rW  type[T] | None&type[T] | Callable[[type[T]], type[T]]c                   s"   d fdd}| d u r|S || S )N	cls_innerr  r&   c                   s>   t j| fi }| j d fdd}dd }||_||_|S )Nr&   r:   c                   s"   t | dst| d |  | jS )Nr   )r  r   __setattr__r   rN   old_hashr.   r/   r   {  s   
z:dataclass_with_cached_hash.<locals>.wrap.<locals>.__hash__c                   s*   t  }t fdd|D } j|fS )Nc                 3  s    | ]	}t  |jV  qd S rc   )r~  r   )r9  frN   r.   r/   r;    s    zOdataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__.<locals>.<genexpr>)dataclassesfieldstupler   )r,   r  field_valuesr.   rN   r/   
__reduce__  s   

z<dataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__r   )r  r   r   r  )r  new_clsr   r  r  r  r/   wrapw  s   z(dataclass_with_cached_hash.<locals>.wrap)r  r  r&   r  r.   )r=   r  r  r.   r  r/   r  s  s   c                   @  s   e Zd Zd&ddZd&ddZd'd
dZejd(ddZe	d)ddZ
ejd)ddZd*ddZd+dd Zd&d!d"Zd&d#d$Zd%S ),r   r&   ra   c                 C     dS r   r.   rN   r.   r.   r/   is_dict_key  r   zSource.is_dict_keyc                 C  r  r   r.   rN   r.   r.   r/   is_ephemeral  r   zSource.is_ephemeralcodegenr   r   c                 C     t rc   NotImplementedError)r,   r  r.   r.   r/   reconstruct  r   zSource.reconstructrP   c                 C  r  rc   r  rN   r.   r.   r/   r     s   zSource.guard_sourcer'   c                 C  r  )a  
        A template for the name of the source. Used to prevent code duplication between
        `name` and `get_value`.

        For non-ChainedSources, `name` and `get_value` use the returned string directly.

        For ChainedSources, `name` and `get_value` expect the return to be a format string
        with `{0}` present - `name` and `get_value` will apply different values to this function's
        returned format string.
        r  rN   r.   r.   r/   _name_template  s   zSource._name_templatec                 C  s   | j S rc   )r  rN   r.   r.   r/   r     s   zSource.nameglobalsr  localscache&weakref.WeakKeyDictionary[Source, Any]r   c                 C  s*   | |v r||  S t | j||}||| < |S rc   )evalr  )r,   r  r  r  r   r.   r.   r/   	get_value  s
   zSource.get_valuefnCallable[..., Any]r   c                 C  s   | j tju rtt| |S rc   )r   rP   ry   r  r   )r,   r  r.   r.   r/   
make_guard  s   
zSource.make_guardc                 C  r   rc   )r   rn   rN   r.   r.   r/   rn     r   zSource.is_specialized_nn_modulec                 C  s   | j tjkS )z+True if you can guard on attributes of this)r   rP   r~   rN   r.   r.   r/   subguards_allowed  r  zSource.subguards_allowedNrw   )r  r   r&   r   r   rD   r  r  r  r  r  r  r&   r   )r  r  r&   r   )rE   rF   rG   r  r  r  r   cached_propertyr   r   r  r   r  r  rn   r  r.   r.   r.   r/   r     s    





r   c                   @  s^   e Zd ZU ded< dddZdddZejdd
dZdddZ	ejdddZ
dddZdS )ChainedSourcer   baser&   ra   c                 C  r   rc   )r  r  rN   r.   r.   r/   r    s   
zChainedSource.is_dict_keyc                 C  r   rc   )r  r  rN   r.   r.   r/   r    r   zChainedSource.is_ephemeralrP   c                 C  r   rc   )r  r   rN   r.   r.   r/   r     r   zChainedSource.guard_sourcec                 C  s"   | }t |tr|j}t |ts|S rc   )r   r  r  )r,   currentr.   r.   r/   get_base  s
   

zChainedSource.get_baser'   c                 C  s   | j | jjS rc   )r  r   r  r   rN   r.   r.   r/   r     s   zChainedSource.namer  r  r  r  r  r   c                 C  st   | |v r||  S d}d}||v rd| }|d7 }||v s| j |||||< t| j|||}||= ||| < |S )Ntmpr   rQ   )r  r  r  r  r   )r,   r  r  r  tmpvarcounterr   r.   r.   r/   r    s   
zChainedSource.get_valueNrw   r   )r&   r   rD   r  )rE   rF   rG   rH   r  r  r   r  r   r  r   r  r.   r.   r.   r/   r    s   
 


r  inputsr  c                   s  ddl m m}m} t  }r|j}|dur|S g }ddlm} t	t
| D ]\}t||r9||df q(t| }t	|D ]5\}	t|	 rU||	jdf t|	rxg }
||	|
d  fdd	|
D }|fd
d	t	|D  qC|r|d \}}}|dd D ]3\}}}||urtd| d| d| d| d| d| d| d| d|j d| d| d|j q|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   )
FakeTensorr   get_plain_tensorsN _get_current_dispatch_mode_stackzactive fake modezfake tensor input)outc                   s   g | ]	}t | r|qS r.   )r   )r9  x)r  r.   r/   
<listcomp>$  s
    
z$detect_fake_mode.<locals>.<listcomp>c                   s"   g | ]\}}|j d   |fqS )zsubclass input )r  )r9  ixtensor)ir.   r/   r  (  s    rQ   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   r  r0  r  r  torch.utils._python_dispatchr  	enumeratereversedr   r   pytreetree_leavesr   r   r+   r   )r   r   r  r  r  
fake_modesr  mflat_inputs
flat_inputr  fake_tensorsdesc1i1desc2i2r.   )r  r
  r/   detect_fake_mode  sb   	




&r  c                  C  sF   ddl m}  ddlm} tt| D ]\}}t|| r |  S q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  rM   r  r.   r.   r/   active_fake_mode=  s   
r  )r  rz  r&   r  )r  r  r&   r  )r=   r  r  r   r&   r  rc   )r=   r   r  r   r&   r  )r=   r  r  r   r&   r  )r   r   r&   r  )r&   r  )b
__future__r   r  r  enumr   loggingre	threadingr  unittest.mockr  r   abcr   collectionsr   r   r   typingr   r   r	   r
   r   r   r   typing_extensionsr   r   torch.utilsr   r  torch.utils._ordered_setr   r  r   torch.utils._tracebackr   r   torch.utils.weakr   	getLoggerrE   r   collections.abcr   r   r   typesr   sympy"torch._dynamo.backends.distributedr   torch._dynamo.codegenr   &torch._functorch._aot_autograd.schemasr   r  r   compiler5   r6   r!   rK   EnumrP   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r#  r>  r@  r^  ro  localrx  rw  r  r0  ry  r  r  r   r  r  r  r.   r.   r.   r/   <module>   s    $

:?

 9


)/!B& J :,A