o
    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r:d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diedjeeR fdkdlZSdgdhdiefdmdnZTdiedjeUfdodpZVdiedjeeR fdqdrZWdiefdsdtZXdieduefdvdwZYejZdiefdxdyZ[ejZdiefdzd{Z\ejZdiefd|d}Z]ejZdiefd~dZ^ejZdiefddZ_ejZdief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 dS  ty   Y dS w NTF)
isinstanceConstantSourceguard_sourcer   CONSTANTNotImplementedErrorsource r   Z/home/www/facesmatcher.com/frenv_anti/lib/python3.10/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
Zdd Zdd ZdS )LocalSource
local_nameFis_inputNdynamismis_derefed_cell_contentscodegenr   c                 C   s,   | j r|| j d S ||| j d S N)r   Z
load_derefr   append_outputcreate_loadselfr   r   r   r   reconstruct   s   zLocalSource.reconstructc                 C      t jS r    )r   LOCALr$   r   r   r   r         zLocalSource.guard_sourcec                 C      dt | j dS )NzL[])reprr   r(   r   r   r   name      zLocalSource.namer   r   )__name__
__module____qualname__str__annotations__r   boolr   r   	frozensetr   r%   r   r-   r   r   r   r   r   o   s   
 
r   c                   @   0   e Zd ZU eed< dddZdd Zdd	 Zd
S )SyntheticLocalSourcer   r   r   c                 C      | || j d S r    )r!   r"   r   r#   r   r   r   r%         z SyntheticLocalSource.reconstructc                 C   r&   r    )r   ZSYNTHETIC_LOCALr(   r   r   r   r      r)   z!SyntheticLocalSource.guard_sourcec                 C   s   d| j dS )NzSYNTHETIC_LOCAL[r+   )r   r(   r   r   r   r-         zSyntheticLocalSource.nameNr/   r0   r1   r2   r3   r4   r%   r   r-   r   r   r   r   r8      
   
 
r8   c                   @   s0   e Zd ZU eed< dd ZdddZdd	 Zd
S )RandomValueSourcerandom_call_indexc                 C   r&   r    )r   ZRANDOM_VALUEr(   r   r   r   r      r)   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_constr?   r   r#   r   r   r   r%      s   zRandomValueSource.reconstructc                 C   s   d| j  S )NZrandom_value_)r?   r(   r   r   r   r-         zRandomValueSource.nameNr/   )r0   r1   r2   intr4   r   r%   r-   r   r   r   r   r>      s
   
 
r>   c                   @   r7   )GlobalSourceglobal_namer   r   c                 C      | |j| jdd d S NTaddr!   create_load_globalrG   r#   r   r   r   r%         zGlobalSource.reconstructc                 C   r&   r    r   GLOBALr(   r   r   r   r      r)   zGlobalSource.guard_sourcec                 C   r*   )NG[r+   r,   rG   r(   r   r   r   r-      r.   zGlobalSource.nameNr/   r<   r   r   r   r   rF      r=   rF   c                   @   r7   )GlobalWeakRefSourcerG   r   r   c                    (      fdd  tdd d S )Nc                      s      jjddS rI   rL   r   r   r$   r   r   <lambda>   s    z1GlobalWeakRefSource.reconstruct.<locals>.<lambda>r   Fadd_push_nullextend_outputr   r#   r   rU   r   r%      s   zGlobalWeakRefSource.reconstructc                 C   r&   r    rO   r(   r   r   r   r      r)   z GlobalWeakRefSource.guard_sourcec                 C   r*   )NrQ   z]()rR   r(   r   r   r   r-      r.   zGlobalWeakRefSource.nameNr/   r<   r   r   r   r   rS      s
   
 
rS   c                   @   &   e Zd Zd
ddZdd Zdd Zd	S )WeakRefCallSourcer   r   c                    rT   )Nc                      s
    j S r    baser   rU   r   r   rV      s   
 z/WeakRefCallSource.reconstruct.<locals>.<lambda>r   FrW   r#   r   rU   r   r%      s   zWeakRefCallSource.reconstructc                 C   
   | j  S r    r]   r   r(   r   r   r   r         
zWeakRefCallSource.guard_sourcec                 C      | j   dS )Nz()r]   r-   r(   r   r   r   r-         zWeakRefCallSource.nameNr/   r0   r1   r2   r%   r   r-   r   r   r   r   r[      s    
r[   c                   @      e Zd ZdS )CallFunctionNoArgsSourceNr0   r1   r2   r   r   r   r   rf          rf   c                   @   8   e Zd ZU eed< dd ZdddZdd	 Zd
d ZdS )
AttrSourcememberc                 C   b   | j sJ dd| jv r/| jd}t| dt| j d|d d  t| d|d  d S d S Nz9Can't construct an AttrSource without a valid base source.r]   rk   r]   rk   splitobject__setattr__rj   joinr$   Zmember_partsr   r   r   __post_init__      
zAttrSource.__post_init__r   r   c                 C       || j  ||| j d S r    r]   rY   create_load_attrsrk   r#   r   r   r   r%         
zAttrSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r      r`   zAttrSource.guard_sourcec                 C   s:   | j  sd| j  d| j dS | j  d| j  S )Nzgetattr(, )rn   )rk   isidentifierr]   r-   r(   r   r   r   r-      s   
zAttrSource.nameNr/   	r0   r1   r2   r3   r4   rv   r%   r   r-   r   r   r   r   rj         
 
	rj   c                   @   ri   )GenericAttrSourcerk   c                 C   rl   rm   rp   ru   r   r   r   rv      rw   zGenericAttrSource.__post_init__r   r   c                 C   rx   r    ry   r#   r   r   r   r%     r{   zGenericAttrSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   zGenericAttrSource.guard_sourcec                 C      d| j   d| jdS )Nzobject.__getattribute__(r|   r}   r]   r-   rk   r(   r   r   r   r-     rN   zGenericAttrSource.nameNr/   r   r   r   r   r   r      r   r   c                   @   s$   e Zd ZU dZee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   r9   r    )r!   Zcreate_load_closurer   r#   r   r   r   r%     s   zLocalCellSource.reconstructNr/   )r0   r1   r2   __doc__r3   r4   r%   r   r   r   r   r     s   
 r   c                   @   s4   e Zd ZU dZeed< dddZdd Zd	d
 ZdS )
GradSourceZgradrk   r   r   c                 C   rx   r    ry   r#   r   r   r   r%   *  r{   zGradSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r   .  r`   zGradSource.guard_sourcec                 C   s   | j   d| j S )Nrn   r   r(   r   r   r   r-   1  r:   zGradSource.nameNr/   )	r0   r1   r2   rk   r3   r4   r%   r   r-   r   r   r   r   r   &  s
   
 
r   c                   @      e Zd Zdd ZdS )ParamBufferSourcec                 C      t | j  S r    #_GUARD_SOURCE_SPECIALIZED_NN_MODULEr]   r   r(   r   r   r   r   7  r;   zParamBufferSource.guard_sourceNr0   r1   r2   r   r   r   r   r   r   5      r   c                   @   re   )UnspecializedParamBufferSourceNrg   r   r   r   r   r   <  rh   r   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   r&   r    )r   Z	EPHEMERALr(   r   r   r   r   N  r)   zEphemeralSource.guard_sourcec                 C   s&   d| j d urd| j   dS d dS )Nz
<ephemeralz:  >)r   r(   r   r   r   r-   Q  s   &zEphemeralSource.namec                 C      t r    r   r$   fnr   r   r   
make_guardT     zEphemeralSource.make_guardc                 C      dS NTr   r(   r   r   r   is_ephemeralW  r   zEphemeralSource.is_ephemeral)r0   r1   r2   r   r   r3   r4   r   r-   r   r   r   r   r   r   r   J  s   
 r   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)r0   r1   r2   r   r   r   r   r   r   r   r   r   [  s
    r   c                   @   sH   e Zd ZU eed< dZee ed< dd Z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 sJ d S | jd usJ d S r    )r]   r   r   r   r   r(   r   r   r   rv   n  s   z"TensorPropertySource.__post_init__r   r   c                    s\      fdd  j jd ur  j  tjd ur'dndd d S )Nc                      s     tjdj  S )NZcall_)load_import_fromr
   r0   r   r   r   rU   r   r   rV   w  s    z2TensorPropertySource.reconstruct.<locals>.<lambda>r   r	   F)rX   r]   r   r!   rC   rY   r   r#   r   rU   r   r%   u  s   

z TensorPropertySource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   z!TensorPropertySource.guard_sourcec                 C   s   | j tju r| j  d| j dS | j tju r$| j  d| j dS | j tju r9| jd u s1J | j  dS td| j  )Nz.size()[r+   z
.stride()[z.storage_offset()z
unhandled )	r   r   r   r]   r-   r   r   r   AssertionErrorr(   r   r   r   r-     s   zTensorPropertySource.namer/   )r0   r1   r2   r   r4   r   r   rE   rv   r%   r   r-   r   r   r   r   r   i  s   
 
r   c                   @   ri   )IndexedSourcer   c                 C      | j d usJ d S r    r\   r(   r   r   r   rv     r.   zIndexedSource.__post_init__r   r   c                 C   r   r    r   r#   r   r   r   r%     r   zIndexedSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   zIndexedSource.guard_sourcec                 C   s   d| j  d| j  dS )N(r|   r}   )r   r]   r-   r(   r   r   r   r-     rN   zIndexedSource.nameNr/   )	r0   r1   r2   rE   r4   rv   r%   r   r-   r   r   r   r   r     s   
 
r   c                   @   .   e Zd Zdd ZdddZdd Zd	d
 ZdS )NegateSourcec                 C   r   r    r\   r(   r   r   r   rv     r.   zNegateSource.__post_init__r   r   c                 C   r   r    r   r#   r   r   r   r%     r   zNegateSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   zNegateSource.guard_sourcec                 C   ra   )Nz
.__neg__()rb   r(   r   r   r   r-     s   zNegateSource.nameNr/   r0   r1   r2   rv   r%   r   r-   r   r   r   r   r     
    
r   c                   @   r   )ConvertIntSourcec                 C   r   r    r\   r(   r   r   r   rv     r.   zConvertIntSource.__post_init__r   r   c                 C      || j  d S r    r\   r#   r   r   r   r%     r;   zConvertIntSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   zConvertIntSource.guard_sourcec                 C      d| j   dS )Nz!cast_symbool_to_symint_guardless(r}   rb   r(   r   r   r   r-     r.   zConvertIntSource.nameNr/   r   r   r   r   r   r     r   r   c                   @   r   )FlattenScriptObjectSourcec                 C   r   r    r\   r(   r   r   r   rv     r.   z'FlattenScriptObjectSource.__post_init__r   r   c                 C   r   r    r\   r#   r   r   r   r%     r;   z%FlattenScriptObjectSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   z&FlattenScriptObjectSource.guard_sourcec                 C   ra   )Nz.__obj_flatten__()rb   r(   r   r   r   r-     rc   zFlattenScriptObjectSource.nameNr/   r   r   r   r   r   r     r   r   c                   @   r   )ScriptObjectQualifiedNameSourcec                 C   r   r    r\   r(   r   r   r   rv     r.   z-ScriptObjectQualifiedNameSource.__post_init__r   r   c                 C   r   r    r\   r#   r   r   r   r%     r;   z+ScriptObjectQualifiedNameSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   z,ScriptObjectQualifiedNameSource.guard_sourcec                 C   ra   )Nz._type().qualified_name()rb   r(   r   r   r   r-     rc   z$ScriptObjectQualifiedNameSource.nameNr/   r   r   r   r   r   r     r   r   c                   @   rZ   )AttrProxySourcer   r   c                 C   r   r    r\   r#   r   r   r   r%     r;   zAttrProxySource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   zAttrProxySource.guard_sourcec                 C   ra   )Nz.get_base()rb   r(   r   r   r   r-     rc   zAttrProxySource.nameNr/   rd   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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r0t| jtsJ t| dd t| d| j   d| j d| j d d S t| jt	s8J 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   rn   z['z']__defaults__[r+   )
r]   r   r   r   r3   rr   rs   r-   r   rE   r(   r   r   r   rv     s   ""zDefaultsSource.__post_init__r   r   c                 C   s@   || j  ||| j ||| j |td d S r@   )r]   rY   rz   r   r!   rC   r   r   r#   r   r   r   r%     s   
zDefaultsSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r   	  r`   zDefaultsSource.guard_sourcec                 C      | j S r    )r   r(   r   r   r   r-     r)   zDefaultsSource.nameNr/   )r0   r1   r2   r   rE   r3   r4   r   r5   dataclassesr   r   rv   r%   r   r-   r   r   r   r   r     s   
 
r   c                       sT   e Zd ZU eed< dZeed<  fddZddd	Zd
d Z	dd Z
dd Z  ZS )GetItemSourceindexFindex_is_slicec                    sD   | j d usJ t| jtr t d| j  t dd d S d S )Nr   r   T)r]   r   r   slicesuperrs   
__reduce__r(   	__class__r   r   rv     s
   zGetItemSource.__post_init__r   r   c                 C   sJ   || j  | jr|||   n	||| j |td d S r@   )r]   r   r!   rC   unpack_slicer   r   r#   r   r   r   r%     s
   
zGetItemSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r   $  r`   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   sH   t | jtrJ | jr| j  d|  dS | j  d| jdS Nr   r+   )r   r   r   r   r]   r-   r   r(   r   r   r   r-   ,  s   zGetItemSource.namer/   )r0   r1   r2   r   r4   r   r5   rv   r%   r   r   r-   __classcell__r   r   r   r   r     s   
 
r   c                   @   ri   )ConstDictKeySourcer   c                 C   r^   r    r_   r(   r   r   r   r   ;  r`   zConstDictKeySource.guard_sourcer   r   c                    B      fdd  | j   | j  tdd d S )Nc                           tjdS )NZdict_keys_getitemr   r
   r0   r   r   r   r   rV   @      z0ConstDictKeySource.reconstruct.<locals>.<lambda>r   FrX   r]   r!   rC   r   rY   r   r#   r   r   r   r%   >     

zConstDictKeySource.reconstructc                 C   r   )Nzlist(dict.keys(z))[r+   r]   r-   r   r(   r   r   r   r-   F  s   zConstDictKeySource.namec                 C   r   r   r   r(   r   r   r   is_dict_keyJ  r   zConstDictKeySource.is_dict_keyNr/   )	r0   r1   r2   r   r4   r   r%   r-   r   r   r   r   r   r   7  s   
 
r   c                   @   8   e Zd ZU eed< dd Zdd Zddd	Zd
d ZdS )DictGetItemSourcer   c                 C   0   ddl m} t| jts|| jsJ d S d S Nr	   )ConstantVariable	variablesr   r   r   r   Z
is_literalr$   r   r   r   r   rv   V     
zDictGetItemSource.__post_init__c                 C   r^   r    r_   r(   r   r   r   r   ]  r`   zDictGetItemSource.guard_sourcer   r   c                 C   sF   || j  t| jtr|| j n	||| j |td d S r@   )r]   r   r   r   r!   rC   r   r#   r   r   r   r%   `  s
   
zDictGetItemSource.reconstructc                 C   s@   t | jtr| j  d| j  dS | j  d| jdS r   r   r   r   r]   r-   r(   r   r   r   r-   k  s   zDictGetItemSource.nameNr/   	r0   r1   r2   r   r4   rv   r   r%   r-   r   r   r   r   r   O  s   
 
r   c                   @   r   )DictSubclassGetItemSourcer   c                 C   r   r   r   r   r   r   r   rv   {  r   z'DictSubclassGetItemSource.__post_init__c                 C   r^   r    r_   r(   r   r   r   r     r`   z&DictSubclassGetItemSource.guard_sourcer   r   c                    sZ      fdd  | j t| jtr | j n	  | j  tdd d S )Nc                      r   )NZdict_getitemr   r   r   r   r   rV     r   z7DictSubclassGetItemSource.reconstruct.<locals>.<lambda>r   F)	rX   r]   r   r   r   r!   rC   rY   r   r#   r   r   r   r%     s   

z%DictSubclassGetItemSource.reconstructc                 C   sB   t | jtrd| j  d| j  dS | j  d| jdS )Nzdict.__getitem__(r|   r}   r   r+   r   r(   r   r   r   r-     s   zDictSubclassGetItemSource.nameNr/   r   r   r   r   r   r   t  s   
 
r   c                   @   s"   e Zd ZdZ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                    sP      fdd  | j | jrtd  | j  tdd d S )Nc                      r   )NZlist_getitemr   r   r   r   r   rV     r   z/ListGetItemSource.reconstruct.<locals>.<lambda>>List[slice] is a temporary object and should not have a sourcer   F)	rX   r]   r   RuntimeErrorr!   rC   r   rY   r   r#   r   r   r   r%     s   

zListGetItemSource.reconstructc                 C   s8   t | jtrJ | jrtdd| j  d| jdS )Nr   zlist.__getitem__(r|   r}   )r   r   r   r   r   r]   r-   r(   r   r   r   r-     s   zListGetItemSource.nameNr/   )r0   r1   r2   r   r%   r-   r   r   r   r   r     s    
r   c                   @   s   e Zd ZdddZdd ZdS )	TupleIteratorGetItemSourcer   r   c                    r   )Nc                      r   )NZtuple_iterator_getitemr   r   r   r   r   rV     r   z8TupleIteratorGetItemSource.reconstruct.<locals>.<lambda>r   Fr   r#   r   r   r   r%     r   z&TupleIteratorGetItemSource.reconstructc                 C   r   )Nz___tuple_iterator_getitem(r|   r}   r   r(   r   r   r   r-     rN   zTupleIteratorGetItemSource.nameNr/   )r0   r1   r2   r%   r-   r   r   r   r   r     s    
r   c                   @   rZ   )DataclassFieldsSourcer   r   c                    0      fdd  | j  tdd d S )Nc                      r   )NZdataclass_fieldsr   r   r   r   r   rV     r   z3DataclassFieldsSource.reconstruct.<locals>.<lambda>r	   FrX   r]   rY   r   r#   r   r   r   r%     s
   

z!DataclassFieldsSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   z"DataclassFieldsSource.guard_sourcec                 C   r   )Nz___dataclass_fields(r}   rb   r(   r   r   r   r-     r.   zDataclassFieldsSource.nameNr/   rd   r   r   r   r   r     s    
r   c                   @   r   )
TypeSourcec                 C   r   r    r\   r(   r   r   r   rv     r.   zTypeSource.__post_init__r   r   c                    r   )Nc                           ddS )Nbuiltinstyper   r   r   r   r   rV         z(TypeSource.reconstruct.<locals>.<lambda>r	   Fr   r#   r   r   r   r%        
zTypeSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   zTypeSource.guard_sourcec                 C   r   )Nztype(r}   rb   r(   r   r   r   r-     r.   zTypeSource.nameNr/   r   r   r   r   r   r     s
    
r   c                   @   rZ   )OptimizerSourcer   r   c                 C   r   r    r\   r#   r   r   r   r%     r;   zOptimizerSource.reconstructc                 C   r^   r    r_   r(   r   r   r   r     r`   zOptimizerSource.guard_sourcec                 C   r^   r    rb   r(   r   r   r   r-     r`   zOptimizerSource.nameNr/   rd   r   r   r   r   r         
r   c                   @   rZ   )NNModuleSourcer   r   c                 C   r   r    r\   r#   r   r   r   r%     r;   zNNModuleSource.reconstructc                 C   r   r    r   r(   r   r   r   r     r;   zNNModuleSource.guard_sourcec                 C   r^   r    rb   r(   r   r   r   r-     r`   zNNModuleSource.nameNr/   rd   r   r   r   r   r     r   r   c                   @   r   )UnspecializedNNModuleSourcec                 C   r   r    )%_GUARD_SOURCE_UNSPECIALIZED_NN_MODULEr]   r   r(   r   r   r   r     r;   z(UnspecializedNNModuleSource.guard_sourceNr   r   r   r   r   r     r   r   c                   @   r   )"UnspecializedBuiltinNNModuleSourcec                 C   r   r    )-_GUARD_SOURCE_UNSPECIALIZED_BUILTIN_NN_MODULEr]   r   r(   r   r   r   r     r;   z/UnspecializedBuiltinNNModuleSource.guard_sourceNr   r   r   r   r   r     r   r   c                   @   r   )FSDPNNModuleSourcec                 C   r   r    )_GUARD_SOURCE_FSDP_MODULEr]   r   r(   r   r   r   r     r;   zFSDPNNModuleSource.guard_sourceNr   r   r   r   r   r     r   r   c                   @      e Zd Zdd Zdd ZdS )GlobalStateSourcec                 C   r   Nr   r   r(   r   r   r   r-   #  r   zGlobalStateSource.namec                 C   r&   r    rO   r(   r   r   r   r   &  r)   zGlobalStateSource.guard_sourceNr0   r1   r2   r-   r   r   r   r   r   r   !      r   c                   @   r   )TorchFunctionModeStackSourceindc                 C   s   d|    dS )Nz$___get_torch_function_mode_stack_at(r}   )
_get_indexr(   r   r   r   r-   .  rc   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                      r   )NZ get_torch_function_mode_stack_atr   r   r   r   r   rV   8  s    z:TorchFunctionModeStackSource.reconstruct.<locals>.<lambda>r	   F)rX   rY   rC   r   r   r#   r   r   r   r%   6  s
   
z(TorchFunctionModeStackSource.reconstructc                 C   r&   r    rO   r(   r   r   r   r   ?  r)   z)TorchFunctionModeStackSource.guard_sourceNr/   )	r0   r1   r2   rE   r4   r-   r   r%   r   r   r   r   r   r   *  s   
 
	r   c                   @   s8   e Zd ZU eed< dddZdd Zdd	 Zd
d ZdS )r   source_namer   r   c                 C   rH   )NFrJ   )r!   rM   r  r#   r   r   r   r%   G  rN   zConstantSource.reconstructc                 C   r&   r    )r   r   r(   r   r   r   r   J  r)   zConstantSource.guard_sourcec                 C   r   r    )r  r(   r   r   r   r-   M  r)   zConstantSource.namec                 C   r   r    r   r   r   r   r   r   P  r   zConstantSource.make_guardNr/   )	r0   r1   r2   r3   r4   r%   r   r-   r   r   r   r   r   r   C  s   
 
r   c                   @   s,   e Zd ZdefddZdd Zddd	Zd
S )NumpyTensorSourcereturnc                 C   r   )Nz___from_numpy(r}   rb   r(   r   r   r   r-   V  r.   zNumpyTensorSource.namec                 C   r^   r    r_   r(   r   r   r   r   Y  r`   zNumpyTensorSource.guard_sourcer   r   c                    r   )Nc                      r   )NZtorchZ	as_tensorr   r   r   r   r   rV   ]  r   z/NumpyTensorSource.reconstruct.<locals>.<lambda>r	   Fr   r#   r   r   r   r%   \  r   zNumpyTensorSource.reconstructNr/   )r0   r1   r2   r3   r-   r   r%   r   r   r   r   r  T  s    r  c                   @   "   e Zd ZdefddZdd ZdS )SubclassAttrListSourcer  c                 C   ra   )Nz.__tensor_flatten__()[0]rb   r(   r   r   r   r-   d  rc   zSubclassAttrListSource.namec                 C   r^   r    r_   r(   r   r   r   r   g  r`   z#SubclassAttrListSource.guard_sourceNr0   r1   r2   r3   r-   r   r   r   r   r   r  b      r  c                   @   r  )FloatTensorSourcer  c                 C   r   )Nz___as_tensor(r}   rb   r(   r   r   r   r-   o  r.   zFloatTensorSource.namec                 C   r^   r    r_   r(   r   r   r   r   r  r`   zFloatTensorSource.guard_sourceNr  r   r   r   r   r	  m  r  r	  c                   @   r  )CallMethodItemSourcer  c                 C   ra   )Nz.item()rb   r(   r   r   r   r-   x  rc   zCallMethodItemSource.namec                 C   r^   r    r_   r(   r   r   r   r   {  r`   z!CallMethodItemSource.guard_sourceNr  r   r   r   r   r
  v  r  r
  c                   @   r   )ShapeEnvSourcec                 C   r   r   r   r(   r   r   r   r-     r   zShapeEnvSource.namec                 C   r&   r    )r   Z	SHAPE_ENVr(   r   r   r   r     r)   zShapeEnvSource.guard_sourceNr   r   r   r   r   r    r   r  c                   @   r   )BackwardStateSourcec                 C   r   r   r   r(   r   r   r   r-     r   zBackwardStateSource.namec                 C   r&   r    )r   ZBACKWARD_STATEr(   r   r   r   r     r)   z BackwardStateSource.guard_sourceNr   r   r   r   r   r    r   r  Fonly_allow_inputr   r  c                C   s:   t | trt| j|dS t | tsd S |r| jsd S | jS Nr  )r   r   get_local_source_namer]   r   r   r   r   r  r   r   r   r    s   


r  c                C   s   t | |dd uS r  )r  r  r   r   r   is_from_local_source  rc   r  c                 C   s   t | d uS r    )get_global_source_namer   r   r   r   is_from_global_source  rD   r  c                 C   s(   t | tr
t| jS t | tsd S | jS r    )r   r   r  r]   rF   rG   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_sourcer]   r   r   r   r   r   r   r   r    s   


r  targetc                 C   s   t | trt| j|S | |kS r    )r   r   is_from_sourcer]   )r   r  r   r   r   r    s   
r  c                 C   &   t | trdS t | trt| jS dS r   )r   r   r   &is_from_unspecialized_nn_module_sourcer]   r   r   r   r   r    
   


r  c                 C   r  r   )r   r   r   .is_from_unspecialized_builtin_nn_module_sourcer]   r   r   r   r   r    r  r  c                 C   r  r   )r   r   r   )is_from_unspecialized_param_buffer_sourcer]   r   r   r   r   r    r  r  c                 C   r  r   )r   r   r   $is_from_flatten_script_object_sourcer]   r   r   r   r   r    r  r  c                 C   r  r   )r   r   r   is_from_optimizer_sourcer]   r   r   r   r   r    r  r  c                 C   sr   t | trdS t | trt | jtr| jjdkrdS t | tr-t | jtr-| jjdkr-dS t | tr7t| jS dS )NTr   r   F)	r   r   r   r]   rj   rk   r   r   is_from_defaultsr   r   r   r   r    s&   






r  )ar   r   enum	functoolstypingr   r   r   r   Ztorch._guardsr   r   r   r   r
   Zbytecode_transformationr   r   r   r   r'   ZLOCAL_SPECIALIZED_NN_MODULErP   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_MODULEr   r   r   r   r   	dataclassr   r8   r>   rF   rS   r[   rf   rj   r   r   r   r   r   r   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  r3   r  r  r5   r  r  r  r  	lru_cacher  r  r  r  r  r  r   r   r   r   <module>   sB  















(





$
&

$
*
(













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