o
    eix                    @   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
Z
ddlZddlZddlmZmZ ddlmZ ddlmZ ddlmZmZmZmZmZmZ ddlZddlmZ ddlm  m Z! ddl"m#Z# ddl$m%Z% dd	l&m'Z' d
dl(m)Z)m*Z*m+Z+m,Z, d
dl-m.Z.m/Z/m0Z0 d
dl1m2Z2 d
dl3m4Z4m5Z5 d
dl6m7Z7m8Z8 d
dl9m:Z: d
dl;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA d
dlmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJ ddlKmLZLmMZMmNZN ddlOmPZPmQZQ ddlRmSZSmTZT ddlUmVZVmWZWmXZX erddlYmZZZ ddl[m\Z\ G dd dZ]G dd deNZ^G dd deNZ_G d d! d!eNZ`G d"d# d#eNZaG d$d% d%eNZbG d&d' d'ebZcG d(d) d)eNZdG d*d+ d+eNZeG d,d- d-eNZfd.ed/ed0ef fd1d2ZgG d3d4 d4eNZhejiG d5d6 d6ZjG d7d8 d8eXZkG d9d: d:eXZlG d;d< d<eNZmG d=d> d>eNZnG d?d@ d@eNZoG dAdB dBeNZpG dCdD dDeNZqG dEdF dFeNZrejsddGd/etejejuf fdHdIZvejsddGd/etejuejf fdJdKZwG dLdM dMeNZxG dNdO dOeNZyG dPdQ dQeNZzG dRdS dSeNZ{G dTdU dUeNZ|G dVdW dWeNZ}G dXdY dYeNZ~G dZd[ d[eNZG d\d] d]eNZG d^d_ d_eZG d`da daeZejeejeejeiZG dbdc dceNZG ddde deeNZG dfdg dgeNZdS )ha  
This module contains miscellaneous variable tracker implementations for various Python types
and features used in Dynamo's symbolic execution. These classes help track and propagate
information about different kinds of variables during graph capture.

Key classes include:
- SuperVariable: Handles super() calls and method resolution
- ExceptionVariable: Tracks exception objects
- RandomVariable: Manages random number generators
- GetAttrVariable: Tracks attribute access
- MethodWrapperVariable: Handles method wrappers
- PythonModuleVariable: Tracks Python modules
- NumpyVariable: Handles numpy functions and types
- StringFormatVariable: Manages string formatting
- DebuggingVariable: Handles print and logging
    N)CallableSequence)Random)BuiltinFunctionType)AnyLiteralNoReturnTYPE_CHECKING	TypeGuardUnion)MutationType)TupleVariable)Source   )configgraph_break_hintstrace_rules	variables)create_call_functioncreate_call_function_excreate_instruction)$do_not_convert_to_tracable_parameter)raise_observed_exceptionunimplemented)GuardBuilderinstall_guard)unpatched_nn_module_init)
AttrSourceGenericAttrSourceGetItemSourceTypeMROSource
TypeSourceWeakRefCallSource)	check_unspec_or_constant_argscmp_name_to_op_mappingidentityis_tensor_base_attr_getteristypelist_methodsproxy_args_kwargsraise_args_mismatchtuple_methods   )#AsPythonConstantNotImplementedErrorraise_type_error_excVariableTracker)CONSTANT_VARIABLE_NONEConstantVariable)NestedUserFunctionVariableUserFunctionVariable)call_random_fnis_standard_setattrUserDefinedObjectVariable)	PyCodegen)InstructionTranslatorc                   @   s   e Zd ZdS )NO_SUCH_SUBOBJN)__name__
__module____qualname__ r=   r=   f/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/_dynamo/variables/misc.pyr9   V   s    r9   c                
       s   e Zd Zh ejZ	ddededB deddf fddZd fd
dZdddede	ee
dB f f fddZdddedefddZdddedee deeef def
ddZ  ZS )SuperVariableNtypevarobjvarkwargsreturnc                    "   t  jdi | || _|| _d S Nr=   )super__init__r@   rA   )selfr@   rA   rB   	__class__r=   r>   rG   _   s   
zSuperVariable.__init__codegenr7   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   BuiltinVariablerF   r=   )rJ   rK   r=   r>   <lambda>q   s    z+SuperVariable.reconstruct.<locals>.<lambda>r   Fr,   )add_push_nullr@   rA   extend_outputr   rH   rK   rI   rK   r>   reconstructp   s   


zSuperVariable.reconstructtxr8   namec              
      sN  | j stddd| d| j d| j  ddgd | j d usJ | j }| j  }| j jr3t| j jnd }t|trB| j j	}| j j}d }|j
}z	||d	 }W n tyc   tt|||d f Y S w t|t|D ]#}	||	 j|t }
r|
tur|rttt||	|}|
|f  S qktd
dd| d| j d| j  dddgd d S )Nz1-arg super not implemented z"Dynamo failed to trace attribute `z$` accessed via `super()` (for type `z` and object `z4`) because one-argument of super() is not supported.z-Use two-argument super(type, object_or_type).gb_typecontextexplanationhintsr,   zUnable to resolve super getattrz8`) because the resolved attribute type is not supported.z0Ensure the attribute exists in the parent class.z(Check the arguments passed to `super()`.)rA   r   r@   as_python_constantpython_typesourcer!   
issubclasstypevalue__mro__index
ValueErrorgetattrrF   rangelen__dict__getr9   r   r   r    )rH   rT   rU   search_typetype_to_usetype_to_use_sourcer^   
search_mrostart_indexrc   resolved_getattrrI   r=   r>   _resolved_getattr_and_sourcey   sb   


	

z*SuperVariable._resolved_getattr_and_sourcec                 C   sJ   |  ||\}}tj|st| |S |rt|tj tjj	||dS )Nr^   )
rp   r   r1   
is_literalGetAttrVariabler   
make_guardr   CONSTANT_MATCHcreate)rH   rT   rU   ra   r^   r=   r=   r>   var_getattr   s   	
zSuperVariable.var_getattrargsc              
   C   s.  |  ||\}}| jd usJ |tju rttS |tjjju r}| j}ddl	m
} t|tjr`t|j|r`|s`|s`t  tj|t|d}	|	|| jg| |W  d    S 1 sYw   Y  ntdd|  d| d| d| d| dg tjd	 n| jjrt|d
r|jdkrtj|r|j}
t|
dr|
jdkrt|
}n|d usJ |j}tj|
|d}| |d||S t|t!rt|j"t#j$rtj||j"|dd}	|	|||S t|t%r%t|j"t#j$r%t| jtjr| j}nd }| jjrt&| jj}t|| jj'|}|d usJ tj||j"t(|ddd}	|	||g||S t|t#j$r@tj|||dd}	|	|| jg| |S t|t#j)rVtj*|j"| j|d|||S t+|rot| jtro| 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	 t|t3sJ |j0j	4| j|t5  tj6S t| jtj7r|| jj8v r| jj9 ||||S t| jtj:r|| jj;v r| jj< ||||S t| jtj=r#|t>v r#| jj? ||||S t| jtj@r:|tAv r:| jjB ||||S |tjCu r|d jD}|j0j	E| j|rh|j0j	jF| j|dd}t|tj5rftGtH| |S d }z
tC| jjD|}W n tHy   tGtH| Y nw d }| jjd urtI| jj|}t|||S |tjJjKu r|d }i }|d jL}|d jLL D ]\}}|jM. }|||< q|jNjO}d|jN_Oz||||W ||jN_OS ||jN_Ow t|t#jPr|tQR v rtj|||dd}||| jg| |S tdd|  d| d| d | d!d"gd	 d S )#Nr   )AttributeMutationNewrq   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.rW   r:   __new__r;   builtinsTr^   realize__func__r   z7Non-constant attribute given to `super().__delattr__()`zbDynamo requires the attribute name passed to `super().__delattr__(...)` to be a constant (string).zEEnsure the attribute name is a string literal or a constant variable.)rX   rY   rZ   r[   from_exczFAttempted super().__delattr__() on an object without mutation trackingzmDynamo needs to track mutations on an object before `super().__delattr__` can be used on it. But the object (z3) doesn't have attribute mutation tracking enabled.z<Ensure the object is tracked by Dynamo's side effect system.)
deleted_ok   F)r   zFAttempted to call a super() attribute that is not a function or methodz4Dynamo does not know how to trace the call `super().z()` because `super().z(` is not a function or method attribute.zMEnsure the attribute accessed via `super()` is a standard method or function.)Srp   rA   objectrG   LambdaVariabler%   torchnnModuleside_effectsry   
isinstancer   r6   mutation_typer   r/   buildr   call_functionr   r   	DIFFICULTr^   hasattrr:   UserDefinedClassVariableis_supported_new_method__self__r;   rM   membercall_methodstaticmethodr   typesFunctionTypeclassmethodr!   
value_typer   
MethodTypeUserMethodVariabler5   method_setattr_standard__delattr__r\   NotImplementedErroroutputis_attribute_mutation
DYNAMO_BUGstr
store_attrDeletedVariabler0   UserDefinedDictVariable_dict_methods_dict_vtUserDefinedSetVariable_set_methods_set_vtUserDefinedTupleVariabler+   	_tuple_vtUserDefinedListVariabler(   _list_vt__getattribute__ra   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)rH   rT   rU   rx   rB   inner_fnr^   rA   ry   fn_vtuser_clsuser_cls_vtuser_cls_sourcecls_variable
cls_sourceattrexc	attr_nameresult
attr_valueattr_sourcefunc	tf_kwargstf_argshash_key_vtvalue_vtkey_strtx_oldfn_varr=   r=   r>   r      st  
	

$
	












zSuperVariable.call_methodrL   rK   r7   rC   N)r:   r;   r<   r/   _nonvar_fieldsr   rG   rS   r   tupler   rp   rw   listdictr   __classcell__r=   r=   rI   r>   r?   Z   sD    	D
r?   c                       sT   e Zd Zdejdeddf fddZdefddZd	d
de	de
f fddZ  ZS )FrameSummaryVariableframe_summaryrB   rC   Nc                       t  jdi | || _d S rE   )rF   rG   r   )rH   r   rB   rI   r=   r>   rG        
zFrameSummaryVariable.__init__c                 C      t jS rL   )	tracebackFrameSummaryrH   r=   r=   r>   r]        z FrameSummaryVariable.python_typerT   r8   rU   c                    sn   |dkrt j| jjS |dkrt j| jjS |dkr$t j| jjS |dkr0t j| jjS t 	||S )NlinenofilenamerU   line)
r   r1   rv   r   r   r   rU   r   rF   rw   rH   rT   rU   rI   r=   r>   rw     s   z FrameSummaryVariable.var_getattr)r:   r;   r<   r   r   r   rG   r`   r]   r   r/   rw   r   r=   r=   rI   r>   r     s    "r   c                
       s  e Zd Zdeded ef deddf fddZede	j
ded ef dd fdd	Zed
edefddZdee	j
eB  fddZdedefddZdeej fddZdddededefddZdddedef fddZdddedee deeef def
 fddZ  ZS ) TracebackVariabler   tb_nextrB   rC   Nc                    s.   t  jdi | || _|d usJ || _d S rE   )rF   rG   r   r   )rH   r   r   rB   rI   r=   r>   rG     s   
zTracebackVariable.__init__c                 C   s   | t ||dS )N)r   )r   )clsr   r   r=   r=   r>   from_frame_summary     z$TracebackVariable.from_frame_summaryobjc                 C   s   t | tpt | to|  S rL   )r'   r   r1   is_constant_noner   r=   r=   r>   is_valid_traceback  s   
z$TracebackVariable.is_valid_tracebackc                 C   s&   t | jtr
| jgS | jg| j  S rL   )r'   r   r1   r   
extract_tbr   r=   r=   r>   r     s   zTracebackVariable.extract_tbtbc                 C   s.   | }t |tr||u rdS |j}t |tsdS )NTF)r'   r   r   )rH   r   curr_tbr=   r=   r>   has_reference_cycle  s   

z%TracebackVariable.has_reference_cyclec                 C   r   rL   )r   TracebackTyper   r=   r=   r>   r]     r   zTracebackVariable.python_typerT   r8   name_varvalc                 C   sj   |  }|dkr2| |stt| t|ttfsJ | |s*t|tr/|| r/tt	| || _
tjS )Nr   )r\   r   r   	TypeErrorr   r   r1   r   r'   rd   r   r   r0   )rH   rT   r   r   rU   r=   r=   r>   call_setattr  s   



zTracebackVariable.call_setattrrU   c                    sd   |dkr| j S |dkr| j|dS |dkr| jS |dkr+td|  ddg tjd	 t ||S )
Nr   	tb_linenor   r   tb_lastiz traceback.tb_lasti not supportedz accessing 'tb_lasti'zNDynamo does not support accessing the tb_lasti attribute of traceback objects.rW   )r   r   rw   r   r   SUPPORTABLErF   r   rI   r=   r>   rw     s   zTracebackVariable.var_getattrrx   c                    sH   |dkrt j| |d u S |dkr| j|g|R  S t ||||S )N__eq__r   __setattr__)r   r1   rv   r   rF   r   rH   rT   rU   rx   rB   rI   r=   r>   r     s
   zTracebackVariable.call_method)r:   r;   r<   r   r   r1   r   rG   r   r   r   r   r   r/   boolr   r   r   r   r`   r   r   r]   r   r   rw   r   r   r   r=   r=   rI   r>   r     sX    

	

r   c                       s   e Zd Z			d"dedeedf deeef dB dedB de	dB ddf fd	d
Z
deddfddZd#ddZdefddZdddededefddZdddedee deeef def
 fddZdddedef fddZdefd d!ZeZ  ZS )$ExceptionVariableNexc_typerx   .init_kwargsr^   r   rC   c                    sf   t  j||d || _|| _|r td|  d| dg tjd t| _t| _	t
d| _t| _d | _d S )N)r^   r   z,Keyword args passed to exception constructorz with kwargs zRDynamo does not know how to handle keyword args passed to an exception constructorrW   F)rF   rG   r   rx   r   r   r   r0   __context__	__cause__r1   __suppress_context____traceback__python_stack)rH   r   rx   r   r^   r   rI   r=   r>   rG   .  s   	

zExceptionVariable.__init__rY   c                 C   s
   || _ d S rL   )r   )rH   rY   r=   r=   r>   set_contextO     
zExceptionVariable.set_contextrK   r7   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 )Nr}   )load_import_fromr   r:   r=   rK   rH   r=   r>   rN   T  s    z/ExceptionVariable.reconstruct.<locals>.<lambda>FrU   rC   c                    sX   t | }t|tr|jdv sJ |d S     |   d  |  d S )N)TFNr   )re   r'   r1   ra   dup_toprP   rot_nr   )rU   r   r  r=   r>   codegen_attrY  s   

z3ExceptionVariable.reconstruct.<locals>.codegen_attrr   r   r  )rO   foreachrx   r   rg   r   )rH   rK   r
  r=   r  r>   rS   R  s   
zExceptionVariable.reconstructc                 C      | j S rL   )r   r   r=   r=   r>   r]   g  r   zExceptionVariable.python_typerT   r8   r   r   c                    s*  dt dtf fdd}| }|dkr$t|ttfsJ | | tjS |dkrL| s9t|tj	tjtj
tjfrE|| _td| _tjS |d tjS |d	krc|dd
r\|| _tjS |d tjS |dkr}t|swtt tdgd || _tjS tdd|  d| d| dg tjd tjS )NmsgrC   c                    s   t t t| gd d S )Nrx   )r   r   r1   )r  rT   r=   r>   raise_errorp  s   z3ExceptionVariable.call_setattr.<locals>.raise_errorr   r   Tz9exception cause must be None or derive from BaseExceptionr  Fr  z0__traceback__ must be a traceback object or Noner  z4Unsupported attribute assignment on Exception objectzcall_setattr r{   z/Dynamo does not support setting the attribute 'z{' on tracked exception objects. Only `__context__`, `__cause__`, `__suppress_context__`, and `__traceback__` are supported.rW   )r   r   r\   r   r   r1   r  r   r   rM   !UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariabler   r  is_constant_matchr   r   r   r   rv   r  r   r   r   r0   )rH   rT   r   r   r  rU   r=   r  r>   r   j  s^   
)
	
	
zExceptionVariable.call_setattrrU   rB   c                    sP   |dkr| j |g|R  S |dkr|\}|  |td| | S t ||||S )Nr   with_tracebackr  )r   r1   rF   r   )rH   rT   rU   rx   rB   r   rI   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| jS |dkr+tjt| j| jdS t	 
||S )Nr   r   r  r  rx   rq   )r   r   r  r  r   ListVariabler   rx   r^   rF   rw   r   rI   r=   r>   rw     s   zExceptionVariable.var_getattrc                 C   s   | j j d| j dS )N())rJ   r:   r   r   r=   r=   r>   __str__     zExceptionVariable.__str__)NNNr   )r:   r;   r<   r   r   r/   r   r   r   r   rG   r  rS   r`   r]   r   r   r   rw   r  __repr__r   r=   r=   rI   r>   r   ,  sV    
!

8
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                       sZ   e Zd ZdZddedB deddf fddZdd	d
ee de	eef defddZ
  ZS )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nr  rB   rC   c                    r   rE   )rF   rG   r  )rH   r  rB   rI   r=   r>   rG     r   z DelayGraphBreakVariable.__init__rT   r8   rx   c                 C   s@   | j d u rdn| j j}tdd| j  d| d| j g d d S )NrV   z#Unsupported function call (delayed)zsource: z@Dynamo determined that a graph break should occur when calling `z`. Reason: rW   )r^   rU   r   r  )rH   rT   rx   rB   rU   r=   r=   r>   r     s   

z%DelayGraphBreakVariable.call_functionrL   )r:   r;   r<   r  r   r   rG   r   r/   r   r   r   r=   r=   rI   r>   r     s     
r   c                   @   sV   e Zd ZdZdddZdd	d
edefddZdd	dee de	eef defddZ
dS )ComptimeVariableza
    This variable is special, it lets you execute arbitrary code at
    Dynamo compile time
    rK   r7   rC   Nc                 C   s   t d)Nzcomptime is special form)r   rQ   r=   r=   r>   rS     s   zComptimeVariable.reconstructrT   r8   rU   c                 C   s8   ddl m } | jd usJ tj|t||t| j|dS )Nr   )comptimerq   )r"  r^   r/   r   re   r   )rH   rT   rU   r"  r=   r=   r>   rw     s
   zComptimeVariable.var_getattrrx   rB   c                 C   s   ddl m} |st|dkrt|ddt| dt| d |d }t|tr3| || tjS t|trg| }|j	rHt
|d|j  t||j|j |jrZt|jjnd d	}||| tjS td
t| )Nr   )ComptimeContextz
comptime()zat most 2 args and 0 kwargsz
 args and  kwargsr   zOcomptime function must not have free variables, but these variables were free: r=   z"unsupported argument to comptime: )r"  r#  rg   r*   r   r3   get_functionr2   get_codeclosurer.   co_freevarsr   r   	f_globalsfn_namer\   defaultsr   r   RuntimeErrorr`   r   r0   )rH   rT   rx   rB   r#  fncoder   r=   r=   r>   r     s:   


zComptimeVariable.call_functionr   )r:   r;   r<   r  rS   r   r/   rw   r   r   r   r=   r=   r=   r>   r!    s    
	
r!  c                       sP   e Zd ZU edB ed< dZedB ed< 	ddedB deddf fddZ  Z	S )	CellVariableNpre_existing_contents
local_namerB   rC   c                    r   rE   )rF   rG   r0  )rH   r0  rB   rI   r=   r>   rG   3  s   
zCellVariable.__init__rL   )
r:   r;   r<   r/   __annotations__r1  r   r   rG   r   r=   r=   rI   r>   r/  &  s   
 r/  c                       s&   e Zd Zdeddf fddZ  ZS )NewGlobalVariablerB   rC   Nc                       t  jdi | d S rE   rF   rG   rH   rB   rI   r=   r>   rG   ;  r  zNewGlobalVariable.__init__)r:   r;   r<   r   rG   r   r=   r=   rI   r>   r3  :  s    r3  fn_clsrC   .c                    s$   dt dt dt f fdd}t|_|S )Nrx   rB   rC   c                     s    j | i |S rL   )applyrx   rB   r7  r=   r>   trampoline_autograd_apply@  s   zDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply)r   !produce_trampoline_autograd_apply_origin)r7  r;  r=   r:  r>   r<  ?  s   r<  c                
       s   e Zd ZdZdhejZdededdf fddZdd	d
ee de	e
ef defddZdd	d
ee de	e
ef defddZdd	d
ee de	e
ef dd fddZdd	de
d
ee de	e
ef def
ddZ  ZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassr7  rB   rC   Nc                    r   rE   )rF   rG   r7  )rH   r7  rB   rI   r=   r>   rG   P  r   z!AutogradFunctionVariable.__init__rT   r8   rx   c                    s  d dt dd f fdd}t |||f  rt r| j}ddlm} ddlm} | j	j
}|| j	j}	|	rB|| j	j
| j	j}d }| 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} |d u r|	st| j	j| j	j}|ot|dd}||| j	j||d||}| jr|	rt| jd}t|tj t| 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d}|||S t!|t"j)r8t*j+|j,t*-| j	|d||S tdd
|  d| d| dt.|j ddgd d S ) NFr   rC   c                    s<   |   r| jdurd t| tjr| rd d S d S d S )NFT)	is_tensorrequires_gradr   r   NNModuleVariableis_training)r   r@  rT   r=   r>   visit\  s   

z2AutogradFunctionVariable.call_apply.<locals>.visitr   )#autograd_function_forward_rewritten)_is_setup_context_definedzUnsupported custom vjpzcall_apply r{   zgDynamo does not support tracing `torch.autograd.Function` subclasses that define a custom `vjp` method.z+Remove the custom `vjp` method if possible.z.Use standard `backward` instead if applicable.rW   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,   )AutogradFunctionApplyVariabler8  )r   rq   forwardsetup_contextTr~   z=Non-function or method in subclass of torch.autograd.FunctionzDynamo requires the `forward` attribute of a `torch.autograd.Function` subclass to be a standard Python function or method. Found type `z
` instead.zPEnsure the `forward` method is defined as a regular function or instance method.)/r/   rD  r   is_grad_enabledr^   "torch._functorch.autograd_functionrE  torch.autograd.functionrF  r7  rH  rI  vjpautogradFunctionr   r   r   jvphigher_order_opsrG  r   import_sourcer;   r:   backwardr   r   rt   r   CLOSURE_MATCHAutogradFunctionContextVariablerv   r   r   r   inspect	signaturerg   
parametersr   r   r   r   r   r   r`   )rH   rT   rx   rB   rD  r^   rE  rF  
forward_fnis_setup_ctx_definedvjp_fnjvp_fnrG  apply_sourcer   fwd_srcsetup_ctx_srcr-  ctxsigr   r=   rC  r>   
call_applyT  s   







z#AutogradFunctionVariable.call_applyc                 C   sn   | j j}t|d jtjjju sJ t|t	j
sJ | jd us!J t| jd}tj|||dd}||||S )Nr   rS  Tr~   )r7  rS  r`   ra   r   _dynamoexternal_utilsFakeBackwardCFunctionr   r   r   r^   r   r/   r   r   )rH   rT   rx   rB   r-  	fn_sourcer   r=   r=   r>   call_backward  s   z&AutogradFunctionVariable.call_backwardc                 C   s
   t | jS rL   )r>  r7  rH   rT   rx   rB   r=   r=   r>   r        
z&AutogradFunctionVariable.call_functionrU   c                 C   sd  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}	t|	}
|
d usqJ |d ur|
j|	|d|||S |
|	|||S t|trtj|j| |d|||S tdd	|  d
| d| ddgd d S )Nr,   )wrap_fx_proxyr8  r   )rT   proxyrS  rq   z$Unsupported autograd.Function methodrz   r{   ,Dynamo does not support calling the method `z` directly on the `torch.autograd.Function` instance. Supported methods include `apply`, `backward`, static methods, and class methods.znEnsure the method is decorated with `@staticmethod` or `@classmethod` if it's meant to be called on the class.rW   )builderrj  r   is_callable_allowedr7  r<  r   create_proxyr)   rb  rg  r^   r   rV  getattr_staticr   r   r   __get__lookupcreate_with_sourcer   r   r   r   r   r   )rH   rT   rU   rx   rB   rj  r;  r^   r   r   tracedr=   r=   r>   r     sb   	




z$AutogradFunctionVariable.call_method)r:   r;   r<   r  r/   r   r   rG   r   r   r   rb  rg  r   r   r   r   r=   r=   rI   r>   r>  H  sZ    

}




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fieldr   rw  r/   r2  r=   r=   r=   r>   ru  +  s   
 ru  c                       s  e Zd ZdZdddhejZ					ddededB dededB d	e	ed
f dB dedB deddf fddZ
e		ddddee dB deeef dB defddZdefddZdddedee deeef def
 fddZdddedef fddZ  ZS )rU  zZ
    Tracks an autograd.Function() context using mutation tracking in side_effects.py
    rk  	inferencesaved_tensorsNFra   r   needs_input_grad.non_differentiablerB   rC   c                    s4   t  jd||d| || _|| _|| _|| _d S N)ra   r   r=   )rF   rG   rz  r{  r|  r}  )rH   ra   r   rz  r{  r|  r}  rB   rI   r=   r>   rG   <  s
   

z(AutogradFunctionContextVariable.__init__rT   r8   rx   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    | ]
}|  o
|jV  qd S rL   )r?  r@  .0xr=   r=   r>   	<genexpr>U  s    z9AutogradFunctionContextVariable.create.<locals>.<genexpr>T)rz  r{  r|  )r   r   r   track_object_newr   rN  functionFunctionCtx	functoolspartialrU  ru  )rT   rx   rB   r|  outr=   r=   r>   rv   L  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.rW   )rk  r   r   r   r   r=   r=   r>   as_proxyc  s   
z(AutogradFunctionContextVariable.as_proxyrU   c                    s  |dkrt  ||||S |dkr*|rt||dt| d t|i d | _tjS |dkrCtdd|  d	| d
| dg t	j
d | jd u rWtdd|  d	| ddgd | jd us^J | jss|sf| jskt|d |jj| | t| jjdkrg | j_|D ]	}| jj| qtjS )Nr   mark_non_differentiablez0 kwargsr$  r   save_for_backwardz,Unsupported autograd.Function context methodrz   r{   rl  z` on `autograd.Function` context objects. Supported methods are `__setattr__`, `save_for_backward` and `mark_non_differentiable`.rW   z9Unsupported autograd.Function context `save_for_backward`zjDynamo requires the `saved_tensors` attribute to be initialized on the `autograd.Function` context object.zEnsure that the `saved_tensors` attribute is properly initialized before calling `save_for_backward`. `save_for_backward` only supported on a newly constructed `torch.autograd.function.FunctionCtx`.z>save_for_backward() requires a source and no keyword arguments)rF   r   r*   rg   r)   r}  r   r0   r   r   r   r{  rz  r^   r.   r   r   track_save_for_backwardrw  append)rH   rT   rU   rx   rB   argrI   r=   r>   r   p  sH   
	
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     t| |S rL   )r   r   r9  rU   rH   rT   r=   r>   rN         z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>r{  r|  )r   r{  r   r   r   rw  r|  r1   rv   r^   r   r/   r   ra   rF   rw   rH   rT   rU   r^   rI   r  r>   rw     s   
z+AutogradFunctionContextVariable.var_getattr)NFNNNNN)r:   r;   r<   r  r6   r   r   r`   r   r   rG   r   r   r/   r   r   rv   r  r   r   rw   r   r=   r=   rI   r>   rU  0  sn    
	

"5rU  c                
       sp   e Zd ZdZ	ddejjdeejj dB deddf fddZ	d	d
de
dee dee
ef def
ddZ  ZS )AutogradEngineVariablez;
    Represents a torch._C._ImperativeEngine instance.
    Nra   r   rB   rC   c                    s   t  jd||d| d S r~  r5  )rH   ra   r   rB   rI   r=   r>   rG     s   zAutogradEngineVariable.__init__rT   r8   rU   rx   c                 C   s   |dkr<t jjjr,|js|jsJ dt|t jjj	j
}|||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=Truez7Unsupported torch._C._ImperativeEngine.queue_callback()rz   r{   zYqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=True.rW   z-Unsupported torch._C._ImperativeEngine methodzgDynamo only supports the `queue_callback` method on a torch._C._ImperativeEngine instance, but found: `z`.)r   rc  compiled_autogradin_compiled_autograd_region	one_grapherror_on_graph_breakr/   r   rd  FakeCompiledAutogradEnginer  r   r   r   get_ca_final_callbacks_varr   )rH   rT   rU   rx   rB   r   r=   r=   r>   r     s8   



z"AutogradEngineVariable.call_methodrL   )r:   r;   r<   r  r   r   _ImperativeEnginer`   r   rG   r   r   r/   r   r   r   r=   r=   rI   r>   r    s.    
r  c                       sX   e Zd Zdedef deddf fddZdd	d
ee dee	ef defddZ
  ZS )r   r-  .rB   rC   Nc                    r   rE   )rF   rG   r-  )rH   r-  rB   rI   r=   r>   rG     r   zLambdaVariable.__init__rT   r8   rx   c                 C   s   | j |i |S rL   )r-  rh  r=   r=   r>   r     r   zLambdaVariable.call_function)r:   r;   r<   r   r/   r   rG   r   r   r   r   r   r=   r=   rI   r>   r     s    "
r   c                       s(  e Zd ZddhejZ	d$dedededB deddf
 fddZdef fd	d
Z	defddZ
edejjdedefddZdefddZdefddZdddedefddZd%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e deeef def
 fd d!Zdddefd"d#Z  ZS )&rs   rU   py_typeNr   rB   rC   c                    sD   t  jdi | t|tsJ t|tsJ || _|| _|| _d S rE   )rF   rG   r   r/   r   r   rU   r  )rH   r   rU   r  rB   rI   r=   r>   rG     s   
zGetAttrVariable.__init__c                    s   | j d ur| j S t  S rL   )r  rF   r]   r   rI   r=   r>   r]     s   

zGetAttrVariable.python_typec                 C   s   | j j d| j d| j dS Nr  z, r  )rJ   r:   r   rU   r   r=   r=   r>   r    s   zGetAttrVariable.__repr__
base_proxyr   c                 C   s
   t | |S rL   )re   )r  r   r=   r=   r>   create_getattr_proxy     
z$GetAttrVariable.create_getattr_proxyc                 C   s   t | j | jS rL   )rs   r  r   r  rU   r   r=   r=   r>   r    s   zGetAttrVariable.as_proxyc                 C   s8   | j  }zt|| jW S  ty   t|  dd w )Nz is not a constant)r   r\   re   rU   r   r   )rH   constantr=   r=   r>   r\      s   
z"GetAttrVariable.as_python_constantrT   r8   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 rL   )r   r   r   rA  r   r   get_submodule
module_keyrU   rh   rV  rp  )rH   rT   rU   step1step2r=   r=   r>   const_getattr'  s   
zGetAttrVariable.const_getattrrK   r7   c                 C   s    || j  ||| j d S rL   )r   rP   create_load_attrsrU   rQ   r=   r=   r>   rS   2  s   
zGetAttrVariable.reconstructrx   c                 C   s   | j || jt||S rL   )r   r   rU   r   rh  r=   r=   r>   r   6  s   zGetAttrVariable.call_functionc                    sl  |dv rD| j dkrD|sD|d  rDt| jtjtjtjfrD| j}|d  }|	||r2|
||S |dkrCt|dkr@|d S tj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__ri   rh   r   ri   r   r,   __contains__TF__setitem__)directly_update_dict)rU   is_python_constantr   r   r   r6   rA  r   r\   has_key_in_generic_dictrw   rg   r0   r1   r   convert_to_unspecializedrF   r   )rH   rT   rU   rx   rB   r   keyrI   r=   r>   r   >  sf   





zGetAttrVariable.call_methodc                 C   sJ   | j dkrt| jtjr|jj| jrJ d| j_t	
|| jjj| jS )Nrh   T)rU   r   r   r   r   r   r   has_pending_mutationban_mutationr/   r   ra   rh   r^   )rH   rT   r=   r=   r>   get_forwarded_dict  s   
z"GetAttrVariable.get_forwarded_dictrL   r   )r:   r;   r<   r/   r   r   r`   r   rG   r]   r  r   r   fxProxyr  r  r\   r  rS   r   r   r   r   r   r  r   r=   r=   rI   r>   rs     s^    




Drs   c                       s   e Zd Zdejdeddf fddZddd	ee de	e
ef def fd
dZded fddZdejfddZded fddZdefddZdedefddZ  ZS )MethodWrapperVariablemethod_wrapperrB   rC   Nc                    r   rE   )rF   rG   r  )rH   r  rB   rI   r=   r>   rG     r   zMethodWrapperVariable.__init__rT   r8   rx   c           	         s>  t | jr)t|d tjr)t|dkrt|dkst|d |d || jjj	S | jj}| jj	}|dkrNt
|j}|tju rMtt|||g||S ntjdkr|t
jd u r|dkrddlm} t|dkr|sz||| |d  W S  ty   tt| Y n	 ty   Y nw td	d
|  d| d| dddgtjd no|t
jd u r|dks|t
jd u r|dkrddlm} t|dkr|sz||| |d  W S  ty   Y nw |t
jd u rdnd}td| dd
|  d| d| d| dd| dd| dgtjd t |||S )Nr   r,   z2tensor attribute getter takes exactly one argumentrG   )r      r2  rq  )SourcelessBuilderz9unsupported type.__dict__['__annotations__'].__get__ callzcall_function , args: 
, kwargs: z{`torch.compile` only supports calling type.__dict__['__annotations__'].__get__ on a single constant argument (i.e. a type).zpMake sure your call to type.__dict__['__annotations__'] only has one positional argument (no keyword arguments).zMake sure the argument to type.__dict__['__annotations__'] is a constant (i.e. type). For example, `object`, `int`, `MyCustomClass`.rW   rb   rh   zunsupported type.__dict__['z'].__get__ callz5`torch.compile` only supports calling type.__dict__['z7'].__get__ on a single constant argument (i.e. a type).z&Make sure your call to type.__dict__['zC'].__get__ only has one positional argument (no keyword arguments).z)Make sure the argument to type.__dict__['zT'].__get__ is a constant (i.e. type). For example, `object`, `int`, `MyCustomClass`.)r&   r  r   r   TensorVariablerg   r.   rw   r   r:   r`   rG   r   rM   r   sysversion_inforh   rm  r  rv   r\   r   r   r-   r   r   r   rF   r   )	rH   rT   rx   rB   self_objwrapper_namefn_objr  r   rI   r=   r>   r     s   




	



z#MethodWrapperVariable.call_functionTc                 C      dS NTr=   r   r=   r=   r>   r       z(MethodWrapperVariable.is_python_constantc                 C   r  rL   )r  r   r=   r=   r>   r\     r   z(MethodWrapperVariable.as_python_constantc                 C   r  r  r=   r   r=   r=   r>   is_python_hashable  r  z(MethodWrapperVariable.is_python_hashablec                 C      t |  S rL   hashr\   r   r=   r=   r>   get_python_hash     z%MethodWrapperVariable.get_python_hashotherc                 C      t |to|  | kS rL   r   r/   r\   rH   r  r=   r=   r>   is_python_equal      
z%MethodWrapperVariable.is_python_equal)r:   r;   r<   r   MethodWrapperTyper   rG   r   r/   r   r   r   r   r  r\   r  intr  r   r   r  r   r=   r=   rI   r>   r    s     
cr  c                       sh   e Zd Zdejdeddf fddZddd	edef fd
dZ	de
d fddZdejfddZ  ZS )GetSetDescriptorVariabledescrB   rC   Nc                    r   rE   )rF   rG   r  )rH   r  rB   rI   r=   r>   rG     r   z!GetSetDescriptorVariable.__init__rT   r8   rU   c                    s:   |dkr| j rt| j d}t|| jj|S t ||S )Nrq  )r^   r   r/   r   r  rq  rF   rw   r  rI   r=   r>   rw     s   z$GetSetDescriptorVariable.var_getattrTc                 C   r  r  r=   r   r=   r=   r>   r    r  z+GetSetDescriptorVariable.is_python_constantc                 C   r  rL   )r  r   r=   r=   r>   r\     r   z+GetSetDescriptorVariable.as_python_constant)r:   r;   r<   r   GetSetDescriptorTyper   rG   r   r/   rw   r   r  r\   r   r=   r=   rI   r>   r    s
    r  c                       s   e Zd ZddhejZdejdeddf fddZde	ej fdd	Z
dejfd
dZdefddZdddedefddZdddedefddZ  ZS )PythonModuleVariablera   is_torchrB   rC   Nc                    s6   t  jdi | || _| jtu p| jjd| _d S )Nztorch.r=   )rF   rG   ra   r   r:   
startswithr  rH   ra   rB   rI   r=   r>   rG   !  s   zPythonModuleVariable.__init__c                 C   r   rL   )r   
ModuleTyper   r=   r=   r>   r]   &  r   z PythonModuleVariable.python_typec                 C   r  rL   ra   r   r=   r=   r>   r\   )  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__rT   r8   rU   c                 C   s   t | j|}tj|S rL   )r   ra   r   r1   rv   rH   rT   rU   r   r=   r=   r>   call_obj_hasattr/  s   z%PythonModuleVariable.call_obj_hasattrc                 C   s   |j j| |r|j j| |S d }| js|| jjvr2zt| j|}W n ty1   t	t| Y nw | jj| }| j
o@t| j
|}t|||S rL   )r   r   r   r   r  ra   rh   re   r   r   r^   r   r/   r   )rH   rT   rU   r   r^   r=   r=   r>   rw   5  s   z PythonModuleVariable.var_getattr)r:   r;   r<   r/   r   r   r  r   rG   r`   r]   r\   r   r  r1   r  rw   r   r=   r=   rI   r>   r    s$    
r  c                
       s   e Zd Zdededdf fddZddd	ed
ee deeef def
ddZ	ddd	edefddZ
defddZd fddZded fddZdefddZdedefddZ  ZS )TypingVariablera   rB   rC   Nc                    r   rE   rF   rG   ra   r  rI   r=   r>   rG   G  r   zTypingVariable.__init__rT   r8   rU   rx   c              
   C   s   |dkrt |dkr| j|d   }t|S |dkr8t |dkr8|s8t|d to1| j|d jk}tj|S tdd| j d| d| d	| d
| d| j dd| d| j dgt	j
d d S )Nr  r,   r   r   z,unsupported method call on `typing` variableztyping variable: z, method name: r  r  z.`torch.compile` does not support method call `z` on `typing` variable f.zAvoid calling the z method on rW   )rg   ra   r\   r  r'   r   r1   rv   r   r   r   )rH   rT   rU   rx   rB   
new_typingr   r=   r=   r>   r   K  s    
zTypingVariable.call_methodc                 C   s|   ddl m}m} |tv rt| |S |jj| |r"|jj	| |S t
| j|}| jr8t| j|}||||S |||S )Nr,   )r  VariableBuilder)rm  r  r  r$   r   rs   r   r   r   r   re   ra   r^   r   rv   )rH   rT   rU   r  r  ra   r   r=   r=   r>   rw   d  s   zTypingVariable.var_getattrc                 C   r  rL   r  r   r=   r=   r>   r\   t  r   z!TypingVariable.as_python_constantrK   r7   c                    s0   t | jtjst |S ||| j d S rL   )r   ra   r   GenericAliasrF   rS   append_outputcreate_load_constrQ   rI   r=   r>   rS   w  s   zTypingVariable.reconstructTc                 C   r  r  r=   r   r=   r=   r>   r    r  z!TypingVariable.is_python_hashablec                 C   r  rL   r  r   r=   r=   r>   r    r  zTypingVariable.get_python_hashr  c                 C   r  rL   r  r  r=   r=   r>   r    r  zTypingVariable.is_python_equalr   )r:   r;   r<   r   rG   r   r   r/   r   r   rw   r\   rS   r   r  r  r  r   r   r  r   r=   r=   rI   r>   r  F  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   rh   callablere   )r  np_fn_to_tnp_fnnp_modtnp_modr*  tnp_fnnp_fnr=   r=   r>   get_np_to_tnp_map  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>  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ededdf fddZe	de
jdefd	d
Ze	de
jdef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e deeef def
ddZdefddZdef fddZded fddZdefddZdede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.
    ra   rB   rC   Nc                    r   rE   r  r  rI   r=   r>   rG     r   zNumpyVariable.__init__r-  c                 C   s:   |j d}t|dkr|d d ddgksJ || jv S Nr  r   r   _numpy)r;   splitrg   constant_fold_functionsr   r-  modr=   r=   r>   can_constant_fold_through     $
z'NumpyVariable.can_constant_fold_throughc                 C   s:   |j d}t|dkr|d d ddgksJ t|S r  )r;   r  rg   np_constant_collections_mapri   r  r=   r=   r>    get_constant_collection_for_func  r  z.NumpyVariable.get_constant_collection_for_funcrT   r8   rx   c           	      C   s  t jstdd| j d| d| d| j ddgd d	d
lm} ddlm} t 	| j}|d u rPtdd| j d| d| d| d	d| j dg t
jd |d usVJ | | }d urz|| jdd |D i dd | D W S  ty   tdd| j d| d| d| d	d| j dg t
jd Y d S w |jdkrt jrtdd| j d| d| d| d	d| j ddd| j dgd ||j||\}}| |rt||rtj|  d d |D i d!d | D S |jjd"||gt||R  }|||S )#Nz?attempted to trace numpy function with config.trace_numpy=Falsenumpy function: r  r  z"Attempted to trace numpy function z; while `torch._dynamo.config.trace_numpy` was set to False.zHSet `torch._dynamo.config.trace_numpy` to True to trace numpy functions.rW   r   )numpy_to_tensor_wrapperr,   )NumpyNdarrayVariablez8attempted to trace numpy function unsupported by PyTorchz  (corresponding torch function: r  z Can't find numpy numpy function z in torch._numpy.c                 S      g | ]}|  qS r=   r\   r  r=   r=   r>   
<listcomp>  r  z/NumpyVariable.call_function.<locals>.<listcomp>c                 S      i | ]	\}}||  qS r=   r  r  r=   r=   r>   r        z/NumpyVariable.call_function.<locals>.<dictcomp>zTnumpy function that produces a const collection type encountered non-const argumentsznumpy function zs that produces a const collection type (e.g. np.dtype, np.iinfo/np.finfo) received arguments that are not constant.ztorch._numpy.randomzXattempted to trace torch._numpy.random function with config.use_numpy_random_stream=TruezAttempted to trace zD when `torch._dynamo.config.use_numpy_random_stream` is set to True.z<Set `torch._dynamo.config.use_numpy_random_stream` to False.zAvoid calling r  c                 S   r  r=   r  r  r=   r=   r>   r  $  r  c                 S   r  r=   r  r  r=   r=   r>   r  %  r  r   )r   trace_numpyr   ra   r  r  tensorr  r  ri   r   r   r  r   r-   
USER_ERRORr;   use_numpy_random_stream
patch_argsr:   r  r#   r   r1   rv   r\   r   ro  r)   )	rH   rT   rx   rB   r  r  r   collection_variable_typrk  r=   r=   r>   r     s   



zNumpyVariable.call_functionrU   c                 C   s.   t dd| j d| d| dg tjd d S )Nz/attempted to trace numpy.* function as a methodr   r  r  z6Tracing numpy.* functions as methods is not supported.rW   )r   ra   r   r   r   r=   r=   r>   r   1  s   
zNumpyVariable.call_methodc                 C   r  rL   r  r   r=   r=   r>   r\   A  r   z NumpyVariable.as_python_constantc                    s8   t jrt| jtjr| jS t| jtr| jjS t 	 S rL   )
r   r  r   ra   enumEnumMetar`   r:   rF   r  r   rI   r=   r>   r  D  s   
zNumpyVariable.as_proxyTc                 C   r  r  r=   r   r=   r=   r>   r  R  r  z NumpyVariable.is_python_hashablec                 C   r  rL   r  r   r=   r=   r>   r  U  r  zNumpyVariable.get_python_hashr  c                 C   r  rL   r  r  r=   r=   r>   r  X  r  zNumpyVariable.is_python_equal) r:   r;   r<   r  tnp
issubdtyper  r   rG   r   r   r   r   r  r  r   r/   r   r   r   r   r   r   r\   r  r   r  r  r  r   r  r   r=   r=   rI   r>   r    sB    

[

r  c                       s>   e Zd Zdeddf fddZdefddZdd
dZ  ZS )NullVariablerB   rC   Nc                    r4  rE   r5  r6  rI   r=   r>   rG   a  r  zNullVariable.__init__c                 C   r  )Nr  r=   r   r=   r=   r>   r  d  r  zNullVariable.__repr__rK   r7   c                 C   s2   t jdk rtdddg tjd |td d S )N)r      z0cannot reconstruct NullVariable in Python < 3.11rV   zdAttempted to generate PUSH_NULL instruction in Python < 3.11; where this instruction does not exist.rW   	PUSH_NULL)r  r  r   r   r   r  r   rQ   r=   r=   r>   rS   g  s   
	zNullVariable.reconstructr   )	r:   r;   r<   r   rG   r   r  rS   r   r=   r=   rI   r>   r  `  s    r  c                   @   r  )r   z"Marker used to implement delattr()Nr  r=   r=   r=   r>   r   u  r  r   c                
       s   e Zd ZdZdhejZededee de	eef defddZ
dedee de	eef dedd	f
 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_stringsym_args
sym_kwargsrC   c                 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 rL   )r  r  r=   r=   r>   r    s
    
z.StringFormatVariable.create.<locals>.<genexpr>c                 S   r  r=   r  )r  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   r1   rv   formatr   r   r   )r   r  r  r  r=   r=   r>   rv     s   zStringFormatVariable.createrB   Nc                    s6   t  jdi | t|tsJ || _|| _|| _d S rE   )rF   rG   r   r   r  r  r  )rH   r  r  r  rB   rI   r=   r>   rG     s
   
zStringFormatVariable.__init__c                 C   s&   | j j d| jd| jd| jdS r  )rJ   r:   r  r  r  r   r=   r=   r>   r    s   &zStringFormatVariable.__repr__rK   r7   c                    sb    j  fdddd  ttj dd j D } t|  t	dd d S )Nc                      s      j dgS )Nr  )rP   r  r  create_load_attrr=   r  r=   r>   rN     s
    
z2StringFormatVariable.reconstruct.<locals>.<lambda>T)call_function_exc                 S   s   i | ]\}}t j||qS r=   r   r1   rv   r  r=   r=   r>   r    s    z4StringFormatVariable.reconstruct.<locals>.<dictcomp>F)
rO   r   r   r   r  r  r   ConstDictVariablerP   r   )rH   rK   rB   r=   r  r>   rS     s   	z StringFormatVariable.reconstructr   )r:   r;   r<   r  r/   r   r   r   r   r   rv   r   rG   r  rS   r   r=   r=   rI   r>   r  y  s4    

r  c                       s<   e Zd Zdededdf fddZdee fddZ  ZS )	ObjectVariablera   rB   rC   Nc                    r   rE   r  r  rI   r=   r>   rG     r   zObjectVariable.__init__c                 C   s   t S rL   )r   r   r=   r=   r>   r]     r  zObjectVariable.python_type)	r:   r;   r<   r   r   rG   r`   r]   r   r=   r=   rI   r>   r"    s    r"  c                	       s   e Zd ZdZdededdf fddZededeej	ej
B  fd	d
Zdddee deeef ddfddZdddZededee deeef defddZ  ZS )DebuggingVariablez
    Represents a call to a debugging function like print(), or something
    registered to config.reorderable_logging_functions.
    ra   rB   rC   Nc                    r   rE   r  r  rI   r=   r>   rG     r   zDebuggingVariable.__init__r   c                 C   s(   t | ot| tjtjfo| tjjjv S rL   )	r  r   r   r   r   r   rc  r   reorderable_logging_functionsr   r=   r=   r>   is_reorderable_logging_function  s
   z1DebuggingVariable.is_reorderable_logging_functionrT   r8   rx   c                 C   sb   |j rd S | | j||s%tdd| j d| d| dd| j dgd |j| t|f d S )	NzJattempted to reorder a debugging function that can't actually be reorderedzfn: r  r  zl`torch.compile` can only reorder functions where the arguments are Tensors, constants, or string formatters.z#Avoid calling the logging function z" with args that are not supported.rW   )exportcan_reorder_logsra   r   debug_localsr  r   rh  r=   r=   r>   r     s   
zDebuggingVariable.call_functionrK   r7   c                 C   s   | j d usJ | j |S rL   )r^   rS   rQ   r=   r=   r>   rS     s   zDebuggingVariable.reconstructr-  c                 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  r1   r  pytreetree_leavesr   )r-  rx   rB   allowed_input_types	flat_argsr  r=   r=   r>   r'    s   
z"DebuggingVariable.can_reorder_logsr   )r:   r;   r<   r  r   rG   r   r
   r   r   r   r%  r   r/   r   r   r   rS   r   r'  r   r=   r=   rI   r>   r#    s*    



0r#  c                       sT   e Zd ZdZdededdf fddZdd	d
ee dee	ef defddZ
  ZS )IgnoredFunctionVariablezL
    Represents a call to an arbitrary function that should be ignored.
    ra   rB   rC   Nc                    r   rE   r  r  rI   r=   r>   rG   	  r   z IgnoredFunctionVariable.__init__rT   r8   rx   c                 C   r   rL   )r   r0   rh  r=   r=   r>   r        z%IgnoredFunctionVariable.call_functionr:   r;   r<   r  r   rG   r   r/   r   r   r   r   r=   r=   rI   r>   r-    s    
r-  c                
       sZ   e Zd ZdZdejdeddf fddZdd	d
ede	e
 deee
f de
f
ddZ  ZS )LoggingLoggerVariablez:
    Represents a call to any logging.Logger methods.
    ra   rB   rC   Nc                    r   rE   r  r  rI   r=   r>   rG     r   zLoggingLoggerVariable.__init__rT   r8   rU   rx   c              
   C   sv   |j rtjS t| j|d }t|dd }tjjj}||v s ||v r#tjS t	dd| j d| d| d| ddgd	 d S )
Nr   z8logging.Logger method not supported for non-export caseszmethod: r  r  r  z>logging.Logger methods are not supported for non-export cases.zJAdd the logging method to `torch._dynamo.config.ignore_logging_functions`.rW   )
r&  r   r0   re   ra   r   rc  r   ignore_logging_functionsr   )rH   rT   rU   rx   rB   methodr  
ignore_setr=   r=   r>   r     s   

z!LoggingLoggerVariable.call_method)r:   r;   r<   r  loggingLoggerr   rG   r   r   r/   r   r   r   r=   r=   rI   r>   r0    s    
r0  c                
       s   e 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ededd	f fd
dZedefddZdefddZddde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=   ra   rB   rC   Nc                    r   rE   r  r  rI   r=   r>   rG   L  r   zConstantLikeVariable.__init__c                 C   s,   t | j}|jdkr|jS |j d|j S )z4Dynamically compute the prefix from the value's typer}   r  )r`   ra   r;   r<   )rH   tr=   r=   r>   _error_prefixP  s   

z"ConstantLikeVariable._error_prefixc                 C   r  rL   r  r   r=   r=   r>   r\   [  r   z'ConstantLikeVariable.as_python_constantrT   r8   rU   rx   c                 C   s  g i }}zdd |D }dd |  D }W n' ty>   td| j d| d| d| d	d
| j d| ddgd Y nw t| j||i |}tj|rVtj	|S t
|tjr`t|S td| j d| d| d| d| 	d
| j d| d| dg tjd d S )Nc                 S   r  r=   r  r  r=   r=   r>   r  i  r  z4ConstantLikeVariable.call_method.<locals>.<listcomp>c                 S   r  r=   r  r  r=   r=   r>   r  j  r  z4ConstantLikeVariable.call_method.<locals>.<dictcomp>z0constant-like method call with non-constant argsr  z(*z, **r  zAttempted to call z with non-constant args.zFEnsure that the args to the method call are constant (int, str, etc.).rW   z6constant-like method call with unsupported return typez) returned z, got unsupported return value )r   r   r   r<  re   ra   r   r1   rr   rv   r   reMatchr6  r   r   )rH   rT   rU   rx   rB   cargsckwargsr   r=   r=   r>   r   ^  s4   


 
z ConstantLikeVariable.call_methodc                 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 rL   )re   ra   r   np_floatingfloatnp_dtypeNumpyDTypeVariabler`   r_   
np_genericr  r   r1   rr   rv   rs   r  r=   r=   r>   rw     s   
z ConstantLikeVariable.var_getattr)r:   r;   r<   r  numpyr7  rC  r8  rA  r9  rE  ImportErrorr`   r   rG   propertyr   r<  r\   r   r/   r   r   rw   r   r=   r=   rI   r>   r6  =  s0    


'r6  c                       s*   e Zd ZdZdeddf fddZ  ZS )TorchVersionVariableztorch.__version__rB   rC   Nc                    s6   | dtj |d tju sJ t jdi | d S )Nra   r=   )
setdefaultr   __version__rF   rG   r6  rI   r=   r>   rG     s   zTorchVersionVariable.__init__)r:   r;   r<   r<  r   rG   r   r=   r=   rI   r>   rI    s    rI  c                   @   s   e Zd ZdefddZdS )rD  rC   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).
        )ra   r`   r:   r   r=   r=   r>   r    ri  zNumpyDTypeVariable.as_proxyN)r:   r;   r<   r   r  r=   r=   r=   r>   rD    s    rD  c                       sP   e Zd ZdZdeddf fddZddd	ee dee	ef dd
fddZ
  ZS )RandomClassVariablezrandom.RandomrB   rC   Nc                    r4  rE   r5  r6  rI   r=   r>   rG     r  zRandomClassVariable.__init__rT   r8   rx   RandomVariablec                 C   s^   t |dks|rtdd| d| dg tjd t |dkr"tjn|d }t|tj dS )	Nr,   z'random.Random() with improper argumentszargs: r  z=random.Random() with > 1 arg or with kwargs is not supported.rW   r   )seedr   )	rg   r   r   r
  r   r0   rM  baseValueMutationNew)rH   rT   rx   rB   rN  r=   r=   r>   r     s   
z!RandomClassVariable.call_functionr/  r=   r=   rI   r>   rL    s    
rL  c                
       sL  e Zd ZdZdhejZh dZ		d$dejdB dedB de	ddf fd	d
Z
deej fddZdejfddZededefddZedeeeedf edB f ddfddZedeeeedf edB f defddZededeeeedf edB f fddZdddedee deeef def
 fddZd%d"d#Z  ZS )&rM  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>   rQ  randintuniform	randrangeNrandrN  rB   rC   c                    sj   t  jdi | |d ur#| |sJ t | _| j|  d S |d ur+| nd }t|| _d S rE   )rF   rG   is_supported_random_objrQ  r   setstategetstater\   )rH   rU  rN  rB   rI   r=   r>   rG     s   
zRandomVariable.__init__c                 C   r   rL   )rQ  r   r   r=   r=   r>   r]     r   zRandomVariable.python_typec                 C   r  rL   )rQ  r   r=   r=   r>   r\     r   z!RandomVariable.as_python_constantr   c                 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)rN  rX  rW  r   T)r`   rQ  r   r  r  rM  _supported_fn_namesr   re   rV  	isbuiltinrq  )r   rU   methr=   r=   r>   rV    s    


z&RandomVariable.is_supported_random_objstate.c                 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 rL   )r`   r  r  r=   r=   r>   r  		  s    z-RandomVariable.check_state.<locals>.<genexpr>r   )r`   r   r  r  rB  r\  r=   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   r  r=   r=   r>   r  	  s    z-RandomVariable.wrap_state.<locals>.<listcomp>r,   r   )rM  r^  r   r   r1   rv   r]  r=   r=   r>   
wrap_state	  s   
zRandomVariable.wrap_statec                 C   s   |   }t| |S rL   )r\   rM  r^  )r\  	state_objr=   r=   r>   unwrap_state	  s   
zRandomVariable.unwrap_staterT   r8   rU   rx   c                    s   dkr#|j j|  | jjdd |D i dd | D  tjS  dkr/| | j	 S  dkrH|j j|  | j
| |d  tjS  | jv r|j j|  | j	 d	td
tdtf fdd}t| j dd |D i dd | D  t||||S t | ||S )NrN  c                 S   r  r=   r  r  r=   r=   r>   r  +	  r  z.RandomVariable.call_method.<locals>.<listcomp>c                 S   r  r=   r  )r  r  r   r=   r=   r>   r  ,	  r  z.RandomVariable.call_method.<locals>.<dictcomp>rX  rW  r   rx   rB   rC   c                     s&   t  }| t| | i |S rL   )rQ  r   rW  re   )rx   rB   rrU   r\  r=   r>   call_random_meth9	  s   
z4RandomVariable.call_method.<locals>.call_random_methc                 S   r  r=   r  r  r=   r=   r>   r  A	  r  c                 S   r  r=   r  r  r=   r=   r>   r  B	  r  )r   r   mutationrQ  rN  r   r   r0   r_  rX  rW  ra  rY  r   re   r4   rF   r   )rH   rT   rU   rx   rB   rd  rI   rc  r>   r   !	  s2   


zRandomVariable.call_methodrK   r7   c                    s\      fdd  dd     d  | | j   dd    d S )Nc                      s      t dgS )Nr   )rP   create_load_python_modulerQ  r  r=   rR   r=   r>   rN   J	  s
    z,RandomVariable.reconstruct.<locals>.<lambda>r   FrW  r,   T)rO   r   r  r   r_  rQ  rX  pop_toprQ   r=   rR   r>   rS   H	  s   

zRandomVariable.reconstructr  r   )r:   r;   r<   r  r/   r   rY  rQ  r   r   rG   r`   r]   r\   r   r   rV  r   r  rB  r^  r   r_  ra  r   r   r   r   rS   r   r=   r=   rI   r>   rM    sX    	**
'rM  c                       s   e Zd Zedddeje dedB dedd f
dd	Zd
e	de	deddf fddZ
dddee	 deee	f de	fddZdddZdefddZdefddZdedefddZ  ZS )WeakRefVariablerT   r8   weakref_valuer^   NoptionsrC   c           	      K   sh   |d usJ |j }|ot|d}t| ||}| }|ot|}t| ||}||d< t||fi |S )N__callback__r^   )rk  r   r/   r   r"   rh  )	rT   ri  r^   rj  callbackcallback_sourcecallback_vtreferentreferent_vtr=   r=   r>   r   \	  s   zWeakRefVariable.buildrp  rn  c                    rD   rE   )rF   rG   rp  rn  )rH   rp  rn  rj  rI   r=   r>   rG   n	  s   
zWeakRefVariable.__init__rx   rB   c                 C   r  rL   )rp  rh  r=   r=   r>   r   u	  r.  zWeakRefVariable.call_functionrK   r7   c                    s:      fdd  | j  | j  tdd d S )Nc                      s     ddS )Nweakrefref)r  r=   rR   r=   r>   rN   ~	  s    z-WeakRefVariable.reconstruct.<locals>.<lambda>r   F)rO   rp  rn  rP   r   rQ   r=   rR   r>   rS   }	  s   

zWeakRefVariable.reconstructc                 C   
   | j  S rL   )rp  r  r   r=   r=   r>   r  	  r  z"WeakRefVariable.is_python_hashablec                 C   rs  rL   )rp  r  r   r=   r=   r>   r  	  r  zWeakRefVariable.get_python_hashr  c                 C   s   t |tsdS | j|jS )NF)r   rh  rp  r  r  r=   r=   r>   r  	  s   
zWeakRefVariable.is_python_equalr   )r:   r;   r<   r   rq  ReferenceTyper   r   r   r/   rG   r   r   r   r   rS   r   r  r  r  r   r  r   r=   r=   rI   r>   rh  [	  sF    


rh  )r  rx  r  r  rV  r  r4  rQ  r=  r  r   r   rq  collections.abcr   r   r   r   typingr   r   r   r	   r
   r   torch._Cr   torch._numpyr  r  torch.utils._pytreer  _pytreer)  torch._dynamo.variables.baser   torch._dynamo.variables.listsr   torch._guardsr   rV   r   r   r   r   bytecode_transformationr   r   r   create_parameter_opr   r   r   r   guardsr   r   mutation_guardr   r^   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   rO  r-   r.   r/   r  r0   r1   	functionsr2   r3   user_definedr4   r5   r6   torch._dynamo.codegenr7   torch._dynamo.symbolic_convertr8   r9   r?   r   r   r   r  r   r!  r/  r3  r<  r>  	dataclassru  rU  r  r   rs   r  r  r  r  	lru_cacher   r   r  r  r  r  r   r  r"  r#  r-  r0  r6  rI  rD  finfoiinfor7  r  rL  rM  rh  r=   r=   r=   r>   <module>   s      ,  ]c B	 d 5 {,
Z
	 "<
E'V	 