
    ^j                        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rdd
lmZ ej4                  ej6                  ej8                  ej:                  ej6                  ej6                  ej:                  ej:                  ej<                  ej<                  ej>                  ej>                  ej@                  ej@                  ejB                  ejB                  ejD                  ejD                  ejF                  ejF                  i
Z$ej4                  ej<                  ej8                  ej>                  ej<                  ej<                  ej>                  ej>                  ej6                  ej<                  ej:                  ej>                  ej@                  ej@                  ejB                  ejB                  ejD                  ejD                  ejF                  ejF                  i
Z%ej4                  ej@                  ej8                  ejB                  ej<                  ej@                  ej>                  ejB                  ej6                  ej@                  ej:                  ejB                  ej@                  ej@                  ejB                  ejB                  ejD                  ejD                  ejF                  ejF                  i
Z&ej4                  ejD                  ej8                  ejF                  ej6                  ejD                  ej:                  ejF                  ejD                  ejD                  ejF                  ejF                  ej<                  ejD                  ej>                  ejF                  ej@                  ejD                  ejB                  ejF                  i
Z'dede(fdZ)dedz  de*fdZ+ddede(de*fdZ, ed       G d de             Z- ed       G d de             Z. ed       G d de             Z/ ed       G d de             Z0 ed       G d de             Z1 ed       G d d e             Z2 ed       G d! d"e             Z3 ed       G d# d$e3             Z4 ed       G d% d&e             Z5 ed       G d' d(e5             Z6 ed       G d) d*e             Z7 ed       G d+ d,e             Z8 ed       G d- d.e             Z9 ed       G d/ d0e             Z: ed       G d1 d2e             Z; ed       G d3 d4e             Z< ed       G d5 d6e             Z= ed       G d7 d8e5             Z> ed       G d9 d:e5             Z? ed       G d; d<e             Z@ ed       G d= d>e             ZA G d? d@ej                        ZC ed       G dA dBe             ZD ed       G dC dDe             ZE ed       G dE dFe             ZF ed       G dG dHe             ZG ed       G dI dJe             ZH ed       G dK dLe             ZI ed       G dM dNe             ZJ G dO dPe      ZK ed       G dQ dRe             ZL ed       G dS dTe             ZM ed       G dU dVe             ZN ed       G dW dXe             ZO ed       G dY dZe             ZP ed       G d[ d\e             ZQ ed       G d] d^eM             ZR ed       G d_ d`eM             ZS ed       G da dbe             ZT ed       G dc dde             ZU ed       G de dfe             ZV ed       G dg dhe             ZW ed       G di dje             ZX ed       G dk dleX             ZY ed       G dm dneY             ZZ ed       G do dpeX             Z[ ed       G dq dre             Z\ ed       G ds dte             Z] ed       G du dve             Z^ ed       G dw dxe             Z_ ed       G dy dze             Z` ed       G d{ d|e             Za ed       G d} d~e             Zb ed       G d de             Zc ed       G d de             Zd ed       G d de             Ze ed       G d de             Zfej                  dddede(de*dz  fd       Zhej                  dddede(de(fd       Ziej                  dede(fd       Zjej                  dedz  de*dz  fd       Zkej                  dede(fd       Zlej                  dede(fd       Zmej                  dedede(fd       Znej                  dede(fd       Zoej                  dede(fd       Zpej                  dede(fd       Zqej                  dede(fd       Zrej                  dede(fd       Zsej                  dede(fd       Ztej                  dede(fd       Zuy)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                     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r   s    _/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/source.pyis_constant_sourcer   m   sL    &.)+"6"66 7
   s   3 	??c                 @    | y	 | j                   S # t        $ r Y yw xY w)Nz<unknown source>)namer   r   s    r   _get_source_debug_namer    y   s+    ~!	&;;" 	&%	&s    	Fs
apply_reprc                 x    |rt        |       } nt        |       } | j                  dd      j                  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"   s     r   _esc_strr)      s7     GF99S$''T22    T)frozenc                   
   e Zd ZU eed<   dZeed<   dZee   dz  ed<   dZ	eed<    e
j                  dd      Zeed<    e
j                  dd      Zeed	<   ddZd
ddefdZedefd       Zej(                  defd       Zy)LocalSource
local_nameFis_inputNdynamismis_derefed_cell_contents)defaultr&   
is_varargsis_varkwcodegenr   r   c                     | j                   r|j                  | j                         y |j                  |j	                  | j                               y N)r1   
load_derefr.   append_outputcreate_loadselfr5   s     r   reconstructzLocalSource.reconstruct   s:    ((t/!!'"5"5doo"FGr*   c                     | j                   S r7   )r.   r;   s     r   reconstruct_pycodezLocalSource.reconstruct_pycode   s    r*   c                 "    t         j                  S r7   )r
   LOCALr<   s    r   r   zLocalSource.guard_source   s       r*   c                 8    dt        | j                  d       dS )NzL[Tr"   ]r)   r.   rB   s    r   _name_templatezLocalSource._name_template   s    HT__>?qAAr*   r5   r   r   N)__name__
__module____qualname__r'   __annotations__r/   boolr0   	frozensetr1   dataclassesfieldr3   r4   r=   r?   propertyr
   r   	functoolscached_propertyrG    r*   r   r-   r-      s    O Hd '+Hint#*
 &+d* ){((UCJC '[&&u5AHdAH+ #  !k ! ! B B Br*   r-   c                   L    e Zd ZU eed<   ddZedefd       Zedefd       Z	y)TempLocalSourcer.   r   Nc                 X    |j                  |j                  | j                               y r7   r9   r:   r.   r;   s     r   r=   zTempLocalSource.reconstruct       g11$//BCr*   c                 "    t         j                  S r7   )r
   
TEMP_LOCALrB   s    r   r   zTempLocalSource.guard_source   s    %%%r*   c                     t        d      )NzhCannot create guard on TempLocalSource - this is an internal Dynamo bug. Please file an issue on GitHub.r   rB   s    r   rG   zTempLocalSource._name_template   s    !v
 	
r*   rH   )
rI   rJ   rK   r'   rL   r=   rQ   r
   r   rG   rT   r*   r   rV   rV      sE     OD &k & & 
 
 
r*   rV   c                   `    e Zd ZU eed<   ddZedefd       Ze	j                  defd       Zy)SyntheticLocalSourcer.   r   Nc                 X    |j                  |j                  | j                               y r7   rX   r;   s     r   r=   z SyntheticLocalSource.reconstruct   rY   r*   c                 "    t         j                  S r7   )r
   SYNTHETIC_LOCALrB   s    r   r   z!SyntheticLocalSource.guard_source   s    ***r*   c                 8    dt        | j                  d       dS )NzSYNTHETIC_LOCAL[TrD   rE   rF   rB   s    r   rG   z#SyntheticLocalSource._name_template   s    !(4??t"L!MQOOr*   rH   rI   rJ   rK   r'   rL   r=   rQ   r
   r   rR   rS   rG   rT   r*   r   r_   r_      sL    OD +k + + P P Pr*   r_   c                   `    e Zd ZU eed<   edefd       ZddZe	j                  defd       Zy)RandomValueSourcerandom_call_indexr   c                 "    t         j                  S r7   )r
   RANDOM_VALUErB   s    r   r   zRandomValueSource.guard_source   s    '''r*   Nc                    |j                  |j                  |j                  j                  j                               |j                  |j                  | j                               |j                  t                      y r7   )r9   r:   txoutputrandom_values_varcreate_load_constrg   r   r;   s     r   r=   zRandomValueSource.reconstruct   s\    g11'**2C2C2U2UVWg778N8NOP245r*   c                 2    dt        | j                         S )Nrandom_value_)r)   rg   rB   s    r   rG   z RandomValueSource._name_template   s    x(>(>?@AAr*   rH   )rI   rJ   rK   intrL   rQ   r
   r   r=   rR   rS   r'   rG   rT   r*   r   rf   rf      sJ    (k ( (6
 B B Br*   rf   c                   p    e Zd ZU eed<   d
dZdddefdZedefd       Z	e
j                  defd	       Zy)GlobalSourceglobal_namer5   r   r   Nc                 \    |j                  |j                  | j                  d             y NTaddr9   create_load_globalrt   r;   s     r   r=   zGlobalSource.reconstruct   s&    g889I9It8TUr*   c                     | j                   S r7   )rt   r;   s     r   r?   zGlobalSource.reconstruct_pycode   s    r*   c                 "    t         j                  S r7   r
   GLOBALrB   s    r   r   zGlobalSource.guard_source      !!!r*   c                 8    dt        | j                  d       dS )NG[TrD   rE   r)   rt   rB   s    r   rG   zGlobalSource._name_template  s    HT--$?@BBr*   rH   )rI   rJ   rK   r'   rL   r=   r?   rQ   r
   r   rR   rS   rG   rT   r*   r   rs   rs      s`    V +  #   "k " " C C Cr*   rs   c                   `    e Zd ZU eed<   ddZedefd       Ze	j                  defd       Zy)GlobalWeakRefSourcert   r   Nc                 h     j                   fd       j                  t        dd             y )Nc                  \     j                   j                  j                  d            S rv   ry   r5   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 r7   r}   rB   s    r   r   z GlobalWeakRefSource.guard_source  r   r*   c                 8    dt        | j                  d       dS )Nr   TrD   z]()r   rB   s    r   rG   z"GlobalWeakRefSource._name_template  s    HT--$?@DDr*   rH   rd   rT   r*   r   r   r   
  sL    > "k " " E E Er*   r   c                   *    e Zd ZddZedefd       Zy)WeakRefCallSourcer   Nc                 h     j                   fd       j                  t        dd             y )Nc                  (      j                         S r7   baser   s   r   r   z/WeakRefCallSource.reconstruct.<locals>.<lambda>"  s    gdii&8 r*   r   Fr   r;   s   ``r   r=   zWeakRefCallSource.reconstruct!  s(    8921e<=r*   c                      y)Nz{0}()rT   rB   s    r   rG   z WeakRefCallSource._name_template%  s    r*   rH   rI   rJ   rK   r=   rQ   r'   rG   rT   r*   r   r   r     s"    >   r*   r   c                       e Zd Zy)CallFunctionNoArgsSourceNrI   rJ   rK   rT   r*   r   r   r   *      r*   r   c                   b    e Zd ZU eed<   d
dZddZdddefdZej                  defd	       Z
y)
AttrSourcememberr   Nc                     | j                   st        d      d| j                  v rt        d| j                  d      y )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.r   AssertionErrorr   rB   s    r   __post_init__zAttrSource.__post_init__3  sQ    yy K  $++ >t{{o NN N  r*   r5   r   c                 |     || j                          |j                  |j                  | j                               y r7   r   r   create_load_attrsr   r;   s     r   r=   zAttrSource.reconstruct>  +    		g77DEr*   c                     | j                   j                  |      }| j                  j                         r| d| j                   S d| d| j                  dS )Nr   zgetattr(z, ))r   r?   r   isidentifier)r<   r5   r   s      r   r?   zAttrSource.reconstruct_pycodeB  sT    yy++G4;;##%V1T[[M**dV2dkk_A66r*   c                     | j                   j                         sdt        | j                   d       dS dt        | j                          S )Nzgetattr({0}, TrD   r   {0}.)r   r   r)   rB   s    r   rG   zAttrSource._name_templateI  sD    {{'')$Xdkkd%K$LANN-.//r*   r   NrH   )rI   rJ   rK   r'   rL   r   r=   r?   rR   rS   rG   rT   r*   r   r   r   /  sF    K	F7+ 7# 7 0 0 0r*   r   c                   J    e Zd ZU dZ ej
                  d      Zeed<   ddZ	y)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.
     )r2   freevar_nameNc                 f    | j                   st        d      | j                  dk7  rt        d      y )Nz@Can't construct a CellContentsSource without a valid base sourcecell_contentsz8CellContentsSource should only be used for cell_contentsr   rB   s    r   r   z CellContentsSource.__post_init__Y  s<    yy R  ;;/) J  *r*   r   )
rI   rJ   rK   __doc__rO   rP   r   r'   rL   r   rT   r*   r   r   r   P  s%    
 *))"5L#5r*   r   c                   R    e Zd ZU eed<   ddZddZej                  defd       Z	y)	GenericAttrSourcer   r   Nc                     | j                   st        d      d| j                  v rt        d| j                  d      y )Nz?Can't construct a GenericAttrSource without a valid base sourcer   z3GenericAttrSource member must not contain '.', got r   r   rB   s    r   r   zGenericAttrSource.__post_init__h  sQ    yy Q  $++ Edkk_ UN N  r*   c                 |     || j                          |j                  |j                  | j                               y r7   r   r;   s     r   r=   zGenericAttrSource.reconstructs  r   r*   c                 8    dt        | j                  d       dS )Nzobject.__getattribute__({0}, TrD   r   r)   r   rB   s    r   rG   z GenericAttrSource._name_templatew  s"     .ht{{t.T-UUVW	
r*   r   rH   )
rI   rJ   rK   r'   rL   r   r=   rR   rS   rG   rT   r*   r   r   r   d  s3    K	F 
 
 
r*   r   c                   *    e Zd ZddZedefd       Zy)TypeDictSourcer   Nc                 h     || j                          |j                  |j                  d             y )N__dict__r   r   r   r;   s     r   r=   zTypeDictSource.reconstruct  '    		g77
CDr*   c                      y)Nzdict({0}.__dict__)rT   rB   s    r   rG   zTypeDictSource._name_template  s     $r*   rH   r   rT   r*   r   r   r     s#    E $ $ $r*   r   c                   *    e Zd ZddZedefd       Zy)TypeMROSourcer   Nc                 h     || j                          |j                  |j                  d             y )N__mro__r   r;   s     r   r=   zTypeMROSource.reconstruct  '    		g77	BCr*   c                      y)Nz{0}.__mro__rT   rB   s    r   rG   zTypeMROSource._name_template  s    r*   rH   r   rT   r*   r   r   r     s#    D   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.   Nc                 X    |j                  |j                  | j                               y r7   )r9   create_load_closurer.   r;   s     r   r=   zLocalCellSource.reconstruct  s!     	g99$//JKr*   rH   )rI   rJ   rK   r   r'   rL   r=   rT   r*   r   r   r     s    
 OLr*   r   c                   *    e Zd ZddZedefd       Zy)
CodeSourcer   Nc                 h     || j                          |j                  |j                  d             y )N__code__r   r;   s     r   r=   zCodeSource.reconstruct  r   r*   c                      y)Nz{0}.__code__rT   rB   s    r   rG   zCodeSource._name_template  s    r*   rH   r   rT   r*   r   r   r     s#    E   r*   r   c                   *    e Zd ZddZedefd       Zy)ClosureSourcer   Nc                 h     || j                          |j                  |j                  d             y )N__closure__r   r;   s     r   r=   zClosureSource.reconstruct  s'    		g77FGr*   c                      y)Nz{0}.__closure__rT   rB   s    r   rG   zClosureSource._name_template  s     r*   rH   r   rT   r*   r   r   r     s#    H ! ! !r*   r   c                   N    e Zd ZU dZeed<   ddZej                  defd       Z	y)
GradSourcegradr   r   Nc                 |     || j                          |j                  |j                  | j                               y r7   r   r;   s     r   r=   zGradSource.reconstruct  r   r*   c                 2    dt        | j                         S )Nr   r   rB   s    r   rG   zGradSource._name_template  s    -.//r*   rH   )
rI   rJ   rK   r   r'   rL   r=   rR   rS   rG   rT   r*   r   r   r     s3    FCF 0 0 0r*   r   c                   6    e Zd Zej                  defd       Zy)ParamBufferSourcer   c                 <    t         | j                  j                     S r7   #_GUARD_SOURCE_SPECIALIZED_NN_MODULEr   r   rB   s    r   r   zParamBufferSource.guard_source      24993I3IJJr*   NrI   rJ   rK   rR   rS   r
   r   rT   r*   r   r   r     s$    Kk K Kr*   r   c                       e Zd Zy)UnspecializedParamBufferSourceNr   rT   r*   r   r   r     r   r*   r   c                       e Zd ZU dZedz  ed<   edefd       Ze	j                  defd       Zdedef   defdZdefd	Zy)
EphemeralSourceNdescr   c                 "    t         j                  S r7   )r
   	EPHEMERALrB   s    r   r   zEphemeralSource.guard_source      $$$r*   c                 Z    | j                   d| j                   z   nd}dt        |       dS )Nz: r   z
<ephemeral>)r   r)   )r<   r   s     r   rG   zEphemeralSource._name_template  s/    #'99#8tdiibHTN+1--r*   fn.c                     t         r7   r]   r<   r   s     r   
make_guardzEphemeralSource.make_guard      !!r*   c                      yNTrT   rB   s    r   is_ephemeralzEphemeralSource.is_ephemeral      r*   )rI   rJ   rK   r   r'   rL   rQ   r
   r   rR   rS   rG   r   r   r	   r   rM   r   rT   r*   r   r   r     sr    D#*%k % % . . ."Xc3h/ "E "d r*   r   c                   *    e Zd ZddZedefd       Zy)SkipGuardSourcer   Nc                 :    | j                   j                  |       y r7   )r   r=   r;   s     r   r=   zSkipGuardSource.reconstruct  s    		g&r*   c                      yNz{0}rT   rB   s    r   rG   zSkipGuardSource._name_template      r*   rH   r   rT   r*   r   r   r      s"    '   r*   r   c                   $    e Zd ZdZdZdZdefdZy)TensorPropertyr   r      r   c                     | t         j                  u ry| t         j                  u ry| t         j                  u ryt	        dt        |              )Nsizestridestorage_offset
unhandled )r   SIZESTRIDESTORAGE_OFFSETr   r)   rB   s    r   method_namezTensorProperty.method_name  sL    >&&&^***^222# :htn-=!>??r*   N)rI   rJ   rK   r  r  r  r'   r  rT   r*   r   r   r   
  s    DFN@S @r*   r   c                   f    e Zd ZU eed<   dZedz  ed<   ddZd	dZe	j                  defd       Zy)
TensorPropertySourcepropNidxr   c                     | j                   t        d      | j                  t        j                  u r| j
                  t        d      y | j
                  t        d| j                   d      y )Nz*TensorPropertySource base must not be None2idx must be None for STORAGE_OFFSET TensorPropertyzidx must not be None for z TensorProperty)r   r   r  r   r  r	  rB   s    r   r   z"TensorPropertySource.__post_init__  sw    99 !MNN99555xx#$H  $
 xx$/		{/J   r*   c                     j                   fd         j                          j                  *j                  j	                   j                               j                  t         j                  dndd             y )Nc                       j                  t        j                  dt        j                  j                                      S )Ncall_)load_import_fromr   rI   r)   r  r  r   s   r   r   z2TensorPropertySource.reconstruct.<locals>.<lambda>/  s5    G,,%1F1F1H(I'J K r*   r   r   F)r   r   r	  r9   rn   r   r   r;   s   ``r   r=   z TensorPropertySource.reconstruct-  sn    	

 			88!!'";";DHH"EF dhh&:5I	
r*   c                    | 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                  t        d      yt        dt        | j                                )Nz{0}.size()[rE   z{0}.stride()[r  z{0}.storage_offset()r  )r  r   r  r)   r	  r  r  r   rB   s    r   rG   z#TensorPropertySource._name_template;  s    99+++"8DHH#5"6a88YY.///$Xdhh%7$8::YY.777xx#$H  * :htyy.A-B!CDDr*   r   rH   )rI   rJ   rK   r   rL   r	  rq   r   r=   rR   rS   r'   rG   rT   r*   r   r  r    sD    
Ct
 E E Er*   r  c                   R    e Zd ZU eed<   ddZddZej                  de	fd       Z
y)	IndexedSourcer	  r   Nc                 2    | j                   t        d      y )Nz#IndexedSource base must not be Noner   r   rB   s    r   r   zIndexedSource.__post_init__O  s    99 !FGG r*   c                     t         r7   r]   r;   s     r   r=   zIndexedSource.reconstructS  r   r*   c                 4    dt        | j                         dS )N(z, {0}))r)   r	  rB   s    r   rG   zIndexedSource._name_templateV  s    8DHH%&h//r*   r   rH   )rI   rJ   rK   rq   rL   r   r=   rR   rS   r'   rG   rT   r*   r   r  r  K  s3    	HH" 0 0 0r*   r  c                   2    e Zd ZddZddZedefd       Zy)NegateSourcer   Nc                 2    | j                   t        d      y )Nz"NegateSource base must not be Noner  rB   s    r   r   zNegateSource.__post_init__]  s    99 !EFF r*   c                     t         r7   r]   r;   s     r   r=   zNegateSource.reconstructa  r   r*   c                      y)Nz{0}.__neg__()rT   rB   s    r   rG   zNegateSource._name_templated  s     r*   r   rH   rI   rJ   rK   r   r=   rQ   r'   rG   rT   r*   r   r  r  [  s(    G"   r*   r  c                   2    e Zd ZddZddZedefd       Zy)ConvertIntSourcer   Nc                 2    | j                   t        d      y )Nz&ConvertIntSource base must not be Noner  rB   s    r   r   zConvertIntSource.__post_init__l  s    99 !IJJ r*   c                 (     || j                          y r7   r   r;   s     r   r=   zConvertIntSource.reconstructp      		r*   c                      y)Nz%cast_symbool_to_symint_guardless({0})rT   rB   s    r   rG   zConvertIntSource._name_templates  s    6r*   r   rH   r  rT   r*   r   r  r  j  s(    K 7 7 7r*   r  c                   >    e Zd ZU eed<   ddZddZedefd       Z	y)	DynamicScalarSourceis_intr   Nc                 2    | j                   t        d      y )Nz)DynamicScalarSource base must not be Noner  rB   s    r   r   z!DynamicScalarSource.__post_init__|  s    99 !LMM r*   c                     j                  fd        | j                         j                  t        dd             y )Nc                  (     j                  dd      S )Nbuiltinsrq   r  r5   s   r   r   z1DynamicScalarSource.reconstruct.<locals>.<lambda>  s    g&>&>z5&Q r*   r   Fr   r   r   r   r;   s    `r   r=   zDynamicScalarSource.reconstruct  s6    
 	QR		21e<=r*   c                      y)Nzint({0})rT   rB   s    r   rG   z"DynamicScalarSource._name_template  s    r*   r   rH   )
rI   rJ   rK   rM   rL   r   r=   rQ   r'   rG   rT   r*   r   r%  r%  x  s-    LN>   r*   r%  c                   2    e Zd ZddZddZedefd       Zy)FlattenScriptObjectSourcer   Nc                 2    | j                   t        d      y )Nz/FlattenScriptObjectSource base must not be Noner  rB   s    r   r   z'FlattenScriptObjectSource.__post_init__  s    99 !RSS r*   c                 (     || j                          y r7   r   r;   s     r   r=   z%FlattenScriptObjectSource.reconstruct  r"  r*   c                      y)Nz{0}.__obj_flatten__()rT   rB   s    r   rG   z(FlattenScriptObjectSource._name_template  s    &r*   r   rH   r  rT   r*   r   r0  r0    s(    T ' ' 'r*   r0  c                   2    e Zd ZddZddZedefd       Zy)ScriptObjectQualifiedNameSourcer   Nc                 2    | j                   t        d      y )Nz5ScriptObjectQualifiedNameSource base must not be Noner  rB   s    r   r   z-ScriptObjectQualifiedNameSource.__post_init__  s!    99 G  r*   c                 (     || j                          y r7   r   r;   s     r   r=   z+ScriptObjectQualifiedNameSource.reconstruct  r"  r*   c                      y)Nz{0}._type().qualified_name()rT   rB   s    r   rG   z.ScriptObjectQualifiedNameSource._name_template  s    -r*   r   rH   r  rT   r*   r   r5  r5    s'     . . .r*   r5  c                   *    e Zd ZddZedefd       Zy)AttrProxySourcer   Nc                 (     || j                          y r7   r   r;   s     r   r=   zAttrProxySource.reconstruct  r"  r*   c                      y)Nz{0}.get_base()rT   rB   s    r   rG   zAttrProxySource._name_template  s    r*   rH   r   rT   r*   r   r:  r:    s"          r*   r:  c                       e Zd ZU eez  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d	Zdd
Zej                  defd       Zy)DefaultsSourceidx_keyFis_kw)initr&   comparerP   _namer   Nc           
         | j                   st        d      | j                  rt        | j                  t
              s!t        dt        | j                               t        j                  | dd       t        j                  | ddt        | j                         dt        | j                         d       y t        | j                  t              s!t        d	t        | j                               t        j                  | dd
       t        j                  | ddt        | j                         dt        | j                         d       y )Nz]Base must be a valid source in order to properly track and guard this Defaults to its origin.z0idx_key must be a str for keyword defaults, got rP   __kwdefaults__rC  r   z['z']z4idx_key must be an int for positional defaults, got __defaults__[rE   )r   r   r@  r   r?  r'   typeobject__setattr__r)   rP   rq   rB   s    r   r   zDefaultsSource.__post_init__  s!   yy o  ::dllC0$FtDLLGYFZ[  tW.>?$**-.b$,,1G0HK dllC0$J4PTP\P\K]J^_  tWn=g(<'=Qx?U>VVWXr*   c                     || j                          |j                  |j                  | j                               |j	                  |j                  | j                               |j	                  t                      y r7   )r   r   r   rP   r9   rn   r?  r   r;   s     r   r=   zDefaultsSource.reconstruct  sZ    		g77

CDg77EF245r*   c                     | j                   S r7   )rC  rB   s    r   rG   zDefaultsSource._name_template  s    zzr*   r   rH   )rI   rJ   rK   rq   r'   rL   r@  rM   rO   rP   rC  r   r=   rR   rS   rG   rT   r*   r   r>  r>    sw    3YE4"""E5IE3I"""E5IE3I46   r*   r>  c                   x     e Zd ZU eed<   dZeed<   d	 fdZd
dZde	fdZ
ej                  defd       Z xZS )GetItemSourceindexFindex_is_slicer   c                     | j                   t        d      t        | j                  t              r9t
        |   d| j                  j                                t
        |   dd       y y )Nz#GetItemSource base must not be NonerO  rP  T)r   r   r   rO  slicesuperrJ  
__reduce__)r<   	__class__s    r   r   zGetItemSource.__post_init__  sX    99 !FGGdjj%(G)>)>)@AG 0$7 )r*   c                 $    || j                          | j                  r/|j                  |j                  | j	                                      n*|j                  |j                  | j
                               |j                  t                      y r7   )r   rP  r9   rn   unpack_slicerO  r   r;   s     r   r=   zGetItemSource.reconstruct  sh    		!!'";";D<M<M<O"PQ!!'";";DJJ"GH245r*   c                 X    | j                   st        d      | j                  \  }} || S )Nz,unpack_slice called but index is not a slice)rP  r   rO  )r<   slice_class
slice_argss      r   rW  zGetItemSource.unpack_slice  s0    "" !OPP"&**ZJ''r*   c                    t        | j                  t              r!t        dt	        | j                               | j
                  rdt        | j                         d       dS dt        | j                  d       dS )Nz.GetItemSource index must not be a Source, got {0}[TrD   rE   )r   rO  r   r   rH  rP  r)   rW  rB   s    r   rG   zGetItemSource._name_template  sy    
 djj&) @djjAQ@RS  HT%6%6%8TJK1MMHTZZDAB!DDr*   r   rH   )rI   rJ   rK   r   rL   rP  rM   r   r=   rR  rW  rR   rS   r'   rG   __classcell__)rU  s   @r   rN  rN    sL    J ND 86(e ( E E Er*   rN  c                   V    e Zd ZU eed<   ddZej                  defd       Z	de
fdZy)ConstDictKeySourcerO  r   Nc                     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   rI   r,  s   r   r   z0ConstDictKeySource.reconstruct.<locals>.<lambda>  s    G,,U^^=PQ r*   r   Fr   r   r9   rn   rO  r   r   r;   s    `r   r=   zConstDictKeySource.reconstruct  sU    Q	
 			g77

CD21e<=r*   c                 8    dt        | j                  d       dS )Nzlist(dict.keys({0}))[TrD   rE   r)   rO  rB   s    r   rG   z!ConstDictKeySource._name_template  s     )$**)N(OqQQr*   c                      yr   rT   rB   s    r   is_dict_keyzConstDictKeySource.is_dict_key  r   r*   rH   )rI   rJ   rK   r   rL   r=   rR   rS   r'   rG   rM   rh  rT   r*   r   r_  r_    s<    J> R R RT r*   r_  c                   ^    e Zd ZU eed<   ddZd	dZej                  de	fd       Z
defdZy)
NonSerializableSetGetItemSourcerO  r   Nc                 v    ddl m} |j                  | j                        st	        d| j                        y )Nr   ConstantVariablez=NonSerializableSetGetItemSource index must be a literal, got )	variablesrm  
is_literalrO  r   r<   rm  s     r   r   z-NonSerializableSetGetItemSource.__post_init__$  s:    /**4::6 OPTPZPZ~^  7r*   c                     j                  fd        | j                         j                  j                  | j                               j                  t        dd             y )Nc                  D     j                  t        j                  d      S )Nset_getitemrc  r,  s   r   r   z=NonSerializableSetGetItemSource.reconstruct.<locals>.<lambda>.  s    G,,U^^]K r*   r   Frd  r;   s    `r   r=   z+NonSerializableSetGetItemSource.reconstruct,  sU    K	
 			g77

CD21e<=r*   c                 8    dt        | j                  d       dS )Nz
list({0})[TrD   rE   rf  rB   s    r   rG   z.NonSerializableSetGetItemSource._name_template4  s     htzzdCDAFFr*   c                      y)NFrT   rB   s    r   rh  z+NonSerializableSetGetItemSource.is_dict_key9  s    r*   r   rH   )rI   rJ   rK   rq   rL   r   r=   rR   rS   r'   rG   rM   rh  rT   r*   r   rj  rj     sA    J> G G GT r*   rj  c                   b    e Zd ZU eed<   d
dZddZdddefdZe	j                  defd	       Zy)DictGetItemSourcerO  r   Nc                     ddl m} t        | j                  t              s4|j                  | j                        st        d| j                        y y )Nr   rl  zEDictGetItemSource index must be a ConstDictKeySource or literal, got rn  rm  r   rO  r_  ro  r   rp  s     r   r   zDictGetItemSource.__post_init__E  sR    /JJ*
"--djj9 WX\XbXbWef  :
r*   r5   r   c                     || j                          t        | j                  t              r || j                         n*|j	                  |j                  | j                               |j	                  t                      y r7   )r   r   rO  r   r9   rn   r   r;   s     r   r=   zDictGetItemSource.reconstructO  sY    		 djj&)DJJ!!'";";DJJ"GH245r*   c                     | j                   j                  |      }t        | j                  t              r| j                  j                  |      }nt        | j                        }| d| dS )NrG  rE   )r   r?   r   rO  r_  r&   )r<   r5   r   rO  s       r   r?   z$DictGetItemSource.reconstruct_pycodeZ  sZ    yy++G4djj"45JJ11':E$Eqq!!r*   c                     t        | j                  t              r#dt        | j                  j                         dS dt        | j                  d       dS )Nr\  rE   TrD   r   rO  r_  r)   r   rB   s    r   rG   z DictGetItemSource._name_templateb  sI    djj"45HTZZ__56a88HTZZDAB!DDr*   r   rH   )rI   rJ   rK   r   rL   r   r=   r'   r?   rR   rS   rG   rT   r*   r   rw  rw  >  sJ    
 J	6"+ "# " E E Er*   rw  c                   R    e Zd ZU eed<   ddZddZej                  de	fd       Z
y)	DictSubclassGetItemSourcerO  r   Nc                     ddl m} t        | j                  t              s4|j                  | j                        st        d| j                        y y )Nr   rl  zMDictSubclassGetItemSource index must be a ConstDictKeySource or literal, got ry  rp  s     r   r   z'DictSubclassGetItemSource.__post_init__s  sR    /JJ*
"--djj9 _`d`j`j_mn  :
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_getitemrc  r,  s   r   r   z7DictSubclassGetItemSource.reconstruct.<locals>.<lambda>      G,,U^^^L r*   r   F)	r   r   r   rO  r   r9   rn   r   r   r;   s    `r   r=   z%DictSubclassGetItemSource.reconstruct}  ss     	L	

 			 djj&)DJJ!!'";";DJJ"GH21e<=r*   c                     t        | j                  t              r#dt        | j                  j                         dS dt        | j                  d       dS )Nzdict.__getitem__({0}, r   r\  TrD   rE   r}  rB   s    r   rG   z(DictSubclassGetItemSource._name_template  sI    djj"45-htzz.G-HJJHTZZDAB!DDr*   r   rH   )rI   rJ   rK   r   rL   r   r=   rR   rS   r'   rG   rT   r*   r   r  r  l  s7    
 J>& E E Er*   r  c                   B    e Zd ZdZddZej                  defd       Zy)ListGetItemSourcezY
    Same as GetItemSource with reconstruct and name overridden to be list specific.
    r   Nc                 
   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_getitemrc  r,  s   r   r   z/ListGetItemSource.reconstruct.<locals>.<lambda>  r  r*   >List[slice] is a temporary object and should not have a sourcer   F)	r   r   rP  RuntimeErrorr9   rn   rO  r   r   r;   s    `r   r=   zListGetItemSource.reconstruct  st    
 	L	

 			 P  !!'";";DJJ"GH21e<=r*   c                     t        | j                  t              r!t        dt	        | j                               | j
                  rt        d      dt        | j                  d       dS )Nz2ListGetItemSource index must not be a Source, got r  zlist.__getitem__({0}, TrD   r   )r   rO  r   r   rH  rP  r  r)   rB   s    r   rG   z ListGetItemSource._name_template  sl    
 djj&) DT$**EUDVW  P  .htzzd.S-TTUVVr*   rH   )	rI   rJ   rK   r   r=   rR   rS   r'   rG   rT   r*   r   r  r    s0    >, W W Wr*   r  c                   >    e Zd ZddZej
                  defd       Zy)TupleIteratorGetItemSourcer   Nc                     j                  fd        | j                         j                  j                  | j                               j                  t        dd             y )Nc                  D     j                  t        j                  d      S )Ntuple_iterator_getitemrc  r,  s   r   r   z8TupleIteratorGetItemSource.reconstruct.<locals>.<lambda>  s    G,,U^^=UV r*   r   Frd  r;   s    `r   r=   z&TupleIteratorGetItemSource.reconstruct  sU    V	
 			g77

CD21e<=r*   c                 8    dt        | j                  d       dS )Nz___tuple_iterator_getitem({0}, TrD   r   rf  rB   s    r   rG   z)TupleIteratorGetItemSource._name_template  s#     0PT0U/VVWX	
r*   rH   )rI   rJ   rK   r=   rR   rS   r'   rG   rT   r*   r   r  r    s(    > 
 
 
r*   r  c                   *    e Zd ZddZedefd       Zy)NamedTupleFieldsSourcer   Nc                 h     || j                          |j                  |j                  d             y )N_fieldsr   r;   s     r   r=   z"NamedTupleFieldsSource.reconstruct  r   r*   c                      y)Nz___namedtuple_fields({0})rT   rB   s    r   rG   z%NamedTupleFieldsSource._name_template  s    *r*   rH   r   rT   r*   r   r  r    s#    D + + +r*   r  c                   *    e Zd ZddZedefd       Zy)DataclassFieldsSourcer   Nc                     j                  fd        | j                         j                  t        dd             y )Nc                  D     j                  t        j                  d      S )Ndataclass_fieldsrc  r,  s   r   r   z3DataclassFieldsSource.reconstruct.<locals>.<lambda>  s    G,,U^^=OP r*   r   Fr-  r;   s    `r   r=   z!DataclassFieldsSource.reconstruct  s8    P	
 			21e<=r*   c                      y)Nz___dataclass_fields({0})rT   rB   s    r   rG   z$DataclassFieldsSource._name_template  s    )r*   rH   r   rT   r*   r   r  r    s"    > * * *r*   r  c                   2    e Zd ZddZddZedefd       Zy)
TypeSourcer   Nc                 2    | j                   t        d      y )Nz TypeSource base must not be Noner  rB   s    r   r   zTypeSource.__post_init__  s    99 !CDD r*   c                     j                  fd        | j                         j                  t        dd             y )Nc                  (     j                  dd      S )Nr*  rH  r+  r,  s   r   r   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                      y)Nz	type({0})rT   rB   s    r   rG   zTypeSource._name_template  s    r*   r   rH   r  rT   r*   r   r  r    s(    E>
   r*   r  c                   *    e Zd ZddZedefd       Zy)OptimizerSourcer   Nc                 (     || j                          y r7   r   r;   s     r   r=   zOptimizerSource.reconstruct  r"  r*   c                      yr   rT   rB   s    r   rG   zOptimizerSource._name_template  r   r*   rH   r   rT   r*   r   r  r    s"       r*   r  c                   d    e Zd Zd	dZdddefdZej                  defd       Z	e
defd       Zy)
NNModuleSourcer5   r   r   Nc                 (     || j                          y r7   r   r;   s     r   r=   zNNModuleSource.reconstruct  r"  r*   c                 8    | j                   j                  |      S r7   )r   r?   r;   s     r   r?   z!NNModuleSource.reconstruct_pycode  s    yy++G44r*   c                 <    t         | j                  j                     S r7   r   rB   s    r   r   zNNModuleSource.guard_source  r   r*   c                      yr   rT   rB   s    r   rG   zNNModuleSource._name_template  r   r*   rH   )rI   rJ   rK   r=   r'   r?   rR   rS   r
   r   rQ   rG   rT   r*   r   r  r  	  sY    5+ 5# 5 Kk K K   r*   r  c                   6    e Zd Zej                  defd       Zy)UnspecializedNNModuleSourcer   c                 <    t         | j                  j                     S r7   )%_GUARD_SOURCE_UNSPECIALIZED_NN_MODULEr   r   rB   s    r   r   z(UnspecializedNNModuleSource.guard_source  s    4TYY5K5KLLr*   Nr   rT   r*   r   r  r    s$    Mk M Mr*   r  c                   6    e Zd Zej                  defd       Zy)"UnspecializedBuiltinNNModuleSourcer   c                 <    t         | j                  j                     S r7   )-_GUARD_SOURCE_UNSPECIALIZED_BUILTIN_NN_MODULEr   r   rB   s    r   r   z/UnspecializedBuiltinNNModuleSource.guard_source#  s    <TYY=S=STTr*   Nr   rT   r*   r   r  r  !  s$    Uk U Ur*   r  c                   6    e Zd Zej                  defd       Zy)FSDPNNModuleSourcer   c                 <    t         | j                  j                     S r7   )_GUARD_SOURCE_FSDP_MODULEr   r   rB   s    r   r   zFSDPNNModuleSource.guard_source*  s    ()?)?@@r*   Nr   rT   r*   r   r  r  (  s$    Ak A Ar*   r  c                   8    e Zd Zedefd       Zedefd       Zy)GlobalStateSourcer   c                      yNr   rT   rB   s    r   rG   z GlobalStateSource._name_template1      r*   c                 "    t         j                  S r7   r}   rB   s    r   r   zGlobalStateSource.guard_source5  r   r*   NrI   rJ   rK   rQ   r'   rG   r
   r   rT   r*   r   r  r  /  s6       "k " "r*   r  c                   l    e Zd ZU dZeed<   d	dZej                  defd       Z	d
dZ
edefd       Zy)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                 V    ddl m}m}  || j                  |j                               y )Nr   )GuardBuilderinstall_guard)guardsr  r  r   ID_MATCH)r<   r  r  s      r   r   zImportSource.__post_init__A  s    7dool&;&;<=r*   c                 "    d| j                    dS )Nz__import__('z'))r  rB   s    r   rG   zImportSource._name_templateF  s    d../r22r*   c                     |j                  |j                  d      t        d      |j                  | j                        g       y )Nr   )r   rn   r   create_import_namer  r;   s     r   r=   zImportSource.reconstructJ  s?    ))!,"1%**4+;+;<	
r*   c                 "    t         j                  S r7   r}   rB   s    r   r   zImportSource.guard_sourceS  r   r*   r   rH   )rI   rJ   rK   r   r'   rL   r   rR   rS   rG   r=   rQ   r
   r   rT   r*   r   r  r  :  sV    P >
 3 3 3
 "k " "r*   r  c                   l    e Zd ZU eed<   ej                  defd       ZdefdZ	ddZ
edefd       Zy)	TorchFunctionModeStackSourceindr   c                 <    dt        | j                                dS )Nz$___get_torch_function_mode_stack_at(r   )r)   
_get_indexrB   s    r   rG   z+TorchFunctionModeStackSource._name_template\  s    5ht?P6Q5RRSTTr*   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_index`  s    L-<<TXXFFr*   Nc                     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_atrc  r,  s   r   r   z:TorchFunctionModeStackSource.reconstruct.<locals>.<lambda>g  s    G,, B r*   r   F)r   r   rn   r  r   r;   s    `r   r=   z(TorchFunctionModeStackSource.reconstructe  sQ    	

 	w889JKLM21e<=r*   c                 "    t         j                  S r7   r}   rB   s    r   r   z)TorchFunctionModeStackSource.guard_sourcen  r   r*   rH   )rI   rJ   rK   rq   rL   rR   rS   r'   rG   r  r=   rQ   r
   r   rT   r*   r   r  r  X  sW    	HU U UGC G
> "k " "r*   r  c                   p    e Zd ZU eed<   d	dZedefd       Ze	j                  defd       ZdedefdZy)
r   source_namer   Nc                 \    |j                  |j                  | j                  d             y )NFrw   )r9   rz   r  r;   s     r   r=   zConstantSource.reconstructw  s&    g889I9Iu8UVr*   c                 "    t         j                  S r7   )r
   r   rB   s    r   r   zConstantSource.guard_sourcez  s    ###r*   c                     | j                   S r7   )r  rB   s    r   rG   zConstantSource._name_template~  s    r*   r   c                     t         r7   r]   r   s     r   r   zConstantSource.make_guard  r   r*   rH   )rI   rJ   rK   r'   rL   r=   rQ   r
   r   rR   rS   rG   r   r   rT   r*   r   r   r   s  s]    W $k $ $      "S "S "r*   r   c                   *    e Zd Zedefd       ZddZy)NumpyTensorSourcer   c                      y)Nz___from_numpy({0})rT   rB   s    r   rG   z NumpyTensorSource._name_template  s    #r*   Nc                     j                  fd        | j                         j                  t        dd             y )Nc                  (     j                  dd      S )Ntorch	as_tensorr+  r,  s   r   r   z/NumpyTensorSource.reconstruct.<locals>.<lambda>  s    g&>&>w&T r*   r   Fr-  r;   s    `r   r=   zNumpyTensorSource.reconstruct  s4    TU		21e<=r*   rH   )rI   rJ   rK   rQ   r'   rG   r=   rT   r*   r   r  r    s     $ $ $>r*   r  c                   "    e Zd Zedefd       Zy)SubclassAttrListSourcer   c                      y)Nz{0}.__tensor_flatten__()[0]rT   rB   s    r   rG   z%SubclassAttrListSource._name_template  s    ,r*   NrI   rJ   rK   rQ   r'   rG   rT   r*   r   r  r    s    - - -r*   r  c                   "    e Zd Zedefd       Zy)FloatTensorSourcer   c                      y)Nz___as_tensor({0})rT   rB   s    r   rG   z FloatTensorSource._name_template  s    "r*   Nr  rT   r*   r   r  r    s    # # #r*   r  c                   "    e Zd Zedefd       Zy)CallMethodItemSourcer   c                      y)Nz
{0}.item()rT   rB   s    r   rG   z#CallMethodItemSource._name_template  s    r*   Nr  rT   r*   r   r  r    s      r*   r  c                   8    e Zd Zedefd       Zedefd       Zy)ShapeEnvSourcer   c                      yr  rT   rB   s    r   rG   zShapeEnvSource._name_template  r  r*   c                 "    t         j                  S r7   )r
   	SHAPE_ENVrB   s    r   r   zShapeEnvSource.guard_source  r   r*   Nr  rT   r*   r   r  r    s6       %k % %r*   r  c                   `    e Zd ZU eed<   ej                  defd       ZddZ	e
defd       Zy)CurrentStreamSourcer   r   c                     dt        | j                  j                         dt        | j                  j                         dS )Nz$___get_current_stream(torch.device('z', z)))r)   r   rH  rO  rB   s    r   rG   z"CurrentStreamSource._name_template  s=    5ht{{?O?O6P5QQTU]^b^i^i^o^oUpTqqsttr*   Nc                    d}j                  fd       j                  fd       j                  j                  | j                  j                        g       | j                  j
                  :|dz  }j                  j                  | j                  j
                        g       j                  t        |d             j                  t        dd             y )Nr   c                  D     j                  t        j                  d      S )Nget_current_streamrc  r,  s   r   r   z1CurrentStreamSource.reconstruct.<locals>.<lambda>  s    G,,U^^=QR r*   c                  (     j                  dd      S )Nr  r   r+  r,  s   r   r   z1CurrentStreamSource.reconstruct.<locals>.<lambda>  s    g&>&>w&Q r*   F)r   r   rn   r   rH  rO  r   )r<   r5   num_argss    ` r   r=   zCurrentStreamSource.reconstruct  s    R	
 	QRw889I9IJKL;;(MH!!7#<#<T[[=N=N#O"PQ28UCD21e<=r*   c                 "    t         j                  S r7   r}   rB   s    r   r   z CurrentStreamSource.guard_source  r   r*   rH   )rI   rJ   rK   device_typerL   rR   rS   r'   rG   r=   rQ   r
   r   rT   r*   r   r  r    sJ    u u u> "k " "r*   r  c                   8    e Zd Zedefd       Zedefd       Zy)BackwardStateSourcer   c                      yr  rT   rB   s    r   rG   z"BackwardStateSource._name_template  r  r*   c                 "    t         j                  S r7   )r
   BACKWARD_STATErB   s    r   r   z BackwardStateSource.guard_source  s    )))r*   Nr  rT   r*   r   r  r    s6       *k * *r*   r  only_allow_inputr  c                    t        | t              rt        | j                  |      S t        | t              sy |r| j
                  sy | j                  S Nr  )r   r   get_local_source_namer   r-   r/   r.   r   r  s     r   r
  r
    sE     &-($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 r7   )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 r7   )r   r   r  r   rs   rt   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 r7   )r   r   is_from_nonlocal_sourcer   r-   r1   r/   r   s    r   r  r    sG    &-(&v{{336;' 	 ++	 r*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   r   r   is_from_closure_sourcer   r   s    r   r  r    s+    &-(&-(%fkk22r*   targetc                 \    | |k(  ryt        | t              rt        | j                  |      S yr   )r   r   is_from_sourcer   )r   r  s     r   r  r    s+    &-(fkk622r*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   r  r   &is_from_unspecialized_nn_module_sourcer   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   r  r   .is_from_unspecialized_builtin_nn_module_sourcer   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_sourcer   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   r0  r   $is_from_flatten_script_object_sourcer   r   s    r   r   r   4  s,    &34	FM	*3FKK@@r*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   r  r   is_from_optimizer_sourcer   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)NTrE  rF  F)	r   r>  rw  r   r   r   rN  r   is_from_defaultsr   s    r   r$  r$  H  s    &.) 	6,-v{{J/KK"22 	6=)v{{J/KK.0&-(,,r*   c                 p    t        | t              ryt        | t              rt        | j                        S yr   )r   r   r   is_from_skip_guard_sourcer   r   s    r   r&  r&  b  s+    &/*&-((55r*   )F)vr   rO   enumrR   collections.abcr   typingr   r   r  r   r   torch._guardsr   r   r	   r
   r   r   r   bytecode_transformationr   r   r   r5   r   rA   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   r  r  r  rM   r   r'   r    r)   r-   rV   r_   rf   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Enumr   r  r  r  r  r%  r0  r5  r:  r>  rN  r_  rj  rw  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  	lru_cacher
  r  r  r  r  r  r  r  r  r  r   r"  r$  r&  rT   r*   r   <module>r6     s  &    $ % '    " {>>@@++[-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 	v 	$ 	&6D= &S &3 3 3# 3 4(,B& ,B ),B^ 4(
f 
 )
$ 4(P6 P )P 4(B B )B" 4(C6 C )C$ 4(E& E )E( 4(  ) 4(	0 	 )	 4(0 0 )0@ 4(  )& 4(
 
 )
4 4($] $ )$ 4(M  ) 4(Lf L )L& 4(  ) 4(!M ! )! 4(	0 	0 )	0 4(K
 K )K 4(	Z 	 )	 4(f  )& 4(m  )@TYY @  4(-E= -E )-E` 4(0M 0 )0 4(=  ) 4(
7} 
7 )
7 4(-  )* 4(
' 
' )
' 4(.m . ). m   4((] ( )(V 4(&EM &E )&ER 4(  )( 4(m  ): 4((E (E )(EZ 4((E (E )(EV 4()W )W ))WX 4(
 
 )
  4(+] + )+ 4(
*M 
* )
* 4(  ) 4(m  ) 4(]  )  4(M. M )M 4(U)D U )U 4(A A )A 4(" " )" 4("6 " )": 4("6 " )"4 4("V " )"$ 4(> > )> 4(-] - )- 4(# # )# 4(=  ) 4(%V % )% 4("& " )"2 4(*& * )* 05		)-	4Z	 	 EJ X Xd Xt X X 6& 6T 6 6 6D= S4Z   F t   6 d   6 6 d   6 d   6 d   f     D   V    V   2 f   r*   