o
    I&il                     @  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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 d dlZd dlmZ d dl m!Z! d dl"m#Z# e$e%Z&ersd 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,G dd deZ-ej.G dd dZ/edZ0	 ej.G dd dZ1	 ej.G dd de1Z2	 G dd deee0 Z3	 G dd dZ4G dd dZ5G d d! d!e3e5 Z6G d"d# d#Z7G d$d% d%e3e7 Z8	 G d&d' d'Z9G d(d) d)e3e4 Z:e; Z<	 G d*d+ d+Z=G d,d- d-Z>ed>d/d0Z?ed?d2d3Z@ej.d4d5G d6d7 d7ZAej.d4d5G d8d9 d9eAZBd@dAd<d=ZCdS )B    )annotationsN)ABCabstractmethod)contextmanager)AnyCallableDictGenericList
NamedTupleOptionalSetTupleTYPE_CHECKINGTypeVar)_pytree)CapturedTraceback)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   8C:\wamp64\www\opt\env\Lib\site-packages\torch/_guards.py__str__>   s   zCompileId.__str__N__name__
__module____qualname____annotations__r   r   r   r   r   r   5   s   
 r   c                   @  s&   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   H   s   

zTraceId.__str__Nr   r   r   r   r   r$   B   s   
 r$   c                   @  sL   e 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 ZdS )GuardSourcer                           returnboolc                 C  s   | t jt jfv S N)r)   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULEr   r   r   r   is_fsdp_moduleZ      zGuardSource.is_fsdp_modulec                 C  s   | t jt jfv p|  S r4   )r)   GLOBAL_NN_MODULELOCAL_NN_MODULEr7   r   r   r   r   is_nn_module]   s   zGuardSource.is_nn_modulec                 C  s   | t jt jt jfv S r4   )r)   LOCALr:   r6   r   r   r   r   is_localg   s
   zGuardSource.is_localNr2   r3   )r    r!   r"   r<   GLOBALr:   r9   CONSTANTZRANDOM_VALUEZ	SHAPE_ENVr6   r5   r7   r;   r=   r   r   r   r   r)   O   s    


r)   c                   @     e Zd ZdS )GuardBuilderBaseNr    r!   r"   r   r   r   r   rB   }   s    rB   c                   @  s   e Zd ZU ded< ded< dS )
ShapeGuardz
sympy.Exprexprr   stackN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Z	dZ
dd Zdd Zdd Zdd Zed.ddZed/ddZedd Zdd Zd d! Zd0d$d%Zd&d' Zd(d) Zd*d+ Zd,d- ZdS )1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_weakrefc                 C  s*   | j d u rt| j| jt| jf| _ | j S r4   )_hashhashnamesourceidrK   r   r   r   r   __hash__   s   
zGuard.__hash__c                 C  s*   | j r| j jndt| j| j|  jjfS )N)rS   valuelenrR   inner_create_fn__code__co_firstlinenor   r   r   r   sort_key   s
   
zGuard.sort_keyc                 C  s   |   |  k S r4   )r\   r   otherr   r   r   __lt__   r8   zGuard.__lt__c                 C  s   t | jtjr| jjS | jS r4   )
isinstancerK   	functoolspartialfuncr   r   r   r   rY      s   zGuard.inner_create_fnr2   r(   c                 C  
   | j  S r4   )rJ   rR   r   r   r   r   rR         
z
Guard.namer)   c                 C  rd   r4   )rJ   guard_sourcer   r   r   r   rS      re   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>)r`   weakrefReferenceTypehexrT   	__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
        }
        )rS   rR   lowerreprrY   r    rL   rM   rm   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: 
rn   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )rq   rR   rS   rp   rY   r    rL   rM   rm   rN   rO   )r   outputrS   r   r   r   r      s   zGuard.__str__builderrB   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:
%srn   )	rK   	Exceptionlogerrorr(   rstriprF   joinformat)r   rv   r   r   r   create   s   $zGuard.createc                 C  rd   r4   )rS   r;   r   r   r   r   r;         
zGuard.is_nn_modulec                 C  rd   r4   )rS   r7   r   r   r   r   r7      r   zGuard.is_fsdp_modulec                 C  rd   r4   )rS   r=   r   r   r   r   r=     r   zGuard.is_localc                 C  sp   | j st | _ | j | | j|d fv sJ d|| _| js"|| _n| j| | j|d fv s3J d|| _d S )Nz+Guarded class id must be identical, or Nonez)Guarded object must be identical, or None)rL   listappendrO   rM   extendrN   )r   Z
guard_typeZguarded_classrM   rN   r   r   r   set_export_info  s(   
zGuard.set_export_infor2   r(   r2   r)   )rv   rB   )r    r!   r"   r#   rL   rM   rN   rO   rF   
user_stackrP   rU   r\   r_   rY   propertyrR   rS   staticmethodrm   rs   r   r~   r;   r7   r=   r   r   r   r   r   rH      s6   
 

	rH   Tc                   @  rA   )GuardEnvExprNrC   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 r4   )r   r   r   r   r   r   __post_init__8  s   zDuplicateInputs.__post_init__N)r    r!   r"   r#   r   r   r   r   r   r   3  s   
 r   c                   @  s(   e Zd Zed	ddZed
ddZdS )Checkpointabler2   r   c                 C     d S r4   r   r   r   r   r   copy_graphstateJ     zCheckpointable.copy_graphstatestatec                 C  r   r4   r   r   r   r   r   r   restore_graphstateN  r   z!Checkpointable.restore_graphstateN)r2   r   )r   r   )r    r!   r"   r   r   r   r   r   r   r   r   I  s
    r   c                   @  s6   e Zd ZU e Zded< dd Z	 dd Zdd Zd	S )
GuardsCheckpointState
Set[Guard]dynamo_guardsc                 C  
   || _ d S r4   )r   )r   r   r   r   r   __init__[  r   zGuardsCheckpointState.__init__c                 C  s"   | j |j }t|dkrd S |S Nr   )r   
differencerX   r   r^   rr   r   r   diffe  s   zGuardsCheckpointState.diffc                 C     |  |d u S r4   r   r]   r   r   r   __eq__k     zGuardsCheckpointState.__eq__N)	r    r!   r"   setr   r#   r   r   r   r   r   r   r   r   X  s   
 r   c                   @  4   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   r4   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 r   )r   r   keysr   rX   r   r   r   r   r   |     z!ModuleContextCheckpointState.diffc                 C  r   r4   r   r]   r   r   r   r     r   z#ModuleContextCheckpointState.__eq__N)r    r!   r"   r   r#   r   r   r   r   r   r   r   r   o     
 r   c                   @  $   e Zd Zdd Zdd Zdd ZdS )ModuleContextc                 C  
   i | _ d S r4   r   r   r   r   r   r     r   zModuleContext.__init__c                 C     t t| jS r4   )r   dictr   r   r   r   r   r     r   zModuleContext.copy_graphstatec                 C  s   t |tsJ |j| _d S r4   )r`   r   r   r   r   r   r   r     s   z ModuleContext.restore_graphstateNr    r!   r"   r   r   r   r   r   r   r   r     s    r   c                   @  r   )
GlobalContextCheckpointStatezDict[str, Tuple[Callable, ...]]global_statec                 C  r   r4   r   )r   Zglobal_statesr   r   r   r     r   z%GlobalContextCheckpointState.__init__c                 C  r   r   )r   r   r   r   rX   r   r   r   r   r     r   z!GlobalContextCheckpointState.diffc                 C  r   r4   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                   @  s0   e Zd ZdZh dZ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.
    >   Zautocast_cache_enabledZautocast_cpu_enabledZautocast_enabledZautocast_cpu_dtypeZautocast_gpu_dtypeZgrad_enabledZtorch_function_enabledc                 C  r   r4   r   r   r   r   r   r     r   zGlobalContext.__init__c                 C  r   r4   )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)r`   r   r   rX   _supported_global_statesr   r   values)r   r   rc   argsr   r   r   r     s   
z GlobalContext.restore_graphstateN)r    r!   r"   __doc__r   r   r   r   r   r   r   r   r     s    
r   c                   @  sP   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ZdddZ	dS )	GuardsSetNc                 C  s   |d u rt  }|| _d S r4   )r   inner)r   r   r   r   r   r     s   
zGuardsSet.__init__c                 C  
   t | jS r4   )iterr   r   r   r   r   __iter__  r   zGuardsSet.__iter__c                 C  r   r4   )rX   r   r   r   r   r   __len__  r   zGuardsSet.__len__c                 C  s   t | j|j S r4   )r   r   r]   r   r   r   __sub__  r8   zGuardsSet.__sub__c                 C  r   r4   )r3   r   r   r   r   r   __bool__  r   zGuardsSet.__bool__r   skipguardrH   c                C  sN   || j v rd S |jd u rtjd| d|_|jd u rt |_| j | d S Nr*   r   )r   rF   r   extractr   TracingContextextract_stackadd)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.updater4   )r   rH   )r   r   )
r    r!   r"   r   r   r   r   r   r   r   r   r   r   r   r     s    
	r   c                   @  r   )GuardsContextc                 C  s   t  | _g | _d S r4   )r   r   Zaotautograd_guardsr   r   r   r   r     s   
zGuardsContext.__init__c                 C  s   t t| jjS r4   )r   r   r   r   r   r   r   r   r     r8   zGuardsContext.copy_graphstatec                 C  s   t |tsJ t|j| _d S r4   )r`   r   r   r   r   r   r   r   r     s   z GuardsContext.restore_graphstateNr   r   r   r   r   r     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 )CompileContextr2   c                   C  s   t jd usJ t jS r4   )_TLScompile_contextr   r   r   r   get  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| _d S r   )r`   r   r%   r&   )r   r%   r   r   r   r   $  s   
zCompileContext.__init__c                  C  s   t  } | d u r
d S | jS r4   )r   r   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 r4   )r   r   r%   r$   r&   r   r   r   r   current_trace_id0  s   
zCompileContext.current_trace_idN)r2   r   )r2   r   )	r    r!   r"   r   r   r   r   r   r   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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.
    r2   Optional[TracingContext]c                   C  r   )Ntracing_contextr   r   r   r   r   r   B  r   zTracingContext.try_getc                  C  s   t   } r| S td)Nz<TracingContext.get() must be called within an ongoing trace.)r   r   RuntimeError)ctxr   r   r   r   F  s
   zTracingContext.getc                 C  sN   t  | _t | _t | _|| _g | _d | _d | _	d | _
d | _d| _t | _d S )NF)r   Zguards_contextr   Zmodule_contextr   Zglobal_context	fake_modeframe_summary_stackloc_in_frameZfw_metadataZparams_flatoutput_stridesZ#force_unspec_int_unbacked_size_liker   Ztensor_to_context)r   r   r   r   r   r   N  s   
zTracingContext.__init__c                  k  s    i }t  }|  D ]	}t||||< q|  D ]
\}}t||| qzd V  W | D ]
\}}t||| q-d S | D ]
\}}t||| q>w r4   )r   r   r   r   itemssetattr)kwargsZpriorr   keyvalr   r   r   patchq  s   zTracingContext.patchc                  C  sD   t  } | d u rt S t| j}| jd ur|| j tj|S r4   )	r   r   	tracebackStackSummaryr   r   r   r   	from_list)r   rF   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   unittestZmockr   objectrx   hasattrr   )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 )Nr   )
r   r   r   r   r   rx   r   r   r   pop)frame_summaryr   oldr   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 r4   )r   r   r   )r   Zold_output_stridesr   r   r   report_output_strides  s   
z$TracingContext.report_output_stridesc                 C  s   t | ||t _d S r4   )r   FrameSummaryr   r   r   )filenamelinenoZ
frame_namer   r   r   set_current_loc  s   zTracingContext.set_current_locN)r2   r   )r2   r   )r    r!   r"   r   r   r   r   r   r   r   r   
contextlibr   r   r   r   r   r   r   r   r   :  s.    #
r   contextc                 c  s0    t tdd }| t_z	| V  W |t_d S |t_w r   )r   r   r   )r   old_contextr   r   r   r     s   r   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   )
r   r   r   rx   r   r   r   r   Z	shape_envcleanup)r   r   r   r   r   r   tracing  s,   	




r  T)frozenc                   @  s<   e Zd Zdd ZdddZddd	ZdddZdddZdS )rI   c                 C     t  r4   NotImplementedError)r   Zcodegenr   r   r   reconstruct      zSource.reconstructr2   r)   c                 C  r  r4   r  r   r   r   r   rf     r  zSource.guard_sourcer(   c                 C  r  r4   r  r   r   r   r   rR     r  zSource.namerH   c                 C  s   |   tju r
t t| |S r4   )rf   r)   r@   r  rH   )r   fnr   r   r   
make_guard	  s   
zSource.make_guardr3   c                 C  s   |    S r4   )rf   r;   r   r   r   r   r;     s   zSource.is_nn_moduleNr   r   )r2   rH   r>   )r    r!   r"   r  rf   rR   r
  r;   r   r   r   r   rI     s    


rI   c                   @  s   e Zd ZU ded< dS )ChainedSourcerI   baseNrG   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 ro   z doesn't match mode (z

fake mode from z allocated at:
z
fake mode from )Ztorch._subclasses.fake_tensorr  r  r   r   r   r   Ztorch.utils._python_dispatchr  	enumeratereversedr`   pytreeZtree_leavesrF   )r  r  r  Z
fake_modesr   r   r  imZflat_inputsZ
flat_inputZdesc1i1Zdesc2i2r   r   r   detect_fake_mode  sH   



&r  )r   r   )r   r   r4   )r  r   )D
__future__r   r   dataclassesenumra   logging	threadingr   Zunittest.mockr   rh   abcr   r   r   typingr   r   r   r	   r
   r   r   r   r   r   r   ZtorchZtorch.utilsr   r  Ztorch.utils._tracebackr   Ztorch.utils.weakr   	getLoggerr    ry   Zsympyr   r$   Enumr)   rB   rD   	dataclassrH   r   r   r   r   r   r   r   r   r   r   r   localr   r   r   r   r  rI   r  r  r   r   r   r   <module>   s|    4
  
!
#  	

