
    9j{                    <   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZmZ ddlmZ ddlmZ ddlmZmZmZmZmZ ddlZddlmZ ddlmc m Z! ddl"m#Z# ddl$m%Z% dd	l&m'Z' d
dl(m)Z)m*Z*m+Z+m,Z, d
dl-m.Z.m/Z/m0Z0 d
dl1m2Z2 d
dl3m4Z4m5Z5m6Z6 d
dl7m8Z8m9Z9 d
dl:m;Z; d
dl<m=Z=m>Z>m?Z?m@Z@mAZAmBZB d
dlmCZCmDZDmEZEmFZFmGZGmHZHmIZI ddlJmKZKmLZLmMZM ddlNmOZO ddlPmQZQmRZR ddlSmTZTmUZUmVZV erddlWmXZX ddlYmZZZ  G d deM      Z[ G d deM      Z\ G d deM      Z] G d  d!eM      Z^ G d" d#eM      Z_ G d$ d%e_      Z` G d& d'eM      Za G d( d)eM      Zb G d* d+eM      Zcd,ed-ed.ef   fd/Zd G d0 d1eM      Zeej                   G d2 d3             Zg G d4 d5eV      Zh G d6 d7eV      Zi G d8 d9eM      Zj G d: d;eM      Zk G d< d=eM      Zl G d> d?eM      Zm G d@ dAeM      Zn G dB dCeM      Zo ej                  dD      d-eqej$                  ej                  f   fdE       Zs ej                  dD      d-eqej                  ej$                  f   fdF       Zt G dG dHeM      Zu G dI dJeM      Zv G dK dLeM      Zw G dM dNeM      Zx G dO dPeM      Zy G dQ dReM      Zz G dS dTeM      Z{ G dU dVeM      Z| G dW dXeM      Z} G dY dZe}      Z~ G d[ d\e}      Zej                   e}ej                  e}ej                  eiZ G d] d^eM      Z G d_ d`eM      Z G da dbeM      Zy)ca  
This module contains miscellaneous variable tracker implementations for various Python types
and features used in Dynamo's symbolic execution. These classes help track and propagate
information about different kinds of variables during graph capture.

Key classes include:
- SuperVariable: Handles super() calls and method resolution
- ExceptionVariable: Tracks exception objects
- RandomVariable: Manages random number generators
- GetAttrVariable: Tracks attribute access
- MethodWrapperVariable: Handles method wrappers
- PythonModuleVariable: Tracks Python modules
- NumpyVariable: Handles numpy functions and types
- StringFormatVariable: Manages string formatting
- DebuggingVariable: Handles print and logging
    N)CallableSequence)Random)BuiltinFunctionType)AnyLiteralTYPE_CHECKING	TypeGuardUnion)MutationType)TupleVariable)Source   )configgraph_break_hintstrace_rules	variables)create_call_functioncreate_call_function_excreate_instruction)$do_not_convert_to_tracable_parameter)raise_observed_exceptionraise_type_errorunimplemented)GuardBuilderinstall_guard)unpatched_nn_module_init)
AttrSourceGenericAttrSourceGetItemSourceTypeMROSource
TypeSourceWeakRefCallSource)check_unspec_or_constant_argscmp_name_to_op_mappingidentityis_tensor_base_attr_getteristypeproxy_args_kwargsraise_args_mismatch   )#AsPythonConstantNotImplementedErrorNO_SUCH_SUBOBJVariableTracker)ConstantVariable)NestedUserFunctionVariableUserFunctionVariable)call_random_fnis_standard_setattrUserDefinedObjectVariable)	PyCodegen)InstructionTranslatorc            
            e Zd ZeZh ej                  Z	 ddededz  deddf fdZde	fdZ
d fdZd	d
dedeeedz  f   f fdZd	d
dedefdZd	d
dedee   deeef   def
dZ xZS )SuperVariableNtypevarobjvarkwargsreturnc                 @    t        |   di | || _        || _        y N )super__init__r9   r:   )selfr9   r:   r;   	__class__s       \/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_dynamo/variables/misc.pyrA   zSuperVariable.__init__Y   s'     	"6" 
     c                 "    t         j                  S N)builtinsr@   rB   s    rD   python_typezSuperVariable.python_typej   s    ~~rE   c                     j                  fd        | j                         | j                  . | j                         j                  t	        dd             y j                  t	        dd             y )Nc                  B     t        j                  t                    S rG   )r   BuiltinVariabler@   )rC   codegens   rD   <lambda>z+SuperVariable.reconstruct.<locals>.<lambda>n   s    gi.G.G.N&O rE   r   Fr+   )add_push_nullr9   r:   extend_outputr   rB   rN   rC   s    `rD   reconstructzSuperVariable.reconstructm   s`    OP;;"DKK !!"6q%"@A!!"6q%"@ArE   txr6   namec                    | j                   s.t        ddd| d| j                   d| j                    ddg       | j                   J | j                  j                         }| j                   j	                         }| j                   j
                  rt        | j                   j
                        nd }t        |t              r,| j                   j                  }| j                   j
                  }d }|j                  }	 |j                  |      d	z   }t        |t!        |            D ]W  }	||	   j"                  j%                  |t&              x}
s)|
t&        us2|rt)        t+        t-        |      |	      |      }|
|fc S  t        d
dd| d| j                   d| j                    dddg       y # t        $ r t        t        ||      |      d fcY S w xY w)Nz1-arg super not implemented z"Dynamo failed to trace attribute `z$` accessed via `super()` (for type `z` and object `z4`) because one-argument of super() is not supported.z-Use two-argument super(type, object_or_type).gb_typecontextexplanationhintsr+   zUnable to resolve super getattrz8`) because the resolved attribute type is not supported.z0Ensure the attribute exists in the parent class.z(Check the arguments passed to `super()`.)r:   r   r9   as_python_constantrJ   sourcer"   
issubclasstypevalue__mro__index
ValueErrorgetattrr@   rangelen__dict__getr-   r   r    r!   )rB   rT   rU   search_typetype_to_usetype_to_use_sourcer^   
search_mrostart_indexrc   resolved_getattrrC   s              rD   _resolved_getattr_and_sourcez*SuperVariable._resolved_getattr_and_sourcev   s    {{5@ G,,0LL>} UDD D	 {{&&&ll557 kk--/.2kk.@.@Jt{{))*d 	 k4(++++K!%!3!3 ((
	H$**;7!;K ;J8 
	4E#-e#4#=#=#A#A$#WWW#>9)!+)-8J*KUS " ,V33
	4 	5<TF C((,~^DKK= QDD C:
	
'  	H 5k:DA4GG	Hs    F3 3!GGc                 .   | j                  ||      \  }}t        j                  j                  |      st	        | |t        |            S |r(t        |j                  t        j                               t        j                  j                  ||      S )Npy_typer^   )rp   r   r/   
is_literalGetAttrVariabler`   r   
make_guardr   CONSTANT_MATCHcreate)rB   rT   rU   ra   r^   s        rD   var_getattrzSuperVariable.var_getattr   sy     99"dCv))44U;"4tE{CC&++L,G,GHI))00v0FFrE   argsc                    | j                  ||      \  }}| j                  J |t        j                  u rt	        t
              S |t        j                  j                  j                  u r| j                  }ddl	m
} t        |t        j                        rkt        |j                  |      rU|sS|sQt               5  t!        j"                  |t$        |      }	|	j'                  || j                  g|z   |      cd d d        S t)        dd|  d| d| d| d| dg t*        j,                  	       n| j                  j.                  rt1        |d
      r|j2                  dk(  rt        j4                  j7                  |      ry|j8                  }
t1        |
d      r&|
j:                  dk(  rt!        j"                  ||
      }n'|J |j<                  }t        j4                  |
|      }|j?                  |d||      S t        |t@              rZt        |jB                  tD        jF                        r6t!        j"                  ||jB                  |d      }	|	j'                  |||      S t        |tH              rt        |jB                  tD        jF                        rt        | j                  t        j4                        r| j                  }nbd }| j                  j.                  rtK        | j                  j.                        }t!        j"                  || j                  jL                  |      }|J t!        j"                  ||jB                  tO        |d      d      }	|	j'                  ||g||      S t        |tD        jF                        r:t!        j"                  |||d      }	|	j'                  || j                  g|z   |      S t        |tD        jP                        r=t        jR                  |jB                  | j                  |      j'                  |||      S tU        |      r:t        | j                  t              r  | j                  jV                  |g|i |S |t        jX                  u r|d   }	 |j[                         }|j^                  j                  ja                  | j                        s4t)        dd|  d| d| j                   ddgt*        jb                  	       t        |td              sJ |j^                  j                  jg                  | j                  |t        jh                                t        jj                  jm                  d       S t        | j                  t        j                        rl| j                  jn                  V| j                  jp                  @|| j                  jp                  v r(| j                  jn                  j?                  ||||      S |t        jr                  u r|d   jt                  }|j^                  j                  jw                  | j                  |      r^|j^                  j                  jy                  | j                  |d      }t        |t        jh                        rt{        t|        |       |S d }	 t        js                  | j                  jt                  |      }d }| j                  j.                   t        | j                  j.                  |      }t!        j"                  |||      S |t        j                  j                  u r|d   }i }|d   j                  }|d   j                  j                         D ]$  \  }}|j                  j[                         }|||<   & |j                  j                  }d|j                  _E        	 |j'                  |||      ||j                  _E        S t        |tD        j                        rP|t        j                         v r:t!        j"                  |||d      }|j'                  || j                  g|z   |      S t)        dd|  d| d| d | d!d"g	       y # 1 sw Y   'xY w# t\        $ r"}t)        dd|  d| ddg|       Y d }~d }~ww xY w# t|        $ r t{        t|        |       Y w xY w# ||j                  _E        w xY w)#Nr   )AttributeMutationNewrt   z#Unsupported super().__init__() callcall_method  z0Dynamo encountered a super().__init__() call on zK that resolved to a `torch.nn.Module.__init__()` call that we cannot trace.rX   __name____new__
__module__rH   Tr^   realize__func__r   z7Non-constant attribute given to `super().__delattr__()`zbDynamo requires the attribute name passed to `super().__delattr__(...)` to be a constant (string).zEEnsure the attribute name is a string literal or a constant variable.)rY   rZ   r[   r\   from_exczFAttempted super().__delattr__() on an object without mutation trackingzmDynamo needs to track mutations on an object before `super().__delattr__` can be used on it. But the object (z3) doesn't have attribute mutation tracking enabled.z<Ensure the object is tracked by Dynamo's side effect system.)
deleted_ok   F)r   zFAttempted to call a super() attribute that is not a function or methodz4Dynamo does not know how to trace the call `super().z()` because `super().z(` is not a function or method attribute.zMEnsure the attribute accessed via `super()` is a standard method or function.)Irp   r:   objectrA   LambdaVariabler&   torchnnModuleside_effectsr}   
isinstancer   r4   mutation_typer   r.   buildr   call_functionr   r   	DIFFICULTr^   hasattrr   UserDefinedClassVariableis_supported_new_method__self__r   membercall_methodstaticmethodr   typesFunctionTypeclassmethodr"   
value_typer   
MethodTypeUserMethodVariabler3   method_setattr_standard__delattr__r]   NotImplementedErroroutputis_attribute_mutation
DYNAMO_BUGstr
store_attrDeletedVariabler/   ry   _base_vt_base_methods__getattribute__ra   has_pending_mutation_of_attr	load_attrr   AttributeErrorr   _C_disabled_torch_function_implitemsvtsymbolic_torch_function_statetorch_function_subclass_enabledMethodDescriptorTyper   get_tensor_method)rB   rT   rU   r{   r;   inner_fnr^   r:   r}   fn_vtuser_clsuser_cls_vtuser_cls_sourcecls_variable
cls_sourceattrexc	attr_nameresult
attr_valueattr_sourcefunc	tf_kwargstf_argshash_key_vtvalue_vtkey_strtx_oldfn_vars                                rD   r   zSuperVariable.call_method   s     <<RF&{{&&& v&!(++111[[F; 69#F#FGv335IJ9; Q+114VE !..rDKK=43GP	Q Q A*4&$qaxH!  "1!1 9-778 KK*-!!Y.22JJ8T((Hx.83F3F*3T/>/D/DR/R)))"(--'@@_ **2y$GG,/Ju115
 $))H%%fdE &&r488+.:u114
 $++y'I'IJ  ${{ "
;;%%!+DKK,>,>!?J.44KK** 
 %%%#))!!!&*5	E &&rL+@4+@&II%"4"45#))"hvtTE&&rDKK=4+?HH%"2"23//!!4;;vmBf-. !*zKK20
 74;;66rKDKFKK+++7D..0 99))??Ld*4&$8!#{{m ,(!(
 W*55 dC(((II""--T9#<#<#> --44T::t{{I$G$GH$$0))5DKK555;;''33BdFKK000 QIyy%%BBY //99KKt :  fi&?&?@,^R@J= $44T[[5F5F	R
 K{{!!-/0B0BIN"((ZEE???
 7DI1gmmG)-a)<)<)> .%X%..;;=%-	'". 55UUFOTB,,L))"gyA  00P x!;!;<K99;; %**2xNF''T[[MD,@&II'"4&$0v24& 9,, `
	
eQ Qh ' 
U*4&$8!L ` !	 	
n " =(<=8  00PsB   ;=a)<a5 &*b# =c )a25	b >bb #c ?c crG   rN   r5   r<   N)r   r   __qualname__r@   _cpython_typer.   _nonvar_fieldsr   rA   r`   rJ   rS   r   tupler   rp   rz   listdictr   __classcell__rC   s   @rD   r8   r8   Q   s    M		'	'N *.   $& 	
 
"T BB
)B
14B
	sJ%%	&B
HG5 GS G_ G X
#X
 X
 ?#	X

 S/)*X
 
X
rE   r8   c                   d     e Zd Zdej                  deddf fdZdefdZddd	e	de
f fd
Z xZS )FrameSummaryVariableframe_summaryr;   r<   Nc                 2    t        |   di | || _        y r>   )r@   rA   r   )rB   r   r;   rC   s      rD   rA   zFrameSummaryVariable.__init__  s    "6"*rE   c                 "    t         j                  S rG   )	tracebackFrameSummaryrI   s    rD   rJ   z FrameSummaryVariable.python_type  s    %%%rE   rT   r6   rU   c                    |dk(  r*t        j                  || j                  j                        S |dk(  r*t        j                  || j                  j                        S |dk(  r*t        j                  || j                  j
                        S |dk(  r*t        j                  || j                  j                        S t        | !  ||      S )NlinenofilenamerU   line)	r.   r   r   r   r   rU   r   r@   rz   rB   rT   rU   rC   s      rD   rz   z FrameSummaryVariable.var_getattr  s    8"((T-?-?-F-FGGZ"((T-?-?-H-HIIV^"((T-?-?-D-DEEV^"((T-?-?-D-DEEw"2t,,rE   )r   r   r   r   r   r   rA   r`   rJ   r   r.   rz   r   r   s   @rD   r   r     sP    +i&<&< + +PT +&T &	-5 	-S 	-_ 	- 	-rE   r   c            
       R    e Zd Zdeded ef   deddf fdZede	j                  ded ef   dd fd       Zededefd	       Zdee	j                  ez     fd
ZdedefdZdeej*                     fdZdddededefdZdddedef fdZdddedee   deeef   def
 fdZ xZS )TracebackVariabler   tb_nextr;   r<   Nc                 H    t        |   di | || _        |J || _        y r>   )r@   rA   r   r   )rB   r   r   r;   rC   s       rD   rA   zTracebackVariable.__init__  s0     	"6"*"""rE   c                 (     | t        |      |      S )N)r   )r   )clsr   r   s      rD   from_frame_summaryz$TracebackVariable.from_frame_summary  s     '6HHrE   objc                 F    t        | t              xs | j                         S rG   )r(   r   is_constant_noner   s    rD   is_valid_tracebackz$TracebackVariable.is_valid_traceback  s    c,-G1E1E1GGrE   c                     t        | j                  t              r| j                  gS | j                  g| j                  j	                         z   S rG   )r(   r   r/   r   
extract_tbrI   s    rD   r   zTracebackVariable.extract_tb  sA    $,, 01&&''""#dll&=&=&???rE   tbc                 l    | }t        |t              r"||u ry|j                  }t        |t              r"y)NTF)r(   r   r   )rB   r   curr_tbs      rD   has_reference_cyclez%TracebackVariable.has_reference_cycle  s7    8<W/0"}ooG W/0 rE   c                 "    t         j                  S rG   )r   TracebackTyperI   s    rD   rJ   zTracebackVariable.python_type  s    """rE   rT   r6   name_varvalc                 n   |j                         }|dk(  r| j                  |      st        t        |       t	        |t
        t        f      sJ | j                  |      s!t        |t
              r!|j                  |       rt        t        |       || _
        t        j                  j                  d       S )Nr   )r]   r   r   	TypeErrorr   r   r/   r   r(   rd   r   r   ry   rB   rT   r   r   rU   s        rD   call_setattrzTracebackVariable.call_setattr  s     **,9**3/(B7c$57G#HIII'',s-.33J3J43P(R8DL))0066rE   rU   c                     |dk(  r| j                   S |dk(  r| j                  j                  |d      S |dk(  r| j                  S |dk(  r"t        d|  ddg t        j
                  	       t        | 	  ||      S )
Nr   	tb_linenor   r   tb_lastiz traceback.tb_lasti not supportedz accessing 'tb_lasti'zNDynamo does not support accessing the tb_lasti attribute of traceback objects.rX   )r   r   rz   r   r   SUPPORTABLEr@   r   s      rD   rz   zTracebackVariable.var_getattr  s    9<<[ %%11"h??_$%%%Z:& 56l6)556	 w"2t,,rE   r{   c                     |dk(  rt        j                  || |d   u       S |dk(  r | j                  |g| S t        |   ||||      S )N__eq__r   __setattr__)r.   r   r   r@   r   )rB   rT   rU   r{   r;   rC   s        rD   r   zTracebackVariable.call_method  s^     8"((TT!W_==]"$4$$R/$//w"2tT6::rE   )r   r   r   r   r   r/   r   rA   r   r   r   r   r   r.   boolr   r   r   r   r`   r   r   rJ   r   r   rz   r   r   r   r   s   @rD   r   r     s   + *,<<= 	
 
$ I --I *,<<=I 
	I I H HD H H@D!7!7:N!NO @
o $ #T%"5"56 #7#7 "7 	7
 
7$-5 -S -_ - ;#; ; ?#	;
 S/)*; 
; ;rE   r   c                        e Zd ZeZ	 	 	 ddedee   dee	ef   dz  de
dz  dedz  ddf fdZd	eddfd
ZddZdefdZdddededefdZddde	dee   dee	ef   def
 fdZddde	def fdZde	fdZeZedede	fd       Zde	fdZ xZS )ExceptionVariableNexc_typer{   init_kwargsr^   r   r<   c                 n   t         |   ||       || _        || _        |r$t	        d|  d| dg t
        j                         t        j                  d       | _	        t        j                  d       | _
        t        j                  d      | _        t        j                  d       | _        d | _        y )N)r^   r   z,Keyword args passed to exception constructorz with kwargs zRDynamo does not know how to handle keyword args passed to an exception constructorrX   F)r@   rA   r  r{   r   r   r   r/   ry   __context__	__cause____suppress_context____traceback__python_stack)rB   r  r{   r  r^   r   rC   s         rD   rA   zExceptionVariable.__init__  s     	mD 	F&k];p6)556	 -=,C,CD,I +;*A*A$*G5E5L5LU5S!.>.E.Ed.K <@rE   rZ   c                     || _         y rG   )r	  )rB   rZ   s     rD   set_contextzExceptionVariable.set_context@  s
    "rE   c                      j                   fd       j                   j                         j                  t	         j                        d       dt
        dd f fd} |d        |d        |d       y )	Nc                  P     j                  dj                  j                        S )NrH   )load_import_fromr  r   rN   rB   s   rD   rO   z/ExceptionVariable.reconstruct.<locals>.<lambda>E  s    G,,Z9O9OP rE   FrU   r<   c                     t        |       }t        |t              r|j                  dv sJ |       y j	                           |       j                  j                  d             j                  |        y )N)TFNr   )re   r(   r/   ra   dup_toprQ   rot_nr   )rU   r   rN   rB   s     rD   codegen_attrz3ExceptionVariable.reconstruct.<locals>.codegen_attrJ  sh    4&Dd,-zz%88>$>8!%%gmmA&67""4(rE   r	  r
  r  )rP   foreachr{   r   rg   r   )rB   rN   r  s   `` rD   rS   zExceptionVariable.reconstructC  sl    P	
 			"c$))ne4	)s 	)t 	) 	]#[!+,rE   c                     | j                   S rG   )r  rI   s    rD   rJ   zExceptionVariable.python_typeX  s    }}rE   rT   r6   r   r   c                 h   |j                         }|dk(  rf|j                         sCt        |t        j                  t        j
                  t        j                  f      s
J | d       | j                  |       n|dk(  r|j                         sHt        |t        j                  t        j                  t        j
                  t        j                  f      r,|| _	        t        j                  j                  d      | _        nt        |d       n|dk(  r'|j                  dd      r|| _        ndt        |d       nW|dk(  r)t        j!                  |      st        |d	       || _        n)t%        d
d|  d| d| dg t&        j(                         t        j                  j                  d       S )Nr	  z! is not a valid exception contextr
  Tz9exception cause must be None or derive from BaseExceptionr  Fr  z0__traceback__ must be a traceback object or Nonez4Unsupported attribute assignment on Exception objectzcall_setattr r   z/Dynamo does not support setting the attribute 'z{' on tracked exception objects. Only `__context__`, `__cause__`, `__suppress_context__`, and `__traceback__` are supported.rX   )r]   r   r   r   r  !UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariabler  rM   r
  r/   ry   r  r   is_constant_matchr   r   r  r   r   r   r   s        rD   r   zExceptionVariable.call_setattr[  s    **,= '')Z//??@@. 9 789  S![ ##%--//??@@	* "%,5,F,F,M,Md,S) S ++$$T51,/) S _$$77< %WX!$DN'vQtf56 ZZ 7)556 ))0066rE   rU   r;   c                     |dk(  r | j                   |g| S |dk(  r-|\  }| j                  |t        j                  |d      |       | S t        |   ||||      S )Nr  with_tracebackr  )r   r.   r   r@   r   )rB   rT   rU   r{   r;   r   rC   s         rD   r   zExceptionVariable.call_method  so     = $4$$R/$//%%DRb/"7"7O"LbQK7&r4v>>rE   c                    |dk(  r t        j                  || j                        S |dk(  r| j                  S |dk(  r| j                  S |dk(  r| j
                  S |dk(  r| j                  S |dk(  rMt        j                  |t        | j                        | j                  xr t        | j                  d            S t        | 1  ||      S )NrC   r	  r
  r  r  r{   rt   )r.   r   r  r	  r
  r  r  r   r{   r^   r   r@   rz   r   s      rD   rz   zExceptionVariable.var_getattr  s    ;"((T]];;]"###[ >>!++,,,_$%%%V^"((dii {{Fz$++v'F 
 w"2t,,rE   c                 N    | j                   j                   d| j                   dS )N())rC   r   r  rI   s    rD   __str__zExceptionVariable.__str__  s$    ..))*!DMM?!<<rE   argc                 t    	 t        | j                               S # t        $ r | j                         cY S w xY wrG   )reprr]   	Exception
debug_reprr%  s    rD   _debug_format_argz#ExceptionVariable._debug_format_arg  s7    	$..011 	$>>##	$s    77c                 x     dj                   fd j                  D              } j                          d| dS )N, c              3   @   K   | ]  }j                  |        y wrG   )r+  ).0r%  rB   s     rD   	<genexpr>z/ExceptionVariable.debug_repr.<locals>.<genexpr>  s     J//4Js   r"  r#  )joinr{   python_type_name)rB   r{   s   ` rD   r)  zExceptionVariable.debug_repr  s8    yyJ		JJ'')*!D633rE   )NNNr   )r   r   r   BaseExceptionr   r   r   r.   r   r   r   r   rA   r  rS   r`   rJ   r   r   rz   r$  __repr__r   r+  r)  r   r   s   @rD   r  r    se   !M :> $-1@@ ?#@ #./$6	@
 @ $d*@ 
@B#? #t #-*T 67#67 "67 	67
 
67p?#? ? ?#	?
 S/)*? 
? -5 -S -_ -&= = H$ $3 $ $4C 4rE   r  c                       e Zd ZdZy)UnknownVariablez
    It could be anything!
    Nr   r   r   __doc__r?   rE   rD   r6  r6    s    rE   r6  c                   `     e Zd ZdZddedz  deddf fdZddd	ee   de	eef   defd
Z
 xZS )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nmsgr;   r<   c                 2    t        |   di | || _        y r>   )r@   rA   r;  )rB   r;  r;   rC   s      rD   rA   z DelayGraphBreakVariable.__init__  s    "6"rE   rT   r6   r{   c                     | j                   dn| j                   j                  }t        dd| j                    d| d| j                   g        y )NrW   z#Unsupported function call (delayed)zsource: z@Dynamo determined that a graph break should occur when calling `z`. Reason: rX   )r^   rU   r   r;  )rB   rT   r{   r;   rU   s        rD   r   z%DelayGraphBreakVariable.call_function  sS     [[(rdkk.>.>9t{{m,!F+dhhZ9	
rE   rG   )r   r   r   r8  r   r   rA   r   r.   r   r   r   r   s   @rD   r:  r:    s`    C$J   
#
 '
 S/)*	

 

rE   r:  c                   T    e Zd ZdZddZdddedefdZddd	ee   d
e	eef   defdZ
y)ComptimeVariableza
    This variable is special, it lets you execute arbitrary code at
    Dynamo compile time
    r<   Nc                     t        d      )Nzcomptime is special form)r   rB   rN   s     rD   rS   zComptimeVariable.reconstruct  s    !"<==rE   rT   r6   rU   c                     ddl m } | j                  J t        j                  |t	        ||      t        | j                  |            S )Nr   )comptimert   )rC  r^   r.   r   re   r   )rB   rT   rU   rC  s       rD   rz   zComptimeVariable.var_getattr  sA    '{{&&&$$$'
4;;0M
 	
rE   r{   r;   c           
         ddl m} |st        |      dkD  r&t        |ddt        |       dt        |       d       |d   }t	        |t
              r |j                          ||             nt	        |t              r|j                         }|j                  rt        |d|j                          t        j                  ||j                  |j                  j!                         |j"                  rt%        |j"                  j&                        nd d	      } | ||             nt)        d
t+        |             t,        j.                  j1                  d       S )Nr   )ComptimeContextz
comptime()zat most 2 args and 0 kwargsz
 args and  kwargsr   zOcomptime function must not have free variables, but these variables were free: r?   z"unsupported argument to comptime: )rC  rE  rg   r*   r   r1   get_functionr0   get_codeclosurer   co_freevarsr   r   	f_globalsfn_namer]   defaultsr   r   RuntimeErrorr`   r   r/   ry   )rB   rT   r{   r;   rE  fncoder   s           rD   r   zComptimeVariable.call_function  s-    	/ SY]-t9+ZF}G<	 !Wb./BOOob1267;;=Dzz efjfvfvewx %%

--/,.KKbkk''(T D $%!CDH:NOO))0066rE   r   )r   r   r   r8  rS   r   r.   rz   r   r   r   r?   rE   rD   r?  r?    sa    
>
5 
S 
_ 
-7#-7 '-7 S/)*	-7
 
-7rE   r?  c                        e Zd ZU ej                  Zedz  ed<   dZe	dz  ed<   	 ddedz  de
ddf fdZdefdZ xZS )	CellVariableNpre_existing_contents
local_namer;   r<   c                 2    t        |   di | || _        y r>   )r@   rA   rS  )rB   rS  r;   rC   s      rD   rA   zCellVariable.__init__8  s     	"6"%:"rE   c                 "    t         j                  S rG   )r   CellTyperI   s    rD   rJ   zCellVariable.python_type>  s    ~~rE   rG   )r   r   r   r   rW  r   r.   __annotations__rT  r   r   rA   r`   rJ   r   r   s   @rD   rR  rR  (  s]    NNM +T11 "Jd
! ?C;%4t%;;NQ;	;T rE   rR  c                   (     e Zd Zdeddf fdZ xZS )NewGlobalVariabler;   r<   Nc                 $    t        |   di | y r>   r@   rA   rB   r;   rC   s     rD   rA   zNewGlobalVariable.__init__C      "6"rE   )r   r   r   r   rA   r   r   s   @rD   rZ  rZ  B  s    # # # #rE   rZ  fn_clsr<   .c                 N     dt         dt         dt         f fd}t        |_        |S )Nr{   r;   r<   c                  (     j                   | i |S rG   )apply)r{   r;   r_  s     rD   trampoline_autograd_applyzDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_applyH  s    v||T,V,,rE   )r   !produce_trampoline_autograd_apply_origin)r_  rc  s   ` rD   rd  rd  G  s/    - - - - )J%$$rE   c            
            e Zd ZdZdhej
                  Zdededdf fdZdefdZ	dd	d
e
e   deeef   defdZdd	d
e
e   deeef   defdZdd	d
ee   deeef   dd fdZdd	ded
e
e   deeef   def
dZ xZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassr_  r;   r<   Nc                 2    t        |   di | || _        y r>   )r@   rA   r_  )rB   r_  r;   rC   s      rD   rA   z!AutogradFunctionVariable.__init__X  s    "6"rE   c                     t         S rG   r`   rI   s    rD   rJ   z$AutogradFunctionVariable.python_type\      rE   rT   r6   r{   c           	      >   ddt         dd ffd}t        j                  |||f       rzt        j                         re| j                  }ddlm} ddlm} | j                  j                  } || j                  j                        }	|	r3 || j                  j                  | j                  j                        }d }| j                  j                  }
|
t        j                  j                  j                  ur*t        dd	|  d
| d
| dddgt         j"                         | j                  j$                  }|t        j                  j                  j$                  ur)t        dd	|  d
| d
| ddgt         j"                         ddlm} |E|	sCt+        j-                  | j                  j.                        | j                  j0                        }|xr t+        |d      } ||| j                  j2                  ||      j5                  ||      }| j                  r~|	r|t+        | j                  d      }t7        |j9                  t:        j<                               t+        | j                  d      }t7        |j9                  t:        j<                               |S | j                  rt+        | j                  d      }nd }| j                  j                  }t>        jA                  ||      }|g|}tC        |tD        jF                        rjtI        jJ                  |      }tM        |      dz
  tM        |jN                        k(  r|dd  }t        jP                  ||d      }|j5                  ||      S tC        |tD        jR                        rPtU        jV                  |jX                  tU        jZ                  | j                        |      j5                  ||      S t        dd	|  d
| d
| dt]        |      j0                   ddg       y )NFr   r<   c                     | j                         r| j                  durdt        | t        j                        r| j                        rdy y y )NFT)	is_tensorrequires_gradr   r   NNModuleVariableis_training)r   ro  rT   s    rD   visitz2AutogradFunctionVariable.call_apply.<locals>.visitg  sJ    ||~##50$(M"i889>>"%$(M & :rE   r   )#autograd_function_forward_rewritten)_is_setup_context_definedzUnsupported custom vjpzcall_apply r   zgDynamo does not support tracing `torch.autograd.Function` subclasses that define a custom `vjp` method.z+Remove the custom `vjp` method if possible.z.Use standard `backward` instead if applicable.rX   zUnsupported custom jvpzgDynamo does not support tracing `torch.autograd.Function` subclasses that define a custom `jvp` method.z+Remove the custom `jvp` method if possible.r+   )AutogradFunctionApplyVariablerb  )r   rt   forwardsetup_contextTr   z=Non-function or method in subclass of torch.autograd.FunctionzDynamo requires the `forward` attribute of a `torch.autograd.Function` subclass to be a standard Python function or method. Found type `z
` instead.zPEnsure the `forward` method is defined as a regular function or instance method.)/r.   rr  r   is_grad_enabledr^   "torch._functorch.autograd_functionrs  torch.autograd.functionrt  r_  rv  rw  vjpautogradFunctionr   r   r   jvphigher_order_opsru  r   import_sourcer   r   backwardr   r   rw   r   CLOSURE_MATCHAutogradFunctionContextVariablery   r   r   r   inspect	signaturerg   
parametersr   r   r   r   r   r   r`   )rB   rT   r{   r;   rr  r^   rs  rt  
forward_fnis_setup_ctx_definedvjp_fnjvp_fnru  apply_sourcer   fwd_srcsetup_ctx_srcrO  ctxsigr   ro  s    `                   @rD   
call_applyz#AutogradFunctionVariable.call_apply_  s    	)o 	)$ 	) 	edF^4U224[[F J,,J#<T[[=V=V#W # AKK'')B)B
 [[__FU^^448884)$qax@!- FH +66 [[__FU^^448884)$qax@!- F*66
 H~&:#$$T[[%;%;<dkk>R>R "Hj&HL/$$#	
 mBf-  {{3$T[[)<g001K1KLM *4;; Hm66|7Q7QRSJ;;Y7FF[[  -44RvF|d|b%,,-##B'C4y1}CNN 33ABx#))"bNE&&r488E,,-//224;;? mBf-	. W%dV1TF!F8<33783D3D2EZQ3
rE   c                    | j                   j                  }t        |d   j                        t        j
                  j                  j                  u sJ t        |t        j                        sJ | j                  J t        | j                  d      }t        j                  |||d      }|j                  |||      S )Nr   r  Tr   )r_  r  r`   ra   r   _dynamoexternal_utilsFakeBackwardCFunctionr   r   r   r^   r   r.   r   r   )rB   rT   r{   r;   rO  	fn_sourcer   s          rD   call_backwardz&AutogradFunctionVariable.call_backward  s     [[!!DGMM"emm&B&B&X&XXXX"e00111{{&&&t{{J7	%%b"YM""2tV44rE   c                 ,    t        | j                        S rG   )rg  r_  rB   rT   r{   r;   s       rD   r   z&AutogradFunctionVariable.call_function  s     (44rE   rU   c           
         ddl m} |dk(  rwt        j                  | j                        rEt        | j                        } || |j                  j                  d|gt        ||             S | j                  |||      S |dk(  r| j                  |||      S | j                  t        | j                  |      nd }	 t        j                  | j                  |      }t!        |t"              rs|j%                  | j                        }	t        j&                  |	      }
|
J |$|
j)                  |	|      j+                  |||      S  |
|	      j+                  |||      S t!        |t,              r3t/        j0                  |j2                  | |      j+                  |||      S t5        dd	|  d
| d| ddg       y # t        $ r d }Y w xY w)Nr+   )wrap_fx_proxyrb  r   )rT   proxyr  rt   z$Unsupported autograd.Function methodr~   r   ,Dynamo does not support calling the method `z` directly on the `torch.autograd.Function` instance. Supported methods include `apply`, `backward`, static methods, and class methods.znEnsure the method is decorated with `@staticmethod` or `@classmethod` if it's meant to be called on the class.rX   )builderr  r   is_callable_allowedr_  rd  r   create_proxyr)   r  r  r^   r   r  getattr_staticr   r   r   __get__lookupcreate_with_sourcer   r   r   r   r   r   )rB   rT   rU   r{   r;   r  rc  r^   r   r   traceds              rD   r   z$AutogradFunctionVariable.call_method  s    	+7?..t{{;,MKK-) %0"))00'1 +48  r488Z%%b$776:kk6MZT2SWF,,T[[$? #|,{{4;;/$++D1)))% 11$v1FTTf "$<55b$GGC- 33LL$v-D&12 B*4&$8!v 9!9
U- " s     G GG)r   r   r   r8  r.   r   r   rA   r`   rJ   r   r   r   r  r  r   r   r   r   r   s   @rD   rg  rg  P  sD   7 			'	'N
s c d T {#{ ?#{ S/)*	{
 
{z5#5 ?#5 S/)*	5
 
55#5 '5 S/)*	5
 
$5@#@ @ ?#	@
 S/)*@ 
@rE   rg  c                   D    e Zd ZU  ej                  e      Zee   ed<   y)SavedTensorBox)default_factorytensorsN)	r   r   r   dataclassesfieldr   r  r.   rX  r?   rE   rD   r  r  6  s    %6[%6%6t%LGT/"LrE   r  c                   ,    e Zd ZdZdddhej
                  Z	 	 	 	 	 ddededz  dededz  de	ed	f   dz  d
edz  deddf fdZ
e	 	 ddddee   dz  deeef   dz  defd       ZdefdZdddedee   deeef   def
 fdZdddedef fdZ xZS )r  zZ
    Tracks an autograd.Function() context using mutation tracking in side_effects.py
    r  	inferencesaved_tensorsNra   r   needs_input_grad.non_differentiabler;   r<   c                 b    t        |   d||d| || _        || _        || _        || _        y N)ra   r   r?   )r@   rA   r  r  r  r  )	rB   ra   r   r  r  r  r  r;   rC   s	           rD   rA   z(AutogradFunctionContextVariable.__init__G  s;     	FuFvF"* 0"4rE   rT   r6   r{   c           
         d }|r|st        d |D              }| j                  j                  j                  d t        j
                  j                  j                  t        j                  t        dt               |      i       }|S )Nc              3   X   K   | ]"  }|j                         xr |j                   $ y wrG   )rn  ro  r/  xs     rD   r0  z9AutogradFunctionContextVariable.create.<locals>.<genexpr>`  s"     $U1Q[[]%Fq%F$Us   (*T)r  r  r  )r   r   r   track_object_newr   r|  functionFunctionCtx	functoolspartialr  r  )rT   r{   r;   r  outs        rD   ry   z&AutogradFunctionContextVariable.createW  sz      $$UPT$UUii$$55NN##///,.!1	 

 
rE   c                 v    | j                   "t        dd|  dg t        j                         | j                   S )Nzproxy not setz	as_proxy zMDynamo requires the autograd.Function context to be initialized with a proxy.rX   )r  r   r   r   rI   s    rD   as_proxyz(AutogradFunctionContextVariable.as_proxyn  sA    ::'#D6*25)445 zzrE   rU   c                 *   |dk(  rt         |   ||||      S |dk(  rO|rt        ||dt        |       d       t	        |i       d   | _        t        j                  j                  d       S |dk7  r)t        dd|  d	| d
| dg t        j                         | j                  t        dd|  d	| ddg       | j                  J | j                  s@|s| j                  st        |d       |j                   j"                  j%                  | |       t        | j                  j&                        dkD  rg | j                  _        |D ]'  }| j                  j&                  j)                  |       ) t        j                  j                  d       S )Nr  mark_non_differentiablez0 kwargsrF  r   save_for_backwardz,Unsupported autograd.Function context methodr~   r   r  z` on `autograd.Function` context objects. Supported methods are `__setattr__`, `save_for_backward` and `mark_non_differentiable`.rX   z9Unsupported autograd.Function context `save_for_backward`zjDynamo requires the `saved_tensors` attribute to be initialized on the `autograd.Function` context object.zEnsure that the `saved_tensors` attribute is properly initialized before calling `save_for_backward`. `save_for_backward` only supported on a newly constructed `torch.autograd.function.FunctionCtx`.z>save_for_backward() requires a source and no keyword arguments)r@   r   r*   rg   r)   r  r   r/   ry   r   r   r   r  r  r^   r   r   r   track_save_for_backwardr  append)rB   rT   rU   r{   r;   r%  rC   s         rD   r   z+AutogradFunctionContextVariable.call_method{  s    = 7&r4v>>..#BjS[M:QR&7b&A!&DD#--44T::&&F&tfAdV46 -- 7)556 %S&tfAdV4Ow
 !!---~~T[[ X II""::4F t!!))*Q.)+D& 	3C&&--c2	3))0066rE   c                     dv rt         fd      S dk(  r> j                  2t        j                  t	         j                  j
                              S dk(  r j                  )t        j                  j                   j                        S  j                  rAt         j                  d      }t        j                   j                  j                  |      S t         =        S )N)r  r  c                  >    j                  t        |       |      S rG   )r   r   )r{   r;   rU   rB   rT   s     rD   rO   z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>  s    (8(8T4:v(V rE   r  r  )r   r  r   r   r   r  r  r/   ry   r^   r   r.   r   ra   r@   rz   rB   rT   rU   r^   rC   s   ``` rD   rz   z+AutogradFunctionContextVariable.var_getattr  s    CC!V  ?"t'9'9'E**40B0B0J0J+KLL%%$$0 11889N9NOO{{#DKK1CD&,,R1L1LfUUw"2t,,rE   )NFNNNNN)r   r   r   r8  r4   r   r   r`   r  r   rA   r   r   r.   r   r   ry   r  r   r   rz   r   r   s   @rD   r  r  ;  sr   
 	 
#	1	1	N #'$(48)-55 4K5 	5
 Tz5  c	*T15  $J5 5 
5   2648#'$. S/)*T1 
	 ,# 37#37 37 ?#	37
 S/)*37 
37j-5 -S -_ - -rE   r  c            
            e Zd ZdZ	 ddej
                  j                  deej
                  j                     dz  deddf fdZ	dd	d
e
dee   dee
ef   def
dZ xZS )AutogradEngineVariablez;
    Represents a torch._C._ImperativeEngine instance.
    Nra   r   r;   r<   c                 *    t        |   d||d| y r  r\  )rB   ra   r   r;   rC   s       rD   rA   zAutogradEngineVariable.__init__  s     	FuFvFrE   rT   r6   rU   r{   c                    |dk(  rt         j                  j                  j                  r|j                  s|j
                  sJ d       t        j                  |t         j                  j                  j                  j                        }|j                  ||j                  j                  j                         g||      S t        dd|  d| dg        y t        dd|  d| d	| d
g        y )Nqueue_callbackzXqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=Truez7Unsupported torch._C._ImperativeEngine.queue_callback()r~   r   zYqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=True.rX   z-Unsupported torch._C._ImperativeEngine methodzgDynamo only supports the `queue_callback` method on a torch._C._ImperativeEngine instance, but found: `z`.)r   r  compiled_autogradin_compiled_autograd_region	one_grapherror_on_graph_breakr.   r   r  FakeCompiledAutogradEnginer  r   r   r   get_ca_final_callbacks_varr   )rB   rT   rU   r{   r;   r   s         rD   r   z"AutogradEngineVariable.call_method  s     ##}}..JJ||r'>'> n> (--MM00KKZZ **YY++FFHP4P  U*4&$8!H G&tfAdV4IIMbRrE   rG   )r   r   r   r8  r   r   _ImperativeEnginer`   r   rA   r   r   r.   r   r   r   r   s   @rD   r  r    s     ?CGxx))G 334t;G 	G
 
G%#% % ?#	%
 S/)*% 
%rE   r  c                   j     e Zd Zdedef   deddf fdZdefdZdd	d
e	e   de
eef   defdZ xZS )r   rO  .r;   r<   Nc                 2    t        |   di | || _        y r>   )r@   rA   rO  )rB   rO  r;   rC   s      rD   rA   zLambdaVariable.__init__  s    "6"rE   c                 "    t         j                  S rG   )r   r   rI   s    rD   rJ   zLambdaVariable.python_type  s    !!!rE   rT   r6   r{   c                 &     | j                   |i |S rG   )rO  r  s       rD   r   zLambdaVariable.call_function   s     tww'''rE   )r   r   r   r   r.   r   rA   r`   rJ   r   r   r   r   r   r   s   @rD   r   r     sm    8C$89 S T "T "(#( '( S/)*	(
 
(rE   r   c                       e Zd Zddhej                  Z	 ddedededz  deddf
 fdZdef fdZ	defd	Z
ed
ej                  j                  dedefd       ZdefdZdefdZdddedefdZddZdddee   deeef   defdZ xZS )rv   rU   rs   Nr   r;   r<   c                     t        |   di | t        |t              sJ t        |t              sJ || _        || _        || _        y r>   )r@   rA   r   r.   r   r   rU   rs   )rB   r   rU   rs   r;   rC   s        rD   rA   zGetAttrVariable.__init__  sJ     	"6"#///$$$$	rE   c                 P    | j                   | j                   S t        | 	         S rG   )rs   r@   rJ   rB   rC   s    rD   rJ   zGetAttrVariable.python_type  s%    <<#<<7&((rE   c                 h    | j                   j                   d| j                   d| j                   dS Nr"  r-  r#  )rC   r   r   rU   rI   s    rD   r4  zGetAttrVariable.__repr__$  s.    ..))*!DHH:R		{!DDrE   
base_proxyr   c                     t        | |      S rG   )re   )r  r   s     rD   create_getattr_proxyz$GetAttrVariable.create_getattr_proxy'  s    z4((rE   c                 r    t         j                  | j                  j                         | j                        S rG   )rv   r  r   r  rU   rI   s    rD   r  zGetAttrVariable.as_proxy+  s&    33DHH4E4E4GSSrE   c                     | j                   j                         }	 t        || j                        S # t        $ r t        |  d      d w xY w)Nz is not a constant)r   r]   re   rU   r   r   )rB   constants     rD   r]   z"GetAttrVariable.as_python_constant.  sQ    88..0	M8TYY// 	M%.@&ABL	Ms	   2 ArT   r6   c                    t        | j                  t        j                        st        |j
                  j                  | j                  j                        }| j                  |j                  vrt        t        j                  || j                        }||j                  vrt        t        j                  ||      S rG   )r   r   r   rp  r   r   get_submodule
module_keyrU   rh   r  r  )rB   rT   rU   step1step2s        rD   const_getattrzGetAttrVariable.const_getattr5  s    $((I$>$>?%%		''(;(;<99ENN*%%&&udii8u~~%%%%%eT22rE   c                 |     || j                          |j                  |j                  | j                               y rG   )r   rQ   create_load_attrsrU   rA  s     rD   rS   zGetAttrVariable.reconstruct@  s+    g77		BCrE   r{   c                 d    | j                   j                  || j                  t        |      |      S rG   )r   r   rU   r   r  s       rD   r   zGetAttrVariable.call_functionD  s'     xx##B		4:vFFrE   rG   r   )r   r   r   r.   r   r   r`   r   rA   rJ   r4  r   r   fxProxyr  r  r]   r  rS   r   r   r   r   r   s   @rD   rv   rv   	  s%    
	'	'N  $	  	
  
)T )E# E ) )s )s ) )T# TMC M	3 7 	3s 	3s 	3DG#G 'G S/)*	G
 
GrE   rv   c                        e Zd Zdej                  deddf fdZdej                  fdZddd	ee	   de
ee	f   de	f fd
Zded   fdZdej                  fdZded   fdZdefdZdedefdZ xZS )MethodWrapperVariablemethod_wrapperr;   r<   Nc                 2    t        |   di | || _        y r>   )r@   rA   r  )rB   r  r;   rC   s      rD   rA   zMethodWrapperVariable.__init__N  s    "6",rE   c                     | j                   S rG   r  rI   s    rD   get_real_python_backed_valuez2MethodWrapperVariable.get_real_python_backed_valueR      """rE   rT   r6   r{   c           	         t        | j                        rxt        |d   t        j                        r[t        |      dk(  rt        |      dk(  st        |d       |d   j                  || j                  j                  j                        S | j                  j                  }| j                  j                  }|dk(  rWt        |      j                  }|t        j                  u r t        j                  |t              j                  |||g||      S t         j"                  dk\  r|t        j$                  d   u rz|dk(  ruddlm} t        |      dk(  r5|s3	 |j+                  || j                  |d   j-                                     S t5        d	d
|  d| d| dddgt6        j8                         nO|t        j$                  d   u r|dk(  s|t        j$                  d   u r|dk(  r|t        j$                  d   u rdnd}t        |      dk(  r|s	 | j                  |d   j-                               }	|d   j:                  }
|
j|d   j-                         }t=        j>                  t        |      |      }||u r4|dk(  rtA        |
      }
ntC        |
|      }
t        j                  ||	|
      S ddlm} |j+                  ||	      S t5        d| dd
|  d| d| d| dd| dd| dgt6        j8                         tD        |   |||      S # t.        $ r t1        t.        |       Y t2        $ r Y w xY w# t2        $ r Y w xY w)Nr   r+   z2tensor attribute getter takes exactly one argumentrA   )r      rX  r  )SourcelessBuilderz9unsupported type.__dict__['__annotations__'].__get__ callzcall_function , args: 
, kwargs: z{`torch.compile` only supports calling type.__dict__['__annotations__'].__get__ on a single constant argument (i.e. a type).zpMake sure your call to type.__dict__['__annotations__'] only has one positional argument (no keyword arguments).zMake sure the argument to type.__dict__['__annotations__'] is a constant (i.e. type). For example, `object`, `int`, `MyCustomClass`.rX   rb   rh   zunsupported type.__dict__['z'].__get__ callz5`torch.compile` only supports calling type.__dict__['z7'].__get__ on a single constant argument (i.e. a type).z&Make sure your call to type.__dict__['zC'].__get__ only has one positional argument (no keyword arguments).z)Make sure the argument to type.__dict__['zT'].__get__ is a constant (i.e. type). For example, `object`, `int`, `MyCustomClass`.)$r'   r  r   r   TensorVariablerg   r   rz   r   r   r`   rA   r   r.   r   r   sysversion_inforh   r  r  ry   r]   r   r   r,   r   r   r   r^   r  r  r!   r   r@   r   )rB   rT   r{   r;   self_objwrapper_namefn_objr  r   ra   r^   cls_valstatic_descrC   s                rD   r   z#MethodWrapperVariable.call_functionU  s    &d&9&9:zGY--@
 INs6{a'7 L 7&&r4+>+>+G+G+P+PQQ &&//**33 :%(^,,F(&,,R8DD %%  ' DMM*;<<	)24yA~f,33D//Q0J0J0LM  S(htfJvhO?FR
 '22 $--	22|y7Pj11li6O &y)AA	z  4yA~f? //Q0J0J0LME "!W^^F)"&q'"<"<">&-&<&<T']I&V&(2(I5)6v)>)3FI)F#2#8#8UF#KK:,33B>>5i[P(htfJvhOST]S^ _? ? =YK HF F?	{ KR R
 '22 w$Rv66} & A,^R@: 4 ; s*   1L "M M5M M	MMTc                      yNTr?   rI   s    rD   is_python_constantz(MethodWrapperVariable.is_python_constant      rE   c                     | j                   S rG   r  rI   s    rD   r]   z(MethodWrapperVariable.as_python_constant  r  rE   c                      yr  r?   rI   s    rD   is_python_hashablez(MethodWrapperVariable.is_python_hashable  r  rE   c                 4    t        | j                               S rG   hashr]   rI   s    rD   get_python_hashz%MethodWrapperVariable.get_python_hash      D++-..rE   otherc                 h    t        |t              xr! | j                         |j                         k(  S rG   r   r.   r]   rB   r  s     rD   is_python_equalz%MethodWrapperVariable.is_python_equal  1    uo. H'')U-E-E-GG	
rE   )r   r   r   r   MethodWrapperTyper   rA   r  r   r.   r   r   r   r   r  r]   r	  intr  r   r  r  r   r   s   @rD   r  r  M  s    -u'>'> -# -RV -#e.E.E #o7#o7 'o7 S/)*	o7
 
o7bGDM #E$;$; #GDM / /
V 
 
rE   r  c                        e Zd Zdej                  deddf fdZdej                  fdZddd	ede	f fd
Z
ded   fdZdej                  fdZ xZS )GetSetDescriptorVariabledescr;   r<   Nc                 2    t        |   di | || _        y r>   )r@   rA   r  )rB   r  r;   rC   s      rD   rA   z!GetSetDescriptorVariable.__init__  s    "6"	rE   c                     | j                   S rG   r  rI   s    rD   r  z5GetSetDescriptorVariable.get_real_python_backed_value      yyrE   rT   r6   rU   c                 n   |dk(  rM| j                   rAt        | j                   d      }t        j                  || j                  j
                  |      S |dv rO| j                   xr t        | j                   |      }t        j                  |t        | j                  |      |      S t        | !  ||      S )Nr  )__objclass__r   )	r^   r   r.   r   r  r  re   r@   rz   r  s       rD   rz   z$GetSetDescriptorVariable.var_getattr  s    9Y7F"((TYY->->GG11[[BZT%BF"((WTYY-EvNN7&r400rE   Tc                      yr  r?   rI   s    rD   r  z+GetSetDescriptorVariable.is_python_constant  r  rE   c                     | j                   S rG   r  rI   s    rD   r]   z+GetSetDescriptorVariable.as_python_constant  r  rE   )r   r   r   r   GetSetDescriptorTyper   rA   r  r   r.   rz   r   r  r]   r   r   s   @rD   r  r    sr    U77 3 4 e.H.H 15 1S 1_ 1GDM E$>$> rE   r  c                       e Zd Zej                  Zddhej                  Zdej                  deddf fdZ	de
ej                     fdZdej                  fdZdej                  fd	Zdefd
ZdddeddfdZdddedefdZ xZS )PythonModuleVariablera   is_torchr;   r<   Nc                     t        |   di | || _        | j                  t        u xs% | j                  j                  j                  d      | _        y )Nztorch.r?   )r@   rA   ra   r   r   
startswithr%  rB   ra   r;   rC   s      rD   rA   zPythonModuleVariable.__init__  sD    "6"


e+Wtzz/B/B/M/Mh/WrE   c                 "    t         j                  S rG   )r   
ModuleTyperI   s    rD   rJ   z PythonModuleVariable.python_type  s    rE   c                     | j                   S rG   ra   rI   s    rD   r]   z'PythonModuleVariable.as_python_constant      zzrE   c                     | j                   S rG   r,  rI   s    rD   r  z1PythonModuleVariable.get_real_python_backed_value  r-  rE   c                 "    d| j                    dS )NzPythonModuleVariable(r#  r,  rI   s    rD   r4  zPythonModuleVariable.__repr__
  s    &tzzl!44rE   rT   r6   rU   r/   c                 Z    t        | j                  |      }t        j                  ||      S rG   )r   ra   r.   r   rB   rT   rU   r   s       rD   call_obj_hasattrz%PythonModuleVariable.call_obj_hasattr  s'     T*$$R00rE   c                    |j                   j                  j                  | |      r&|j                   j                  j                  | |      S d }| j                  s|| j
                  j                  vr	 t        | j
                  |      }n| j
                  j                  |   }| j                  xr t        | j                  |      }t        j                  |||      S # t        $ r t        t        |       Y Vw xY wrG   )r   r   r   r   r%  ra   rh   re   r   r   r^   r   r.   r   )rB   rT   rU   r   r^   s        rD   rz   z PythonModuleVariable.var_getattr  s    99!!>>tTJ99))33D$??
==D

(;(;;=$TZZ6
 ,,T2J>DKK!>$$RV<< " =(<=s   4C C;:C;)r   r   r   r   r*  r   r.   r   r   rA   r`   rJ   r]   r  r   r4  r2  rz   r   r   s   @rD   r$  r$    s    $$M 	 
	'	'NXe.. X# X$ X
 T%"2"23  E$4$4 e.>.> 5# 51)1141	1=5 =S =_ =rE   r$  c            
            e Zd Zdededdf fdZdddedefd	Zddd
edee   de	eef   def
dZ
ddd
edefdZdefdZdefdZd fdZded   fdZdefdZdedefdZ xZS )TypingVariablera   r;   r<   Nc                 2    t        |   di | || _        y r>   r@   rA   ra   r(  s      rD   rA   zTypingVariable.__init__%      "6"
rE   rT   r6   keyc                 R    | j                   |j                            }t        |      S rG   )ra   r]   r5  )rB   rT   r9  
new_typings       rD   mp_subscript_implz TypingVariable.mp_subscript_impl)  s&     ZZ 6 6 89
j))rE   rU   r{   c                 ~   |dk(  r`t        |      dk(  rR|sPt        |d   t              xr | j                  |d   j                  k(  }t        j
                  j                  |      S t        dd| j                   d| d| d| d	| d
| j                   dd| d| j                   dgt        j                         y )Nr  r+   r   z,unsupported method call on `typing` variableztyping variable: z, method name: r  r  z.`torch.compile` does not support method call `z` on `typing` variable f.zAvoid calling the z method on rX   )
rg   r(   r5  ra   r   r/   ry   r   r   r   )rB   rT   rU   r{   r;   r   s         rD   r   zTypingVariable.call_method3  s     84yA~fQ8XTZZ4PQ7===X 1188@@B'

|?4&QUPVV`ag`hiHNfgkgqgqfrrst$TF+djj\C"..		
rE   c           
         ddl m}m} |t        v r5t	        j
                  | |t        t        | j                  |                  S |j                  j                  j                  | |      r&|j                  j                  j                  | |      S t        | j                  |      }| j                  r%t        | j                  |      }  |||      |      S |j                  ||      S )Nr+   )r  VariableBuilderrr   )r  r  r@  r%   r   rv   r`   re   ra   r   r   r   r   r^   r   ry   )rB   rT   rU   r  r@  ra   r   s          rD   rz   zTypingVariable.var_getattrH  s    ?)),,dDT)B$C  99!!>>tTJ99))33D$??

D);;$T[[$7K3?2{3E::$++B66rE   c                     | j                   S rG   r,  rI   s    rD   r]   z!TypingVariable.as_python_constantZ  r-  rE   c                     | j                   S rG   r,  rI   s    rD   r  z+TypingVariable.get_real_python_backed_value]  r-  rE   c                     t        | j                  t        j                        st        |   |      S |j                  |j                  | j                               y rG   )r   ra   r   GenericAliasr@   rS   append_outputcreate_load_constrR   s     rD   rS   zTypingVariable.reconstruct`  sF    $**e&8&897&w//0 	g77

CDrE   Tc                      yr  r?   rI   s    rD   r	  z!TypingVariable.is_python_hashable|  r  rE   c                 4    t        | j                               S rG   r  rI   s    rD   r  zTypingVariable.get_python_hash  r  rE   r  c                 h    t        |t              xr! | j                         |j                         k(  S rG   r  r  s     rD   r  zTypingVariable.is_python_equal  r  rE   r   )r   r   r   r   rA   r.   r<  r   r   r   r   rz   r]   r  rS   r   r	  r  r  r   r  r  r   r   s   @rD   r5  r5  $  s    c S T *#* * 
	*
#
 
 ?#	

 S/)*
 

*75 7S 7_ 7$C c E8GDM / /
V 
 
rE   r5  )maxsizec                      ddl m}  i }| j                         D ]H  \  }}|j                  j                         D ]&  \  }}t	        |      st        ||d      x}s"|||<   ( J |S )zd
    This generates a mapping from numpy modules to their torch._numpy
    modules equivalents.
    r   )NP_TO_TNP_MODULEN)utilsrL  r   rh   callablere   )rL  np_fn_to_tnp_fnnp_modtnp_modrL  tnp_fnnp_fns          rD   get_np_to_tnp_maprT    sz     )O+113 4&//557 	4OGV $FGT::5:-3OE*	44 rE   c                  j    t               } | j                         D ci c]  \  }}||
 c}}S c c}}w )z
    This is just the reverse mapping of get_np_to_tnp_map() - mapping from
    torch._numpy modules to numpy equivalents.
    )rT  r   )mkvs      rD   get_tnp_to_np_maprY    s.     	AWWY'TQAqD'''s   /c            
       P    e Zd ZdZej
                  fZdededdf fdZdefdZ	e
dej                  defd	       Ze
dej                  defd
       Zdddee   deeef   defdZdddedee   deeef   def
dZdefdZdef fdZded   fdZdefdZdedefdZ xZ S )NumpyVariablezz
    Wrapper around `numpy.*`. Currently, is able to trace a small subset of numpy functions as well as numpy dtypes.
    ra   r;   r<   Nc                 2    t        |   di | || _        y r>   r7  r(  s      rD   rA   zNumpyVariable.__init__  r8  rE   c                     | j                   S rG   r,  rI   s    rD   r  z*NumpyVariable.get_real_python_backed_value  r-  rE   rO  c                     |j                   j                  d      }t        |      dk\  r
|d d ddgk(  sJ || j                  v S Nr>  r   r   _numpy)r   splitrg   constant_fold_functionsr   rO  mods      rD   can_constant_fold_throughz'NumpyVariable.can_constant_fold_through  sJ    mm!!#&3x1}RaWh,?!???S0000rE   c                     |j                   j                  d      }t        |      dk\  r
|d d ddgk(  sJ t        j	                  |      S r_  )r   ra  rg   np_constant_collections_mapri   rc  s      rD    get_constant_collection_for_funcz.NumpyVariable.get_constant_collection_for_func  sJ    mm!!#&3x1}RaWh,?!???*..r22rE   rT   r6   r{   c                    t         j                  s1t        dd| j                   d| d| d| j                   ddg       d	d
lm} ddlm} t               j                  | j                        }|Dt        dd| j                   d| d| d| d	d| j                   dg t        j                         |J | j                  |      x}	 e	  | | j                  |D cg c]  }|j                          c}i |j                         D 	
ci c]  \  }	}
|	|
j                          c}
}	      S |j"                  dk(  rTt         j$                  rDt        dd| j                   d| d| d| d	d| j                   ddd| j                   dg       |j'                  |j(                  ||      \  }}| j+                  |      rt-        ||      rvt/        j0                  | | j                         |D cg c]  }|j                          c}i |j                         D 	
ci c]  \  }	}
|	|
j                          c}
}	      S  |j2                  j4                  d ||      gt7        ||       }|j9                  ||      S c c}w c c}
}	w # t        $ rG t        dd| j                   d| d| d| d	d| j                   dg t        j                          Y y w xY wc c}w c c}
}	w )Nz?attempted to trace numpy function with config.trace_numpy=Falsenumpy function: r  r  z"Attempted to trace numpy function z; while `torch._dynamo.config.trace_numpy` was set to False.zHSet `torch._dynamo.config.trace_numpy` to True to trace numpy functions.rX   r   )numpy_to_tensor_wrapperr+   )NumpyNdarrayVariablez8attempted to trace numpy function unsupported by PyTorchz  (corresponding torch function: r#  z Can't find numpy numpy function z in torch._numpy.zTnumpy function that produces a const collection type encountered non-const argumentsznumpy function zs that produces a const collection type (e.g. np.dtype, np.iinfo/np.finfo) received arguments that are not constant.ztorch._numpy.randomzXattempted to trace torch._numpy.random function with config.use_numpy_random_stream=TruezAttempted to trace zD when `torch._dynamo.config.use_numpy_random_stream` is set to True.z<Set `torch._dynamo.config.use_numpy_random_stream` to False.zAvoid calling r>  r   )r   trace_numpyr   ra   rM  rk  tensorrl  rT  ri   r   r   rh  r]   r   r,   
USER_ERRORr   use_numpy_random_stream
patch_argsr   re  r$   r.   r   r   r  r)   ry   )rB   rT   r{   r;   rk  rl  r   collection_variable_typr  rW  rX  r  s               rD   r   zNumpyVariable.call_function  sZ    !!Y*4::,htfJvhW@ MM M _ 	40 "&&tzz2<R*4::,htfJvhVvw{v||}~>tzzlJ[\&22	 '+'L'LT'RR#.DJJ:>?Q!..0?AGPA1a2244P & #8822v.tzzl(4&
SYRZZz{  {A  AB  C"5djj\ B& !& W(A6	 0::4==$PVWLD&--d3-dF; ',,-D++-:>?Q!..0?AGPA1a2244P  +BII**'- #40E
 (..r599g @P 7 
r.tzzl(4&
SYRZZz{  {A  AB  C"1$** >@ !@*55	
H @Ps=   J "J
9J J*	J 8K(%K-
J AK%$K%rU   c           	      h    t        dd| j                   d| d| dg t        j                         y )Nz/attempted to trace numpy.* function as a methodrj  r  r  z6Tracing numpy.* functions as methods is not supported.rX   )r   ra   r   r   )rB   rT   rU   r{   r;   s        rD   r   zNumpyVariable.call_method  s?     	E&tzzl(4&
6(SP",,		
rE   c                     | j                   S rG   r,  rI   s    rD   r]   z NumpyVariable.as_python_constant.  r-  rE   c                     t         j                  r`t        | j                  t        j
                        r| j                  S t        | j                  t              r| j                  j                  S t        | %         S rG   )
r   rm  r   ra   enumEnumMetar`   r   r@   r  r  s    rD   r  zNumpyVariable.as_proxy1  sV    $**dmm4zz!$**d+ zz***w!!rE   Tc                      yr  r?   rI   s    rD   r	  z NumpyVariable.is_python_hashable?  r  rE   c                 4    t        | j                               S rG   r  rI   s    rD   r  zNumpyVariable.get_python_hashB  r  rE   r  c                 h    t        |t              xr! | j                         |j                         k(  S rG   r  r  s     rD   r  zNumpyVariable.is_python_equalE  r  rE   )!r   r   r   r8  tnp
issubdtyperb  r   rA   r  r   r   r   r  re  rh  r   r.   r   r   r   r   r   r   r]   r  r   r	  r  r  r   r  r   r   s   @rD   r[  r[    sY     #~~/c S T c  15+=+= 1$ 1 1
 3%2D2D 3 3 3
Z:#Z: 'Z: S/)*	Z:
 
Z:x
#
 
 ?#	

 S/)*
 

 $7 "# "GDM / /
V 
 
rE   r[  c                   <     e Zd Zdeddf fdZdefdZddZ xZS )NullVariabler;   r<   Nc                 $    t        |   di | y r>   r\  r]  s     rD   rA   zNullVariable.__init__N  r^  rE   c                      y)Nr~  r?   rI   s    rD   r4  zNullVariable.__repr__Q  s    rE   c                     t         j                  dk  rt        dddg t        j                         |j                  t        d             y )N)r      z0cannot reconstruct NullVariable in Python < 3.11rW   zdAttempted to generate PUSH_NULL instruction in Python < 3.11; where this instruction does not exist.rX   	PUSH_NULL)r  r  r   r   r   rE  r   rA  s     rD   rS   zNullVariable.reconstructT  sL    g%J9&11 	0=>rE   r   )	r   r   r   r   rA   r   r4  rS   r   r   s   @rD   r~  r~  M  s'    # # ## ?rE   r~  c                       e Zd ZdZy)r   z"Marker used to implement delattr()Nr7  r?   rE   rD   r   r   b  s    ,rE   r   c            
            e Zd ZdZdhej
                  ZdefdZede	de
e   dee	ef   defd       Zde	de
e   dee	ef   dedd	f
 fd
Zde	fdZededefd       Zde	fdZddZ xZS )StringFormatVariablez[
    Represents a call to str.format(), we delay calling format until after the graph.
    format_stringr<   c                     t         S rG   )r   rI   s    rD   rJ   z StringFormatVariable.python_typem  s    
rE   sym_args
sym_kwargsc                    t        d t        j                  ||j                               D              r{t        j
                  j                   |j                  |D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}}      S  | |t        |      t        |            S c c}w c c}}w )Nc              3   <   K   | ]  }|j                           y wrG   )r  r  s     rD   r0  z.StringFormatVariable.create.<locals>.<genexpr>w  s!      
   "
s   )all	itertoolschainvaluesr   r/   ry   formatr]   r   r   r   )r   r  r  r  rX  rW  s         rD   ry   zStringFormatVariable.createp  s      
__Xz/@/@/BC
 
 --44$$$6>?a**,?=G=M=M=OPTQq!..00P  =$x.$z2BCC	 @Ps   CCr;   Nc                 r    t        |   di | t        |t              sJ || _        || _        || _        y r>   )r@   rA   r   r   r  r  r  )rB   r  r  r  r;   rC   s        rD   rA   zStringFormatVariable.__init__  s;     	"6"----* $rE   c                     | j                   j                   d| j                  d| j                  d| j                  dS r  )rC   r   r  r  r  rI   s    rD   r4  zStringFormatVariable.__repr__  sB    ..))*!D,>,>+ADMMCTTVW[WfWfViijkkrE   r%  c                 b    	 | j                         S # t        $ r | j                         cY S w xY wrG   )r]   r(  r)  r*  s    rD   r+  z&StringFormatVariable._debug_format_arg  s2    	$))++ 	$>>##	$s    ..c                 `   	  | j                   j                  | j                  D cg c]  }| j                  |       c}i | j                  j                         D ci c]  \  }}|| j                  |       c}}}t        |      S c c}w c c}}w # t        $ r t        |       cY S w xY wrG   )r  r  r  r+  r  r   r(  r'  )rB   r%  r9  ra   rendereds        rD   r)  zStringFormatVariable.debug_repr  s    		0t))009=G#$((-G '+oo&;&;&="U //66H H~ H
  	:	s-   $B B B B:B B B-,B-c                     j                   fdd        t        j                  t         j                                      j
                  j                         D ci c]%  \  }}t        j                  j                  |      |' }}} t        j                  |             j                  t        dd             y c c}}w )Nc                  z     j                   j                  j                         j                  d      g      S )Nr  )rQ   rF  r  create_load_attrr  s   rD   rO   z2StringFormatVariable.reconstruct.<locals>.<lambda>  s9    G))--d.@.@A,,X6 rE   T)call_function_exF)rP   r   r   r   r  r  r   r/   ry   ConstDictVariablerQ   r   )rB   rN   rW  rX  r;   s   ``   rD   rS   z StringFormatVariable.reconstruct  s     " 	 	
 		''T]](;<=@D@U@U@W
8<1I&&--a0!3
 
 		++F345dEBC	
s   %*C	r   )r   r   r   r8  r.   r   r`   rJ   r   r   r   r   ry   r   rA   r4  r   r   r+  r)  rS   r   r   s   @rD   r  r  f  s     &G(F(FGNT  DD ?+D o-.	D
 
D D$%% ?+% o-.	%
 % 
%l# l $ $6 $ $C DrE   r  c                   N     e Zd ZeZdededdf fdZdefdZdee   fdZ	 xZ
S )ObjectVariablera   r;   r<   Nc                 2    t        |   di | || _        y r>   r7  r(  s      rD   rA   zObjectVariable.__init__  r8  rE   c                     | j                   S rG   r,  rI   s    rD   r  z+ObjectVariable.get_real_python_backed_value  r-  rE   c                     t         S rG   )r   rI   s    rD   rJ   zObjectVariable.python_type  s    rE   )r   r   r   r   r   r   rA   r  r`   rJ   r   r   s   @rD   r  r    s>    Mf   f T&\ rE   r  c            	            e Zd ZdZdededdf fdZdefdZedede	e
j                  e
j                  z     fd	       Zd
ddee   deeef   defdZddZededee   deeef   defd       Z xZS )DebuggingVariablez
    Represents a call to a debugging function like print(), or something
    registered to config.reorderable_logging_functions.
    ra   r;   r<   Nc                 2    t        |   di | || _        y r>   r7  r(  s      rD   rA   zDebuggingVariable.__init__  r8  rE   c                 ,    t        | j                        S rG   r`   ra   rI   s    rD   rJ   zDebuggingVariable.python_type      DJJrE   r   c                     t        |       xrR t        | t        j                  t        j                  f      xr& | t
        j                  j                  j                  v S rG   )	rN  r   r   r   r   r   r  r   reorderable_logging_functionsr   s    rD   is_reorderable_logging_functionz1DebuggingVariable.is_reorderable_logging_function  sO    
 SM J3!3!3U5N5N OPJu}}++III	
rE   rT   r6   r{   c           	      V   |j                   rt        j                  d       S | j                  | j                  ||      s1t        dd| j                   d| d| dd| j                   dg       |j                  j                  | t        |      f       t        j                  d       S )	NzJattempted to reorder a debugging function that can't actually be reorderedzfn: r  r  zl`torch.compile` can only reorder functions where the arguments are Tensors, constants, or string formatters.z#Avoid calling the logging function z" with args that are not supported.rX   )	exportr/   ry   can_reorder_logsra   r   debug_localsr  r   r  s       rD   r   zDebuggingVariable.call_function  s     99#**400$$TZZv>dtzzl(4&
6(K@ :$**Egh 	d4j12&&t,,rE   c                 T    | j                   J | j                   j                  |      S rG   )r^   rS   rA  s     rD   rS   zDebuggingVariable.reconstruct  s'    {{&&&{{&&w//rE   rO  c                     t         j                  t         j                  t        f}t	        j
                  ||g      }|D ]  }t        ||      r y y)zm
        Run some additional checks for what sort of function calls can we
        actually reorder.
        FT)r   r  r/   r  pytreetree_leavesr   )rO  r{   r;   allowed_input_types	flat_argsr%  s         rD   r  z"DebuggingVariable.can_reorder_logs  sY     $$&& 
 &&f~6	 	Cc#67	 rE   r   )r   r   r   r8  r   rA   r`   rJ   r   r
   r   r   r   r  r   r.   r   r   r   rS   r  r  r   r   s   @rD   r  r    s    
c S T  T   

	5%%(A(AA	B
 
-#- '- S/)*	-
 
-00 S  tCH~ RV  rE   r  c                   p     e Zd ZdZdededdf fdZdefdZdefdZd	d
de	e
   deee
f   de
fdZ xZS )IgnoredFunctionVariablezL
    Represents a call to an arbitrary function that should be ignored.
    ra   r;   r<   Nc                 2    t        |   di | || _        y r>   r7  r(  s      rD   rA   z IgnoredFunctionVariable.__init__  r8  rE   c                 ,    t        | j                        S rG   r  rI   s    rD   rJ   z#IgnoredFunctionVariable.python_type  r  rE   c                     | j                   S rG   r,  rI   s    rD   r  z4IgnoredFunctionVariable.get_real_python_backed_value  r-  rE   rT   r6   r{   c                 @    t         j                  j                  d       S rG   )r   r/   ry   r  s       rD   r   z%IgnoredFunctionVariable.call_function   s     ))0066rE   )r   r   r   r8  r   rA   r`   rJ   r  r   r.   r   r   r   r   r   s   @rD   r  r    st    c S T  T  c 7#7 '7 S/)*	7
 
7rE   r  c            
            e Zd ZdZdej
                  deddf fdZdefdZ	dej
                  fdZ
d	d
dedee   deeef   def
dZ xZS )LoggingLoggerVariablez:
    Represents a call to any logging.Logger methods.
    ra   r;   r<   Nc                 2    t        |   di | || _        y r>   r7  r(  s      rD   rA   zLoggingLoggerVariable.__init__.  r8  rE   c                 ,    t        | j                        S rG   r  rI   s    rD   rJ   z!LoggingLoggerVariable.python_type2  r  rE   c                     | j                   S rG   r,  rI   s    rD   r  z2LoggingLoggerVariable.get_real_python_backed_value5  r-  rE   rT   r6   rU   r{   c                    |j                   rt        j                  j                  d       S t	        | j
                  |d       }t	        |dd       }t        j                  j                  j                  }||v s||v rt        j                  j                  d       S t        dd| j
                   d| d| d| ddg	       y )
Nr   z8logging.Logger method not supported for non-export caseszmethod: r>  r  r  z>logging.Logger methods are not supported for non-export cases.zJAdd the logging method to `torch._dynamo.config.ignore_logging_functions`.rX   )r  r   r/   ry   re   ra   r   r  r   ignore_logging_functionsr   )rB   rT   rU   r{   r;   methodr  
ignore_sets           rD   r   z!LoggingLoggerVariable.call_method8  s     99--44T::T406:t4 ]]))BB
Z8z#9--44T::Ntzzl!D6$z&RX\		
rE   )r   r   r   r8  loggingLoggerr   rA   r`   rJ   r  r   r   r.   r   r   r   r   s   @rD   r  r  )  s    gnn    T  gnn 
#
 
 ?#	

 S/)*
 

rE   r  c            
            e Zd ZdZ	 ddlmZmZm	Z
 dededd	f fd
Zedefd       ZdefdZdddedee   deeef   def
dZdddedefdZ xZS # e$ r  eddi       Z eddi       ZY \w xY w)ConstantLikeVariablez8self.value is a compile-time constant, but not a literalr   )dtypefloatinggenericinvalid_typer?   ra   r;   r<   Nc                 2    t        |   di | || _        y r>   r7  r(  s      rD   rA   zConstantLikeVariable.__init__e  r8  rE   c                     t        | j                        }|j                  dk(  r|j                  S |j                   d|j                   S )z4Dynamically compute the prefix from the value's typerH   r>  )r`   ra   r   r   )rB   ts     rD   _error_prefixz"ConstantLikeVariable._error_prefixi  sD      <<:%>>!,,q 011rE   c                     | j                   S rG   r,  rI   s    rD   r]   z'ConstantLikeVariable.as_python_constantt  r-  rE   rT   r6   rU   r{   c                    g i }}	 |D cg c]  }|j                          }}|j                         D 	ci c]  \  }}	||	j                          }}}	 t        | j                  |      |i |}
t        j                  j                  |
      rt        j                  ||
      S t        |
t        j                        rt        |
      S t        d
| j                   d| d| d| d|
 	d| j                   d| d|
 dg t         j"                  	       y c c}w c c}	}w # t        $ r; t        d| j                   d| d| d| dd| j                   d| ddg	       Y w xY w)Nz0constant-like method call with non-constant argsr>  z(*z, **r#  zAttempted to call z with non-constant args.zFEnsure that the args to the method call are constant (int, str, etc.).rX   z6constant-like method call with unsupported return typez) returned z, got unsupported return value )r]   r   r   r   r  re   ra   r   r/   ru   r.   r   r   reMatchr  r   r   )rB   rT   rU   r{   r;   cargsckwargsr  rW  rX  r   s              rD   r   z ConstantLikeVariable.call_methodw  s    Rw	59:Q))+:E:=C\\^LTQq!..00LGL +T*E=W=%%008"((V44fbhh''//L))*!D6D6fX[QWPXY,T-?-?,@$Gfgmfnnop"..		
' ;L" 	J--.avRvT&K01C1C0DAdVKcd\	 	s-   D! DD! DD! D! !A E%$E%c                    t        | j                  |      }t        || j                        rt	        |      }t        || j
                        rt        |      S t        |t              r!t        || j                        rt        |      S t        j                  j                  |      rt        j                  ||      S t!        | |t        |            S )Nrr   )re   ra   r   np_floatingfloatnp_dtypeNumpyDTypeVariabler`   r_   
np_genericr[  r   r/   ru   r.   r   rv   r1  s       rD   rz   z ConstantLikeVariable.var_getattr  s    T*fd../6]Ffdmm,%f--fd#
64??(K ((%%008"((V44tT4<@@rE   )r   r   r   r8  numpyr  r  r  r  r  r  ImportErrorr`   r   rA   propertyr   r  r]   r   r.   r   r   rz   r   r   s   @rD   r  r  V  s    B
0	
 	
c S T  2s 2 2C %
#%
 %
 ?#	%

 S/)*%
 
%
NA5 AS A_ A  0>2r2B/	0s   
A A0/A0r  c                   ,     e Zd ZdZdeddf fdZ xZS )TorchVersionVariableztorch.__version__r;   r<   Nc                     |j                  dt        j                         |d   t        j                  u sJ t        |   di | y )Nra   r?   )
setdefaultr   __version__r@   rA   r]  s     rD   rA   zTorchVersionVariable.__init__  sA    '5#4#45g%"3"3333"6"rE   )r   r   r   r  r   rA   r   r   s   @rD   r  r    s     'M# # # #rE   r  c                       e Zd ZdefdZy)r  r<   c                 B    | j                   j                  j                  S )a0  Similar to how numpy dtype descriptors (e.g. np.float32 ) are handled by NumpyVariable:

        np.dtype() objects are serialized as strings, torch._numpy wrappers will normalize to the torch dtype.
        This also handles unsupported things nicely (i.e. structured arrays and object arrays).
        )ra   r`   r   rI   s    rD   r  zNumpyDTypeVariable.as_proxy  s     zz'''rE   N)r   r   r   r   r  r?   rE   rD   r  r    s    (# (rE   r  c                   `     e Zd ZdZdeddf fdZdefdZddd	ee	   de
ee	f   dd
fdZ xZS )RandomClassVariablezrandom.Randomr;   r<   Nc                 $    t        |   di | y r>   r\  r]  s     rD   rA   zRandomClassVariable.__init__  r^  rE   c                     t         S rG   rj  rI   s    rD   rJ   zRandomClassVariable.python_type  rk  rE   rT   r6   r{   RandomVariablec                 "   t        |      dkD  s|r%t        dd| d| dg t        j                         t        |      dk(  rt        j
                  j                  d       n|d   }t        |t        j                  j                               S )	Nr+   z'random.Random() with improper argumentszargs: r  z=random.Random() with > 1 arg or with kwargs is not supported.rX   r   )seedr   )
rg   r   r   ro  r   r/   ry   r  baseValueMutationNew)rB   rT   r{   r;   r  s        rD   r   z!RandomClassVariable.call_function  s     t9q=FA j9[&11	 ;>d)q.y))006dSTgY^^%D%D%F
 	
rE   )r   r   r   r8  r   rA   r`   rJ   r   r.   r   r   r   r   r   s   @rD   r  r    s_    # # #T 
#
 '
 S/)*	

 

rE   r  c            
           e Zd ZdZej
                  Zdhej                  Zh dZ		 	 ddej
                  dz  dedz  de
ddf fd	Zdeej
                     fd
Zdej
                  fdZededefd       Zedeeeedf   edz  f   ddfd       Zedeeeedf   edz  f   defd       Zededeeeedf   edz  f   fd       Zdddedee   deeef   def
 fdZddZ xZS )r  zrandom.Random()

    Implemented by wrapping a VariableTracker around a random.Random object.
    The supported methods for the random.Random object cannot be overridden.
    Assumes that random objects behave the same given a set seed or state.
    random>   r  randintuniform	randrangeNrandr  r;   r<   c                 0   t        |   di | |V| j                  |      sJ t        j                         | _        | j                  j                  |j                                y ||j                         nd }t        j                  |      | _        y r>   )r@   rA   is_supported_random_objr  r   setstategetstater]   )rB   r  r  r;   rC   s       rD   rA   zRandomVariable.__init__  sy     	"6"//555 --/DKKK  1040@4**,dD ---DKrE   c                 "    t         j                  S rG   )r  r   rI   s    rD   rJ   zRandomVariable.python_type		  s    }}rE   c                     | j                   S rG   )r  rI   s    rD   r]   z!RandomVariable.as_python_constant	  s    {{rE   r   c                    t        |       t        j                  uryt        j                  t
        j                  d      D ]  }t        | |      s yt        | |      }t        j                  |      r/|t        t        j                  |      j                  |       k7  s_ yt        |dd       t        t        j                  |      us y y)NF)r  r  r  r   T)r`   r  r   r  r  r  _supported_fn_namesr   re   r  	isbuiltinr  )r   rU   meths      rD   r  z&RandomVariable.is_supported_random_obj	  s    9FMM)OO..0P
 	!D 3%3%D  &76==$7??DD 4T2'&--QU:VV 	! rE   state.c                     t        |       t        u sJ t        | d         t        u sJ t        | d         t        u sJ t        d | d   D              sJ | d   t        | d         t        u sJ y y )Nr   r+   c              3   >   K   | ]  }t        |      t        u   y wrG   )r`   r  r  s     rD   r0  z-RandomVariable.check_state.<locals>.<genexpr>'	  s     4a47c>4s   r   )r`   r   r  r  r  )r  s    rD   check_statezRandomVariable.check_state"	  s}    E{e###E!H~$$$E!H~&&&4584444Qx4a>U#:::#:rE   c                 h   t         j                  |        t        j                  t        j                  j                  | d         t        j                  | d   D cg c]!  }t        j                  j                  |      # c}      t        j                  j                  | d         g      S c c}w )Nr   r+   r   )r  r  r   r   r/   ry   )r  r  s     rD   
wrap_statezRandomVariable.wrap_state*	  s    ""5)&&**11%(;''CH8LaY//66q9L **11%(;
 	
 Ms   &B/c                 P    | j                         }t        j                  |       |S rG   )r]   r  r  )r  	state_objs     rD   unwrap_statezRandomVariable.unwrap_state7	  s&     ,,.	""9-rE   rT   r6   rU   r{   c                    dk(  r|j                   j                  j                  |         | j                  j                  |D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}} t        j                  j                  d       S dk(  r)| j                  | j                  j                               S dk(  rq|j                   j                  j                  |        | j                  j                  | j                  |d                t        j                  j                  d       S | j                  v r|j                   j                  j                  |        | j                  j                         dt        dt        dt        ffd} t!        | j                        |D cg c]  }|j                          c}i |j                         D 	
ci c]  \  }	}
|	|
j                          c}
}	 t#        ||||      S t$        | M  |||      S c c}w c c}}w c c}w c c}
}	w )	Nr  r  r  r   r{   r;   r<   c                  r    t        j                         }|j                          t        |      | i |S rG   )r  r   r  re   )r{   r;   rrU   r  s      rD   call_random_methz4RandomVariable.call_method.<locals>.call_random_methW	  s3    MMO

5!'wq$'888rE   )r   r   mutationr  r  r]   r   r   r/   ry   r  r  r  r  r  r   re   r2   r@   r   )rB   rT   rU   r{   r;   r  r9  r   r
  rW  rX  r  rC   s     `        @rD   r   zRandomVariable.call_method?	  s    6>II""++D1DKK267Q!&&(7AGPXS#3..00P --44T::Z??4;;#7#7#9::ZII""++D1KK  !2!247!;<--44T::T---II""++D1KK((*E9 9s 9s 9 'GDKK&267Q!&&(79?HA1a**,,H
 ""&6fEEw"2tT6::7 8P* 8Hs   I5IIIc                 6   j                  fd       j                  dd       j                          j                  d        | j	                  | j
                  j                                      j                  dd       j                          y )Nc                  n     j                   j                  t               j                  d      g      S )Nr   )rQ   create_load_python_moduler  r  rN   s   rD   rO   z,RandomVariable.reconstruct.<locals>.<lambda>h	  s3    G))55f=,,X6 rE   r   Fr  r+   T)rP   r   r  r   r  r  r  pop_toprA  s    `rD   rS   zRandomVariable.reconstructf	  sz    	
 	a' 	*% 4 4 678a&rE   r  r   )r   r   r   r8  r  r   r   r.   r   r  r   rA   r`   rJ   r]   r   r  r  r   r  r  r  r   r  r  r   r   r   r   rS   r   r   s   @rD   r  r    s    MMM 			'	'N
 &*'+.mmd". $. 	.
 
.T&--0 FMM  V   $ ;5eCHout|!CD ; ; ; 

%U38_edl BC 

 

 

 	sE#s(OUT\1	2 %;#%; %; ?#	%;
 S/)*%; 
%;NrE   r  c                        e Zd ZdefdZedddej                  e   de	dz  dedd f
d	       Z
d
edededdf fdZdddee   deeef   defdZddZdefdZdefdZdedefdZ xZS )WeakRefVariabler<   c                 "    t         j                  S rG   )weakrefrefrI   s    rD   rJ   zWeakRefVariable.python_typez	  s    {{rE   rT   r6   weakref_valuer^   Noptionsc                     |J |j                   }|xr t        |d      }t        j                  | ||      } |       }|xr t	        |      }t        j                  | ||      }||d<   t        ||fi |S )N__callback__r^   )r  r   r.   r   r#   r  )	rT   r  r^   r  callbackcallback_sourcecallback_vtreferentreferent_vts	            rD   r   zWeakRefVariable.build}	  s     !!! -- GZ%G%++B/J ?5-f5%++B&A"{KC7CCrE   r  r  c                 @    t        |   di | || _        || _        y r>   )r@   rA   r  r  )rB   r  r  r  rC   s       rD   rA   zWeakRefVariable.__init__	  s%     	#7#&&rE   r{   r;   c                     | j                   S rG   )r  r  s       rD   r   zWeakRefVariable.call_function	  s     rE   c                     j                  fd        | j                          | j                         j                  t	        dd             y )Nc                  (     j                  dd      S )Nr  r  )r  r  s   rD   rO   z-WeakRefVariable.reconstruct.<locals>.<lambda>	  s    g&>&>y%&P rE   r   F)rP   r  r  rQ   r   rA  s    `rD   rS   zWeakRefVariable.reconstruct	  sD    PQ  !  !21e<=rE   c                 6    | j                   j                         S rG   )r  r	  rI   s    rD   r	  z"WeakRefVariable.is_python_hashable	  s    2244rE   c                 6    | j                   j                         S rG   )r  r  rI   s    rD   r  zWeakRefVariable.get_python_hash	  s    //11rE   r  c                 n    t        |t              sy| j                  j                  |j                        S )NF)r   r  r  r  r  s     rD   r  zWeakRefVariable.is_python_equal	  s,    %1//0A0ABBrE   r   )r   r   r   r`   rJ   r   r  ReferenceTyper   r   r   r.   rA   r   r   r   r   rS   r  r	  r  r  r   r  r   r   s   @rD   r  r  y	  s    T  D#D,,S1D D 	D
 
D D '*'9H'UX'	' #  '  S/)*	 
 
 >5D 52 2CV C CrE   r  )r8  rH   r  rv  r  r  r  r  r  r  r  r   r   r  collections.abcr   r   r   r   typingr   r   r	   r
   r   torch._Cr   torch._numpyr`  r{  torch.utils._pytreerM  _pytreer  torch._dynamo.variables.baser   torch._dynamo.variables.listsr   torch._guardsr   rW   r   r   r   r   bytecode_transformationr   r   r   create_parameter_opr   r   r   r   r   guardsr   r   mutation_guardr   r^   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r  r,   r-   r.   r  r/   	functionsr0   r1   user_definedr2   r3   r4   torch._dynamo.codegenr5   torch._dynamo.symbolic_convertr6   r8   r   r   r  r6  r:  r?  rR  rZ  rd  rg  	dataclassr  r  r  r   rv   r  r  r$  r5  	lru_cacher   r   rT  rY  r[  r~  r   r  r  r  r  r  r  r  r  finfoiinfor  rg  r  r  r  r?   rE   rD   <module>r<     sK  "         	 
    .  % @ @   $ $ 5 7   @ @ 
 G K K 0 5    W V & G X X /DQ
O Q
h
-? -(^; ^;Bj4 j4Zo 
o 
2?7 ?7D? 4# #
%c %hsCx6H %c cL M M MD-&? D-N26 2j(_ ($AGo AGHI
O I
X 2/=? /=db
_ b
J Q4 9 95;M;M MN   ( Q(4 2 2E4M4M MN (  (a
O a
J?? ?*-o -PD? PDf_  E EP7o 70*
O *
ZSA? SAl#/ #(- ( II#II#II! 
/ 
<R_ Rj5Co 5CrE   