
    ix                    T   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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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l3m4Z4m5Z5 d
dl6m7Z7m8Z8 d
dl9m:Z: d
dl;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA d
dlmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJ ddlKmLZLmMZMmNZN ddlOmPZPmQZQ ddlRmSZSmTZT ddlUmVZVmWZWmXZX erddlYmZZZ ddl[m\Z\  G d d      Z] G d deN      Z^ G d deN      Z_ G d  d!eN      Z` G d" d#eN      Za G d$ d%eN      Zb G d& d'eb      Zc G d( d)eN      Zd G d* d+eN      Ze G d, d-eN      Zfd.ed/ed0ef   fd1Zg G d2 d3eN      Zhej                   G d4 d5             Zj G d6 d7eX      Zk G d8 d9eX      Zl G d: d;eN      Zm G d< d=eN      Zn G d> d?eN      Zo G d@ dAeN      Zp G dB dCeN      Zq G dD dEeN      Zr ej                  dF      d/etej"                  ej                  f   fdG       Zv ej                  dF      d/etej                  ej"                  f   fdH       Zw G dI dJeN      Zx G dK dLeN      Zy G dM dNeN      Zz G dO dPeN      Z{ G dQ dReN      Z| G dS dTeN      Z} G dU dVeN      Z~ G dW dXeN      Z G dY dZeN      Z G d[ d\e      Z G d] d^e      Zej                  eej                  eej
                  eiZ G d_ d`eN      Z G da dbeN      Z G dc ddeN      Zy)ea  
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NoReturn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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list_methodsproxy_args_kwargsraise_args_mismatchtuple_methods   )#AsPythonConstantNotImplementedErrorraise_type_error_excVariableTracker)CONSTANT_VARIABLE_NONEConstantVariable)NestedUserFunctionVariableUserFunctionVariable)call_random_fnis_standard_setattrUserDefinedObjectVariable)	PyCodegen)InstructionTranslatorc                       e Zd Zy)NO_SUCH_SUBOBJN)__name__
__module____qualname__     m/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/torch/_dynamo/variables/misc.pyr;   r;   V   s    r@   r;   c            
            e Zd Zh ej                  Z	 ddededz  deddf 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 Nr?   )super__init__rD   rE   )selfrD   rE   rF   	__class__s       rA   rK   zSuperVariable.__init___   s'     	"6" 
 r@   c                     j                  fd        | j                         | j                  . | j                         j                  t	        dd             y j                  t	        dd             y )Nc                  B     t        j                  t                    S N)r   BuiltinVariablerJ   )rM   codegens   rA   <lambda>z+SuperVariable.reconstruct.<locals>.<lambda>q   s    gi.G.G.N&O r@   r   Fr-   )add_push_nullrD   rE   extend_outputr   rL   rR   rM   s    `rA   reconstructzSuperVariable.reconstructp   s`    OP;;"DKK !!"6q%"@A!!"6q%"@Ar@   txr9   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()`.)rE   r   rD   as_python_constantpython_typesourcer"   
issubclasstypevalue__mro__index
ValueErrorgetattrrJ   rangelen__dict__getr;   r   r    r!   )rL   rX   rY   search_typetype_to_usetype_to_use_sourcerc   
search_mrostart_indexrh   resolved_getattrrM   s              rA   _resolved_getattr_and_sourcez*SuperVariable._resolved_getattr_and_sourcey   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	        | |      S |r(t        |j                  t        j                               t        j                  j                  ||      S )Nrc   )
ru   r   r2   
is_literalGetAttrVariabler   
make_guardr   CONSTANT_MATCHcreate)rL   rX   rY   rf   rc   s        rA   var_getattrzSuperVariable.var_getattr   ss     99"dCv))44U;"4..&++L,G,GHI))00v0FFr@   argsc                 R   | 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                  |      rx|j8                  }
t1        |
d      r%|
j:                  dk(  rt        j<                  |
      }n'|J |j>                  }t        j4                  |
|      }|jA                  |d||      S t        |tB              rZt        |jD                  tF        jH                        r6t!        j"                  ||jD                  |d      }	|	j'                  |||      S t        |tJ              rt        |jD                  tF        jH                        rt        | j                  t        j4                        r| j                  }nbd }| j                  j.                  rtM        | j                  j.                        }t!        j"                  || j                  jN                  |      }|J t!        j"                  ||jD                  tQ        |d      d      }	|	j'                  ||g||      S t        |tF        jH                        r:t!        j"                  |||d      }	|	j'                  || j                  g|z   |      S t        |tF        jR                        r=t        jT                  |jD                  | j                  |      j'                  |||      S tW        |      r:t        | j                  t              r  | j                  jX                  |g|i |S |t        jZ                  u r|d   }	 |j]                         }|j`                  j                  jc                  | j                        s4t)        dd|  d| d| j                   ddgt*        jd                  	       t        |tf              sJ |j`                  j                  ji                  | j                  |t        jj                                t        jl                  S t        | j                  t        jn                        r@|| j                  jp                  v r(| j                  jr                  jA                  ||||      S t        | j                  t        jt                        r@|| j                  jv                  v r(| j                  jx                  jA                  ||||      S t        | j                  t        jz                        r0|t|        v r(| j                  j~                  jA                  ||||      S t        | j                  t        j                        r0|t        v r(| j                  j                  jA                  ||||      S |t        j                  u r|d   j                  }|j`                  j                  j                  | j                  |      r^|j`                  j                  j                  | j                  |d      }t        |t        jj                        rt        t        |       |S d }	 t        j                  | j                  j                  |      }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                  _O        	 |j'                  |||      ||j                  _O        S t        |tF        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                  _O        w xY w)#Nr   )AttributeMutationNewrw   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.r\   r<   __new__r=   builtinsTrc   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.)r]   r^   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.)Sru   rE   objectrK   LambdaVariabler&   torchnnModuleside_effectsr   
isinstancer   r7   mutation_typer   r0   buildr   call_functionr   r   	DIFFICULTrc   hasattrr<   UserDefinedClassVariableis_supported_new_method__self__r=   rQ   membercall_methodstaticmethodr   typesFunctionTypeclassmethodr"   
value_typer   
MethodTypeUserMethodVariabler6   method_setattr_standard__delattr__ra   NotImplementedErroroutputis_attribute_mutation
DYNAMO_BUGstr
store_attrDeletedVariabler1   UserDefinedDictVariable_dict_methods_dict_vtUserDefinedSetVariable_set_methods_set_vtUserDefinedTupleVariabler,   	_tuple_vtUserDefinedListVariabler)   _list_vt__getattribute__rf   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)rL   rX   rY   r~   rF   inner_fnrc   rE   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                                rA   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/8/H/H/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#<#<#> 333t{{I$E$EFDKK555;;''33BdFKKt{{I$D$DEDKK444;;&&222tT6JJt{{I$F$FGM);;((44RtVLLt{{I$E$EFL(;;''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,, `
	
Q Qh ' 
U*4&$8!L ` !	 	
H " =(<=8  00PsB   ;=d9;e 6*e3 f 9e	e0e++e03fff&rP   rR   r8   rG   N)r<   r=   r>   r0   _nonvar_fieldsr   rK   rW   r   tupler   ru   r}   listdictr   __classcell__rM   s   @rA   rC   rC   Z   s    		'	'N *.   $& 	
 
"BB
)B
14B
	sJ%%	&B
HG5 GS G_ G e
#e
 e
 ?#	e

 S/)*e
 
e
r@   rC   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_summaryrF   rG   Nc                 2    t        |   di | || _        y rI   )rJ   rK   r   )rL   r   rF   rM   s      rA   rK   zFrameSummaryVariable.__init__  s    "6"*r@   c                 "    t         j                  S rP   )	tracebackFrameSummaryrL   s    rA   rb   z FrameSummaryVariable.python_type  s    %%%r@   rX   r9   rY   c                    |dk(  r3t         j                  j                  | j                  j                        S |dk(  r3t         j                  j                  | j                  j
                        S |dk(  r3t         j                  j                  | j                  j                        S |dk(  r3t         j                  j                  | j                  j                        S t        | %  ||      S )NlinenofilenamerY   line)
r   r2   r|   r   r   r   rY   r   rJ   r}   rL   rX   rY   rM   s      rA   r}   z FrameSummaryVariable.var_getattr  s    8--44T5G5G5N5NOOZ--44T5G5G5P5PQQV^--44T5G5G5L5LMMV^--44T5G5G5L5LMMw"2t,,r@   )r<   r=   r>   r   r   r   rK   re   rb   r   r0   r}   r   r   s   @rA   r   r     sP    +i&<&< + +PT +&T &	-5 	-S 	-_ 	- 	-r@   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_nextrF   rG   Nc                 H    t        |   di | || _        |J || _        y rI   )rJ   rK   r   r   )rL   r   r   rF   rM   s       rA   rK   zTracebackVariable.__init__  s0     	"6"*"""r@   c                 (     | t        |      |      S )N)r   )r   )clsr   r   s      rA   from_frame_summaryz$TracebackVariable.from_frame_summary  s     '6HHr@   objc                 j    t        | t              xs" t        | t              xr | j                         S rP   )r(   r   r2   is_constant_noner   s    rA   is_valid_tracebackz$TracebackVariable.is_valid_traceback  s0    c,- 
3()Dc.B.B.D	
r@   c                     t        | j                  t              r| j                  gS | j                  g| j                  j	                         z   S rP   )r(   r   r2   r   
extract_tbr   s    rA   r   zTracebackVariable.extract_tb  sA    $,, 01&&''""#dll&=&=&???r@   tbc                 l    | }t        |t              r"||u ry|j                  }t        |t              r"y)NTF)r(   r   r   )rL   r   curr_tbs      rA   has_reference_cyclez%TracebackVariable.has_reference_cycle  s7    8<W/0"}ooG W/0 r@   c                 "    t         j                  S rP   )r   TracebackTyper   s    rA   rb   zTracebackVariable.python_type  s    """r@   rX   r9   name_varvalc                 P   |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                  S )Nr   )ra   r   r   	TypeErrorr   r   r2   r   r(   ri   r   r   r1   )rL   rX   r   r   rY   s        rA   call_setattrzTracebackVariable.call_setattr  s     **,9**3/(B7c$57G#HIII'',s-.33J3J43P(R8DL///r@   rY   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.r\   )r   r   r}   r   r   SUPPORTABLErJ   r   s      rA   r}   zTracebackVariable.var_getattr  s    9<<[ %%11"h??_$%%%Z:& 56l6)556	 w"2t,,r@   r~   c                     |dk(  r$t         j                  j                  | |d   u       S |dk(  r | j                  |g| S t        |   ||||      S )N__eq__r   __setattr__)r   r2   r|   r  rJ   r   )rL   rX   rY   r~   rF   rM   s        rA   r   zTracebackVariable.call_method  sb     8--44TT!W_EE]"$4$$R/$//w"2tT6::r@   )r<   r=   r>   r   r   r2   r   rK   r   r   r   r   r   r0   boolr   r   r   r   re   r   r   rb   r  r   r}   r   r   r   r   s   @rA   r   r     s{   + *,<<= 	
 
$ I --I *,<<=I 
	I I 
 
D 
 

@D!7!7:N!NO @
o $ #T%"5"56 #0#0 "0 	0
 
0$-5 -S -_ - ;#; ; ?#	;
 S/)*; 
; ;r@   r   c                        e Zd Z	 	 	 ddedeedf   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 xZS )ExceptionVariableNexc_typer~   .init_kwargsrc   r   rG   c                     t         |   ||       || _        || _        |r$t	        d|  d| dg t
        j                         t        | _        t        | _	        t        d      | _        t        | _        d | _        y )N)rc   r   z,Keyword args passed to exception constructorz with kwargs zRDynamo does not know how to handle keyword args passed to an exception constructorr\   F)rJ   rK   r  r~   r   r   r  r1   __context__	__cause__r2   __suppress_context____traceback__python_stack)rL   r  r~   r  rc   r   rM   s         rA   rK   zExceptionVariable.__init__.  s     	mD 	F&k];p6)556	 -C +A5Ee5L!.D <@r@   r^   c                     || _         y rP   )r  )rL   r^   s     rA   set_contextzExceptionVariable.set_contextO  s
    "r@   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 )Nr   )load_import_fromr  r<   rR   rL   s   rA   rS   z/ExceptionVariable.reconstruct.<locals>.<lambda>T  s    G,,Z9O9OP r@   FrY   rG   c                     t        |       }t        |t              r|j                  dv sJ |       y j	                           |       j                  j                  d             j                  |        y )N)TFNr   )rj   r(   r2   rf   dup_toprU   rot_nr   )rY   r   rR   rL   s     rA   codegen_attrz3ExceptionVariable.reconstruct.<locals>.codegen_attrY  sh    4&Dd,-zz%88>$>8!%%gmmA&67""4(r@   r  r  r  )rT   foreachr~   r   rl   r   )rL   rR   r  s   `` rA   rW   zExceptionVariable.reconstructR  sl    P	
 			"c$))ne4	)s 	)t 	) 	]#[!+,r@   c                     | j                   S rP   )r  r   s    rA   rb   zExceptionVariable.python_typeg  s    }}r@   rX   r9   r   r   c                    dt         dt        ffd}|j                         }|dk(  r9t        |t        t
        f      sJ | j                  |       t        j0                  S |dk(  r|j                         sHt        |t        j                  t        j                  t        j                  t        j                  f      r1|| _        t        j
                  d      | _        t        j0                  S  |d       t        j0                  S |dk(  rA|j                  dd	      r|| _        t        j0                  S  |d       t        j0                  S |d
k(  rRt        j!                  |      s&t#        t$        t        j&                  d      g       || _        t        j0                  S t+        dd|  d| d| dg t,        j.                         t        j0                  S )NmsgrG   c                 >    t        t        t        |       g       y )Nr~   )r   r  r2   )r#  rX   s    rA   raise_errorz3ExceptionVariable.call_setattr.<locals>.raise_errorp  s    $Y:J3:O9PQr@   r  r  Tz9exception cause must be None or derive from BaseExceptionr  Fr  z0__traceback__ must be a traceback object or Noner%  z4Unsupported 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.r\   )r   r	   ra   r   r  r2   r  r   r   rQ   !UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariabler  r  is_constant_matchr   r   r   r  r|   r  r   r   r  r1   )rL   rX   r   r   r&  rY   s    `    rA   r  zExceptionVariable.call_setattrj  s   	RS 	RX 	R **,= c$57G#HIIIS!R ///Q [ ##%--//??@@	* "%,5,F,Ft,L): ///7 WX6 ///5 ++$$T51,/)0 ///- WX, ///+ _$$77<((//N "%D /// N'vQtf56 ZZ 7)556 ///r@   rY   rF   c                     |dk(  r | j                   |g| S |dk(  r"|\  }| j                  |t        d      |       | S t        |   ||||      S )Nr
  with_tracebackr  )r  r2   rJ   r   )rL   rX   rY   r~   rF   r   rM   s         rA   r   zExceptionVariable.call_method  sh     = $4$$R/$//%%DRb"2?"CRHK7&r4v>>r@   c                    |dk(  r| j                   S |dk(  r| j                  S |dk(  r| j                  S |dk(  r| j                  S |dk(  r4t	        j
                  t        | j                        | j                        S t        | )  ||      S )Nr  r  r  r  r~   rw   )r  r  r  r  r   ListVariabler   r~   rc   rJ   r}   r   s      rA   r}   zExceptionVariable.var_getattr  s    = ###[ >>!++,,,_$%%%V^))$tyy/$++NNw"2t,,r@   c                 N    | j                   j                   d| j                   dS )N())rM   r<   r  r   s    rA   __str__zExceptionVariable.__str__  s$    ..))*!DMM?!<<r@   )NNNr   )r<   r=   r>   r   r   r0   r   r   r   r   rK   r  rW   re   rb   r  r   r   r}   r1  __repr__r   r   s   @rA   r  r  ,  s9    :> $-1@@ OS()@ #./$6	@
 @ $d*@ 
@B#? #t #-*T 60#60 "60 	60
 
60p?#? ? ?#	?
 S/)*? 
? -5 -S -_ -= = Hr@   r  c                       e Zd ZdZy)UnknownVariablez
    It could be anything!
    Nr<   r=   r>   __doc__r?   r@   rA   r4  r4    s    r@   r4  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.
    Nr#  rF   rG   c                 2    t        |   di | || _        y rI   )rJ   rK   r#  )rL   r#  rF   rM   s      rA   rK   z DelayGraphBreakVariable.__init__  s    "6"r@   rX   r9   r~   c                     | j                   dn| j                   j                  }t        dd| j                    d| d| j                   g        y )Nr[   z#Unsupported function call (delayed)zsource: z@Dynamo determined that a graph break should occur when calling `z`. Reason: r\   )rc   rY   r   r#  )rL   rX   r~   rF   rY   s        rA   r   z%DelayGraphBreakVariable.call_function  sS     [[(rdkk.>.>9t{{m,!F+dhhZ9	
r@   rP   )r<   r=   r>   r6  r   r   rK   r   r0   r   r   r   r   s   @rA   r8  r8    s`    C$J   
#
 '
 S/)*	

 

r@   r8  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
    rG   Nc                     t        d      )Nzcomptime is special form)r   rL   rR   s     rA   rW   zComptimeVariable.reconstruct  s    !"<==r@   rX   r9   rY   c                     ddl m } | j                  J t        j                  |t	        ||      t        | j                  |            S )Nr   )comptimerw   )r@  rc   r0   r   rj   r   )rL   rX   rY   r@  s       rA   r}   zComptimeVariable.var_getattr  sA    '{{&&&$$$'
4;;0M
 	
r@   r~   rF   c           
         ddl m} |st        |      dkD  r&t        |ddt        |       dt        |       d       |d   }t	        |t
              r, |j                          ||             t,        j.                  S t	        |t              r|j                         }|j                  rt        |d|j                          t        j                  ||j                  |j                  j!                         |j"                  rt%        |j"                  j&                        nd d	      } | ||             t,        j.                  S t)        d
t+        |             )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: )r@  rB  rl   r+   r   r4   get_functionr3   get_codeclosurer/   co_freevarsr   r   	f_globalsfn_namera   defaultsr   r   RuntimeErrorre   r   r1   )rL   rX   r~   rF   rB  fncoder   s           rA   r   zComptimeVariable.call_function  s6    	/ SY]-t9+ZF}G<	 !Wb./BOOob124 ///3 67;;=Dzz$efjfvfvewx %%

--/,.KKbkk''(T D $% /// !CDH:NOOr@   r   )r<   r=   r>   r6  rW   r   r0   r}   r   r   r   r?   r@   rA   r<  r<    sa    
>
5 
S 
_ 
-0#-0 '-0 S/)*	-0
 
-0r@   r<  c                   \     e Zd ZU 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 xZ	S )CellVariableNpre_existing_contents
local_namerF   rG   c                 2    t        |   di | || _        y rI   )rJ   rK   rP  )rL   rP  rF   rM   s      rA   rK   zCellVariable.__init__3  s     	"6"%:"r@   rP   )
r<   r=   r>   r0   __annotations__rQ  r   r   rK   r   r   s   @rA   rO  rO  &  sM     +T11 "Jd
! ?C;%4t%;;NQ;	; ;r@   rO  c                   (     e Zd Zdeddf fdZ xZS )NewGlobalVariablerF   rG   Nc                 $    t        |   di | y rI   rJ   rK   rL   rF   rM   s     rA   rK   zNewGlobalVariable.__init__;      "6"r@   )r<   r=   r>   r   rK   r   r   s   @rA   rU  rU  :  s    # # # #r@   rU  fn_clsrG   .c                 N     dt         dt         dt         f fd}t        |_        |S )Nr~   rF   rG   c                  (     j                   | i |S rP   )apply)r~   rF   rZ  s     rA   trampoline_autograd_applyzDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply@  s    v||T,V,,r@   )r   !produce_trampoline_autograd_apply_origin)rZ  r^  s   ` rA   r_  r_  ?  s/    - - - - )J%$$r@   c            
            e Zd ZdZdhej
                  Z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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 subclassrZ  rF   rG   Nc                 2    t        |   di | || _        y rI   )rJ   rK   rZ  )rL   rZ  rF   rM   s      rA   rK   z!AutogradFunctionVariable.__init__P  s    "6"r@   rX   r9   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   rG   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   rg  rX   s    rA   visitz2AutogradFunctionVariable.call_apply.<locals>.visit\  sJ    ||~##50$(M"i889>>"%$(M & :r@   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.r\   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-   )AutogradFunctionApplyVariabler]  )r   rw   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.)/r0   rj  r   is_grad_enabledrc   "torch._functorch.autograd_functionrk  torch.autograd.functionrl  rZ  rn  ro  vjpautogradFunctionr   r   r  jvphigher_order_opsrm  r   import_sourcer=   r<   backwardr   r   rz   r   CLOSURE_MATCHAutogradFunctionContextVariabler|   r   r   r   inspect	signaturerl   
parametersr   r   r   r   r   r   re   )rL   rX   r~   rF   rj  rc   rk  rl  
forward_fnis_setup_ctx_definedvjp_fnjvp_fnrm  apply_sourcer   fwd_srcsetup_ctx_srcrL  ctxsigr   rg  s    `                   @rA   
call_applyz#AutogradFunctionVariable.call_applyT  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
r@   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   ry  Tr   )rZ  ry  re   rf   r   _dynamoexternal_utilsFakeBackwardCFunctionr   r   r   rc   r   r0   r   r   )rL   rX   r~   rF   rL  	fn_sourcer   s          rA   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4r@   c                 ,    t        | j                        S rP   )rb  rZ  rL   rX   r~   rF   s       rA   r   z&AutogradFunctionVariable.call_function  s     (44r@   rY   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_proxyr]  r   )rX   proxyry  rw   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.r\   )builderr  r   is_callable_allowedrZ  r_  r   create_proxyr*   r  r  rc   r   r|  getattr_staticr   r   r   __get__lookupcreate_with_sourcer   r   r   r   r   r   )rL   rX   rY   r~   rF   r  r^  rc   r   r   traceds              rA   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>   r6  r0   r   r   rK   r   r   r   r  r  r   r   r   r   r   s   @rA   rb  rb  H  s8   7 			'	'N
s c d {#{ ?#{ S/)*	{
 
{z5#5 ?#5 S/)*	5
 
55#5 '5 S/)*	5
 
$5@#@ @ ?#	@
 S/)*@ 
@r@   rb  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  r0   rS  r?   r@   rA   r  r  +  s    %6[%6%6t%LGT/"Lr@   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_tensorsNrf   r   needs_input_grad.non_differentiablerF   rG   c                 b    t        |   d||d| || _        || _        || _        || _        y N)rf   r   r?   )rJ   rK   r  r  r  r  )	rL   rf   r   r  r  r  r  rF   rM   s	           rA   rK   z(AutogradFunctionContextVariable.__init__<  s;     	FuFvF"* 0"4r@   rX   r9   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rP   )rf  rg  .0xs     rA   	<genexpr>z9AutogradFunctionContextVariable.create.<locals>.<genexpr>U  s"     $U1Q[[]%Fq%F$Us   (*T)r  r  r  )r   r   r   track_object_newr   rt  functionFunctionCtx	functoolspartialr{  r  )rX   r~   rF   r  outs        rA   r|   z&AutogradFunctionContextVariable.createL  sz      $$UPT$UUii$$55NN##///,.!1	 

 
r@   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.r\   )r  r   r   r   r   s    rA   as_proxyz(AutogradFunctionContextVariable.as_proxyc  sA    ::'#D6*25)445 zzr@   rY   c                    |dk(  rt         |   ||||      S |dk(  r@|rt        ||dt        |       d       t	        |i       d   | _        t        j                  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                  S )Nr
  mark_non_differentiablez0 kwargsrC  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`.r\   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)rJ   r   r+   rl   r*   r  r   r1   r   r   r  r  r  rc   r/   r   r   track_save_for_backwardr  append)rL   rX   rY   r~   rF   argrM   s         rA   r   z+AutogradFunctionContextVariable.call_methodp  s    = 7&r4v>>..#BjS[M:QR&7b&A!&DD#333&&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///r@   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 rP   )r   r   )r~   rF   rY   rL   rX   s     rA   rS   z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>  s    (8(8T4:v(V r@   r  r  )r   r  r   r   r   r  r  r2   r|   rc   r   r0   r   rf   rJ   r}   rL   rX   rY   rc   rM   s   ``` rA   r}   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,,r@   )NFNNNNN)r<   r=   r>   r6  r7   r   r   re   r  r   rK   r   r   r0   r   r   r|   r  r   r   r}   r   r   s   @rA   r{  r{  0  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0#30 30 ?#	30
 S/)*30 
30j-5 -S -_ - -r@   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.
    Nrf   r   rF   rG   c                 *    t        |   d||d| y r  rW  )rL   rf   r   rF   rM   s       rA   rK   zAutogradEngineVariable.__init__  s     	FuFvFr@   rX   r9   rY   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.r\   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_breakr0   r   r  FakeCompiledAutogradEnginer  r   r   r   get_ca_final_callbacks_varr   )rL   rX   rY   r~   rF   r   s         rA   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r@   rP   )r<   r=   r>   r6  r   r   _ImperativeEnginere   r   rK   r   r   r0   r   r   r   r   s   @rA   r  r    s     ?CGxx))G 334t;G 	G
 
G%#% % ?#	%
 S/)*% 
%r@   r  c                   ^     e Zd Zdedef   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 )r   rL  .rF   rG   Nc                 2    t        |   di | || _        y rI   )rJ   rK   rL  )rL   rL  rF   rM   s      rA   rK   zLambdaVariable.__init__  s    "6"r@   rX   r9   r~   c                 &     | j                   |i |S rP   )rL  r  s       rA   r   zLambdaVariable.call_function  s     tww'''r@   )r<   r=   r>   r   r0   r   rK   r   r   r   r   r   r   s   @rA   r   r     sa    8C$89 S T (#( '( S/)*	(
 
(r@   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dddedee   deeef   def
 fdZdddefdZ xZS )ry   rY   py_typeNr   rF   rG   c                     t        |   di | t        |t              sJ t        |t              sJ || _        || _        || _        y rI   )rJ   rK   r   r0   r   r   rY   r  )rL   r   rY   r  rF   rM   s        rA   rK   zGetAttrVariable.__init__  sJ     	"6"#///$$$$	r@   c                 P    | j                   | j                   S t        | 	         S rP   )r  rJ   rb   rL   rM   s    rA   rb   zGetAttrVariable.python_type  s%    <<#<<7&((r@   c                 h    | j                   j                   d| j                   d| j                   dS Nr/  z, r0  )rM   r<   r   rY   r   s    rA   r2  zGetAttrVariable.__repr__  s.    ..))*!DHH:R		{!DDr@   
base_proxyr   c                     t        | |      S rP   )rj   )r  r   s     rA   create_getattr_proxyz$GetAttrVariable.create_getattr_proxy  s    z4((r@   c                 r    t         j                  | j                  j                         | j                        S rP   )ry   r  r   r  rY   r   s    rA   r  zGetAttrVariable.as_proxy  s&    33DHH4E4E4GSSr@   c                     | j                   j                         }	 t        || j                        S # t        $ r t        |  d      d w xY w)Nz is not a constant)r   ra   rj   rY   r   r   )rL   constants     rA   ra   z"GetAttrVariable.as_python_constant   sQ    88..0	M8TYY// 	M%.@&ABL	Ms	   2 ArX   r9   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 rP   )r   r   r   rh  r   r   get_submodule
module_keyrY   rm   r|  r  )rL   rX   rY   step1step2s        rA   const_getattrzGetAttrVariable.const_getattr'  s    $((I$>$>?%%		''(;(;<99ENN*%%&&udii8u~~%%%%%eT22r@   c                 |     || j                          |j                  |j                  | j                               y rP   )r   rU   create_load_attrsrY   r>  s     rA   rW   zGetAttrVariable.reconstruct2  s+    g77		BCr@   r~   c                 d    | j                   j                  || j                  t        |      |      S rP   )r   r   rY   r   r  s       rA   r   zGetAttrVariable.call_function6  s'     xx##B		4:vFFr@   c                    |dv r| j                   dk(  r|s|d   j                         rt        | j                  t        j
                  t        j                  t        j                  f      rl| j                  }|d   j                         }|j                  ||      r|j                  ||      S |dk(  rt        |      dk(  r|d   S t        j                  S |dk(  r| j                   dk(  rt        |      dk(  r|d   j                         r|st        | j                  t        j
                  t        j                  t        j                  f      r[| j                  }|d   j                         }|j                  ||      rt	        j                  d      S t	        j                  d	      S |d
k(  r| j                   dk(  r|st        | j                  t        j
                        r%| j                  j                  ||d   |d   d      S t        | j                  t        j                        r| j                  j                  |       t         | E  ||||      S )N)__getitem__rn   rm   r   rn   r   r-   __contains__TF__setitem__)directly_update_dict)rY   is_python_constantr   r   r   r7   rh  r   ra   has_key_in_generic_dictr}   rl   r1   r2   r   convert_to_unspecializedrJ   r   )rL   rX   rY   r~   rF   r   keyrM   s          rA   r   zGetAttrVariable.call_method>  s	    **		Z'Q**,77..66 ((Cq',,.C**2s3r3// u}t9>7N$;;; N"		Z'D	QQ**,77..66 ((Cq',,.C**2s3 11$77 11%88]"tyyJ'>v$((I$G$GHxx77Qat 8   $((I$>$>?11"5w"2tT6::r@   c                 j   | j                   dk(  rSt        | j                  t        j                        r/|j
                  j                  j                  | j                        rJ d| j                  _        t        j                  || j                  j                  j                  | j                        S )Nrm   T)rY   r   r   r   r   r   r   has_pending_mutationban_mutationr0   r   rf   rm   rc   )rL   rX   s     rA   get_forwarded_dictz"GetAttrVariable.get_forwarded_dict  s~    II#488Y%G%GHII**??I	
J !%$$R)@)@$++NNr@   rP   r   )r<   r=   r>   r0   r   r   re   r   rK   rb   r2  r   r   fxProxyr  r  ra   r  rW   r   r   r   r   r   r  r   r   s   @rA   ry   ry     s    
	'	'N  $	  	
  
)T )E# E ) )s )s ) )T# TMC M	3 7 	3s 	3s 	3DG#G 'G S/)*	G
 
GB;#B; B; ?#	B;
 S/)*B; 
B;HO%< O Or@   ry   c                        e Zd Zdej                  deddf 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_wrapperrF   rG   Nc                 2    t        |   di | || _        y rI   )rJ   rK   r  )rL   r  rF   rM   s      rA   rK   zMethodWrapperVariable.__init__  s    "6",r@   rX   r9   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(  rVt        |      j                  }|t        j                  u rt        j                  t              j                  |||g||      S t        j                   dk\  r|t        j"                  d   u ry|dk(  rtddlm} t        |      dk(  r5|s3	 |j)                  || j                  |d   j+                                     S t3        d	d
|  d| d| dddgt4        j6                         n|t        j"                  d   u r|dk(  s|t        j"                  d   u r|dk(  rddlm} t        |      dk(  r5|s3	 |j)                  || j                  |d   j+                                     S |t        j"                  d   u rdnd}t3        d| dd
|  d| d| d| dd| dd| dgt4        j6                         t8        	| u  |||      S # t,        $ r t/        t,        |       Y (t0        $ r Y 3w xY w# t0        $ r Y w xY w)Nr   r-   z2tensor attribute getter takes exactly one argumentrK   )r      rS  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`.r\   rg   rm   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   TensorVariablerl   r/   r}   r   r<   re   rK   r   rQ   r   sysversion_inform   r  r  r|   ra   r   r   r.   r   r   r  rJ   r   )
rL   rX   r~   rF   self_objwrapper_namefn_objr  r   rM   s
            rA   r   z#MethodWrapperVariable.call_function  s    &d&9&9:zGY--@
 INs6{a'7$L 7&&r4+>+>+G+G+P+PQQ &&//**33 :%(^,,F( 00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24yA~f,33D//Q0J0J0LM  &y)AA	z  5i[P(htfJvhOST]S^ _? ? =YK HF F?	{ KR R
 '22 w$Rv66a & A,^R@: 4 ; s*   1J 1K K 4K ?K 	KKTc                      yNTr?   r   s    rA   r  z(MethodWrapperVariable.is_python_constant      r@   c                     | j                   S rP   )r  r   s    rA   ra   z(MethodWrapperVariable.as_python_constant  s    """r@   c                      yr  r?   r   s    rA   is_python_hashablez(MethodWrapperVariable.is_python_hashable  r  r@   c                 4    t        | j                               S rP   hashra   r   s    rA   get_python_hashz%MethodWrapperVariable.get_python_hash      D++-..r@   otherc                 h    t        |t              xr! | j                         |j                         k(  S rP   r   r0   ra   rL   r  s     rA   is_python_equalz%MethodWrapperVariable.is_python_equal   1    uo. H'')U-E-E-GG	
r@   )r<   r=   r>   r   MethodWrapperTyper   rK   r   r0   r   r   r   r   r  ra   r
  intr  r   r  r  r   r   s   @rA   r  r    s    -u'>'> -# -RV -a7#a7 'a7 S/)*	a7
 
a7FGDM #E$;$; #GDM / /
V 
 
r@   r  c                        e Zd Zdej                  deddf 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descrF   rG   Nc                 2    t        |   di | || _        y rI   )rJ   rK   r  )rL   r  rF   rM   s      rA   rK   z!GetSetDescriptorVariable.__init__  s    "6"	r@   rX   r9   rY   c                     |dk(  rM| j                   rAt        | j                   d      }t        j                  || j                  j
                  |      S t        |   ||      S )Nr  )rc   r   r0   r   r  r  rJ   r}   r  s       rA   r}   z$GetSetDescriptorVariable.var_getattr  sR    9Y7F"((TYY->->GG7&r400r@   Tc                      yr  r?   r   s    rA   r  z+GetSetDescriptorVariable.is_python_constant  r  r@   c                     | j                   S rP   )r  r   s    rA   ra   z+GetSetDescriptorVariable.as_python_constant  s    yyr@   )r<   r=   r>   r   GetSetDescriptorTyper   rK   r   r0   r}   r   r  ra   r   r   s   @rA   r  r    s`    U77 3 4 15 1S 1_ 1GDM E$>$> r@   r  c                        e Zd 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fd	Zd
ddedefdZd
ddedefdZ xZS )PythonModuleVariablerf   is_torchrF   rG   Nc                     t        |   di | || _        | j                  t        u xs% | j                  j                  j                  d      | _        y )Nztorch.r?   )rJ   rK   rf   r   r<   
startswithr"  rL   rf   rF   rM   s      rA   rK   zPythonModuleVariable.__init__!  sD    "6"


e+Wtzz/B/B/M/Mh/Wr@   c                 "    t         j                  S rP   )r   
ModuleTyper   s    rA   rb   z PythonModuleVariable.python_type&  s    r@   c                     | j                   S rP   rf   r   s    rA   ra   z'PythonModuleVariable.as_python_constant)      zzr@   c                 "    d| j                    dS )NzPythonModuleVariable(r0  r)  r   s    rA   r2  zPythonModuleVariable.__repr__,  s    &tzzl!44r@   rX   r9   rY   c                 l    t        | j                  |      }t        j                  j	                  |      S rP   )r   rf   r   r2   r|   rL   rX   rY   r   s       rA   call_obj_hasattrz%PythonModuleVariable.call_obj_hasattr/  s+     T*))0088r@   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rP   )r   r   r   r   r"  rf   rm   rj   r   r   rc   r   r0   r   )rL   rX   rY   r   rc   s        rA   r}   z PythonModuleVariable.var_getattr5  s    99!!>>tTJ99))33D$??
==D

(;(;;=$TZZ6
 ,,T2J>DKK!>$$RV<< " =(<=s   4C C;:C;)r<   r=   r>   r0   r   r   r'  r   rK   re   rb   ra   r   r2  r2   r.  r}   r   r   s   @rA   r!  r!    s     
	'	'NXe.. X# X$ X
 T%"2"23  E$4$4 5# 59)9149	9=5 =S =_ =r@   r!  c            
            e Zd Zde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	ddde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 )TypingVariablerf   rF   rG   Nc                 2    t        |   di | || _        y rI   rJ   rK   rf   r%  s      rA   rK   zTypingVariable.__init__G      "6"
r@   rX   r9   rY   r~   c                    |dk(  r9t        |      dk(  r+| j                  |d   j                            }t        |      S |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   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 r\   )rl   rf   ra   r1  r(   r   r2   r|   r   r   r  )rL   rX   rY   r~   rF   
new_typingr   s          rA   r   zTypingVariable.call_methodK  s    = SY!^DG$>$>$@AJ!*--X4yA~fQ8XTZZ4PQ7===X 1188@@B'

|?4&QUPVV`ag`hiHNfgkgqgqfrrst$TF+djj\C"..		
r@   c                    ddl m}m} |t        v rt	        j
                  | |      S |j                  j                  j                  | |      r&|j                  j                  j                  | |      S t        | j                  |      }| j                  r%t        | j                  |      }  |||      |      S |j                  ||      S )Nr-   )r  VariableBuilder)r  r  r9  r%   r   ry   r   r   r   r   rj   rf   rc   r   r|   )rL   rX   rY   r  r9  rf   r   s          rA   r}   zTypingVariable.var_getattrd  s    ?)),,T48899!!>>tTJ99))33D$??

D);;$T[[$7K3?2{3E::$++B66r@   c                     | j                   S rP   r)  r   s    rA   ra   z!TypingVariable.as_python_constantt  r*  r@   c                     t        | j                  t        j                        st        |   |      S |j                  |j                  | j                               y rP   )r   rf   r   GenericAliasrJ   rW   append_outputcreate_load_constrV   s     rA   rW   zTypingVariable.reconstructw  sF    $**e&8&897&w//0 	g77

CDr@   Tc                      yr  r?   r   s    rA   r
  z!TypingVariable.is_python_hashable  r  r@   c                 4    t        | j                               S rP   r  r   s    rA   r  zTypingVariable.get_python_hash  r  r@   r  c                 h    t        |t              xr! | j                         |j                         k(  S rP   r  r  s     rA   r  zTypingVariable.is_python_equal  r  r@   r   )r<   r=   r>   r   rK   r   r   r0   r   r   r}   ra   rW   r   r
  r  r  r   r  r  r   r   s   @rA   r1  r1  F  s    c S T 
#
 
 ?#	

 S/)*
 

275 7S 7_ 7 C E8GDM / /
V 
 
r@   r1  )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)utilsrD  r   rm   callablerj   )rD  np_fn_to_tnp_fnnp_modtnp_modrI  tnp_fnnp_fns          rA   get_np_to_tnp_maprL    sz     )O+113 4&//557 	4OGV $FGT::5:-3OE*	44 r@   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.
    )rL  r   )mkvs      rA   get_tnp_to_np_maprQ    s.     	AWWY'TQAqD'''s   /c            
       D    e Zd ZdZej
                  fZdededdf 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.
    rf   rF   rG   Nc                 2    t        |   di | || _        y rI   r3  r%  s      rA   rK   zNumpyVariable.__init__  r4  r@   rL  c                     |j                   j                  d      }t        |      dk\  r
|d d ddgk(  sJ || j                  v S Nr6  r   r   _numpy)r=   splitrl   constant_fold_functionsr   rL  mods      rA   can_constant_fold_throughz'NumpyVariable.can_constant_fold_through  sJ    mm!!#&3x1}RaWh,?!???S0000r@   c                     |j                   j                  d      }t        |      dk\  r
|d d ddgk(  sJ t        j	                  |      S rV  )r=   rX  rl   np_constant_collections_maprn   rZ  s      rA    get_constant_collection_for_funcz.NumpyVariable.get_constant_collection_for_func  sJ    mm!!#&3x1}RaWh,?!???*..r22r@   rX   r9   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-        ||      rt.        j0                  j3                   | j                         |D cg c]  }|j                          c}i |j                         D 	
ci c]  \  }	}
|	|
j                          c}
}	      S  |j4                  j6                  d ||      gt9        ||       }|j3                  ||      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.r\   r   )numpy_to_tensor_wrapperr-   )NumpyNdarrayVariablez8attempted to trace numpy function unsupported by PyTorchz  (corresponding torch function: r0  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 r6  r   )r   trace_numpyr   rf   rE  rb  tensorrc  rL  rn   r   r  r_  ra   r   r.   
USER_ERRORr=   use_numpy_random_stream
patch_argsr<   r\  r$   r   r2   r|   r   r  r*   )rL   rX   r~   rF   rb  rc  r   collection_variable_typr  rO  rP  r  s               rA   r   zNumpyVariable.call_function  s]    !!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; !1188-D++-:>?Q!..0?AGPA1a2244P  +BII**'- #40E
 (..r599e @P 7 
r.tzzl(4&
SYRZZz{  {A  AB  C"1$** >@ !@*55	
F @Ps=   J "J9J J*	J K1.K6J AK.-K.rY   c           	      h    t        dd| j                   d| d| dg t        j                         y )Nz/attempted to trace numpy.* function as a methodra  r  r  z6Tracing numpy.* functions as methods is not supported.r\   )r   rf   r   r   )rL   rX   rY   r~   rF   s        rA   r   zNumpyVariable.call_method1  s?     	E&tzzl(4&
6(SP",,		
r@   c                     | j                   S rP   r)  r   s    rA   ra   z NumpyVariable.as_python_constantA  r*  r@   c                     t         j                  r`t        | j                  t        j
                        r| j                  S t        | j                  t              r| j                  j                  S t        | %         S rP   )
r   rd  r   rf   enumEnumMetare   r<   rJ   r  r  s    rA   r  zNumpyVariable.as_proxyD  sV    $**dmm4zz!$**d+ zz***w!!r@   Tc                      yr  r?   r   s    rA   r
  z NumpyVariable.is_python_hashableR  r  r@   c                 4    t        | j                               S rP   r  r   s    rA   r  zNumpyVariable.get_python_hashU  r  r@   r  c                 h    t        |t              xr! | j                         |j                         k(  S rP   r  r  s     rA   r  zNumpyVariable.is_python_equalX  r  r@   ) r<   r=   r>   r6  tnp
issubdtyperY  r   rK   r   r   r   r  r\  r_  r   r0   r   r   r   r   r   r   ra   r  r   r
  r  r  r   r  r   r   s   @rA   rS  rS    sM     #~~/c S T  15+=+= 1$ 1 1
 3%2D2D 3 3 3
Y:#Y: 'Y: S/)*	Y:
 
Y:v
#
 
 ?#	

 S/)*
 

 $7 "# "GDM / /
V 
 
r@   rS  c                   <     e Zd Zdeddf fdZdefdZddZ xZS )NullVariablerF   rG   Nc                 $    t        |   di | y rI   rW  rX  s     rA   rK   zNullVariable.__init__a  rY  r@   c                      y)Nru  r?   r   s    rA   r2  zNullVariable.__repr__d  s    r@   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.11r[   zdAttempted to generate PUSH_NULL instruction in Python < 3.11; where this instruction does not exist.r\   	PUSH_NULL)r   r  r   r   r   r=  r   r>  s     rA   rW   zNullVariable.reconstructg  sL    g%J9&11 	0=>r@   r   )	r<   r=   r>   r   rK   r   r2  rW   r   r   s   @rA   ru  ru  `  s'    # # ## ?r@   ru  c                       e Zd ZdZy)r   z"Marker used to implement delattr()Nr5  r?   r@   rA   r   r   u  s    ,r@   r   c            
            e Zd ZdZdhej
                  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ddZ xZS )StringFormatVariablez[
    Represents a call to str.format(), we delay calling format until after the graph.
    format_stringsym_args
sym_kwargsrG   c                    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rP   )r  r  s     rA   r  z.StringFormatVariable.create.<locals>.<genexpr>  s!      
   "
s   )all	itertoolschainvaluesr   r2   r|   formatra   r   r   r   )r   r~  r  r  rP  rO  s         rA   r|   zStringFormatVariable.create  s      
__Xz/@/@/BC
 
 --44$$$6>?a**,?=G=M=M=OPTQq!..00P  =$x.$z2BCC	 @Ps   CCrF   Nc                 r    t        |   di | t        |t              sJ || _        || _        || _        y rI   )rJ   rK   r   r   r~  r  r  )rL   r~  r  r  rF   rM   s        rA   rK   zStringFormatVariable.__init__  s;     	"6"----* $r@   c                     | j                   j                   d| j                  d| j                  d| j                  dS r  )rM   r<   r~  r  r  r   s    rA   r2  zStringFormatVariable.__repr__  sB    ..))*!D,>,>+ADMMCTTVW[WfWfViijkkr@   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  )rU   r>  r~  create_load_attrr  s   rA   rS   z2StringFormatVariable.reconstruct.<locals>.<lambda>  s9    G))--d.@.@A,,X6 r@   T)call_function_exF)rT   r   r   r   r  r  r   r2   r|   ConstDictVariablerU   r   )rL   rR   rO  rP  rF   s   ``   rA   rW   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>   r6  r0   r   r   r   r   r   r|   r   rK   r2  rW   r   r   s   @rA   r}  r}  y  s     &G(F(FGNDD ?+D o-.	D
 
D D$%% ?+% o-.	%
 % 
%l# lDr@   r}  c                   >     e Zd Zdededdf fdZdee   fdZ xZS )ObjectVariablerf   rF   rG   Nc                 2    t        |   di | || _        y rI   r3  r%  s      rA   rK   zObjectVariable.__init__  r4  r@   c                     t         S rP   )r   r   s    rA   rb   zObjectVariable.python_type  s    r@   )	r<   r=   r>   r   r   rK   re   rb   r   r   s   @rA   r  r    s-    f   T&\ r@   r  c            	            e Zd ZdZdededdf 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d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.
    rf   rF   rG   Nc                 2    t        |   di | || _        y rI   r3  r%  s      rA   rK   zDebuggingVariable.__init__  r4  r@   r   c                     t        |       xrR t        | t        j                  t        j                  f      xr& | t
        j                  j                  j                  v S rP   )	rF  r   r   r   r   r   r  r   reorderable_logging_functionsr   s    rA   is_reorderable_logging_functionz1DebuggingVariable.is_reorderable_logging_function  sO    
 SM J3!3!3U5N5N OPJu}}++III	
r@   rX   r9   r~   c           	         |j                   ry | j                  | j                  ||      s1t        dd| j                   d| d| dd| j                   dg       |j                  j                  | t        |      f       y )	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.r\   )exportcan_reorder_logsrf   r   debug_localsr  r   r  s       rA   r   zDebuggingVariable.call_function  s     99$$TZZv>dtzzl(4&
6(K@ :$**Egh 	d4j12r@   c                 T    | j                   J | j                   j                  |      S rP   )rc   rW   r>  s     rA   rW   zDebuggingVariable.reconstruct  s'    {{&&&{{&&w//r@   rL  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  r2   r}  pytreetree_leavesr   )rL  r~   rF   allowed_input_types	flat_argsr  s         rA   r  z"DebuggingVariable.can_reorder_logs  sY     $$&& 
 &&f~6	 	Cc#67	 r@   r   )r<   r=   r>   r6  r   rK   r   r   r   r   r   r  r   r0   r   r   r   rW   r  r  r   r   s   @rA   r  r    s    
c S T  

	5%%(A(AA	B
 
3#3 '3 S/)*	3
 
3.0 S  tCH~ RV  r@   r  c                   X     e Zd ZdZ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
 xZS )IgnoredFunctionVariablezL
    Represents a call to an arbitrary function that should be ignored.
    rf   rF   rG   Nc                 2    t        |   di | || _        y rI   r3  r%  s      rA   rK   z IgnoredFunctionVariable.__init__	  r4  r@   rX   r9   r~   c                 "    t         j                  S rP   )r   r1   r  s       rA   r   z%IgnoredFunctionVariable.call_function  s     ///r@   r<   r=   r>   r6  r   rK   r   r0   r   r   r   r   r   s   @rA   r  r    s\    c S T 0#0 '0 S/)*	0
 
0r@   r  c            
       p     e Zd ZdZdej
                  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 )LoggingLoggerVariablez:
    Represents a call to any logging.Logger methods.
    rf   rF   rG   Nc                 2    t        |   di | || _        y rI   r3  r%  s      rA   rK   zLoggingLoggerVariable.__init__  r4  r@   rX   r9   rY   r~   c                 H   |j                   rt        j                  S t        | j                  |d       }t        |dd       }t
        j                  j                  j                  }||v s||v rt        j                  S t        dd| j                   d| d| d| ddg	       y )
Nr   z8logging.Logger method not supported for non-export caseszmethod: r6  r  r  z>logging.Logger methods are not supported for non-export cases.zJAdd the logging method to `torch._dynamo.config.ignore_logging_functions`.r\   )
r  r   r1   rj   rf   r   r  r   ignore_logging_functionsr   )rL   rX   rY   r~   rF   methodr  
ignore_sets           rA   r   z!LoggingLoggerVariable.call_method  s     99333T406:t4 ]]))BB
Z8z#9333Ntzzl!D6$z&RX\		
r@   )r<   r=   r>   r6  loggingLoggerr   rK   r   r   r0   r   r   r   r   s   @rA   r  r    sj    gnn   
#
 
 ?#	

 S/)*
 

r@   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?   rf   rF   rG   Nc                 2    t        |   di | || _        y rI   r3  r%  s      rA   rK   zConstantLikeVariable.__init__L  r4  r@   c                     t        | j                        }|j                  dk(  r|j                  S |j                   d|j                   S )z4Dynamically compute the prefix from the value's typer   r6  )re   rf   r=   r>   )rL   ts     rA   _error_prefixz"ConstantLikeVariable._error_prefixP  sD      <<:%>>!,,q 011r@   c                     | j                   S rP   r)  r   s    rA   ra   z'ConstantLikeVariable.as_python_constant[  r*  r@   rX   r9   rY   r~   c                    g i }}	 |D cg c]  }|j                          }}|j                         D 	ci c]  \  }}	||	j                          }}}	 t        | j                  |      |i |}
t        j                  j                  |
      rt        j                  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 argsr6  z(*z, **r0  zAttempted to call z with non-constant args.zFEnsure that the args to the method call are constant (int, str, etc.).r\   z6constant-like method call with unsupported return typez) returned z, got unsupported return value )ra   r   r   r   r  rj   rf   r   r2   rx   r|   r   reMatchr  r   r  )rL   rX   rY   r~   rF   cargsckwargsr  rO  rP  r   s              rA   r   z ConstantLikeVariable.call_method^  s    Rw	59:Q))+:E:=C\\^LTQq!..00LGL +T*E=W=%%008--44V<<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                  j                  |      S t        | |      S rP   )rj   rf   r   np_floatingfloatnp_dtypeNumpyDTypeVariablere   rd   
np_genericrS  r   r2   rx   r|   ry   r-  s       rA   r}   z ConstantLikeVariable.var_getattr  s    T*fd../6]Ffdmm,%f--fd#
64??(K ((%%008--44V<<tT**r@   )r<   r=   r>   r6  numpyr  r  r  r  r  r  ImportErrorre   r   rK   propertyr   r  ra   r   r0   r   r   r}   r   r   s   @rA   r  r  =  s    B
0	
 	
c S T  2s 2 2C %
#%
 %
 ?#	%

 S/)*%
 
%
N+5 +S +_ +  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__rF   rG   Nc                     |j                  dt        j                         |d   t        j                  u sJ t        |   di | y )Nrf   r?   )
setdefaultr   __version__rJ   rK   rX  s     rA   rK   zTorchVersionVariable.__init__  sA    '5#4#45g%"3"3333"6"r@   )r<   r=   r>   r  r   rK   r   r   s   @rA   r  r    s     'M# # # #r@   r  c                       e Zd ZdefdZy)r  rG   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).
        )rf   re   r<   r   s    rA   r  zNumpyDTypeVariable.as_proxy  s     zz'''r@   N)r<   r=   r>   r   r  r?   r@   rA   r  r    s    (# (r@   r  c                   T     e Zd ZdZdeddf fdZdddee   dee	ef   dd	fd
Z
 xZS )RandomClassVariablezrandom.RandomrF   rG   Nc                 $    t        |   di | y rI   rW  rX  s     rA   rK   zRandomClassVariable.__init__  rY  r@   rX   r9   r~   RandomVariablec                    t        |      dkD  s|r%t        dd| d| dg t        j                         t        |      dk(  rt        j
                  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.r\   r   )seedr   )	rl   r   r   rf  r   r1   r  baseValueMutationNew)rL   rX   r~   rF   r  s        rA   r   z!RandomClassVariable.call_function  s~     t9q=FA j9[&11	 47t9>y//tAwY^^%D%D%F
 	
r@   r  r   s   @rA   r  r    sS    # # #
#
 '
 S/)*	

 

r@   r  c            
           e Zd Zd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  rF   rG   c                 0   t        |   di | |V| j                  |      sJ t        j                         | _        | j                  j                  |j                                y ||j                         nd }t        j                  |      | _        y rI   )rJ   rK   is_supported_random_objr  r   setstategetstatera   )rL   r  r  rF   rM   s       rA   rK   zRandomVariable.__init__  sy     	"6"//555 --/DKKK  1040@4**,dD ---DKr@   c                 "    t         j                  S rP   )r  r   r   s    rA   rb   zRandomVariable.python_type  s    }}r@   c                     | j                   S rP   )r  r   s    rA   ra   z!RandomVariable.as_python_constant  s    {{r@   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)re   r  r   r  r  r  _supported_fn_namesr   rj   r|  	isbuiltinr  )r   rY   meths      rA   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 	! r@   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rP   )re   r  r  s     rA   r  z-RandomVariable.check_state.<locals>.<genexpr>		  s     4a47c>4s   r   )re   r   r  r  r  )r  s    rA   check_statezRandomVariable.check_state	  s}    E{e###E!H~$$$E!H~&&&4584444Qx4a>U#:::#:r@   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   r2   r|   )r  r  s     rA   
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 rP   )ra   r  r  )r  	state_objs     rA   unwrap_statezRandomVariable.unwrap_state	  s&     ,,.	""9-r@   rX   r9   rY   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                  S dk(  r)| j                  | j                  j                               S dk(  rb|j                   j                  j                  |        | j                  j                  | j                  |d                t        j                  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"        | I  |||      S c c}w c c}}w c c}w c c}
}	w )	Nr  r  r  r   r~   rF   rG   c                  r    t        j                         }|j                          t        |      | i |S rP   )r  r   r  rj   )r~   rF   rrY   r  s      rA   call_random_methz4RandomVariable.call_method.<locals>.call_random_meth9	  s3    MMO

5!'wq$'888r@   )r   r   mutationr  r  ra   r   r   r1   r  r  r  r  r  r   rj   r5   rJ   r   )rL   rX   rY   r~   rF   r  r  r   r  rO  rP  r  rM   s     `        @rA   r   zRandomVariable.call_method!	  s    6>II""++D1DKK267Q!&&(7AGPXS#3..00P 333Z??4;;#7#7#9::ZII""++D1KK  !2!247!;<333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   H)5H.;H4(H9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   )rU   create_load_python_moduler  r  rR   s   rA   rS   z,RandomVariable.reconstruct.<locals>.<lambda>J	  s3    G))55f=,,X6 r@   r   Fr  r-   T)rT   r   r  r   r  r  r  pop_topr>  s    `rA   rW   zRandomVariable.reconstructH	  sz    	
 	a' 	*% 4 4 678a&r@   r  r   )r<   r=   r>   r6  r0   r   r  r  r   r   rK   re   rb   ra   r   r  r  r   r  r  r  r   r  r  r   r   r   r   rW   r   r   s   @rA   r  r    s    			'	'N
 &*'+.mmd". $. 	.
 
.T&--0 FMM  V   $ ;5eCHout|!CD ; ; ; 

%U38_edl BC 

 

 

 	sE#s(OUT\1	2 %;#%; %; ?#	%;
 S/)*%; 
%;Nr@   r  c                        e Z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 )WeakRefVariablerX   r9   weakref_valuerc   NoptionsrG   c                     |J |j                   }|xr t        |d      }t        j                  | ||      } |       }|xr t	        |      }t        j                  | ||      }||d<   t        ||fi |S )N__callback__rc   )r  r   r0   r   r#   r  )	rX   r  rc   r  callbackcallback_sourcecallback_vtreferentreferent_vts	            rA   r   zWeakRefVariable.build\	  s     !!! -- GZ%G%++B/J ?5-f5%++B&A"{KC7CCr@   r  r  c                 @    t        |   di | || _        || _        y rI   )rJ   rK   r  r  )rL   r  r  r  rM   s       rA   rK   zWeakRefVariable.__init__n	  s%     	#7#&&r@   r~   rF   c                     | j                   S rP   )r  r  s       rA   r   zWeakRefVariable.call_functionu	  s     r@   c                     j                  fd        | j                          | j                         j                  t	        dd             y )Nc                  (     j                  dd      S )Nweakrefref)r  r  s   rA   rS   z-WeakRefVariable.reconstruct.<locals>.<lambda>~	  s    g&>&>y%&P r@   r   F)rT   r  r  rU   r   r>  s    `rA   rW   zWeakRefVariable.reconstruct}	  sD    PQ  !  !21e<=r@   c                 6    | j                   j                         S rP   )r  r
  r   s    rA   r
  z"WeakRefVariable.is_python_hashable	  s    2244r@   c                 6    | j                   j                         S rP   )r  r  r   s    rA   r  zWeakRefVariable.get_python_hash	  s    //11r@   r  c                 n    t        |t              sy| j                  j                  |j                        S )NF)r   r  r  r  r  s     rA   r  zWeakRefVariable.is_python_equal	  s,    %1//0A0ABBr@   r   )r<   r=   r>   r   r  ReferenceTyper   r   r   r0   rK   r   r   r   r   rW   r  r
  r  r  r   r  r   r   s   @rA   r  r  [	  s    D#D,,S1D D 	D
 
D D '*'9H'UX'	' #  '  S/)*	 
 
 >5D 52 2CV C Cr@   r  )r6  r  rm  r  r|  r  r  r  r  r   r   r   r  collections.abcr   r   r   r   typingr   r   r	   r
   r   r   torch._Cr   torch._numpyrW  rr  torch.utils._pytreerE  _pytreer  torch._dynamo.variables.baser   torch._dynamo.variables.listsr   torch._guardsr   r[   r   r   r   r   bytecode_transformationr   r   r   create_parameter_opr   r   r   r   guardsr   r   mutation_guardr   rc   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r  r.   r/   r0   r  r1   r2   	functionsr3   r4   user_definedr5   r6   r7   torch._dynamo.codegenr8   torch._dynamo.symbolic_convertr9   r;   rC   r   r   r  r4  r8  r<  rO  rU  r_  rb  	dataclassr  r{  r  r   ry   r  r  r!  r1  	lru_cacher   r   rL  rQ  rS  ru  r   r}  r  r  r  r  r  r  r  finfoiinfor  r^  r  r  r  r?   r@   rA   <module>r&     sN  "        	 
    .  % J J   $ $ 5 7   @ @ 
 G 9 0 5 
 
 
 
 ? G X X /D	 	X
O X
v
-? -(`; `;FV Vro 
o 
2?0 ?0D;? ;(# #
%c %hsCx6H %` `F M M MD-&? D-N26 2j(_ (NOo NObx
O x
v &)=? )=XW
_ W
t Q4 9 95;M;M MN   ( Q(4 2 2E4M4M MN (  (]
O ]
B?? ?*-o -9D? 9Dx_ B BJ0o 0$$
O $
NS+? S+l#/ #(- ( II#II#II! 
/ 
6P_ Pf2Co 2Cr@   