
    9iF                       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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mZmZmZ ddlmZmZmZ ddl m!Z! ddl"m#Z#m$Z$ dd	l%m&Z&m'Z' dd
l(m)Z) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 ddlm1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZAmBZB ddlCmDZDmEZEmFZF erddlGmHZH ddlImJZJ  G d d          ZK G d de=          ZL G d de=          ZM G d de=          ZN G d deN          ZO G d de=          ZP G d  d!e=          ZQ G d" d#e=          ZRd$ ZS G d% d&e=          ZTejU         G d' d(                      ZV G d) d*eF          ZW G d+ d,eF          ZX G d- d.e=          ZY G d/ d0e=          ZZ G d1 d2e=          Z[ G d3 d4e=          Z\ G d5 d6e=          Z] G d7 d8e=          Z^ ej_        d9          d:             Z` ej_        d9          d;             Za G d< d=e=          Zb G d> d?e=          Zc G d@ dAe=          Zd G dB dCe=          Ze G dD dEe=          Zf G dF dGe=          Zg G dH dIe=          Zh G dJ dKeh          Zi G dL dMeh          Zjejk        ehejl        ehejm        ejiZn G dN dOe=          Zo G dP dQe=          Zp G dR dSe=          ZqdS )Ta  
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)OptionalTYPE_CHECKING   )configgraph_break_hintstrace_rules	variables)create_call_functioncreate_call_function_excreate_instruction)$do_not_convert_to_tracable_parameter)raise_observed_exceptionunimplemented)GuardBuilderinstall_guard)unpatched_nn_module_init)
AttrSourceGenericAttrSourceGetItemSourceTypeMROSource
TypeSourceWeakRefCallSource)	check_unspec_or_constant_argscmp_name_to_op_mappingidentityis_tensor_base_attr_getteristypelist_methodsproxy_args_kwargsraise_args_mismatchtuple_methods   )#AsPythonConstantNotImplementedErrorraise_type_error_excVariableTracker)ConstantVariable)NestedUserFunctionVariableUserFunctionVariable)call_random_fnis_standard_setattrUserDefinedObjectVariable)	PyCodegen)InstructionTranslatorc                       e Zd ZdS )NO_SUCH_SUBOBJN)__name__
__module____qualname__     O/var/www/icac/venv/lib/python3.11/site-packages/torch/_dynamo/variables/misc.pyr/   r/   P   s        Dr4   r/   c                   x     e Zd Zh ej        Zdd fdZd fdZd fd	Zddd
eddfdZ		 	 	 	 	 	 	 	 ddZ
 xZS )SuperVariableNreturnc                 V     t                      j        di | || _        || _        d S Nr3   )super__init__typevarobjvar)selfr=   r>   kwargs	__class__s       r5   r<   zSuperVariable.__init__Y   s6    ""6""" 
 r4   codegenr,   c                                         fd            | j                   | j        5 | j                                       t	          dd                     d S                     t	          dd                     d S )Nc                  H     t          j        t                              S N)r	   BuiltinVariabler;   )rA   rB   s   r5   <lambda>z+SuperVariable.reconstruct.<locals>.<lambda>f   s    ggi.G.N.N&O&O r4   r   Fr"   )add_push_nullr=   r>   extend_outputr
   r?   rB   rA   s    `r5   reconstructzSuperVariable.reconstructe   s    OOOOOPPP;"GDK   !!"6q%"@"@AAAAA!!"6q%"@"@AAAAAr4   txr-   c                 r   | j         s(t          ddd| d| j         d| j          ddg           | j                                        }| j                                         }| j         j        rt          | j         j                  nd }t          |t                    r| j         j	        }| j         j        }d }|j
        }	 |                    |          d	z   }n0# t          $ r# t          t          ||          |          d fcY S w xY wt          |t!          |                    D ]f}	||	         j                            |t&                    x}
r<|
t&          ur3|r+t)          t+          t-          |          |	          |          }|
|fc S gt          d
dd| d| j         d| j          dddg           d S )Nz1-arg super not implemented z"Dynamo failed to trace attribute `z$` accessed via `super()` (for type `z` and object `z4`) because one-argument of super() is not supported.z-Use two-argument super(type, object_or_type).gb_typecontextexplanationhintsr"   zUnable to resolve super getattrz8`) because the resolved attribute type is not supported.z0Ensure the attribute exists in the parent class.z(Check the arguments passed to `super()`.)r>   r   r=   as_python_constantpython_typesourcer   
issubclasstypevalue__mro__index
ValueErrorgetattrr;   rangelen__dict__getr/   r   r   r   )r?   rL   namesearch_typetype_to_usetype_to_use_sourcerV   
search_mrostart_indexr[   resolved_getattrrA   s              r5   _resolved_getattr_and_sourcez*SuperVariable._resolved_getattr_and_sourcen   sb   { 
	5D D D,0LD DHLD D D D	 	 	 	 l5577 k--//.2k.@JJt{)***d 	 k4(( 	4++K!%!3 (
	H$**;77!;KK 	H 	H 	H 5k::DAA4GGGG	H ;J88 
	4 
	4E#-e#4#=#A#A$#W#WW 4#>99) !+)-8J*K*KUSS " " ,V33335DT D D(,D DDHKD D D C:
	
 
	
 
	
 
	
 
	
 
	
s   C *DDrb   r%   c                 0   |                      | |          \  }}t          j                            |          st	          | |          S |r,t          |                    t          j                             t          j        	                    ||          S )NrV   )
ri   r	   r&   
is_literalGetAttrVariabler   
make_guardr   CONSTANT_MATCHcreate)r?   rL   rb   rY   rV   s        r5   var_getattrzSuperVariable.var_getattr   s     99$EEv)44U;; 	/"4... 	J&++L,GHHIII)00v0FFFr4   argslist[VariableTracker]r@   dict[str, VariableTracker]c                 6   |                      | |          \  }}|t          j        u rt          t                    S |t
          j        j        j        u r| j        }ddl	m
} t          |t          j                  r}t          |j        |          rh|sf|sdt                      5  t!          j        |t$          |          }	|	                    || j        g|z   |          cd d d            S # 1 swxY w Y   nt)          dd|  d| d| d| d| dg t*          j        	           nO| j        j        rt1          |d
          r|j        dk    rt          j                            |          rl|j        }
t1          |
d          r |
j        dk    rt          j        |
          }n|j        }t          j        |
|          }|                     |d||          S t          |tB                    rRt          |j"        tF          j$                  r3t!          j        ||j"        |          }	|	                    |||          S t          |tJ                    rt          |j"        tF          j$                  rt          | j        t          j                  r| j        }nGd }| j        j        rtM          | j        j                  }t!          j        || j        j'        |          }t!          j        ||j"        tQ          |d                    }	|	                    ||g||          S t          |tF          j$                  r7t!          j        |||          }	|	                    || j        g|z   |          S t          |tF          j)                  r6t          j*        |j"        | j        |                              |||          S tW          |          r0t          | j        t                    r | j        j,        |g|R i |S |t          j-        u r|d         }	 |.                                }n2# t^          $ r%}t)          dd|  d| ddg|           Y d }~nd }~ww xY w|j0        j	        1                    | j                  s/t)          dd|  d| d| j         ddgt*          j2        	           |j0        j	        3                    | j        |t          j4                               t          j5        d           S t          | j        t          j6                  r0|| j        j7        v r"| j        j8                             ||||          S t          | j        t          j9                  r0|| j        j:        v r"| j        j;                             ||||          S t          | j        t          j<                  r+|tz          v r"| j        j>                             ||||          S t          | j        t          j?                  r+|t          v r"| j        jA                             ||||          S |t          jB        u r|d         jC        }|j0        j	        D                    | j        |          rX|j0        j	        E                    | j        |d          }t          |t          j4                  rt          t          |           |S 	 t          B                    | j        jC        |          }n%# t          $ r t          t          |           Y nw xY wd }| j        j        t          | j        j        |          }t!          j        |||          S |t
          jI        jJ        u r|d         }i }|d         jK        }|d         jK        K                                D ]#\  }}|jL        .                                }|||<   $|jM        jN        }d|jM        _N        	 |                    |||          ||jM        _N        S # ||jM        _N        w xY wt          |tF          jO                  rK|t          jQ                    v r6t!          j        |||          }|                    || j        g|z   |          S t)          dd|  d| d| d| dd g	           d S )!Nr   )AttributeMutationNewrk   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.rO   r0   __new__r1   builtins__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.)rP   rQ   rR   rS   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.T)
deleted_ok   FzFAttempted 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.)Rri   objectr<   LambdaVariabler   torchnnModuler>   side_effectsrv   
isinstancer	   r+   mutation_typer   r%   buildr   call_functionr   r   	DIFFICULTrV   hasattrr0   UserDefinedClassVariableis_supported_new_method__self__r1   rF   membercall_methodstaticmethodr{   typesFunctionTypeclassmethodr   
value_typer   
MethodTypeUserMethodVariabler*   method_setattr_standard__delattr__rT   NotImplementedErroroutputis_attribute_mutation
DYNAMO_BUG
store_attrDeletedVariabler&   UserDefinedDictVariable_dict_methods_dict_vtUserDefinedSetVariable_set_methods_set_vtUserDefinedTupleVariabler!   	_tuple_vtUserDefinedListVariabler   _list_vt__getattribute__rY   has_pending_mutation_of_attr	load_attrr   AttributeErrorr   _C_disabled_torch_function_implitemsvtsymbolic_torch_function_statetorch_function_subclass_enabledMethodDescriptorTyper   get_tensor_method)r?   rL   rb   rr   r@   inner_fnrV   r>   rv   fn_vtuser_clsuser_cls_vtuser_cls_sourcecls_variable
cls_sourceattrexc	attr_nameresult
attr_valueattr_sourcefunc	tf_kwargstf_argshash_key_vtvalue_vtkey_strtx_oldfn_vars                                r5   r   zSuperVariable.call_method   s
     <<T4HH& v&&!(+++111[F;;;;;; 69#FGGv35IJJ  "(
 :;; Q Q+14V  E !..rDK=43GPP	Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q AH4HH$HHHHHH!1 !1 !1 !1 9-78     Kb	J*--b	J !Y..2JJ8TT /  (Hx.. 83F*3T3T'7AA"(-'@_   **2y$GGG,// S	JJu15
 5
 S	J $)"h.?OOOE&&r4888+.. N	J:u14
 4
 N	J $+y'IJJ   ${ "
;% @!+DK,>!?!?J.4.
    $)H%j.L.L  E &&rL+@4+@&III%"455 t	J#)"hvFFFE&&rDK=4+?HHH%"233 q	J/!4;v  mBf--. !** m	JzK20
 0
 m	J 74;6rKDKKKFKKK+++7D..00& 
 
 
U8488$88!L ` !	 	 	 	 	 	 	 	 	
 9)??LL d8488$88!(#{!( !( !(
 W*5    I"--T9#<#>#>   -d333t{I$EFFH	JDK555;'33BdFKKKt{I$DEEC	JDK444;&222tT6JJJt{I$FGG>	JM));(44RtVLLLt{I$EFF9	JL((;'33BdFKKK000 QIy%BBY   /99Kt :   fi&?@@ A,^R@@@=#44T[5F	RR

! = = =(<<<<<= K{!-/0BINN"(ZEEE???
 7DI1gmG)-a)<)<)>)> . .%X%.;;==%-	'""5UFOTB,L))"gyAA  0PP 0P     x!;<<	JK9;;;; %*2x@@F''T[MD,@&III'0400$00,, ,37, , , `
	
 
	
 
	
 
	
 
	
 
	
sH   "<C++C/2C/P4 4
Q#>QQ#%[6 6\\` `rE   r8   NrB   r,   rL   r-   rL   r-   rr   rs   r@   rt   r8   r%   )r0   r1   r2   r%   _nonvar_fieldsr<   rK   ri   strrq   r   __classcell__rA   s   @r5   r7   r7   T   s       		'N
 
 
 
 
 
 
B B B B B B?
 ?
 ?
 ?
 ?
 ?
BG5 GS GEV G G G G W
#W
 &	W

 -W
 
W
 W
 W
 W
 W
 W
 W
 W
r4   r7   c                   n     e Zd Z	 d	 d fdZddZddZd	 Zd
ddedefdZ fdZ	 fdZ
d ZeZ xZS )ExceptionVariableNr8   c                 V   t                                          ||           || _        || _        |r$t	          d|  d| dg t
          j                   t          d           | _        t          d           | _	        t          d          | _
        t          d           | _        d S )N)rV   r   z,Keyword args passed to exception constructorz with kwargs zRDynamo does not know how to handle keyword args passed to an exception constructorrO   F)r;   r<   exc_typerr   r   r   SUPPORTABLEr&   __context__	__cause____suppress_context____traceback__)r?   r   rr   init_kwargsrV   r   rA   s         r5   r<   zExceptionVariable.__init__  s     	mDDD 	 	F;;k;;p6)56	    ,D11 *$//$4U$;$;! .d33r4   rQ   c                     || _         d S rE   )r   )r?   rQ   s     r5   set_contextzExceptionVariable.set_context  s    "r4   rB   r,   c                 "                          fd                                j                                       t	           j                  d           dt
          dd f fd} |d            |d            |d           d S )	Nc                  D                          dj        j                  S )Nrz   )load_import_fromr   r0   rB   r?   s   r5   rG   z/ExceptionVariable.reconstruct.<locals>.<lambda>  s    G,,Z9OPP r4   Frb   r8   c                 4   t          |           }t          |t                    r|j        dv s
J |            d S                                   |                                                   d                                         |            d S )N)TFNr   )r]   r   r&   rY   dup_toprI   rot_nr   )rb   r   rB   r?   s     r5   codegen_attrz3ExceptionVariable.reconstruct.<locals>.codegen_attr  s    4&&Dd,-- )z%8888$88888!!!%%gmmA&6&6777""4(((((r4   r   r   r   )rH   foreachrr   r   r_   r   )r?   rB   r   s   `` r5   rK   zExceptionVariable.reconstruct  s    PPPPP	
 	
 	
 		"""c$)nne444	)s 	)t 	) 	) 	) 	) 	) 	) 	) 	]###[!!!+,,,,,r4   c                     | j         S rE   )r   r?   s    r5   rU   zExceptionVariable.python_type  s
    }r4   rL   r-   name_varvalc                    fd}|                                 }|dk    r|                     |           n|dk    r}|                                s<t          |t          j        t          j        t          j        t          j        f          r!|| _	        t	          j
        d          | _        n |d           n|dk    r*|                    dd          r|| _        nr |d           nf|dk    r7|                                r|| _        nDt          d	d
|  d| ddg           n)t          dd
|  d| d| dg t          j                   t	          j
        d           S )Nc                 R    t          t          t          |           g           d S )N)rr   )r   	TypeErrorr&   )msgrL   s    r5   raise_errorz3ExceptionVariable.call_setattr.<locals>.raise_error  s*    $Y:J3:O:O9PQQQQQQr4   r   r   Tz9exception cause must be None or derive from BaseExceptionr   Fr   z<Set Exception object `__traceback__` attribute to not-`None`zcall_setattr rx   zwDynamo does not support setting the attribute '__traceback__' on tracked exception objects to anything other than None.zYAvoid setting '__traceback__' on exception objects within traced code, or set it to None.rO   z4Unsupported attribute assignment on Exception objectz/Dynamo does not support setting the attribute 'z{' on tracked exception objects. Only `__context__`, `__cause__`, `__suppress_context__`, and `__traceback__` are supported.)rT   r   is_constant_noner   r	   rF   r   !UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariabler   r&   r   is_constant_matchr   r   r   r   )r?   rL   r   r   r   rb   s    `    r5   call_setattrzExceptionVariable.call_setattr  s   	R 	R 	R 	R 	R **,,=  S!!!![  ##%% Y-/?@	* * Y "%,5,Ft,L,L))WXXXX+++$$T511 Y,/))WXXXX_$$##%% %(""Z9D99499!'A
 
 
 
 
 N555t55ZZ Z Z 7)56    )$///r4   c                     |dk    r | j         |g|R  S |dk    r*|\  }|                      |t          d          |           | S t                                          ||||          S )N__setattr__with_tracebackr   )r   r&   r;   r   )r?   rL   rb   rr   r@   tbrA   s         r5   r   zExceptionVariable.call_method	  s    =  $4$R/$////%%%DRb"2?"C"CRHHHK77&&r4v>>>r4   c                    |dk    r| j         S |dk    r| j        S |dk    r| j        S |dk    rt          j        d           S |dk    r t          j        | j        | j                  S t                      	                    ||          S )Nr   r   r   r   rr   rk   )
r   r   r   r	   r&   ListVariablerr   rV   r;   rq   )r?   rL   rb   rA   s      r5   rq   zExceptionVariable.var_getattr  s    =  ##[  >!+++,,_$$-d333V^^)$)DKHHHHww""2t,,,r4   c                 0    | j         j         d| j         dS )N())rA   r0   r   r   s    r5   __str__zExceptionVariable.__str__   s     .)<<DM<<<<r4   )NNNr   )rQ   r   r   )r0   r1   r2   r<   r   rK   rU   r%   r   r   rq   r   __repr__r   r   s   @r5   r   r     s         LP4	4 4 4 4 4 44# # # #- - - -*  70#70 "70 	70 70 70 70r? ? ? ? ?- - - - -= = = HHHHHr4   r   c                       e Zd ZdZdS )UnknownVariablez
    It could be anything!
    Nr0   r1   r2   __doc__r3   r4   r5   r  r  &  s           r4   r  c                   <     e Zd ZdZd fd	Z	 	 	 	 	 	 	 	 ddZ xZS )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nc                 H     t                      j        di | || _        d S r:   )r;   r<   r   )r?   r   r@   rA   s      r5   r<   z DelayGraphBreakVariable.__init__1  s+    ""6"""r4   rL   r-   rr   rs   r@   rt   r8   r%   c                 f    t          dd| j         d| j        j         d| j         g            d S )Nz#Unsupported function call (delayed)zsource: z@Dynamo determined that a graph break should occur when calling `z`. Reason: rO   )r   rV   rb   r   r?   rL   rr   r@   s       r5   r   z%DelayGraphBreakVariable.call_function5  sc     	9,t{,,E![-E E:>(E E	
 	
 	
 	
 	
 	
r4   rE   r   r0   r1   r2   r  r<   r   r   r   s   @r5   r  r  ,  s              
#
 &
 -	

 

 
 
 
 
 
 
 
r4   r  c                   F    e Zd ZdZddZdddedd	fd
Z	 	 	 	 	 	 	 	 ddZdS )ComptimeVariableza
    This variable is special, it lets you execute arbitrary code at
    Dynamo compile time
    rB   r,   c                      t          d          )Nzcomptime is special form)r   r?   rB   s     r5   rK   zComptimeVariable.reconstructJ  s    !"<===r4   rL   r-   rb   r8   r%   c                 ~    ddl m } t          j        |t          ||          t	          | j        |                    S )Nr   )comptimerk   )r  r%   r   r]   r   rV   )r?   rL   rb   r  s       r5   rq   zComptimeVariable.var_getattrM  sM    '''''' $$''
4;0M0M
 
 
 	
r4   rr   rs   r@   rt   c           
         ddl m} |st          |          dk    r2t          |ddt          |           dt          |           d           |d         }t	          |t
                    r' |                                 ||                     nt	          |t                    r|                                }|j	        rt          |d|j                    t          j        ||j        |j                                        |j        rt%          |j        j                  nd d	          } | ||                     nt)          d
t+          |                     t,          j                            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: r3   z"unsupported argument to comptime: )r  r  r_   r    r   r(   get_functionr'   get_codeclosurer$   co_freevarsr   r   	f_globalsfn_namerT   defaultstupler   RuntimeErrorrX   r	   r&   rp   )r?   rL   rr   r@   r  fncoder   s           r5   r   zComptimeVariable.call_functionU  s    	/.....  	SYY]]-t99<<F<<<	   !Wb.// 	PBOOoob112222677 	P;;==Dz $xfjfvxx   %
--//,.KAbk'(((T  D D$$%%%%NDHHNNOOO)00666r4   Nr   r   )r0   r1   r2   r  rK   r   rq   r   r3   r4   r5   r  r  D  s         
> > > >
5 
S 
EV 
 
 
 
,7#,7 &,7 -	,7
 
,7 ,7 ,7 ,7 ,7 ,7r4   r  c                   j     e Zd ZU ee         ed<   dZee         ed<   	 ddee         ddf fdZ xZ	S )CellVariablepre_existing_contentsN
local_namer8   c                 H     t                      j        di | || _        d S r:   )r;   r<   r   )r?   r   r@   rA   s      r5   r<   zCellVariable.__init__  s0     	""6"""%:"""r4   rE   )
r0   r1   r2   r   r%   __annotations__r!  r   r<   r   r   s   @r5   r  r    s          $O4444 !%J$$$ BF; ;%-o%>;	; ; ; ; ; ; ; ; ; ;r4   r  c                         e Zd Zd fdZ xZS )NewGlobalVariabler8   Nc                 :     t                      j        di | d S r:   r;   r<   r?   r@   rA   s     r5   r<   zNewGlobalVariable.__init__  &    ""6"""""r4   r   )r0   r1   r2   r<   r   r   s   @r5   r%  r%    s=        # # # # # # # # # #r4   r%  c                 *      fd}t           |_        |S )Nc                       j         | i |S rE   )apply)rr   r@   fn_clss     r5   trampoline_autograd_applyzDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply  s    v|T,V,,,r4   )!produce_trampoline_autograd_apply_origin)r-  r.  s   ` r5   r/  r/    s.    - - - - - )J%$$r4   c                   d     e Zd ZdZdhej        Zd fdZddZdd	Zdd
Z		 	 	 	 	 	 ddZ
 xZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassr-  r8   Nc                 H     t                      j        di | || _        d S r:   )r;   r<   r-  )r?   r-  r@   rA   s      r5   r<   z!AutogradFunctionVariable.__init__  s+    ""6"""r4   rL   r-   c           	         dfd}t          j        |||f           r0t          j                    rt          j        du rt          j        d           ddlm	} ddl
m} | j        j        } || j        j                  }|r  || j        j        | j        j                  }| 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} | j        }|7t3                              | j        j                  | j        j                  } ||| j        j        |t3          |d                                        ||          }| j        rt3          | j        d          }t?          |                     tB          j"                             |rAt3          | j        d          }t?          |                     tB          j"                             |S | j        rt3          | j        d          }nd }| j        j        }tF          $                    ||          }|g|}tK          |tL          j'                  rttQ          j)        |          }tU          |          dz
  tU          |j+                  k    r
|dd          }t          j,        ||          }|                    ||          S tK          |tL          j-                  rHt]          j/        |j0        t]          j1        | j                  |                              ||          S t%          dd|  d	| d	| dte          |          j         ddg           d S )NFc                     |                                  r| j        durdt          | t          j                  r|                               rdd S d S d S )NFT)	is_tensorrequires_gradr   r	   NNModuleVariableis_training)r   r7  rL   s    r5   visitz2AutogradFunctionVariable.call_apply.<locals>.visit  sl    ||~~ )#500$(M"i899 )>>"%% )$(MMM) )) )r4   zNThe config.capture_autograd_function flag is deprecated, it's now always true.r   )#autograd_function_forward_rewritten)_is_setup_context_definedzUnsupported custom vjpzcall_apply rx   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.rO   zUnsupported custom jvpzgDynamo does not support tracing `torch.autograd.Function` subclasses that define a custom `jvp` method.z+Remove the custom `jvp` method if possible.r"   )AutogradFunctionApplyVariabler,  )r   rk   forwardsetup_contextz=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:  r   is_grad_enabledr   capture_autograd_functionwarningswarn"torch._functorch.autograd_functionr;  torch.autograd.functionr<  r-  r>  r?  vjpautogradFunctionr   r   r   jvphigher_order_opsr=  rV   r   import_sourcer1   r0   backwardr   r   rn   r   CLOSURE_MATCHAutogradFunctionContextVariablerp   r   r   r   inspect	signaturer_   _parametersr   r   r	   r   r{   r   rX   )r?   rL   rr   r@   r:  r;  r<  
forward_fnis_setup_ctx_definedvjp_fnjvp_fnr=  rV   r   fwd_srcsetup_ctx_srcr  ctxsigr   r7  s    `                  @r5   
call_applyz#AutogradFunctionVariable.call_apply  s   	) 	) 	) 	) 	) 	) 	edF^444 L	U244 L	/588d        JIIIII,J#<#<T[=V#W#W #  A@K')B 
 [_FU^48884@$@@@@@@!- FH +6    [_FU^48884@$@@@@@@!- F*6
 
 
 
 HGGGGG[F~#$$T[%;<<dk>R  0/$!&999	  
 mBf--  { X$T[)<<g001KLLMMM' X$.t{O$L$LM!-":":<;U"V"VWWWJ; 	Y77FFF[ -44RvFF|d|b%,-- 	#B''C4yy1}CO 4 444ABBx#)"b@@@E&&r4888E,-- 	/24;??   mBf--	. W<d<<T<<F<<Q37883DQ Q Q3
 
 
 
 
 
r4   c                 <   | j         j        }t          |d         j                  t          j        j        j        u sJ t          |t          j
                  sJ t          | j        d          }t          j        |||          }|                    |||          S )Nr   rL  rk   )r-  rL  rX   rY   r   _dynamoexternal_utilsFakeBackwardCFunctionr   r   r   r   rV   r%   r   r   )r?   rL   rr   r@   r  	fn_sourcer   s          r5   call_backwardz&AutogradFunctionVariable.call_backward.  s    [!DGM""em&B&XXXXX"e011111t{J77	%b"Y???""2tV444r4   c                 *    t          | j                  S rE   )r2  r-  r  s       r5   r   z&AutogradFunctionVariable.call_function7  s    '444r4   rr   rs   r@   rt   c           
         ddl m} |dk    rqt          j        | j                  rAt          | j                  } || |j        j        d|gt          ||          R            S | 	                    |||          S |dk    r| 
                    |||          S | j        t          | j        |          nd }	 t          j        | j        |          }n# t          $ r d }Y nw xY wt!          |t"                    r|                    | j                  }	|>t          j        |	                              |	|                              |||          S  t          j        |	          |	                              |||          S t!          |t,                    r1t/          j        |j        | |                              |||          S t5          dd	|  d
| d| ddg           d S )Nr"   )wrap_fx_proxyr,  r   )rL   proxyrL  rk   z$Unsupported autograd.Function methodrw   rx   ,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.rO   )builderrc  r   is_callable_allowedr-  r/  r   create_proxyr   rZ  r`  rV   r   rO  getattr_staticr   r   r   __get__lookupcreate_with_sourcer   r   r	   r   r{   r   )
r?   rL   rb   rr   r@   rc  r.  rV   objr   s
             r5   r   z$AutogradFunctionVariable.call_method:  so    	+*****7??.t{;; 9,MK- -) %}0")0'1 +488      r4888Z%%b$7776:k6MZT222SWF,T[$??!    #|,, {{4;//%#*400++D+@@&r488 4;-d33D99GGD&   C--  3L$v  -D&112 B8488$88!9!9 !9 !9
U     s   :C C$#C$r   r   )rL   r-   rr   rs   r@   rt   )r0   r1   r2   r  r%   r   r<   rZ  r`  r   r   r   r   s   @r5   r2  r2    s        77 			'N
     { { { {z5 5 5 55 5 5 5>#> &	>
 -> > > > > > > >r4   r2  c                   H    e Zd ZU  ej        e          Zee         ed<   dS )SavedTensorBox)default_factorytensorsN)	r0   r1   r2   dataclassesfieldlistrq  r%   r#  r3   r4   r5   ro  ro  {  s7         %6[%6t%L%L%LGT/"LLLLLr4   ro  c                        e Zd ZdZdddhej        Z	 	 	 	 	 d	 d fdZeddd            Zd Z		 	 	 	 	 	 	 	 d fdZ
d fdZ xZS )rN  zZ
    Tracks an autograd.Function() context using mutation tracking in side_effects.py
    rd  	inferencesaved_tensorsNFr8   c                 x     t                      j        d||d| || _        || _        || _        || _        d S N)rY   r   r3   )r;   r<   rv  rw  needs_input_gradnon_differentiable)	r?   rY   r   rv  rw  rz  r{  r@   rA   s	           r5   r<   z(AutogradFunctionContextVariable.__init__  sN     	FuFFvFFF"* 0"4r4   rL   r-   c           
          d }|r|st          d |D                       }| j        j                            d t          j        j        j        t          j	        t          dt                      |          i           }|S )Nc              3   L   K   | ]}|                                 o|j        V   d S rE   )r6  r7  .0xs     r5   	<genexpr>z9AutogradFunctionContextVariable.create.<locals>.<genexpr>  s1      $U$U1Q[[]]%Fq$U$U$U$U$U$Ur4   T)rv  rw  rz  )r  r   r   track_object_newr   rG  functionFunctionCtx	functoolspartialrN  ro  )rL   rr   r@   rz  outs        r5   rp   z&AutogradFunctionContextVariable.create  s     	V 	V$$U$UPT$U$U$UUUi$55N#//,..!1	   

 

 
r4   c                 b    | 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.rO   )rd  r   r   r   r   s    r5   as_proxyz(AutogradFunctionContextVariable.as_proxy  sJ    :'*D**25)45    zr4   rr   rs   r@   rt   r%   c                    |dk    r$t                                          ||||          S |dk    r^|r"t          ||dt          |           d           t	          |i           d         | _        t          j                            d           S |dk    r)t          dd|  d	| d
| dg t          j                   | j        t          dd|  d	| ddg           | j        s9|s| j        st          |d           |j        j                            | |           t          | j        j                  dk    rg | j        _        |D ]!}| j        j                            |           "t          j                            d           S )Nr   mark_non_differentiablez0 kwargsr  r   save_for_backwardz,Unsupported autograd.Function context methodrw   rx   re  z` on `autograd.Function` context objects. Supported methods are `__setattr__`, `save_for_backward` and `mark_non_differentiable`.rO   z9Unsupported autograd.Function context `save_for_backward`zjDynamo requires the `saved_tensors` attribute to be initialized on the `autograd.Function` context object.zEnsure that the `saved_tensors` attribute is properly initialized before calling `save_for_backward`. `save_for_backward` only supported on a newly constructed `torch.autograd.function.FunctionCtx`.z>save_for_backward() requires a source and no keyword arguments)r;   r   r    r_   r   r{  r	   r&   rp   r   r   r   rw  rv  rV   r$   r   r   track_save_for_backwardrq  append)r?   rL   rb   rr   r@   argrA   s         r5   r   z+AutogradFunctionContextVariable.call_method  s    =  77&&r4v>>>... S#BjS[[:Q:Q:QRRR&7b&A&A!&DD#-44T:::&&&F4t44d44-- - - 7)56    %S4t44d44Ow
 
 
 
 ~ 	G T[ $X   I"::4FFF t!)**Q..)+D& 	3 	3C&--c2222)00666r4   c                     dv rt           fd          S dk    r2 j        +t          j        t	           j        j                            S dk    rg j        $t          j                             j                  S  j	        r5t           j	        d          }t          j         j        j        |          S t                                                    S )N)r  r  c                  4                         | |          S rE   )r   )rr   r@   rb   r?   rL   s     r5   rG   z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>  s    (8(8T4(P(P r4   rw  rz  )r   rw  r	   TupleVariablert  rq  rz  r&   rp   rV   r   r%   r   rY   r;   rq   r?   rL   rb   rV   rA   s   ``` r5   rq   z+AutogradFunctionContextVariable.var_getattr  s    CCC!PPPPPP   ?""t'9'E*40B0J+K+KLLL%%%$0 1889NOOO{ V#DK1CDD&,R1LfUUUww""2t,,,r4   )NFNNNr   NNr   r   )r0   r1   r2   r  r+   r   r<   r   rp   r  r   rq   r   r   s   @r5   rN  rN    s        
 	 
#	1	N 5 
5 5 5 5 5 5      \"	 	 	27#27 &	27
 -27 
27 27 27 27 27 27h- - - - - - - - - -r4   rN  c                   B     e Zd ZdZ	 d	 d fdZ	 	 	 	 	 	 	 	 ddZ xZS )AutogradEngineVariablez;
    Represents a torch._C._ImperativeEngine instance.
    Nr8   c                 @     t                      j        d||d| d S ry  r'  )r?   rY   r   r@   rA   s       r5   r<   zAutogradEngineVariable.__init__  s/     	FuFFvFFFFFr4   rL   r-   rr   rs   r@   rt   r%   c                    |dk    rt           j        j        j        r}|j        s|j        s
J d            t          j        |t           j        j        j	        j
                  }|                    ||j        j                                        g|R |          S t          dd|  d| dg            d S t          dd|  d| d	| d
g            d S )Nqueue_callbackzXqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=Truez7Unsupported torch._C._ImperativeEngine.queue_callback()rw   rx   zYqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=True.rO   z-Unsupported torch._C._ImperativeEngine methodzgDynamo only supports the `queue_callback` method on a torch._C._ImperativeEngine instance, but found: `z`.)r   r\  compiled_autogradin_compiled_autograd_region	one_grapherror_on_graph_breakr%   r   r]  FakeCompiledAutogradEnginer  r   r   r   get_ca_final_callbacks_varr   )r?   rL   rb   rr   r@   r   s         r5   r   z"AutogradEngineVariable.call_method  sH    ###}.J | r'>  n   (-M0KZ  **Y+FFHHP4PP   U8488$88!H      G4t44d44RIMR R R     r4   rE   r   r   r0   r1   r2   r  r<   r   r   r   s   @r5   r  r    s          G
 
G G G G G G%#% &	%
 -% 
% % % % % % % %r4   r  c                   8     e Zd Zd fdZ	 	 	 	 	 	 	 	 ddZ xZS )r   r8   Nc                 H     t                      j        di | || _        d S r:   )r;   r<   r  )r?   r  r@   rA   s      r5   r<   zLambdaVariable.__init__4  s+    ""6"""r4   rL   r-   rr   rs   r@   rt   r%   c                      | j         |i |S rE   )r  r  s       r5   r   zLambdaVariable.call_function8  s     tw''''r4   r   r   )r0   r1   r2   r<   r   r   r   s   @r5   r   r   3  sw             (#( &( -	(
 
( ( ( ( ( ( ( (r4   r   c                        e Zd Zddhej        Zdd fdZ fdZdefdZe	de
j        j        fd	            Zd
 Zd ZddZddZ	 	 	 	 	 	 	 	 ddZdddee         deeef         def fdZd Z xZS )rm   rb   py_typeNr8   c                      t                      j        di | t          |t                    sJ t          |t                    sJ || _        || _        || _        d S r:   )r;   r<   r   r%   r   rm  rb   r  )r?   rm  rb   r  r@   rA   s        r5   r<   zGetAttrVariable.__init__H  se    ""6"""#/////$$$$$$	r4   c                 `    | j         | j         S t                                                      S rE   )r  r;   rU   r?   rA   s    r5   rU   zGetAttrVariable.python_typeP  s)    <#<77&&(((r4   c                 @    | j         j         d| j         d| j         dS Nr   z, r   )rA   r0   rm  rb   r   s    r5   r   zGetAttrVariable.__repr__V  s*    .)DDDHDD	DDDDr4   
base_proxyc                 "    t          | |          S rE   )r]   )r  r   s     r5   create_getattr_proxyz$GetAttrVariable.create_getattr_proxyY  s    z4(((r4   c                 p    t                               | j                                        | j                  S rE   )rm   r  rm  r  rb   r   s    r5   r  zGetAttrVariable.as_proxy]  s(    33DH4E4E4G4GSSSr4   c                     | j                                         }	 t          || j                  S # t          $ r t          |  d          d w xY w)Nz is not a constant)rm  rT   r]   rb   r   r   )r?   constants     r5   rT   z"GetAttrVariable.as_python_constant`  se    8..00	M8TY/// 	M 	M 	M%&A&A&ABBL	Ms	   0 ArL   r-   c                 >   t          | j        t          j                  st          |j                            | j        j                  }| j        |j	        vrt          t          j        || j                  }||j	        vrt          t          j        ||          S rE   )r   rm  r	   r8  r   r   get_submodule
module_keyrb   r`   rO  ri  )r?   rL   rb   step1step2s        r5   const_getattrzGetAttrVariable.const_getattrg  s    $(I$>?? 	&%%	''(;<<9EN**%%&udi88u~%%%%%eT222r4   rB   r,   c                      || j                    |                    |                    | j                             d S rE   )rm  rI   create_load_attrsrb   r  s     r5   rK   zGetAttrVariable.reconstructr  s>    g77	BBCCCCCr4   rr   rs   r@   rt   r%   c                 F    | j                             || j        ||          S rE   )rm  r   rb   r  s       r5   r   zGetAttrVariable.call_functionv  s"     x##B	4@@@r4   c                    |dv r| j         dk    r|s|d                                         rt          | j        t          j        t          j        t          j        f          r| j        }|d                                         }|	                    ||          r|
                    ||          S |dk    r/t          |          dk    r|d         S t	          j        d           S nj|dk    r| j         dk    rt          |          dk    r|d                                         r|st          | j        t          j        t          j        t          j        f          r_| j        }|d                                         }|	                    ||          rt	          j        d          S t	          j        d	          S |d
k    r| j         dk    r|st          | j        t          j                  r*| j                            ||d         |d         d          S t          | j        t          j                  r| j                            |           t                                          ||||          S )N)__getitem__ra   r`   r   ra   r   r"   __contains__TF__setitem__)directly_update_dict)rb   is_python_constantr   rm  r	   r+   r8  r   rT   has_key_in_generic_dictrq   r_   r&   r   convert_to_unspecializedr;   r   )r?   rL   rb   rr   r@   rm  keyrA   s          r5   r   zGetAttrVariable.call_method~  sr    ***	Z'' (Q**,, ( 7.6  ( (Cq',,..C**2s33 0r3/// u}}t99>>7N$5d;;;	  N""	Z''D		QQ**,,   7.6   (Cq',,..C**2s33 9 1$777 1%888]""tyJ'>'>v'>$(I$GHH x77Qat 8    $(I$>?? 611"555ww""2tT6:::r4   c                    | j         dk    rCt          | j        t          j                  r$|j        j                            | j                  rJ d| j        _        t          j
        || j        j        j        | j                  S )Nr`   T)rb   r   rm  r	   r   r   r   has_pending_mutationban_mutationr%   r   rY   r`   rV   )r?   rL   s     r5   get_forwarded_dictz"GetAttrVariable.get_forwarded_dict  ss    I##48Y%GHH $I*??II $## !%$R)@$+NNNr4   rE   r   r   r   r   )r0   r1   r2   r%   r   r<   rU   r   r   r   r   fxProxyr  r  rT   r  rK   r   rt  dictr   r  r   r   s   @r5   rm   rm   A  s        
	'N      ) ) ) ) )E# E E E E ) ) ) ) \)T T TM M M	3 	3 	3 	3D D D DA#A &A -	A
 
A A A AB;#B; ?#	B;
 S/)*B; 
B; B; B; B; B; B;HO O O O O O Or4   rm   c                   Z     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Zd Zd Zd Zd Z	 xZ
S )MethodWrapperVariabler8   Nc                 V     t                      j        di | || _        i | _        d S r:   )r;   r<   method_wrapper_builtin_fns)r?   r  r@   rA   s      r5   r<   zMethodWrapperVariable.__init__  s6    ""6""",r4   rL   r-   rr   rs   r@   rt   r%   c           	         t          | j                  rt          |d         t          j                  rat          |          dk    rt          |          dk    st          |d           |d                             || j        j        j	                  S | j        j        }| j        j	        }|dk    rUt          |          j        }|t          j        u r2t          j        t                                        |||g||          S nt          j        dk    r|t          j        d         u r|dk    rddlm} t          |          dk    rt|sr	 |                    ||                     |d                                                             S # t,          $ r t/          t,          |           Y nt0          $ r Y nw xY wt3          d	d
|  d| d| dddgt4          j                   t9                                          |||          S )Nr   r"   z2tensor attribute getter takes exactly one argumentr<   )r~      r#  rj  )SourcelessBuilderz9unsupported type.__dict__['__annotations__'].__get__ callzcall_function , args: 
, kwargs: z{`torch.compile` only supports calling type.__dict__['__annotations__'].__get__ on a single constant argument (i.e. a type).zpMake sure your call to type.__dict__['__annotations__'] only has one positional argument (no keyword arguments).zMake sure the argument to type.__dict__['__annotations__'] is a constant (i.e. type). For example, `object`, `int`, `MyCustomClass`.rO   )r   r  r   r	   TensorVariabler_   r$   rq   r   r0   rX   r<   r   rF   r   sysversion_infor`   rf  r  rp   rT   r   r   r#   r   r   r   r;   r   )	r?   rL   rr   r@   self_objwrapper_namefn_objr  rA   s	           r5   r   z#MethodWrapperVariable.call_function  sj    &d&9:: 	RzGY-@
 @
 	R IINNs6{{a'7'7$L   7&&r4+>+G+PQQQ &/*3 :%%(^^,F(( 088DDx&7$&7   )
 '' DM*;<<<	))2222224yyA~~f~,33D//Q0J0J0L0LMM   & A A A,^R@@@@@:   D SOOOtOOvOO?FR
 '2    ww$$Rv666s   A F F>2	F>=F>c                     dS NTr3   r   s    r5   r  z(MethodWrapperVariable.is_python_constant      tr4   c                     | j         S rE   )r  r   s    r5   rT   z(MethodWrapperVariable.as_python_constant  s    ""r4   c                     dS r  r3   r   s    r5   is_python_hashablez(MethodWrapperVariable.is_python_hashable  r  r4   c                 D    t          |                                           S rE   hashrT   r   s    r5   get_python_hashz%MethodWrapperVariable.get_python_hash      D++--...r4   c                 V    |                                  |                                 k    S rE   rT   r?   others     r5   is_python_equalz%MethodWrapperVariable.is_python_equal   %    &&((E,D,D,F,FFFr4   r   r   )r0   r1   r2   r<   r   r  rT   r  r  r  r   r   s   @r5   r  r    s             
@7#@7 &@7 -	@7
 
@7 @7 @7 @7 @7 @7D  # # #  / / /G G G G G G Gr4   r  c                   8     e Zd Zd	 fdZd
 fdZd Zd Z xZS )GetSetDescriptorVariabler8   Nc                 H     t                      j        di | || _        d S r:   )r;   r<   desc)r?   r  r@   rA   s      r5   r<   z!GetSetDescriptorVariable.__init__%  s+    ""6"""			r4   rL   r-   c                     |dk    r<| j         r5t          | j         d          }t          j        || j        j        |          S t                                          ||          S )Nrj  )rV   r   r%   r   r  rj  r;   rq   r  s       r5   rq   z$GetSetDescriptorVariable.var_getattr)  sZ    9Y77F"(TY->GGG77&&r4000r4   c                     dS r  r3   r   s    r5   r  z+GetSetDescriptorVariable.is_python_constant0  r  r4   c                     | j         S rE   )r  r   s    r5   rT   z+GetSetDescriptorVariable.as_python_constant3  s
    yr4   r   r   )r0   r1   r2   r<   rq   r  rT   r   r   s   @r5   r  r  $  sy             1 1 1 1 1 1        r4   r  c                   p     e Zd Zddhej        Zdej        ddf fdZd Zd Z	de
fdZddZddZ xZS )PythonModuleVariablerY   is_torchr8   Nc                      t                      j        di | || _        | j        t          u p| j        j                            d          | _        d S )Nztorch.r3   )r;   r<   rY   r   r0   
startswithr  r?   rY   r@   rA   s      r5   r<   zPythonModuleVariable.__init__>  sQ    ""6"""

e+Wtz/B/M/Mh/W/Wr4   c                     t           j        S rE   )r   
ModuleTyper   s    r5   rU   z PythonModuleVariable.python_typeC  s    r4   c                     | j         S rE   rY   r   s    r5   rT   z'PythonModuleVariable.as_python_constantF  
    zr4   c                     d| j          dS )NzPythonModuleVariable(r   r  r   s    r5   r   zPythonModuleVariable.__repr__I  s    4tz4444r4   rL   r-   c                 j    t          | j        |          }t          j                            |          S rE   )r   rY   r	   r&   rp   r?   rL   rb   r   s       r5   call_obj_hasattrz%PythonModuleVariable.call_obj_hasattrL  s*    T**)00888r4   c                    |j         j                            | |          r |j         j                            | |          S | j        s|| j        j        vr<	 t          | j        |          }n7# t          $ r t          t          |           Y nw xY w| j        j        |         }| j
        ot          | j
        |          }t          j        |||          S rE   )r   r   r   r   r  rY   r`   r]   r   r   rV   r   r%   r   )r?   rL   rb   r   rV   s        r5   rq   z PythonModuleVariable.var_getattrP  s    9!>>tTJJ 	@9)33D$???= 	3D
(;;;=$TZ66

! = = =(<<<<<= ,T2J>DK!>!>$RV<<<s   A- -BBr   )r0   r1   r2   r%   r   r   r  r<   rU   rT   r   r   r   rq   r   r   s   @r5   r  r  7  s         
	'NXe. XT X X X X X X
       5# 5 5 5 59 9 9 9= = = = = = = =r4   r  c                   l     e Zd Zd fdZ	 	 	 	 	 	 	 	 ddZdddefdZd Zd fdZd Z	d Z
d Z xZS )TypingVariabler8   Nc                 H     t                      j        di | || _        d S r:   r;   r<   rY   r  s      r5   r<   zTypingVariable.__init__a  +    ""6"""


r4   rL   r-   rr   rs   r@   rt   r%   c                 2   |dk    rGt          |          dk    r4| j        |d                                                  }t          |          S t	          dd| j         d| d| d| d	| d
| j         dd| d| j         dgt
          j                   d S )Nr  r"   r   z,unsupported method call on `typing` variableztyping variable: z, method name: r  r  z.`torch.compile` does not support method call `z` on `typing` variable f.zAvoid calling the z method on rO   )r_   rY   rT   r  r   r   r   )r?   rL   rb   rr   r@   
new_typings         r5   r   zTypingVariable.call_methode  s     =  SYY!^^DG$>$>$@$@AJ!*---Bi
ii4iiQUiiagiitttgkgqtttCTCCdjCCC".		
 	
 	
 	
 	
 	
r4   rb   c                 |   ddl m}m} |t          v rt	          j        | |          S |j        j                            | |          r|j        	                    | |          S t          | j        |          }| j        r*t          | j        |          }  |||          |          S |                    ||          S )Nr"   )r  VariableBuilder)rf  r  r  r   r	   rm   r   r   r   r   r]   rY   rV   r   rp   )r?   rL   rb   r  r  rY   r   s          r5   rq   zTypingVariable.var_getattrz  s    ????????))),T48889!>>tTJJ 	9?,,T4888
D)); 	7$T[$77K3??2{33E:::$++B666r4   c                     | j         S rE   r  r   s    r5   rT   z!TypingVariable.as_python_constant  r  r4   rB   r,   c                     t          | j        t          j                  s!t	                                          |          S |                    |                    | j                             d S rE   )r   rY   r   GenericAliasr;   rK   append_outputcreate_load_constrJ   s     r5   rK   zTypingVariable.reconstruct  s^    $*e&899 	077&&w///0 	g77
CCDDDDDr4   c                     dS r  r3   r   s    r5   r  z!TypingVariable.is_python_hashable  r  r4   c                 D    t          |                                           S rE   r  r   s    r5   r  zTypingVariable.get_python_hash  r  r4   c                 V    |                                  |                                 k    S rE   r  r  s     r5   r  zTypingVariable.is_python_equal  r  r4   r   r   )rB   r,   r8   N)r0   r1   r2   r<   r   r   rq   rT   rK   r  r  r  r   r   s   @r5   r  r  `  s            
#
 &	

 -
 

 
 
 
*75 7S 7 7 7 7   E E E E E E8  / / /G G G G G G Gr4   r  )maxsizec                      ddl m}  i }|                                 D ]K\  }}|j                                        D ],\  }}t	          |          rt          ||d          x}r|||<   -L|S )zd
    This generates a mapping from numpy modules to their torch._numpy
    modules equivalents.
    r   )NP_TO_TNP_MODULEN)utilsr  r   r`   callabler]   )r  np_fn_to_tnp_fnnp_modtnp_modr  tnp_fnnp_fns          r5   get_np_to_tnp_mapr    s     )(((((O+1133 4 4&/5577 	4 	4OGV 4 $FGT:::5 4-3OE*	4 r4   c                  Z    t                      } d |                                 D             S )z
    This is just the reverse mapping of get_np_to_tnp_map() - mapping from
    torch._numpy modules to numpy equivalents.
    c                     i | ]\  }}||	S r3   r3   r  kvs      r5   
<dictcomp>z%get_tnp_to_np_map.<locals>.<dictcomp>  s    '''TQAq'''r4   )r  r   )ms    r5   get_tnp_to_np_mapr&    s+     	A''QWWYY''''r4   c                        e Zd ZdZej        fZd fdZed             Z	ed             Z
	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZd Z fdZd Zd Z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.
    r8   Nc                 H     t                      j        di | || _        d S r:   r  r  s      r5   r<   zNumpyVariable.__init__  r  r4   c                     |j                             d          }t          |          dk    r|d d         ddgk    sJ || j        v S Nr  r   r   _numpy)r1   splitr_   constant_fold_functionsclsr  mods      r5   can_constant_fold_throughz'NumpyVariable.can_constant_fold_through  sQ    m!!#&&3xx1}}RaRWh,?!?!?!?!?S000r4   c                     |j                             d          }t          |          dk    r|d d         ddgk    sJ t                              |          S r+  )r1   r-  r_   np_constant_collections_mapra   r/  s      r5    get_constant_collection_for_funcz.NumpyVariable.get_constant_collection_for_func  sW    m!!#&&3xx1}}RaRWh,?!?!?!?!?*..r222r4   rL   r-   rr   rs   r@   rt   r%   c                    t           j        s+t          dd| j         d| d| d| j         ddg           d	d
lm} ddlm} t                      	                    | j                  }|:t          dd| j         d| d| d| d	d| j         dg t          j                   |                     |          x}	 	  | | j        d |D             i d |                                D                       S # t          $ r> t          dd| j         d| d| d| d	d| j         dg t          j                   Y d S w xY w|j        dk    rEt           j        r9t          dd| j         d| d| d| d	d| j         ddd| j         dg           |                    |j        ||          \  }}|                     |          rmt+          ||          r]t,          j                             |                                 d |D             i d |                                D                       S  |j        j        d  ||          gt9          ||          R  }|                    ||          S )!Nz?attempted to trace numpy function with config.trace_numpy=Falsenumpy function: r  r  z"Attempted to trace numpy function z; while `torch._dynamo.config.trace_numpy` was set to False.zHSet `torch._dynamo.config.trace_numpy` to True to trace numpy functions.rO   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.c                 6    g | ]}|                                 S r3   r  r~  s     r5   
<listcomp>z/NumpyVariable.call_function.<locals>.<listcomp>  $    ???Q!..00???r4   c                 >    i | ]\  }}||                                 S r3   r  r!  s      r5   r$  z/NumpyVariable.call_function.<locals>.<dictcomp>  *    PPPA1a2244PPPr4   zTnumpy function that produces a const collection type encountered non-const argumentsznumpy function zs that produces a const collection type (e.g. np.dtype, np.iinfo/np.finfo) received arguments that are not constant.ztorch._numpy.randomzXattempted to trace torch._numpy.random function with config.use_numpy_random_stream=TruezAttempted to trace zD when `torch._dynamo.config.use_numpy_random_stream` is set to True.z<Set `torch._dynamo.config.use_numpy_random_stream` to False.zAvoid calling r  c                 6    g | ]}|                                 S r3   r  r~  s     r5   r;  z/NumpyVariable.call_function.<locals>.<listcomp>6  r<  r4   c                 >    i | ]\  }}||                                 S r3   r  r!  s      r5   r$  z/NumpyVariable.call_function.<locals>.<dictcomp>7  r>  r4   r   )r   trace_numpyr   rY   r  r8  tensorr9  r  ra   r   r   r5  r   r#   
USER_ERRORr1   use_numpy_random_stream
patch_argsr0   r2  r   r	   r&   rp   rT   r   rh  r   )	r?   rL   rr   r@   r8  r9  r   collection_variable_typrd  s	            r5   r   zNumpyVariable.call_function  s    ! 		YW4:WWtWWvWWM M M M _    	433333000000 ""&&tz22<R~4:~~t~~v~~w{~~~\tz\\\&2	    (,'L'LT'R'RR#..DJ??$???PPPPP    7 
 
 
r Ctz  C  C4  C  CSY  C  C{  C  C  C!@$* !@ !@ !@*5	 	 	 	 	 	 	
 #8882 9 v Ctz  C  C4  C  CSY  C  C{  C  C  C!&dj !& !& !& W6666	 	 	 	 0::4=$PVWWLD&--d33 	-dF;;	 !188-D++--??$???PPPPP    +BI*''-- #400  E
 (..r5999s   ?;C; ;AEEc           	      `    t          dd| j         d| d| dg t          j                   d S )Nz/attempted to trace numpy.* function as a methodr7  r  r  z6Tracing numpy.* functions as methods is not supported.rO   )r   rY   r   r   )r?   rL   rb   rr   r@   s        r5   r   zNumpyVariable.call_methodC  sX     	EStzSS4SS6SSP",		
 	
 	
 	
 	
 	
r4   c                     | j         S rE   r  r   s    r5   rT   z NumpyVariable.as_python_constantS  r  r4   c                     t           j        rLt          | j        t          j                  r| j        S t          | j        t                    r| j        j        S t                      	                                S rE   )
r   rA  r   rY   enumEnumMetarX   r0   r;   r  r  s    r5   r  zNumpyVariable.as_proxyV  sc     		+$*dm44 "z!$*d++ + z**ww!!!r4   c                     dS r  r3   r   s    r5   r  z NumpyVariable.is_python_hashabled  r  r4   c                 D    t          |                                           S rE   r  r   s    r5   r  zNumpyVariable.get_python_hashg  r  r4   c                 V    |                                  |                                 k    S rE   r  r  s     r5   r  zNumpyVariable.is_python_equalj  r  r4   r   r   )r0   r1   r2   r  tnp
issubdtyper.  r<   r   r2  r5  r   r   rT   r  r  r  r  r   r   s   @r5   r(  r(    sc          #~/      1 1 [1
 3 3 [3
X:#X: &X: -	X:
 
X: X: X: X:t
#
 &	

 -
 

 
 
 
   " " " " "  / / /G G G G G G Gr4   r(  c                   4     e Zd Zd fdZdefdZd	dZ xZS )
NullVariabler8   Nc                 :     t                      j        di | d S r:   r'  r(  s     r5   r<   zNullVariable.__init__p  r)  r4   c                     dS )NrR  r3   r   s    r5   r   zNullVariable.__repr__s  s    ~r4   rB   r,   c                     t           j        dk     rt          dddg t          j                   |                    t          d                     d S )N)r~      z0cannot reconstruct NullVariable in Python < 3.11rN   zdAttempted to generate PUSH_NULL instruction in Python < 3.11; where this instruction does not exist.rO   	PUSH_NULL)r  r  r   r   r   r  r   r  s     r5   rK   zNullVariable.reconstructv  sg    g%%J9&1    	0==>>>>>r4   r   r   )r0   r1   r2   r<   r   r   rK   r   r   s   @r5   rR  rR  o  sl        # # # # # ##    ? ? ? ? ? ? ? ?r4   rR  c                       e Zd ZdZdS )r   z"Marker used to implement delattr()Nr  r3   r4   r5   r   r     s        ,,,,r4   r   c                   b     e Zd ZdZdhej        Zed             Zd 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_stringc           	      l   t          d t          j        ||                                          D                       rPt          j                             |j        d |D             i d |                                D                       S  | |t          |          t          |                    S )Nc              3   >   K   | ]}|                                 V  d S rE   )r  r~  s     r5   r  z.StringFormatVariable.create.<locals>.<genexpr>  sB       
 
   ""
 
 
 
 
 
r4   c                 6    g | ]}|                                 S r3   r  )r  r#  s     r5   r;  z/StringFormatVariable.create.<locals>.<listcomp>  s$    ???a**,,???r4   c                 >    i | ]\  }}||                                 S r3   r  r!  s      r5   r$  z/StringFormatVariable.create.<locals>.<dictcomp>  s*    PPPTQq!..00PPPr4   )all	itertoolschainvaluesr	   r&   rp   formatr   rt  r  )r0  r[  sym_args
sym_kwargss       r5   rp   zStringFormatVariable.create  s     
 
_Xz/@/@/B/BCC
 
 
 
 
 		 -44$$??h???PPZ=M=M=O=OPPP    s=$x..$z2B2BCCCr4   r8   Nc                      t                      j        di | t          |t                    sJ || _        || _        || _        d S r:   )r;   r<   r   r   r[  re  rf  )r?   r[  re  rf  r@   rA   s        r5   r<   zStringFormatVariable.__init__  sP    ""6"""------* $r4   c                 P    | j         j         d| j        d| j        d| j        dS r  )rA   r0   r[  re  rf  r   s    r5   r   zStringFormatVariable.__repr__  s7    .)kkD,>kkDMkkW[Wfkkkkr4   rB   r,   c                 J                          fdd            t          j         j                             d  j                                        D             } t          j        |                                         t          dd                     d S )Nc                                                                 j                                       d          g          S )Nrd  )rI   r  r[  create_load_attrr   s   r5   rG   z2StringFormatVariable.reconstruct.<locals>.<lambda>  sA    G))--d.@AA,,X66  r4   T)call_function_exc                 T    i | ]%\  }}t           j                            |          |&S r3   r	   r&   rp   r!  s      r5   r$  z4StringFormatVariable.reconstruct.<locals>.<dictcomp>  s<     
 
 
8<1I&--a00!
 
 
r4   F)	rH   r	   r  re  rf  r   ConstDictVariablerI   r   )r?   rB   r@   s   `` r5   rK   z StringFormatVariable.reconstruct  s         " 	 	
 	
 	
 		'66777
 
@D@U@U@W@W
 
 
 		+F334445dEBBCCCCCr4   r   r   )r0   r1   r2   r  r%   r   r   rp   r<   r   r   rK   r   r   s   @r5   rZ  rZ    s          &G(FGND D [D% % % % % %l# l l l lD D D D D D D Dr4   rZ  c                   b     e Zd ZdZd fdZed             ZddZddZedd            Z	 xZ
S )DebuggingVariablez
    Represents a call to a debugging function like print(), or something
    registered to config.reorderable_logging_functions.
    r8   Nc                 H     t                      j        di | || _        d S r:   r  r  s      r5   r<   zDebuggingVariable.__init__  r  r4   c                     t          |           o=t          | t          j        t          j        f          o| t
          j        j        j        v S rE   )	r  r   r   r   BuiltinFunctionTyper   r\  r   reorderable_logging_functions)rm  s    r5   is_reorderable_logging_functionz1DebuggingVariable.is_reorderable_logging_function  sF     SMM J3!3U5N OPPJu}+II	
r4   rL   r-   c           	          |j         rd S |                     | j        ||          s+t          dd| j         d| d| dd| j         dg           |j                            | t          |          f           d S )	NzJattempted to reorder a debugging function that can't actually be reorderedzfn: r  r  zl`torch.compile` can only reorder functions where the arguments are Tensors, constants, or string formatters.z#Avoid calling the logging function z" with args that are not supported.rO   )exportcan_reorder_logsrY   r   debug_localsr  rt  r  s       r5   r   zDebuggingVariable.call_function  s    9 	F$$TZv>> 		dKtzKK4KK6KK@ i$*hhh    	d4jj122222r4   rB   r,   c                 6    | j                             |          S rE   )rV   rK   r  s     r5   rK   zDebuggingVariable.reconstruct  s    {&&w///r4   Tc                     t           j        t           j        t          f}t	          j        ||g          }|D ]}t          ||          s dS dS )zm
        Run some additional checks for what sort of function calls can we
        actually reorder.
        FT)r	   r  r&   rZ  pytreetree_leavesr   )r  rr   r@   allowed_input_types	flat_argsr  s         r5   ry  z"DebuggingVariable.can_reorder_logs  sf     $& 
 &f~66	 	 	Cc#677 uu tr4   r   r   r   )r8   T)r0   r1   r2   r  r<   r   rv  r   rK   ry  r   r   s   @r5   rq  rq    s         
      
 
 \
3 3 3 3$0 0 0 0    \    r4   rq  c                   <     e Zd ZdZd fdZ	 	 	 	 	 	 	 	 ddZ xZS )LoggingLoggerVariablez<
    Represents a call to any of logging.Logger methods
    r8   Nc                 H     t                      j        di | || _        d S r:   r  r  s      r5   r<   zLoggingLoggerVariable.__init__  r  r4   rL   r-   rr   rs   r@   rt   r%   c                 D   |j         rd S t          | j        |d           }t          |dd           }||h                    t          j        j        j                  rt          j	        
                    d           S t          dd| j         d| d| d| ddg	           d S )
Nr{   z8logging.Logger method not supported for non-export caseszmethod: r  r  r  z>logging.Logger methods are not supported for non-export cases.zFAdd the logging method to `torch._dynamo.config.ignore_logger_methods.rO   )rx  r]   rY   intersectionr   r\  r   ignore_logger_methodsr	   r&   rp   r   )r?   rL   rb   rr   r@   methodr  s          r5   r   z!LoggingLoggerVariable.call_method  s     9 	FT4006:t44H**5=+?+UVV 	;-44T:::NRtzRRDRR$RR&RRXX		
 	
 	
 	
 	
 	
r4   r   r   r  r   s   @r5   r  r    s              
#
 &	

 -
 

 
 
 
 
 
 
 
r4   r  c                        e Zd ZdZ	 ddlmZmZm	Z
 n%# e$ r  eddi           Z eddi           ZY nw xY wd fdZed	             Zd
 Zdd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 )ConstantLikeVariablez8self.value is a compile-time constant, but not a literalr   )dtypefloatinggenericinvalid_typer3   r8   Nc                 H     t                      j        di | || _        d S r:   r  r  s      r5   r<   zConstantLikeVariable.__init__#  r  r4   c                 p    t          | j                  }|j        dk    r|j        S |j         d|j         S )z4Dynamically compute the prefix from the value's typerz   r  )rX   rY   r1   r2   )r?   ts     r5   _error_prefixz"ConstantLikeVariable._error_prefix'  sB      <:%%>!,11111r4   c                     | j         S rE   r  r   s    r5   rT   z'ConstantLikeVariable.as_python_constant2  r  r4   rL   r-   rr   r@   c                 f   	 d |D             }d |                                 D             }nA# t          $ r4 t          d| j         d| d| d| dd| j         d| d	d
g           Y nw xY w t	          | j        |          |i |}t          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                   d S )Nc                 6    g | ]}|                                 S r3   r  r~  s     r5   r;  z4ConstantLikeVariable.call_method.<locals>.<listcomp>>  s$    :::Q))++:::r4   c                 >    i | ]\  }}||                                 S r3   r  r!  s      r5   r$  z4ConstantLikeVariable.call_method.<locals>.<dictcomp>?  s*    LLLTQq!..00LLLr4   z0constant-like method call with non-constant argsr  z(*z, **r   zAttempted to call z with non-constant args.zFEnsure that the args to the method call are constant (int, str, etc.).rO   z6constant-like method call with unsupported return typez) returned z, got unsupported return value )r   r   r   r  r]   rY   r	   r&   rl   rp   r   reMatchr  r   r   )r?   rL   rb   rr   r@   cargsckwargsr   s           r5   r   z ConstantLikeVariable.call_method5  s   	::T:::ELLV\\^^LLLGG" 	 	 	J-KKKKKK&KKKd1Cdddddd\	     	 +T**E=W==%0088 	=-44V<<<fbh'' 	0'///L)YYDYYDYYfYYQWYYpT-?pp$ppgmppp".		
 	
 	
 	
 	
 	
s   *- ;A+*A+rb   c                    t          | j        |          }t          || j                  rt	          |          }t          || j                  rt          |          S t          |t                    r$t          || j	                  rt          |          S t          j                            |          rt          j                            |          S t          | |          S rE   )r]   rY   r   np_floatingfloatnp_dtypeNumpyDTypeVariablerX   rW   
np_genericr(  r	   r&   rl   rp   rm   r  s       r5   rq   z ConstantLikeVariable.var_getattrZ  s    T**fd.// 	#6]]Ffdm,, 	.%f---fd## 	)
64?(K(K 	) (((%0088 	=-44V<<<tT***r4   r   )r0   r1   r2   r  numpyr  r  r  r  r  r  ImportErrorrX   r<   propertyr  rT   rt  r%   r  r   r   rq   r   r   s   @r5   r  r    sd       BB0	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	

  0 0 0d>2r224B//0      2 2 X2  #
##
 ?#	#

 S/)*#
 
#
 #
 #
 #
J+5 +S +_ + + + + + + + +s   
 66r  c                   $     e Zd ZdZd fdZ xZS )TorchVersionVariableztorch.__version__r8   Nc                     |                     dt          j                   |d         t          j        u sJ  t                      j        di | d S )NrY   r3   )
setdefaultr   __version__r;   r<   r(  s     r5   r<   zTorchVersionVariable.__init__k  sW    '5#4555g%"33333""6"""""r4   r   )r0   r1   r2   r  r<   r   r   s   @r5   r  r  h  sB        'M# # # # # # # # # #r4   r  c                       e Zd Zd ZdS )r  c                 $    | 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).
        )rY   rX   r0   r   s    r5   r  zNumpyDTypeVariable.as_proxyr  s     z''r4   N)r0   r1   r2   r  r3   r4   r5   r  r  q  s#        ( ( ( ( (r4   r  c                   ,     e Zd ZdZd fdZd	dZ xZS )
RandomClassVariablezrandom.Randomr8   Nc                 :     t                      j        di | d S r:   r'  r(  s     r5   r<   zRandomClassVariable.__init__  r)  r4   rL   r-   c                 D   t          |          dk    s|r%t          dd| d| dg t          j                   t          |          dk    rt          j                            d           n|d         }t          |t          j        	                                          S )	Nr"   z'random.Random() with improper argumentszargs: r  z=random.Random() with > 1 arg or with kwargs is not supported.rO   r   )seedr   )
r_   r   r   rC  r	   r&   rp   RandomVariablebaseValueMutationNew)r?   rL   rr   r@   r  s        r5   r   z!RandomClassVariable.call_function  s    t99q==F=A99999[&1	    ;>d))q..y)00666dSTgY^%D%D%F%F
 
 
 	
r4   r   r   r	  r   s   @r5   r  r    sW        # # # # # #
 
 
 
 
 
 
 
r4   r  c                       e Zd ZdZdhej        Zh dZ	 	 ddeej	                 dee         ddf fdZ
d	 Zd
 Zed             Zed             Zed             Zed             Zdd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  r8   c                 Z    t                      j        di | |]|                     |          sJ t          j                    | _        | j                            |                                           d S ||                                nd }t          j        |          | _        d S r:   )r;   r<   is_supported_random_objr  RandomsetstategetstaterT   )r?   r  r  r@   rA   s       r5   r<   zRandomVariable.__init__  s     	""6"""//55555 -//DKK  11111040@4**,,,dD ---DKKKr4   c                     t           j        S rE   )r  r  r   s    r5   rU   zRandomVariable.python_type  s
    }r4   c                     | j         S rE   )r  r   s    r5   rT   z!RandomVariable.as_python_constant  s
    {r4   c                    t          |           t          j        urdS t          j        t
          j        d          D ]}t          | |          s dS t          | |          }t          j
        |          r5|t          t          j        |                              |           k    r dS nt          |dd           t          t          j        |          ur dS dS )NF)r  r  r  r{   T)rX   r  r  ra  rb  r  _supported_fn_namesr   r]   rO  	isbuiltinrj  )r   rb   meths      r5   r  z&RandomVariable.is_supported_random_obj  s    99FM))5O.0P
 
 	! 	!D 3%% uu3%%D && !76=$77??DDDD 55 E 4T22'&-QU:V:VVV 55 Wtr4   c                 @   t          |           t          u sJ t          | d                   t          u sJ t          | d                   t          u sJ t          d | d         D                       sJ | d         t          | d                   t          u sJ d S d S )Nr   r"   c              3   B   K   | ]}t          |          t          u V  d S rE   )rX   intr~  s     r5   r  z-RandomVariable.check_state.<locals>.<genexpr>  s,      44a477c>444444r4   r   )rX   r  r  r`  r  states    r5   check_statezRandomVariable.check_state  s    E{{e####E!H~~$$$$E!H~~&&&&445844444444Qx4a>>U#:#:#:#:#:#:r4   c                 4   t                               |            t          j        t          j                            | d                   t          j        d | d         D                       t          j                            | d                   g          S )Nr   c                 L    g | ]!}t           j                            |          "S r3   rn  r~  s     r5   r;  z-RandomVariable.wrap_state.<locals>.<listcomp>  s)    LLLaY/66q99LLLr4   r"   r   )r  r  r	   r  r&   rp   r  s    r5   
wrap_statezRandomVariable.wrap_state  s    ""5)))&*11%(;;'LL58LLL  *11%(;;
 
 	
r4   c                 b    |                                  }t                              |           |S rE   )rT   r  r  )r  	state_objs     r5   unwrap_statezRandomVariable.unwrap_state  s-    ,,..	""9---r4   rL   r-   rr   r@   c                    dk    rv|j         j                            |             | j        j        d |D             i d |                                D              t          j                            d           S dk    r,| 	                    | j        
                                          S dk    rq|j         j                            |            | j                            |                     |d                              t          j                            d           S | j        v r|j         j                            |            | j        
                                fd} t          | j                  d |D             i d	 |                                D              t          ||||          S t!                                          |||          S )
Nr  c                 6    g | ]}|                                 S r3   r  r~  s     r5   r;  z.RandomVariable.call_method.<locals>.<listcomp>  $    777Q!&&((777r4   c                 >    i | ]\  }}||                                 S r3   r  )r  r  r   s      r5   r$  z.RandomVariable.call_method.<locals>.<dictcomp>  s*    PPPXS#3..00PPPr4   r  r  r   c                      t          j                    }|                                t          |          | i |S rE   )r  r  r  r]   )rr   r@   rrb   r  s      r5   call_random_methz4RandomVariable.call_method.<locals>.call_random_meth  s?    MOO

5!!!'wq$''8888r4   c                 6    g | ]}|                                 S r3   r  r~  s     r5   r;  z.RandomVariable.call_method.<locals>.<listcomp>  r  r4   c                 >    i | ]\  }}||                                 S r3   r  r!  s      r5   r$  z.RandomVariable.call_method.<locals>.<dictcomp>  s*    HHHA1a**,,HHHr4   )r   r   mutationr  r  r   r	   r&   rp   r  r  r  r  r  r]   r)   r;   r   )r?   rL   rb   rr   r@   r  r  rA   s     `   @r5   r   zRandomVariable.call_method  s    6>>I"++D111DK77$777PPPPP   -44T:::Z??4;#7#7#9#9:::ZI"++D111K  !2!247!;!;<<<-44T:::T---I"++D111K((**E9 9 9 9 9 9 'GDK&&77$777HHHHH  
 ""&6fEEEww""2tT6:::r4   rB   r,   c                 t                        fd                               dd                                                                d            |                     | j                                                                                 dd                                            d S )Nc                                                                 t                                         d          g          S )Nr  )rI   create_load_python_moduler  rk  rB   s   r5   rG   z,RandomVariable.reconstruct.<locals>.<lambda>  s>    G))55f==,,X66  r4   r   Fr  r"   T)rH   r   r   r   r  r  r  pop_topr  s    `r5   rK   zRandomVariable.reconstruct  s       	
 	
 	
 	a''' 	*%%% 4 4 6 677888a&&&r4   r  r   )r0   r1   r2   r  r%   r   r  r   r  r  r<   rU   rT   r   r  r  r  r  rt  r  r   r   rK   r   r   s   @r5   r  r    s         			'N
   )-*.. .v}%. '.
 
. . . . . .       \$ ; ; \; 

 

 \

   \
%;#%; ?#	%;
 S/)*%; 
%; %; %; %; %; %;N       r4   r  c                   f     e Zd Zed             Z fdZ	 	 	 	 	 	 	 	 ddZddZd Zd Z	d Z
 xZS )WeakRefVariablec                    |                     d          }|j        }|ot          |d          }t          j        | ||          } |            }|ot          |          }t          j        | ||          }||d<   t          ||fi |S )NrV   __callback__)ra   r  r   r%   r   r   r  )	rL   weakref_valueoptionsrV   callbackcallback_sourcecallback_vtreferentreferent_vts	            r5   r   zWeakRefVariable.build*  s    X&& - GZ%G%G%+B/JJ =??5-f55%+B&AA"{KCC7CCCr4   c                 V     t                      j        di | || _        || _        d S r:   )r;   r<   r  r  )r?   r  r  r  rA   s       r5   r<   zWeakRefVariable.__init__6  s6    ##7###&&r4   rL   r-   rr   rs   r@   rt   r8   r%   c                     | j         S rE   )r  r  s       r5   r   zWeakRefVariable.call_function;  s     r4   rB   r,   c                                          fd            | j                    | j                                       t	          dd                     d S )Nc                  0                          dd          S )Nweakrefref)r   r  s   r5   rG   z-WeakRefVariable.reconstruct.<locals>.<lambda>D  s    g&>&>y%&P&P r4   r   F)rH   r  r  rI   r
   r  s    `r5   rK   zWeakRefVariable.reconstructC  sl    PPPPQQQ !!! !!!21e<<=====r4   c                 4    | j                                         S rE   )r  r  r   s    r5   r  z"WeakRefVariable.is_python_hashableI  s    22444r4   c                 4    | j                                         S rE   )r  r  r   s    r5   r  zWeakRefVariable.get_python_hashL  s    //111r4   c                 @    | j                             |j                   S rE   )r  r  r  s     r5   r  zWeakRefVariable.is_python_equalP  s    //0ABBBr4   r   r   )r0   r1   r2   r   r   r<   r   rK   r  r  r  r   r   s   @r5   r  r  )  s        	D 	D \	D' ' ' ' '
 #  &  -	 
 
       > > > >5 5 52 2 2C C C C C C Cr4   r  )rr  rr  rJ  r  rO  ra  r  r  r  r   rB  typingr   r   torch._Cr   torch._numpyr,  rO  torch.utils._pytreer  _pytreer}  rN   r   r   r   r	   bytecode_transformationr
   r   r   create_parameter_opr   r   r   r   guardsr   r   mutation_guardr   rV   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r  r#   r$   r%   r  r&   	functionsr'   r(   user_definedr)   r*   r+   torch._dynamo.codegenr,   torch._dynamo.symbolic_convertr-   r/   r7   r   r  r  r  r  r%  r/  r2  	dataclassro  rN  r  r   rm   r  r  r  r  	lru_cacher  r&  r(  rR  r   rZ  rq  r  r  r  r  finfoiinfor  r4  r  r  r  r3   r4   r5   <module>r     s	   "                				 



   * * * * * * * *        $ $ $ $ $ $ $ $ $ @ @ @ @ @ @ @ @ @ @ @ @         
 G F F F F F 9 9 9 9 9 9 9 9 0 0 0 0 0 0 0 0 5 5 5 5 5 5               
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
         
 ' & & & & & G G G G G G G G X X X X X X X X X X  E//////DDDDDD	 	 	 	 	 	 	 	B
 B
 B
 B
 B
O B
 B
 B
J
J J J J J J J JZ    o   
 
 
 
 
o 
 
 
0=7 =7 =7 =7 =7 =7 =7 =7@; ; ; ; ;? ; ; ;(# # # # # # # #
% % %S S S S S S S Sl M M M M M M M M{- {- {- {- {-&? {- {- {-|2 2 2 2 26 2 2 2j( ( ( ( (_ ( ( (HO HO HO HO HOo HO HO HOVUG UG UG UG UGO UG UG UGp       &&= &= &= &= &=? &= &= &=RPG PG PG PG PG_ PG PG PGf Q   ( Q( (  (YG YG YG YG YGO YG YG YGz? ? ? ? ?? ? ? ?*- - - - -o - - -.D .D .D .D .D? .D .D .Db9 9 9 9 9 9 9 9x
 
 
 
 
O 
 
 
BO+ O+ O+ O+ O+? O+ O+ O+d# # # # #/ # # #( ( ( ( (- ( ( ( I#I#I! 
 
 
 
 
/ 
 
 
,N N N N N_ N N Nb(C (C (C (C (Co (C (C (C (C (Cr4   