
    ^jĊ                       d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZmZ ddlmZ ddlmZ ddlmZmZmZmZ 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.m/Z/ d
dl0m1Z1 d
dl2m3Z3m4Z4m5Z5 d
dl6m7Z7m8Z8 d
dl9m:Z: d
dl;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA d
dlmBZBmCZCmDZDmEZEmFZFmGZG ddlHmIZImJZJmKZK ddlLmMZM ddlNmOZOmPZP ddlQmRZRmSZSmTZT erddlUmVZV ddlWmXZX  G d deK      ZY G d deK      ZZ G d deK      Z[ G d  d!eK      Z\ G d" d#eK      Z] G d$ d%e]      Z^ G d& d'eK      Z_ G d( d)eK      Z` G d* d+eK      Zad,ed-ed.ef   fd/Zb G d0 d1eK      Zcej                   G d2 d3             Ze G d4 d5eT      Zf G d6 d7eT      Zg G d8 d9eK      Zh G d: d;eK      Zi G d< d=eK      Zj G d> d?eK      Zk ej                  d@      d-emej$                  ej                  f   fdA       Zo ej                  d@      d-emej                  ej$                  f   fdB       Zp G dC dDeK      Zq G dE dFeK      Zr G dG dHeK      Zs G dI dJeK      Zt G dK dLeK      Zu G dM dNeK      Zv G dO dPeK      Zw G dQ dReK      Zx G dS dTeK      Zy G dU dVey      Zzej                  eyej                  eyej                  eziZ~ G dW dXeK      Z G dY dZeK      Z G d[ d\eK      Zy)]a  
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TYPE_CHECKING	TypeGuardUnion)MutationType)TupleVariable)Source   )configgraph_break_hintstrace_rules	variables)create_call_functioncreate_call_function_excreate_instruction)$do_not_convert_to_tracable_parameter)raise_observed_exceptionraise_type_errorunimplemented)GuardBuilderinstall_guard)unpatched_nn_module_init)
AttrSourceGenericAttrSourceGetItemSourceTypeMROSource
TypeSourceWeakRefCallSource)check_unspec_or_constant_argscmp_name_to_op_mappingidentityistypeproxy_args_kwargsraise_args_mismatch   )#AsPythonConstantNotImplementedErrorNO_SUCH_SUBOBJVariableTracker)ConstantVariable)NestedUserFunctionVariableUserFunctionVariable)call_random_fnis_standard_setattrUserDefinedObjectVariable)	PyCodegen)InstructionTranslatorBasec            
            e Zd ZeZh ej                  Z	 ddededz  deddf fdZde	fdZ
d fdZd	d
dedeeedz  f   f fdZd	d
dedefdZd	d
dedee   deeef   def
dZ xZS )SuperVariableNtypevarobjvarkwargsreturnc                 @    t        |   di | || _        || _        y N )super__init__r7   r8   )selfr7   r8   r9   	__class__s       g/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/variables/misc.pyr?   zSuperVariable.__init__X   s'     	"6" 
     c                 "    t         j                  S N)builtinsr>   r@   s    rB   python_typezSuperVariable.python_typei   s    ~~rC   c                     j                  fd        | j                         | j                  . | j                         j                  t	        dd             y j                  t	        dd             y )Nc                  B     t        j                  t                    S rE   )r   BuiltinVariabler>   )rA   codegens   rB   <lambda>z+SuperVariable.reconstruct.<locals>.<lambda>m   s    gi.G.G.N&O rC   r   Fr)   )add_push_nullr7   r8   extend_outputr   r@   rL   rA   s    `rB   reconstructzSuperVariable.reconstructl   s`    OP;;"DKK !!"6q%"@A!!"6q%"@ArC   txr4   namec                    | j                   s.t        ddd| d| j                   d| j                    ddg       | j                   t        d	      | 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hintsz!super() requires objvar to be setr)   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()`.)r8   r   r7   AssertionErroras_python_constantrH   sourcer!   
issubclasstypevalue__mro__index
ValueErrorgetattrr>   rangelen__dict__getr+   r   r   r    )r@   rR   rS   search_typetype_to_usetype_to_use_sourcer]   
search_mrostart_indexrb   resolved_getattrrA   s              rB   _resolved_getattr_and_sourcez*SuperVariable._resolved_getattr_and_sourceu   s    {{5@ G,,0LL>} UDD D	 ;; !DEE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   	F< <!G G c                 .   | j                  ||      \  }}t        j                  j                  |      st	        | |t        |            S |r(t        |j                  t        j                               t        j                  j                  ||      S )Npy_typer]   )ro   r   r-   
is_literalGetAttrVariabler_   r   
make_guardr   CONSTANT_MATCHcreate)r@   rR   rS   r`   r]   s        rB   var_getattrzSuperVariable.var_getattr   sy     99"dCv))44U;"4tE{CC&++L,G,GHI))00v0FFrC   argsc                    | j                  ||      \  }}| j                  t        d      |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                  j0                  rt3        |d      r|j4                  dk(  rt        j6                  j9                  |      r|j:                  }
t3        |
d      r&|
j<                  dk(  rt#        j$                  ||
      }n0|t        d      |j>                  }t        j6                  |
|      }|jA                  |d||      S t        |tB              rZt        |jD                  tF        jH                        r6t#        j$                  ||jD                  |d      }	|	j)                  |||      S t        |tJ              rt        |jD                  tF        jH                        rt        | j                  t        j6                        r| j                  }nbd }| j                  j0                  rtM        | j                  j0                        }t#        j$                  || j                  jN                  |      }|t        d      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t        dti        |             |j`                  j                  jk                  | j                  |t        jl                                t        jn                  jq                  d       S t        | j                  t        j                        rl| j                  jr                  V| j                  jt                  @|| j                  jt                  v r(| j                  jr                  jA                  ||||      S |t        jv                  u r|d   jx                  }t        | j                  t              r| j                  j{                  ||      S d }	 t        jw                  | j                  jx                  |      }d }| j                  j0                   t        | j                  j0                  |      }t#        j$                  |||      S |t        j                  j                  u r|d   }i }|d   j                  }|d   j                  j                         D ]$  \  }}|j                  j]                         }|||<   & |j                  j                  }d|j                  _F        	 |j)                  |||      ||j                  _F        S t        |tF        j                        rP|t        j                         v r:t#        j$                  |||d       }|j)                  || j                  g|z   |      S t        |tF        j                        r,t#        j$                  |||d      }	|	j)                  |||      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 5w xY w# ||j                  _F        w xY w)&Nz2super() requires objvar to be set for method callsr   )AttributeMutationNewrs   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.rV   __name____new__
__module__rF   z.source must not be None for user-defined classTr]   realizez2source must not be None for classmethod resolution__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.)rW   rX   rY   rZ   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.zattr must be a str, got    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.)Kro   r8   r[   objectr?   LambdaVariabler%   torchnnModuleside_effectsr|   
isinstancer   r2   mutation_typer   r,   buildr   call_functionr   r   	DIFFICULTr]   hasattrr   UserDefinedClassVariableis_supported_new_method__self__r   membercall_methodstaticmethodr   typesFunctionTypeclassmethodr!   
value_typer   
MethodTypeUserMethodVariabler1   method_setattr_standard__delattr__r\   NotImplementedErroroutputis_attribute_mutation
DYNAMO_BUGstrr_   
store_attrDeletedVariabler-   rx   _base_vt_base_methods__getattribute__r`   generic_getattrAttributeErrorr   r   _C_disabled_torch_function_implitemsvtsymbolic_torch_function_statetorch_function_subclass_enabledMethodDescriptorTyper   get_tensor_methodr   )r@   rR   rS   rz   r9   inner_fnr]   r8   r|   fn_vtuser_clsuser_cls_vtuser_cls_sourcecls_variable
cls_sourceattrexc	attr_name
attr_valueattr_sourcefunc	tf_kwargstf_argshash_key_vtvalue_vtkey_strtx_oldfn_vars                               rB   r   zSuperVariable.call_method   s(     <<RF&;; !UVV v&!(++111[[F; 69#F#FGv335IJ9; Q+114VE !..rDKK=43GP	Q Q A*4&$qaxH!  "1!1 9-778 KK*-!!Y.22JJ8T((Hx.83F3F*3T/>/D/DR/R>(H  #)--'@@_ **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** 
 ~$H  $))!!!&*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($'?T
|%LMMII""--T9#<#<#> --44T::t{{I$G$GH$$0))5DKK555;;''33BdFKK000QI $++'@A{{222yAAJ=#44KK%%
 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%";";<#))"hvtTE&&r488'"4&$0v24& 9,, `
	
mQ Qt ' 
U*4&$8!L ` !	 	
f " =(<=6  00PsB   =bb ?*c c" b	b?b::b?cc"c5rE   rL   r3   r:   N)r   r   __qualname__r>   _cpython_typer,   _nonvar_fieldsr   r?   r_   rH   rQ   r   tupler   ro   ry   listdictr   __classcell__rA   s   @rB   r6   r6   P   s    M		'	'N *.   $& 	
 
"T BC
-C
58C
	sJ%%	&C
JG-G58G	G$]
']
 ]
 ?#	]

 S/)*]
 
]
rC   r6   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_summaryr9   r:   Nc                 2    t        |   di | || _        y r<   )r>   r?   r   )r@   r   r9   rA   s      rB   r?   zFrameSummaryVariable.__init__  s    "6"*rC   c                 "    t         j                  S rE   )	tracebackFrameSummaryrG   s    rB   rH   z FrameSummaryVariable.python_type  s    %%%rC   rR   r4   rS   c                    |dk(  r*t        j                  || j                  j                        S |dk(  r*t        j                  || j                  j                        S |dk(  r*t        j                  || j                  j
                        S |dk(  r*t        j                  || j                  j                        S t        | !  ||      S )NlinenofilenamerS   line)	r,   r   r   r   r   rS   r   r>   ry   r@   rR   rS   rA   s      rB   ry   z FrameSummaryVariable.var_getattr  s     8"((T-?-?-F-FGGZ"((T-?-?-H-HIIV^"((T-?-?-D-DEEV^"((T-?-?-D-DEEw"2t,,rC   )r   r   r   r   r   r   r?   r_   rH   r   r,   ry   r   r   s   @rB   r   r     sR    +i&<&< + +PT +&T &---58-	- -rC   r   c            
       j    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ddedd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_nextr9   r:   Nc                 Z    t        |   di | || _        |t        d      || _        y )Nztb_next must not be Noner=   )r>   r?   r   r[   r   )r@   r   r   r9   rA   s       rB   r?   zTracebackVariable.__init__  s5     	"6"*? !;<<rC   c                 (     | t        |      |      S )N)r   )r   )clsr   r   s      rB   from_frame_summaryz$TracebackVariable.from_frame_summary  s     '6HHrC   objc                 F    t        | t              xs | j                         S rE   )r&   r   is_constant_noner   s    rB   is_valid_tracebackz$TracebackVariable.is_valid_traceback  s    c,-G1E1E1GGrC   c                     t        | j                  t              r| j                  gS | j                  g| j                  j	                         z   S rE   )r&   r   r-   r   
extract_tbrG   s    rB   r   zTracebackVariable.extract_tb  sA    $,, 01&&''""#dll&=&=&???rC   tbc                 l    | }t        |t              r"||u ry|j                  }t        |t              r"y)NTF)r&   r   r   )r@   r   curr_tbs      rB   has_reference_cyclez%TracebackVariable.has_reference_cycle  s7    8<W/0"}ooG W/0 rC   c                 "    t         j                  S rE   )r   TracebackTyperG   s    rB   rH   zTracebackVariable.python_type  s    """rC   rR   r4   name_varvalc                    |j                         }|dk(  r| j                  |      st        t        |       t	        |t
        t        f      st        dt        |             | j                  |      s!t        |t
              r!|j                  |       rt        t        |       || _        t        j                  j                  d       S )Nr   z?tb_next val must be TracebackVariable or ConstantVariable, got )r\   r   r   	TypeErrorr   r   r-   r[   r_   r   r&   rc   r   r   rx   )r@   rR   r   r   rS   s        rB   call_setattrzTracebackVariable.call_setattr  s     **,9**3/(B7c$57G#HI$UVZ[^V_U`a  '',s-.33J3J43P(R8DL))0066rC   rS   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.rV   )r   r   ry   r   r   SUPPORTABLEr>   r   s      rB   ry   zTracebackVariable.var_getattr  s     9<<[ %%11"h??_$%%%Z:& 56l6)556	 w"2t,,rC   otherr,   opc                 $    ddl m}  || |||      S Nr)   )object_richcompareobject_protocolr   r@   rR   r   r   r   s        rB   richcompare_implz"TracebackVariable.richcompare_impl       	8!$E266rC   rz   c                 X    |dk(  r | j                   |g| S t        | 	  ||||      S )N__setattr__)r   r>   r   )r@   rR   rS   rz   r9   rA   s        rB   r   zTracebackVariable.call_method!  s;     = $4$$R/$//w"2tT6::rC   )r   r   r   r   r
   r-   r   r?   r   r   r   r   r   r,   boolr   r   r   r   r_   r   r   rH   r   r   ry   r  r   r   r   r   s   @rB   r   r     s   + *,<<= 	
 
& I --I *,<<=I 
	I I H HD H H@D!7!7:N!NO @
o $ #T%"5"56 #7'7 "7 	7
 
7*---58-	-$7-76G7MP7	7	;'	; 	; ?#		;
 S/)*	; 
	; 	;rC   r   c                   0    e Zd ZeZ	 	 	 ddedee   dee	ef   dz  de
dz  dedz  ddf fdZd	eddfd
ZddZdefdZddddde	ddfdZddde	dee   dee	ef   def
 fdZddde	def fdZde	fdZeZedede	fd       Zde	fdZdddefdZ xZS )ExceptionVariableNexc_typerz   init_kwargsr]   r   r:   c                 n   t         |   ||       || _        || _        |r$t	        d|  d| dg t
        j                         t        j                  d       | _	        t        j                  d       | _
        t        j                  d      | _        t        j                  d       | _        d | _        y )N)r]   r   z,Keyword args passed to exception constructorz with kwargs zRDynamo does not know how to handle keyword args passed to an exception constructorrV   F)r>   r?   r
  rz   r   r   r   r-   rx   __context__	__cause____suppress_context____traceback__python_stack)r@   r
  rz   r  r]   r   rA   s         rB   r?   zExceptionVariable.__init__2  s     	mD 	F&k];p6)556	 -=,C,CD,I +;*A*A$*G5E5L5LU5S!.>.E.Ed.K <@rC   rX   c                     || _         y rE   )r  )r@   rX   s     rB   set_contextzExceptionVariable.set_contextS  s
    "rC   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 )NrF   )load_import_fromr
  r   rL   r@   s   rB   rM   z/ExceptionVariable.reconstruct.<locals>.<lambda>X  s    G,,Z9O9OP rC   FrS   r:   c                 
   t        |       }t        |t              r|j                  dvrt	        d|       y j                           |       j                  j                  d             j                  |        y )N)TFNz-attr.value must be True, False, or None, got r   )	rd   r&   r-   r`   r[   dup_toprO   rot_nr   )rS   r   rL   r@   s     rB   codegen_attrz3ExceptionVariable.reconstruct.<locals>.codegen_attr]  s{    4&Dd,-::%88(GvN  9
 !%%gmmA&67""4(rC   r  r  r  )rN   foreachrz   r   rf   r   )r@   rL   r  s   `` rB   rQ   zExceptionVariable.reconstructV  sl    P	
 			"c$))ne4	)s 	)t 	) 	]#[!+,rC   c                     | j                   S rE   )r
  rG   s    rB   rH   zExceptionVariable.python_typen  s    }}rC   rR   r4   r   r,   r   c                 $    ddl m}  || |||      S r   r   r  s        rB   r  z"ExceptionVariable.richcompare_implq  r  rC   rS   r9   c                    |dk(  r|d   j                         }|d   }|dk(  rj|j                         sGt        |t        j                  t        j
                  t        j                  f      st        | d      | j                  |       n|dk(  r|j                         sHt        |t        j                  t        j                  t        j
                  t        j                  f      r,|| _
        t        j                  j                  d      | _        nt        |d       n|d	k(  r'|j                  dd
      r|| _        ndt        |d       nW|dk(  r)t         j#                  |      st        |d       || _        n)t'        dd|  d| d| dg t(        j*                         t        j                  j                  d       S |dk(  r.|\  }t         j#                  |      st        |d       || _        | S t,        | ]  ||||      S )Nr  r   r)   r  z! is not a valid exception contextr  Tz9exception cause must be None or derive from BaseExceptionr  Fr  z0__traceback__ must be a traceback object or Nonez4Unsupported attribute assignment on Exception objectzcall_setattr r~   z/Dynamo does not support setting the attribute 'z{' on tracked exception objects. Only `__context__`, `__cause__`, `__suppress_context__`, and `__traceback__` are supported.rV   with_traceback)r\   r   r   r   r	  !UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariabler[   r  rK   r  r-   rx   r  r   is_constant_matchr   r   r  r   r   r   r>   r   )r@   rR   rS   rz   r9   r   r   rA   s          rB   r   zExceptionVariable.call_methodx  s    = 7--/Dq'C}$ ((*!%77%GG%HH )C50Q)RSS  %$'')Z!11!33!CC!DD	. &)DN090J0J0Q0QRV0WD-$W //((u503D-$W ((;;C@$N &)"R+D64&9!v ^!^ ;-99: --44T::%%DR$77; %WX!#DK7&r4v>>rC   c                    |dk(  r t        j                  || j                        S |dk(  r| j                  S |dk(  r| j                  S |dk(  r| j
                  S |dk(  r| j                  S |dk(  rMt        j                  |t        | j                        | j                  xr t        | j                  d            S t        | 1  ||      S )NrA   r  r  r  r  rz   rs   )r,   r   r
  r  r  r  r  r   rz   r]   r   r>   ry   r   s      rB   ry   zExceptionVariable.var_getattr  s     ;"((T]];;]"###[ >>!++,,,_$%%%V^"((dii {{Fz$++v'F 
 w"2t,,rC   c                 N    | j                   j                   d| j                   dS )N())rA   r   r
  rG   s    rB   __str__zExceptionVariable.__str__  s$    ..))*!DMM?!<<rC   argc                 t    	 t        | j                               S # t        $ r | j                         cY S w xY wrE   )reprr\   	Exception
debug_reprr)  s    rB   _debug_format_argz#ExceptionVariable._debug_format_arg  s7    	$..011 	$>>##	$s    77c                 x     dj                   fd j                  D              } j                          d| dS )N, c              3   @   K   | ]  }j                  |        y wrE   )r/  ).0r)  r@   s     rB   	<genexpr>z/ExceptionVariable.debug_repr.<locals>.<genexpr>  s     J//4J   r&  r'  )joinrz   python_type_name)r@   rz   s   ` rB   r-  zExceptionVariable.debug_repr  s8    yyJ		JJ'')*!D633rC   c                 J    t        j                  || j                               S rE   )r,   r   r-  r@   rR   s     rB   	repr_implzExceptionVariable.repr_impl  s    $$R):;;rC   )NNNr   )r   r   r   BaseExceptionr   r   r   r,   r   r   r   r   r?   r  rQ   r_   rH   r  r   ry   r(  __repr__r   r/  r-  r:  r   r   s   @rB   r	  r	  -  s{   !M :> $-1@@ ?#@ #./$6	@
 @ $d*@ 
@B#? #t #-0T 7-76G7MP7	7G?'G? G? ?#	G?
 S/)*G? 
G?R---58-	-*= = H$ $3 $ $4C 4<7 <O <rC   r	  c                       e Zd ZdZy)UnknownVariablez
    It could be anything!
    Nr   r   r   __doc__r=   rC   rB   r>  r>    s    rC   r>  c                   `     e Zd ZdZddedz  deddf fdZddd	ee   de	eef   defd
Z
 xZS )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nmsgr9   r:   c                 2    t        |   di | || _        y r<   )r>   r?   rC  )r@   rC  r9   rA   s      rB   r?   z DelayGraphBreakVariable.__init__  s    "6"rC   rR   r4   rz   c                     | j                   dn| j                   j                  }t        dd| j                    d| d| j                   g        y )NrU   z#Unsupported function call (delayed)zsource: z@Dynamo determined that a graph break should occur when calling `z`. Reason: rV   )r]   rS   r   rC  )r@   rR   rz   r9   rS   s        rB   r   z%DelayGraphBreakVariable.call_function  sS     [[(rdkk.>.>9t{{m,!F+dhhZ9	
rC   rE   )r   r   r   r@  r   r   r?   r   r,   r   r   r   r   s   @rB   rB  rB    s`    C$J   
'
 ?#
 S/)*	

 

rC   rB  c                   T    e Zd ZdZddZdddedefdZddd	ee   d
e	eef   defdZ
y)ComptimeVariableza
    This variable is special, it lets you execute arbitrary code at
    Dynamo compile time
    r:   Nc                     t        d      )Nzcomptime is special form)r   r@   rL   s     rB   rQ   zComptimeVariable.reconstruct  s    !"<==rC   rR   r4   rS   c                     ddl m } | j                  t        d      t        j                  |t        ||      t        | j                  |            S )Nr   )comptimez"ComptimeVariable requires a sourcers   )rK  r]   r[   r,   r   rd   r   )r@   rR   rS   rK  s       rB   ry   zComptimeVariable.var_getattr  sI     	(;; !EFF$$$'
4;;0M
 	
rC   rz   r9   c           
         ddl m} |st        |      dkD  r&t        |ddt        |       dt        |       d       |d   }t	        |t
              r |j                          ||             nt	        |t              r|j                         }|j                  rt        |d|j                          t        j                  ||j                  |j                  j!                         |j"                  rt%        |j"                  j&                        nd d	      } | ||             nt)        d
t+        |             t,        j.                  j1                  d       S )Nr   )ComptimeContextz
comptime()zat most 2 args and 0 kwargsz
 args and  kwargsr   zOcomptime function must not have free variables, but these variables were free: r=   z"unsupported argument to comptime: )rK  rM  rf   r(   r   r/   get_functionr.   get_codeclosurer   co_freevarsr   r   	f_globalsfn_namer\   defaultsr   r   RuntimeErrorr_   r   r-   rx   )r@   rR   rz   r9   rM  fncoder   s           rB   r   zComptimeVariable.call_function  s-    	/ SY]-t9+ZF}G<	 !Wb./BOOob1267;;=Dzz efjfvfvewx %%

--/,.KKbkk''(T D $%!CDH:NOO))0066rC   r   )r   r   r   r@  rQ   r   r,   ry   r   r   r   r=   rC   rB   rG  rG  
  sc    
>

-

58

	

-7'-7 ?#-7 S/)*	-7
 
-7rC   rG  c                        e Zd ZU ej                  Zedz  ed<   dZe	dz  ed<   	 ddedz  de
ddf fdZdefdZ xZS )	CellVariableNpre_existing_contents
local_namer9   r:   c                 2    t        |   di | || _        y r<   )r>   r?   r[  )r@   r[  r9   rA   s      rB   r?   zCellVariable.__init___  s     	"6"%:"rC   c                 "    t         j                  S rE   )r   CellTyperG   s    rB   rH   zCellVariable.python_typee  s    ~~rC   rE   )r   r   r   r   r_  r   r,   __annotations__r\  r   r   r?   r_   rH   r   r   s   @rB   rZ  rZ  O  s]    NNM +T11 "Jd
! ?C;%4t%;;NQ;	;T rC   rZ  c                   (     e Zd Zdeddf fdZ xZS )NewGlobalVariabler9   r:   Nc                 $    t        |   di | y r<   r>   r?   r@   r9   rA   s     rB   r?   zNewGlobalVariable.__init__j      "6"rC   )r   r   r   r   r?   r   r   s   @rB   rb  rb  i  s    # # # #rC   rb  fn_clsr:   .c                 N     dt         dt         dt         f fd}t        |_        |S )Nrz   r9   r:   c                  (     j                   | i |S rE   )apply)rz   r9   rg  s     rB   trampoline_autograd_applyzDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_applyo  s    v||T,V,,rC   )r   !produce_trampoline_autograd_apply_origin)rg  rk  s   ` rB   rl  rl  n  s/    - - - - )J%$$rC   c            
       .    e Zd ZdZdhej
                  Zdededdf fdZdefdZ	de
e   deeef   de
e   dz  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 subclassrg  r9   r:   Nc                 2    t        |   di | || _        y r<   )r>   r?   rg  )r@   rg  r9   rA   s      rB   r?   z!AutogradFunctionVariable.__init__  s    "6"rC   c                     t         S rE   r_   rG   s    rB   rH   z$AutogradFunctionVariable.python_type      rC   rz   c                 F   ddl m} | j                  j                  }|j                  } || j                  j
                         }|rdnd}t        |j                  ||j                         |D ]4  }|vr yj                  |      t        |      k  s't        d| d       t        fd|D        t        |      dz
        }	t        |      }
t        t        |      |	dz         D ]1  }|   }||v r|
j                  ||          !t        d	| d
| d       |
S )a	  Resolve kwargs to positional args using forward().__code__.

        Uses co_varnames/co_argcount directly to match the C++
        resolve_kwargs_to_positional in python_function.cpp.
        Keyword-only args are not resolved; callers should graph break.
        r   _is_setup_context_definedr)   Nz,forward() got multiple values for argument ''c              3   @   K   | ]  }j                  |        y wrE   )rb   )r3  rS   param_namess     rB   r4  z;AutogradFunctionVariable._resolve_kwargs.<locals>.<genexpr>  s     8[t$8r5  )defaultz&forward() missing required argument: 'z' (position r'  )torch.autograd.functionrv  rg  forward__code__setup_contextr   co_varnamesco_argcountrb   rf   r   maxre   append)r@   rz   r9   rv  rW  rX  has_ctxparam_offsetrS   max_idxresultiry  s               @rB   _resolve_kwargsz(AutogradFunctionVariable._resolve_kwargs  s6    	F[[  {{/0I0IJJ#q4++L4;K;KLM 	XD;&  &T2"NtfTU VWW		X 88IM

 )-T
s4y'A+. 	Aq>Dv~fTl+<TF,qcQRS 	 rC   rR   r4   c           	      0	   |rw| j                  ||      }|_t        ddt        |      t        | j                  j                  j
                  j                        z
   dg t        j                         |}i }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|
sCt3        j5                  | j                  j6                        | j                  j8                        }|xr t3        |d      } ||	| j                  j:                  ||      j=                  ||      }| j                  r~|
r|t3        | j                  d      }t?        |jA                  tB        jD                               t3        | j                  d      }t?        |jA                  tB        jD                               |S | j                  rt3        | j                  d      }nd }| j                  j                  }tF        jI                  ||      }|g|}tK        |tL        jN                        rjtQ        jR                  |      }tU        |      dz
  tU        |jV                        k(  r|dd  }t        jX                  ||d      }|j=                  ||      S tK        |tL        jZ                        rPt]        j^                  |j`                  t]        jb                  | j                        |      j=                  ||      S t        dd|  d| d| dte        |      j8                   d d!g       y )"Nautograd_function_kwonly_argsz!forward() has keyword-only args: zMautograd.Function.apply does not support keyword-only arguments in forward().rV   Fr   r:   c                     | j                         r| j                  durdt        | t        j                        r| j                        rdy y y )NFT)	is_tensorrequires_gradr   r   NNModuleVariableis_training)r   r  rR   s    rB   visitz2AutogradFunctionVariable.call_apply.<locals>.visit  sJ    ||~##50$(M"i889>>"%$(M & :rC   r   )#autograd_function_forward_rewrittenru  z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.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)   )AutogradFunctionApplyVariablerj  )r   rs   r|  r~  Tr   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.)3r  r   setrg  r|  r}  r  r   r   r,   r  r   is_grad_enabledr]   "torch._functorch.autograd_functionr  r{  rv  r~  vjpautogradFunctionjvphigher_order_opsr  r   import_sourcer   r   backwardr   r   rv   r   CLOSURE_MATCHAutogradFunctionContextVariablerx   r   r   r   inspect	signaturerf   
parametersr   r   r   r   r   r   r_   )r@   rR   rz   r9   resolvedr  r]   r  rv  
forward_fnis_setup_ctx_definedvjp_fnjvp_fnr  apply_sourcer   fwd_srcsetup_ctx_srcrW  ctxsigr   r  s    `                    @rB   
call_applyz#AutogradFunctionVariable.call_apply  s%    ++D&9H;?FcRVR]R]ReReRnRnRzRzN{@{?|} o:-99:	 DF	)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
rC   c                    | j                   j                  }t        |d   j                        t        j
                  j                  j                  ur$t        dt        |d   j                               t        |t        j                        st        dt        |             | j                  t        d      t        | j                  d      }t        j                  |||d      }|j!                  |||      S )Nr   z$Expected FakeBackwardCFunction, got zExpected FunctionType, got z*AutogradFunctionVariable requires a sourcer  Tr   )rg  r  r_   r`   r   _dynamoexternal_utilsFakeBackwardCFunctionr[   r   r   r   r]   r   r,   r   r   )r@   rR   rz   r9   rW  	fn_sourcer   s          rB   call_backwardz&AutogradFunctionVariable.call_backward8  s     [[!!a==//EEF !6tDGMM7J6KL  "e001 #>tBxj!IJJ;; !MNNt{{J7	%%b"YM""2tV44rC   c                 ,    t        | j                        S rE   )ro  rg  r@   rR   rz   r9   s       rB   r   z&AutogradFunctionVariable.call_functionN  s     (44rC   rS   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"              r|j%                  | j                        }	t        j&                  |	      }
|
t)        d|	       |$|
j+                  |	|      j-                  |||      S  |
|	      j-                  |||      S t!        |t.              r3t1        j2                  |j4                  | |      j-                  |||      S t7        d	d
|  d| d| ddg       y # t        $ r d }Y w xY w)Nr)   )wrap_fx_proxyrj  r   )rR   proxyr  z%trace_rules.lookup returned None for rs   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.rV   )builderr  r   is_callable_allowedrg  rl  r   create_proxyr'   r  r  r]   r   r  getattr_staticr   r   r   __get__lookupr[   create_with_sourcer   r   r   r   r   r   )r@   rR   rS   rz   r9   r  rk  r]   r   r   traceds              rB   r   z$AutogradFunctionVariable.call_methodV  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>(+PQUPV)WXX% 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   r@  r,   r   r   r?   r_   rH   r   r   r   r  r  r  r   r   r   r   s   @rB   ro  ro  w  s   7 			'	'N
s c d T '?#' S/)*' 
o		%	'RG'G ?#G S/)*	G
 
GR5'5 ?#5 S/)*	5
 
5,5'5 ?#5 S/)*	5
 
$5A'A A ?#	A
 S/)*A 
ArC   ro  c                   D    e Zd ZU  ej                  e      Zee   ed<   y)SavedTensorBox)default_factorytensorsN)	r   r   r   dataclassesfieldr   r  r,   r`  r=   rC   rB   r  r    s    %6[%6%6t%LGT/"LrC   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
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_tensorsNr`   r   needs_input_grad.non_differentiabledirty_tensorsr9   r:   c                 p    t        	|   d||d| || _        || _        || _        || _        || _        y N)r`   r   r=   )r>   r?   r  r  r  r  r  )
r@   r`   r   r  r  r  r  r  r9   rA   s
            rB   r?   z(AutogradFunctionContextVariable.__init__  sC     	FuFvF"* 0"4*rC   rR   r4   rz   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rE   )r  r  r3  xs     rB   r4  z9AutogradFunctionContextVariable.create.<locals>.<genexpr>  s"     $U1Q[[]%Fq%F$Us   (*T)r  r  r  )r   r   r   track_object_newr   r  functionFunctionCtx	functoolspartialr  r  )rR   rz   r9   r  outs        rB   rx   z&AutogradFunctionContextVariable.create  sz      $$UPT$UUii$$55NN##///,.!1	 

 
rC   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.rV   )r  r   r   r   rG   s    rB   as_proxyz(AutogradFunctionContextVariable.as_proxy  sA    ::'#D6*25)445 zzrC   rS   c                 .   |dk(  rt         |   ||||      S |dk(  rO|rt        ||dt        |       d       t	        |i       d   | _        t        j                  j                  d       S |dk(  rt|rt        ||dt        |       d       t        | dd       %t        dd	|  d
| dg t        j                         || _        t        j                  j                  d       S |dk7  r)t        dd	|  d
| d| dg t        j                         | j                  t        dd	|  d
| ddg       | j                  t        d      | j                   s@|s| j"                  st%        |d       |j&                  j(                  j+                  | |       t        | j                  j,                        dkD  rg | j                  _        |D ]'  }| j                  j,                  j/                  |       ) t        j                  j                  d       S )Nr  mark_non_differentiablez0 kwargsrN  r   
mark_dirtyr  z2Unsupported autograd.Function context `mark_dirty`r}   r~   zKDynamo only supports tracing ctx.mark_dirty inside autograd.Function.apply.rV   save_for_backwardz,Unsupported autograd.Function context methodr  z` on `autograd.Function` context objects. Supported methods are `__setattr__`, `save_for_backward`, `mark_dirty` and `mark_non_differentiable`.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:saved_tensors must be initialized before save_for_backwardz>save_for_backward() requires a source and no keyword arguments)r>   r   r(   rf   r'   r  r   r-   rx   rd   r   r   r   r  r  r[   r  r]   r   r   r   track_save_for_backwardr  r  )r@   rR   rS   rz   r9   r)  rA   s         rB   r   z+AutogradFunctionContextVariable.call_method  s<    = 7&r4v>>..#BjS[M:QR&7b&A!&DD#--44T::\!#BjS[M:QRtWd+3P*4&$8!6:-99: "&D--44T::&&F&tfAdV46 >> 7)556 %S&tfAdV4Ow
 % L  ~~T[[ X II""::4F t!!))*Q.)+D& 	3C&&--c2	3))0066rC   c                     dv rt         fd      S dk(  rS j                  t        j                  j	                  d       S t        j
                  t         j                              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         A        S )N)r  r  r  c                  >    j                  t        |       |      S rE   )r   r   )rz   r9   rS   r@   rR   s     rB   rM   z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>+  s    (8(8T4:v(V rC   r  r  r  )r   r  r   r-   rx   r   r   r  r  r  r]   r   r,   r   r`   r>   ry   )r@   rR   rS   r]   rA   s   ``` rB   ry   z+AutogradFunctionContextVariable.var_getattr&  s    QQ!V  ?"!!) 1188>>**40B0B+CDD?"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,,rC   )NFNNNNNN)r   r   r   r@  r2   r   r   r_   r  r   r   r,   r?   r   r   r   rx   r  r   ry   r   r   s   @rB   r  r    s   
 	 
#	1	1	N #'$(48)-6:++ 4K+ 	+
 Tz+  c	*T1+  $J+ O,t3+ + 
+$  .248'?#d* S/)*T1 
	 ,# C7'C7 C7 ?#	C7
 S/)*C7 
C7J---58-	- -rC   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.
    Nr`   r   r9   r:   c                 *    t        |   d||d| y r  rd  )r@   r`   r   r9   rA   s       rB   r?   zAutogradEngineVariable.__init__C  s     	FuFvFrC   rR   r4   rS   rz   c                    |dk(  rt         j                  j                  j                  r|j                  s|j
                  st        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.rV   z-Unsupported torch._C._ImperativeEngine methodzgDynamo only supports the `queue_callback` method on a torch._C._ImperativeEngine instance, but found: `z`.)r   r  compiled_autogradin_compiled_autograd_region	one_grapherror_on_graph_breakr[   r,   r   r  FakeCompiledAutogradEnginer  r   r   r   get_ca_final_callbacks_varr   )r@   rR   rS   rz   r9   r   s         rB   r   z"AutogradEngineVariable.call_methodK  s     ##}}..JJ(?(?(r  (--MM00KKZZ **YY++FFHP4P  U*4&$8!H G&tfAdV4IIMbRrC   rE   )r   r   r   r@  r   r   _ImperativeEnginer_   r   r?   r   r   r,   r   r   r   r   s   @rB   r  r  >  s     ?CGxx))G 334t;G 	G
 
G&'& & ?#	&
 S/)*& 
&rC   r  c                   j     e Zd Zdedef   deddf fdZdefdZdd	d
e	e   de
eef   defdZ xZS )r   rW  .r9   r:   Nc                 2    t        |   di | || _        y r<   )r>   r?   rW  )r@   rW  r9   rA   s      rB   r?   zLambdaVariable.__init__v  s    "6"rC   c                 "    t         j                  S rE   )r   r   rG   s    rB   rH   zLambdaVariable.python_typez  s    !!!rC   rR   r4   rz   c                 &     | j                   |i |S rE   )rW  r  s       rB   r   zLambdaVariable.call_function}  s     tww'''rC   )r   r   r   r   r,   r   r?   r_   rH   r   r   r   r   r   r   s   @rB   r   r   t  sm    8C$89 S T "T "('( ?#( S/)*	(
 
(rC   r   c                   f    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edeeef   fdZdddedefdZddZdddddeddfdZdddee   deeef   defdZdddedef fdZ xZS )ru   rS   rr   Nr   r9   r:   c                     t        |   di | t        |t              st	        dt        |             t        |t              st	        dt        |             || _        || _        || _	        y )Nz#obj must be a VariableTracker, got zname must be a str, got r=   )
r>   r?   r   r,   r[   r_   r   r   rS   rr   )r@   r   rS   rr   r9   rA   s        rB   r?   zGetAttrVariable.__init__  sl     	"6"#/ #FtCyk!RSS$$ #;DJ<!HII	rC   c                 P    | j                   | j                   S t        | 	         S rE   )rr   r>   rH   r@   rA   s    rB   rH   zGetAttrVariable.python_type  s%    <<#<<7&((rC   c                 h    | j                   j                   d| j                   d| j                   dS Nr&  r1  r'  )rA   r   r   rS   rG   s    rB   r<  zGetAttrVariable.__repr__  s.    ..))*!DHH:R		{!DDrC   
base_proxyr   c                     t        | |      S rE   )rd   )r  r   s     rB   create_getattr_proxyz$GetAttrVariable.create_getattr_proxy  s    z4((rC   c                 r    t         j                  | j                  j                         | j                        S rE   )ru   r  r   r  rS   rG   s    rB   r  zGetAttrVariable.as_proxy  s&    33DHH4E4E4GSSrC   c                     | j                   j                         }	 t        || j                        S # t        $ r t        |  d      d w xY w)Nz is not a constant)r   r\   rd   rS   r   r   )r@   constants     rB   r\   z"GetAttrVariable.as_python_constant  sQ    88..0	M8TYY// 	M%.@&ABL	Ms	   2 ArR   c                     	 | j                         }t              dfS # t        t        f$ r, ddlm}  |dd|  d|  dg t
        j                         Y Gw xY w)	Nr   )r   z!Non-constant GetAttrVariable hashz
hash_impl zCannot hash zh because Dynamo doesn't know how to represent the type of the getattr() result, which is not a constant.rV   F)r\   r*   r   r   r   r   r   hash)r@   rR   r   r   s       rB   	hash_implzGetAttrVariable.hash_impl  sw    	))+C Cy% 45HI 		+;$TF+*4& 1M M6)556		s    8AAr4   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 rE   )r   r   r   r  r   r   get_submodule
module_keyrS   rg   r  r  )r@   rR   rS   step1step2s        rB   const_getattrzGetAttrVariable.const_getattr  s    $((I$>$>?%%		''(;(;<99ENN*%%&&udii8u~~%%%%%eT22rC   c                 |     || j                          |j                  |j                  | j                               y rE   )r   rO   create_load_attrsrS   rI  s     rB   rQ   zGetAttrVariable.reconstruct  s+    g77		BCrC   r   r,   r   c                    ddl m} 	 | j                  j                  || j                        }|t        |t              r| j                  j                         rEt        | j                  j                         | j                        }t        j                  ||      }n)t        dd|  d| d|  dg t        j                          |||||      S # t
        $ r d }Y w xY w)	Nr)   )generic_richcomparez%Unresolved GetAttrVariable comparisonzrichcompare_impl r~   zCannot compare zA because the attribute could not be resolved to a concrete value.rV   )r  r  r   ry   rS   r   r   ru   is_python_constantrd   r\   r,   r   r   r   r   )r@   rR   r   r   r  r  r   s          rB   r  z GetAttrVariable.richcompare_impl  s     	9	xx++B		:H z(ODxx**,dhh99;TYYG*00S9C/vQrd;"1$ 8A !B:-99: #2x;; # 	H	s   &C C! C!rz   c                 R    | j                   j                  || j                  ||      S rE   )r   r   rS   r  s       rB   r   zGetAttrVariable.call_function  s#     xx##B		4@@rC   keyc                     | j                   dk(  rAt        | j                  d      r+| j                  j                  |      j	                  ||      S t
        |   ||      S )Nrg   get_dict_vt)rS   r   r   r  mp_subscript_implr>   )r@   rR   r  rA   s      rB   r  z!GetAttrVariable.mp_subscript_impl  sR    
 99
"wtxx'G88''+==b#FFw(S11rC   rE   r   )r   r   r   r,   r   r   r_   r   r?   rH   r<  r   r   fxProxyr  r  r\   r   intr  r  r  rQ   r  r   r   r   r  r   r   s   @rB   ru   ru     s    
	'	'N  $	  	
  
 )T )E# E ) )s )s ) )T# TMC M C  E#t)$4  "	3 ; 	33 	33 	3D<-<6G<MP<	<.A'A ?#A S/)*	A
 
A2'2 2 
	2 2rC   ru   c                   *    e Zd Zej                  Zddhej                  Zdej                  deddf fdZ	de
ej                     fdZdej                  fdZdej                  fd	Zdefd
ZdddeddfdZdddedefdZdddddeddfdZ xZS )PythonModuleVariabler`   is_torchr9   r:   Nc                     t        |   di | || _        | j                  t        u xs% | j                  j                  j                  d      | _        y )Nztorch.r=   )r>   r?   r`   r   r   
startswithr  r@   r`   r9   rA   s      rB   r?   zPythonModuleVariable.__init__  sD    "6"


e+Wtzz/B/B/M/Mh/WrC   c                 "    t         j                  S rE   )r   
ModuleTyperG   s    rB   rH   z PythonModuleVariable.python_type  s    rC   c                     | j                   S rE   r`   rG   s    rB   r\   z'PythonModuleVariable.as_python_constant      zzrC   c                     | j                   S rE   r  rG   s    rB   get_real_python_backed_valuez1PythonModuleVariable.get_real_python_backed_value  r  rC   c                 "    d| j                    dS )NzPythonModuleVariable(r'  r  rG   s    rB   r<  zPythonModuleVariable.__repr__  s    &tzzl!44rC   rR   r4   rS   r-   c                 Z    t        | j                  |      }t        j                  ||      S rE   )r   r`   r,   r   r@   rR   rS   r  s       rB   call_obj_hasattrz%PythonModuleVariable.call_obj_hasattr  s'     T*$$R00rC   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rE   )r   r   has_pending_mutation_of_attr	load_attrr  r`   rg   rd   r   r   r]   r   r,   r   )r@   rR   rS   r   r]   s        rB   ry   z PythonModuleVariable.var_getattr  s     99!!>>tTJ99))33D$??
==D

(;(;;=$TZZ6
 ,,T2J>DKK!>$$RV<< " =(<=s   4C C;:C;r   r,   r   c                 $    ddl m}  || |||      S r   r   r  s        rB   r  z%PythonModuleVariable.richcompare_impl0  r  rC   )r   r   r   r   r  r   r,   r   r   r?   r_   rH   r\   r!  r   r<  r%  ry   r  r   r   s   @rB   r  r    s    $$M 	 
	'	'NXe.. X# X$ X
 T%"2"23  E$4$4 e.>.> 5# 51-1581	1=-=58=	=$7-76G7MP7	7rC   r  c            
           e Zd Zdededdf fdZdddedefd	Zddd
ddeddfdZdddede	e   de
eef   def
dZ	 dddd
ededefdZdddedefdZdefdZdedeeef   fdZdefdZd fdZd
edefdZ xZS )TypingVariabler`   r9   r:   Nc                 2    t        |   di | || _        y r<   r>   r?   r`   r  s      rB   r?   zTypingVariable.__init__9      "6"
rC   rR   r4   r  c                     |j                         s1t        dd| dd| j                   dg t        j                         | j                  |j                            }t        |      S )Nznon-constant typing subscriptzTypingVariable[]z"Cannot subscript typing construct z with a non-constant key.rV   )r  r   r`   r   r   r\   r+  )r@   rR   r  
new_typings       rB   r  z TypingVariable.mp_subscript_impl=  sk     %%'7)#a0@Lef6)556	 ZZ 6 6 89
j))rC   r   r,   r   c                     |dv r_t        |t              r6| j                  |j                  k(  }|dk(  r| }t        j                  |      S t        j                  t
              S t        j                  t
              S )N)__eq____ne__r4  )r&   r+  r`   r-   rx   NotImplemented)r@   rR   r   r   r  s        rB   r  zTypingVariable.richcompare_implM  sj     %%e^,u{{2>!'ZF'..v66#**>::&&~66rC   rS   rz   c                     t        dd| j                   d| d| d| d| d| j                   dd	| d
| j                   dgt        j                         y )Nz,unsupported method call on `typing` variableztyping variable: z, method name: , args: 
, kwargs: z.`torch.compile` does not support method call `z` on `typing` variable f.zAvoid calling the z method on rV   )r   r`   r   r   r@   rR   rS   rz   r9   s        rB   r   zTypingVariable.call_methodY  sy     	B'

|?4&QUPVV`ag`hiHNfgkgqgqfrrst$TF+djj\C"..		
rC   reversec                 D   |j                         st        j                  |t              S |j	                         }t        | j                        j                  | j                  |      }|t        u rt        j                  |t              S t        j                  ||      S rE   )r  r,   r   r5  r\   r_   r`   __or__)r@   rR   r   r;  	other_valr  s         rB   
nb_or_implzTypingVariable.nb_or_implj  s~     '')"((^<<,,.	djj!((Y?^#"((^<<$$R00rC   c           
         ddl m}m} |t        v r5t	        j
                  | |t        t        | j                  |                  S |j                  j                  j                  | |      r&|j                  j                  j                  | |      S t        | j                  |      }| j                  r%t        | j                  |      }  |||      |      S |j                  ||      S )Nr)   )SourcelessBuilderVariableBuilderrq   )r  rA  rB  r$   r   ru   r_   rd   r`   r   r   r'  r(  r]   r   rx   )r@   rR   rS   rA  rB  r`   r   s          rB   ry   zTypingVariable.var_getattr{  s     	@)),,dDT)B$C  99!!>>tTJ99))33D$??

D);;$T[[$7K3?2{3E::$++B66rC   c                     | j                   S rE   r  rG   s    rB   r\   z!TypingVariable.as_python_constant  r  rC   c                 0    t        | j                        dfS NFr   r`   r9  s     rB   r  zTypingVariable.hash_impl      DJJ&&rC   c                     | j                   S rE   r  rG   s    rB   r!  z+TypingVariable.get_real_python_backed_value  r  rC   c                     t        | j                  t        j                        st        |   |      S |j                  |j                  | j                               y rE   )r   r`   r   GenericAliasr>   rQ   append_outputcreate_load_constrP   s     rB   rQ   zTypingVariable.reconstruct  sF    $**e&8&897&w//0 	g77

CDrC   c                 h    t        |t              xr! | j                         |j                         k(  S rE   r   r,   r\   r@   r   s     rB   is_python_equalzTypingVariable.is_python_equal  1    uo. H'')U-E-E-GG	
rC   )Fr   )r   r   r   r   r?   r,   r  r   r  r   r   r   r  r?  ry   r\   r   r  r  r!  rQ   r   rP  r   r   s   @rB   r+  r+  8  sA   c S T *'* * 
	* 
7-
76G
7MP
7	
7
'
 
 ?#	

 S/)*
 

* 	1'1 1 	1
 
1"7-7587	7(C 'C 'E#t)$4 'c E8
V 
 
rC   r+  )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)utilsrT  r   rg   callablerd   )rT  np_fn_to_tnp_fnnp_modtnp_modrT  tnp_fnnp_fns          rB   get_np_to_tnp_mapr\    sz     )O+113 4&//557 	4OGV $FGT::5:-3OE*	44 rC   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.
    )r\  r   )mkvs      rB   get_tnp_to_np_mapra    s.     	AWWY'TQAqD'''s   /c            
       2    e Zd ZdZej
                  fZdededdf fdZdefdZ	e
dej                  defd	       Ze
dej                  defd
       Zdddee   deeef   defdZdddedee   deeef   def
dZdefdZdef fdZded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.
    r`   r9   r:   Nc                 2    t        |   di | || _        y r<   r-  r  s      rB   r?   zNumpyVariable.__init__  r.  rC   c                     | j                   S rE   r  rG   s    rB   r!  z*NumpyVariable.get_real_python_backed_value  r  rC   rW  c                     |j                   j                  d      }t        |      dk  rt        d|       |d d ddgk7  r t        ddj	                  |d d              || j
                  v S Nr9  r   z0Expected module path with at least 2 parts, got r   _numpyz"Expected torch._numpy module, got )r   splitrf   r[   r6  constant_fold_functionsr   rW  mods      rB   can_constant_fold_throughz'NumpyVariable.can_constant_fold_through  s    mm!!#&s8a< B3%H  r7w)) 4SXXc"1g5F4GH  S0000rC   c                     |j                   j                  d      }t        |      dk  rt        d|       |d d ddgk7  r t        ddj	                  |d d              t
        j                  |      S rg  )r   ri  rf   r[   r6  np_constant_collections_maprh   rk  s      rB    get_constant_collection_for_funcz.NumpyVariable.get_constant_collection_for_func  s    mm!!#&s8a< B3%H  r7w)) 4SXXc"1g5F4GH  +..r22rC   rR   r4   rz   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                         |t        d| 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/        ||      rvt1        j2                  | | j                         |D cg c]  }|j                          c}i |j                         D 	
ci c]  \  }	}
|	|
j                          c}
}	      S  |j4                  j6                  d ||      gt9        ||       }|j;                  ||      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: r7  r8  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.rV   r   )numpy_to_tensor_wrapperr)   )NumpyNdarrayVariablez8attempted to trace numpy function unsupported by PyTorchz  (corresponding torch function: r'  z Can't find numpy numpy function z in torch._numpy.z+Could not find torch._numpy equivalent for 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 r9  r   )r   trace_numpyr   r`   rU  rs  tensorrt  r\  rh   r   r   r[   rp  r\   r   r*   
USER_ERRORr   use_numpy_random_stream
patch_argsr   rm  r#   r,   r   r   r  r'   rx   )r@   rR   rz   r9   rs  rt  r   collection_variable_typr  r_  r`  r  s               rB   r   zNumpyVariable.call_function  sp    !!Y*4::,htfJvhW@ MM M _ 	40 "&&tzz2<R*4::,htfJvhVvw{v||}~>tzzlJ[\&22	 < =djj\J  (,'L'LT'RR#.DJJ:>?Q!..0?AGPA1a2244P & #8822v.tzzl(4&
SYRZZz{  {A  AB  C"5djj\ B& !& W(A6	 0::4==$PVWLD&--d3-dF; ',,-D++-:>?Q!..0?AGPA1a2244P  +BII**'- #40E
 (..r599g @P 7 
r.tzzl(4&
SYRZZz{  {A  AB  C"1$** >@ !@*55	
H @Ps=   &J+ 8J J+ %J% 	J+ K>;L J+ +AK;:K;rS   c           	      h    t        dd| j                   d| d| dg t        j                         y )Nz/attempted to trace numpy.* function as a methodrr  r7  r8  z6Tracing numpy.* functions as methods is not supported.rV   )r   r`   r   r   r:  s        rB   r   zNumpyVariable.call_methoda  s?     	E&tzzl(4&
6(SP",,		
rC   c                     | j                   S rE   r  rG   s    rB   r\   z NumpyVariable.as_python_constantq  r  rC   c                     t         j                  r`t        | j                  t        j
                        r| j                  S t        | j                  t              r| j                  j                  S t        | %         S rE   )
r   ru  r   r`   enumEnumMetar_   r   r>   r  r  s    rB   r  zNumpyVariable.as_proxyt  sV    $**dmm4zz!$**d+ zz***w!!rC   r   c                 h    t        |t              xr! | j                         |j                         k(  S rE   rN  rO  s     rB   rP  zNumpyVariable.is_python_equal  rQ  rC   )r   r   r   r@  tnp
issubdtyperj  r   r?   r!  r   r   r   r  rm  rp  r   r,   r   r   r   r   r   r\   r  r   rP  r   r   s   @rB   rc  rc    s=     #~~/c S T c  
15+=+= 
1$ 
1 
1 
3%2D2D 
3 
3 
3]:']: ?#]: S/)*	]:
 
]:~
'
 
 ?#	

 S/)*
 

 $7 "# "
V 
 
rC   rc  c                   L     e Zd Zdeddf fdZdefdZd
dZdddefd	Z xZ	S )NullVariabler9   r:   Nc                 $    t        |   di | y r<   rd  re  s     rB   r?   zNullVariable.__init__  rf  rC   c                      y)Nr  r=   rG   s    rB   r<  zNullVariable.__repr__  s    rC   rL   r3   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.11rU   zdAttempted to generate PUSH_NULL instruction in Python < 3.11; where this instruction does not exist.rV   	PUSH_NULL)sysversion_infor   r   r   rK  r   rI  s     rB   rQ   zNullVariable.reconstruct  sL    g%J9&11 	0=>rC   c                      y)NNoner=   rI  s     rB   reconstruct_pycodezNullVariable.reconstruct_pycode  s    rC   r   )
r   r   r   r   r?   r   r<  rQ   r  r   r   s   @rB   r  r    s:    # # ## ?+ # rC   r  c                       e Zd ZdZy)r   z"Marker used to implement delattr()Nr?  r=   rC   rB   r   r     s    ,rC   r   c            
            e Zd ZdZdhej
                  ZdefdZede	de
e   dee	ef   defd       Zde	de
e   dee	ef   dedd	f
 fd
Zde	fdZededefd       Zde	fdZddZ xZS )StringFormatVariablez[
    Represents a call to str.format(), we delay calling format until after the graph.
    format_stringr:   c                     t         S rE   )r   rG   s    rB   rH   z StringFormatVariable.python_type  s    
rC   sym_args
sym_kwargsc                    t        d t        j                  ||j                               D              r{t        j
                  j                   |j                  |D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}}      S  | |t        |      t        |            S c c}w c c}}w )Nc              3   <   K   | ]  }|j                           y wrE   )r  r  s     rB   r4  z.StringFormatVariable.create.<locals>.<genexpr>  s!      
   "
s   )all	itertoolschainvaluesr   r-   rx   formatr\   r   r   r   )r   r  r  r  r`  r_  s         rB   rx   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r9   Nc                     t        |   di | t        |t              st	        dt        |             || _        || _        || _        y )Nz!format_string must be a str, got r=   )	r>   r?   r   r   r[   r_   r  r  r  )r@   r  r  r  r9   rA   s        rB   r?   zStringFormatVariable.__init__  sT     	"6"-- 3D4G3HI  + $rC   c                     | j                   j                   d| j                  d| j                  d| j                  dS r  )rA   r   r  r  r  rG   s    rB   r<  zStringFormatVariable.__repr__  sB    ..))*!D,>,>+ADMMCTTVW[WfWfViijkkrC   r)  c                 b    	 | j                         S # t        $ r | j                         cY S w xY wrE   )r\   r,  r-  r.  s    rB   r/  z&StringFormatVariable._debug_format_arg  s2    	$))++ 	$>>##	$s    ..c                 `   	  | j                   j                  | j                  D cg c]  }| j                  |       c}i | j                  j                         D ci c]  \  }}|| j                  |       c}}}t        |      S c c}w c c}}w # t        $ r t        |       cY S w xY wrE   )r  r  r  r/  r  r   r,  r+  )r@   r)  r  r`   rendereds        rB   r-  zStringFormatVariable.debug_repr  s    		0t))009=G#$((-G '+oo&;&;&="U //66H H~ H
  	:	s-   $B B B B:B B B-,B-c                     j                   fdd        t        j                  t         j                                      j
                  j                         D ci c]%  \  }}t        j                  j                  |      |' }}} t        j                  |             j                  t        dd             y c c}}w )Nc                  z     j                   j                  j                         j                  d      g      S )Nr  )rO   rL  r  create_load_attrr  s   rB   rM   z2StringFormatVariable.reconstruct.<locals>.<lambda>  s9    G))--d.@.@A,,X6 rC   T)call_function_exF)rN   r   r   r   r  r  r   r-   rx   ConstDictVariablerO   r   )r@   rL   r_  r`  r9   s   ``   rB   rQ   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   r@  r,   r   r_   rH   r   r   r   r   rx   r   r?   r<  r   r   r/  r-  rQ   r   r   s   @rB   r  r    s     &G(F(FGNT  DD 'D o-.	D
 
D D$%% '% o-.	%
 % 
% l# l $ $6 $ $C DrC   r  c                   N     e Zd ZeZdededdf fdZdefdZdee   fdZ	 xZ
S )ObjectVariabler`   r9   r:   Nc                 2    t        |   di | || _        y r<   r-  r  s      rB   r?   zObjectVariable.__init__  r.  rC   c                     | j                   S rE   r  rG   s    rB   r!  z+ObjectVariable.get_real_python_backed_value  r  rC   c                     t         S rE   )r   rG   s    rB   rH   zObjectVariable.python_type  s    rC   )r   r   r   r   r   r   r?   r!  r_   rH   r   r   s   @rB   r  r    s>    Mf   f T&\ rC   r  c            	            e Zd ZdZdededdf fdZdefdZedede	e
j                  e
j                  z     fd	       Zd
ddee   deeef   defdZddZededee   deeef   defd       Z xZS )DebuggingVariablez
    Represents a call to a debugging function like print(), or something
    registered to config.reorderable_logging_functions.
    r`   r9   r:   Nc                 2    t        |   di | || _        y r<   r-  r  s      rB   r?   zDebuggingVariable.__init__  r.  rC   c                 ,    t        | j                        S rE   r_   r`   rG   s    rB   rH   zDebuggingVariable.python_type      DJJrC   r   c                     t        |       xrR t        | t        j                  t        j                  f      xr& | t
        j                  j                  j                  v S rE   )	rV  r   r   r   r   r   r  r   reorderable_logging_functionsr   s    rB   is_reorderable_logging_functionz1DebuggingVariable.is_reorderable_logging_function  sO    
 SM J3!3!3U5N5N OPJu}}++III	
rC   rR   r4   rz   c           	      V   |j                   rt        j                  d       S | j                  | j                  ||      s1t        dd| j                   d| d| dd| j                   dg       |j                  j                  | t        |      f       t        j                  d       S )	NzJattempted to reorder a debugging function that can't actually be reorderedzfn: r7  r8  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.rV   )	exportr-   rx   can_reorder_logsr`   r   debug_localsr  r   r  s       rB   r   zDebuggingVariable.call_function#  s     99#**400$$TZZv>dtzzl(4&
6(K@ :$**Egh 	d4j12&&t,,rC   c                 f    | j                   t        d      | j                   j                  |      S )Nz7DebugLocalVariable requires a source for reconstruction)r]   r[   rQ   rI  s     rB   rQ   zDebuggingVariable.reconstruct;  s3    ;; I  {{&&w//rC   rW  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   TensorVariabler-   r  pytreetree_leavesr   )rW  rz   r9   allowed_input_types	flat_argsr)  s         rB   r  z"DebuggingVariable.can_reorder_logsB  sY     $$&& 
 &&f~6	 	Cc#67	 rC   r   )r   r   r   r@  r   r?   r_   rH   r   r	   r   r   r   r  r   r,   r   r   r   rQ   r   r  r  r   r   s   @rB   r  r    s    
c S T  T   

	5%%(A(AA	B
 
-'- ?#- S/)*	-
 
-00 S  tCH~ RV  rC   r  c                   p     e Zd ZdZdededdf fdZdefdZdefdZd	d
de	e
   deee
f   de
fdZ xZS )IgnoredFunctionVariablezL
    Represents a call to an arbitrary function that should be ignored.
    r`   r9   r:   Nc                 2    t        |   di | || _        y r<   r-  r  s      rB   r?   z IgnoredFunctionVariable.__init__\  r.  rC   c                 ,    t        | j                        S rE   r  rG   s    rB   rH   z#IgnoredFunctionVariable.python_type`  r  rC   c                     | j                   S rE   r  rG   s    rB   r!  z4IgnoredFunctionVariable.get_real_python_backed_valuec  r  rC   rR   r4   rz   c                 @    t         j                  j                  d       S rE   )r   r-   rx   r  s       rB   r   z%IgnoredFunctionVariable.call_functionf  s     ))0066rC   )r   r   r   r@  r   r?   r_   rH   r!  r   r,   r   r   r   r   r   s   @rB   r  r  W  st    c S T  T  c 7'7 ?#7 S/)*	7
 
7rC   r  c            
            e Zd ZdZdej
                  deddf fdZdefdZ	dej
                  fdZ
d	d
dedee   deeef   def
dZ xZS )LoggingLoggerVariablez:
    Represents a call to any logging.Logger methods.
    r`   r9   r:   Nc                 2    t        |   di | || _        y r<   r-  r  s      rB   r?   zLoggingLoggerVariable.__init__t  r.  rC   c                 ,    t        | j                        S rE   r  rG   s    rB   rH   z!LoggingLoggerVariable.python_typex  r  rC   c                     | j                   S rE   r  rG   s    rB   r!  z2LoggingLoggerVariable.get_real_python_backed_value{  r  rC   rR   r4   rS   rz   c                    |j                   rt        j                  j                  d       S t	        | j
                  |d       }t	        |dd       }t        j                  j                  j                  }||v s||v rt        j                  j                  d       S t        dd| j
                   d| d| d| ddg	       y )
Nr   z8logging.Logger method not supported for non-export caseszmethod: r9  r7  r8  z>logging.Logger methods are not supported for non-export cases.zJAdd the logging method to `torch._dynamo.config.ignore_logging_functions`.rV   )r  r   r-   rx   rd   r`   r   r  r   ignore_logging_functionsr   )r@   rR   rS   rz   r9   methodr  
ignore_sets           rB   r   z!LoggingLoggerVariable.call_method~  s     99--44T::T406:t4 ]]))BB
Z8z#9--44T::Ntzzl!D6$z&RX\		
rC   )r   r   r   r@  loggingLoggerr   r?   r_   rH   r!  r   r   r,   r   r   r   r   s   @rB   r  r  o  s    gnn    T  gnn 
'
 
 ?#	

 S/)*
 

rC   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eef   fdZdddddedd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 uw xY w)ConstantLikeVariablez8self.value is a compile-time constant, but not a literalr   )dtypefloatinggenericinvalid_typer=   r`   r9   r:   Nc                 2    t        |   di | || _        y r<   r-  r  s      rB   r?   zConstantLikeVariable.__init__  r.  rC   c                     t        | j                        }|j                  dk(  r|j                  S |j                   d|j                   S )z4Dynamically compute the prefix from the value's typerF   r9  )r_   r`   r   r   )r@   ts     rB   _error_prefixz"ConstantLikeVariable._error_prefix  sD      <<:%>>!,,q 011rC   c                     | j                   S rE   r  rG   s    rB   r\   z'ConstantLikeVariable.as_python_constant  r  rC   rR   r4   c                 0    t        | j                        dfS rE  rF  r9  s     rB   r  zConstantLikeVariable.hash_impl  rG  rC   r   r,   r   c                 $    ddl m}  || |||      S )Nr)   ) python_constant_richcompare_impl)r  r  )r@   rR   r   r   r  s        rB   r  z%ConstantLikeVariable.richcompare_impl  s     	F/b%DDrC   rS   rz   c                    g i }}	 |D cg c]  }|j                          }}|j                         D 	ci c]  \  }}	||	j                          }}}	 t        | j                  |      |i |}
t        j                  j                  |
      rt        j                  ||
      S t        |
t        j                        rt        |
      S t        d
| j                   d| d| d| d|
 	d| j                   d| d|
 dg t         j"                  	       y c c}w c c}	}w # t        $ r; t        d| j                   d| d| d| dd| j                   d| ddg	       Y w xY w)Nz0constant-like method call with non-constant argsr9  z(*z, **r'  zAttempted to call z with non-constant args.zFEnsure that the args to the method call are constant (int, str, etc.).rV   z6constant-like method call with unsupported return typez) returned z, got unsupported return value )r\   r   r   r   r  rd   r`   r   r-   rt   r,   r   r   reMatchr  r   r   )r@   rR   rS   rz   r9   cargsckwargsr  r_  r`  r  s              rB   r   z ConstantLikeVariable.call_method  s    Rw	59:Q))+:E:=C\\^LTQq!..00LGL +T*E=W=%%008"((V44fbhh''//L))*!D6D6fX[QWPXY,T-?-?,@$Gfgmfnnop"..		
' ;L" 	J--.avRvT&K01C1C0DAdVKcd\	 	s-   D! DD! DD! D! !A E%$E%c                    t        | j                  |      }t        || j                        rt	        |      }t        || j
                        rt        |      S t        |t              r!t        || j                        rt        |      S t        j                  j                  |      rt        j                  ||      S t!        | |t        |            S )Nrq   )rd   r`   r   np_floatingfloatnp_dtypeNumpyDTypeVariabler_   r^   
np_genericrc  r   r-   rt   r,   r   ru   r$  s       rB   ry   z ConstantLikeVariable.var_getattr  s     T*fd../6]Ffdmm,%f--fd#
64??(K ((%%008"((V44tT4<@@rC   )r   r   r   r@  numpyr  r  r  r  r  r  ImportErrorr_   r   r?   propertyr   r  r\   r   r  r  r  r  r   r,   r   r   ry   r   r   s   @rB   r  r    s1   B
0	
 	
c S T  2s 2 2C '7 'E#t)<L 'E-E6GEMPE	E%
'%
 %
 ?#	%

 S/)*%
 
%
NA-A58A	AS  0>2r2B/	0s   
A- -B	B	r  c                       e Zd ZdefdZy)r  r:   c                 B    | j                   j                  j                  S )a0  Similar to how numpy dtype descriptors (e.g. np.float32 ) are handled by NumpyVariable:

        np.dtype() objects are serialized as strings, torch._numpy wrappers will normalize to the torch dtype.
        This also handles unsupported things nicely (i.e. structured arrays and object arrays).
        )r`   r_   r   rG   s    rB   r  zNumpyDTypeVariable.as_proxy  s     zz'''rC   N)r   r   r   r   r  r=   rC   rB   r  r    s    (# (rC   r  c                   `     e Zd ZdZdeddf fdZdefdZddd	ee	   de
ee	f   dd
fdZ xZS )RandomClassVariablezrandom.Randomr9   r:   Nc                 $    t        |   di | y r<   rd  re  s     rB   r?   zRandomClassVariable.__init__	  rf  rC   c                     t         S rE   rr  rG   s    rB   rH   zRandomClassVariable.python_type	  rs  rC   rR   r4   rz   RandomVariablec                 "   t        |      dkD  s|r%t        dd| d| dg t        j                         t        |      dk(  rt        j
                  j                  d       n|d   }t        |t        j                  j                               S )	Nr)   z'random.Random() with improper argumentszargs: r8  z=random.Random() with > 1 arg or with kwargs is not supported.rV   r   )seedr   )
rf   r   r   rw  r   r-   rx   r  baseValueMutationNew)r@   rR   rz   r9   r  s        rB   r   z!RandomClassVariable.call_function	  s     t9q=FA j9[&11	 ;>d)q.y))006dSTgY^^%D%D%F
 	
rC   )r   r   r   r@  r   r?   r_   rH   r   r,   r   r   r   r   r   s   @rB   r  r  	  s_    # # #T 
'
 ?#
 S/)*	

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

    Implemented by wrapping a VariableTracker around a random.Random object.
    The supported methods for the random.Random object cannot be overridden.
    Assumes that random objects behave the same given a set seed or state.
    random>   r  randintuniform	randrangeNrandr  r9   r:   c                 B   t        |   di | |_| j                  |      st        d      t	        j
                         | _        | j                  j                  |j                                y ||j                         nd }t	        j
                  |      | _        y )Nz8Unsupported random.Random object with overridden methodsr=   )	r>   r?   is_supported_random_objr[   r  r   setstategetstater\   )r@   r  r  r9   rA   s       rB   r?   zRandomVariable.__init__C	  s     	"6"//5$N  !--/DKKK  1040@4**,dD ---DKrC   c                 "    t         j                  S rE   )r  r   rG   s    rB   rH   zRandomVariable.python_typeU	  s    }}rC   c                     | j                   S rE   )r  rG   s    rB   r\   z!RandomVariable.as_python_constantX	  s    {{rC   r   c                    t        |       t        j                  uryt        j                  t
        j                  d      D ]  }t        | |      s yt        | |      }t        j                  |      r/|t        t        j                  |      j                  |       k7  s_ yt        |dd       t        t        j                  |      us y y)NF)r  r  r   r   T)r_   r  r   r  r  r  _supported_fn_namesr   rd   r  	isbuiltinr  )r   rS   meths      rB   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 	! rC   state.c                    t        |       t        urt        dt        |              t        | d         t        urt        dt        | d                t        | d         t        urt        dt        | d                t	        d | d   D              st        d      | d   /t        | d         t
        urt        d	t        | d                y y )
Nzstate must be a tuple, got r   zstate[0] must be an int, got r)   zstate[1] must be a tuple, got c              3   >   K   | ]  }t        |      t        u   y wrE   )r_   r  r  s     rB   r4  z-RandomVariable.check_state.<locals>.<genexpr>v	  s     4a47c>4s   z$all elements of state[1] must be intr   z$state[2] must be None or float, got )r_   r   r[   r  r  r  )r  s    rB   check_statezRandomVariable.check_staten	  s    ;e# #>tE{m!LMMa>$ #@eAh@P!QRRa>& #A$uQx.AQ!RSS45844 !GHH8DqN%$? 6tE!H~6FG  %@rC   c                 h   t         j                  |        t        j                  t        j                  j                  | d         t        j                  | d   D cg c]!  }t        j                  j                  |      # c}      t        j                  j                  | d         g      S c c}w )Nr   r)   r   )r  r  r   r   r-   rx   )r  r  s     rB   
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 rE   )r\   r  r  )r  	state_objs     rB   unwrap_statezRandomVariable.unwrap_state	  s&     ,,.	""9-rC   rR   r4   rS   rz   c                    dk(  r|j                   j                  j                  |         | j                  j                  |D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}} t        j                  j                  d       S dk(  r)| j                  | j                  j                               S dk(  rq|j                   j                  j                  |        | j                  j                  | j                  |d                t        j                  j                  d       S | j                  v r|j                   j                  j                  |        | j                  j                         dt        dt        dt        ffd} t!        | j                        |D cg c]  }|j                          c}i |j                         D 	
ci c]  \  }	}
|	|
j                          c}
}	 t#        ||||      S t$        | M  |||      S c c}w c c}}w c c}w c c}
}	w )	Nr  r  r   r   rz   r9   r:   c                  r    t        j                         }|j                          t        |      | i |S rE   )r  r   r   rd   )rz   r9   rrS   r  s      rB   call_random_methz4RandomVariable.call_method.<locals>.call_random_meth	  s3    MMO

5!'wq$'888rC   )r   r   mutationr  r  r\   r   r   r-   rx   r  r  r   r  r  r   rd   r0   r>   r   )r@   rR   rS   rz   r9   r  r  r   r  r_  r`  r  rA   s     `        @rB   r   zRandomVariable.call_method	  s    6>II""++D1DKK267Q!&&(7AGPXS#3..00P --44T::Z??4;;#7#7#9::ZII""++D1KK  !2!247!;<--44T::T---II""++D1KK((*E9 9s 9s 9 'GDKK&267Q!&&(79?HA1a**,,H
 ""&6fEEw"2tT6::7 8P* 8Hs   I5IIIc                 6   j                  fd       j                  dd       j                          j                  d        | j	                  | j
                  j                                      j                  dd       j                          y )Nc                  n     j                   j                  t               j                  d      g      S )Nr   )rO   create_load_python_moduler  r  rL   s   rB   rM   z,RandomVariable.reconstruct.<locals>.<lambda>	  s3    G))55f=,,X6 rC   r   Fr   r)   T)rN   r   r  r(  r  r  r  pop_toprI  s    `rB   rQ   zRandomVariable.reconstruct	  sz    	
 	a' 	*% 4 4 678a&rC   r  r   )r   r   r   r@  r  r   r   r,   r   r  r   r?   r_   rH   r\   r   r  r  r   r  r  r  r   r  r  r   r   r   r   rQ   r   r   s   @rB   r  r  -	  s    MMM 			'	'N
 &*'+.mmd". $. 	.
 
.$T&--0 FMM  V   $ 5eCHout|!CD    

%U38_edl BC 

 

 

 	sE#s(OUT\1	2 %;'%; %; ?#	%;
 S/)*%; 
%;NrC   r  c                        e Zd ZdefdZedddej                  e   de	dz  dedd f
d	       Z
d
edededdf fdZdddee   deeef   defdZddZdedeeef   fdZdddddeddfdZdedefdZ xZS )WeakRefVariabler:   c                 "    t         j                  S rE   )weakrefrefrG   s    rB   rH   zWeakRefVariable.python_type	  s    {{rC   rR   r4   weakref_valuer]   Noptionsc                     |t        d      |j                  }|xr t        |d      }t        j                  | ||      } |       }|xr t        |      }t        j                  | ||      }||d<   t        ||fi |S )Nz'WeakRefVariable.build requires a source__callback__r]   )r[   r#  r   r,   r   r"   r  )	rR   r   r]   r!  callbackcallback_sourcecallback_vtreferentreferent_vts	            rB   r   zWeakRefVariable.build	  s     > !JKK -- GZ%G%++B/J ?5-f5%++B&A"{KC7CCrC   r(  r&  c                 @    t        |   di | || _        || _        y r<   )r>   r?   r(  r&  )r@   r(  r&  r!  rA   s       rB   r?   zWeakRefVariable.__init__	  s%     	#7#&&rC   rz   r9   c                     | j                   S rE   )r(  r  s       rB   r   zWeakRefVariable.call_function	  s     rC   c                     j                  fd        | j                          | j                         j                  t	        dd             y )Nc                  (     j                  dd      S )Nr  r  )r  r  s   rB   rM   z-WeakRefVariable.reconstruct.<locals>.<lambda>	  s    g&>&>y%&P rC   r   F)rN   r(  r&  rO   r   rI  s    `rB   rQ   zWeakRefVariable.reconstruct	  sD    PQ  !  !21e<=rC   c                 4    ddl m}  ||| j                        S )Nr)   )generic_hash_impl)r  r.  r(  )r@   rR   r.  s      rB   r  zWeakRefVariable.hash_impl	  s     	7 T%5%566rC   r   r,   r   c                 $    ddl m}  || |||      S r   r   r  s        rB   r  z WeakRefVariable.richcompare_impl	  r  rC   c                 n    t        |t              sy| j                  j                  |j                        S rE  )r   r  r(  rP  rO  s     rB   rP  zWeakRefVariable.is_python_equal
  s,    %1//0A0ABBrC   r   )r   r   r   r_   rH   r   r  ReferenceTyper   r   r   r,   r?   r   r   r   r   rQ   r   r  r  r  r  r   rP  r   r   s   @rB   r  r  	  s"   T  D'D,,S1D D 	D
 
D D"'*'9H'UX'	' '  ?#  S/)*	 
 
 >7C 7E#t)$4 77-76G7MP7	7CV C CrC   r  )r@  rF   r  r~  r  r  r  r  r  r  r  r   r   r  collections.abcr   r   r   r   typingr   r   r	   r
   torch._Cr   torch._numpyrh  r  torch.utils._pytreerU  _pytreer  torch._dynamo.variables.baser   torch._dynamo.variables.listsr   torch._guardsr   rU   r   r   r   r   bytecode_transformationr   r   r   create_parameter_opr   r   r   r   r   guardsr   r   mutation_guardr   r]   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r  r*   r+   r,   r  r-   	functionsr.   r/   user_definedr0   r1   r2   torch._dynamo.codegenr3   torch._dynamo.symbolic_convertr4   r6   r   r   r	  r>  rB  rG  rZ  rb  rl  ro  	dataclassr  r  r  r   ru   r  r+  	lru_cacher   r   r\  ra  rc  r  r   r  r  r  r  r  r  r  finfoiinfor  ro  r  r  r  r=   rC   rB   <module>rG     s  "         	 
    .  % 7 7   $ $ 5 7   @ @ 
 G K K 0 5   W V & G X X /HY
O Y
x
-? -,h; h;V{< {<|o 
o 
2B7 B7J? 4# #
%c %hsCx6H %` `F	 M M M\-&? \-~36 3l(_ ($t2o t2n87? 87v@
_ @
F Q4 9 95;M;M MN   ( Q(4 2 2E4M4M MN (  (l
O l
`? 0-o -SD? SDl_  H HV7o 70*
O *
Z_A? _AD(- ( II#II#II! 
/ 
<\_ \~=Co =CrC   