o
    ,h\#                    @   s  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
mZmZ ddlZddlmZ ddlm  mZ ddlmZmZ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# dd	l$m%Z%m&Z& dd
l'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z. ddlm/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@mAZA erddlBmCZC ddlDmEZE G dd dZFG dd de8ZGG dd de8ZHG dd de8ZIG dd deIZJG dd de8ZKG d d! d!e8ZLG d"d# d#e8ZMd$d% ZNG d&d' d'e8ZOejPG d(d) d)ZQG d*d+ d+eAZRG d,d- d-eAZSG d.d/ d/e8ZTG d0d1 d1e8ZUG d2d3 d3e8ZVG d4d5 d5e8ZWG d6d7 d7e8ZXG d8d9 d9e8ZYejZdd:d;d< Z[ejZdd:d=d> Z\G d?d@ d@e8Z]G dAdB dBe8Z^G dCdD dDe8Z_G dEdF dFe8Z`G dGdH dHe8ZaG dIdJ dJe8ZbG dKdL dLe8ZcG dMdN dNecZdG dOdP dPecZeG dQdR dRecZfG dSdT dTecZgG dUdV dVecZhejiegejjegejkehiZlG dWdX dXe8ZmG dYdZ dZe8ZnG d[d\ d\e8ZodS )]a  
This module contains miscellaneous variable tracker implementations for various Python types
and features used in Dynamo's symbolic execution. These classes help track and propagate
information about different kinds of variables during graph capture.

Key classes include:
- SuperVariable: Handles super() calls and method resolution
- ExceptionVariable: Tracks exception objects
- RandomVariable: Manages random number generators
- GetAttrVariable: Tracks attribute access
- MethodWrapperVariable: Handles method wrappers
- PythonModuleVariable: Tracks Python modules
- NumpyVariable: Handles numpy functions and types
- StringFormatVariable: Manages string formatting
- DebuggingVariable: Handles print and logging
    N)OptionalTYPE_CHECKING   )configgraph_break_hintstrace_rules	variables)create_call_functioncreate_instruction)$do_not_convert_to_tracable_parameter)raise_observed_exceptionunimplementedunimplemented_v2)GuardBuilderinstall_guard)unpatched_nn_module_init)
AttrSourceGenericAttrSourceGetItemSource
TypeSourceWeakRefCallSource)check_unspec_or_constant_argscmp_name_to_op_mappingidentityis_tensor_base_attr_getteristypelist_methodsproxy_args_kwargstuple_methods   )VariableTracker)ConstantVariable)NestedUserFunctionVariableUserFunctionVariable)call_random_fnis_standard_setattrUserDefinedObjectVariable)	PyCodegen)InstructionTranslatorc                   @   s   e Zd ZdS )NO_SUCH_SUBOBJN)__name__
__module____qualname__ r-   r-   W/var/www/html/scripts/venv/lib/python3.10/site-packages/torch/_dynamo/variables/misc.pyr)   E   s    r)   c                       sv   e Zd Zh ejZdd fddZd fddZd fddZd	d
deddfddZ				
						dddZ
  ZS )SuperVariableNreturnc                    "   t  jdi | || _|| _d S Nr-   )super__init__typevarobjvar)selfr5   r6   kwargs	__class__r-   r.   r4   N   s   
zSuperVariable.__init__codegenr'   c                    sZ      fdd  | j | jd ur# | j  tdd d S  tdd d S )Nc                      s   t tS N)r   BuiltinVariabler3   r-   )r:   r;   r-   r.   <lambda>[   s    z+SuperVariable.reconstruct.<locals>.<lambda>r   Fr   )add_push_nullr5   r6   extend_outputr	   r7   r;   r9   r;   r.   reconstructZ   s   


zSuperVariable.reconstructtxr(   c              
      s   | j sJ d| j }| j  }| j jrt| j jnd }t|tr*| j j}| j j}d }|j	}z	|
|d }W n tyK   tt|||d f Y S w t|t|D ]$}	||	 j|t }
rw|
turw|rqttt|d|	|}|
|f  S qStddd| d| j d| j  d	d
dgd d S )Nz1-arg super not implementedr   __mro__zUnable to resolve super getattr z"Dynamo failed to trace attribute `z$` accessed via `super()` (for type `z` and object `z8`) because the resolved attribute type is not supported.z0Ensure the attribute exists in the parent class.z(Check the arguments passed to `super()`.gb_typecontextexplanationhints)r6   r5   as_python_constantpython_typesourcer   
issubclasstypevaluerE   index
ValueErrorgetattrr3   rangelen__dict__getr)   r   r   r   )r7   rD   namesearch_typetype_to_usetype_to_use_sourcerN   
search_mrostart_indexrR   resolved_getattrr9   r-   r.   _resolved_getattr_and_sourcec   sN   

	


z*SuperVariable._resolved_getattr_and_sourcerY   r    c                 C   sJ   |  | |\}}tj|st| |S |rt|tj tjj	||dS )NrN   )
r`   r   r!   
is_literalGetAttrVariabler   
make_guardr   CONSTANT_MATCHcreate)r7   rD   rY   rQ   rN   r-   r-   r.   var_getattr   s   	
zSuperVariable.var_getattrargslist[VariableTracker]r8   dict[str, VariableTracker]c              
   C   s  |  | |\}}|tju rttS |tjjju rs| j}ddl	m
} t|tjrVt|j|rV|sV|sVt  tjt|d|| jg| |W  d    S 1 sOw   Y  n_tdd|  d| d| d| d| dg tjd	 nB| jjrt|d
r|jdkrtj|r|j}	t|	dr|	jdkrt|	}
n
|j}tj|	|d}
|
|d||S t|t rt|j!t"j#rtj|j!|d|||S t|t$rt|j!t"j#rt| jtjr| j}nd }| jjrt%| jjd}t&'|| jj(|}tj)|j!||d|||S t|t"j#rtj||d|| jg| |S t|t"j*r/tj)|j!| j|d|||S t+|rHt| jtrH| jj,|g|R i |S |tj-u r|d }z|. }W n# t/y{ } ztdd|  d| ddg|d W Y d }~nd }~ww |j0j	1| jstdd|  d| d| j ddgtj2d	 |j0j	3| j|t4  t5d S t| jtj6r|| jj7v r| jj8||||S t| jtj9r|t:v r| jj;||||S t| jtj<r|t=v r| jj>||||S |tj?u rS|d j@}|j0j	A| j|r"|j0j	jB| j|dd}t|tj4r tCtD| |S z
t?| jj@|}W n tDy;   tCtD| Y nw d }| jjd urLtE| jj|}t&'|||S |tjFjGu r|d }i }|d jH}|d jHH D ]\}}|jI. }|||< ql|jJjK}d|jJ_Kz||||W ||jJ_KS ||jJ_Kw t|t"jLr|tMN v rt&'|||}||| jg| |S tdd|  d| d| d| dd gd	 d S )!Nr   )AttributeMutationNewra   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.rG   r*   __new__r+   builtinsr:   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.)rH   rI   rJ   rK   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.)Or`   objectr4   LambdaVariabler   torchnnModuler6   side_effectsrk   
isinstancer   r&   mutation_typer   r#   r   call_functionr   r   	DIFFICULTrN   hasattrr*   UserDefinedClassVariableis_supported_new_method__self__r+   r=   membercall_methodstaticmethod__func__typesFunctionTypeclassmethodr   r    build
value_typeUserMethodVariable
MethodTyper%   method_setattr_standard__delattr__rL   NotImplementedErroroutputis_attribute_mutation
DYNAMO_BUG
store_attrDeletedVariabler!   UserDefinedDictVariable_dict_methods_dict_vtUserDefinedTupleVariabler   	_tuple_vtUserDefinedListVariabler   _list_vt__getattribute__rQ   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)r7   rD   rY   rh   r8   inner_fnrN   r6   rk   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_varr-   r-   r.   r      sb  
	

$
	















zSuperVariable.call_methodr<   r0   Nr;   r'   rD   r(   rD   r(   rh   ri   r8   rj   r0   r    )r*   r+   r,   r    _nonvar_fieldsr4   rC   r`   strrg   r   __classcell__r-   r-   r9   r.   r/   I   s"    	9r/   c                       st   e Zd Zd fddZdddZdd
dZdd ZdddedefddZ fddZ	 fddZ
dd ZeZ  ZS )ExceptionVariabler0   Nc                    sJ   t  jdi | || _|| _td | _td | _td| _td | _d S )NFr-   )	r3   r4   exc_typerh   r!   __context__	__cause____suppress_context____traceback__)r7   r   rh   r8   r9   r-   r.   r4     s   


zExceptionVariable.__init__rI   c                 C   s
   || _ d S r<   )r   )r7   rI   r-   r-   r.   set_context     
zExceptionVariable.set_contextr;   r'   c                    sf      fdd  j  tjd dtdd f fdd}|d |d	 |d
 d S )Nc                      s     djjS )Nro   )load_import_fromr   r*   r-   r;   r7   r-   r.   r>         z/ExceptionVariable.reconstruct.<locals>.<lambda>FrY   r0   c                    sX   t | }t|tr|jdv sJ |d S     |   d  |  d S )N)TFNr   )rT   r   r!   rQ   dup_topr@   rot_nr   )rY   r   r   r-   r.   codegen_attr  s   

z3ExceptionVariable.reconstruct.<locals>.codegen_attrr   r   r   )r?   foreachrh   r{   rV   r   )r7   r;   r   r-   r   r.   rC     s   
zExceptionVariable.reconstructc                 C      | j S r<   )r   r7   r-   r-   r.   rM        zExceptionVariable.python_typerD   r(   name_varvalc                    s"   fdd}|  }|dkr| | nx|dkr>t|tr"|jd u s/t|tjtjtjtj	fr9|| _
td| _nS|d nN|dkrUt|trP|jdv rP|| _n<|d n7|d	krwt|trg|jd u rg|| _n%td
d|  d| ddgd ntdd|  d| d| dg tjd td S )Nc                    s   t t t| gd d S )N)rh   )r   	TypeErrorr!   )msgrD   r-   r.   raise_error  s   z3ExceptionVariable.call_setattr.<locals>.raise_errorr   r   Tz9exception cause must be None or derive from BaseExceptionr   )TFr   z<Set Exception object `__traceback__` attribute to not-`None`zcall_setattr rm   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.rG   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.)rL   r   ry   r!   rQ   r   r=   r   !UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariabler   r   r   r   r   SUPPORTABLE)r7   rD   r   r   r   rY   r-   r   r.   call_setattr  sN   	


zExceptionVariable.call_setattrc                    sP   |dkr| j |g|R  S |dkr|\}|  |td| | S t ||||S )N__setattr__with_tracebackr   )r   r!   r3   r   )r7   rD   rY   rh   r8   tbr9   r-   r.   r     s   zExceptionVariable.call_methodc                    sd   |dkr| j S |dkr| jS |dkr| jS |dkrtd S |dkr+tj| j| jdS t 	||S )Nr   r   r   r   rh   ra   )
r   r   r   r   r!   ListVariablerh   rN   r3   rg   )r7   rD   rY   r9   r-   r.   rg     s   
zExceptionVariable.var_getattrc                 C   s   | j j d| j dS )N())r:   r*   r   r   r-   r-   r.   __str__     zExceptionVariable.__str__r   )rI   r   r   )r*   r+   r,   r4   r   rC   rM   r    r   r   rg   r   __repr__r   r-   r-   r9   r.   r     s     


9
r   c                   @      e Zd ZdZdS )UnknownVariablez
    It could be anything!
    Nr*   r+   r,   __doc__r-   r-   r-   r.   r         r   c                       s<   e Zd ZdZd fdd	Z							
		dddZ  ZS )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nc                       t  jdi | || _d S r2   )r3   r4   r   )r7   r   r8   r9   r-   r.   r4        
z DelayGraphBreakVariable.__init__rD   r(   rh   ri   r8   rj   r0   r    c                 C   s0   t dd| j d| j  d| j g d d S )Nz#Unsupported function call (delayed)zsource: z@Dynamo determined that a graph break should occur when calling `z`. Reason: rG   )r   rN   rY   r   r7   rD   rh   r8   r-   r-   r.   r{     s   

z%DelayGraphBreakVariable.call_functionr<   r   r*   r+   r,   r   r4   r{   r   r-   r-   r9   r.   r     s    r   c                   @   sJ   e Zd ZdZdddZddded	d
fddZ									
dddZdS )ComptimeVariableza
    This variable is special, it lets you execute arbitrary code at
    Dynamo compile time
    r;   r'   c                 C   s   t d)Nzcomptime is special form)r   rA   r-   r-   r.   rC   )  s   zComptimeVariable.reconstructrD   r(   rY   r0   r    c                 C   s2   ddl m } ddlm} |t||t| j|dS )Nr   )comptimer   )r#   ra   )r   	functionsr#   rT   r   rN   )r7   rD   rY   r   r#   r-   r-   r.   rg   ,  s
   zComptimeVariable.var_getattrrh   ri   r8   rj   c                 C   s   ddl m} |r
J t|dksJ |d }t|tr$| || n:t|trU| }|jr8J d|j	 t
||j|j |jrJt|jjnd d}||| n	tdt| tjd S )Nr   )ComptimeContextr   zOcomptime function must not have free variables, but these variables were free: r-   z"unsupported argument to comptime: )r   r   rV   ry   r#   get_functionr"   get_codeclosureco_freevarsr   r   	f_globalsfn_namerL   defaultstupler   RuntimeErrorrP   r   r!   rf   )r7   rD   rh   r8   r   fncoder   r-   r-   r.   r{   6  s.   

zComptimeVariable.call_functionNr   r   )r*   r+   r,   r   rC   r   rg   r{   r-   r-   r-   r.   r   #  s    

r   c                       sL   e Zd ZU ee ed< dZee ed< 	ddee ddf fddZ  Z	S )CellVariablepre_existing_contentsN
local_namer0   c                    r   r2   )r3   r4   r  )r7   r  r8   r9   r-   r.   r4   m  s   
zCellVariable.__init__r<   )
r*   r+   r,   r   r    __annotations__r  r   r4   r   r-   r-   r9   r.   r   `  s   
 r   c                       s   e Zd Zd fddZ  ZS )NewGlobalVariabler0   Nc                       t  jdi | d S r2   r3   r4   r7   r8   r9   r-   r.   r4   u  r   zNewGlobalVariable.__init__r   )r*   r+   r,   r4   r   r-   r-   r9   r.   r  t  s    r  c                    s    fdd}t |_|S )Nc                     s    j | i |S r<   )applyrh   r8   fn_clsr-   r.   trampoline_autograd_applyz  s   zDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply)!produce_trampoline_autograd_apply_origin)r  r  r-   r
  r.   r  y  s   r  c                       sb   e Zd ZdZdhejZd fddZdd	d
ZdddZdddZ							dddZ
  ZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassr  r0   Nc                    r   r2   )r3   r4   r  )r7   r  r8   r9   r-   r.   r4     r   z!AutogradFunctionVariable.__init__rD   r(   c                    s  d  fdd}t |||f  rt rtjdu r!td ddlm	} ddl
m} | jj}|| jj}|rB|| jj| jj}| jj}	|	tjjjurctdd	|  d
| d
| dddgtjd | jj}
|
tjjjurtdd	|  d
| d
| ddgtjd ddlm} | j}|d u rt| jj| jj}||| jj|t|ddd||}| jrt| jd}t| t!j" |rt| jd}t| t!j" |S | jrt| jd}nd }| jj}t#$||}|g|}t%|t&j'rt()|}t*|d t*|j+kr|dd  }t,j-||d||S t%|t&j.r1t,j/|j0t,1| j|d||S tdd	|  d
| d
| dt2|j ddgd d S )NFc                    s@   t | tjr| jdurd t | tjr| rd d S d S d S )NFT)ry   r   TensorVariablerequires_gradNNModuleVariableis_training)noder  rD   r-   r.   visit  s   

z2AutogradFunctionVariable.call_apply.<locals>.visitzNThe 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 rm   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.rG   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   ra   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  ru   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  rN   r   import_sourcer+   r*   backwardr{   r   rd   r   FUNCTION_MATCHAutogradFunctionContextVariablerf   ry   r   r   inspect	signaturerV   _parametersr   r#   r   r   r   r~   rP   )r7   rD   rh   r8   r  r  r  
forward_fnis_setup_ctx_definedvjp_fnjvp_fnr  rN   r   fwd_srcsetup_ctx_srcr   ctxsigr-   r  r.   
call_apply  s   	







z#AutogradFunctionVariable.call_applyc                 C   sX   | j j}t|d jtjjju sJ t|t	j
sJ t| jd}tj||d|||S )Nr   r(  ra   )r  r(  rP   rQ   ru   _dynamoexternal_utilsFakeBackwardCFunctionry   r   r   r   rN   r   r#   r{   )r7   rD   rh   r8   r   	fn_sourcer-   r-   r.   call_backward  s   z&AutogradFunctionVariable.call_backwardc                 C   s
   t | jS r<   )r  r  r   r-   r-   r.   r{     r   z&AutogradFunctionVariable.call_functionrh   ri   r8   rj   c           
      C   sZ  ddl m} |dkr.t| jr't| j}|||jjd|gt||R  dS | 	|||S |dkr9| 
|||S | jd urDt| j|nd }z	t| j|}W n tyZ   d }Y nw t|tr|| j}	|d uryt|	j|	|d|||S t|	|	|||S t|trtj|j| |d|||S tdd	|  d
| d| ddgd d S )Nr   )wrap_fx_proxyr  r{   )rD   proxyr(  ra   z$Unsupported autograd.Function methodrl   rm   ,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.rG   )builderr<  r   is_callable_allowedr  r  r   create_proxyr   r6  r;  rN   r   r+  getattr_staticr   ry   r   __get__lookupcreate_with_sourcer{   r   r   r   r   r   )
r7   rD   rY   rh   r8   r<  r  rN   objr   r-   r-   r.   r     sd   	




z$AutogradFunctionVariable.call_methodr   r   )rD   r(   rh   ri   r8   rj   )r*   r+   r,   r   r    r   r4   r6  r;  r{   r   r   r-   r-   r9   r.   r    s"    

~

r  c                   @   s&   e Zd ZU ejedZee ed< dS )SavedTensorBox)default_factorytensorsN)	r*   r+   r,   dataclassesfieldlistrI  r    r  r-   r-   r-   r.   rG  Y  s   
 rG  c                       s   e Zd ZdZdddhejZ					d	d fdd	ZeddddZdd Z		
							d fddZ
d fddZ  ZS )r*  zZ
    Tracks an autograd.Function() context using mutation tracking in side_effects.py
    r=  	inferencesaved_tensorsNFr0   c                    s4   t  jd||d| || _|| _|| _|| _d S N)rQ   r   r-   )r3   r4   rM  rN  needs_input_gradnon_differentiable)r7   rQ   r   rM  rN  rP  rQ  r8   r9   r-   r.   r4   j  s
   

z(AutogradFunctionContextVariable.__init__rD   r(   c              
   C   sL   d }|r|st dd |D }| jjd tjjjtj	t
dt |di }|S )Nc                 s   s"    | ]}t |tjo|jV  qd S r<   )ry   r   r  r  .0xr-   r-   r.   	<genexpr>~  s
    
z9AutogradFunctionContextVariable.create.<locals>.<genexpr>T)rM  rN  rP  )r   r   rx   track_object_newru   r#  functionFunctionCtx	functoolspartialr*  rG  )rD   rh   r8   rP  outr-   r-   r.   rf   z  s"   z&AutogradFunctionContextVariable.createc                 C   s,   | j d u rtdd|  dg tjd | j S )Nzproxy not setz	as_proxy zMDynamo requires the autograd.Function context to be initialized with a proxy.rG   )r=  r   r   r   r   r-   r-   r.   as_proxy  s   
z(AutogradFunctionContextVariable.as_proxyrh   ri   r8   rj   r    c                    s  |dkrt  ||||S |dkr't|dksJ t|i d | _tjd S |dkr@tdd|  d| d| d	g t	j
d
 | jd u rTtdd|  d| ddgd
 | jsf| jr\|r^J |jj| | t| jjdkrrg | j_|D ]	}| jj| qttjd S )Nr   mark_non_differentiabler   save_for_backwardz,Unsupported autograd.Function context methodrl   rm   r>  z` on `autograd.Function` context objects. Supported methods are `__setattr__`, `save_for_backward` and `mark_non_differentiable`.rG   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`.)r3   r   rV   r   rQ  r   r!   rf   r   r   r   rN  rM  rN   r   rx   track_save_for_backwardrI  append)r7   rD   rY   rh   r8   argr9   r-   r.   r     s>   
	z+AutogradFunctionContextVariable.call_methodc                    s    dv rt  fddS  dkr jd ur ttjjS  dkrBjd ur0tjjS j	rBt
j	d}tjj|S t  S )N)r^  r]  c                     s     | |S r<   )r   r	  rY   r7   rD   r-   r.   r>     r   z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>rN  rP  )rt   rN  r   TupleVariablerL  rI  rP  r!   rf   rN   r   r    r   rQ   r3   rg   r7   rD   rY   rN   r9   rb  r.   rg     s   
z+AutogradFunctionContextVariable.var_getattr)NFNNNr   NNr   r   )r*   r+   r,   r   r&   r   r4   r   rf   r\  r   rg   r   r-   r-   r9   r.   r*  ^  s:    
	0r*  c                       sB   e Zd ZdZ	d	d fddZ						
			dddZ  ZS )AutogradEngineVariablez;
    Represents a torch._C._ImperativeEngine instance.
    Nr0   c                    s   t  jd||d| d S rO  r  )r7   rQ   r   r8   r9   r-   r.   r4     s   zAutogradEngineVariable.__init__rD   r(   rh   ri   r8   rj   r    c                 C   s   |dkr9t jjjr)|jsJ dtjt jjjj	| j
d||jj g|R |S tdd|  d| dg d d S td	d|  d| d
| dg d d S )Nqueue_callbackzXqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=Truera   z7Unsupported torch._C._ImperativeEngine.queue_callback()rl   rm   zYqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=True.rG   z-Unsupported torch._C._ImperativeEngine methodzgDynamo only supports the `queue_callback` method on a torch._C._ImperativeEngine instance, but found: `z`.)ru   r7  compiled_autogradin_compiled_autograd_region	one_graphr   r#   r8  FakeCompiledAutogradEnginerg  rN   r{   r   rx   get_ca_final_callbacks_varr   r7   rD   rY   rh   r8   r-   r-   r.   r     s:   

	

z"AutogradEngineVariable.call_methodr<   r   r   r*   r+   r,   r   r4   r   r   r-   r-   r9   r.   rf    s    rf  c                       s8   e Zd Zd fddZ							
		dddZ  ZS )rt   r0   Nc                    r   r2   )r3   r4   r   )r7   r   r8   r9   r-   r.   r4     r   zLambdaVariable.__init__rD   r(   rh   ri   r8   rj   r    c                 C   s   | j |i |S r<   )r   r   r-   r-   r.   r{     s   zLambdaVariable.call_functionr   r   )r*   r+   r,   r4   r{   r   r-   r-   r9   r.   rt     s    rt   c                       s   e Zd ZddhejZd#d$ fddZ fddZdefd	d
Ze	de
jjfddZdd Zdd Zd%ddZd&ddZ								d'dd Zdddee deeef def fd!d"Z  ZS )(rc   rY   py_typeNr0   c                    sD   t  jdi | t|tsJ t|tsJ || _|| _|| _d S r2   )r3   r4   ry   r    r   rF  rY   ro  )r7   rF  rY   ro  r8   r9   r-   r.   r4   #  s   
zGetAttrVariable.__init__c                    s   | j d ur| j S t  S r<   )ro  r3   rM   r   r9   r-   r.   rM   +  s   

zGetAttrVariable.python_typec                 C   s   | j j d| j d| j dS Nr   z, r   )r:   r*   rF  rY   r   r-   r-   r.   r   1  s   zGetAttrVariable.__repr__
base_proxyc                 C   s
   t | |S r<   )rT   )rq  r   r-   r-   r.   create_getattr_proxy4  s   
z$GetAttrVariable.create_getattr_proxyc                 C   s   t | j | jS r<   )rc   rr  rF  r\  rY   r   r-   r-   r.   r\  8  s   zGetAttrVariable.as_proxyc                 C   s8   | j  }zt|| jW S  ty   t|  dd w )Nz is not a constant)rF  rL   rT   rY   r   r   )r7   constantr-   r-   r.   rL   ;  s   
z"GetAttrVariable.as_python_constantrD   r(   c                 C   sZ   t | jtjs	t|j| jj}| j|j	vrtt
|| j}||j	vr'tt
||S r<   )ry   rF  r   r  r   r   get_submodule
module_keyrY   rW   r+  rB  )r7   rD   rY   step1step2r-   r-   r.   const_getattrB  s   
zGetAttrVariable.const_getattrr;   r'   c                 C   s    || j  ||| j d S r<   )rF  r@   create_load_attrsrY   rA   r-   r-   r.   rC   M  s   
zGetAttrVariable.reconstructrh   ri   r8   rj   r    c                 C   s   | j || j||S r<   )rF  r   rY   r   r-   r-   r.   r{   Q  s   zGetAttrVariable.call_functionc                    sp  |dv rF| j dkrF|sF|d  rFt| jtjtjtjfrF| j}|d  }|	||r2|
||S |dkrEt|dkr@|d S td S ni|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dS td	S |d
kr| j dkr|st| jtjr| jj||d |d ddS t| jtjr| j| t ||||S )N)__getitem__rX   rW   r   rX   r   r   __contains__TF__setitem__)directly_update_dict)rY   is_python_constantry   rF  r   r&   r  r~   rL   has_key_in_generic_dictrg   rV   r!   r   convert_to_unspecializedr3   r   )r7   rD   rY   rh   r8   rF  keyr9   r-   r.   r   Y  sf   






zGetAttrVariable.call_methodr<   r   r   r   r   )r*   r+   r,   r    r   r4   rM   r   r   r   ru   fxProxyrr  r\  rL   rx  rC   r{   rL  dictr   r   r-   r-   r9   r.   rc     sB    



rc   c                       sL   e Zd Zd fddZ							
		d fddZdd Zdd Z  ZS )MethodWrapperVariabler0   Nc                    s"   t  jdi | || _i | _d S r2   )r3   r4   method_wrapper_builtin_fns)r7   r  r8   r9   r-   r.   r4        
zMethodWrapperVariable.__init__rD   r(   rh   ri   r8   rj   r    c                    s   t | jr&t|d tjr&t|dkrt|dksJ |d || jjjS | jj}| jj}|dkrJt	|j
}|tj
u rJtt|||g||S t |||S )Nr   r   r4   )r   r  ry   r   r  rV   rg   r   r*   rP   r4   rs   r=   r   r3   r{   )r7   rD   rh   r8   self_objwrapper_namefn_objr9   r-   r.   r{     s   



z#MethodWrapperVariable.call_functionc                 C      dS NTr-   r   r-   r-   r.   r~       z(MethodWrapperVariable.is_python_constantc                 C   r   r<   )r  r   r-   r-   r.   rL     r   z(MethodWrapperVariable.as_python_constantr   r   )r*   r+   r,   r4   r{   r~  rL   r   r-   r-   r9   r.   r    s    r  c                       s<   e Zd Zd fddZd fddZd	d
 Zdd Z  ZS )GetSetDescriptorVariabler0   Nc                    r   r2   )r3   r4   desc)r7   r  r8   r9   r-   r.   r4     r   z!GetSetDescriptorVariable.__init__rD   r(   c                    s:   |dkr| j rt| j d}t|| jj|S t ||S )NrC  )rN   r   r    r   r  rC  r3   rg   rd  r9   r-   r.   rg     s   z$GetSetDescriptorVariable.var_getattrc                 C   r  r  r-   r   r-   r-   r.   r~    r  z+GetSetDescriptorVariable.is_python_constantc                 C   r   r<   )r  r   r-   r-   r.   rL     r   z+GetSetDescriptorVariable.as_python_constantr   r   )r*   r+   r,   r4   rg   r~  rL   r   r-   r-   r9   r.   r    s
    r  c                       sh   e Zd ZddhejZdejddf fddZdd Zd	d
 Z	de
fddZdddZdddZ  ZS )PythonModuleVariablerQ   is_torchr0   Nc                    s6   t  jdi | || _| jtu p| jjd| _d S )Nztorch.r-   )r3   r4   rQ   ru   r*   
startswithr  r7   rQ   r8   r9   r-   r.   r4     s   zPythonModuleVariable.__init__c                 C      t jS r<   )r   
ModuleTyper   r-   r-   r.   rM     r   z PythonModuleVariable.python_typec                 C   r   r<   rQ   r   r-   r-   r.   rL     r   z'PythonModuleVariable.as_python_constantc                 C   s   d| j  dS )NzPythonModuleVariable(r   r  r   r-   r-   r.   r     s   zPythonModuleVariable.__repr__rD   r(   c                 C   s   t | j|}tj|S r<   )r}   rQ   r   r!   rf   r7   rD   rY   r   r-   r-   r.   call_obj_hasattr  s   z%PythonModuleVariable.call_obj_hasattrc                 C   s   |j j| |r|j j| |S | js|| jjvr0zt| j|}W n ty/   t	t| Y nw | jj| }| j
o>t| j
|}t|||S r<   )r   rx   r   r   r  rQ   rW   rT   r   r   rN   r   r    r   )r7   rD   rY   r   rN   r-   r-   r.   rg     s   z PythonModuleVariable.var_getattrr   )r*   r+   r,   r    r   r   r  r4   rM   rL   r   r   r  rg   r   r-   r-   r9   r.   r    s    
r  c                       s\   e Zd Zd fddZ							
		dddZdddefddZdd ZdddZ  Z	S )TypingVariabler0   Nc                    r   r2   r3   r4   rQ   r  r9   r-   r.   r4     r   zTypingVariable.__init__rD   r(   rh   ri   r8   rj   r    c                 C   s:   |dkrt |dkr| j|d   }t|S td d S )Nrz  r   r   z+unsupported method call on typing variablel)rV   rQ   rL   r  r   )r7   rD   rY   rh   r8   
new_typingr-   r-   r.   r     s   zTypingVariable.call_methodrY   c                 C   sz   ddl m}m} |tv rt| |S |jj| |r!|j	| |S t
| j|}| jr7t| j|}||||S |||S )Nr   )SourcelessBuilderVariableBuilder)r?  r  r  r   r   rc   r   rx   r   r   rT   rQ   rN   r   rf   )r7   rD   rY   r  r  rQ   r   r-   r-   r.   rg     s   zTypingVariable.var_getattrc                 C   r   r<   r  r   r-   r-   r.   rL   (  r   z!TypingVariable.as_python_constantr;   r'   c                 C   s   | || j d S r<   )append_outputcreate_load_constrQ   rA   r-   r-   r.   rC   +  s   zTypingVariable.reconstructr   r   )r;   r'   r0   N)
r*   r+   r,   r4   r   r   rg   rL   rC   r   r-   r-   r9   r.   r    s    
r  )maxsizec                  C   sZ   ddl m}  i }|  D ]\}}|j D ]\}}t|r)t||d }r)|||< qq|S )zd
    This generates a mapping from numpy modules to their torch._numpy
    modules equivalents.
    r   )NP_TO_TNP_MODULEN)utilsr  r   rW   callablerT   )r  np_fn_to_tnp_fnnp_modtnp_modr   tnp_fnnp_fnr-   r-   r.   get_np_to_tnp_mapF  s   r  c                  C   s   t  } d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                 S   s   i | ]\}}||qS r-   r-   rS  kvr-   r-   r.   
<dictcomp>b  s    z%get_tnp_to_np_map.<locals>.<dictcomp>)r  r   )mr-   r-   r.   get_tnp_to_np_map[  s   r  c                       s   e Zd ZdZejfZd fddZedd Z	edd	 Z
	
							dddZ	
							dddZdd Z fddZ  ZS )NumpyVariablezz
    Wrapper around `numpy.*`. Currently, is able to trace a small subset of numpy functions as well as numpy dtypes.
    r0   Nc                    r   r2   r  r  r9   r-   r.   r4   l  r   zNumpyVariable.__init__c                 C   s:   |j d}t|dkr|d d ddgksJ || jv S N.r   ru   _numpy)r+   splitrV   constant_fold_functionsclsr   modr-   r-   r.   can_constant_fold_throughp  s   $
z'NumpyVariable.can_constant_fold_throughc                 C   s<   |j d}t|dkr|d d ddgksJ t|d S r  )r+   r  rV   np_constant_collections_maprX   r  r-   r-   r.    get_constant_collection_for_funcv  s   $z.NumpyVariable.get_constant_collection_for_funcrD   r(   rh   ri   r8   rj   r    c           
   	   C   s  t jstd| j d ddlm} ddlm} t 	| j}|d u r,td| j d | 
| }d urez|| jd	d
 |D i dd | D W S  tyd   t| jj d| d|  Y d S w |jdkrt jrd|j d}|dt j 7 }t| ||j||\}}| |rt||rtj|  dd
 |D i dd | D S |jjd||gt||R  }	|||	S )Nznumpy.z()r   )numpy_to_tensor_wrapperr   )NumpyNdarrayVariablezCan't find numpy function zM in torch._numpy.  Please file an issue to request support for this function.c                 S      g | ]}|  qS r-   rL   rR  r-   r-   r.   
<listcomp>      z/NumpyVariable.call_function.<locals>.<listcomp>c                 S      i | ]	\}}||  qS r-   r  r  r-   r-   r.   r        z/NumpyVariable.call_function.<locals>.<dictcomp>z with non-const args: rm   ztorch._numpy.randomz
delegate 'z' to NumPy itself via zconfig.use_numpy_random_stream=c                 S   r  r-   r  rR  r-   r-   r.   r    r  c                 S   r  r-   r  r  r-   r-   r.   r    r  r{   )r   trace_numpyr   rQ   r  r  tensorr  r  rX   r  r   r   r*   r+   use_numpy_random_streamr,   
patch_argsr  r   r   r!   rf   rL   r   rA  r   )
r7   rD   rh   r8   r  r  r   collection_variable_typr   r=  r-   r-   r.   r{   |  sf   



zNumpyVariable.call_functionc                 C   s   t d d S )Nnumpy)r   rm  r-   r-   r.   r     s   zNumpyVariable.call_methodc                 C   r   r<   r  r   r-   r-   r.   rL     r   z NumpyVariable.as_python_constantc                    s$   t jrt| jtr| jjS t  S r<   )r   r  ry   rQ   rP   r*   r3   r\  r   r9   r-   r.   r\    s   
zNumpyVariable.as_proxyr   r   )r*   r+   r,   r   tnp
issubdtyper  r4   r   r  r  r{   r   rL   r\  r   r-   r-   r9   r.   r  e  s8    


B
	r  c                       s6   e Zd Zd fddZdefddZdd	d
Z  ZS )NullVariabler0   Nc                    r  r2   r  r  r9   r-   r.   r4     r   zNullVariable.__init__c                 C   r  )Nr  r-   r   r-   r-   r.   r     r  zNullVariable.__repr__r;   r'   c                 C   s$   t jdk r	td |td d S )N)rr      z0cannot reconstruct NullVariable in < Python 3.11	PUSH_NULL)sysversion_infor   r  r
   rA   r-   r-   r.   rC     s   
zNullVariable.reconstructr   r   )r*   r+   r,   r4   r   r   rC   r   r-   r-   r9   r.   r    s    r  c                   @   r   )r   z"Marker used to implement delattr()Nr   r-   r-   r-   r.   r     r   r   c                       sR   e Zd ZdZdhejZedd Zd fddZde	fd	d
Z
dddZ  ZS )StringFormatVariablez[
    Represents a call to str.format(), we delay calling format until after the graph.
    format_stringc                 C   sb   t dd t|| D r'tj|jdd |D i dd | D S | |t	|t
|S )Nc                 s   s    | ]}|  V  qd S r<   )r~  rR  r-   r-   r.   rU    s
    
z.StringFormatVariable.create.<locals>.<genexpr>c                 S   r  r-   r  )rS  r  r-   r-   r.   r    r  z/StringFormatVariable.create.<locals>.<listcomp>c                 S   r  r-   r  r  r-   r-   r.   r    r  z/StringFormatVariable.create.<locals>.<dictcomp>)all	itertoolschainvaluesr   r!   rf   formatr   rL  r  )r  r  sym_args
sym_kwargsr-   r-   r.   rf     s   zStringFormatVariable.creater0   Nc                    s6   t  jdi | t|tsJ || _|| _|| _d S r2   )r3   r4   ry   r   r  r  r  )r7   r  r  r  r8   r9   r-   r.   r4     s
   
zStringFormatVariable.__init__c                 C   s&   | j j d| jd| jd| jdS rp  )r:   r*   r  r  r  r   r-   r-   r.   r     s   &zStringFormatVariable.__repr__r;   r'   c                    s`    j  fdddd  tj dd j D } t|  tddd	 d S )
Nc                      s      j dgS )Nr  )r@   r  r  create_load_attrr-   r   r-   r.   r>     s
    
z2StringFormatVariable.reconstruct.<locals>.<lambda>T)call_function_exc                 S   s   i | ]\}}t j||qS r-   r   r!   rf   r  r-   r-   r.   r    s    z4StringFormatVariable.reconstruct.<locals>.<dictcomp>CALL_FUNCTION_EXr   )ra  )	r?   r   rc  r  r  r   ConstDictVariabler  r
   )r7   r;   r8   r-   r   r.   rC     s   	z StringFormatVariable.reconstructr   r   )r*   r+   r,   r   r    r   r   rf   r4   r   r   rC   r   r-   r-   r9   r.   r    s    
r  c                       sP   e Zd ZdZd fddZedd Zdd
dZdddZedddZ	  Z
S )DebuggingVariablez
    Represents a call to a debugging function like print(), or something
    registered to config.reorderable_logging_functions.
    r0   Nc                    r   r2   r  r  r9   r-   r.   r4     r   zDebuggingVariable.__init__c                 C   s(   t | ot| tjtjfo| tjjjv S r<   )	r  ry   r   r   BuiltinFunctionTyperu   r7  r   reorderable_logging_functions)rF  r-   r-   r.   is_reorderable_logging_function!  s
   z1DebuggingVariable.is_reorderable_logging_functionrD   r(   c                 C   sP   |j rd S | | j||std| j d| d| d |j| t|f d S )NzReordering debugging function z with inputs rm   z is not yet implemented.)exportcan_reorder_logsrQ   r   debug_localsr`  rL  r   r-   r-   r.   r{   )  s   
zDebuggingVariable.call_functionr;   r'   c                 C   s   | j |S r<   )rN   rC   rA   r-   r-   r.   rC   6  s   zDebuggingVariable.reconstructTc                 C   s:   t jt jtf}t||g}|D ]
}t||s dS qdS )zm
        Run some additional checks for what sort of function calls can we
        actually reorder.
        FT)r   r  r!   r  pytreetree_leavesry   )r   rh   r8   allowed_input_types	flat_argsra  r-   r-   r.   r  9  s   
z"DebuggingVariable.can_reorder_logsr   r   r   )r0   T)r*   r+   r,   r   r4   r   r  r{   rC   r  r   r-   r-   r9   r.   r    s    


r  c                       s<   e Zd ZdZd fddZ						
			dddZ  ZS )LoggingLoggerVariablez<
    Represents a call to any of logging.Logger methods
    r0   Nc                    r   r2   r  r  r9   r-   r.   r4   S  r   zLoggingLoggerVariable.__init__rD   r(   rh   ri   r8   rj   r    c                 C   sP   |j rd S t| j|d }t|dd }||htjjjr"tj	
d S td d S )Nr   zLogger not supported for non-export cases. To avoid graph breaks caused by logger in compile-mode, it is recommended to disable logging by adding logging methods to config.ignore_logger_methods)r  rT   rQ   intersectionru   r7  r   ignore_logger_methodsr   r!   rf   r   )r7   rD   rY   rh   r8   methodrW  r-   r-   r.   r   W  s   z!LoggingLoggerVariable.call_methodr   r   rn  r-   r-   r9   r.   r  N  s    r  c                       s   e Zd ZdZd ZzddlmZmZ	m
Z W n ey)   eddi Z	eddi ZY nw d fdd	Zd
d Zdddee deeef defddZdddedefddZ  ZS )ConstantLikeVariablez8self.value is a compile-time constant, but not a literalr   )dtypefloatinggenericinvalid_typer-   r0   Nc                    r   r2   r  r  r9   r-   r.   r4   z  r   zConstantLikeVariable.__init__c                 C   r   r<   r  r   r-   r-   r.   rL   ~  r   z'ConstantLikeVariable.as_python_constantrD   r(   rh   r8   c                 C   s   zdd |D }dd |  D }W n ty,   t| j d| d| d| d Y nw t| j||i |}tj|rDtj	|S t
|tjrNt|S t| j d| d	|  d S )
Nc                 S   r  r-   r  rR  r-   r-   r.   r    r  z4ConstantLikeVariable.call_method.<locals>.<listcomp>c                 S   r  r-   r  r  r-   r-   r.   r    r  z4ConstantLikeVariable.call_method.<locals>.<dictcomp>r  z(*z, **r   z() -> )r   r   r   _error_prefixrT   rQ   r   r!   rb   rf   ry   reMatchConstantRegexMatchVariable)r7   rD   rY   rh   r8   cargsckwargsr   r-   r-   r.   r     s   &z ConstantLikeVariable.call_methodrY   c                 C   st   t | j|}t|| jrt|}t|| jrt|S t|tr)t|| j	r)t
|S tj|r5tj|S t| |S r<   )rT   rQ   ry   np_floatingfloatnp_dtypeNumpyDTypeVariablerP   rO   
np_genericr  r   r!   rb   rf   rc   r  r-   r-   r.   rg     s   
z ConstantLikeVariable.var_getattrr   )r*   r+   r,   r   r  r  r  r  r  r  r  r  ImportErrorrP   r4   rL   rL  r    r  r   r   rg   r   r-   r-   r9   r.   r  l  s*    

r  c                   @   r   )RegexPatternVariablez
re.PatternNr*   r+   r,   r  r-   r-   r-   r.   r     r   r   c                   @   r   )r  zre.MatchNr  r-   r-   r-   r.   r    r   r  c                       s"   e Zd ZdZd fddZ  ZS )TorchVersionVariableztorch.__version__r0   Nc                    s6   | dtj |d tju sJ t jdi | d S )NrQ   r-   )
setdefaultru   __version__r3   r4   r  r9   r-   r.   r4     s   zTorchVersionVariable.__init__r   )r*   r+   r,   r  r4   r   r-   r-   r9   r.   r    s    r  c                   @   r   )NumpyTypeInfoVariableznp.iinfo/np.finfoNr  r-   r-   r-   r.   r    r   r  c                   @   s   e Zd ZdZdd ZdS )r  znp.dtype[...]c                 C   s
   | 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).
        )rQ   rP   r*   r   r-   r-   r.   r\    s   
zNumpyDTypeVariable.as_proxyN)r*   r+   r,   r  r\  r-   r-   r-   r.   r    s    r  c                       s,   e Zd ZdZd
 fddZddd	Z  ZS )RandomClassVariablezrandom.Randomr0   Nc                    r  r2   r  r  r9   r-   r.   r4     r   zRandomClassVariable.__init__rD   r(   c                 C   sT   t |dkrtd n|rtd t |dkrtjd n|d }t|tj dS )Nr   zrandom.Random() with > 1 argzrandom.Random() with kwargsr   )seedrz   )rV   r   r   r!   rf   RandomVariablebaseValueMutationNew)r7   rD   rh   r8   r  r-   r-   r.   r{     s   
 
z!RandomClassVariable.call_functionr   r   r   r-   r-   r9   r.   r    s    r  c                       s   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d	Z
d
d Zdd Zedd Zedd Zedd Zedd Zdddee deeef def fddZd!ddZ  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  r0   c                    sj   t  jdi | |d ur#| |sJ t | _| j|  d S |d ur+| nd }t|| _d S r2   )r3   r4   is_supported_random_objr  RandomsetstategetstaterL   )r7   r  r  r8   r9   r-   r.   r4     s   
zRandomVariable.__init__c                 C   r  r<   )r  r  r   r-   r-   r.   rM     r   zRandomVariable.python_typec                 C   r   r<   )r  r   r-   r-   r.   rL     r   z!RandomVariable.as_python_constantc                 C   s   t | tjur	dS ttjdD ]2}t| |s dS t| |}t	
|r3|ttj|| kr2 dS qt|dd ttj|urB dS qdS )NF)r  r  r  r   T)rP   r  r  r  r  r  _supported_fn_namesr}   rT   r+  	isbuiltinrC  )r   rY   methr-   r-   r.   r    s    


z&RandomVariable.is_supported_random_objc                 C   sz   t | tu sJ t | d tu sJ t | d tu sJ tdd | d D s)J | d d u s9t | d tu s;J d S d S )Nr   r   c                 s   s    | ]	}t |tu V  qd S r<   )rP   intrR  r-   r-   r.   rU     s    z-RandomVariable.check_state.<locals>.<genexpr>r   )rP   r   r  r  r  stater-   r-   r.   check_state  s
   (zRandomVariable.check_statec                 C   sF   t |  ttj| d tdd | d D tj| d gS )Nr   c                 S   s   g | ]}t j|qS r-   r  rR  r-   r-   r.   r  *  s    z-RandomVariable.wrap_state.<locals>.<listcomp>r   r   )r  r  r   rc  r!   rf   r  r-   r-   r.   
wrap_state#  s   
zRandomVariable.wrap_statec                 C   s   |   }t| |S r<   )rL   r  r  )r  	state_objr-   r-   r.   unwrap_state0  s   
zRandomVariable.unwrap_staterD   r(   rh   r8   c                    s   dkr&|j j|  | jjdd |D i dd | D  tjd S  dkr2| 	| j
 S  dkrN|j j|  | j| |d  tjd S  | jv r|j j|  | j
  fd	d
}t| j dd |D i dd | D  t||||S t | ||S )Nr  c                 S   r  r-   r  rR  r-   r-   r.   r  @  r  z.RandomVariable.call_method.<locals>.<listcomp>c                 S   r  r-   r  )rS  r  r   r-   r-   r.   r  A  r  z.RandomVariable.call_method.<locals>.<dictcomp>r  r  r   c                     s&   t  }| t| | i |S r<   )r  r  r  rT   )rh   r8   rrY   r  r-   r.   call_random_methN  s   
z4RandomVariable.call_method.<locals>.call_random_methc                 S   r  r-   r  rR  r-   r-   r.   r  V  r  c                 S   r  r-   r  r  r-   r-   r.   r  W  r  )r   rx   mutationr  r  r   r   r!   rf   r  r  r  r  r  rT   r$   r3   r   )r7   rD   rY   rh   r8   r   r9   r  r.   r   6  s2   


zRandomVariable.call_methodr;   r'   c                    s\      fdd  dd     d  | | j   dd    d S )Nc                      s      t dgS )Nr  )r@   create_load_python_moduler  r  r-   rB   r-   r.   r>   _  s
    z,RandomVariable.reconstruct.<locals>.<lambda>r   Fr  r   T)r?   r{   r   r   r  r  r  pop_toprA   r-   rB   r.   rC   ]  s   

zRandomVariable.reconstructre  r   )r*   r+   r,   r   r    r   r  r   r  r  r4   rM   rL   r   r  r  r  r  rL  r  r   r   rC   r   r-   r-   r9   r.   r    sH    	




'r  c                       sL   e Zd Zedd Z fddZ							
		dddZdddZ  ZS )WeakRefVariablec           	      K   sh   | dd }|j}|ot|d}t| ||}| }|ot|}t| ||}||d< t||fi |S )NrN   __callback__)rX   r%  r   r    r   r   r$  )	rD   weakref_valueoptionsrN   callbackcallback_sourcecallback_vtreferentreferent_vtr-   r-   r.   r   q  s   zWeakRefVariable.buildc                    r1   r2   )r3   r4   r,  r*  )r7   r,  r*  r'  r9   r-   r.   r4   }  r  zWeakRefVariable.__init__rD   r(   rh   ri   r8   rj   r0   r    c                 C   r   r<   )r,  r   r-   r-   r.   r{     s   zWeakRefVariable.call_functionr;   r'   c                    s:      fdd  | j  | j  tdd d S )Nc                      s     ddS )Nweakrefref)r   r-   rB   r-   r.   r>     s    z-WeakRefVariable.reconstruct.<locals>.<lambda>r   F)r?   r,  r*  r@   r	   rA   r-   rB   r.   rC     s   

zWeakRefVariable.reconstructr   r   )	r*   r+   r,   r   r   r4   r{   rC   r   r-   r-   r9   r.   r$  p  s    

r$  )pr   rJ  rY  r+  r  r  r  r  r   r  typingr   r   torch._Cru   torch._numpyr  r  torch.utils._pytreer  _pytreer  rF   r   r   r   r   bytecode_transformationr	   r
   create_parameter_opr   r   r   r   r   guardsr   r   mutation_guardr   rN   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r    rs  r!   r   r"   r#   user_definedr$   r%   r&   torch._dynamo.codegenr'   torch._dynamo.symbolic_convertr(   r)   r/   r   r   r   r   r   r  r  r  	dataclassrG  r*  rf  rt   rc   r  r  r  r  	lru_cacher  r  r  r  r   r  r  r  r  r   r  r  r  r  finfoiinfor  r  r  r  r$  r-   r-   r-   r.   <module>   s   (
  : = Y}3 ,)
@


	p17:	 