o
    ^i                     @  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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'e(Z)erd dl*m+Z+m,Z, d dl-m.Z. d dl/Z/d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 	 e8dZ9e8dZ:eddG dd dZ;G dd deZ<G dd dej=Z>	 G dd dZ?ejddG dd dZ@G d d! d!eZAe!ejG d"d# d#ZBed$ZC	 ejddG d%d& d&ZD	 ejddG d'd( d(eDZE	 ejddG d)d* d*eDZF	 G d+d, d,eeC ZGG d-d. d.ZHG d/d0 d0ZIG d1d2 d2eGeI ZJG d3d4 d4ZKG d5d6 d6eGeK ZLG d7d8 d8ZM	 G d9d: d:eGeH ZNG d;d< d<ZOG d=d> d>eOZPG d?d@ d@ZQeR ZS	 G dAdB dBZTG dCdD dDZUedZdIdJZVed[dMdNZWejddG dOdP dPZXejddG dQdR dReXZYd\d]dVdWZZd^dXdYZ[dS )_    )annotationsN)abstractmethod)defaultdict)contextmanager)	dataclass)AnyCallableGeneric
NamedTupleOptionalTYPE_CHECKINGTypeVarUnion)_pytree)dataclass_slots)CapturedTracebackformat_frame)WeakTensorKeyDictionary)	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)frozenc                   @  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Optional[int]frame_idframe_compile_idNcompiled_autograd_idreturnstrc                 C  s~   | j d ur*| jd u | jd u ksJ d}| jd ur"d| j d| j }d| j  | S | jd ur4| jd us6J | j d| j S )N /!)r!   r   r    )self	frame_str r)   H/var/www/html/RAG/RAG_venv/lib/python3.10/site-packages/torch/_guards.py__str__[   s   

zCompileId.__str__
compile_idOptional[str]Optional[CompileId]c              
   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)clsr,   patternr2   groupskver)   r)   r*   from_stringh   s"   zCompileId.from_stringr"   r#   )r,   r-   r"   r.   )__name__
__module____qualname____annotations__r!   r+   classmethodr>   r)   r)   r)   r*   r   L   s   
 
r   c                   @  (   e Zd ZU ded< ded< d
ddZd	S )TraceIdr   r,   r5   attemptr"   r#   c                 C  s&   | j dkr
t| jS | j d| j  S )Nr   _)rG   r#   r,   r'   r)   r)   r*   r+      s   

zTraceId.__str__Nr?   r@   rA   rB   rC   r+   r)   r)   r)   r*   rF      s   
 rF   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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)rK   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULErI   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_guardsrK   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULEr`   )r'   re   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 r]   )rK   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULErI   r)   r)   r*   is_unspecialized_nn_module   s   z&GuardSource.is_unspecialized_nn_modulec                 C  r\   r]   )rK   rl   rm   rI   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 r]   )rK   LOCALrh   r_   rk   rm   rI   r)   r)   r*   is_local   s   zGuardSource.is_localNr"   r[   )r@   rA   rB   rp   GLOBALrh   rg   CONSTANTRANDOM_VALUE	SHAPE_ENVr_   r^   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALrk   rj   rm   rl   r`   ri   rn   ro   rq   r)   r)   r)   r*   rK      s,    



rK   c                   @     e Zd ZdS )GuardBuilderBaseNr@   rA   rB   r)   r)   r)   r*   r{      s    r{   c                   @  rE   )SLocz,Optional[Union[traceback.FrameSummary, str]]framework_locr-   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*   r+      s   

zSLoc.__str__Nr?   rJ   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}   slocr[   size_obliviousNr@   rA   rB   rC   r)   r)   r)   r*   r      s   
 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Optional[list[str]]guard_types	code_listzOptional[object]obj_weakref$Optional[weakref.ReferenceType[Any]]guarded_class_weakrefzOptional[CapturedTraceback]stackz Optional[traceback.StackSummary]
user_stackr   _hashFr[   _unserializabler"   r5   c                 C  s*   | j d u rt| j| jt| jf| _ | j S r]   )r   hashnamesourceidr   rI   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torchrd   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 r]   )r   r'   r   r)   r)   r*   __lt__+  ra   zGuard.__lt__(Callable[[GuardBuilderBase, Guard], Any]c                 C  s   t | jtjr| jjS | jS r]   )r   r   r   r   r   rI   r)   r)   r*   r   .  s   zGuard.inner_create_fnr#   c                 C  
   | j  S r]   )r   r   rI   r)   r)   r*   r   4     
z
Guard.namerK   c                 C  r   r]   )r   guard_sourcerI   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__r@   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   r@   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   r@   r   r   r   r   r   )r'   outputr   r)   r)   r*   r+   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   r7   log	exceptionr#   rstripr   errorjoinformat)r'   r   r)   r)   r*   createl  s   $zGuard.createc                 C  r   r]   )r   ri   rI   r)   r)   r*   ri   u     
zGuard.is_specialized_nn_modulec                 C  r   r]   )r   r`   rI   r)   r)   r*   r`   x  r   zGuard.is_fsdp_modulec                 C  r   r]   )r   rq   rI   r)   r)   r*   rq   {  r   zGuard.is_localc                 C  s(   t | jtjr| jj}|jS | j}|jS r]   )r   r   r   r   r   r@   )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 sJ d|| _| js!|| _n| j| | j|d fv s=t| jr9|  d u s=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'   r   r   r   r   r)   r)   r*   set_export_info  s(   
zGuard.set_export_infor"   r5   )r"   r   )r   r   r"   r[   )r"   r   r?   r"   rK   )r   r   r"   r#   )r   r{   r"   r   rr   )
r   r#   r   r   r   r   r   r   r"   r   )r@   rA   rB   rC   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r+   r   ri   r`   rq   r   r   r)   r)   r)   r*   r      s:   
 







	


r   Tc                   @  rz   )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sJ d S r]   )r   r   rI   r)   r)   r*   __post_init__     zDuplicateInputs.__post_init__Nr"   r   )r@   rA   rB   rC   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 r]   r)   rI   r)   r)   r*   copy_graphstate     zCheckpointable.copy_graphstatestater   c                 C  r   r]   r)   r'   r   r)   r)   r*   restore_graphstate  r   z!Checkpointable.restore_graphstateN)r"   r   )r   r   r"   r   )r@   rA   rB   r   r   r   r)   r)   r)   r*   r     s
    r   c                   @  s>   e Zd ZU dZe 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
    
set[Guard]dynamo_guardsr"   r   c                 C  
   || _ d S r]   )r   )r'   r   r)   r)   r*   __init__  r   zGuardsCheckpointState.__init__r   Optional[set[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 a set() of mismatched
        Guard type objects.
        r   N)r   
differencer   r'   r   rr)   r)   r*   diff  s   zGuardsCheckpointState.diffr   r[   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"   r[   )
r@   rA   rB   __doc__setr   rC   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   r]   r   )r'   r   r)   r)   r*   r     r   z%ModuleContextCheckpointState.__init__r   Optional[set[str]]c                 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)r   r   keysr   r   r   r)   r)   r*   r        z!ModuleContextCheckpointState.diffr   r[   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   )r@   rA   rB   r   rC   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 r]   r   rI   r)   r)   r*   r   $  r   zModuleContext.__init__r   c                 C  s   t t| jS r]   )r   dictr   rI   r)   r)   r*   r   '     zModuleContext.copy_graphstater   c                 C  s   t |tsJ |j| _d S r]   )r   r   r   r   r)   r)   r*   r   *  s   z ModuleContext.restore_graphstateNr   )r"   r   )r   r   r"   r   r@   rA   rB   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   r]   r  )r'   r  r)   r)   r*   r   2  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   5  r  z!GlobalContextCheckpointState.diffr   r[   c                 C  r   r   )r   r  r   r   r)   r)   r*   r   A  r   z#GlobalContextCheckpointState.__eq__N)r  r  r"   r   )r   r  r"   r   r   )r@   rA   rB   r  rC   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  r]   r  rI   r)   r)   r*   r   V  r   zGlobalContext.__init__r  c                 C  
   t | jS r]   )r  r  rI   r)   r)   r*   r   Y  r   zGlobalContext.copy_graphstater   c                 C  sf   t |tsJ |j| _t| jt| jkrt| j | jks#J d| j D ]\}}|| q(d S )NzGlobal state mismatch)r   r  r  r   _supported_global_statesr   r  values)r'   r   r   argsr)   r)   r*   r   \  s   
z GlobalContext.restore_graphstateNr   )r"   r  )r   r  r"   r   )r@   rA   rB   r   r  r   r   r   r)   r)   r)   r*   r  G  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 r]   )r   r  )r'   r  r)   r)   r*   r   j  s   
zGuardsSet.__init__Iterator[Guard]c                 C  r  r]   )iterr  rI   r)   r)   r*   __iter__o  r   zGuardsSet.__iter__r5   c                 C  r  r]   )r   r  rI   r)   r)   r*   __len__r  r   zGuardsSet.__len__r   c                 C  s   t | j|j S r]   )r  r  r   r)   r)   r*   __sub__w  ra   zGuardsSet.__sub__r[   c                 C  r  r]   )r[   r  rI   r)   r)   r*   __bool__z  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 NrL   )r$  )r  r   r   extractr   TracingContextextract_stackadd)r'   r%  r#  r$  r)   r)   r*   r*  }  s   



zGuardsSet.addothersr   c                 G  s&   |D ]}|D ]	}| j |dd qqd S r&  )r*  )r'   r+  ogr)   r)   r*   update  s
   zGuardsSet.updater   r   c                   s(   ddl m   fdd| jD | _dS )z.Delete all guards that contains a given sourcerL   )is_from_sourcec                   s   h | ]
} |j s|qS r)   )r   ).0r-  r/  r   r)   r*   	<setcomp>  s
    z6GuardsSet.remove_guards_with_source.<locals>.<setcomp>N)_dynamo.sourcer/  r  )r'   r   r)   r1  r*   remove_guards_with_source  s   z#GuardsSet.remove_guards_with_sourcer]   )r  r   r"   r   )r"   r  r   )r   r  r"   r  rr   )r%  r   r#  r[   r$  r5   r"   r   )r+  r   r"   r   )r   r   r"   r   )r@   rA   rB   r   r  r   r!  r"  r*  r.  r4  r)   r)   r)   r*   r  i  s    




r  c                   @  r  )GuardsContextr"   r   c                 C  s   t  | _g | _d S r]   )r  r   aotautograd_guardsrI   r)   r)   r*   r     s   
zGuardsContext.__init__r   c                 C  s   t t| jjS r]   )r   r   r   r  rI   r)   r)   r*   r     ra   zGuardsContext.copy_graphstater   c                 C  s   t |tsJ t|j| _d S 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*   r5    s    

r5  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_idr5   
identifierr#   r"   r   c                 C  r   r]   r)   r'   r8  r9  r)   r)   r*   add_dynamo_installed_submodule  r   z/HopSubgraphCache.add_dynamo_installed_submoduler   c                 C  r   r]   r)   r'   r8  r)   r)   r*   get_dynamo_installed_submodules  r   z0HopSubgraphCache.get_dynamo_installed_submoduleskeyr   c                 C  r   r]   r)   r'   r9  r>  r)   r)   r*   add_autograd_key_entry  r   z'HopSubgraphCache.add_autograd_key_entryOptional[Callable]c                 C  r   r]   r)   r'   r9  r)   r)   r*   get_autograd_key_entry  r   z'HopSubgraphCache.get_autograd_key_entryc                 C  r   r]   r)   r?  r)   r)   r*   add_proxy_dispatch_entry  r   z)HopSubgraphCache.add_proxy_dispatch_entryc                 C  r   r]   r)   rB  r)   r)   r*   get_proxy_dispatch_entry  r   z)HopSubgraphCache.get_proxy_dispatch_entrytangent_metadatatuple[object]gmodtorch.fx.GraphModulec                 C  r   r]   r)   )r'   r9  rF  rH  r)   r)   r*   add_lazy_bwd_entry  s   z#HopSubgraphCache.add_lazy_bwd_entry4tuple[Optional[torch.fx.GraphModule], Optional[int]]c                 C  r   r]   r)   r'   r9  rF  r)   r)   r*   get_lazy_bwd_entry  s   z#HopSubgraphCache.get_lazy_bwd_entryNr8  r5   r9  r#   r"   r   r8  r5   r"   r   r9  r#   r>  r   r"   r   r9  r#   r"   rA  r9  r#   rF  rG  rH  rI  r"   r5   r9  r#   rF  rG  r"   rK  )r@   rA   rB   r   r;  r=  r@  rC  rD  rE  rJ  rM  r)   r)   r)   r*   r7    s"    r7  c                   @  sf   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
d)d d!Zd"S )*InvokeSubgraphCacher"   r   c                 C  s$   i | _ i | _tt| _tt| _d S r]   )autograd_cacheproxy_dispatch_cacher   listdynamo_installed_submodulesr  lazy_bwd_cacherI   r)   r)   r*   r     s
   
zInvokeSubgraphCache.__init__r8  r5   r9  r#   c                 C  s   | j | | d S r]   )rX  r   r:  r)   r)   r*   r;    r   z2InvokeSubgraphCache.add_dynamo_installed_submoduler   c                 C  s   | j |g S r]   )rX  getr<  r)   r)   r*   r=    r	  z3InvokeSubgraphCache.get_dynamo_installed_submodulesr>  r   c                 C     || j |< d S r]   )rU  r?  r)   r)   r*   r@    r	  z*InvokeSubgraphCache.add_autograd_key_entryrA  c                 C     | j |d S r]   )rU  rZ  rB  r)   r)   r*   rC    r	  z*InvokeSubgraphCache.get_autograd_key_entryc                 C  r[  r]   )rV  r?  r)   r)   r*   rD    r	  z,InvokeSubgraphCache.add_proxy_dispatch_entryc                 C  r\  r]   )rV  rZ  rB  r)   r)   r*   rE    r	  z,InvokeSubgraphCache.get_proxy_dispatch_entryrF  rG  rH  rI  c                 C  s$   t | j| }||f| j| |< |S r]   )r   rY  )r'   r9  rF  rH  	num_gmodsr)   r)   r*   rJ    s   z&InvokeSubgraphCache.add_lazy_bwd_entryrK  c                 C  s    || j vrdS | j | |dS )N)NN)rY  rZ  rL  r)   r)   r*   rM    s   
z&InvokeSubgraphCache.get_lazy_bwd_entryNr   rN  rO  rP  rQ  rR  rS  )r@   rA   rB   r   r;  r=  r@  rC  rD  rE  rJ  rM  r)   r)   r)   r*   rT    s    







rT  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_subgraphr_  rT  hop_cache_map)r'   r_  r)   r)   r*   r     s   zHopDispatchSetCache.__init__optorch._ops.HigherOrderOperatorOptional[HopSubgraphCache]c                 C  s   || j vrd S | j | S r]   )ra  )r'   rb  r)   r)   r*   	get_cache  s   

zHopDispatchSetCache.get_cacheNr   )rb  rc  r"   rd  )r@   rA   rB   r   re  r)   r)   r)   r*   r^    s    
r^  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sJ t jS r]   )_TLScompile_contextr)   r)   r)   r*   rZ  "  s   zCompileContext.getOptional[CompileContext]c                   C     t tdd S Nrh  getattrrg  r)   r)   r)   r*   try_get'     zCompileContext.try_getr,   r.   r   c                 C  s,   |d u st |tsJ || _d| _g | _d S rb   )r   r   r,   rG   shape_env_guards)r'   r,   r)   r)   r*   r   +  s   
zCompileContext.__init__c                  C     t  } | d u r
d S | jS r]   )rf  rn  r,   rI   r)   r)   r*   current_compile_id2     z!CompileContext.current_compile_idOptional[TraceId]c                  C  s0   t  } | d u r
d S | jd u rd S t| j| jS r]   )rf  rn  r,   rF   rG   rI   r)   r)   r*   current_trace_id9  s   
zCompileContext.current_trace_idN)r"   rf  )r"   ri  )r,   r.   r"   r   )r"   r.   )r"   rt  )	r@   rA   rB   r   rZ  rn  r   rr  ru  r)   r)   r)   r*   rf  !  s    
rf  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 ):r(  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"   Optional[TracingContext]c                   C  rj  )Ntracing_contextrl  r)   r)   r)   r*   rn  K  ro  zTracingContext.try_getc                  C  s   t   } r| S td)Nz<TracingContext.get() must be called within an ongoing trace.)r(  rn  RuntimeError)ctxr)   r)   r*   rZ  O  s
   zTracingContext.get	fake_modeOptional[FakeTensorMode]r   c                 C  s   t  | _t | _t | _t | _t | _|| _	g | _
d | _d | _d | _d | _d | _d | _d | _d | _d| _t | _d| _t | _g | _d S r   )r5  guards_contextr  module_contextr  global_contextr  previously_inlined_functionspreviously_cleaned_instructionsrz  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_callr^  hop_dispatch_set_cachetraced_code)r'   rz  r)   r)   r*   r   W  s(   

zTracingContext.__init__c                 C  s    i | j _| j  | j  d S r]   )r~  r  r  clearr  rI   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 r]   )r(  rZ  r  rm  r4   setattr)r  priorry  r>  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 r]   )r(  rn  	tracebackStackSummaryr  r  _populate_loc_in_frame_summary	from_list)r'   r   r)   r)   r*   r)    s   
zTracingContext.extract_stacktraceback.FrameSummaryc                 C  s,   | j d usJ | j \}}}tj|||ddS )NF)lookup_line)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)	r(  rZ  unittestmockr  r   r7   hasattrr  )tcr=   r)   r)   r*   clear_frame  s    

PzTracingContext.clear_frameframe_summary Optional[traceback.FrameSummary]c              
   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  )
r(  rZ  r  r   r  r7   r  r)  r  pop)r  r  oldr=   r)   r)   r*   current_frame  s*   





zTracingContext.current_frame@Generator[Optional[list[Optional[tuple[int, ...]]]], None, None]c                  c  sF    t  } | d u rd V  d S | j}g | _z
| jV  W || _d S || _w r]   )r(  rn  r  )r  old_output_stridesr)   r)   r*   report_output_strides  s   
z$TracingContext.report_output_stridesr  r#   r  r5   r  c                 C  s   | ||ft  _d S r]   )r(  rZ  r  )r  r  r  r)   r)   r*   set_current_loc  s   zTracingContext.set_current_locOptional[list[CodeType]]c                  C  rq  r]   )r(  rn  r  )r  r)   r)   r*   get_traced_code   rs  zTracingContext.get_traced_codeN)r"   rv  )r"   r(  )rz  r{  r"   r   r   )r  r   r"   r  )r"   r  )r"   r  )r"   r  )r  r  r"   r  )r"   r  )r  r#   r  r5   r  r#   r"   r   )r"   r  )r@   rA   rB   r   r   rn  rZ  r   r  r   r  r)  r  
contextlibr  r  r  r  r  r)   r)   r)   r*   r(  C  s6    

3
	r(  contextri  r"   /Generator[Optional[CompileContext], None, None]c                 c  s0    t tdd }| t_z	| V  W |t_d S |t_w rk  )rm  rg  rh  )r  old_contextr)   r)   r*   rh    s   rh  rv  /Generator[Optional[TracingContext], 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.
    rw  Nr  )
rm  rg  rw  r7   r  r)  r  rz  	shape_envcleanup)r  r  r=   r)   r)   r*   tracing  s,   




r  c                   @  s\   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
dS )!r   r"   r[   c                 C     dS r   r)   rI   r)   r)   r*   is_dict_key5  r   zSource.is_dict_keyc                 C  r  r   r)   rI   r)   r)   r*   is_ephemeral8  r   zSource.is_ephemeralcodegenr   r   c                 C     t r]   NotImplementedError)r'   r  r)   r)   r*   reconstruct;  r   zSource.reconstructrK   c                 C  r  r]   r  rI   r)   r)   r*   r   >  r   zSource.guard_sourcer#   c                 C  r  r]   r  rI   r)   r)   r*   r   A  r   zSource.namefnCallable[..., Any]r   c                 C  s   |   tju r	tt| |S r]   )r   rK   rt   r  r   )r'   r  r)   r)   r*   
make_guardD  s   
zSource.make_guardc                 C  s   |    S r]   )r   ri   rI   r)   r)   r*   ri   I  s   zSource.is_specialized_nn_modulec                 C  s   |   tjkS )z+True if you can guard on attributes of this)r   rK   ry   rI   r)   r)   r*   subguards_allowedL  s   zSource.subguards_allowedNrr   )r  r   r"   r   r   r?   )r  r  r"   r   )r@   rA   rB   r  r  r  r   r   r  ri   r  r)   r)   r)   r*   r   3  s    






r   c                   @  s4   e Zd ZU ded< dddZdddZdd	d
ZdS )ChainedSourcer   baser"   r[   c                 C  r   r]   )r  r  rI   r)   r)   r*   r  V  r   zChainedSource.is_dict_keyc                 C  r   r]   )r  r  rI   r)   r)   r*   r  Z  r   zChainedSource.is_ephemeralc                 C  s"   | }t |tr|j}t |ts|S r]   )r   r  r  )r'   currentr)   r)   r*   get_base]  s
   

zChainedSource.get_baseNrr   )r"   r   )r@   rA   rB   rC   r  r  r  r)   r)   r)   r*   r  R  s
   
 

r  inputsr   r{  c                 C  sB  ddl m}m} g }t  }r|j}|dur||ddf ddlm} t	t
| D ]\}}t||r=||d|f q,t| }	t	|	D ]\}}
t|
|rY||
jd|f qG|r|d \}}}|dd D ]3\}}}||u sJ d	| d
| d| d| d
| d| d| d| d|j d| d| d|j qi|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   Nztracing context _get_current_dispatch_mode_stackzactive fake modezfake tensor inputrL   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(  rn  rz  r   torch.utils._python_dispatchr  	enumeratereversedr   pytreetree_leavesr   )r  r  r   
fake_modesr  rz  r  imflat_inputs
flat_inputdesc1i1desc2i2r)   r)   r*   detect_fake_moded  sH   



&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  rH   r  r)   r)   r*   active_fake_mode  s   
r  )r  ri  r"   r  )r  rv  r"   r  r]   )r  r   r"   r{  )r"   r{  )\
__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   collections.abcr   r   typesr   sympy"torch._dynamo.backends.distributedr   torch._dynamo.codegenr   &torch._functorch._aot_autograd.schemasr   r  r   compiler0   r1   r   rF   EnumrK   r{   r}   r   r   r   r   r   r   r   r   r   r  r  r  r  r5  r7  rT  r^  localrg  rf  r(  rh  r  r   r  r  r  r)   r)   r)   r*   <module>   s    (

2>
 3


".!/" F

,