a
    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-   J/var/www/auris/lib/python3.9/site-packages/torch/_dynamo/variables/misc.pyr)   E   s   r)   c                       sx   e Zd Zh ejZddd fddZdd fddZd	d
 fddZd	eddddZ	d	ddddddZ
  ZS )SuperVariableNreturnc                    s"   t  jf i | || _|| _d S N)super__init__typevarobjvar)selfr5   r6   kwargs	__class__r-   r.   r4   N   s    zSuperVariable.__init__r'   codegenc                    sX      fdd  | j | jd urD | j  tdd n tdd d S )Nc                      s   t tS r2   )r   BuiltinVariabler3   r-   )r:   r<   r-   r.   <lambda>[       z+SuperVariable.reconstruct.<locals>.<lambda>r   Fr   )add_push_nullr5   r6   extend_outputr	   r7   r<   r9   r;   r.   reconstructZ   s    


zSuperVariable.reconstructr(   txc              
      s   | j sJ d| j }| j  }| j jr6t| j jnd }t|trT| j j}| j j}d }|j	}z|
|d }W n& ty   tt|||d f Y S 0 t|t|D ]H}	||	 j|t }
r|
tur|rttt|d|	|}|
|f  S qt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valuerF   index
ValueErrorgetattrr3   rangelen__dict__getr)   r   r   r   )r7   rE   nameZsearch_typeZtype_to_useZtype_to_use_sourcerO   Z
search_mrostart_indexrS   Zresolved_getattrr9   r-   r.   _resolved_getattr_and_sourcec   sJ    
	


z*SuperVariable._resolved_getattr_and_sourcer    rE   rZ   r1   c                 C   sJ   |  | |\}}tj|s&t| |S |r:t|tj tjj	||dS )NrO   )
r\   r   r!   
is_literalGetAttrVariabler   
make_guardr   ZCONSTANT_MATCHcreate)r7   rE   rZ   rR   rO   r-   r-   r.   var_getattr   s    	
zSuperVariable.var_getattrlist[VariableTracker]dict[str, VariableTracker]rE   argsr8   r1   c              
   C   s  |  | |\}}|tju r"ttS |tjjju r| j}ddl	m
} t|tjrt|j|r|s|st 0 tjt|d|| jg| |W  d    S 1 s0    Y  n6tdd|  d| d| d| d| dg tjd	 n| jjrbt|d
rb|jdkrbtj|rb|j}	t|	dr>|	jdkr>t|	}
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#rDtj||d|| jg| |S t|t"j*rptj)|j!| j|d|||S t+|rt| jtr| jj,|g|R i |S |tj-u rl|d }z|. }W nF t/y } z,tdd|  d| ddg|d W Y d }~n
d }~0 0 |j0j	1| jsJt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 r|d j@}|j0j	A| j|rV|j0j	jB| j|dd}t|tj4rRtCtD| |S zt?| jj@|}W n tDy   tCtD| Y n0 d }| jjd urtE| jj|}t&'|||S |tjFjGu r>|d }i }|d jH}|d jHH D ]\}}|jI. }|||< q|jJjK}d|jJ_Kz||||W ||jJ_KS ||jJ_K0 n@t|t"jLr~|tMN v r~t&'|||}||| jg| |S tdd|  d| d| d| dd gd	 d S )!Nr   )AttributeMutationNewr^   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.rH   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.)rI   rJ   rK   rL   Z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)Z
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_effectsrh   
isinstancer   r&   mutation_typer   r#   r   call_functionr   r   Z	DIFFICULTrO   hasattrr*   UserDefinedClassVariableZ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__rM   NotImplementedErroroutputZis_attribute_mutation
DYNAMO_BUG
store_attrDeletedVariabler!   ZUserDefinedDictVariableZ_dict_methodsZ_dict_vtZUserDefinedTupleVariabler   Z	_tuple_vtZUserDefinedListVariabler   Z_list_vt__getattribute__rR   has_pending_mutation_of_attr	load_attrr   AttributeErrorr   Z_CZ_disabled_torch_function_implitemsZvtZsymbolic_torch_function_stateZtorch_function_subclass_enabledMethodDescriptorTyper   Zget_tensor_method)r7   rE   rZ   rg   r8   Zinner_fnrO   r6   rh   Zuser_clsZuser_cls_vtZuser_cls_sourceZcls_variableZ
cls_sourceattrexc	attr_nameresult
attr_valueattr_sourcefuncZ	tf_kwargsZtf_argsZhash_key_vtZvalue_vtZkey_strZtx_oldZfn_varr-   r-   r.   r{      sl   	


$
	










  


zSuperVariable.call_method)N)r*   r+   r,   r    _nonvar_fieldsr4   rC   r\   strrc   r{   __classcell__r-   r-   r9   r.   r/   I   s   	9r/   c                       s|   e Zd Zdd fddZd dddZdd	d
dZdd ZdeedddZ fddZ	 fddZ
dd ZeZ  ZS )ExceptionVariableNr0   c                    sJ   t  jf i | || _|| _td | _td | _td| _td | _d S )NF)	r3   r4   exc_typerg   r!   __context__	__cause____suppress_context____traceback__)r7   r   rg   r8   r9   r-   r.   r4     s    


zExceptionVariable.__init__)rJ   c                 C   s
   || _ d S r2   )r   )r7   rJ   r-   r-   r.   set_context  s    zExceptionVariable.set_contextr'   r;   c                    sd      fdd  j  tjd td d fdd}|d |d |d	 d S )
Nc                      s     djjS )Nrl   )load_import_fromr   r*   r-   r<   r7   r-   r.   r>     r?   z/ExceptionVariable.reconstruct.<locals>.<lambda>F)rZ   r1   c                    sV   t | }t|tr(|jdv sRJ |n*    |   d  |  d S )N)TFNr   )rU   r   r!   rR   dup_toprA   Zrot_nr   )rZ   r   r   r-   r.   codegen_attr  s    

z3ExceptionVariable.reconstruct.<locals>.codegen_attrr   r   r   )r@   Zforeachrg   rv   rW   r   )r7   r<   r   r-   r   r.   rC     s    
zExceptionVariable.reconstructc                 C   s   | j S r2   )r   r7   r-   r-   r.   rN     s    zExceptionVariable.python_typer(   )rE   name_varvalc                    s"   fdd}|  }|dkr(| | n|dkr|t|trD|jd u s^t|tjtjtjtj	frr|| _
td| _n|d n|dkrt|tr|jdv r|| _n|d nn|d	krt|tr|jd u r|| _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)rg   )r   	TypeErrorr!   )msgrD   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 rj   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.rH   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.)rM   r   rt   r!   rR   r   r=   r   Z!UserDefinedExceptionClassVariableZ"UserDefinedExceptionObjectVariabler   r   r   r   r   SUPPORTABLE)r7   rE   r   r   r   rZ   r-   rD   r.   call_setattr  sN    	

zExceptionVariable.call_setattrc                    sT   |dkr| j |g|R  S |dkr>|\}|  |td| | S t ||||S d S )N__setattr__with_tracebackr   )r   r!   r3   r{   )r7   rE   rZ   rg   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rVtj| j| jdS t 	||S )Nr   r   r   r   rg   r^   )
r   r   r   r   r!   ZListVariablerg   rO   r3   rc   )r7   rE   rZ   r9   r-   r.   rc     s    
zExceptionVariable.var_getattrc                 C   s   | j j d| j dS )N())r:   r*   r   r   r-   r-   r.   __str__  s    zExceptionVariable.__str__)r*   r+   r,   r4   r   rC   rN   r    r   r{   rc   r   __repr__r   r-   r-   r9   r.   r     s   9
r   c                   @   s   e Zd ZdZdS )UnknownVariablez
    It could be anything!
    Nr*   r+   r,   __doc__r-   r-   r-   r.   r     s   r   c                       s6   e Zd ZdZd fdd	Zdddd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                    s   t  jf i | || _d S r2   )r3   r4   r   )r7   r   r8   r9   r-   r.   r4     s    z DelayGraphBreakVariable.__init__r(   rd   re   r    rf   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: rH   )r   rO   rZ   r   r7   rE   rg   r8   r-   r-   r.   rv     s    
z%DelayGraphBreakVariable.call_function)Nr*   r+   r,   r   r4   rv   r   r-   r-   r9   r.   r     s   r   c                   @   sD   e Zd ZdZddddZdeddd	d
Zdddd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d S )Nzcomptime is special form)r   rB   r-   r-   r.   rC   )  s    zComptimeVariable.reconstructr(   r    r]   c                 C   s2   ddl m } ddlm} |t||t| j|dS )Nr   )comptimer   )r#   r^   )r   	functionsr#   rU   r   rO   )r7   rE   rZ   r   r#   r-   r-   r.   rc   ,  s
    zComptimeVariable.var_getattrrd   re   rf   c                 C   s   ddl m} |rJ t|dks$J |d }t|trH| || ntt|tr| }|jrpJ d|j	 t
||j|j |jrt|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   rW   rt   r#   Zget_functionr"   get_codeclosureco_freevarsr~   r   	f_globalsfn_namerM   defaultstupler   RuntimeErrorrQ   r   r!   rb   )r7   rE   rg   r8   r   fncoder   r-   r-   r.   rv   6  s.    

zComptimeVariable.call_functionN)r*   r+   r,   r   rC   r   rc   rv   r-   r-   r-   r.   r   #  s   r   c                       sH   e Zd ZU ee ed< dZee ed< dee dd fddZ  Z	S )CellVariablepre_existing_contentsN
local_name)r   r1   c                    s   t  jf i | || _d S r2   )r3   r4   r   )r7   r   r8   r9   r-   r.   r4   m  s    zCellVariable.__init__)N)
r*   r+   r,   r   r    __annotations__r   r   r4   r   r-   r-   r9   r.   r   `  s   
 r   c                       s"   e Zd Zdd fddZ  ZS )NewGlobalVariableNr0   c                    s   t  jf i | d S r2   r3   r4   r7   r8   r9   r-   r.   r4   u  s    zNewGlobalVariable.__init__)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 r2   )applyrg   r8   fn_clsr-   r.   trampoline_autograd_applyz  s    zDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply)!produce_trampoline_autograd_applyZ_origin)r   r   r-   r   r.   r   y  s    r   c                       sn   e Zd ZdZdhejZdd fddZddd	d
ZddddZddddZ	ddddddZ
  ZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassr   Nr0   c                    s   t  jf i | || _d S r2   )r3   r4   r   )r7   r   r8   r9   r-   r.   r4     s    z!AutogradFunctionVariable.__init__r(   rD   c                    s  d  fdd}t |||f  rt rtjdu rFt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rtdd	|  d
| d
| dddgtjd | jj}
|
tjjjurtdd	|  d
| d
| ddgtjd ddlm} | j}|d u r@t| 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.rpt,j/|j0t,1| j|d||S tdd	|  d
| d
| dt2|j ddgd d S )NFc                    s8   t | tjr| jdurd t | tjr4| r4d d S )NFT)rt   r   TensorVariablerequires_gradNNModuleVariableZis_training)noder   rE   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 rj   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.rH   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   )rz   r^   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   rp   Zis_grad_enabledr   Zcapture_autograd_functionwarningswarnZ"torch._functorch.autograd_functionr   Ztorch.autograd.functionr   r   r   r   ZvjpautogradZFunctionr   r   r   ZjvpZhigher_order_opsr   rO   r   Zimport_sourcer+   r*   backwardrv   r   ra   r   ZFUNCTION_MATCHAutogradFunctionContextVariablerb   rt   r~   r   inspect	signaturerW   _parametersr   r#   r   r   r}   rx   rQ   )r7   rE   rg   r8   r   r   r   Z
forward_fnZis_setup_ctx_definedZvjp_fnZjvp_fnr   rO   r   Zfwd_srcZ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
s4J t| jd}tj||d|||S )Nr   r   r^   )r   r   rQ   rR   rp   _dynamoexternal_utilsZFakeBackwardCFunctionrt   r~   r   r   rO   r   r#   rv   )r7   rE   rg   r8   r   Z	fn_sourcer-   r-   r.   call_backward  s    z&AutogradFunctionVariable.call_backwardc                 C   s
   t | jS r2   )r   r   r   r-   r-   r.   rv     s    z&AutogradFunctionVariable.call_functionrd   re   )rE   rg   r8   c           
      C   sd  ddl m} |dkr`t| jrNt| j}|||jjd|gt||R  dS | 	|||S n |dkrv| 
|||S | jd urt| j|nd }zt| j|}W n ty   d }Y n0 t|tr|| j}	|d urt|	j|	|d|||S t|	|	|||S nNt|tr:tj|j| |d|||S tdd	|  d
| d| ddgd d S )Nr   )wrap_fx_proxyr   rv   )rE   proxyr   r^   z$Unsupported autograd.Function methodri   rj   ,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.rH   )builderr   r   Zis_callable_allowedr   r   r   create_proxyr   r   r   rO   r   r   getattr_staticr   rt   r|   __get__lookupZcreate_with_sourcerv   r   r   r   r}   r   )
r7   rE   rZ   rg   r8   r   r   rO   objr   r-   r-   r.   r{     sd    	

z$AutogradFunctionVariable.call_method)r*   r+   r,   r   r    r   r4   r   r   rv   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listr   r    r   r-   r-   r-   r.   r   Y  s   
r   c                       s~   e Zd ZdZdddhejZddd fdd	Zedd
dddZdd Z	d
dddd fddZ
d
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f ||d| || _|| _|| _|| _d S N)rR   r   )r3   r4   r   r  needs_input_gradnon_differentiable)r7   rR   r   r   r  r  r  r8   r9   r-   r.   r4   j  s
    
z(AutogradFunctionContextVariable.__init__r(   rD   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 r2   )rt   r   r   r   .0xr-   r-   r.   	<genexpr>~  s   z9AutogradFunctionContextVariable.create.<locals>.<genexpr>T)r   r  r  )r   r   rs   Ztrack_object_newrp   r   functionZFunctionCtx	functoolspartialr   r   )rE   rg   r8   r  outr-   r-   r.   rb   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.rH   )r   r   r   r   r   r-   r-   r.   as_proxy  s    
z(AutogradFunctionContextVariable.as_proxyrd   re   r    rf   c                    s  |dkrt  ||||S |dkrNt|dks2J t|i d | _tjd S |dkrtdd|  d| d| d	g t	j
d
 | jd u rtdd|  d| ddgd
 | js| jr|rJ |jj| | t| jjdkrg | j_|D ]}| jj| qtjd S )Nr   mark_non_differentiabler   save_for_backwardz,Unsupported autograd.Function context methodri   rj   r   z` on `autograd.Function` context objects. Supported methods are `__setattr__`, `save_for_backward` and `mark_non_differentiable`.rH   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{   rW   r   r  r   r!   rb   r   r   r   r  r   rO   r   rs   Ztrack_save_for_backwardr   append)r7   rE   rZ   rg   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rjd ur`tjjS j	rt
j	d}tjj|S t  S )N)r  r  c                     s     | |S r2   )r{   r   rZ   r7   rE   r-   r.   r>     r?   z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>r  r  )ro   r  r   TupleVariabler   r   r  r!   rb   rO   r   r    r   rR   r3   rc   r7   rE   rZ   rO   r9   r  r.   rc     s    
z+AutogradFunctionContextVariable.var_getattr)NFNNN)NN)r*   r+   r,   r   r&   r   r4   r|   rb   r  r{   rc   r   r-   r-   r9   r.   r   ^  s0   
     	0r   c                       s<   e Zd ZdZddd fddZdddd	d
ddZ  ZS )AutogradEngineVariablez;
    Represents a torch._C._ImperativeEngine instance.
    Nr0   c                    s   t  jf ||d| d S r  r   )r7   rR   r   r8   r9   r-   r.   r4     s    zAutogradEngineVariable.__init__r(   rd   re   r    rf   c                 C   s   |dkrpt jjjrR|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 n$td	d|  d| d
| dg d d S )Nqueue_callbackzXqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=Truer^   z7Unsupported torch._C._ImperativeEngine.queue_callback()ri   rj   zYqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=True.rH   z-Unsupported torch._C._ImperativeEngine methodzgDynamo only supports the `queue_callback` method on a torch._C._ImperativeEngine instance, but found: `z`.)rp   r   Zcompiled_autogradZin_compiled_autograd_regionZ	one_graphr   r#   r   ZFakeCompiledAutogradEnginer  rO   rv   r   rs   Zget_ca_final_callbacks_varr   r7   rE   rZ   rg   r8   r-   r-   r.   r{     s6    

	z"AutogradEngineVariable.call_method)Nr*   r+   r,   r   r4   r{   r   r-   r-   r9   r.   r    s    
r  c                       s6   e Zd Zdd fddZddddd	d
dZ  ZS )ro   Nr0   c                    s   t  jf i | || _d S r2   )r3   r4   r   )r7   r   r8   r9   r-   r.   r4     s    zLambdaVariable.__init__r(   rd   re   r    rf   c                 C   s   | j |i |S r2   )r   r   r-   r-   r.   rv     s    zLambdaVariable.call_function)r*   r+   r,   r4   rv   r   r-   r-   r9   r.   ro     s   ro   c                       s   e Zd ZddhejZd"dd fddZ fddZedd	d
Ze	e
jjdddZdd Zdd ZddddZddddZdddddddZdee eeef ed fd d!Z  ZS )#r`   rZ   py_typeNr0   c                    sD   t  jf i | t|ts J t|ts.J || _|| _|| _d S r2   )r3   r4   rt   r    r   r   rZ   r  )r7   r   rZ   r  r8   r9   r-   r.   r4   #  s    zGetAttrVariable.__init__c                    s   | j d ur| j S t  S d S r2   )r  r3   rN   r   r9   r-   r.   rN   +  s    
zGetAttrVariable.python_typec                 C   s   | j j d| j d| j dS Nr   z, r   )r:   r*   r   rZ   r   r-   r-   r.   r   1  s    zGetAttrVariable.__repr__)
base_proxyc                 C   s
   t | |S r2   )rU   )r  r   r-   r-   r.   create_getattr_proxy4  s    z$GetAttrVariable.create_getattr_proxyc                 C   s   t | j | jS r2   )r`   r  r   r  rZ   r   r-   r-   r.   r  8  s    zGetAttrVariable.as_proxyc                 C   s@   | j  }zt|| jW S  ty:   t|  dd Y n0 d S )Nz is not a constant)r   rM   rU   rZ   r   r   )r7   constantr-   r-   r.   rM   ;  s
    
z"GetAttrVariable.as_python_constantr(   rD   c                 C   sZ   t | jtjst|j| jj}| j|j	vr2tt
|| j}||j	vrNtt
||S r2   )rt   r   r   r   r   r   Zget_submoduleZ
module_keyrZ   rX   r   r   )r7   rE   rZ   Zstep1Zstep2r-   r-   r.   const_getattrB  s    
zGetAttrVariable.const_getattrr'   r;   c                 C   s    || j  ||| j d S r2   )r   rA   Zcreate_load_attrsrZ   rB   r-   r-   r.   rC   M  s    
zGetAttrVariable.reconstructrd   re   r    rf   c                 C   s   | j || j||S r2   )r   r{   rZ   r   r-   r-   r.   rv   Q  s    zGetAttrVariable.call_functionc                    s  |dv r| j dkr|s|d  rt| jtjtjtjfr| j}|d  }|	||rd|
||S |dkrt|dkr|d S td S n|dkr| j dkrt|dkr|d  r|st| jtjtjtjfr| j}|d  }|	||rtdS td	S nd|d
krx| j dkrx|sxt| jtjr\| jj||d |d ddS t| jtjrx| j| t ||||S )N)__getitem__rY   rX   r   rY   r   r   __contains__TF__setitem__)Zdirectly_update_dict)rZ   is_python_constantrt   r   r   r&   r   rx   rM   Zhas_key_in_generic_dictrc   rW   r!   r   Zconvert_to_unspecializedr3   r{   )r7   rE   rZ   rg   r8   r   keyr9   r-   r.   r{   Y  sn    



zGetAttrVariable.call_method)N)r*   r+   r,   r    r   r4   rN   r   r   r|   rp   ZfxZProxyr  r  rM   r  rC   rv   r   dictr{   r   r-   r-   r9   r.   r`     s0   

r`   c                       sJ   e Zd Zdd fddZddddd	 fd
dZdd Zdd Z  ZS )MethodWrapperVariableNr0   c                    s"   t  jf i | || _i | _d S r2   )r3   r4   method_wrapperZ_builtin_fns)r7   r&  r8   r9   r-   r.   r4     s    zMethodWrapperVariable.__init__r(   rd   re   r    rf   c                    s   t | jrLt|d tjrLt|dkr2t|dks6J |d || jjjS | jj}| jj}|dkrt	|j
}|tj
u rtt|||g||S t |||S )Nr   r   r4   )r   r&  rt   r   r   rW   rc   ry   r*   rQ   r4   rn   r=   r{   r3   rv   )r7   rE   rg   r8   Zself_objZwrapper_nameZfn_objr9   r-   r.   rv     s    



z#MethodWrapperVariable.call_functionc                 C   s   dS NTr-   r   r-   r-   r.   r"    s    z(MethodWrapperVariable.is_python_constantc                 C   s   | j S r2   )r&  r   r-   r-   r.   rM     s    z(MethodWrapperVariable.as_python_constant)r*   r+   r,   r4   rv   r"  rM   r   r-   r-   r9   r.   r%    s   r%  c                       sD   e Zd Zdd fddZdd fddZd	d
 Zdd Z  ZS )GetSetDescriptorVariableNr0   c                    s   t  jf i | || _d S r2   )r3   r4   desc)r7   r)  r8   r9   r-   r.   r4     s    z!GetSetDescriptorVariable.__init__r(   rD   c                    s>   |dkr,| j r,t| j d}t|| jj|S t ||S d S )Nr   )rO   r   r    r   r)  r   r3   rc   r  r9   r-   r.   rc     s    z$GetSetDescriptorVariable.var_getattrc                 C   s   dS r'  r-   r   r-   r-   r.   r"    s    z+GetSetDescriptorVariable.is_python_constantc                 C   s   | j S r2   )r)  r   r-   r-   r.   rM     s    z+GetSetDescriptorVariable.as_python_constant)r*   r+   r,   r4   rc   r"  rM   r   r-   r-   r9   r.   r(    s   r(  c                       sn   e Zd ZddhejZejdd fddZdd Zd	d
 Z	e
dddZddddZddddZ  ZS )PythonModuleVariablerR   is_torchN)rR   r1   c                    s6   t  jf i | || _| jtu p.| jjd| _d S )Nztorch.)r3   r4   rR   rp   r*   
startswithr+  r7   rR   r8   r9   r-   r.   r4     s    zPythonModuleVariable.__init__c                 C   s   t jS r2   )r~   
ModuleTyper   r-   r-   r.   rN     s    z PythonModuleVariable.python_typec                 C   s   | j S r2   rR   r   r-   r-   r.   rM     s    z'PythonModuleVariable.as_python_constantr0   c                 C   s   d| j  dS )NzPythonModuleVariable(r   r/  r   r-   r-   r.   r     s    zPythonModuleVariable.__repr__r(   rD   c                 C   s   t | j|}tj|S r2   )rw   rR   r   r!   rb   r7   rE   rZ   r   r-   r-   r.   call_obj_hasattr  s    z%PythonModuleVariable.call_obj_hasattrc                 C   s   |j j| |r |j j| |S | js2|| jjvrbzt| j|}W qn ty^   t	t| Y qn0 n| jj| }| j
o~t| j
|}t|||S r2   )r   rs   r   r   r+  rR   rX   rU   r   r   rO   r   r    r   )r7   rE   rZ   r   rO   r-   r-   r.   rc     s    z PythonModuleVariable.var_getattr)r*   r+   r,   r    r   r~   r.  r4   rN   rM   r   r   r1  rc   r   r-   r-   r9   r.   r*    s   r*  c                       s^   e Zd Zdd fddZddddd	d
dZdedddZdd ZdddddZ  Z	S )TypingVariableNr0   c                    s   t  jf i | || _d S r2   r3   r4   rR   r-  r9   r-   r.   r4     s    zTypingVariable.__init__r(   rd   re   r    rf   c                 C   s:   |dkr.t |dkr.| j|d   }t|S td d S )Nr  r   r   z+unsupported method call on typing variablel)rW   rR   rM   r2  r   )r7   rE   rZ   rg   r8   Z
new_typingr-   r-   r.   r{     s    zTypingVariable.call_method)rE   rZ   c                 C   s~   ddl m}m} |tv r$t| |S |jj| |rB|j	| |S t
| j|}| jrnt| j|}||||S |||S d S )Nr   )SourcelessBuilderVariableBuilder)r   r4  r5  r   r   r`   r   rs   r   r   rU   rR   rO   r   rb   )r7   rE   rZ   r4  r5  rR   r   r-   r-   r.   rc     s    zTypingVariable.var_getattrc                 C   s   | j S r2   r/  r   r-   r-   r.   rM   (  s    z!TypingVariable.as_python_constantr'   )r<   r1   c                 C   s   | || j d S r2   )append_outputcreate_load_constrR   rB   r-   r-   r.   rC   +  s    zTypingVariable.reconstruct)
r*   r+   r,   r4   r{   r   rc   rM   rC   r   r-   r-   r9   r.   r2    s   r2  )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)utilsr9  r   rX   callablerU   )r9  Znp_fn_to_tnp_fnZnp_modZtnp_modr   Ztnp_fnZ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-   r  kvr-   r-   r.   
<dictcomp>b  r?   z%get_tnp_to_np_map.<locals>.<dictcomp>)r<  r   )mr-   r-   r.   get_tnp_to_np_map[  s    rB  c                       s   e Zd ZdZejfZdd fddZedd Z	edd	 Z
d
ddddddZd
ddd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.
    Nr0   c                    s   t  jf i | || _d S r2   r3  r-  r9   r-   r.   r4   l  s    zNumpyVariable.__init__c                 C   s:   |j d}t|dkr,|d d ddgks0J || jv S N.r   rp   _numpy)r+   splitrW   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s0J t|d S rD  )r+   rG  rW   np_constant_collections_maprY   rI  r-   r-   r.    get_constant_collection_for_funcv  s    $z.NumpyVariable.get_constant_collection_for_funcr(   rd   re   r    rf   c           
   	   C   s  t jstd| j d ddlm} ddlm} t 	| j}|d u rXtd| j d | 
| }d urz.|| jd	d
 |D i dd | D W S  ty   t| jj d| d|  Y n0 n|jdkrt jrd|j d}|dt j 7 }t| ||j||\}}| |rbt||rbtj|  dd
 |D i dd | D S |jjd||gt||R  }	|||	S d 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   s   g | ]}|  qS r-   rM   r  r-   r-   r.   
<listcomp>  r?   z/NumpyVariable.call_function.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r-   rQ  r=  r-   r-   r.   r@    r?   z/NumpyVariable.call_function.<locals>.<dictcomp>z with non-const args: rj   ztorch._numpy.randomz
delegate 'z' to NumPy itself via zconfig.use_numpy_random_stream=c                 S   s   g | ]}|  qS r-   rQ  r  r-   r-   r.   rR    r?   c                 S   s   i | ]\}}||  qS r-   rQ  r=  r-   r-   r.   r@    r?   rv   )r   trace_numpyr   rR   r:  rO  ZtensorrP  r<  rY   rN  r   r   r*   r+   Zuse_numpy_random_streamr,   Z
patch_argsrL  r   r   r!   rb   rM   r   r   r   )
r7   rE   rg   r8   rO  rP  r   Zcollection_variable_typr   r   r-   r-   r.   rv   |  sf    
zNumpyVariable.call_functionc                 C   s   t d d S )Nnumpy)r   r  r-   r-   r.   r{     s    zNumpyVariable.call_methodc                 C   s   | j S r2   r/  r   r-   r-   r.   rM     s    z NumpyVariable.as_python_constantc                    s$   t jrt| jtr| jjS t  S r2   )r   rS  rt   rR   rQ   r*   r3   r  r   r9   r-   r.   r    s    zNumpyVariable.as_proxy)r*   r+   r,   r   tnpZ
issubdtyperH  r4   r   rL  rN  rv   r{   rM   r  r   r-   r-   r9   r.   rC  e  s&   

D	rC  c                       s>   e Zd Zdd fddZedddZddd	d
Z  ZS )NullVariableNr0   c                    s   t  jf i | d S r2   r   r   r9   r-   r.   r4     s    zNullVariable.__init__c                 C   s   dS )NrV  r-   r   r-   r-   r.   r     s    zNullVariable.__repr__r'   r;   c                 C   s$   t jdk rtd |td d S )N)rm      z0cannot reconstruct NullVariable in < Python 3.11Z	PUSH_NULL)sysversion_infor   r6  r
   rB   r-   r-   r.   rC     s    
zNullVariable.reconstruct)r*   r+   r,   r4   r   r   rC   r   r-   r-   r9   r.   rV    s   rV  c                   @   s   e Zd ZdZdS )r   z"Marker used to implement delattr()Nr   r-   r-   r-   r.   r     s   r   c                       sZ   e Zd ZdZdhejZedd Zdd fddZe	dd	d
Z
d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 rNtj|jdd |D i dd | D S | |t	|t
|S )Nc                 s   s   | ]}|  V  qd S r2   )r"  r  r-   r-   r.   r    s   z.StringFormatVariable.create.<locals>.<genexpr>c                 S   s   g | ]}|  qS r-   rQ  )r  r?  r-   r-   r.   rR    r?   z/StringFormatVariable.create.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r-   rQ  r=  r-   r-   r.   r@    r?   z/StringFormatVariable.create.<locals>.<dictcomp>)all	itertoolschainvaluesr   r!   rb   formatr   r   r$  )rJ  r[  sym_args
sym_kwargsr-   r-   r.   rb     s    zStringFormatVariable.createNr0   c                    s6   t  jf i | t|ts J || _|| _|| _d S r2   )r3   r4   rt   r   r[  ra  rb  )r7   r[  ra  rb  r8   r9   r-   r.   r4     s
    zStringFormatVariable.__init__c                 C   s&   | j j d| jd| jd| jdS r  )r:   r*   r[  ra  rb  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`  )rA   r7  r[  create_load_attrr-   r   r-   r.   r>     s   
z2StringFormatVariable.reconstruct.<locals>.<lambda>T)Zcall_function_exc                 S   s   i | ]\}}t j||qS r-   r   r!   rb   r=  r-   r-   r.   r@    s   z4StringFormatVariable.reconstruct.<locals>.<dictcomp>CALL_FUNCTION_EXr   )r  )	r@   r   r  ra  rb  r   ZConstDictVariabler6  r
   )r7   r<   r8   r-   r   r.   rC     s    	z StringFormatVariable.reconstruct)r*   r+   r,   r   r    r   r   rb   r4   r   r   rC   r   r-   r-   r9   r.   rZ    s   
rZ  c                       s`   e Zd ZdZdd fddZedd Zdd	d
dZddddZeddddZ	  Z
S )DebuggingVariablez
    Represents a call to a debugging function like print(), or something
    registered to config.reorderable_logging_functions.
    Nr0   c                    s   t  jf i | || _d S r2   r3  r-  r9   r-   r.   r4     s    zDebuggingVariable.__init__c                 C   s(   t | o&t| tjtjfo&| tjjjv S r2   )	r;  rt   r~   r   BuiltinFunctionTyperp   r   r   Zreorderable_logging_functions)r   r-   r-   r.   is_reorderable_logging_function!  s
    z1DebuggingVariable.is_reorderable_logging_functionr(   rD   c                 C   sP   |j r
d S | | j||s8td| j d| d| d |j| t|f d S )NzReordering debugging function z with inputs rj   z is not yet implemented.)exportcan_reorder_logsrR   r   Zdebug_localsr  r   r   r-   r-   r.   rv   )  s    
zDebuggingVariable.call_functionr'   r;   c                 C   s   | j |S r2   )rO   rC   rB   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!   rZ  pytreeZtree_leavesrt   )r   rg   r8   Zallowed_input_typesZ	flat_argsr  r-   r-   r.   rj  9  s    
z"DebuggingVariable.can_reorder_logs)r*   r+   r,   r   r4   r|   rh  rv   rC   rj  r   r-   r-   r9   r.   rf    s   
rf  c                       s:   e Zd ZdZdd fddZdddd	d
ddZ  ZS )LoggingLoggerVariablez<
    Represents a call to any of logging.Logger methods
    Nr0   c                    s   t  jf i | || _d S r2   r3  r-  r9   r-   r.   r4   S  s    zLoggingLoggerVariable.__init__r(   rd   re   r    rf   c                 C   sP   |j r
d S t| j|d }t|dd }||htjjjrDt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)ri  rU   rR   intersectionrp   r   r   Zignore_logger_methodsr   r!   rb   r   )r7   rE   rZ   rg   r8   methodr	  r-   r-   r.   r{   W  s    z!LoggingLoggerVariable.call_methodr  r-   r-   r9   r.   rl  N  s   rl  c                       s   e Zd ZdZd ZzddlmZmZ	m
Z W n* eyR   eddi Z	eddi ZY n0 dd fdd	Zd
d Zdee eeef edddZdeedddZ  ZS )ConstantLikeVariablez8self.value is a compile-time constant, but not a literalr   )dtypefloatinggenericZinvalid_typer-   Nr0   c                    s   t  jf i | || _d S r2   r3  r-  r9   r-   r.   r4   z  s    zConstantLikeVariable.__init__c                 C   s   | j S r2   r/  r   r-   r-   r.   rM   ~  s    z'ConstantLikeVariable.as_python_constantr(   rf   c                 C   s   z$dd |D }dd |  D }W n4 tyX   t| j d| d| d| d Y n0 t| j||i |}tj|rtj	|S t
|tjrt|S t| j d| d	|  d S )
Nc                 S   s   g | ]}|  qS r-   rQ  r  r-   r-   r.   rR    r?   z4ConstantLikeVariable.call_method.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r-   rQ  r=  r-   r-   r.   r@    r?   z4ConstantLikeVariable.call_method.<locals>.<dictcomp>rE  z(*z, **r   z() -> )r   r   r   _error_prefixrU   rR   r   r!   r_   rb   rt   reMatchConstantRegexMatchVariable)r7   rE   rZ   rg   r8   ZcargsZckwargsr   r-   r-   r.   r{     s    (z ConstantLikeVariable.call_methodr]   c                 C   st   t | j|}t|| jr t|}t|| jr4t|S t|trRt|| j	rRt
|S tj|rjtj|S t| |S r2   )rU   rR   rt   np_floatingfloatnp_dtypeNumpyDTypeVariablerQ   rP   
np_genericrC  r   r!   r_   rb   r`   r0  r-   r-   r.   rc     s    z ConstantLikeVariable.var_getattr)r*   r+   r,   r   rs  rT  rp  ry  rq  rw  rr  r{  ImportErrorrQ   r4   rM   r   r    r$  r   r{   rc   r   r-   r-   r9   r.   ro  l  s   
ro  c                   @   s   e Zd ZdZdS )RegexPatternVariablez
re.PatternNr*   r+   r,   rs  r-   r-   r-   r.   r}    s   r}  c                   @   s   e Zd ZdZdS )rv  zre.MatchNr~  r-   r-   r-   r.   rv    s   rv  c                       s&   e Zd ZdZdd fddZ  ZS )TorchVersionVariableztorch.__version__Nr0   c                    s6   | dtj |d tju s J t jf i | d S )NrR   )
setdefaultrp   __version__r3   r4   r   r9   r-   r.   r4     s    zTorchVersionVariable.__init__)r*   r+   r,   rs  r4   r   r-   r-   r9   r.   r    s   r  c                   @   s   e Zd ZdZdS )NumpyTypeInfoVariableznp.iinfo/np.finfoNr~  r-   r-   r-   r.   r    s   r  c                   @   s   e Zd ZdZdd ZdS )rz  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).
        )rR   rQ   r*   r   r-   r-   r.   r    s    zNumpyDTypeVariable.as_proxyN)r*   r+   r,   rs  r  r-   r-   r-   r.   rz    s   rz  c                       s4   e Zd ZdZdd fddZdddd	Z  ZS )
RandomClassVariablezrandom.RandomNr0   c                    s   t  jf i | d S r2   r   r   r9   r-   r.   r4     s    zRandomClassVariable.__init__r(   rD   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   )seedru   )rW   r   r   r!   rb   RandomVariablebaseZValueMutationNew)r7   rE   rg   r8   r  r-   r-   r.   rv     s    
 
z!RandomClassVariable.call_functionr   r-   r-   r9   r.   r    s   r  c                       s   e Zd ZdZdhejZh dZdeej	 ee dd 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ee eeef ed fddZd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>   	randrangeuniformr  randintN)randr  r1   c                    sh   t  jf i | |d urD| |s(J t | _| j|  n |d urT| nd }t|| _d S r2   )r3   r4   is_supported_random_objr  RandomsetstategetstaterM   )r7   r  r  r8   r9   r-   r.   r4     s    
zRandomVariable.__init__c                 C   s   t jS r2   )r  r  r   r-   r-   r.   rN     s    zRandomVariable.python_typec                 C   s   | j S r2   )r  r   r-   r-   r.   rM     s    z!RandomVariable.as_python_constantc                 C   s   t | tjurdS ttjdD ]d}t| |s4 dS t| |}t	
|rf|ttj|| kr dS q t|dd ttj|ur  dS q dS )NF)r  r  r  r}   T)rQ   r  r  r]  r^  r  _supported_fn_namesrw   rU   r   	isbuiltinr   )r   rZ   methr-   r-   r.   r    s    


z&RandomVariable.is_supported_random_objc                 C   sv   t | tu sJ t | d tu s$J t | d tu s8J tdd | d D sRJ | d d u srt | d tu srJ d S )Nr   r   c                 s   s   | ]}t |tu V  qd S r2   )rQ   intr  r-   r-   r.   r     r?   z-RandomVariable.check_state.<locals>.<genexpr>r   )rQ   r   r  r\  rx  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-   rd  r  r-   r-   r.   rR  *  r?   z-RandomVariable.wrap_state.<locals>.<listcomp>r   r   )r  r  r   r  r!   rb   r  r-   r-   r.   
wrap_state#  s    
zRandomVariable.wrap_statec                 C   s   |   }t| |S r2   )rM   r  r  )r  Z	state_objr-   r-   r.   unwrap_state0  s    
zRandomVariable.unwrap_stater(   rf   c                    s   dkrL|j j|  | jjdd |D i dd | D  tjd S  dkrd| 	| j
 S  dkr|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   s   g | ]}|  qS r-   rQ  r  r-   r-   r.   rR  @  r?   z.RandomVariable.call_method.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r-   rQ  )r  r#  r   r-   r-   r.   r@  A  r?   z.RandomVariable.call_method.<locals>.<dictcomp>r  r  r   c                     s&   t  }| t| | i |S r2   )r  r  r  rU   )rg   r8   rrZ   r  r-   r.   call_random_methN  s    
z4RandomVariable.call_method.<locals>.call_random_methc                 S   s   g | ]}|  qS r-   rQ  r  r-   r-   r.   rR  V  r?   c                 S   s   i | ]\}}||  qS r-   rQ  r=  r-   r-   r.   r@  W  r?   )r   rs   Zmutationr  r  r   r   r!   rb   r  r  r  r  r  rU   r$   r3   r{   )r7   rE   rZ   rg   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  )rA   Zcreate_load_python_moduler  rc  r-   r;   r-   r.   r>   _  s   z,RandomVariable.reconstruct.<locals>.<lambda>r   Fr  r   T)r@   rv   r   r   r  r  r  Zpop_toprB   r-   r;   r.   rC   ]  s    

zRandomVariable.reconstruct)NN)r*   r+   r,   r   r    r   r  r   r  r  r4   rN   rM   r|   r  r  r  r  r   r$  r   r{   rC   r   r-   r-   r9   r.   r    s:   	  




'r  c                       sJ   e Zd Zedd Z fddZddddd	d
dZddddZ  ZS )WeakRefVariablec           	      K   sh   | dd }|j}|ot|d}t| ||}| }|o>t|}t| ||}||d< t||fi |S )NrO   __callback__)rY   r  r   r    r   r   r  )	rE   Zweakref_valueoptionsrO   callbackZcallback_sourcecallback_vtZreferentreferent_vtr-   r-   r.   r   q  s    zWeakRefVariable.buildc                    s"   t  jf i | || _|| _d S r2   )r3   r4   r  r  )r7   r  r  r  r9   r-   r.   r4   }  s    zWeakRefVariable.__init__r(   rd   re   r    rf   c                 C   s   | j S r2   )r  r   r-   r-   r.   rv     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-   r;   r-   r.   r>     r?   z-WeakRefVariable.reconstruct.<locals>.<lambda>r   F)r@   r  r  rA   r	   rB   r-   r;   r.   rC     s    

zWeakRefVariable.reconstruct)	r*   r+   r,   r|   r   r4   rv   rC   r   r-   r-   r9   r.   r  p  s   
r  )pr   r   r
  r   r]  r  rt  rX  r~   r   typingr   r   Ztorch._Crp   Ztorch._numpyrF  rU  Ztorch.utils._pytreer:  Z_pytreerk  rG   r   r   r   r   Zbytecode_transformationr	   r
   Zcreate_parameter_opr   r   r   r   r   Zguardsr   r   Zmutation_guardr   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r    r  r!   r   r"   r#   Zuser_definedr$   r%   r&   Ztorch._dynamo.codegenr'   Ztorch._dynamo.symbolic_convertr(   r)   r/   r   r   r   r   r   r   r   r   Z	dataclassr   r   r  ro   r`   r%  r(  r*  r2  	lru_cacher<  rB  rC  rV  r   rZ  rf  rl  ro  r}  rv  r  r  rz  ZfinfoZiinforp  rM  r  r  r  r-   r-   r-   r.   <module>   s   (
  : = Y}3 ,)@



	p17:	 