
    rh                     0   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j0                  ej0                  ej2                  ej2                  ej4                  ej4                  ej6                  ej6                  ej8                  ej8                  ej:                  ej:                  i
Zej(                  ej0                  ej,                  ej2                  ej0                  ej0                  ej2                  ej2                  ej*                  ej0                  ej.                  ej2                  ej4                  ej4                  ej6                  ej6                  ej8                  ej8                  ej:                  ej:                  i
Zej(                  ej4                  ej,                  ej6                  ej0                  ej4                  ej2                  ej6                  ej*                  ej4                  ej.                  ej6                  ej4                  ej4                  ej6                  ej6                  ej8                  ej8                  ej:                  ej:                  i
Z ej(                  ej8                  ej,                  ej:                  ej*                  ej8                  ej.                  ej:                  ej8                  ej8                  ej:                  ej:                  ej0                  ej8                  ej2                  ej:                  ej4                  ej8                  ej6                  ej:                  i
Z!d	 Z" ejF                  d
       G d de             Z$ ejF                  d
       G d de             Z% ejF                  d
       G d de             Z& ejF                  d
       G d de             Z' ejF                  d
       G d de             Z( ejF                  d
       G d de
             Z) ejF                  d
       G d de)             Z* ejF                  d
       G d de
             Z+ ejF                  d
       G d de
             Z, ejF                  d
       G d de             Z- ejF                  d
       G d  d!e
             Z. ejF                  d
       G d" d#e+             Z/ ejF                  d
       G d$ d%e+             Z0 ejF                  d
       G d& d'e             Z1 G d( d)ejd                        Z3 ejF                  d
       G d* d+e
             Z4 ejF                  d
       G d, d-e
             Z5 ejF                  d
       G d. d/e
             Z6 ejF                  d
       G d0 d1e
             Z7 ejF                  d
       G d2 d3e
             Z8 ejF                  d
       G d4 d5e
             Z9 G d6 d7e
      Z: ejF                  d
       G d8 d9e
             Z; ejF                  d
       G d: d;e
             Z< ejF                  d
       G d< d=e
             Z= ejF                  d
       G d> d?e
             Z> ejF                  d
       G d@ dAe
             Z? ejF                  d
       G dB dCe<             Z@ ejF                  d
       G dD dEe<             ZA ejF                  d
       G dF dGe
             ZB ejF                  d
       G dH dIe
             ZC ejF                  d
       G dJ dKe
             ZD ejF                  d
       G dL dMe
             ZE ejF                  d
       G dN dOeE             ZF ejF                  d
       G dP dQeF             ZG ejF                  d
       G dR dSeE             ZH ejF                  d
       G dT dUe             ZI ejF                  d
       G dV dWe             ZJ ejF                  d
       G dX dYe             ZK ejF                  d
       G dZ d[e
             ZL ejF                  d
       G d\ d]e
             ZM ejF                  d
       G d^ d_e
             ZN ejF                  d
       G d` dae
             ZO ejF                  d
       G db dce             ZP ejF                  d
       G dd dee             ZQdfdgdhedieeR   fdjZSdfdgdhefdkZTdhedieUfdlZVdhedieeR   fdmZWdhefdnZXdhedoefdpZYej                  dhefdq       Z[ej                  dhefdr       Z\ej                  dhefds       Z]ej                  dhefdt       Z^ej                  dhefdu       Z_ej                  dhefdv       Z`y)wa  
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                     t        | t              ry	 | j                         t        j                  k(  ry	 y# t
        $ r Y yw xY wNTF)
isinstanceConstantSourceguard_sourcer   CONSTANTNotImplementedErrorsources    g/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/source.pyis_constant_sourcer   c   sO    &.) K$8$88 9
   s   !7 	AAT)frozenc                   b    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Zd Zd	 Zy)LocalSource
local_nameFis_inputNdynamismis_derefed_cell_contentsc                     | j                   r|j                  | j                         y |j                  |j	                  | j                               y N)r    
load_derefr   append_outputcreate_loadselfcodegens     r   reconstructzLocalSource.reconstruct   s:    ((t/!!'"5"5doo"FG    c                 "    t         j                  S r"   )r   LOCALr'   s    r   r   zLocalSource.guard_source   s       r*   c                 4    dt        | j                         dS )NzL[])reprr   r-   s    r   namezLocalSource.name   s    D)*!,,r*   r(   r   )__name__
__module____qualname__str__annotations__r   boolr   r   	frozensetr    r)   r   r1    r*   r   r   r   o   sE    O Hd *.Hhy~&-
 &+d*H!-r*   r   c                   ,    e Zd ZU eed<   ddZd Zd Zy)SyntheticLocalSourcer   c                 X    |j                  |j                  | j                               y r"   )r$   r%   r   r&   s     r   r)   z SyntheticLocalSource.reconstruct   s    g11$//BCr*   c                 "    t         j                  S r"   )r   SYNTHETIC_LOCALr-   s    r   r   z!SyntheticLocalSource.guard_source   s    ***r*   c                 "    d| j                   dS )NzSYNTHETIC_LOCAL[r/   )r   r-   s    r   r1   zSyntheticLocalSource.name   s    !$//!4A66r*   Nr2   r3   r4   r5   r6   r7   r)   r   r1   r:   r*   r   r<   r<      s    OD+7r*   r<   c                   ,    e Zd ZU eed<   d ZddZd Zy)RandomValueSourcerandom_call_indexc                 "    t         j                  S r"   )r   RANDOM_VALUEr-   s    r   r   zRandomValueSource.guard_source   s    '''r*   c                    |j                  |j                  |j                  j                  j                               |j                  |j                  | j                               |j                  t        d             y NBINARY_SUBSCR)r$   r%   txoutputrandom_values_varcreate_load_constrD   r   r&   s     r   r)   zRandomValueSource.reconstruct   s^    g11'**2C2C2U2UVWg778N8NOP0ABr*   c                      d| j                    S )Nrandom_value_)rD   r-   s    r   r1   zRandomValueSource.name   s    t55677r*   Nr2   )r3   r4   r5   intr7   r   r)   r1   r:   r*   r   rC   rC      s    (C
8r*   rC   c                   ,    e Zd ZU eed<   ddZd Zd Zy)GlobalSourceglobal_namec                 \    |j                  |j                  | j                  d             y NTaddr$   create_load_globalrS   r&   s     r   r)   zGlobalSource.reconstruct   s&    g889I9It8TUr*   c                 "    t         j                  S r"   r   GLOBALr-   s    r   r   zGlobalSource.guard_source       !!!r*   c                 4    dt        | j                         dS )NG[r/   r0   rS   r-   s    r   r1   zGlobalSource.name   s    D))*+1--r*   Nr2   rA   r:   r*   r   rR   rR      s    V".r*   rR   c                   ,    e Zd ZU eed<   ddZd Zd Zy)GlobalWeakRefSourcerS   c                 h     j                   fd       j                  t        dd             y )Nc                  \     j                   j                  j                  d            S rU   rX   r(   r'   s   r   <lambda>z1GlobalWeakRefSource.reconstruct.<locals>.<lambda>   s+    G))**4+;+;*F r*   r   Fadd_push_nullextend_outputr   r&   s   ``r   r)   zGlobalWeakRefSource.reconstruct   s.    	

 	21e<=r*   c                 "    t         j                  S r"   r[   r-   s    r   r   z GlobalWeakRefSource.guard_source   r]   r*   c                 4    dt        | j                         dS )Nr_   z]()r`   r-   s    r   r1   zGlobalWeakRefSource.name   s    D))*+3//r*   Nr2   rA   r:   r*   r   rb   rb      s    >"0r*   rb   c                        e Zd ZddZd Zd Zy)WeakRefCallSourcec                 h     j                   fd       j                  t        dd             y )Nc                  (      j                         S r"   basere   s   r   rf   z/WeakRefCallSource.reconstruct.<locals>.<lambda>   s    gdii&8 r*   r   Frg   r&   s   ``r   r)   zWeakRefCallSource.reconstruct   s(    8921e<=r*   c                 6    | j                   j                         S r"   rq   r   r-   s    r   r   zWeakRefCallSource.guard_source       yy%%''r*   c                 <    | j                   j                          dS )Nz()rq   r1   r-   s    r   r1   zWeakRefCallSource.name   s    )).."#2&&r*   Nr2   r3   r4   r5   r)   r   r1   r:   r*   r   rm   rm      s    >('r*   rm   c                       e Zd Zy)CallFunctionNoArgsSourceNr3   r4   r5   r:   r*   r   ry   ry          r*   ry   c                   2    e Zd ZU eed<   d ZddZd Zd Zy)
AttrSourcememberc                 ,   | j                   sJ d       d| j                  v rs| j                  j                  d      }t        j	                  | dt        | j                   dj                  |d d                    t        j	                  | d|d          y y Nz9Can't construct an AttrSource without a valid base source.rq   r~   rq   r~   splitobject__setattr__r}   joinr'   member_partss     r   __post_init__zAttrSource.__post_init__       yyUUUy$++;;,,S1LfjCHH\#2=N4OP tX|B/?@ r*   c                 |     || j                          |j                  |j                  | j                               y r"   rq   ri   create_load_attrsr~   r&   s     r   r)   zAttrSource.reconstruct   +    		g77DEr*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zAttrSource.guard_source   rt   r*   c                     | j                   j                         s+d| j                  j                          d| j                   dS | j                  j                          d| j                    S )Nzgetattr(, )r   )r~   isidentifierrq   r1   r-   s    r   r1   zAttrSource.name   sX    {{'')diinn./r$++BB)).."#1T[[M22r*   Nr2   	r3   r4   r5   r6   r7   r   r)   r   r1   r:   r*   r   r}   r}      s    KAF(3r*   r}   c                   2    e Zd ZU eed<   d ZddZd Zd Zy)GenericAttrSourcer~   c                 ,   | j                   sJ d       d| j                  v rs| j                  j                  d      }t        j	                  | dt        | j                   dj                  |d d                    t        j	                  | d|d          y y r   r   r   s     r   r   zGenericAttrSource.__post_init__   r   r*   c                 |     || j                          |j                  |j                  | j                               y r"   r   r&   s     r   r)   zGenericAttrSource.reconstruct  r   r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zGenericAttrSource.guard_source  rt   r*   c                 X    d| j                   j                          d| j                  dS )Nzobject.__getattribute__(r   r   rq   r1   r~   r-   s    r   r1   zGenericAttrSource.name  s'    )$))..*:);2dkk_ANNr*   Nr2   r   r:   r*   r   r   r      s    KAF(Or*   r   c                   $    e Zd ZU dZeed<   ddZy)LocalCellSourcez
    Conceptually, this class is `LocalSource` for cell objects implicitly
    generated by Python (e.g., captured variables).
    r   c                 X    |j                  |j                  | j                               y r"   )r$   create_load_closurer   r&   s     r   r)   zLocalCellSource.reconstruct  s!     	g99$//JKr*   Nr2   )r3   r4   r5   __doc__r6   r7   r)   r:   r*   r   r   r     s    
 OLr*   r   c                   0    e Zd ZU dZeed<   ddZd Zd Zy)
GradSourcegradr~   c                 |     || j                          |j                  |j                  | j                               y r"   r   r&   s     r   r)   zGradSource.reconstruct*  r   r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zGradSource.guard_source.  rt   r*   c                 T    | j                   j                          d| j                   S )Nr   r   r-   s    r   r1   zGradSource.name1  s#    )).."#1T[[M22r*   Nr2   )	r3   r4   r5   r~   r6   r7   r)   r   r1   r:   r*   r   r   r   &  s    FCF(3r*   r   c                       e Zd Zd Zy)ParamBufferSourcec                 D    t         | j                  j                            S r"   #_GUARD_SOURCE_SPECIALIZED_NN_MODULErq   r   r-   s    r   r   zParamBufferSource.guard_source7      24993I3I3KLLr*   Nr3   r4   r5   r   r:   r*   r   r   r   5  s    Mr*   r   c                       e Zd Zy)UnspecializedParamBufferSourceNrz   r:   r*   r   r   r   <  r{   r*   r   c                   :    e Zd ZU dZee   ed<   d Zd Zd Z	d Z
y)EphemeralSourceNdescc                 "    t         j                  S r"   )r   	EPHEMERALr-   s    r   r   zEphemeralSource.guard_sourceN      $$$r*   c                 J    d| j                   d| j                   z    dS d dS )Nz
<ephemeralz:  >)r   r-   s    r   r1   zEphemeralSource.nameQ  s1    		0ED499,NaPP2NaPPr*   c                     t         r"   r   r'   fns     r   
make_guardzEphemeralSource.make_guardT      !!r*   c                      yNTr:   r-   s    r   is_ephemeralzEphemeralSource.is_ephemeralW      r*   )r3   r4   r5   r   r   r6   r7   r   r1   r   r   r:   r*   r   r   r   J  s%    D(3-%Q"r*   r   c                       e Zd ZdZdZdZd Zy)TensorPropertyr   r
      c                 v    | t         j                  u ry| t         j                  u ry| t         j                  u ryy )Nsizestridestorage_offset)r   SIZESTRIDESTORAGE_OFFSETr-   s    r   method_namezTensorProperty.method_name`  s:    >&&&^***^222# 3r*   N)r3   r4   r5   r   r   r   r   r:   r*   r   r   r   [  s    DFN$r*   r   c                   F    e Zd ZU eed<   dZee   ed<   d ZddZ	d Z
d Zy)	TensorPropertySourcepropNidxc                     | j                   J | j                  t        j                  u r| j                  J y | j                  J y r"   )rq   r   r   r   r   r-   s    r   r   z"TensorPropertySource.__post_init__n  sD    yy$$$9955588###88'''r*   c                     j                   fd         j                          j                  *j                  j	                   j                               j                  t         j                  dndd             y )Nc                  z     j                  t        j                  dj                  j	                                S )Ncall_)load_import_fromr   r3   r   r   re   s   r   rf   z2TensorPropertySource.reconstruct.<locals>.<lambda>w  s0    G,,%		(=(=(?'@ A r*   r   r
   F)rh   rq   r   r$   rM   ri   r   r&   s   ``r   r)   z TensorPropertySource.reconstructu  sn    	

 			88!!'";";DHH"EF dhh&:5I	
r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   z!TensorPropertySource.guard_source  rt   r*   c                    | j                   t        j                  u r*| j                  j	                          d| j
                   dS | j                   t        j                  u r*| j                  j	                          d| j
                   dS | j                   t        j                  u r+| j
                  J | j                  j	                          dS t        d| j                          )Nz.size()[r/   z
.stride()[z.storage_offset()z
unhandled )	r   r   r   rq   r1   r   r   r   AssertionErrorr-   s    r   r1   zTensorPropertySource.name  s    99+++iinn&'xz;;YY.///iinn&'z$((1==YY.77788###iinn&''899 :dii[!9::r*   r2   )r3   r4   r5   r   r7   r   r   rP   r   r)   r   r1   r:   r*   r   r   r   i  s*    
C#(
(	;r*   r   c                   2    e Zd ZU eed<   d ZddZd Zd Zy)IndexedSourcer   c                      | j                   J y r"   rp   r-   s    r   r   zIndexedSource.__post_init__      yy$$$r*   c                     t         r"   r   r&   s     r   r)   zIndexedSource.reconstruct  r   r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zIndexedSource.guard_source  rt   r*   c                 X    d| j                    d| j                  j                          dS )N(r   r   )r   rq   r1   r-   s    r   r1   zIndexedSource.name  s'    488*Btyy~~/022r*   Nr2   )	r3   r4   r5   rP   r7   r   r)   r   r1   r:   r*   r   r   r     s    	H%"(3r*   r   c                   &    e Zd Zd ZddZd Zd Zy)NegateSourcec                      | j                   J y r"   rp   r-   s    r   r   zNegateSource.__post_init__  r   r*   c                     t         r"   r   r&   s     r   r)   zNegateSource.reconstruct  r   r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zNegateSource.guard_source  rt   r*   c                 <    | j                   j                          dS )Nz
.__neg__()rv   r-   s    r   r1   zNegateSource.name  s    )).."#:..r*   Nr2   r3   r4   r5   r   r)   r   r1   r:   r*   r   r   r     s    %"(/r*   r   c                   &    e Zd Zd ZddZd Zd Zy)ConvertIntSourcec                      | j                   J y r"   rp   r-   s    r   r   zConvertIntSource.__post_init__  r   r*   c                 (     || j                          y r"   rp   r&   s     r   r)   zConvertIntSource.reconstruct      		r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zConvertIntSource.guard_source  rt   r*   c                 >    d| j                   j                          dS )Nz!cast_symbool_to_symint_guardless(r   rv   r-   s    r   r1   zConvertIntSource.name  s    2499>>3C2DAFFr*   Nr2   r   r:   r*   r   r   r     s    %(Gr*   r   c                   &    e Zd Zd ZddZd Zd Zy)FlattenScriptObjectSourcec                      | j                   J y r"   rp   r-   s    r   r   z'FlattenScriptObjectSource.__post_init__  r   r*   c                 (     || j                          y r"   rp   r&   s     r   r)   z%FlattenScriptObjectSource.reconstruct  r   r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   z&FlattenScriptObjectSource.guard_source  rt   r*   c                 <    | j                   j                          dS )Nz.__obj_flatten__()rv   r-   s    r   r1   zFlattenScriptObjectSource.name  s    )).."##566r*   Nr2   r   r:   r*   r   r   r     s    %(7r*   r   c                   &    e Zd Zd ZddZd Zd Zy)ScriptObjectQualifiedNameSourcec                      | j                   J y r"   rp   r-   s    r   r   z-ScriptObjectQualifiedNameSource.__post_init__  r   r*   c                 (     || j                          y r"   rp   r&   s     r   r)   z+ScriptObjectQualifiedNameSource.reconstruct  r   r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   z,ScriptObjectQualifiedNameSource.guard_source  rt   r*   c                 <    | j                   j                          dS )Nz._type().qualified_name()rv   r-   s    r   r1   z$ScriptObjectQualifiedNameSource.name  s    )).."##<==r*   Nr2   r   r:   r*   r   r   r     s    %(>r*   r   c                        e Zd ZddZd Zd Zy)AttrProxySourcec                 (     || j                          y r"   rp   r&   s     r   r)   zAttrProxySource.reconstruct  r   r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zAttrProxySource.guard_source  rt   r*   c                 <    | j                   j                          dS )Nz.get_base()rv   r-   s    r   r1   zAttrProxySource.name  s    )).."#;//r*   Nr2   rw   r:   r*   r   r   r     s    (0r*   r   c                       e Zd ZU eeef   ed<   dZeed<    e	j                  ddd      Z
eed<    e	j                  ddd      Zeed<   d ZddZd	 Zd
 Zy)DefaultsSourceidx_keyFis_kw)initr0   comparefield_namec           
      @   | j                   sJ d       | j                  rt        | j                  t              sJ t
        j                  | dd       t
        j                  | d| j                   j                          d| j                   d| j                   d       y t        | j                  t              sJ t
        j                  | dd       t
        j                  | d| j                   j                          d| j                   d	| j                   d
       y )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/   )
rq   r  r   r  r6   r   r   r1   r  rP   r-   s    r   r   zDefaultsSource.__post_init__  s    yy 	
k	
y ::dllC000tW.>?g$)).."2!31TZZL4<<.PRS dllC000tWn=g$)).."2!31TZZL$,,qQr*   c                     || j                          |j                  |j                  | j                               |j	                  |j                  | j                               |j	                  t        d             y rH   )rq   ri   r   r  r$   rM   r  r   r&   s     r   r)   zDefaultsSource.reconstruct  s\    		g77

CDg77EF0ABr*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zDefaultsSource.guard_source	  rt   r*   c                     | j                   S r"   )r	  r-   s    r   r1   zDefaultsSource.name  s    zzr*   Nr2   )r3   r4   r5   r   rP   r6   r7   r  r8   dataclassesr  r	  r   r)   r   r1   r:   r*   r   r  r    se    38_E4"""E5IE3I"""E5IE3I"C(r*   r  c                   R     e Zd ZU eed<   dZeed<    fdZd	dZd Z	d Z
d Z xZS )
GetItemSourceindexFindex_is_slicec                     | j                   J t        | j                  t              r9t        |   d| j                  j                                t        |   dd       y y )Nr  r  T)rq   r   r  slicesuperr   
__reduce__)r'   	__class__s    r   r   zGetItemSource.__post_init__  sR    yy$$$djj%(G)>)>)@AG 0$7 )r*   c                 &    || j                          | j                  r/|j                  |j                  | j	                                      n*|j                  |j                  | j
                               |j                  t        d             y rH   )rq   r  r$   rM   unpack_slicer  r   r&   s     r   r)   zGetItemSource.reconstruct  sj    		!!'";";D<M<M<O"PQ!!'";";DJJ"GH0ABr*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zGetItemSource.guard_source$  rt   r*   c                 F    | j                   sJ | j                  \  }} || S r"   )r  r  )r'   slice_class
slice_argss      r   r  zGetItemSource.unpack_slice'  s*    """""&**ZJ''r*   c                    t        | j                  t              rJ | j                  r.| j                  j                          d| j                         dS | j                  j                          d| j                  dS Nr  r/   )r   r  r	   r  rq   r1   r  r-   s    r   r1   zGetItemSource.name,  sm     djj&111iinn&'q):):)<(?qAAiinn&'qa88r*   r2   )r3   r4   r5   r   r7   r  r8   r   r)   r   r  r1   __classcell__)r  s   @r   r  r    s,    J ND 8C((
9r*   r  c                   2    e Zd ZU eed<   d ZddZd Zd Zy)ConstDictKeySourcer  c                 6    | j                   j                         S r"   rs   r-   s    r   r   zConstDictKeySource.guard_source;  rt   r*   c                     j                  fd        | j                         j                  j                  | j                               j                  t        dd             y )Nc                  D     j                  t        j                  d      S )Ndict_keys_getitemr   r   r3   r(   s   r   rf   z0ConstDictKeySource.reconstruct.<locals>.<lambda>@  s    G,,U^^=PQ r*   r   Frh   rq   r$   rM   r  ri   r   r&   s    `r   r)   zConstDictKeySource.reconstruct>  sU    Q	
 			g77

CD21e<=r*   c                 X    d| j                   j                          d| j                  dS )Nzlist(dict.keys(z))[r/   rq   r1   r  r-   s    r   r1   zConstDictKeySource.nameF  s'     !1 2#djj^1EEr*   c                      yr   r:   r-   s    r   is_dict_keyzConstDictKeySource.is_dict_keyJ  r   r*   Nr2   )	r3   r4   r5   r   r7   r   r)   r1   r0  r:   r*   r   r%  r%  7  s    J(>Fr*   r%  c                   2    e Zd ZU eed<   d Zd ZddZd Zy)DictGetItemSourcer  c                     ddl m} t        | j                  t              s|j                  | j                        sJ y y Nr
   )ConstantVariable	variablesr5  r   r  r%  
is_literalr'   r5  s     r   r   zDictGetItemSource.__post_init__V  ;    /JJ*
((4	5 54
r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zDictGetItemSource.guard_source]  rt   r*   c                 
    || j                          t        | j                  t              r || j                         n*|j	                  |j                  | j                               |j	                  t        d             y rH   )rq   r   r  r	   r$   rM   r   r&   s     r   r)   zDictGetItemSource.reconstruct`  s[    		 djj&)DJJ!!'";";DJJ"GH0ABr*   c                     t        | j                  t              r8| j                  j	                          d| j                  j	                          dS | j                  j	                          d| j                  dS r"  r   r  r%  rq   r1   r-   s    r   r1   zDictGetItemSource.namek  s_    djj"45iinn&'q):(;1==iinn&'qa88r*   Nr2   	r3   r4   r5   r   r7   r   r   r)   r1   r:   r*   r   r2  r2  O  s    
 J5(	C9r*   r2  c                   2    e Zd ZU eed<   d Zd ZddZd Zy)DictSubclassGetItemSourcer  c                     ddl m} t        | j                  t              s|j                  | j                        sJ y y r4  r6  r9  s     r   r   z'DictSubclassGetItemSource.__post_init__{  r:  r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   z&DictSubclassGetItemSource.guard_source  rt   r*   c                 6   j                  fd        | j                         t        | j                  t              r | j                         n*j                  j                  | j                               j                  t        dd             y )Nc                  D     j                  t        j                  d      S )Ndict_getitemr*  r+  s   r   rf   z7DictSubclassGetItemSource.reconstruct.<locals>.<lambda>      G,,U^^^L r*   r   F)	rh   rq   r   r  r	   r$   rM   ri   r   r&   s    `r   r)   z%DictSubclassGetItemSource.reconstruct  ss     	L	

 			 djj&)DJJ!!'";";DJJ"GH21e<=r*   c                     t        | j                  t              r9d| j                  j	                          d| j                  j	                          dS | j                  j	                          d| j                  dS )Nzdict.__getitem__(r   r   r  r/   r>  r-   s    r   r1   zDictSubclassGetItemSource.name  sa    djj"45&tyy~~'7&84::??;L:MQOOiinn&'qa88r*   Nr2   r?  r:   r*   r   rA  rA  t  s    
 J5(>&9r*   rA  c                       e Zd ZdZddZd Zy)ListGetItemSourcezY
    Same as GetItemSource with reconstruct and name overridden to be list specific.
    c                 
   j                  fd        | j                         | j                  rt        d      j	                  j                  | j                               j                  t        dd             y )Nc                  D     j                  t        j                  d      S )Nlist_getitemr*  r+  s   r   rf   z/ListGetItemSource.reconstruct.<locals>.<lambda>  rG  r*   >List[slice] is a temporary object and should not have a sourcer   F)	rh   rq   r  RuntimeErrorr$   rM   r  ri   r   r&   s    `r   r)   zListGetItemSource.reconstruct  st    
 	L	

 			 P  !!'";";DJJ"GH21e<=r*   c                     t        | j                  t              rJ | j                  rt	        d      d| j
                  j                          d| j                  dS )NrN  zlist.__getitem__(r   r   )r   r  r	   r  rO  rq   r1   r-   s    r   r1   zListGetItemSource.name  sX     djj&111P  'tyy~~'7&84::.JJr*   Nr2   )r3   r4   r5   r   r)   r1   r:   r*   r   rJ  rJ    s    >,
Kr*   rJ  c                       e Zd ZddZd Zy)TupleIteratorGetItemSourcec                     j                  fd        | j                         j                  j                  | j                               j                  t        dd             y )Nc                  D     j                  t        j                  d      S )Ntuple_iterator_getitemr*  r+  s   r   rf   z8TupleIteratorGetItemSource.reconstruct.<locals>.<lambda>  s    G,,U^^=UV r*   r   Fr,  r&   s    `r   r)   z&TupleIteratorGetItemSource.reconstruct  sU    V	
 			g77

CD21e<=r*   c                 X    d| j                   j                          d| j                  dS )Nz___tuple_iterator_getitem(r   r   r.  r-   s    r   r1   zTupleIteratorGetItemSource.name  s'    +DIINN,<+=R

~QOOr*   Nr2   )r3   r4   r5   r)   r1   r:   r*   r   rR  rR    s    >Pr*   rR  c                        e Zd ZddZd Zd Zy)DataclassFieldsSourcec                     j                  fd        | j                         j                  t        dd             y )Nc                  D     j                  t        j                  d      S )Ndataclass_fieldsr*  r+  s   r   rf   z3DataclassFieldsSource.reconstruct.<locals>.<lambda>  s    G,,U^^=OP r*   r
   Frh   rq   ri   r   r&   s    `r   r)   z!DataclassFieldsSource.reconstruct  s8    P	
 			21e<=r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   z"DataclassFieldsSource.guard_source  rt   r*   c                 >    d| j                   j                          dS )Nz___dataclass_fields(r   rv   r-   s    r   r1   zDataclassFieldsSource.name  s    %diinn&6%7q99r*   Nr2   rw   r:   r*   r   rX  rX    s    >(:r*   rX  c                   &    e Zd Zd ZddZd Zd Zy)
TypeSourcec                      | j                   J y r"   rp   r-   s    r   r   zTypeSource.__post_init__  r   r*   c                     j                  fd        | j                         j                  t        dd             y )Nc                  (     j                  dd      S )Nbuiltinstyper   r+  s   r   rf   z(TypeSource.reconstruct.<locals>.<lambda>  s    g&>&>z6&R r*   r
   Fr\  r&   s    `r   r)   zTypeSource.reconstruct  s4    RS		21e<=r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zTypeSource.guard_source  rt   r*   c                 >    d| j                   j                          dS )Nztype(r   rv   r-   s    r   r1   zTypeSource.name  s    tyy~~'(**r*   Nr2   r   r:   r*   r   r`  r`    s    %>
(+r*   r`  c                        e Zd ZddZd Zd Zy)OptimizerSourcec                 (     || j                          y r"   rp   r&   s     r   r)   zOptimizerSource.reconstruct  r   r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zOptimizerSource.guard_source  rt   r*   c                 6    | j                   j                         S r"   rv   r-   s    r   r1   zOptimizerSource.name      yy~~r*   Nr2   rw   r:   r*   r   rj  rj    s    ( r*   rj  c                        e Zd ZddZd Zd Zy)NNModuleSourcec                 (     || j                          y r"   rp   r&   s     r   r)   zNNModuleSource.reconstruct  r   r*   c                 D    t         | j                  j                            S r"   r   r-   s    r   r   zNNModuleSource.guard_source  r   r*   c                 6    | j                   j                         S r"   rv   r-   s    r   r1   zNNModuleSource.name  rn  r*   Nr2   rw   r:   r*   r   rp  rp    s    M r*   rp  c                       e Zd Zd Zy)UnspecializedNNModuleSourcec                 D    t         | j                  j                            S r"   )%_GUARD_SOURCE_UNSPECIALIZED_NN_MODULErq   r   r-   s    r   r   z(UnspecializedNNModuleSource.guard_source  s    4TYY5K5K5MNNr*   Nr   r:   r*   r   ru  ru    s    Or*   ru  c                       e Zd Zd Zy)"UnspecializedBuiltinNNModuleSourcec                 D    t         | j                  j                            S r"   )-_GUARD_SOURCE_UNSPECIALIZED_BUILTIN_NN_MODULErq   r   r-   s    r   r   z/UnspecializedBuiltinNNModuleSource.guard_source  s    <TYY=S=S=UVVr*   Nr   r:   r*   r   ry  ry    s    Wr*   ry  c                       e Zd Zd Zy)FSDPNNModuleSourcec                 D    t         | j                  j                            S r"   )_GUARD_SOURCE_FSDP_MODULErq   r   r-   s    r   r   zFSDPNNModuleSource.guard_source  s    ()?)?)ABBr*   Nr   r:   r*   r   r}  r}    s    Cr*   r}  c                       e Zd Zd Zd Zy)GlobalStateSourcec                      yNr   r:   r-   s    r   r1   zGlobalStateSource.name#      r*   c                 "    t         j                  S r"   r[   r-   s    r   r   zGlobalStateSource.guard_source&  r]   r*   Nr3   r4   r5   r1   r   r:   r*   r   r  r  !  s    "r*   r  c                   2    e Zd ZU eed<   d Zd ZddZd Zy)TorchFunctionModeStackSourceindc                 *    d| j                          dS )Nz$___get_torch_function_mode_stack_at(r   )
_get_indexr-   s    r   r1   z!TorchFunctionModeStackSource.name.  s    5doo6G5HJJr*   c                 D    ddl m} |j                  | j                        S )Nr
   )TorchFunctionModeStackVariable)variables.torch_functionr  get_mode_indexr  )r'   r  s     r   r  z'TorchFunctionModeStackSource._get_index1  s    L-<<TXXFFr*   c                     j                  fd       j                  j                  | j                               g       j                  t	        dd             y )Nc                  D     j                  t        j                  d      S )N get_torch_function_mode_stack_atr*  r+  s   r   rf   z:TorchFunctionModeStackSource.reconstruct.<locals>.<lambda>8  s    G,, B r*   r
   F)rh   ri   rM   r  r   r&   s    `r   r)   z(TorchFunctionModeStackSource.reconstruct6  sQ    	

 	w889JKLM21e<=r*   c                 "    t         j                  S r"   r[   r-   s    r   r   z)TorchFunctionModeStackSource.guard_source?  r]   r*   Nr2   )	r3   r4   r5   rP   r7   r1   r  r)   r   r:   r*   r   r  r  *  s    	HKG
>"r*   r  c                   2    e Zd ZU eed<   ddZd Zd Zd Zy)r   source_namec                 \    |j                  |j                  | j                  d             y )NFrV   )r$   rY   r  r&   s     r   r)   zConstantSource.reconstructG  s&    g889I9Iu8UVr*   c                 "    t         j                  S r"   )r   r   r-   s    r   r   zConstantSource.guard_sourceJ  s    ###r*   c                     | j                   S r"   )r  r-   s    r   r1   zConstantSource.nameM  s    r*   c                     t         r"   r   r   s     r   r   zConstantSource.make_guardP  r   r*   Nr2   )	r3   r4   r5   r6   r7   r)   r   r1   r   r:   r*   r   r   r   C  s    W$ "r*   r   c                   &    e Zd ZdefdZd ZddZy)NumpyTensorSourcereturnc                 >    d| j                   j                          dS )Nz___from_numpy(r   rv   r-   s    r   r1   zNumpyTensorSource.nameV  s    		 0133r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zNumpyTensorSource.guard_sourceY  rt   r*   c                     j                  fd        | j                         j                  t        dd             y )Nc                  (     j                  dd      S )Ntorch	as_tensorrf  r+  s   r   rf   z/NumpyTensorSource.reconstruct.<locals>.<lambda>]  s    g&>&>w&T r*   r
   Fr\  r&   s    `r   r)   zNumpyTensorSource.reconstruct\  s4    TU		21e<=r*   Nr2   )r3   r4   r5   r6   r1   r   r)   r:   r*   r   r  r  T  s    4c 4(>r*   r  c                       e Zd ZdefdZd Zy)SubclassAttrListSourcer  c                 <    | j                   j                          dS )Nz.__tensor_flatten__()[0]rv   r-   s    r   r1   zSubclassAttrListSource.named  s    )).."##;<<r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   z#SubclassAttrListSource.guard_sourceg  rt   r*   Nr3   r4   r5   r6   r1   r   r:   r*   r   r  r  b  s    =c =(r*   r  c                       e Zd ZdefdZd Zy)FloatTensorSourcer  c                 >    d| j                   j                          dS )Nz___as_tensor(r   rv   r-   s    r   r1   zFloatTensorSource.nameo  s    tyy~~/022r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   zFloatTensorSource.guard_sourcer  rt   r*   Nr  r:   r*   r   r  r  m  s    3c 3(r*   r  c                       e Zd ZdefdZd Zy)CallMethodItemSourcer  c                 <    | j                   j                          dS )Nz.item()rv   r-   s    r   r1   zCallMethodItemSource.namex  s    )).."#7++r*   c                 6    | j                   j                         S r"   rs   r-   s    r   r   z!CallMethodItemSource.guard_source{  rt   r*   Nr  r:   r*   r   r  r  v  s    ,c ,(r*   r  c                       e Zd Zd Zd Zy)ShapeEnvSourcec                      yr  r:   r-   s    r   r1   zShapeEnvSource.name  r  r*   c                 "    t         j                  S r"   )r   	SHAPE_ENVr-   s    r   r   zShapeEnvSource.guard_source  r   r*   Nr  r:   r*   r   r  r    s    %r*   r  c                       e Zd Zd Zd Zy)BackwardStateSourcec                      yr  r:   r-   s    r   r1   zBackwardStateSource.name  r  r*   c                 "    t         j                  S r"   )r   BACKWARD_STATEr-   s    r   r   z BackwardStateSource.guard_source  s    )))r*   Nr  r:   r*   r   r  r    s    *r*   r  Fonly_allow_inputr   r  c                    t        | t              rt        | j                  |      S t        | t              sy |r| j
                  sy | j                  S Nr  )r   r   get_local_source_namerq   r   r   r   r   r  s     r   r  r    sC    &-($V[[CSTTfk*r*   c                     t        | |      d uS r  )r  r  s     r   is_from_local_sourcer    s     :JKSWWWr*   c                     t        |       d uS r"   )get_global_source_namer   s    r   is_from_global_sourcer    s    !&)55r*   c                     t        | t              rt        | j                        S t        | t              sy | j
                  S r"   )r   r   r  rq   rR   rS   r   s    r   r  r    s4    &-(%fkk22fl+r*   c                     t        | t              rt        | j                        S t        | t              xr | j
                  xr | j                   S r"   )r   r   is_from_nonlocal_sourcerq   r   r    r   r   s    r   r  r    sG    &-(&v{{336;' 	 ++	 r*   targetc                 X    t        | t              rt        | j                  |      S | |k(  S r"   )r   r   is_from_sourcerq   )r   r  s     r   r  r    s(    &-(fkk622Vr*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   ru  r   &is_from_unspecialized_nn_module_sourcerq   r   s    r   r  r    s,    &56&-(5fkkBBr*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   ry  r   .is_from_unspecialized_builtin_nn_module_sourcerq   r   s    r   r  r    s,    &<=&-(=fkkJJr*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   r   r   )is_from_unspecialized_param_buffer_sourcerq   r   s    r   r  r    s,    &89&-(8EEr*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   r   r   $is_from_flatten_script_object_sourcerq   r   s    r   r  r    s,    &34	FM	*3FKK@@r*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   rj  r   is_from_optimizer_sourcerq   r   s    r   r  r    s+    &/*&-('44r*   c                    t        | t              ryt        | t              r4t        | j                  t              r| j                  j
                  dk(  ryt        | t              r4t        | j                  t              r| j                  j
                  dk(  ryt        | t              rt        | j                        S y)NTr  r  F)	r   r  r2  rq   r}   r~   r  r   is_from_defaultsr   s    r   r  r    s    &.) 	6,-v{{J/KK"22 	6=)v{{J/KK.0&-(,,r*   )ar   r  enum	functoolstypingr   r   r   r   torch._guardsr   r   r	   r   r   bytecode_transformationr   r   r(   r   r,   LOCAL_SPECIALIZED_NN_MODULEr\   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   rw  r{  r  r   	dataclassr   r<   rC   rR   rb   rm   ry   r}   r   r   r   r   r   r   Enumr   r   r   r   r   r   r   r   r  r  r%  r2  rA  rJ  rR  rX  r`  rj  rp  ru  ry  r}  r  r  r   r  r  r  r  r  r  r6   r  r  r8   r  r  r  r  	lru_cacher  r  r  r  r  r  r:   r*   r   <module>r     s<  &    6 6 < <  M " {>>@@++[-T-T,,k.V.V--{/X/X..0Z0Z55{7h7h668j8j!!;#@#@""K$B$B' #  {@@BB--{/X/X..0Z0Z++[-V-V,,k.X.X55{7h7h668j8j!!;#@#@""K$B$B) %" {HHJJ--{/`/`..0b0b++[-^-^,,k.`.`55{7h7h668j8j!!;#@#@""K$B$B1 - {4466++[-J-J,,k.L.L!!;#@#@""K$B$B--{/L/L..0N0N55{7T7T668V8V 	 d#-& - $-8 d#
76 
7 $
7 d#8 8 $8 d#
.6 
. $
. d#0& 0 $0$ d#	' 	' $	' d#	0 	 $	 d#3 3 $32 d#O O $O. d#Lf L $L, d#3 3 $3 d#M
 M $M d#	Z 	 $	 d#f  $ $TYY $ d#%;= %; $%;P d#3M 3 $3  d#/= / $/ d#G} G $G d#7 7 $7 d#>m > $>0m 0 d#!] ! $!H d##9M #9 $#9L d#  $. d#9 9 $9H d#'9 '9 $'9T d#%K %K $%KP d#
P 
P $
P d#:M : $: d#+ + $+  d# m   $  d# ]   $  d#O. O $O
 d#W)D W $W
 d#C C $C
 d#" " $" d#"6 " $"0 d#"V " $"  d#
> 
> $
> d#(] ( $( d#( ( $( d#(= ( $( d#%V % $% d#*& * $* ?D & QT  >C X X6& 6T 66 hsm F 6 6  6   6   f      V   V  r*   