a
    h                     @   s  d Z ddlZddlZddlZddlmZmZmZmZ ddl	m
Z
mZmZ ddlmZ ddlmZmZ ertddlmZ ejejejejejejejejejejejejejejejejejejejeji
Zejejejejejejejejejejejejejejejejejejejeji
Zejejejejejejejejejejejejejejejejejejejeji
Z ejejejejejejejejejejejejejejejejejejejeji
Z!d	d
 Z"ej#ddG dd deZ$ej#ddG dd deZ%ej#ddG dd deZ&ej#ddG dd deZ'ej#ddG dd deZ(ej#ddG dd de
Z)ej#ddG dd de)Z*ej#ddG dd de
Z+ej#ddG dd de
Z,ej#ddG dd  d eZ-ej#ddG d!d" d"e
Z.ej#ddG d#d$ d$e+Z/ej#ddG d%d& d&e+Z0ej#ddG d'd( d(eZ1G d)d* d*ej2Z3ej#ddG d+d, d,e
Z4ej#ddG d-d. d.e
Z5ej#ddG d/d0 d0e
Z6ej#ddG d1d2 d2e
Z7ej#ddG d3d4 d4e
Z8ej#ddG d5d6 d6e
Z9G d7d8 d8e
Z:ej#ddG d9d: d:e
Z;ej#ddG d;d< d<e
Z<ej#ddG d=d> d>e
Z=ej#ddG d?d@ d@e
Z>ej#ddG dAdB dBe
Z?ej#ddG dCdD dDe<Z@ej#ddG dEdF dFe<ZAej#ddG dGdH dHe
ZBej#ddG dIdJ dJe
ZCej#ddG dKdL dLe
ZDej#ddG dMdN dNe
ZEej#ddG dOdP dPeEZFej#ddG dQdR dReFZGej#ddG dSdT dTeEZHej#ddG dUdV dVeZIej#ddG dWdX dXeZJej#ddG dYdZ dZeZKej#ddG d[d\ d\e
ZLej#ddG d]d^ d^e
ZMej#ddG d_d` d`e
ZNej#ddG dadb dbe
ZOej#ddG dcdd ddeZPej#ddG dedf dfeZQdgdheeeR didjdkZSdgdhedldmdnZTeeUdidodpZVeeeR didqdrZWedldsdtZXeedudvdwZYejZedldxdyZ[ejZedldzd{Z\ejZedld|d}Z]ejZedld~dZ^ejZedlddZ_ejZedlddZ`dS )a  
This module provides Source classes that track the origins of values in PyTorch Dynamo.
Sources represent where values come from (e.g. local variables, globals, attributes) and
are used for guard generation and code reconstruction during compilation.

The module includes specialized sources for:
- Local variables and synthetic locals
- Global variables and constants
- Object attributes and method calls
- NN module specialization (specialized vs unspecialized)
- Random values and tensor properties
- Default argument handling
- FSDP (Fully Sharded Data Parallel) modules

Sources play a key role in Dynamo's guard system by tracking value origins for
guard generation, and in code reconstruction by providing methods to rebuild
the code needed to recreate values.
    N)AnyOptionalTYPE_CHECKINGUnion)ChainedSourceGuardSourceSource   )utils)create_call_functioncreate_instruction)	PyCodegenc                 C   s>   t | trdS z|  tjkr$W dS W n ty8   Y n0 dS NTF)
isinstanceConstantSourceguard_sourcer   CONSTANTNotImplementedErrorsource r   B/var/www/auris/lib/python3.9/site-packages/torch/_dynamo/source.pyis_constant_sourcec   s    

r   T)frozenc                   @   s`   e Zd ZU eed< dZeed< dZee	e  ed< dZ
eed< ddd	d
Zdd Zdd ZdS )LocalSource
local_nameFis_inputNdynamismis_derefed_cell_contentsr   codegenc                 C   s*   | j r|| j n||| j d S N)r   Z
load_derefr   append_outputcreate_loadselfr    r   r   r   reconstruct   s    zLocalSource.reconstructc                 C   s   t jS r!   )r   LOCALr%   r   r   r   r      s    zLocalSource.guard_sourcec                 C   s   dt | j dS )NzL[])reprr   r(   r   r   r   name   s    zLocalSource.name)__name__
__module____qualname__str__annotations__r   boolr   r   	frozensetr   r&   r   r+   r   r   r   r   r   o   s   
r   c                   @   s4   e Zd ZU eed< ddddZdd Zdd	 Zd
S )SyntheticLocalSourcer   r   r   c                 C   s   | || j d S r!   )r"   r#   r   r$   r   r   r   r&      s    z SyntheticLocalSource.reconstructc                 C   s   t jS r!   )r   ZSYNTHETIC_LOCALr(   r   r   r   r      s    z!SyntheticLocalSource.guard_sourcec                 C   s   d| j dS )NzSYNTHETIC_LOCAL[r)   )r   r(   r   r   r   r+      s    zSyntheticLocalSource.nameNr,   r-   r.   r/   r0   r&   r   r+   r   r   r   r   r3      s   
r3   c                   @   s4   e Zd ZU eed< dd ZddddZdd	 Zd
S )RandomValueSourcerandom_call_indexc                 C   s   t jS r!   )r   ZRANDOM_VALUEr(   r   r   r   r      s    zRandomValueSource.guard_sourcer   r   c                 C   s:   | ||jjj | || j | td d S NBINARY_SUBSCR)r"   r#   ZtxoutputZrandom_values_varcreate_load_constr6   r   r$   r   r   r   r&      s    zRandomValueSource.reconstructc                 C   s   d| j  S )NZrandom_value_)r6   r(   r   r   r   r+      s    zRandomValueSource.nameN)r,   r-   r.   intr0   r   r&   r+   r   r   r   r   r5      s   
r5   c                   @   s4   e Zd ZU eed< ddddZdd Zdd	 Zd
S )GlobalSourceglobal_namer   r   c                 C   s   | |j| jdd d S NTaddr"   create_load_globalr=   r$   r   r   r   r&      s    zGlobalSource.reconstructc                 C   s   t jS r!   r   GLOBALr(   r   r   r   r      s    zGlobalSource.guard_sourcec                 C   s   dt | j dS )NG[r)   r*   r=   r(   r   r   r   r+      s    zGlobalSource.nameNr4   r   r   r   r   r<      s   
r<   c                   @   s4   e Zd ZU eed< ddddZdd Zdd	 Zd
S )GlobalWeakRefSourcer=   r   r   c                    s(      fdd  tdd d S )Nc                      s      jjddS r>   rA   r   r    r%   r   r   <lambda>   s   z1GlobalWeakRefSource.reconstruct.<locals>.<lambda>r   Fadd_push_nullextend_outputr   r$   r   rH   r   r&      s    zGlobalWeakRefSource.reconstructc                 C   s   t jS r!   rC   r(   r   r   r   r      s    z GlobalWeakRefSource.guard_sourcec                 C   s   dt | j dS )NrE   z]()rF   r(   r   r   r   r+      s    zGlobalWeakRefSource.nameNr4   r   r   r   r   rG      s   
rG   c                   @   s*   e Zd ZddddZdd Zdd Zd	S )
WeakRefCallSourcer   r   c                    s(      fdd  tdd d S )Nc                      s
    j S r!   baser   rH   r   r   rI          z/WeakRefCallSource.reconstruct.<locals>.<lambda>r   FrJ   r$   r   rH   r   r&      s    zWeakRefCallSource.reconstructc                 C   s
   | j  S r!   rO   r   r(   r   r   r   r      s    zWeakRefCallSource.guard_sourcec                 C   s   | j   dS )Nz()rO   r+   r(   r   r   r   r+      s    zWeakRefCallSource.nameNr,   r-   r.   r&   r   r+   r   r   r   r   rM      s   rM   c                   @   s   e Zd ZdS )CallFunctionNoArgsSourceNr,   r-   r.   r   r   r   r   rT      s   rT   c                   @   s<   e Zd ZU eed< dd ZddddZdd	 Zd
d ZdS )
AttrSourcememberc                 C   s^   | j sJ dd| jv rZ| jd}t| dt| j d|d d  t| d|d  d S Nz9Can't construct an AttrSource without a valid base source.rO   rW   rO   rW   splitobject__setattr__rV   joinr%   Zmember_partsr   r   r   __post_init__   s    
zAttrSource.__post_init__r   r   c                 C   s    || j  ||| j d S r!   rO   rL   create_load_attrsrW   r$   r   r   r   r&      s    
zAttrSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r      s    zAttrSource.guard_sourcec                 C   s:   | j  s$d| j  d| j dS | j  d| j  S )Nzgetattr(, )rY   )rW   isidentifierrO   r+   r(   r   r   r   r+      s    
zAttrSource.nameN	r,   r-   r.   r/   r0   ra   r&   r   r+   r   r   r   r   rV      s
   
	rV   c                   @   s<   e Zd ZU eed< dd ZddddZdd	 Zd
d ZdS )GenericAttrSourcerW   c                 C   s^   | j sJ dd| jv rZ| jd}t| dt| j d|d d  t| d|d  d S rX   r[   r`   r   r   r   ra      s    
zGenericAttrSource.__post_init__r   r   c                 C   s    || j  ||| j d S r!   rb   r$   r   r   r   r&     s    
zGenericAttrSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    zGenericAttrSource.guard_sourcec                 C   s   d| j   d| jdS )Nzobject.__getattribute__(rd   re   rO   r+   rW   r(   r   r   r   r+     s    zGenericAttrSource.nameNrg   r   r   r   r   rh      s
   
	rh   c                   @   s(   e Zd ZU dZeed< ddddZdS )LocalCellSourcez
    Conceptually, this class is `LocalSource` for cell objects implicitly
    generated by Python (e.g., captured variables).
    r   r   r   c                 C   s   | || j d S r!   )r"   Zcreate_load_closurer   r$   r   r   r   r&     s    zLocalCellSource.reconstructN)r,   r-   r.   __doc__r/   r0   r&   r   r   r   r   rj     s   
rj   c                   @   s8   e Zd ZU dZeed< ddddZdd Zd	d
 ZdS )
GradSourceZgradrW   r   r   c                 C   s    || j  ||| j d S r!   rb   r$   r   r   r   r&   *  s    
zGradSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r   .  s    zGradSource.guard_sourcec                 C   s   | j   d| j S )NrY   ri   r(   r   r   r   r+   1  s    zGradSource.nameN)	r,   r-   r.   rW   r/   r0   r&   r   r+   r   r   r   r   rl   &  s   
rl   c                   @   s   e Zd Zdd ZdS )ParamBufferSourcec                 C   s   t | j  S r!   #_GUARD_SOURCE_SPECIALIZED_NN_MODULErO   r   r(   r   r   r   r   7  s    zParamBufferSource.guard_sourceNr,   r-   r.   r   r   r   r   r   rm   5  s   rm   c                   @   s   e Zd ZdS )UnspecializedParamBufferSourceNrU   r   r   r   r   rq   <  s   rq   c                   @   s>   e Zd ZU dZee ed< dd Zdd Zdd Z	d	d
 Z
dS )EphemeralSourceNdescc                 C   s   t jS r!   )r   Z	EPHEMERALr(   r   r   r   r   N  s    zEphemeralSource.guard_sourcec                 C   s    d| j d urd| j  nd dS )Nz
<ephemeralz:  >)rs   r(   r   r   r   r+   Q  s    zEphemeralSource.namec                 C   s   t d S r!   r   r%   fnr   r   r   
make_guardT  s    zEphemeralSource.make_guardc                 C   s   dS NTr   r(   r   r   r   is_ephemeralW  s    zEphemeralSource.is_ephemeral)r,   r-   r.   rs   r   r/   r0   r   r+   ry   r{   r   r   r   r   rr   J  s
   
rr   c                   @   s    e Zd ZdZdZdZdd ZdS )TensorPropertyr   r	      c                 C   s.   | t ju rdS | t ju rdS | t ju r*dS d S )NsizeZstrideZstorage_offset)r|   SIZESTRIDESTORAGE_OFFSETr(   r   r   r   method_name`  s    


zTensorProperty.method_nameN)r,   r-   r.   r   r   r   r   r   r   r   r   r|   [  s   r|   c                   @   sL   e Zd ZU eed< dZee ed< dd Zdddd	Z	d
d Z
dd ZdS )TensorPropertySourcepropNidxc                 C   s<   | j d usJ | jtju r*| jd u s8J n| jd us8J d S r!   )rO   r   r|   r   r   r(   r   r   r   ra   n  s    z"TensorPropertySource.__post_init__r   r   c                    s\      fdd  j jd ur:  j  tjd urNdndd d S )Nc                      s     tjdj  S )NZcall_)load_import_fromr
   r,   r   r   r   rH   r   r   rI   w  s   z2TensorPropertySource.reconstruct.<locals>.<lambda>r}   r	   F)rK   rO   r   r"   r:   rL   r   r$   r   rH   r   r&   u  s    

z TensorPropertySource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    z!TensorPropertySource.guard_sourcec                 C   s   | j tju r$| j  d| j dS | j tju rH| j  d| j dS | j tju rr| jd u sbJ | j  dS td| j  d S )Nz.size()[r)   z
.stride()[z.storage_offset()z
unhandled )	r   r|   r   rO   r+   r   r   r   AssertionErrorr(   r   r   r   r+     s    zTensorPropertySource.name)r,   r-   r.   r|   r0   r   r   r;   ra   r&   r   r+   r   r   r   r   r   i  s   
r   c                   @   s<   e Zd ZU eed< dd ZddddZdd	 Zd
d ZdS )IndexedSourcer   c                 C   s   | j d usJ d S r!   rN   r(   r   r   r   ra     s    zIndexedSource.__post_init__r   r   c                 C   s   t d S r!   rv   r$   r   r   r   r&     s    zIndexedSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    zIndexedSource.guard_sourcec                 C   s   d| j  d| j  dS )N(rd   re   )r   rO   r+   r(   r   r   r   r+     s    zIndexedSource.nameN)	r,   r-   r.   r;   r0   ra   r&   r   r+   r   r   r   r   r     s
   
r   c                   @   s2   e Zd Zdd ZddddZdd Zd	d
 ZdS )NegateSourcec                 C   s   | j d usJ d S r!   rN   r(   r   r   r   ra     s    zNegateSource.__post_init__r   r   c                 C   s   t d S r!   rv   r$   r   r   r   r&     s    zNegateSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    zNegateSource.guard_sourcec                 C   s   | j   dS )Nz
.__neg__()rR   r(   r   r   r   r+     s    zNegateSource.nameNr,   r-   r.   ra   r&   r   r+   r   r   r   r   r     s   r   c                   @   s2   e Zd Zdd ZddddZdd Zd	d
 ZdS )ConvertIntSourcec                 C   s   | j d usJ d S r!   rN   r(   r   r   r   ra     s    zConvertIntSource.__post_init__r   r   c                 C   s   || j  d S r!   rN   r$   r   r   r   r&     s    zConvertIntSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    zConvertIntSource.guard_sourcec                 C   s   d| j   dS )Nz!cast_symbool_to_symint_guardless(re   rR   r(   r   r   r   r+     s    zConvertIntSource.nameNr   r   r   r   r   r     s   r   c                   @   s2   e Zd Zdd ZddddZdd Zd	d
 ZdS )FlattenScriptObjectSourcec                 C   s   | j d usJ d S r!   rN   r(   r   r   r   ra     s    z'FlattenScriptObjectSource.__post_init__r   r   c                 C   s   || j  d S r!   rN   r$   r   r   r   r&     s    z%FlattenScriptObjectSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    z&FlattenScriptObjectSource.guard_sourcec                 C   s   | j   dS )Nz.__obj_flatten__()rR   r(   r   r   r   r+     s    zFlattenScriptObjectSource.nameNr   r   r   r   r   r     s   r   c                   @   s2   e Zd Zdd ZddddZdd Zd	d
 ZdS )ScriptObjectQualifiedNameSourcec                 C   s   | j d usJ d S r!   rN   r(   r   r   r   ra     s    z-ScriptObjectQualifiedNameSource.__post_init__r   r   c                 C   s   || j  d S r!   rN   r$   r   r   r   r&     s    z+ScriptObjectQualifiedNameSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    z,ScriptObjectQualifiedNameSource.guard_sourcec                 C   s   | j   dS )Nz._type().qualified_name()rR   r(   r   r   r   r+     s    z$ScriptObjectQualifiedNameSource.nameNr   r   r   r   r   r     s   r   c                   @   s*   e Zd ZddddZdd Zdd Zd	S )
AttrProxySourcer   r   c                 C   s   || j  d S r!   rN   r$   r   r   r   r&     s    zAttrProxySource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    zAttrProxySource.guard_sourcec                 C   s   | j   dS )Nz.get_base()rR   r(   r   r   r   r+     s    zAttrProxySource.nameNrS   r   r   r   r   r     s   r   c                   @   s   e Zd ZU eeef ed< dZeed< e	j
ddddZ
eed< e	j
ddddZeed< dd Zd	d
ddZdd Zdd ZdS )DefaultsSourceidx_keyFis_kw)initr*   comparefield_namec              
   C   s   | j sJ d| jr^t| jts$J t| dd t| d| j   d| j d| j d nHt| jt	snJ t| dd t| d| j   d| j d	| j d
 d S )Nz]Base must be a valid source in order to properly track and guard this Defaults to its origin.r   __kwdefaults__r   rY   z['z']__defaults__[r)   )
rO   r   r   r   r/   r]   r^   r+   r   r;   r(   r   r   r   ra     s    ""zDefaultsSource.__post_init__r   r   c                 C   s@   || j  ||| j ||| j |td d S r7   )rO   rL   rc   r   r"   r:   r   r   r$   r   r   r   r&     s    
zDefaultsSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r   	  s    zDefaultsSource.guard_sourcec                 C   s   | j S r!   )r   r(   r   r   r   r+     s    zDefaultsSource.nameN)r,   r-   r.   r   r;   r/   r0   r   r1   dataclassesr   r   ra   r&   r   r+   r   r   r   r   r     s   
r   c                       sX   e Zd ZU eed< dZeed<  fddZdddd	Zd
d Z	dd Z
dd Z  ZS )GetItemSourceindexFindex_is_slicec                    s@   | j d usJ t| jtr<t d| j  t dd d S )Nr   r   T)rO   r   r   slicesuperr^   
__reduce__r(   	__class__r   r   ra     s    zGetItemSource.__post_init__r   r   c                 C   sJ   || j  | jr&|||   n||| j |td d S r7   )rO   r   r"   r:   unpack_slicer   r   r$   r   r   r   r&     s
    
zGetItemSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r   $  s    zGetItemSource.guard_sourcec                 C   s   | j s
J | j\}}|| S r!   )r   r   )r%   Zslice_classZ
slice_argsr   r   r   r   '  s    

zGetItemSource.unpack_slicec                 C   sL   t | jtrJ | jr0| j  d|  dS | j  d| jdS d S Nr   r)   )r   r   r   r   rO   r+   r   r(   r   r   r   r+   ,  s    zGetItemSource.name)r,   r-   r.   r   r0   r   r1   ra   r&   r   r   r+   __classcell__r   r   r   r   r     s   
r   c                   @   s<   e Zd ZU eed< dd ZddddZdd	 Zd
d ZdS )ConstDictKeySourcer   c                 C   s
   | j  S r!   rQ   r(   r   r   r   r   ;  s    zConstDictKeySource.guard_sourcer   r   c                    sB      fdd  | j   | j  tdd d S )Nc                      s     tjdS )NZdict_keys_getitemr   r
   r,   r   r   r   r   rI   @  rP   z0ConstDictKeySource.reconstruct.<locals>.<lambda>r}   FrK   rO   r"   r:   r   rL   r   r$   r   r   r   r&   >  s    

zConstDictKeySource.reconstructc                 C   s   d| j   d| jdS )Nzlist(dict.keys(z))[r)   rO   r+   r   r(   r   r   r   r+   F  s    zConstDictKeySource.namec                 C   s   dS rz   r   r(   r   r   r   is_dict_keyJ  s    zConstDictKeySource.is_dict_keyN)	r,   r-   r.   r   r0   r   r&   r+   r   r   r   r   r   r   7  s
   
r   c                   @   s<   e Zd ZU eed< dd Zdd Zdddd	Zd
d ZdS )DictGetItemSourcer   c                 C   s,   ddl m} t| jts(|| js(J d S Nr	   )ConstantVariable	variablesr   r   r   r   Z
is_literalr%   r   r   r   r   ra   V  s    
zDictGetItemSource.__post_init__c                 C   s
   | j  S r!   rQ   r(   r   r   r   r   ]  s    zDictGetItemSource.guard_sourcer   r   c                 C   sF   || j  t| jtr"|| j n||| j |td d S r7   )rO   r   r   r   r"   r:   r   r$   r   r   r   r&   `  s
    
zDictGetItemSource.reconstructc                 C   sD   t | jtr(| j  d| j  dS | j  d| jdS d S r   r   r   r   rO   r+   r(   r   r   r   r+   k  s    zDictGetItemSource.nameN	r,   r-   r.   r   r0   ra   r   r&   r+   r   r   r   r   r   O  s
   
r   c                   @   s<   e Zd ZU eed< dd Zdd Zdddd	Zd
d ZdS )DictSubclassGetItemSourcer   c                 C   s,   ddl m} t| jts(|| js(J d S r   r   r   r   r   r   ra   {  s    
z'DictSubclassGetItemSource.__post_init__c                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    z&DictSubclassGetItemSource.guard_sourcer   r   c                    sZ      fdd  | j t| jtr4 | j n  | j  tdd d S )Nc                      s     tjdS )NZdict_getitemr   r   r   r   r   rI     rP   z7DictSubclassGetItemSource.reconstruct.<locals>.<lambda>r}   F)	rK   rO   r   r   r   r"   r:   rL   r   r$   r   r   r   r&     s    

z%DictSubclassGetItemSource.reconstructc                 C   sF   t | jtr*d| j  d| j  dS | j  d| jdS d S )Nzdict.__getitem__(rd   re   r   r)   r   r(   r   r   r   r+     s    zDictSubclassGetItemSource.nameNr   r   r   r   r   r   t  s
   
r   c                   @   s&   e Zd ZdZddddZdd ZdS )	ListGetItemSourcezY
    Same as GetItemSource with reconstruct and name overridden to be list specific.
    r   r   c                    sR      fdd  | j | jr,tdn  | j  tdd d S )Nc                      s     tjdS )NZlist_getitemr   r   r   r   r   rI     rP   z/ListGetItemSource.reconstruct.<locals>.<lambda>>List[slice] is a temporary object and should not have a sourcer}   F)	rK   rO   r   RuntimeErrorr"   r:   r   rL   r   r$   r   r   r   r&     s    

zListGetItemSource.reconstructc                 C   s>   t | jtrJ | jr tdnd| j  d| jdS d S )Nr   zlist.__getitem__(rd   re   )r   r   r   r   r   rO   r+   r(   r   r   r   r+     s    zListGetItemSource.nameN)r,   r-   r.   rk   r&   r+   r   r   r   r   r     s   r   c                   @   s"   e Zd ZddddZdd ZdS )TupleIteratorGetItemSourcer   r   c                    sB      fdd  | j   | j  tdd d S )Nc                      s     tjdS )NZtuple_iterator_getitemr   r   r   r   r   rI     rP   z8TupleIteratorGetItemSource.reconstruct.<locals>.<lambda>r}   Fr   r$   r   r   r   r&     s    

z&TupleIteratorGetItemSource.reconstructc                 C   s   d| j   d| jdS )Nz___tuple_iterator_getitem(rd   re   r   r(   r   r   r   r+     s    zTupleIteratorGetItemSource.nameN)r,   r-   r.   r&   r+   r   r   r   r   r     s   r   c                   @   s*   e Zd ZddddZdd Zdd Zd	S )
DataclassFieldsSourcer   r   c                    s0      fdd  | j  tdd d S )Nc                      s     tjdS )NZdataclass_fieldsr   r   r   r   r   rI     rP   z3DataclassFieldsSource.reconstruct.<locals>.<lambda>r	   FrK   rO   rL   r   r$   r   r   r   r&     s
    

z!DataclassFieldsSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    z"DataclassFieldsSource.guard_sourcec                 C   s   d| j   dS )Nz___dataclass_fields(re   rR   r(   r   r   r   r+     s    zDataclassFieldsSource.nameNrS   r   r   r   r   r     s   r   c                   @   s2   e Zd Zdd ZddddZdd Zd	d
 ZdS )
TypeSourcec                 C   s   | j d usJ d S r!   rN   r(   r   r   r   ra     s    zTypeSource.__post_init__r   r   c                    s0      fdd  | j  tdd d S )Nc                      s     ddS )Nbuiltinstyper   r   r   r   r   rI     rP   z(TypeSource.reconstruct.<locals>.<lambda>r	   Fr   r$   r   r   r   r&     s    
zTypeSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    zTypeSource.guard_sourcec                 C   s   d| j   dS )Nztype(re   rR   r(   r   r   r   r+     s    zTypeSource.nameNr   r   r   r   r   r     s   r   c                   @   s*   e Zd ZddddZdd Zdd Zd	S )
OptimizerSourcer   r   c                 C   s   || j  d S r!   rN   r$   r   r   r   r&     s    zOptimizerSource.reconstructc                 C   s
   | j  S r!   rQ   r(   r   r   r   r     s    zOptimizerSource.guard_sourcec                 C   s
   | j  S r!   rR   r(   r   r   r   r+     s    zOptimizerSource.nameNrS   r   r   r   r   r     s   r   c                   @   s*   e Zd ZddddZdd Zdd Zd	S )
NNModuleSourcer   r   c                 C   s   || j  d S r!   rN   r$   r   r   r   r&     s    zNNModuleSource.reconstructc                 C   s   t | j  S r!   rn   r(   r   r   r   r     s    zNNModuleSource.guard_sourcec                 C   s
   | j  S r!   rR   r(   r   r   r   r+     s    zNNModuleSource.nameNrS   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd ZdS )UnspecializedNNModuleSourcec                 C   s   t | j  S r!   )%_GUARD_SOURCE_UNSPECIALIZED_NN_MODULErO   r   r(   r   r   r   r     s    z(UnspecializedNNModuleSource.guard_sourceNrp   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd ZdS )"UnspecializedBuiltinNNModuleSourcec                 C   s   t | j  S r!   )-_GUARD_SOURCE_UNSPECIALIZED_BUILTIN_NN_MODULErO   r   r(   r   r   r   r     s    z/UnspecializedBuiltinNNModuleSource.guard_sourceNrp   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd ZdS )FSDPNNModuleSourcec                 C   s   t | j  S r!   )_GUARD_SOURCE_FSDP_MODULErO   r   r(   r   r   r   r     s    zFSDPNNModuleSource.guard_sourceNrp   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )GlobalStateSourcec                 C   s   dS Nrt   r   r(   r   r   r   r+   #  s    zGlobalStateSource.namec                 C   s   t jS r!   rC   r(   r   r   r   r   &  s    zGlobalStateSource.guard_sourceNr,   r-   r.   r+   r   r   r   r   r   r   !  s   r   c                   @   s<   e Zd ZU eed< dd Zdd Zdddd	Zd
d ZdS )TorchFunctionModeStackSourceindc                 C   s   d|    dS )Nz$___get_torch_function_mode_stack_at(re   )
_get_indexr(   r   r   r   r+   .  s    z!TorchFunctionModeStackSource.namec                 C   s   ddl m} || jS )Nr	   )TorchFunctionModeStackVariable)Zvariables.torch_functionr   Zget_mode_indexr   )r%   r   r   r   r   r   1  s    z'TorchFunctionModeStackSource._get_indexr   r   c                    s<      fdd   |  g  tdd d S )Nc                      s     tjdS )NZ get_torch_function_mode_stack_atr   r   r   r   r   rI   8  s   z:TorchFunctionModeStackSource.reconstruct.<locals>.<lambda>r	   F)rK   rL   r:   r   r   r$   r   r   r   r&   6  s
    
z(TorchFunctionModeStackSource.reconstructc                 C   s   t jS r!   rC   r(   r   r   r   r   ?  s    z)TorchFunctionModeStackSource.guard_sourceN)	r,   r-   r.   r;   r0   r+   r   r&   r   r   r   r   r   r   *  s
   
	r   c                   @   s<   e Zd ZU eed< ddddZdd Zdd	 Zd
d ZdS )r   source_namer   r   c                 C   s   | |j| jdd d S )NFr?   )r"   rB   r   r$   r   r   r   r&   G  s    zConstantSource.reconstructc                 C   s   t jS r!   )r   r   r(   r   r   r   r   J  s    zConstantSource.guard_sourcec                 C   s   | j S r!   )r   r(   r   r   r   r+   M  s    zConstantSource.namec                 C   s   t d S r!   rv   rw   r   r   r   ry   P  s    zConstantSource.make_guardN)	r,   r-   r.   r/   r0   r&   r   r+   ry   r   r   r   r   r   C  s
   
r   c                   @   s0   e Zd ZedddZdd Zdddd	Zd
S )NumpyTensorSourcereturnc                 C   s   d| j   dS )Nz___from_numpy(re   rR   r(   r   r   r   r+   V  s    zNumpyTensorSource.namec                 C   s
   | j  S r!   rQ   r(   r   r   r   r   Y  s    zNumpyTensorSource.guard_sourcer   r   c                    s0      fdd  | j  tdd d S )Nc                      s     ddS )NZtorchZ	as_tensorr   r   r   r   r   rI   ]  rP   z/NumpyTensorSource.reconstruct.<locals>.<lambda>r	   Fr   r$   r   r   r   r&   \  s    
zNumpyTensorSource.reconstructN)r,   r-   r.   r/   r+   r   r&   r   r   r   r   r   T  s   r   c                   @   s"   e Zd ZedddZdd ZdS )SubclassAttrListSourcer   c                 C   s   | j   dS )Nz.__tensor_flatten__()[0]rR   r(   r   r   r   r+   d  s    zSubclassAttrListSource.namec                 C   s
   | j  S r!   rQ   r(   r   r   r   r   g  s    z#SubclassAttrListSource.guard_sourceNr,   r-   r.   r/   r+   r   r   r   r   r   r   b  s   r   c                   @   s"   e Zd ZedddZdd ZdS )FloatTensorSourcer   c                 C   s   d| j   dS )Nz___as_tensor(re   rR   r(   r   r   r   r+   o  s    zFloatTensorSource.namec                 C   s
   | j  S r!   rQ   r(   r   r   r   r   r  s    zFloatTensorSource.guard_sourceNr   r   r   r   r   r   m  s   r   c                   @   s"   e Zd ZedddZdd ZdS )CallMethodItemSourcer   c                 C   s   | j   dS )Nz.item()rR   r(   r   r   r   r+   x  s    zCallMethodItemSource.namec                 C   s
   | j  S r!   rQ   r(   r   r   r   r   {  s    z!CallMethodItemSource.guard_sourceNr   r   r   r   r   r   v  s   r   c                   @   s   e Zd Zdd Zdd ZdS )ShapeEnvSourcec                 C   s   dS r   r   r(   r   r   r   r+     s    zShapeEnvSource.namec                 C   s   t jS r!   )r   Z	SHAPE_ENVr(   r   r   r   r     s    zShapeEnvSource.guard_sourceNr   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )BackwardStateSourcec                 C   s   dS r   r   r(   r   r   r   r+     s    zBackwardStateSource.namec                 C   s   t jS r!   )r   ZBACKWARD_STATEr(   r   r   r   r     s    z BackwardStateSource.guard_sourceNr   r   r   r   r   r     s   r   Fonly_allow_input)r   r   c                C   s:   t | trt| j|dS t | ts&d S |r4| js4d S | jS Nr   )r   r   get_local_source_namerO   r   r   r   r   r   r   r   r   r     s    


r   r   c                C   s   t | |dd uS r   )r   r   r   r   r   is_from_local_source  s    r   c                 C   s   t | d uS r!   )get_global_source_namer   r   r   r   is_from_global_source  s    r   c                 C   s(   t | trt| jS t | ts"d S | jS r!   )r   r   r   rO   r<   r=   r   r   r   r   r     s
    


r   c                 C   s,   t | trt| jS t | to*| jo*| j S r!   )r   r   is_from_nonlocal_sourcerO   r   r   r   r   r   r   r   r     s    


r   r   targetc                 C   s   t | trt| j|S | |kS r!   )r   r   is_from_sourcerO   r   r   r   r   r     s    
r   c                 C   s&   t | trdS t | tr"t| jS dS r   )r   r   r   &is_from_unspecialized_nn_module_sourcerO   r   r   r   r   r     s
    


r   c                 C   s&   t | trdS t | tr"t| jS dS r   )r   r   r   .is_from_unspecialized_builtin_nn_module_sourcerO   r   r   r   r   r     s
    


r   c                 C   s&   t | trdS t | tr"t| jS dS r   )r   rq   r   )is_from_unspecialized_param_buffer_sourcerO   r   r   r   r   r     s
    


r   c                 C   s&   t | trdS t | tr"t| jS dS r   )r   r   r   $is_from_flatten_script_object_sourcerO   r   r   r   r   r     s
    


r   c                 C   s&   t | trdS t | tr"t| jS dS r   )r   r   r   is_from_optimizer_sourcerO   r   r   r   r   r     s
    


r   c                 C   sr   t | trdS t | tr4t | jtr4| jjdkr4dS t | trZt | jtrZ| jjdkrZdS t | trnt| jS dS )NTr   r   F)	r   r   r   rO   rV   rW   r   r   is_from_defaultsr   r   r   r   r     s&    






r   )ark   r   enum	functoolstypingr   r   r   r   Ztorch._guardsr   r   r   rt   r
   Zbytecode_transformationr   r   r    r   r'   ZLOCAL_SPECIALIZED_NN_MODULErD   ZGLOBAL_SPECIALIZED_NN_MODULEZLOCAL_UNSPECIALIZED_NN_MODULEZGLOBAL_UNSPECIALIZED_NN_MODULEZ%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULEZ&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULEZLOCAL_FSDP_MODULEZGLOBAL_FSDP_MODULEro   r   r   r   r   Z	dataclassr   r3   r5   r<   rG   rM   rT   rV   rh   rj   rl   rm   rq   rr   Enumr|   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   r   r/   r   r   r1   r   r   r   r   	lru_cacher   r   r   r   r   r   r   r   r   r   <module>   s@  














(





$
&

$
*
(




















