o
    h=                     @  s2  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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r}d dl(Z(	 e)dZ*e)d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Z0ejddG dd dZ1G dd deZ2eejG dd dZ3edZ4	 ejG dd dZ5	 ejG d d! d!e5Z6	 ejG d"d# d#e5Z7	 G d$d% d%ee4 Z8G d&d' d'Z9G d(d) d)Z:G d*d+ d+e8e: Z;G d,d- d-Z<G d.d/ d/e8e< Z=	 G d0d1 d1Z>G d2d3 d3e8e9 Z?G d4d5 d5Z@G d6d7 d7e@ZAG d8d9 d9ZBeC ZD	 G d:d; d;ZEG d<d= d=ZFedOd@dAZGedPdCdDZHejddG dEdF dFZIejddG dGdH dHeIZJdQdRdKdLZKdMdN ZLdS )S    )annotationsN)abstractmethod)contextmanager)	dataclass)AnyCallableGeneric
NamedTupleOptionalTYPE_CHECKINGTypeVarUnion)_pytree)dataclass_slots)CapturedTracebackformat_frame)WeakTensorKeyDictionaryz-^(?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   a/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/_guards.py__str__S   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_string`   s"   zCompileId.from_string)r!   r"   )__name__
__module____qualname____annotations__r   r    classmethodr2   r   r   r   r   r   D   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r3   r4   r5   r6   r    r   r   r   r   r9   w   s   
 r9   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)r?   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_guardsr?   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULErU   )r   rZ   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 rR   )r?   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  rQ   rR   )r?   ra   rb   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 rR   )r?   LOCALr]   rT   r`   rb   r=   r   r   r   is_local   s   zGuardSource.is_localNrO   rP   )r3   r4   r5   re   GLOBALr]   r\   CONSTANTRANDOM_VALUE	SHAPE_ENVrT   rS   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALr`   r_   rb   ra   rU   r^   rc   rd   rf   r   r   r   r   r?      s,    



r?   c                   @     e Zd ZdS )GuardBuilderBaseNr3   r4   r5   r   r   r   r   rp      s    rp   c                   @  r8   )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 ()()
isinstancers   r<   r   rt   )r   flocr   r   r   r       s   

zSLoc.__str__Nr>   r   r   r   r   rr         
 rr   c                   @  s&   e Zd ZU ded< ded< ded< dS )
ShapeGuardzsympy.logic.boolalg.Booleanexprrr   slocrP   size_obliviousNr3   r4   r5   r6   r   r   r   r   rz      s   
 rz   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 rR   )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)rw   r   	functoolspartialfunctorchrY   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 rR   )r   r   otherr   r   r   __lt__"  rV   zGuard.__lt__c                 C  s   t | jtjr| jjS | jS rR   )rw   r   r   r   r   r=   r   r   r   r   %  s   zGuard.inner_create_fnrO   r<   c                 C  
   | j  S rR   )r   r   r=   r   r   r   r   +     
z
Guard.namer?   c                 C  r   rR   )r   guard_sourcer=   r   r   r   r   /  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>)rw   weakrefReferenceTypehexr   	__class__r3   r<   )r   objr   r   r   weakref_to_str3  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   r3   r   r   r   r   r   )r   sr   r   r   __repr__L  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   r3   r   r   r   r   r   )r   outputr   r   r   r   r    X  s   zGuard.__str__builderrp   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   createc  s   $zGuard.createc                 C  r   rR   )r   r^   r=   r   r   r   r^   l     
zGuard.is_specialized_nn_modulec                 C  r   rR   )r   rU   r=   r   r   r   rU   o  r   zGuard.is_fsdp_modulec                 C  r   rR   )r   rf   r=   r   r   r   rf   r  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_infou  s(   
zGuard.set_export_inforO   r<   rO   r?   )r   rp   )r3   r4   r5   r6   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r    r   r^   rU   rf   r   r   r   r   r   r      s6   
 

	r   Tc                   @  ro   )GuardEnvExprNrq   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 rR   )r   r   r=   r   r   r   __post_init__  s   zDuplicateInputs.__post_init__N)r3   r4   r5   r6   r   r   r   r   r   r     ry   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 )CheckpointablerO   r   c                 C     d S rR   r   r=   r   r   r   copy_graphstate     zCheckpointable.copy_graphstatestatec                 C  r   rR   r   r   r   r   r   r   restore_graphstate  r   z!Checkpointable.restore_graphstateN)rO   r   )r   r   )r3   r4   r5   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 rR   )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 rR   r   r   r   r   r   __eq__     zGuardsCheckpointState.__eq__N)
r3   r4   r5   __doc__setr   r6   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   rR   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   rR   r   r   r   r   r   r     r   z#ModuleContextCheckpointState.__eq__N)r3   r4   r5   r   r6   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 )ModuleContextrO   Nonec                 C  
   i | _ d S rR   r   r=   r   r   r   r   
  r   zModuleContext.__init__c                 C     t t| jS rR   )r   dictr   r=   r   r   r   r     r   zModuleContext.copy_graphstatec                 C  s   t |tsJ |j| _d S rR   )rw   r   r   r   r   r   r   r     s   z ModuleContext.restore_graphstateNrO   r   r3   r4   r5   r   r   r   r   r   r   r   r   	  s    
r   c                   @  r   )
GlobalContextCheckpointStatezdict[str, tuple[Callable, ...]]global_statec                 C  r   rR   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   rR   r   r   r   r   r   r   '  r   z#GlobalContextCheckpointState.__eq__N)r3   r4   r5   r   r6   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_enabledtorch_function_enabledrO   r   c                 C  r   rR   r   r=   r   r   r   r   ;  r   zGlobalContext.__init__c                 C  r   rR   )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)rw   r   r   r   _supported_global_statesr   r   values)r   r   r   argsr   r   r   r   A  s   
z GlobalContext.restore_graphstateNr   )r3   r4   r5   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 rR   )r   inner)r   r  r   r   r   r   W  s   
zGuardsSet.__init__c                 C  
   t | jS rR   )iterr  r=   r   r   r   __iter__\  r   zGuardsSet.__iter__c                 C  r  rR   )r   r  r=   r   r   r   __len___  r   zGuardsSet.__len__c                 C  s   t | j|j S rR   )r  r  r   r   r   r   __sub__d  rV   zGuardsSet.__sub__c                 C  r  rR   )rP   r  r=   r   r   r   __bool__g  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 Nr@   )r  )r  r   r   extractr   TracingContextextract_stackadd)r   r  r  r  r   r   r   r  j  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   updatet  s
   zGuardsSet.updatec                   s    fdd| j D | _ dS )z%Delete all guards with a given sourcec                   s   h | ]	}|j  kr|qS r   )r   ).0r  r   r   r   	<setcomp>{  s    z6GuardsSet.remove_guards_with_source.<locals>.<setcomp>N)r  )r   r   r   r  r   remove_guards_with_sourcey  s   z#GuardsSet.remove_guards_with_sourcerR   )r  r   )r  r   )r3   r4   r5   r   r  r	  r
  r  r  r  r  r   r   r   r   r  V  s    


r  c                   @  r   )GuardsContextrO   r   c                 C  s   t  | _g | _d S rR   )r  r   aotautograd_guardsr=   r   r   r   r     s   
zGuardsContext.__init__c                 C  s   t t| jjS rR   )r   r   r   r  r=   r   r   r   r     rV   zGuardsContext.copy_graphstatec                 C  s   t |tsJ t|j| _d S rR   )rw   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	dS )HopSubgraphCache	cache_keyr<   
identifierc                 C  r   rR   r   r   r  r   r   r   r   add_dynamo_identifier  r   z&HopSubgraphCache.add_dynamo_identifierrO   r"   c                 C  r   rR   r   r   r  r   r   r   get_dynamo_identifier  r   z&HopSubgraphCache.get_dynamo_identifierkeyr   c                 C  r   rR   r   r   r   r%  r   r   r   add_autograd_key_entry  r   z'HopSubgraphCache.add_autograd_key_entryc                 C  r   rR   r   r   r   r   r   r   get_autograd_key_entry  r   z'HopSubgraphCache.get_autograd_key_entryc                 C  r   rR   r   r&  r   r   r   add_proxy_dispatch_entry  r   z)HopSubgraphCache.add_proxy_dispatch_entryc                 C  r   rR   r   r(  r   r   r   get_proxy_dispatch_entry  r   z)HopSubgraphCache.get_proxy_dispatch_entryNr  r<   r   r<   r  r<   rO   r"   r   r<   r%  r   r   r<   )
r3   r4   r5   r   r"  r$  r'  r)  r*  r+  r   r   r   r   r    s    r  c                   @  sR   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S )InvokeSubgraphCacherO   r   c                 C  s   i | _ i | _i | _d S rR   )autograd_cacheproxy_dispatch_cachedynamo_identifiersr=   r   r   r   r     s   
zInvokeSubgraphCache.__init__r  r<   r   c                 C     || j |< d S rR   )r3  r!  r   r   r   r"    r   z)InvokeSubgraphCache.add_dynamo_identifierr"   c                 C     | j |d S rR   )r3  getr#  r   r   r   r$    r   z)InvokeSubgraphCache.get_dynamo_identifierr%  r   c                 C  r4  rR   )r1  r&  r   r   r   r'    r   z*InvokeSubgraphCache.add_autograd_key_entryc                 C  r5  rR   )r1  r6  r(  r   r   r   r)    r   z*InvokeSubgraphCache.get_autograd_key_entryc                 C  r4  rR   )r2  r&  r   r   r   r*    r   z,InvokeSubgraphCache.add_proxy_dispatch_entryc                 C  r5  rR   )r2  r6  r(  r   r   r   r+    r   z,InvokeSubgraphCache.get_proxy_dispatch_entryNr   r,  r-  r.  r/  )
r3   r4   r5   r   r"  r$  r'  r)  r*  r+  r   r   r   r   r0    s    





r0  c                   @  s    e Zd ZdddZddd	Zd
S )HopDispatchSetCacherO   r   c                 C  s   ddl m} |t i| _d S )Nr   )invoke_subgraph)'torch._higher_order_ops.invoke_subgraphr8  r0  hop_cache_map)r   r8  r   r   r   r     s   zHopDispatchSetCache.__init__optorch._ops.HigherOrderOperatorOptional[HopSubgraphCache]c                 C  s   || j vrd S | j | S rR   )r:  )r   r;  r   r   r   	get_cache  s   

zHopDispatchSetCache.get_cacheNr   )r;  r<  rO   r=  )r3   r4   r5   r   r>  r   r   r   r   r7    s    
r7  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 )CompileContextrO   c                   C  s   t jd usJ t jS rR   )_TLScompile_contextr   r   r   r   r6    s   zCompileContext.getOptional[CompileContext]c                   C     t tdd S NrA  getattrr@  r   r   r   r   try_get     zCompileContext.try_getc                 C  s,   |d u st |tsJ || _d| _g | _d S rW   )rw   r   r!   r:   shape_env_guards)r   r!   r   r   r   r     s   
zCompileContext.__init__c                  C  s   t  } | d u r
d S | jS rR   )r?  rG  r!   r=   r   r   r   current_compile_id  s   z!CompileContext.current_compile_idc                  C  s0   t  } | d u r
d S | jd u rd S t| j| jS rR   )r?  rG  r!   r9   r:   r=   r   r   r   current_trace_id  s   
zCompileContext.current_trace_idN)rO   r?  )rO   rB  )	r3   r4   r5   r   r6  rG  r   rJ  rK  r   r   r   r   r?    s    
r?  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eej	dd Zeej	dd Zeej	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.
    rO   Optional[TracingContext]c                   C  rC  )Ntracing_contextrE  r   r   r   r   rG    rH  zTracingContext.try_getc                  C  s   t   } r| S td)Nz<TracingContext.get() must be called within an ongoing trace.)r  rG  RuntimeError)ctxr   r   r   r6    s
   zTracingContext.getc                 C  sn   t  | _t | _t | _|| _g | _d | _d | _	d | _
d | _d | _d | _d | _d| _t | _d| _t | _d S NF)r  guards_contextr   module_contextr   global_context	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_callr7  hop_dispatch_set_cache)r   rT  r   r   r   r     s    
zTracingContext.__init__c                 C  s   i | j _d S rR   )rS  r   r=   r   r   r   clear@  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 rR   )r  r6  r   rF  r(   setattr)kwargspriorrO  r%  valr   r   r   patchE  s   zTracingContext.patchc                  C  s@   t  } | d u rt S | j}| jd ur|| jg }tj|S rR   )r  rG  	tracebackStackSummaryrU  rV  	from_list)r   r   r   r   r   r  V  s   
zTracingContext.extract_stackc                  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 )NrU  rV  
real_stack)	r  r6  unittestmockrf  objectr+   hasattrrj  )tcr1   r   r   r   clear_frameb  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 )Nrj  )
r  r6  rU  r   rV  r+   rn  r  rj  pop)frame_summaryro  oldr1   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 rR   )r  rG  r\  )ro  old_output_stridesr   r   r   report_output_strides  s   
z$TracingContext.report_output_stridesc                 C  s   t j| ||ddt _d S )NF)lookup_line)rg  FrameSummaryr  r6  rV  )filenamelineno
frame_namer   r   r   set_current_loc  s   zTracingContext.set_current_locN)rO   rL  )rO   r  )r3   r4   r5   r   r   rG  r6  r   ra  r   rf  r  
contextlibrp  rt  rv  r|  r   r   r   r   r    s0    -
r  contextrB  c                 c  s0    t tdd }| t_z	| V  W |t_d S |t_w rD  )rF  r@  rA  )r~  old_contextr   r   r   rA    s   rA  rL  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.
    rM  Nrj  )
rF  r@  rM  r+   rn  r  rj  rT  	shape_envcleanup)r~  r  r1   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 rP  r   r=   r   r   r   is_dict_key  r   zSource.is_dict_keyc                 C  r  rP  r   r=   r   r   r   is_ephemeral  r   zSource.is_ephemeralc                 C     t rR   NotImplementedError)r   codegenr   r   r   reconstruct  r   zSource.reconstructrO   r?   c                 C  r  rR   r  r=   r   r   r   r     r   zSource.guard_sourcer<   c                 C  r  rR   r  r=   r   r   r   r     r   zSource.namer   c                 C  s   |   tju r	tt| |S rR   )r   r?   ri   r  r   )r   fnr   r   r   
make_guard  s   
zSource.make_guardrP   c                 C  s   |    S rR   )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   r?   rn   r=   r   r   r   subguards_allowed  s   zSource.subguards_allowedNr   r   )rO   r   rg   )r3   r4   r5   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   rR   )r  r  r=   r   r   r   r    r   zChainedSource.is_dict_keyc                 C  r   rR   )r  r  r=   r   r   r   r    r   zChainedSource.is_ephemeralrO   c                 C  s"   | }t |tr|j}t |ts|S rR   )rw   r  r  )r   currentr   r   r   get_base  s
   

zChainedSource.get_baseN)rO   r   )r3   r4   r5   r6   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 inputr@   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  rG  rT  r   torch.utils._python_dispatchr  	enumeratereversedrw   pytreetree_leavesr   )r  r  r  
fake_modesr~  rT  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  rw   )r  r  r;   r  r   r   r   active_fake_mode0  s   
r  )r~  rB  )r~  rL  rR   )r  r   )M
__future__r   r}  dataclassesenumr   loggingre	threadingrg  unittest.mockrk  r   abcr   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   	getLoggerr3   r   sympycompiler$   r%   r   r9   Enumr?   rp   rr   rz   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r0  r7  localr@  r?  r  rA  r  r   r  r  r  r   r   r   r   <module>   s   (

2>
 %!
(" .	

,