o
    j9:j                     @   sP	  d Z ddlZddlZddlZddlm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 ddlmZ dd	lmZmZmZ erHd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ede(fddZ)dedB de*fddZ+ddede(de*fddZ,eddG dd deZ-eddG dd deZ.eddG dd deZ/eddG dd deZ0eddG d d! d!eZ1eddG d"d# d#eZ2eddG d$d% d%eZ3eddG d&d' d'e3Z4eddG d(d) d)eZ5eddG d*d+ d+e5Z6eddG d,d- d-eZ7eddG d.d/ d/eZ8eddG d0d1 d1eZ9eddG d2d3 d3eZ:eddG d4d5 d5eZ;eddG d6d7 d7eZ<eddG d8d9 d9eZ=eddG d:d; d;e5Z>eddG d<d= d=e5Z?eddG d>d? d?eZ@eddG d@dA dAeZAG dBdC dCejBZCeddG dDdE dEeZDeddG dFdG dGeZEeddG dHdI dIeZFeddG dJdK dKeZGeddG dLdM dMeZHeddG dNdO dOeZIeddG dPdQ dQeZJG dRdS dSeZKeddG dTdU dUeZLeddG dVdW dWeZMeddG dXdY dYeZNeddG dZd[ d[eZOeddG d\d] d]eZPeddG d^d_ d_eZQeddG d`da daeMZReddG dbdc dceMZSeddG ddde deeZTeddG dfdg dgeZUeddG dhdi dieZVeddG djdk dkeZWeddG dldm dmeZXeddG dndo doeXZYeddG dpdq dqeYZZeddG drds dseXZ[eddG dtdu dueZ\eddG dvdw dweZ]eddG dxdy dyeZ^eddG dzd{ d{eZ_eddG d|d} d}eZ`eddG d~d deZaeddG dd deZbeddG dd deZceddG dd deZdeddG dd deZeeddG dd deZfejgdddede(de*dB fddZhejgdddede(de(fddZiejgdede(fddZjejgdedB de*dB fddZkejgdede(fddZlejgdede(fddZmejgdedede(fddZnejgdede(fddZoejgdede(fddZpejgdede(fddZqejgdede(fddZrejgdede(fddZsejgdede(fddZtejgdede(fddZud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)Callable)AnyTYPE_CHECKING)device)ChainedSourcedataclass_with_cached_hashGuardGuardSourceSource   )utils)create_binary_subscrcreate_build_tuplecreate_call_function)	PyCodegensourcereturnc                 C   s<   t | trdS z| jtjkrW dS W dS  ty   Y dS w NTF)
isinstanceConstantSourceguard_sourcer	   CONSTANTNotImplementedErrorr    r   [/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/torch/_dynamo/source.pyis_constant_sourcem   s   
r   c                 C   s*   | d u rdS z| j W S  ty   Y dS w )Nz<unknown source>)namer   r   r   r   r   _get_source_debug_namey   s   r   Fs
apply_reprc                 C   s*   |rt | } nt| } | ddddS )a!  
    Escapes curly brackets for format strings.
    e.g. "frozenset({0})" becomes "frozenset({{0}})".
    This is used by _name_template for example, because it's
    expected to return a format string, but we may wish to include
    strings that should not be accidentally formatted.
    {z{{}z}})reprstrreplace)r   r    r   r   r   _esc_str   s   
r&   T)frozenc                   @   sr   e Zd ZU eed< dZeed< dZee dB ed< dZ	eed< dd
dZ
ed	efddZejd	efddZdS )LocalSource
local_nameFis_inputNdynamismis_derefed_cell_contentscodegenr   r   c                 C   s,   | j r|| j d S ||| j d S N)r,   
load_derefr)   append_outputcreate_loadselfr-   r   r   r   reconstruct   s   zLocalSource.reconstructc                 C      t jS r.   )r	   LOCALr3   r   r   r   r         zLocalSource.guard_sourcec                 C      dt | jdd dS )NzL[Tr    ]r&   r)   r7   r   r   r   _name_template      zLocalSource._name_templater-   r   r   N)__name__
__module____qualname__r$   __annotations__r*   boolr+   	frozensetr,   r4   propertyr	   r   	functoolscached_propertyr=   r   r   r   r   r(      s   
 
r(   c                   @   sD   e Zd ZU eed< dddZedefdd	Zedefd
dZ	dS )TempLocalSourcer)   r-   r   r   Nc                 C      | || j d S r.   r0   r1   r)   r2   r   r   r   r4         zTempLocalSource.reconstructc                 C   r5   r.   )r	   
TEMP_LOCALr7   r   r   r   r      r8   zTempLocalSource.guard_sourcec                 C   s   t d)NzhCannot create guard on TempLocalSource - this is an internal Dynamo bug. Please file an issue on GitHub.r   r7   r   r   r   r=      s   zTempLocalSource._name_templater?   )
r@   rA   rB   r$   rC   r4   rF   r	   r   r=   r   r   r   r   rI      s   
 
rI   c                   @   F   e Zd ZU eed< dddZedefdd	Ze	j
defd
dZdS )SyntheticLocalSourcer)   r-   r   r   Nc                 C   rJ   r.   rK   r2   r   r   r   r4      rL   z SyntheticLocalSource.reconstructc                 C   r5   r.   )r	   SYNTHETIC_LOCALr7   r   r   r   r      r8   z!SyntheticLocalSource.guard_sourcec                 C   r9   )NzSYNTHETIC_LOCAL[Tr:   r;   r<   r7   r   r   r   r=      r>   z#SyntheticLocalSource._name_templater?   r@   rA   rB   r$   rC   r4   rF   r	   r   rG   rH   r=   r   r   r   r   rP         
 
rP   c                   @   sF   e Zd ZU eed< edefddZddd	Ze	j
defd
dZdS )RandomValueSourcerandom_call_indexr   c                 C   r5   r.   )r	   RANDOM_VALUEr7   r   r   r   r      r8   zRandomValueSource.guard_sourcer-   r   Nc                 C   s8   | ||jjj | || j | t  d S r.   )r0   r1   txoutputrandom_values_varcreate_load_constrU   r   r2   r   r   r   r4      s   zRandomValueSource.reconstructc                 C      dt | j S )Nrandom_value_)r&   rU   r7   r   r   r   r=         z RandomValueSource._name_templater?   )r@   rA   rB   intrC   rF   r	   r   r4   rG   rH   r$   r=   r   r   r   r   rT      s   
 
rT   c                   @   rO   )GlobalSourceglobal_namer-   r   r   Nc                 C      | |j| jdd d S NTaddr0   create_load_globalr`   r2   r   r   r   r4         zGlobalSource.reconstructc                 C   r5   r.   r	   GLOBALr7   r   r   r   r      r8   zGlobalSource.guard_sourcec                 C   r9   )NG[Tr:   r;   r&   r`   r7   r   r   r   r=      r>   zGlobalSource._name_templater?   rR   r   r   r   r   r_      rS   r_   c                   @   rO   )GlobalWeakRefSourcer`   r-   r   r   Nc                    (      fdd  tdd d S )Nc                      s      jjddS rb   re   r   r-   r3   r   r   <lambda>   s    z1GlobalWeakRefSource.reconstruct.<locals>.<lambda>r   Fadd_push_nullextend_outputr   r2   r   rn   r   r4      s   zGlobalWeakRefSource.reconstructc                 C   r5   r.   rh   r7   r   r   r   r     r8   z GlobalWeakRefSource.guard_sourcec                 C   r9   )Nrj   Tr:   z]()rk   r7   r   r   r   r=     r>   z"GlobalWeakRefSource._name_templater?   rR   r   r   r   r   rl      s   
 
rl   c                   @   (   e Zd Zd	ddZedefddZdS )
WeakRefCallSourcer-   r   r   Nc                    rm   )Nc                      s
    j S r.   baser   rn   r   r   ro     s   
 z/WeakRefCallSource.reconstruct.<locals>.<lambda>r   Frp   r2   r   rn   r   r4     s   zWeakRefCallSource.reconstructc                 C      dS )Nz{0}()r   r7   r   r   r   r=        z WeakRefCallSource._name_templater?   r@   rA   rB   r4   rF   r$   r=   r   r   r   r   rt         
rt   c                   @      e Zd ZdS )CallFunctionNoArgsSourceNr@   rA   rB   r   r   r   r   r|         r|   c                   @   >   e Zd ZU eed< dddZddd	Zejdefd
dZ	dS )
AttrSourcememberr   Nc                 C   .   | j sJ dd| jvsJ d| jdd S )Nz9Can't construct an AttrSource without a valid base source.z,AttrSource member must not contain '.', got =. Use OutputGraph.get_chained_attr_source() for dotted paths.rv   r   r7   r   r   r   __post_init__  s   zAttrSource.__post_init__r-   r   c                 C       || j  ||| j d S r.   rv   rr   create_load_attrsr   r2   r   r   r   r4   &     
zAttrSource.reconstructc                 C   s0   | j  sdt| j dd dS dt| j  S )Nzgetattr({0}, Tr:   ){0}.)r   isidentifierr&   r7   r   r   r   r=   *  s   
zAttrSource._name_templater   Nr?   
r@   rA   rB   r$   rC   r   r4   rG   rH   r=   r   r   r   r   r     s   
 

r   c                   @   s0   e Zd ZU dZejddZeed< d	ddZ	dS )
CellContentsSourcez
    Source for closure cell contents that also stores the freevar name.
    This allows guard failure messages to show which variable the closure cell refers to.
     )defaultfreevar_namer   Nc                 C   s$   | j sJ d| jdksJ dd S )Nz@Can't construct a CellContentsSource without a valid base sourcecell_contentsz8CellContentsSource should only be used for cell_contentsr   r7   r   r   r   r   :  s   z CellContentsSource.__post_init__r   )
r@   rA   rB   __doc__dataclassesfieldr   r$   rC   r   r   r   r   r   r   1  s   
 r   c                   @   r   )GenericAttrSourcer   r   Nc                 C   r   )Nz?Can't construct a GenericAttrSource without a valid base sourcer   z3GenericAttrSource member must not contain '.', got r   r   r7   r   r   r   r   G  s   zGenericAttrSource.__post_init__r-   r   c                 C   r   r.   r   r2   r   r   r   r4   P  r   zGenericAttrSource.reconstructc                 C   r9   )Nzobject.__getattribute__({0}, Tr:   r   r&   r   r7   r   r   r   r=   T     z GenericAttrSource._name_templater   r?   r   r   r   r   r   r   C  s   
 

	r   c                   @   rs   )
TypeDictSourcer-   r   r   Nc                 C      || j  ||d d S )N__dict__rv   rr   r   r2   r   r   r   r4   ^     
zTypeDictSource.reconstructc                 C   rw   )Nzdict({0}.__dict__)r   r7   r   r   r   r=   b  s   zTypeDictSource._name_templater?   ry   r   r   r   r   r   \  rz   r   c                   @   rs   )
TypeMROSourcer-   r   r   Nc                 C   r   )N__mro__r   r2   r   r   r   r4   n  r   zTypeMROSource.reconstructc                 C   rw   )Nz{0}.__mro__r   r7   r   r   r   r=   r  rx   zTypeMROSource._name_templater?   ry   r   r   r   r   r   l  rz   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   r   Nc                 C   rJ   r.   )r0   create_load_closurer)   r2   r   r   r   r4     s   zLocalCellSource.reconstructr?   )r@   rA   rB   r   r$   rC   r4   r   r   r   r   r   w  s   
 r   c                   @   rs   )

CodeSourcer-   r   r   Nc                 C   r   )N__code__r   r2   r   r   r   r4     r   zCodeSource.reconstructc                 C   rw   )Nz{0}.__code__r   r7   r   r   r   r=     rx   zCodeSource._name_templater?   ry   r   r   r   r   r     rz   r   c                   @   rs   )
ClosureSourcer-   r   r   Nc                 C   r   )N__closure__r   r2   r   r   r   r4     r   zClosureSource.reconstructc                 C   rw   )Nz{0}.__closure__r   r7   r   r   r   r=     rx   zClosureSource._name_templater?   ry   r   r   r   r   r     rz   r   c                   @   s8   e Zd ZU dZeed< dddZejdefd	d
Z	dS )
GradSourcegradr   r-   r   r   Nc                 C   r   r.   r   r2   r   r   r   r4     r   zGradSource.reconstructc                 C   r[   )Nr   r   r7   r   r   r   r=     r]   zGradSource._name_templater?   )
r@   rA   rB   r   r$   rC   r4   rG   rH   r=   r   r   r   r   r     s
   
 
r   c                   @       e Zd ZejdefddZdS )ParamBufferSourcer   c                 C      t | jj S r.   #_GUARD_SOURCE_SPECIALIZED_NN_MODULErv   r   r7   r   r   r   r        zParamBufferSource.guard_sourceNr@   rA   rB   rG   rH   r	   r   r   r   r   r   r         r   c                   @   r{   )UnspecializedParamBufferSourceNr}   r   r   r   r   r     r~   r   c                   @   sl   e Zd ZU dZedB ed< edefddZe	j
defddZded	ef defd
dZdefddZdS )EphemeralSourceNdescr   c                 C   r5   r.   )r	   	EPHEMERALr7   r   r   r   r     r8   zEphemeralSource.guard_sourcec                 C   s(   | j d ur
d| j  nd}dt| dS )Nz: r   z
<ephemeral>)r   r&   )r3   r   r   r   r   r=     s   zEphemeralSource._name_templatefn.c                 C      t r.   rN   r3   r   r   r   r   
make_guard     zEphemeralSource.make_guardc                 C   rw   NTr   r7   r   r   r   is_ephemeral  r   zEphemeralSource.is_ephemeral)r@   rA   rB   r   r$   rC   rF   r	   r   rG   rH   r=   r   r   r   r   rD   r   r   r   r   r   r     s   
 r   c                   @   rs   )
SkipGuardSourcer-   r   r   Nc                 C   s   | j | d S r.   )rv   r4   r2   r   r   r   r4     s   zSkipGuardSource.reconstructc                 C   rw   Nz{0}r   r7   r   r   r   r=     rx   zSkipGuardSource._name_templater?   ry   r   r   r   r   r         
r   c                   @   s&   e Zd ZdZdZdZdefddZdS )TensorPropertyr   r      r   c                 C   s<   | t ju rdS | t ju rdS | t ju rdS tdt|  )Nsizestridestorage_offset
unhandled )r   SIZESTRIDESTORAGE_OFFSETAssertionErrorr&   r7   r   r   r   method_name  s   


zTensorProperty.method_nameN)r@   rA   rB   r   r   r   r$   r   r   r   r   r   r     s
    r   c                   @   sN   e Zd ZU eed< dZedB ed< dddZdd	d
Ze	j
defddZdS )TensorPropertySourcepropNidxr   c                 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.   )rv   r   r   r   r   r7   r   r   r   r     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tj  S )Ncall_)load_import_fromr   r@   r&   r   r   r   rn   r   r   ro     s    z2TensorPropertySource.reconstruct.<locals>.<lambda>r   r   F)rq   rv   r   r0   rZ   rr   r   r2   r   rn   r   r4     s   

z TensorPropertySource.reconstructc                 C   sn   | j tju rdt| j dS | j tju rdt| j dS | j tju r-| jd u s+J dS tdt| j  )Nz{0}.size()[r;   z{0}.stride()[z{0}.storage_offset()r   )r   r   r   r&   r   r   r   r   r7   r   r   r   r=     s   z#TensorPropertySource._name_templater   r?   )r@   rA   rB   r   rC   r   r^   r   r4   rG   rH   r$   r=   r   r   r   r   r     s   
 

r   c                   @   >   e Zd ZU eed< dddZddd	Zejde	fd
dZ
dS )IndexedSourcer   r   Nc                 C      | j d usJ d S r.   ru   r7   r   r   r   r   "     zIndexedSource.__post_init__r-   r   c                 C   r   r.   rN   r2   r   r   r   r4   %  r   zIndexedSource.reconstructc                 C   s   dt | j dS )N(z, {0}))r&   r   r7   r   r   r   r=   (  s   zIndexedSource._name_templater   r?   )r@   rA   rB   r^   rC   r   r4   rG   rH   r$   r=   r   r   r   r   r     s   
 

r   c                   @   2   e Zd ZdddZdddZedefd	d
ZdS )NegateSourcer   Nc                 C   r   r.   ru   r7   r   r   r   r   /  r   zNegateSource.__post_init__r-   r   c                 C   r   r.   rN   r2   r   r   r   r4   2  r   zNegateSource.reconstructc                 C   rw   )Nz{0}.__neg__()r   r7   r   r   r   r=   5  s   zNegateSource._name_templater   r?   r@   rA   rB   r   r4   rF   r$   r=   r   r   r   r   r   -  
    

r   c                   @   r   )ConvertIntSourcer   Nc                 C   r   r.   ru   r7   r   r   r   r   =  r   zConvertIntSource.__post_init__r-   r   c                 C      || j  d S r.   ru   r2   r   r   r   r4   @     zConvertIntSource.reconstructc                 C   rw   )Nz%cast_symbool_to_symint_guardless({0})r   r7   r   r   r   r=   C  rx   zConvertIntSource._name_templater   r?   r   r   r   r   r   r   ;  r   r   c                   @   s<   e Zd ZU eed< dddZddd	Zedefd
dZ	dS )DynamicScalarSourceis_intr   Nc                 C   r   r.   ru   r7   r   r   r   r   L  r   z!DynamicScalarSource.__post_init__r-   r   c                    0      fdd  | j  tdd d S )Nc                           ddS )Nbuiltinsr^   r   r   r-   r   r   ro   T      z1DynamicScalarSource.reconstruct.<locals>.<lambda>r   Frq   rv   rr   r   r2   r   r   r   r4   O  s   
zDynamicScalarSource.reconstructc                 C   rw   )Nzint({0})r   r7   r   r   r   r=   X  rx   z"DynamicScalarSource._name_templater   r?   )
r@   rA   rB   rD   rC   r   r4   rF   r$   r=   r   r   r   r   r   H  s   
 

	r   c                   @   r   )FlattenScriptObjectSourcer   Nc                 C   r   r.   ru   r7   r   r   r   r   _  r   z'FlattenScriptObjectSource.__post_init__r-   r   c                 C   r   r.   ru   r2   r   r   r   r4   b  r   z%FlattenScriptObjectSource.reconstructc                 C   rw   )Nz{0}.__obj_flatten__()r   r7   r   r   r   r=   e  rx   z(FlattenScriptObjectSource._name_templater   r?   r   r   r   r   r   r   ]  r   r   c                   @   r   )ScriptObjectQualifiedNameSourcer   Nc                 C   r   r.   ru   r7   r   r   r   r   l  r   z-ScriptObjectQualifiedNameSource.__post_init__r-   r   c                 C   r   r.   ru   r2   r   r   r   r4   o  r   z+ScriptObjectQualifiedNameSource.reconstructc                 C   rw   )Nz{0}._type().qualified_name()r   r7   r   r   r   r=   r  rx   z.ScriptObjectQualifiedNameSource._name_templater   r?   r   r   r   r   r   r   j  r   r   c                   @   rs   )
AttrProxySourcer-   r   r   Nc                 C   r   r.   ru   r2   r   r   r   r4   x  r   zAttrProxySource.reconstructc                 C   rw   )Nz{0}.get_base()r   r7   r   r   r   r=   {  rx   zAttrProxySource._name_templater?   ry   r   r   r   r   r   w  s    
r   c                   @   s~   e Zd ZU eeB 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	d
ZdddZejdefddZdS )DefaultsSourceidx_keyFis_kw)initr#   comparer   _namer   Nc              	   C   s   | j sJ d| jr/t| jtsJ t| dd t| ddt| j dt| j d d S t| jt	s7J t| dd t| ddt| j d	t| 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   r   z['z']__defaults__[r;   )
rv   r   r   r   r$   object__setattr__r&   r   r^   r7   r   r   r   r     s     zDefaultsSource.__post_init__r-   r   c                 C   s>   || j  ||| j ||| j |t  d S r.   )rv   rr   r   r   r0   rZ   r   r   r2   r   r   r   r4     s   
zDefaultsSource.reconstructc                 C      | j S r.   )r   r7   r   r   r   r=     r8   zDefaultsSource._name_templater   r?   )r@   rA   rB   r^   r$   rC   r   rD   r   r   r   r   r4   rG   rH   r=   r   r   r   r   r     s   
 

r   c                       s`   e Zd ZU eed< dZeed< d fddZdd
dZde	fddZ
ejdefddZ  ZS )GetItemSourceindexFindex_is_slicer   Nc                    sD   | j d usJ t| jtr t d| j  t dd d S d S )Nr   r   T)rv   r   r   slicesuperr   
__reduce__r7   	__class__r   r   r     s
   zGetItemSource.__post_init__r-   r   c                 C   sH   || j  | jr|||   n	||| j |t  d S r.   )rv   r   r0   rZ   unpack_slicer   r   r2   r   r   r   r4     s
   
zGetItemSource.reconstructc                 C   s   | j sJ | j\}}|| S r.   )r   r   )r3   slice_class
slice_argsr   r   r   r     s   

zGetItemSource.unpack_slicec                 C   sD   t | jtrJ | jrdt|  dd dS dt| jdd dS )N{0}[Tr:   r;   )r   r   r
   r   r&   r   r7   r   r   r   r=     s   zGetItemSource._name_templater   r?   )r@   rA   rB   r   rC   r   rD   r   r4   r   r   rG   rH   r$   r=   __classcell__r   r   r   r   r     s   
 
r   c                   @   sB   e Zd ZU eed< dddZejdefdd	Z	de
fd
dZdS )ConstDictKeySourcer   r-   r   r   Nc                    B      fdd  | j   | j  tdd d S )Nc                           tjdS )Ndict_keys_getitemr   r   r@   r   r   r   r   ro         z0ConstDictKeySource.reconstruct.<locals>.<lambda>r   Frq   rv   r0   rZ   r   rr   r   r2   r   r   r   r4        

zConstDictKeySource.reconstructc                 C   r9   )Nzlist(dict.keys({0}))[Tr:   r;   r&   r   r7   r   r   r   r=        z!ConstDictKeySource._name_templatec                 C   rw   r   r   r7   r   r   r   is_dict_key  r   zConstDictKeySource.is_dict_keyr?   )r@   rA   rB   r   rC   r4   rG   rH   r$   r=   rD   r  r   r   r   r   r    s   
 
r  c                   @   sL   e Zd ZU eed< dddZddd	Zejde	fd
dZ
defddZdS )NonSerializableSetGetItemSourcer   r   Nc                 C   s    ddl m} || jsJ d S Nr   )ConstantVariable)	variablesr  
is_literalr   r3   r  r   r   r   r     s   z-NonSerializableSetGetItemSource.__post_init__r-   r   c                    r  )Nc                      r  )Nset_getitemr  r   r   r   r   ro     r  z=NonSerializableSetGetItemSource.reconstruct.<locals>.<lambda>r   Fr  r2   r   r   r   r4     r	  z+NonSerializableSetGetItemSource.reconstructc                 C   r9   )Nz
list({0})[Tr:   r;   r
  r7   r   r   r   r=     r  z.NonSerializableSetGetItemSource._name_templatec                 C   rw   )NFr   r7   r   r   r   r    r   z+NonSerializableSetGetItemSource.is_dict_keyr   r?   )r@   rA   rB   r^   rC   r   r4   rG   rH   r$   r=   rD   r  r   r   r   r   r    s   
 

r  c                   @   r   )DictGetItemSourcer   r   Nc                 C   0   ddl m} t| jts|| jsJ d S d S r  r  r  r   r   r  r  r  r   r   r   r        
zDictGetItemSource.__post_init__r-   r   c                 C   sD   || j  t| jtr|| j n	||| j |t  d S r.   )rv   r   r   r
   r0   rZ   r   r2   r   r   r   r4     s
   
zDictGetItemSource.reconstructc                 C   s6   t | jtrdt| jj dS dt| jdd dS )Nr   r;   Tr:   r   r   r  r&   r   r7   r   r   r   r=        z DictGetItemSource._name_templater   r?   r@   rA   rB   r   rC   r   r4   rG   rH   r$   r=   r   r   r   r   r    s   
 

r  c                   @   r   )DictSubclassGetItemSourcer   r   Nc                 C   r  r  r  r  r   r   r   r   $  r  z'DictSubclassGetItemSource.__post_init__r-   r   c                    sZ      fdd  | j t| jtr | j n	  | j  tdd d S )Nc                      r  )Ndict_getitemr  r   r   r   r   ro   0  r  z7DictSubclassGetItemSource.reconstruct.<locals>.<lambda>r   F)	rq   rv   r   r   r
   r0   rZ   rr   r   r2   r   r   r   r4   +  s   

z%DictSubclassGetItemSource.reconstructc                 C   s6   t | jtrdt| jj dS dt| jdd dS )Nzdict.__getitem__({0}, r   r   Tr:   r;   r  r7   r   r   r   r=   >  r  z(DictSubclassGetItemSource._name_templater   r?   r  r   r   r   r   r    s   
 

r  c                   @   s.   e Zd ZdZd
ddZejdefdd	ZdS )ListGetItemSourcezY
    Same as GetItemSource with reconstruct and name overridden to be list specific.
    r-   r   r   Nc                    sP      fdd  | j | jrtd  | j  tdd d S )Nc                      r  )Nlist_getitemr  r   r   r   r   ro   R  r  z/ListGetItemSource.reconstruct.<locals>.<lambda>>List[slice] is a temporary object and should not have a sourcer   F)	rq   rv   r   RuntimeErrorr0   rZ   r   rr   r   r2   r   r   r   r4   L  s   

zListGetItemSource.reconstructc                 C   s4   t | jtrJ | jrtddt| jdd dS )Nr  zlist.__getitem__({0}, Tr:   r   )r   r   r
   r   r   r&   r7   r   r   r   r=   b  s   z ListGetItemSource._name_templater?   )	r@   rA   rB   r   r4   rG   rH   r$   r=   r   r   r   r   r  F  s
    
r  c                   @   s*   e Zd Zd	ddZejdefddZdS )
TupleIteratorGetItemSourcer-   r   r   Nc                    r  )Nc                      r  )Ntuple_iterator_getitemr  r   r   r   r   ro   t  r  z8TupleIteratorGetItemSource.reconstruct.<locals>.<lambda>r   Fr  r2   r   r   r   r4   r  r	  z&TupleIteratorGetItemSource.reconstructc                 C   r9   )Nz___tuple_iterator_getitem({0}, Tr:   r   r
  r7   r   r   r   r=   z  r   z)TupleIteratorGetItemSource._name_templater?   )r@   rA   rB   r4   rG   rH   r$   r=   r   r   r   r   r!  p  s    
r!  c                   @   rs   )
NamedTupleFieldsSourcer-   r   r   Nc                 C   r   )N_fieldsr   r2   r   r   r   r4     r   z"NamedTupleFieldsSource.reconstructc                 C   rw   )Nz___namedtuple_fields({0})r   r7   r   r   r   r=     rx   z%NamedTupleFieldsSource._name_templater?   ry   r   r   r   r   r#    rz   r#  c                   @   rs   )
DataclassFieldsSourcer-   r   r   Nc                    r   )Nc                      r  )Ndataclass_fieldsr  r   r   r   r   ro     r  z3DataclassFieldsSource.reconstruct.<locals>.<lambda>r   Fr   r2   r   r   r   r4     s
   

z!DataclassFieldsSource.reconstructc                 C   rw   )Nz___dataclass_fields({0})r   r7   r   r   r   r=     rx   z$DataclassFieldsSource._name_templater?   ry   r   r   r   r   r%    s    
r%  c                   @   r   )
TypeSourcer   Nc                 C   r   r.   ru   r7   r   r   r   r     r   zTypeSource.__post_init__r-   r   c                    r   )Nc                      r   )Nr   typer   r   r   r   r   ro     r   z(TypeSource.reconstruct.<locals>.<lambda>r   Fr   r2   r   r   r   r4        
zTypeSource.reconstructc                 C   rw   )Nz	type({0})r   r7   r   r   r   r=     rx   zTypeSource._name_templater   r?   r   r   r   r   r   r'    s
    

r'  c                   @   rs   )
OptimizerSourcer-   r   r   Nc                 C   r   r.   ru   r2   r   r   r   r4     r   zOptimizerSource.reconstructc                 C   rw   r   r   r7   r   r   r   r=     rx   zOptimizerSource._name_templater?   ry   r   r   r   r   r*    r   r*  c                   @   s<   e Zd ZdddZejdefddZede	fd	d
Z
dS )NNModuleSourcer-   r   r   Nc                 C   r   r.   ru   r2   r   r   r   r4     r   zNNModuleSource.reconstructc                 C   r   r.   r   r7   r   r   r   r     r   zNNModuleSource.guard_sourcec                 C   rw   r   r   r7   r   r   r   r=     rx   zNNModuleSource._name_templater?   )r@   rA   rB   r4   rG   rH   r	   r   rF   r$   r=   r   r   r   r   r+    s    
r+  c                   @   r   )UnspecializedNNModuleSourcer   c                 C   r   r.   )%_GUARD_SOURCE_UNSPECIALIZED_NN_MODULErv   r   r7   r   r   r   r     r   z(UnspecializedNNModuleSource.guard_sourceNr   r   r   r   r   r,    r   r,  c                   @   r   )"UnspecializedBuiltinNNModuleSourcer   c                 C   r   r.   )-_GUARD_SOURCE_UNSPECIALIZED_BUILTIN_NN_MODULErv   r   r7   r   r   r   r     r   z/UnspecializedBuiltinNNModuleSource.guard_sourceNr   r   r   r   r   r.    r   r.  c                   @   r   )FSDPNNModuleSourcer   c                 C   r   r.   )_GUARD_SOURCE_FSDP_MODULErv   r   r7   r   r   r   r     r   zFSDPNNModuleSource.guard_sourceNr   r   r   r   r   r0    r   r0  c                   @   0   e Zd ZedefddZedefddZdS )GlobalStateSourcer   c                 C   rw   Nr   r   r7   r   r   r   r=     rx   z GlobalStateSource._name_templatec                 C   r5   r.   rh   r7   r   r   r   r     r8   zGlobalStateSource.guard_sourceNr@   rA   rB   rF   r$   r=   r	   r   r   r   r   r   r3    
    r3  c                   @   sT   e Zd ZU dZeed< dddZejdefddZ	dddZ
edefddZdS )ImportSourcezPoints to an imported module - used instead of GlobalSource
    in case the user has overridden the module name in their local namespacemodule_namer   Nc                 C   s$   ddl m}m} || |j d S )Nr   )GuardBuilderinstall_guard)guardsr9  r:  r   ID_MATCH)r3   r9  r:  r   r   r   r     s   zImportSource.__post_init__c                 C   s   d| j  dS )Nz__import__('z'))r8  r7   r   r   r   r=     s   zImportSource._name_templater-   r   c                 C   s&   | |dtd|| jg d S )Nr   )rr   rZ   r   create_import_namer8  r2   r   r   r   r4     s   
zImportSource.reconstructc                 C   r5   r.   rh   r7   r   r   r   r     r8   zImportSource.guard_sourcer   r?   )r@   rA   rB   r   r$   rC   r   rG   rH   r=   r4   rF   r	   r   r   r   r   r   r7    s   
 

	r7  c                   @   sT   e Zd ZU eed< ejdefddZdefddZ	dd
dZ
edefddZd	S )TorchFunctionModeStackSourceindr   c                 C   s   dt |   dS )Nz$___get_torch_function_mode_stack_at(r   )r&   
_get_indexr7   r   r   r   r=     s   z+TorchFunctionModeStackSource._name_templatec                 C   s   ddl m} || jS )Nr   )TorchFunctionModeStackVariable)variables.torch_functionrA  get_mode_indexr?  )r3   rA  r   r   r   r@    s   z'TorchFunctionModeStackSource._get_indexr-   r   Nc                    s<      fdd   |  g  tdd d S )Nc                      r  )N get_torch_function_mode_stack_atr  r   r   r   r   ro     s    z:TorchFunctionModeStackSource.reconstruct.<locals>.<lambda>r   F)rq   rr   rZ   r@  r   r2   r   r   r   r4     s
   
z(TorchFunctionModeStackSource.reconstructc                 C   r5   r.   rh   r7   r   r   r   r     r8   z)TorchFunctionModeStackSource.guard_sourcer?   )r@   rA   rB   r^   rC   rG   rH   r$   r=   r@  r4   rF   r	   r   r   r   r   r   r>    s   
 
	r>  c                   @   sX   e Zd ZU eed< dddZedefdd	Ze	j
defd
dZdedefddZdS )r   source_namer-   r   r   Nc                 C   ra   )NFrc   )r0   rf   rE  r2   r   r   r   r4     rg   zConstantSource.reconstructc                 C   r5   r.   )r	   r   r7   r   r   r   r   !  r8   zConstantSource.guard_sourcec                 C   r   r.   )rE  r7   r   r   r   r=   %  r8   zConstantSource._name_templater   c                 C   r   r.   rN   r   r   r   r   r   )  r   zConstantSource.make_guardr?   )r@   rA   rB   r$   rC   r4   rF   r	   r   rG   rH   r=   r   r   r   r   r   r   r     s   
 
r   c                   @   s(   e Zd ZedefddZd	ddZdS )
NumpyTensorSourcer   c                 C   rw   )Nz___from_numpy({0})r   r7   r   r   r   r=   /  rx   z NumpyTensorSource._name_templater-   r   Nc                    r   )Nc                      r   )Ntorch	as_tensorr   r   r   r   r   ro   4  r   z/NumpyTensorSource.reconstruct.<locals>.<lambda>r   Fr   r2   r   r   r   r4   3  r)  zNumpyTensorSource.reconstructr?   )r@   rA   rB   rF   r$   r=   r4   r   r   r   r   rF  -  s    rF  c                   @      e Zd ZedefddZdS )SubclassAttrListSourcer   c                 C   rw   )Nz{0}.__tensor_flatten__()[0]r   r7   r   r   r   r=   ;  rx   z%SubclassAttrListSource._name_templateNr@   rA   rB   rF   r$   r=   r   r   r   r   rJ  9      rJ  c                   @   rI  )FloatTensorSourcer   c                 C   rw   )Nz___as_tensor({0})r   r7   r   r   r   r=   D  rx   z FloatTensorSource._name_templateNrK  r   r   r   r   rM  B  rL  rM  c                   @   rI  )CallMethodItemSourcer   c                 C   rw   )Nz
{0}.item()r   r7   r   r   r   r=   K  rx   z#CallMethodItemSource._name_templateNrK  r   r   r   r   rN  I  rL  rN  c                   @   r2  )ShapeEnvSourcer   c                 C   rw   r4  r   r7   r   r   r   r=   U  rx   zShapeEnvSource._name_templatec                 C   r5   r.   )r	   	SHAPE_ENVr7   r   r   r   r   Y  r8   zShapeEnvSource.guard_sourceNr5  r   r   r   r   rO  S  r6  rO  c                   @   sF   e Zd ZU eed< ejdefddZddd	Z	e
defd
dZdS )CurrentStreamSourcer   r   c                 C   s"   dt | jj dt | jj dS )Nz$___get_current_stream(torch.device('z', z)))r&   r   r(  r   r7   r   r   r   r=   b  s   "z"CurrentStreamSource._name_templater-   r   Nc                    s   d}   fdd    fdd   | jjg | jjd ur4|d7 }  | jjg  t|d  tdd d S )Nr   c                      r  )Nget_current_streamr  r   r   r   r   ro   i  r  z1CurrentStreamSource.reconstruct.<locals>.<lambda>c                      r   )NrG  r   r   r   r   r   r   ro   k  r   F)rq   rr   rZ   r   r(  r   r   )r3   r-   num_argsr   r   r   r4   f  s   
zCurrentStreamSource.reconstructc                 C   r5   r.   rh   r7   r   r   r   r   s  r8   z CurrentStreamSource.guard_sourcer?   )r@   rA   rB   device_typerC   rG   rH   r$   r=   r4   rF   r	   r   r   r   r   r   rQ  ^  s   
 
rQ  c                   @   r2  )BackwardStateSourcer   c                 C   rw   r4  r   r7   r   r   r   r=   z  rx   z"BackwardStateSource._name_templatec                 C   r5   r.   )r	   BACKWARD_STATEr7   r   r   r   r   ~  r8   z BackwardStateSource.guard_sourceNr5  r   r   r   r   rU  x  r6  rU  only_allow_inputrX  c                C   s:   t | trt| j|dS t | tsd S |r| jsd S | jS NrW  )r   r   get_local_source_namerv   r(   r*   r)   r   rX  r   r   r   rZ    s   


rZ  c                C   s   t | |dd uS rY  )rZ  r[  r   r   r   is_from_local_source  r]   r\  c                 C   s   t | d uS r.   )get_global_source_namer   r   r   r   is_from_global_source  r   r^  c                 C   s(   t | tr
t| jS t | tsd S | jS r.   )r   r   r]  rv   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_sourcerv   r(   r,   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_closure_sourcerv   r   r   r   r   ra    
   


ra  targetc                 C   s&   | |krdS t | trt| j|S dS r   )r   r   is_from_sourcerv   )r   rc  r   r   r   rd    s
   
rd  c                 C   r`  r   )r   r,  r   &is_from_unspecialized_nn_module_sourcerv   r   r   r   r   re    rb  re  c                 C   r`  r   )r   r.  r   .is_from_unspecialized_builtin_nn_module_sourcerv   r   r   r   r   rf    rb  rf  c                 C   r`  r   )r   r   r   )is_from_unspecialized_param_buffer_sourcerv   r   r   r   r   rg    rb  rg  c                 C   r`  r   )r   r   r   $is_from_flatten_script_object_sourcerv   r   r   r   r   rh    rb  rh  c                 C   r`  r   )r   r*  r   is_from_optimizer_sourcerv   r   r   r   r   ri    rb  ri  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  rv   r   r   r   r   is_from_defaultsr   r   r   r   rj    s"   




rj  c                 C   r`  r   )r   r   r   is_from_skip_guard_sourcerv   r   r   r   r   rk  	  s
   


rk  )F)vr   r   enumrG   collections.abcr   typingr   r   rG  r   rT  torch._guardsr   r   r   r	   r
   r   r   bytecode_transformationr   r   r   r-   r   r6   LOCAL_SPECIALIZED_NN_MODULEri   GLOBAL_SPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULEGLOBAL_UNSPECIALIZED_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULELOCAL_FSDP_MODULEGLOBAL_FSDP_MODULEr   r-  r/  r1  rD   r   r$   r   r&   r(   rI   rP   rT   r_   rl   rt   r|   r   r   r   r   r   r   r   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.  r0  r3  r7  r>  r   rF  rJ  rM  rN  rO  rQ  rU  	lru_cacherZ  r\  r^  r]  r_  ra  rd  re  rf  rg  rh  ri  rj  rk  r   r   r   r   <module>   s   


	&	$$"()
	
	



