o
    ,h]                     @  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+ d dl,Z,	 e-dZ.e-dZ/eddG dd dZ0G dd deZ1G dd dej2Z3	 G dd dZ4ejddG dd dZ5G dd deZ6e!ejG dd dZ7edZ8	 ejddG d d! d!Z9	 ejddG d"d# d#e9Z:	 ejddG d$d% d%e9Z;	 G d&d' d'ee8 Z<G d(d) d)Z=G d*d+ d+Z>G d,d- d-e<e> Z?G d.d/ d/Z@G d0d1 d1e<e@ ZAG d2d3 d3ZB	 G d4d5 d5e<e= ZCG d6d7 d7ZDG d8d9 d9eDZEG d:d; d;ZFeG ZH	 G d<d= d=ZIG d>d? d?ZJedQdBdCZKedRdEdFZLejddG dGdH dHZMejddG dIdJ dJeMZNdSdTdMdNZOdOdP ZPdS )U    )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                   @  s@   e Zd ZU ded< ded< dZded< dd Zedd
dZdS )	CompileIdOptional[int]frame_idframe_compile_idNcompiled_autograd_idc                 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/scripts/venv/lib/python3.10/site-packages/torch/_guards.py__str__V   s   

zCompileId.__str__
compile_idOptional[str]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#   patternr(   groupskver    r    r!   from_stringc   s"   zCompileId.from_string)r#   r$   )__name__
__module____qualname____annotations__r   r"   classmethodr4   r    r    r    r!   r   G   s   
 r   c                   @  &   e Zd ZU ded< ded< dd ZdS )TraceIdr   r#   r+   attemptc                 C  s&   | j dkr
t| jS | j d| j  S )Nr   _)r<   strr#   r   r    r    r!   r"      s   

zTraceId.__str__Nr5   r6   r7   r8   r"   r    r    r    r!   r;   z   s   
 r;   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 ZdS )GuardSourcer                           	   
                  returnboolc                 C     | t jt jfv S N)rA   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULEr?   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_guardsrA   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULErW   )r   r\   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 rT   )rA   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULEr?   r    r    r!   is_unspecialized_nn_module   s   z&GuardSource.is_unspecialized_nn_modulec                 C  rS   rT   )rA   rc   rd   r?   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 rT   )rA   LOCALr_   rV   rb   rd   r?   r    r    r!   is_local   s   zGuardSource.is_localNrQ   rR   )r5   r6   r7   rg   GLOBALr_   r^   CONSTANTRANDOM_VALUE	SHAPE_ENVrV   rU   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALrb   ra   rd   rc   rW   r`   re   rf   rh   r    r    r    r!   rA      s,    



rA   c                   @     e Zd ZdS )GuardBuilderBaseNr5   r6   r7   r    r    r    r!   rr      s    rr   c                   @  r:   )SLocz,Optional[Union[traceback.FrameSummary, str]]framework_locr$   maybe_user_locc                 C  sD   t | jtr	| jnt| j}| jd ur| j d| dS d| dS )Nz ()()
isinstanceru   r>   r   rv   )r   flocr    r    r!   r"      s   

zSLoc.__str__Nr@   r    r    r    r!   rt         
 rt   c                   @  s&   e Zd ZU ded< ded< ded< dS )
ShapeGuardzsympy.logic.boolalg.Booleanexprrt   slocrR   size_obliviousNr5   r6   r7   r8   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d Zdd Zdd Zdd Zed4ddZed5d d!Zed"d# Zd$d% Zd&d' Zd6d*d+Zd,d- Zd.d/ Zd0d1 Zd2d3 ZdS )7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                 C  s*   | j d u rt| j| jt| jf| _ | j S rT   )r   hashnamesourceidr   r?   r    r    r!   __hash__  s   
zGuard.__hash__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)ry   r   	functoolspartialfunctorchr[   guardsGuardBuilderDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r   is_duplicate_inputr    r    r!   sort_key  s   
zGuard.sort_keyc                 C  s   |   |  k S rT   )r   r   otherr    r    r!   __lt__%  rX   zGuard.__lt__c                 C  s   t | jtjr| jjS | jS rT   )ry   r   r   r   r   r?   r    r    r!   r   (  s   zGuard.inner_create_fnrQ   r>   c                 C  
   | j  S rT   )r   r   r?   r    r    r!   r   .     
z
Guard.namerA   c                 C  r   rT   )r   guard_sourcer?   r    r    r!   r   2  r   zGuard.sourcec                 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>)ry   weakrefReferenceTypehexr   	__class__r5   r>   )r   objr    r    r!   weakref_to_str6  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   r5   r   r   r   r   r   )r   sr    r    r!   __repr__O  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   r5   r   r   r   r   r   )r   outputr   r    r    r!   r"   [  s   zGuard.__str__builderrr   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!   createf  s   $zGuard.createc                 C  r   rT   )r   r`   r?   r    r    r!   r`   o     
zGuard.is_specialized_nn_modulec                 C  r   rT   )r   rW   r?   r    r    r!   rW   r  r   zGuard.is_fsdp_modulec                 C  r   rT   )r   rh   r?   r    r    r!   rh   u  r   zGuard.is_localc                 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   
guard_typeguarded_classr   r   r    r    r!   set_export_infox  s(   
zGuard.set_export_inforQ   r>   rQ   rA   )r   rr   )r5   r6   r7   r8   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r"   r   r`   rW   rh   r   r    r    r    r!   r      s6   
 

	r   Tc                   @  rq   )GuardEnvExprNrs   r    r    r    r!   r     s    r   c                   @  s&   e Zd ZU ded< ded< dd ZdS )DuplicateInputsr   input_source_ainput_source_bc                 C  s   | j | jksJ d S rT   )r   r   r?   r    r    r!   __post_init__     zDuplicateInputs.__post_init__N)r5   r6   r7   r8   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 )CheckpointablerQ   r   c                 C     d S rT   r    r?   r    r    r!   copy_graphstate     zCheckpointable.copy_graphstatestatec                 C  r   rT   r    r   r   r    r    r!   restore_graphstate  r   z!Checkpointable.restore_graphstateN)rQ   r   )r   r   )r5   r6   r7   r   r   r   r    r    r    r!   r     s
    r   c                   @  s8   e Zd ZU dZe Zded< dd Zdd Zdd	 Z	d
S )GuardsCheckpointStatezW
    The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext
    
set[Guard]dynamo_guardsc                 C  
   || _ d S rT   )r   )r   r   r    r    r!   __init__  r   zGuardsCheckpointState.__init__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.diffc                 C     |  |d u S rT   r   r   r    r    r!   __eq__     zGuardsCheckpointState.__eq__N)
r5   r6   r7   __doc__setr   r8   r   r   r   r    r    r    r!   r     s   
 r   c                   @  2   e Zd ZU i Zded< dd Zdd Zdd Zd	S )
ModuleContextCheckpointStatezdict[str, torch.nn.Module]
nn_modulesc                 C  r   rT   r   )r   r   r    r    r!   r     r   z%ModuleContextCheckpointState.__init__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.diffc                 C  r   rT   r   r   r    r    r!   r     r   z#ModuleContextCheckpointState.__eq__N)r5   r6   r7   r   r8   r   r   r   r    r    r    r!   r     
   
 r   c                   @  &   e Zd Zd
ddZdd Zdd Zd	S )ModuleContextrQ   Nonec                 C  
   i | _ d S rT   r   r?   r    r    r!   r     r   zModuleContext.__init__c                 C     t t| jS rT   )r   dictr   r?   r    r    r!   r     r   zModuleContext.copy_graphstatec                 C  s   t |tsJ |j| _d S rT   )ry   r   r   r   r    r    r!   r     s   z ModuleContext.restore_graphstateNrQ   r   r5   r6   r7   r   r   r   r    r    r    r!   r     s    
r   c                   @  r   )
GlobalContextCheckpointStatezdict[str, tuple[Callable, ...]]global_statec                 C  r   rT   r   )r   global_statesr    r    r!   r     r   z%GlobalContextCheckpointState.__init__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     r   z!GlobalContextCheckpointState.diffc                 C  r   rT   r   r   r    r    r!   r   *  r   z#GlobalContextCheckpointState.__eq__N)r5   r6   r7   r   r8   r   r   r   r    r    r    r!   r     r   r   c                   @  s2   e Zd ZdZh dZdddZdd Z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_enabledrQ   r   c                 C  r   rT   r   r?   r    r    r!   r   =  r   zGlobalContext.__init__c                 C  r   rT   )r   r   r   r?   r    r    r!   r   @  r   zGlobalContext.copy_graphstatec                 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)ry   r   r   r   _supported_global_statesr   r   values)r   r   r   argsr    r    r!   r   C  s   
z GlobalContext.restore_graphstateNr   )r5   r6   r7   r   r  r   r   r   r    r    r    r!   r   .  s    
	r   c                   @  sZ   e Zd ZdddZdd Zdd Zdd	 Zd
d ZddddddZdddZ	dd Z
dS )	GuardsSetNc                 C  s   |d u rt  }|| _d S rT   )r   inner)r   r  r    r    r!   r   Q  s   
zGuardsSet.__init__c                 C  
   t | jS rT   )iterr  r?   r    r    r!   __iter__V  r   zGuardsSet.__iter__c                 C  r  rT   )r   r  r?   r    r    r!   __len__Y  r   zGuardsSet.__len__c                 C  s   t | j|j S rT   )r  r  r   r    r    r!   __sub__^  rX   zGuardsSet.__sub__c                 C  r  rT   )rR   r  r?   r    r    r!   __bool__a  r   zGuardsSet.__bool__Tr   )collect_debug_stackskipguardr   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 NrB   )r  )r  r   r   extractr   TracingContextextract_stackadd)r   r  r  r  r    r    r!   r  d  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!   updaten  s
   zGuardsSet.updatec                   s(   ddl m   fdd| jD | _dS )z.Delete all guards that contains a given sourcerB   )is_from_sourcec                   s   h | ]
} |j s|qS r    )r   ).0r  r  r   r    r!   	<setcomp>w  s
    z6GuardsSet.remove_guards_with_source.<locals>.<setcomp>N)_dynamo.sourcer  r  )r   r   r    r  r!   remove_guards_with_sources  s   z#GuardsSet.remove_guards_with_sourcerT   )r  r   )r  r   )r5   r6   r7   r   r
  r  r  r  r  r  r  r    r    r    r!   r  P  s    


r  c                   @  r   )GuardsContextrQ   r   c                 C  s   t  | _g | _d S rT   )r  r   aotautograd_guardsr?   r    r    r!   r     s   
zGuardsContext.__init__c                 C  s   t t| jjS rT   )r   r   r   r  r?   r    r    r!   r     rX   zGuardsContext.copy_graphstatec                 C  s   t |tsJ t|j| _d S rT   )ry   r   r  r   r   r    r    r!   r     s   z GuardsContext.restore_graphstateNr   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>   c                 C  r   rT   r    r   r#  r$  r    r    r!   add_dynamo_installed_submodule  r   z/HopSubgraphCache.add_dynamo_installed_submodulerQ   	list[str]c                 C  r   rT   r    r   r#  r    r    r!   get_dynamo_installed_submodules  r   z0HopSubgraphCache.get_dynamo_installed_submoduleskeyr   c                 C  r   rT   r    r   r$  r*  r    r    r!   add_autograd_key_entry  r   z'HopSubgraphCache.add_autograd_key_entryc                 C  r   rT   r    r   r$  r    r    r!   get_autograd_key_entry  r   z'HopSubgraphCache.get_autograd_key_entryc                 C  r   rT   r    r+  r    r    r!   add_proxy_dispatch_entry  r   z)HopSubgraphCache.add_proxy_dispatch_entryc                 C  r   rT   r    r-  r    r    r!   get_proxy_dispatch_entry  r   z)HopSubgraphCache.get_proxy_dispatch_entrytangent_metadatatuple[object]gmodtorch.fx.GraphModulec                 C  r   rT   r    )r   r$  r1  r3  r    r    r!   add_lazy_bwd_entry  s   z#HopSubgraphCache.add_lazy_bwd_entryc                 C  r   rT   r    r   r$  r1  r    r    r!   get_lazy_bwd_entry  s   z#HopSubgraphCache.get_lazy_bwd_entryNr#  r+   r$  r>   r#  r+   rQ   r'  r$  r>   r*  r   r$  r>   r$  r>   r1  r2  r3  r4  )r$  r>   r1  r2  rQ   r+   )r5   r6   r7   r   r&  r)  r,  r.  r/  r0  r5  r7  r    r    r    r!   r"    s"    r"  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 )(InvokeSubgraphCacherQ   r   c                 C  s$   i | _ i | _tt| _tt| _d S rT   )autograd_cacheproxy_dispatch_cacher   listdynamo_installed_submodulesr   lazy_bwd_cacher?   r    r    r!   r     s
   
zInvokeSubgraphCache.__init__r#  r+   r$  r>   c                 C  s   | j | | d S rT   )rA  r   r%  r    r    r!   r&    r   z2InvokeSubgraphCache.add_dynamo_installed_submoduler'  c                 C  s   | j |g S rT   )rA  getr(  r    r    r!   r)    r   z3InvokeSubgraphCache.get_dynamo_installed_submodulesr*  r   c                 C     || j |< d S rT   )r>  r+  r    r    r!   r,    r   z*InvokeSubgraphCache.add_autograd_key_entryc                 C     | j |d S rT   )r>  rC  r-  r    r    r!   r.    r   z*InvokeSubgraphCache.get_autograd_key_entryc                 C  rD  rT   )r?  r+  r    r    r!   r/    r   z,InvokeSubgraphCache.add_proxy_dispatch_entryc                 C  rE  rT   )r?  rC  r-  r    r    r!   r0    r   z,InvokeSubgraphCache.get_proxy_dispatch_entryr1  r2  r3  r4  c                 C  s$   t | j| }||f| j| |< |S rT   )r   rB  )r   r$  r1  r3  	num_gmodsr    r    r!   r5    s   z&InvokeSubgraphCache.add_lazy_bwd_entryc                 C  s    || j vrdS | j | |dS )N)NN)rB  rC  r6  r    r    r!   r7    s   
z&InvokeSubgraphCache.get_lazy_bwd_entryNr   r8  r9  r:  r;  r<  )r$  r>   r1  r2  )r5   r6   r7   r   r&  r)  r,  r.  r/  r0  r5  r7  r    r    r    r!   r=    s    







r=  c                   @  s    e Zd ZdddZddd	Zd
S )HopDispatchSetCacherQ   r   c                 C  s   ddl m} |t i| _d S )Nr   )invoke_subgraph)'torch._higher_order_ops.invoke_subgraphrH  r=  hop_cache_map)r   rH  r    r    r!   r     s   zHopDispatchSetCache.__init__optorch._ops.HigherOrderOperatorOptional[HopSubgraphCache]c                 C  s   || j vrd S | j | S rT   )rJ  )r   rK  r    r    r!   	get_cache  s   

zHopDispatchSetCache.get_cacheNr   )rK  rL  rQ   rM  )r5   r6   r7   r   rN  r    r    r    r!   rG    s    
rG  c                   @  sH   e Zd ZedddZedddZdd Zed	d
 Zedd ZdS )CompileContextrQ   c                   C  s   t jd usJ t jS rT   )_TLScompile_contextr    r    r    r!   rC    s   zCompileContext.getOptional[CompileContext]c                   C     t tdd S NrQ  getattrrP  r    r    r    r!   try_get
     zCompileContext.try_getc                 C  s,   |d u st |tsJ || _d| _g | _d S rY   )ry   r   r#   r<   shape_env_guards)r   r#   r    r    r!   r     s   
zCompileContext.__init__c                  C     t  } | d u r
d S | jS rT   )rO  rW  r#   r?   r    r    r!   current_compile_id     z!CompileContext.current_compile_idc                  C  s0   t  } | d u r
d S | jd u rd S t| j| jS rT   )rO  rW  r#   r;   r<   r?   r    r    r!   current_trace_id  s   
zCompileContext.current_trace_idN)rQ   rO  )rQ   rR  )	r5   r6   r7   r   rC  rW  r   r[  r]  r    r    r    r!   rO    s    
rO  c                   @  s   e Zd ZdZedddZedddZdd	 Zd
d Zee	dd Z
edd Zdd Zeej	dd Zeej	dd Zeej	dd Zedd Zed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.
    rQ   Optional[TracingContext]c                   C  rS  )Ntracing_contextrU  r    r    r    r!   rW  .  rX  zTracingContext.try_getc                  C  s   t   } r| S td)Nz<TracingContext.get() must be called within an ongoing trace.)r  rW  RuntimeError)ctxr    r    r!   rC  2  s
   zTracingContext.getc                 C  s   t  | _t | _t | _t | _t | _|| _	g | _
d | _d | _d | _d | _d | _d | _d | _d| _t | _d| _t | _g | _d S NF)r   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_callrG  hop_dispatch_set_cachetraced_code)r   rh  r    r    r!   r   :  s&   

zTracingContext.__init__c                 C  s    i | j _| j  | j  d S rT   )re  r   rf  clearrg  r?   r    r    r!   rv  k  s   
zTracingContext.clearc                  k  s    i }t  }|  D ]	}t||||< q|  D ]
\}}t||| qzd V  W | D ]
\}}t||| q-d S | D ]
\}}t||| q>w rT   )r  rC  r   rV  r*   setattr)kwargspriorra  r*  valr    r    r!   patchr  s   zTracingContext.patchc                  C  sB   t  } | d u rt S | j}| jd ur||  g }tj|S rT   )r  rW  	tracebackStackSummaryri  rj  _populate_loc_in_frame_summary	from_list)r   r   r    r    r!   r    s   
zTracingContext.extract_stackc                 C  s,   | j d usJ | j \}}}tj|||ddS )NF)lookup_line)rj  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 )Nri  rj  
real_stack)	r  rC  unittestmockr{  objectr-   hasattrr  )tcr3   r    r    r!   clear_frame  s    

PzTracingContext.clear_framec              
   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  rC  ri  r   rj  r-   r  r  r  pop)frame_summaryr  oldr3   r    r    r!   current_frame  s*   





zTracingContext.current_framec                  c  sF    t  } | d u rd V  d S | j}g | _z
| jV  W || _d S || _w rT   )r  rW  rp  )r  old_output_stridesr    r    r!   report_output_strides  s   
z$TracingContext.report_output_stridesc                 C  s   | ||ft  _d S rT   )r  rC  rj  )r  r  r  r    r    r!   set_current_loc  s   zTracingContext.set_current_locc                  C  rZ  rT   )r  rW  ru  )r  r    r    r!   get_traced_code  r\  zTracingContext.get_traced_codeN)rQ   r^  )rQ   r  )r5   r6   r7   r   r   rW  rC  r   rv  r   r{  r  r~  
contextlibr  r  r  r  r  r    r    r    r!   r  &  s6    1
	
r  contextrR  c                 c  s0    t tdd }| t_z	| V  W |t_d S |t_w rT  )rV  rP  rQ  )r  old_contextr    r    r!   rQ    s   rQ  r^  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  )
rV  rP  r_  r-   r  r  r  rh  	shape_envcleanup)r  r  r3   r    r    r!   tracing  s,   	




r  c                   @  sT   e Zd Zdd Zdd Zdd Zdd	d
ZdddZdddZdddZ	dd Z
dS )r   c                 C     dS rb  r    r?   r    r    r!   is_dict_key  r   zSource.is_dict_keyc                 C  r  rb  r    r?   r    r    r!   is_ephemeral  r   zSource.is_ephemeralc                 C     t rT   NotImplementedError)r   codegenr    r    r!   reconstruct  r   zSource.reconstructrQ   rA   c                 C  r  rT   r  r?   r    r    r!   r     r   zSource.guard_sourcer>   c                 C  r  rT   r  r?   r    r    r!   r     r   zSource.namer   c                 C  s   |   tju r	tt| |S rT   )r   rA   rk   r  r   )r   fnr    r    r!   
make_guard  s   
zSource.make_guardrR   c                 C  s   |    S rT   )r   r`   r?   r    r    r!   r`   "  s   zSource.is_specialized_nn_modulec                 C  s   |   tjkS )z+True if you can guard on attributes of this)r   rA   rp   r?   r    r    r!   subguards_allowed%  s   zSource.subguards_allowedNr   r   )rQ   r   ri   )r5   r6   r7   r  r  r  r   r   r  r`   r  r    r    r    r!   r     s    



r   c                   @  s0   e Zd ZU ded< dd Zdd Zddd	Zd
S )ChainedSourcer   basec                 C  r   rT   )r  r  r?   r    r    r!   r  /  r   zChainedSource.is_dict_keyc                 C  r   rT   )r  r  r?   r    r    r!   r  3  r   zChainedSource.is_ephemeralrQ   c                 C  s"   | }t |tr|j}t |ts|S rT   )ry   r  r  )r   currentr    r    r!   get_base6  s
   

zChainedSource.get_baseN)rQ   r   )r5   r6   r7   r8   r  r  r  r    r    r    r!   r  +  s
   
 r  inputsr   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   )
FakeTensorFakeTensorModeNztracing context _get_current_dispatch_mode_stackzactive fake modezfake tensor inputrB   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  rW  rh  r   torch.utils._python_dispatchr  	enumeratereversedry   pytreetree_leavesr   )r  r  r  
fake_modesr  rh  r  imflat_inputs
flat_inputdesc1i1desc2i2r    r    r!   detect_fake_mode=  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  ry   )r  r  r=   r  r    r    r!   active_fake_modei  s   
r  )r  rR  )r  r^  rT   )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   	getLoggerr5   r   typesr   sympycompiler&   r'   r   r;   EnumrA   rr   rt   r|   r   r   r   r   r   r   r   r   r   r   r   r  r   r"  r=  rG  localrP  rO  r  rQ  r  r   r  r  r  r    r    r    r!   <module>   s   (

2>
 %


",!-" @	

,