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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mZmZmZmZ d dlmZ d dlm Z m!Z! d dl"m#Z# e$e%Z&erud dl'Z'd dl(Z(	 G d	d
 d
e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Z0ej.G dd dZ1edZ2	 ej.G dd dZ3	 ej.G dd de3Z4	 ej.G dd de3Z5	 G d d! d!ee2 Z6G d"d# d#Z7G d$d% d%Z8G d&d' d'e6e8 Z9G d(d) d)Z:G d*d+ d+e6e: Z;	 G d,d- d-Z<G d.d/ d/e6e7 Z=G d0d1 d1Z>G d2d3 d3e>Z?G d4d5 d5Z@eA ZB	 G d6d7 d7ZCG d8d9 d9ZDedKd<d=ZEedLd?d@ZFej.ddG dAdB dBZGej.ddG dCdD dDeGZHdMdNdGdHZIdIdJ ZJdS )O    )annotationsN)abstractmethod)contextmanager)AnyCallableDictGenericList
NamedTupleOptionalSetTupleTYPE_CHECKINGTypeVarUnion)_pytree)CapturedTracebackformat_frame)WeakTensorKeyDictionaryc                   @  &   e Zd ZU ded< ded< dd ZdS )	CompileIdintframe_idframe_compile_idc                 C  s   | j  d| j S )N/)r   r   self r   K/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/_guards.py__str__@   s   zCompileId.__str__N__name__
__module____qualname____annotations__r   r   r   r   r   r   7   s   
 r   c                   @  &   e Zd ZU ded< ded< dd ZdS )TraceIdr   
compile_idr   attemptc                 C  s&   | j dkr
t| jS | j d| j  S )Nr   _)r(   strr'   r   r   r   r   r   J   s   

zTraceId.__str__Nr    r   r   r   r   r&   D   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)r+   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULEr   r   r   r   is_fsdp_modulec      zGuardSource.is_fsdp_modulec                 C  r=   r>   )r+   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULEr   r   r   r   is_specialized_nn_modulef      z$GuardSource.is_specialized_nn_modulec                 C  s   | t jt jt jt jfv S r>   )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_modulel   s   z&GuardSource.is_unspecialized_nn_modulec                 C  r=   r>   )r+   rI   rJ   r   r   r   r   "is_unspecialized_builtin_nn_modulet   rF   z.GuardSource.is_unspecialized_builtin_nn_modulec                 C  s   | t jt jt jt jt jfv S r>   )r+   LOCALrD   r@   rH   rJ   r   r   r   r   is_localz   s   zGuardSource.is_localNr;   r<   )r!   r"   r#   rM   GLOBALrD   rC   CONSTANTRANDOM_VALUE	SHAPE_ENVr@   r?   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALrH   rG   rJ   rI   rA   rE   rK   rL   rN   r   r   r   r   r+   Q   s,    



r+   c                   @     e Zd ZdS )GuardBuilderBaseNr!   r"   r#   r   r   r   r   rX      s    rX   T)frozenc                   @  r%   )SLocz,Optional[Union[traceback.FrameSummary, str]]framework_locOptional[str]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 ()()
isinstancer\   r*   r   r^   )r   flocr   r   r   r      s   

zSLoc.__str__Nr    r   r   r   r   r[         
 r[   c                   @  s   e Zd ZU ded< ded< dS )
ShapeGuardzsympy.logic.boolalg.Booleanexprr[   slocNr!   r"   r#   r$   r   r   r   r   rd      s   
 rd   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_stackzOptional[int]_hashc                 C  s*   | j d u rt| j| jt| jf| _ | j S r>   )rr   hashnamesourceidrk   r   r   r   r   __hash__   s   
zGuard.__hash__c                 C  sT   ddl m} t| jtjo| jj|ju }|| jr| jj	ndt
| j| j|  jjfS )Nr   )GuardBuilder)torch._dynamo.guardsrx   ra   rk   	functoolspartialfuncDUPLICATE_INPUTru   valuelenrt   inner_create_fn__code__co_firstlineno)r   rx   is_duplicate_inputr   r   r   sort_key   s   
zGuard.sort_keyc                 C  s   |   |  k S r>   )r   r   otherr   r   r   __lt__   rB   zGuard.__lt__c                 C  s   t | jtjr| jjS | jS r>   )ra   rk   r{   r|   r}   r   r   r   r   r      s   zGuard.inner_create_fnr;   r*   c                 C  
   | j  S r>   )rj   rt   r   r   r   r   rt         
z
Guard.namer+   c                 C  r   r>   )rj   guard_sourcer   r   r   r   ru      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>)ra   weakrefReferenceTypehexrv   	__class__r!   r*   )rn   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	
          z&
        {
            'guard_types': z,
            'code': z,
            'obj_weakref': z
            'guarded_class': z
        }
        )ru   rt   lowerreprr   r!   rl   rm   r   rn   ro   )r   sr   r   r   __repr__  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   rt   ru   r   r   r!   rl   rm   r   rn   ro   )r   outputru   r   r   r   r     s   zGuard.__str__builderrX   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   )
rk   	Exceptionlog	exceptionr*   rstriprp   errorjoinformat)r   r   r   r   r   create#  s   $zGuard.createc                 C  r   r>   )ru   rE   r   r   r   r   rE   ,     
zGuard.is_specialized_nn_modulec                 C  r   r>   )ru   rA   r   r   r   r   rA   /  r   zGuard.is_fsdp_modulec                 C  r   r>   )ru   rN   r   r   r   r   rN   2  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))rl   appendro   rm   extendrn   callable)r   
guard_typeguarded_classrm   rn   r   r   r   set_export_info5  s(   
zGuard.set_export_infor;   r*   r;   r+   )r   rX   )r!   r"   r#   r$   rl   rm   rn   ro   rp   rq   rr   rw   r   r   r   propertyrt   ru   staticmethodr   r   r   r   rE   rA   rN   r   r   r   r   r   rh      s6   
 

	rh   Tc                   @  rW   )GuardEnvExprNrY   r   r   r   r   r   \  s    r   c                   @  r   )DuplicateInputsri   input_source_ainput_source_bc                 C  s   | j | jksJ d S r>   )r   r   r   r   r   r   __post_init__l  s   zDuplicateInputs.__post_init__N)r!   r"   r#   r$   r   r   r   r   r   r   g  rc   r   c                   @  s   e Zd ZU ded< ded< dS )StorageOverlapzList[Source]overlapping_sourcesnon_overlapping_sourcesNrg   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   r   r   r   r   copy_graphstate     zCheckpointable.copy_graphstatestatec                 C  r   r>   r   r   r   r   r   r   restore_graphstate  r   z!Checkpointable.restore_graphstateN)r;   r   )r   r   )r!   r"   r#   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 r>   )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 r>   r   r   r   r   r   __eq__     zGuardsCheckpointState.__eq__N)
r!   r"   r#   __doc__setr   r$   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   r>   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   r>   r   r   r   r   r   r     r   z#ModuleContextCheckpointState.__eq__N)r!   r"   r#   r   r$   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 )ModuleContextr;   Nonec                 C  
   i | _ d S r>   r   r   r   r   r   r     r   zModuleContext.__init__c                 C     t t| jS r>   )r   dictr   r   r   r   r   r     r   zModuleContext.copy_graphstatec                 C  s   t |tsJ |j| _d S r>   )ra   r   r   r   r   r   r   r     s   z ModuleContext.restore_graphstateNr;   r   r!   r"   r#   r   r   r   r   r   r   r   r     s    
r   c                   @  r   )
GlobalContextCheckpointStatezDict[str, Tuple[Callable, ...]]global_statec                 C  r   r>   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   r>   r   r   r   r   r   r     r   z#GlobalContextCheckpointState.__eq__N)r!   r"   r#   r   r$   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_enabledr;   r   c                 C  r   r>   r   r   r   r   r   r     r   zGlobalContext.__init__c                 C  r   r>   )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)ra   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   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 r>   )r   inner)r   r   r   r   r   r     s   
zGuardsSet.__init__c                 C  
   t | jS r>   )iterr   r   r   r   r   __iter__  r   zGuardsSet.__iter__c                 C  r   r>   )r   r   r   r   r   r   __len__  r   zGuardsSet.__len__c                 C  s   t | j|j S r>   )r   r   r   r   r   r   __sub__$  rB   zGuardsSet.__sub__c                 C  r   r>   )r<   r   r   r   r   r   __bool__'  r   zGuardsSet.__bool__Tr   )collect_debug_stackskipguardrh   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   rp   r   extractrq   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   update4  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   )rj   ).0r   ru   r   r   	<setcomp>;  s    z6GuardsSet.remove_guards_with_source.<locals>.<setcomp>N)r   )r   ru   r   r  r   remove_guards_with_source9  s   z#GuardsSet.remove_guards_with_sourcer>   )r   rh   )r   r   )r!   r"   r#   r   r   r   r   r   r   r  r  r   r   r   r   r     s    


r   c                   @  r   )GuardsContextr;   r   c                 C  s   t  | _g | _d S r>   )r   r   aotautograd_guardsr   r   r   r   r   ?  s   
zGuardsContext.__init__c                 C  s   t t| jjS r>   )r   r   r   r   r   r   r   r   r   C  rB   zGuardsContext.copy_graphstatec                 C  s   t |tsJ t|j| _d S r>   )ra   r   r   r   r   r   r   r   r   F  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   r>   r   r   r	  r
  r   r   r   add_dynamo_identifierM  r   z&HopSubgraphCache.add_dynamo_identifierr;   r]   c                 C  r   r>   r   r   r	  r   r   r   get_dynamo_identifierP  r   z&HopSubgraphCache.get_dynamo_identifierkeyr   c                 C  r   r>   r   r   r
  r  r   r   r   add_autograd_key_entryS  r   z'HopSubgraphCache.add_autograd_key_entryc                 C  r   r>   r   r   r
  r   r   r   get_autograd_key_entryV  r   z'HopSubgraphCache.get_autograd_key_entryc                 C  r   r>   r   r  r   r   r   add_proxy_dispatch_entryY  r   z)HopSubgraphCache.add_proxy_dispatch_entryc                 C  r   r>   r   r  r   r   r   get_proxy_dispatch_entry\  r   z)HopSubgraphCache.get_proxy_dispatch_entryNr	  r*   r
  r*   r	  r*   r;   r]   r
  r*   r  r   r
  r*   )
r!   r"   r#   r   r  r  r  r  r  r  r   r   r   r   r  L  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 )InvokeSubgraphCacher;   r   c                 C  s   i | _ i | _i | _d S r>   )autograd_cacheproxy_dispatch_cachedynamo_identifiersr   r   r   r   r   a  s   
zInvokeSubgraphCache.__init__r	  r*   r
  c                 C     || j |< d S r>   )r  r  r   r   r   r  f  r   z)InvokeSubgraphCache.add_dynamo_identifierr]   c                 C     | j |d S r>   )r  getr  r   r   r   r  i  r   z)InvokeSubgraphCache.get_dynamo_identifierr  r   c                 C  r  r>   )r  r  r   r   r   r  l  r   z*InvokeSubgraphCache.add_autograd_key_entryc                 C  r  r>   )r  r   r  r   r   r   r  o  r   z*InvokeSubgraphCache.get_autograd_key_entryc                 C  r  r>   )r  r  r   r   r   r  r  r   z,InvokeSubgraphCache.add_proxy_dispatch_entryc                 C  r  r>   )r  r   r  r   r   r   r  u  r   z,InvokeSubgraphCache.get_proxy_dispatch_entryNr   r  r  r  r  )
r!   r"   r#   r   r  r  r  r  r  r  r   r   r   r   r  `  s    





r  c                   @  s    e Zd ZdddZddd	Zd
S )HopDispatchSetCacher;   r   c                 C  s   ddl m} |t i| _d S )Nr   )invoke_subgraph)'torch._higher_order_ops.invoke_subgraphr"  r  hop_cache_map)r   r"  r   r   r   r   z  s   zHopDispatchSetCache.__init__optorch._ops.HigherOrderOperatorOptional[HopSubgraphCache]c                 C  s   || j vrd S | j | S r>   )r$  )r   r%  r   r   r   	get_cache  s   

zHopDispatchSetCache.get_cacheNr   )r%  r&  r;   r'  )r!   r"   r#   r   r(  r   r   r   r   r!  y  s    
r!  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 )CompileContextr;   c                   C  s   t jd usJ t jS r>   )_TLScompile_contextr   r   r   r   r     s   zCompileContext.getOptional[CompileContext]c                   C     t tdd S Nr+  getattrr*  r   r   r   r   try_get     zCompileContext.try_getc                 C  s,   |d u st |tsJ || _d| _g | _d S )Nr   )ra   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 r>   )r)  r1  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 r>   )r)  r1  r'   r&   r(   r   r   r   r   current_trace_id  s   
zCompileContext.current_trace_idN)r;   r)  )r;   r,  )	r!   r"   r#   r   r   r1  r   r4  r5  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.
    r;   Optional[TracingContext]c                   C  r-  )Ntracing_contextr/  r   r   r   r   r1    r2  zTracingContext.try_getc                  C  s   t   } r| S td)Nz<TracingContext.get() must be called within an ongoing trace.)r   r1  RuntimeError)ctxr   r   r   r     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_callr!  hop_dispatch_set_cache)r   r>  r   r   r   r     s    
zTracingContext.__init__c                 C  s   i | j _d S r>   )r=  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 r>   )r   r   r   r0  itemssetattr)kwargspriorr9  r  valr   r   r   patch  s   zTracingContext.patchc                  C  s@   t  } | d u rt S | j}| jd ur|| jg }tj|S r>   )r   r1  	tracebackStackSummaryr?  r@  	from_list)r   rp   r   r   r   r     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 )Nr?  r@  
real_stack)	r   r   unittestmockrQ  objectr   hasattrrU  )tcer   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 )NrU  )
r   r   r?  r   r@  r   rY  r   rU  pop)frame_summaryrZ  oldr[  r   r   r   current_frameA  s*   





zTracingContext.current_framec                  c  sF    t  } | d u rd V  d S | j}g | _z
| jV  W || _d S || _w r>   )r   r1  rF  )rZ  old_output_stridesr   r   r   report_output_stridesV  s   
z$TracingContext.report_output_stridesc                 C  s   t j| ||ddt _d S )NF)lookup_line)rR  FrameSummaryr   r   r@  )filenamelineno
frame_namer   r   r   set_current_locd  s   zTracingContext.set_current_locN)r;   r6  )r;   r   )r!   r"   r#   r   r   r1  r   r   rK  r   rQ  r   
contextlibr\  r`  rb  rh  r   r   r   r   r     s0    -
r   contextr,  c                 c  s0    t tdd }| t_z	| V  W |t_d S |t_w r.  )r0  r*  r+  )rj  old_contextr   r   r   r+  k  s   r+  r6  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.
    r7  NrU  )
r0  r*  r7  r   rY  r   rU  r>  	shape_envcleanup)rj  rk  r[  r   r   r   tracingu  s,   	




rn  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 )ri   c                 C     dS r:  r   r   r   r   r   is_dict_key  r   zSource.is_dict_keyc                 C  ro  r:  r   r   r   r   r   is_ephemeral  r   zSource.is_ephemeralc                 C     t r>   NotImplementedError)r   codegenr   r   r   reconstruct  r   zSource.reconstructr;   r+   c                 C  rr  r>   rs  r   r   r   r   r     r   zSource.guard_sourcer*   c                 C  rr  r>   rs  r   r   r   r   rt     r   zSource.namerh   c                 C  s   |   tju r	tt| |S r>   )r   r+   rQ   rt  rh   )r   fnr   r   r   
make_guard  s   
zSource.make_guardr<   c                 C  s   |    S r>   )r   rE   r   r   r   r   rE     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+   rV   r   r   r   r   subguards_allowed  s   zSource.subguards_allowedNr   r   )r;   rh   rO   )r!   r"   r#   rp  rq  rv  r   rt   rx  rE   ry  r   r   r   r   ri     s    



ri   c                   @  s&   e Zd ZU ded< dd Zdd ZdS )ChainedSourceri   basec                 C  r   r>   )r{  rp  r   r   r   r   rp    r   zChainedSource.is_dict_keyc                 C  r   r>   )r{  rq  r   r   r   r   rq    r   zChainedSource.is_ephemeralN)r!   r"   r#   r$   rp  rq  r   r   r   r   rz    s   
 rz  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   r1  r>  r   torch.utils._python_dispatchr  	enumeratereversedra   pytreetree_leavesrp   )r|  r}  r~  
fake_modesrj  r>  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  ra   )r~  r  r)   r  r   r   r   active_fake_mode  s   
r  )rj  r,  )rj  r6  r>   )r|  r   )K
__future__r   ri  dataclassesenumr{   logging	threadingrR  unittest.mockrV  r   abcr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   r   torch.utilsr   r  torch.utils._tracebackr   r   torch.utils.weakr   	getLoggerr!   r   sympytorchr   r&   Enumr+   rX   	dataclassr[   rd   rh   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r!  localr*  r)  r   r+  rn  ri   rz  r  r  r   r   r   r   <module>   s   8
3
 &!
(" -	

,