a
    h                    @   s4  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mZmZ ddlmZmZ ddl
mZmZmZ ddlZddlmZmZ ddlmZ ddlmZ d	d
lmZmZmZm Z  d	dl!m"Z"m#Z#m$Z$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/m0Z0m1Z1m2Z2m3Z3 d	dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZD ddlEmFZFmGZGmHZH ddlImJZJ ddlKmLZLmMZM ddlNmOZOmPZPmQZQmRZRmSZSmTZT ddlUmVZVmWZWmXZXmYZYmZZZm[Z[ ddl\m]Z]m^Z^m_Z_m`Z`maZa ddlbmcZcmdZd er6ddlemfZf ddlgmhZh eiejZkejlejmejnejoejpejqejrejsejtejuejvejwejxejxejyejzej{ej|ej}ej~ejejejejejejiZede
je
jgeHf ZeeeH eeeH df f ZejejejejejejejejejejejejiZG dd deHZejdd ZdS ) a\  
Built-in function and type variable tracking for TorchDynamo's symbolic execution.

This module contains variable tracker classes for Python built-in functions, types,
and operations during graph compilation. It handles symbolic execution of:

- Built-in functions (len, getattr, isinstance, etc.)
- Type constructors (int, float, str, list, dict, etc.)
- Built-in operators and methods
- Special Python constructs (super, hasattr, etc.)

Key classes:
- BuiltinVariable: Tracks built-in functions and handles their execution
- TypeVariable: Manages type constructor calls and type checking
- SuperVariable: Handles super() calls in class hierarchies

These variable trackers ensure that built-in Python operations are correctly
handled during symbolic execution, either by executing them directly when safe
or by creating appropriate graph nodes when needed.
    N)defaultdictOrderedDict)KeysViewSequence)CallableTYPE_CHECKINGUnion)	sym_floatsym_int)is_sparse_any)is_traceable_wrapper_subclass   )configgraph_break_hints	polyfills	variables)AttributeMutationErrorObservedAttributeErrorraise_observed_exceptionunimplemented_v2Unsupported	UserErrorUserErrorType)GuardBuilderinstall_guard)DummyModule)
AttrSourceGetItemSourceGlobalSourceis_constant_source
TypeSource)check_constant_argscheck_numpy_ndarray_argscheck_unspec_or_constant_argscheck_unspec_python_argscmp_name_to_op_mappingdict_methodsextract_fake_example_valueget_fake_valueguard_if_dynis_tensor_getset_descriptoris_wrapper_or_member_descriptoristypenumpy_operator_wrapperproxy_args_kwargsstr_methodstensortype_to_dtype   )#AsPythonConstantNotImplementedErrorValueMutationNewVariableTrackerConstantVariable)EventVariableStreamVariable)ConstDictVariableDefaultDictVariableDictViewVariableFrozensetVariableis_hashableSetVariable)BaseListVariableListIteratorVariableListVariableSizeVariableTupleIteratorVariableTupleVariable)FakeItemVariablesupported_comparison_opsSymNodeVariableTensorVariableUnspecializedPythonVariable)UserDefinedObjectVariableUserDefinedVariable)	PyCodegen)InstructionTranslatorrM   .c                	       s  e Zd ZU dZe ZdhejZedd Z	e
ejdd Zdd Ze
ejd	d
 Ze
ejeedef eee edef f f dddZe
ejdd Ze
dd Zdd Zdd fddZedddZdd Z fddZddd d!Zd"d# Zd$d% Zd&d' Z d(d) Z!e
d*d+ Z"d,d- Z#e
ee$ e%d.d/d0Z&d1d2d3d4Z'i Z(eeedf ed1e)e eeef gef f e*d5< d1e)d6 d7d6d8d9d:Z+d;d7d6d< fd=d>Z,d1d2d?d@Z-e-Z.e-Z/d1d2dAdBZ0d1d2dCdDZ1d1d2dEdFZ2d1d2dGdHZ3d1d2dIdJZ4e2Z5e2Z6d1d6dKdLdMZ7d1d6dKdNdOZ8d1d6dKdPdQZ9d1d2dRdSZ:d1d2dTdUZ;dVdW Z<d1d2dXdYZ=d1d2dZd[Z>dd1d2d\d]Z?d^d_ Z@dd`daZAd1d2dbdcZBeAZCeAZDd1d2dddeZEdfdg ZFd1d2dhdiZGe
d1d2djdkZHe
d1d2dldmZId1d2dndoZJd1d2dpdqZKd1d2drdsZLd1d2dtduZMd1d2dvdwZNd1d2dxdyZOd1d2dzd{ZPd1d2d|d}ZQd1edKd~dZRd1d2ddZSd1d2ddZTd1d2ddZUdd1eedddZVd1eeedddZWd1eedddZXd1edddZYd1edddZZd1eedddZ[d1d2ddZ\d1d2ddZ]d1d2ddZ^d1d2ddZ_d1d2ddZ`d1d2ddZad1d2ddZbebZcd1d2ddZdedZed1d2ddZfd1eedddZg  ZhS )BuiltinVariablea  
    A VariableTracker that represents a built-in value (functions and operators).
    A lot of the code here assumes it will be a function object.

    The BuiltinVariable class wraps Python built-in functions (like len, isinstance, etc.)
    and operators (like +, -, *, etc.) to enable symbolic execution during tracing. This allows
    Dynamo to properly handle these operations when converting Python code to FX graphs while
    maintaining correct semantics and enabling optimizations.
    fnc                 C   s   t |tj | ||dS )Nsource)r   
make_guardr   ZBUILTIN_MATCH)clsvaluerQ    rU   M/var/www/auris/lib/python3.9/site-packages/torch/_dynamo/variables/builtin.pycreate_with_source   s    z"BuiltinVariable.create_with_sourcec               8   C   s   t ttttttttt	t
ttttttttjtttj tjtjtjtjtjtjtjtjtjtjtjtj tj!tj"tj#tj$tj%tj&tj'tj(tj)tj*tj+tj,tj-tj.tj/tj0tj1tj2tj3tj4tj5tj6h8} ddl7m8} | 9|:  | 9dd t;j<: D  | S )Nr1   )rF   c                 s   s"   | ]}t |ttjr|V  qd S N)
isinstancetypemathsqrt.0xrU   rU   rV   	<genexpr>       z;BuiltinVariable._constant_fold_functions.<locals>.<genexpr>)=absallanyboolcallablechrdivmodfloatgetattrintlenmaxminordpowreprroundstrformatsumrZ   operatorposnegnot_truthinvertmulmatmulfloordivtruedivmodaddsubgetitemlength_hintlshiftrshiftand_or_xoripowimulimatmul	ifloordivitruedivimodiaddisubilshiftirshiftiandixoriorindextensorrF   updatevaluesr[   __dict__)fnsrF   rU   rU   rV   _constant_fold_functions   sz    :z(BuiltinVariable._constant_fold_functionsc                 C   s   | j |  v S rX   )rO   r   selfrU   rU   rV   can_constant_fold_through   s    z)BuiltinVariable.can_constant_fold_throughc               '   C   s   t jt jt jt jt jt jt jt jt j	t j
t jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt jt j t j!t j"t j#t j$t j%t j&t j'h'} | S rX   )(rv   rb   rw   rx   ry   r{   rp   r|   r}   r~   r   r   r   ltgtgeleneeqr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   rU   rV   _fx_graph_functions   sR    )z#BuiltinVariable._fx_graph_functions.)returnc                  C   s   t jg dt jft jg dt jft jg dt jft jg dt jft j	g dt j
ft jg dt jftg dt jft jg dt jft jg dt jft jg d	t jfi
} | S )
N)__add____radd____iadd__)__sub____rsub____isub__)__mul____rmul____imul__)__truediv____rtruediv____itruediv__)__floordiv____rfloordiv____ifloordiv__)__mod____rmod____imod__)__pow____rpow____ipow__)
__lshift____rlshift____ilshift__)
__rshift____rrshift____irshift__)rv   r   r   r   r   r|   r   r   r   r~   r   r   r   rp   r   r   r   r   r   r   rU   rU   rV   _binops  s0    zBuiltinVariable._binopsc               
      s  ddl mm ddlm ddlm ddlm  ddl	m
mm i } t  D ]\}\}}g | |< g | |< |\}}}||dfdd	
}| | tf|f | | tf|f |d
dddd}| | tf|f | | tf|f |ddddd}	| | ttf|	f | | ttf|	f | | ttf|	f | | ttf|	f qXdddd}
dddd}ttf|fttf|fttf|fttf|
fttf|
fttfdd fttttffdd fttfdd fg}| tj | dddd}ttf|fttf|
fttf|
fg}| tj | dddd}ttf|fttf|fttf|fttf|fg}| tj |  fdd}t D ].}t |sJ || vsJ ||| |< q| S ) Nr1   )BaseUserFunctionVariableUserFunctionVariableNNModuleVariable)supported_const_comparison_ops)BaseTorchVariable)UserDefinedClassVariablerJ   rK   )forward_namereverse_namec                   s2   t | r|| ||gi S || ||gi S d S rX   )rY   call_method)txabr   r   )rK   rU   rV   user_defined_handlern  s    
z=BuiltinVariable._binop_handlers.<locals>.user_defined_handler)r   rM   r   c                S   s   | | ||gi S rX   r   )r   r   r   r   rU   rU   rV   user_defined_inplace_handler  s    zEBuiltinVariable._binop_handlers.<locals>.user_defined_inplace_handlerrO   c                S   s2   ddl m} || | jjd|gt||gi R  S Nr1   wrap_fx_proxycall_function)builderr   outputcreate_proxyr.   )r   r   r   rO   r   rU   rU   rV   dynamic_handler  s    z8BuiltinVariable._binop_handlers.<locals>.dynamic_handlerc                 S   s   t g |j|| S rX   )rD   itemsunpack_var_sequencer   r   r   rU   rU   rV   tuple_add_handler  s    z:BuiltinVariable._binop_handlers.<locals>.tuple_add_handlerc                 S   s   t g |j|| S rX   )rB   r   r   r   rU   rU   rV   size_add_handler  s    z9BuiltinVariable._binop_handlers.<locals>.size_add_handlerc                 S   s   t g || |jS rX   )rD   r   r   r   rU   rU   rV   <lambda>  s   z1BuiltinVariable._binop_handlers.<locals>.<lambda>c                 S   s   t g |j|| t dS Nmutation_type)rA   r   r   r3   r   rU   rU   rV   r     s   c                 S   s   t |g |j|jS rX   )rZ   r   r   rU   rU   rV   r     s   c                 S   s>   |  s|| sd S || }| jj| |j| |S rX   )Zis_immutablehas_unpack_var_sequencer   r   side_effectsZmutationr   extend)r   r   r   seqrU   rU   rV   list_iadd_handler  s    
z:BuiltinVariable._binop_handlers.<locals>.list_iadd_handlerc                 S   s.   t |tr|| }}|j|j|  t dS )N)r   r   )rY   r6   	__class__r   as_python_constantr3   )r   lstconstrU   rU   rV   expand_list_like  s    

z9BuiltinVariable._binop_handlers.<locals>.expand_list_likec           
         s  ddfdd}t t f|fg}tv rt t f|fg}t}|ttf|jfttf|jfg fdd}|ttf|f |S t t f|fg}	 v rj	
drt d  dd fdd	}|}|}ttttf}||t f|ft |f|fg t}|tftfffd
dfffddff|ff|fttt fttt ff|fttf|jfttf|jfttf|jfttf|jfg	 ddfdd}	|ttf|	f |S )NrM   r   c                    s   t  |j|jS rX   )r6   rT   r   oprU   rV   compare_by_value4  s    zYBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.compare_by_valuec                    s   |  t| t  ||gi S rX   )inline_user_function_returnr4   buildpolyfill_fn_mappingr   r   rU   rV   handlerV  s    zPBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.handleris_c                    s   t  S rX   r5   r   )none_resultrU   rV   neverf  s    zNBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.neverc                    s   t  |j|jS rX   )r6   rO   r   r   rU   rV   r     ra   zQBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.<lambda>c                    s"   t  | j|j| j|jS rX   )r6   r   get_submodule
module_keyr   r   rU   rV   r     s
   c                    sp   t |t |ur t jdkS ||u r8t ||S t|tjrlt|tjrl|j|jurlt ||S d S )Nr   )rZ   r6   create__name__r,   r   ExceptionVariableexc_type)r   leftrightr   rU   rV   	handle_is  s    


zRBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.handle_is)r6   r   rN   r   rG   r4   _comparison_with_symnodeappendr   r   
startswithobjectrH   r?   r9   r8   r7   _comparison_with_tensor)
r   r   resultZop_varr   r   Zobj_op_noneZnone_op_objZtypes_that_are_never_noner  r   r   r   r   rJ   rK   r   r   )r   r   rV   create_cmp_op_handlers3  s    


	
7z?BuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers)!	functionsr   r   	nn_moduler   r   r   torchr   user_definedr   rJ   rK   rN   r   r   r  r4   rG   rB   rD   r6   rA   r?   r@   rv   r   r   r   r|   rF   r   rf   )Zop_handlersr   Zmagic_method_namesZin_place_opr   r   Zinplace_namer   r   r   r   r   Zlist_like_addition_handlersr   Zlist_like_iadd_handlersr   Zlist_like_expansion_handlersr  rU   r  rV   _binop_handlersF  s    










=



 zBuiltinVariable._binop_handlersc                 C   sR   t  | }|d u rd S g }|D ]*\\}}}t||r"t||r"|| q"|S rX   )rN   r  get
issubclassr  )r   a_typeb_typehandlersmatchesZtype1Ztype2r   rU   rU   rV   _find_binop_handler  s    z#BuiltinVariable._find_binop_handlerc                 C   s   | j |  v S rX   )rO   r   r   rU   rU   rV   can_insert_in_graph  s    z#BuiltinVariable.can_insert_in_graphNc                    s   t  jf i | || _d S rX   )super__init__rO   )r   rO   kwargsr   rU   rV   r    s    zBuiltinVariable.__init__c                 C   s,   | j d u rd}n| j j}| jj d| dS )NNone())rO   r   r   )r   namerU   rU   rV   __repr__  s    
zBuiltinVariable.__repr__c                 C   s   | j S rX   r   r   rU   rU   rV   r     s    z"BuiltinVariable.as_python_constantc                    s4   t tj ttjttji}| j|v r*|| j S t  S rX   )	re   r  rk   int64ri   float64rO   r  as_proxy)r   ZDTYPEr  rU   rV   r&    s    

zBuiltinVariable.as_proxyrL   )codegenc                 C   sD   | j j}| j jdksJ ||jjvs,J d||j|dd d S )Nbuiltinszshadowed globalT)r   )rO   r   
__module__r   	f_globalsZappend_outputZcreate_load_global)r   r'  r"  rU   rU   rV   reconstruct  s    zBuiltinVariable.reconstructc                 O   s
   t ||S rX   )r!   r   argsr  rU   rU   rV   constant_args  s    zBuiltinVariable.constant_argsc                 G   s4   d}|D ]&}t |tjr dS |p,t |tj}q|S NF)rY   r   GetAttrVariablerH   )r   r-  
any_tensorargrU   rU   rV   tensor_args  s    zBuiltinVariable.tensor_argsc                 C   s4   d}|D ]&}t |tjr dS |p,t |tj}q|S r/  )r  r   r0  rH   )r   	arg_typesr1  arg_typerU   rU   rV   tensor_args_type  s    z BuiltinVariable.tensor_args_typec                 O   s\   g }g }t || D ]&}t|tjr4|| q|| qtdd |D oZ| j| S )Nc                 s   s&   | ]}|j d urt|j ndV  qd S r/  )rQ   r   r^   trU   rU   rV   r`     s   zBBuiltinVariable.python_and_tensor_constant_only.<locals>.<genexpr>)		itertoolschainr   rY   r   rH   r  rc   r.  )r   r-  r  r3  Znon_tensor_argsirU   rU   rV   python_and_tensor_constant_only  s    z/BuiltinVariable.python_and_tensor_constant_onlyc                 C   s    dd | D dd |  D fS )Nc                 S   s   g | ]}|  qS rU   r   r]   rU   rU   rV   
<listcomp>$  ra   z=BuiltinVariable.unwrap_unspec_args_kwargs.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS rU   r=  r^   kvrU   rU   rV   
<dictcomp>$  s   z=BuiltinVariable.unwrap_unspec_args_kwargs.<locals>.<dictcomp>)r   r-  r  rU   rU   rV   unwrap_unspec_args_kwargs"  s    z)BuiltinVariable.unwrap_unspec_args_kwargsc                 C   s   |   ot||S rX   )r   r#   r,  rU   rU   rV   has_constant_handler(  s    
z$BuiltinVariable.has_constant_handler)r4  
has_kwargsc                    s  ddl m  t	g t fddD r:	fddS trlttsVtu rlddfd	d
}|S 		 rt
ju rtd tjr	r	jS r	j tdkrstjgR  snDtdkr\fdd nddfdd}| t	dj d 

rXdd	
fdd}| 	 rtdd D rsddfdd}nddfdd}| fddtdkrևfddS tdkr \ddfdd}nddfdd}|S )Nr1   LazyVariableTrackerc                 3   s   | ]}t | V  qd S rX   )r  r7  rG  rU   rV   r`   4  ra   z0BuiltinVariable._make_handler.<locals>.<genexpr>c                    s     | dd |D |S )Nc                 S   s   g | ]}|  qS rU   realizer^   rA  rU   rU   rV   r>  6  ra   zCBuiltinVariable._make_handler.<locals>.<lambda>.<locals>.<listcomp>)r   r   r-  r  objrU   rV   r   5  s   z/BuiltinVariable._make_handler.<locals>.<lambda>rM   r   c                    sH    t u r4tdd |D s4tdt|dg tjd tj |fi |S )Nc                 s   s&   | ]}t |tjot |jtV  qd S rX   )rY   r   r6   rT   rs   r]   rU   rU   rV   r`   D  s   zWBuiltinVariable._make_handler.<locals>.create_exception_class_object.<locals>.<genexpr>zassert with non-string messagez1Dynamo only supports asserts with string messagesgb_typecontextexplanationhints)AssertionErrorrc   r   rs   r   SUPPORTABLEr   r  rL  r   rU   rV   create_exception_class_objectA  s    zDBuiltinVariable._make_handler.<locals>.create_exception_class_objectr   r   c                    s    | g|R  S rX   rU   )r   r-  _)binop_handlerrU   rV   r   g  ra   c                    s*    D ] }|| g|R  }|r|  S qd S rX   rU   )r   r-  rW  rO   rv)binop_handlersrU   rV   call_binop_handlersj  s    z:BuiltinVariable._make_handler.<locals>.call_binop_handlersZcall_c                    s  z(| g|R i |}|d ur&|W S W n t y   z"tj| g|R i | W nr t y } zZ||}|std| tdd d| d| d j g t	j
d W Y d }~nd }~0 0  Y n@ ty } z&||}|s |  W Y d }~n
d }~0 0 d S )Nz1incorrect arg count %s %s and no constant handlerz"invalid call to builtin op handlerzinvalid args to z:  z/Encountered TypeError when trying to handle op rO  )	TypeErrorinspect	signaturebindrE  logwarningr   r   r   Z	DIFFICULTr   remove_from_stats)r   r-  r  r  erE  exc)rO   rN  self_handlerrU   rV   call_self_handleru  s6    
"
z8BuiltinVariable._make_handler.<locals>.call_self_handlerc                 s   s   | ]}t |tV  qd S rX   )r  r6   r]   rU   rU   rV   r`     ra   c              
      s   z dd |D  }W n t yZ } z,tt|| tttj|jd W Y d }~nPd }~0  ty } z0t	dd  d| dg t
j|d W Y d }~n
d }~0 0 t| |S )	Nc                 S   s   g | ]}|  qS rU   r=  r]   rU   rU   rV   r>    ra   PBuiltinVariable._make_handler.<locals>.constant_fold_handler.<locals>.<listcomp>r-  constant fold exceptionattempted to run function  with arguments 7Encountered exception when attempting to constant fold.rP  rQ  rR  rS  Zfrom_exc)	Exceptionr   rZ   listmapr6   r   r-  r2   r   r   
DYNAMO_BUGr4   r   r   r-  r  resre  r   rU   rV   constant_fold_handler  s&    z<BuiltinVariable._make_handler.<locals>.constant_fold_handlerc              
      s   t ||rz* dd |D i dd | D }W n ty| } z0tdd  d| dg tj|d	 W Y d }~nLd }~0  ty } z,tt|| t	t
tj|jd
 W Y d }~n
d }~0 0 t| |S d S )Nc                 S   s   g | ]}|  qS rU   r=  r]   rU   rU   rV   r>    ra   rh  c                 S   s   i | ]\}}||  qS rU   r=  r?  rU   rU   rV   rB    s   zPBuiltinVariable._make_handler.<locals>.constant_fold_handler.<locals>.<dictcomp>rj  rk  rl  rm  rn  ri  )r#   r   r2   r   r   rr  ro  r   rZ   rp  rq  r6   r   r-  r4   r   rs  r   rU   rV   ru    s0    

c                    sj   dd | D }t ddj d  d dj d| d d	d
j d| dj dddgd d S )Nc                 S   s   g | ]}|  qS rU   )Zpython_type_namer^   r2  rU   rU   rV   r>    ra   zPBuiltinVariable._make_handler.<locals>.call_unimplemented_v2.<locals>.<listcomp>z Failed to trace builtin operatorzbuiltin r\  z4Dynamo does not know how to trace builtin operator `z` with argument types z (has_kwargs r!  zAvoid calling builtin `z?. Consider using an equivalent alternative function/method to `z`.zIf you are attempting to call a logging function (e.g. `print`), you can try adding it to `torch._dynamo.config.reorderable_logging_functions`.z"Please report an issue to PyTorch.rO  )r   r   )r-  Zreal_arg_types)r4  rO   rF  rU   rV   call_unimplemented_v2  s     
z<BuiltinVariable._make_handler.<locals>.call_unimplemented_v2c                    s    |S rX   rU   rL  )rw  rU   rV   r     ra   c                    s    | ||}|r|S  | d S rX   rU   )r   r-  r  rY  )rw  r   rU   rV   builtin_dispatch  s    z7BuiltinVariable._make_handler.<locals>.builtin_dispatchc                    s.   D ]}|| ||}|r|  S q | d S rX   rU   )r   r-  r  rO   rY  )rw  r  rU   rV   rx    s
    
)ZlazyrH  rN   rd   r^  isclassr  ro  GeneratorExitr  rv   r   r   rH   r6  _handle_insert_op_in_graphr  rl   r  rj   r   r   rc   )rO   r4  rF  rV  r[  rg  ru  rx  rU   )rH  r4  rX  rZ  rw  rO   r   r  rF  rN  rf  rV   _make_handler-  sf    
	

!


zBuiltinVariable._make_handlerrM   r   c                 C   s~  ddl m}m} |r0| jg || R  s0d S ddl m} ddlm}m}m	}	m
}
 |	|||r|jst|d ts| j|v r|| j }n
|| j }|d }|d |d< ||d< q|| j }n| j}|||}|
||||S | j}zL| j|i |r*ddlm} ddlm} |||||j||W S |tv r`t|d tjr`t| |d |d g }}|tju rt|d trtj|d tjd|d g }}t||rtd	d
 |D s|j j!dt"|gt#||R  }|tj$||W S |tj%u r8t&|dkr8t|d tjr8|d '|d|dd  |W S |j j!d|gt#||R  }tdd
 |D rv|t(||W S t)||r| *||\}}||i |}tdd
 t+,|| D }|t-||||dW S t.dd
 |D rt||d W S |tj/u r$t|d tj-r$|d 0 |d< |||W S W nF t1yx   t2dd|  d| d| d| j dg t3j4d Y n0 d S )Nr1   )r   wrap_fx_proxy_cls)SourcelessBuilder)BUILTIN_TO_TENSOR_FN_MAPBUILTIN_TO_TENSOR_RFN_MAPcan_dispatch_torch_functiondispatch_torch_functionr   r   )	unique_id)invoke_and_store_as_constantc                 s   s   | ]}t |tjkV  qd S rX   )rZ   r   rH   rv  rU   rU   rV   r`   D  s   z=BuiltinVariable._handle_insert_op_in_graph.<locals>.<genexpr>r   __eq__c                 s   s   | ]}t |tV  qd S rX   rY   rE   rv  rU   rU   rV   r`   ]  ra   c                 s   s    | ]}t |tjr|jV  qd S rX   rY   r   rI   need_unwrapr]   rU   rU   rV   r`   g  s   )	raw_valuer  c                 s   s   | ]}t |tV  qd S rX   rY   rG   r]   rU   rU   rV   r`   t  ra   z,unimplemented builtin op on tensor argumentszpartial tensor op: r\  z3Dynamo does not know how to trace builtin operator z with tensor argumentsrO  )5r   r   r}  r3  r   r~  Ztorch_functionr  r  r  r  exportrY   rH   rO   r   r<  Zbytecode_transformationr  r  r  r   IN_PLACE_DESUGARING_MAPr   r6   rv   r   rG   r  selectr"   rd   r   r   r-   r.   NumpyNdarrayVariabler   rl   r   rE   r$   rD  r9  r:  rI   rc   r   r   NotImplementedErrorr   r   rU  )r   r   r-  r  r   r}  r~  r  r  r  r  functmpZfn_varrO   r  r  proxy_args_kwargsr  r  rU   rU   rV   r{    s    



	



z*BuiltinVariable._handle_insert_op_in_graphcall_function_handler_cacher4   zdict[str, VariableTracker])r   r-  r  r   c                 C   s   |r4dd |  D }| jgdd |D dR }n| jgdd |D R }| j|}|s| | jdd |D t| | j|< }||||S )	Nc                 S   s   i | ]\}}||  qS rU   rI  r?  rU   rU   rV   rB    ra   z1BuiltinVariable.call_function.<locals>.<dictcomp>c                 s   s   | ]}t |V  qd S rX   rZ   r]   rU   rU   rV   r`     ra   z0BuiltinVariable.call_function.<locals>.<genexpr>Tc                 s   s   | ]}t |V  qd S rX   r  r]   rU   rU   rV   r`     ra   c                 S   s   g | ]}t |qS rU   r  r]   rU   rU   rV   r>    ra   z1BuiltinVariable.call_function.<locals>.<listcomp>)r   rO   r  r  r|  re   )r   r   r-  r  keyr   rU   rU   rV   r     s    zBuiltinVariable.call_functionzlist[VariableTracker])r-  r  r   c                    sX  | j tu rr|dkrrt|dks"J t|dks2J |\}}}| }t|trr|jj|rr|	 rr|
|||S |dkr| j tu rt|dkrt|dksJ |jj| |d |dd  S | j tu r2t|dkr2|s2ti tt d}t|d tr|d j tu r|S |jj| |d |dd  S | j tu rt|dkr|d |r|st|d tr|d j tu r|d |}	tj|	t dS |jj| |d |dd  S | j tu rtg t d}
t|d tr|d j tu r|
S |jj| |d |dd  S | j tu r:|dkr:td S | j tu rj|d	krjtj|tg|R i |S | j tu rt| j |}|tv rt|d tjr|d j|||dd  |S t|d tjr|d |||dd  |S | j tu rFt|dkrFt| j |}|tv rFt|d trF|d |||dd  |S t  ||||S )
N__setattr__   r   __new__r1   r   r   r  fromkeys)!rO   r	  rl   rJ  rY   rJ   r   r   is_attribute_mutationis_python_constantZmethod_setattr_standardZtrack_new_user_defined_objectdictr9   r3   rN   tupler   r   r   rD   rp  rA   r6   call_custom_dict_fromkeysrj   r&   ZUserDefinedDictVariableZ_dict_vtr   rs   r/   r  )r   r   r"  r-  r  rN  name_varvalZdict_vtZ	init_argsZlist_vtZresolved_fnr  rU   rV   r     s    

  

 
 



zBuiltinVariable.call_methodc                 C   st   t |ttjfrpt |tjr.||dg i }n|}| jtu r@tnt}ddl	m
} |||jd|| fi dS d S )Nitemr   r   r   r   r  )rY   rG   r   rH   r   rO   rk   r
   r	   Ztorch._dynamo.variables.builderr   r   r   r&  )r   r   r2  r  Zfn_r   rU   rU   rV   _call_int_float  s    zBuiltinVariable._call_int_floatc                 C   sL   t |trHt |jtjr|S t|jtjtjfs4J t||	 dkS d S Nr   )
rY   rG   sym_numr  ZSymBoolr,   ZSymIntZSymFloatr   r&  )r   r   r2  rU   rU   rV   	call_bool  s
    
zBuiltinVariable.call_boolc              
   C   s  t |tjr tjjt|jdS t |tjr4t|j	drD|j	j
}n0t|j	drZ|j	j}ntdt|dg tjd t|j	j
tj
u rztjj| dW S  ty   Y d S 0 nt|rtddt|j	 d	d
gd nZ|j}zt|}W n4 ty  } ztd| W Y d }~d S d }~0 0 |||gi S nNt |tjfrt|jdkr^|j }nddd |jD }tjj|dS d S )N)rT   __str__r#  z+failed to call str() on user defined objectz5User defined object has no __str__ or __repr__ methodrO  z0Attempted to a str() method implemented in C/C++ z5 has a C/C++ based str method. This is not supported.zWrite the str method in Pythonz)Failed to create UserFunctionVariable: %sr   , c                 s   s   | ]}|  V  qd S rX   r=  )r^   r   rU   rU   rV   r`   `  ra   z+BuiltinVariable.call_str.<locals>.<genexpr>)rY   r   r   r6   r   rs   rO   rJ   hasattrrT   r  r#  r   r   
USER_ERRORrZ   r	  AttributeErrorr+   __func__rT  ra  rb  r   r  rl   r-  r  join)r   r   r2  Z
str_methodZbound_methodZuser_func_variablerd  rT   rU   rU   rV   call_str,  sJ    


	
zBuiltinVariable.call_strc                 G   sr   t |dkr4|d |r4|d |}| ||S t |dkrV| ||d |d S t |dkrn| ||S d S )Nr1   r   r   )rl   has_force_unpack_var_sequenceforce_unpack_var_sequence_call_min_max_seq_call_min_max_binary)r   r   r-  r   rU   rU   rV   _call_min_maxc  s    zBuiltinVariable._call_min_maxc                 C   s:   t |dksJ t |dkr$|d S tt| j||S Nr   r1   )rl   	functoolsreducepartialr  )r   r   r   rU   rU   rV   r  l  s    z!BuiltinVariable._call_min_max_seqc                 C   s  |d u s|d u rd S |  ||r2t|tjs8|| }}t|tjsHJ t|trjttj||gi }t|t	s~t|t	rddl
m} |t|||jjd| jgt||gi R  dS | r t|tjrdd l}t|j}nttj}| jtu rd|ind|i}|||g|}njt|tjrXdd l}t|jt|ji| j }	t|	}n ttjttji| j }
t|
}||||gi }tdd	 ||fD r,td
d	 ||fD rtj|S | r| }n|j}| jtu rt|j|}nt|j|}tdd	 ||fD }tj |||S |S nt|t	sJt|t	r| jtu r\tj!ntj"}|jjd|gt||gi R  }t	#||d S t|t$rt|t$r| | | }t$|S d S )Nr1   r}  r   r  r   rn   rm   c                 s   s    | ]}t |tjtjfV  qd S rX   )rY   r   rI   r6   )r^   r;  rU   rU   rV   r`     s   z7BuiltinVariable._call_min_max_binary.<locals>.<genexpr>c                 s   s   | ]}t |tV  qd S rX   r  )r^   r  rU   rU   rV   r`     ra   c                 s   s    | ]}t |tjr|jV  qd S rX   r  r]   rU   rU   rV   r`     s   )%r3  rY   r   rH   rE   TorchInGraphFunctionVariabler  r   r   rG   r   r}  rZ   r   r   rO   r.   r  r  numpyZNumpyVariableZclipclamprm   maximumrn   Zminimumrc   rd   Zfrom_tensor_variabler   r  rI   Zsym_maxZsym_minr   r6   )r   r   r   r   r}  nprO   r  r  Znp_fnZtorch_fnZraw_bZraw_resr  Zpy_fnr  rT   rU   rU   rV   r  s  s    







z$BuiltinVariable._call_min_max_binary)r   r2  c                 C   s*   t t||tdgi }||g i S )N__abs__rN   rj   r   r6   r   )r   r   r2  Z
abs_methodrU   rU   rV   call_abs  s    zBuiltinVariable.call_absc                 C   s*   t t||tdgi }||g i S )N__pos__r  )r   r   r2  Z
pos_methodrU   rU   rV   call_pos  s    zBuiltinVariable.call_posc                 C   s:   t |tjrtdddg d t|}t|}tj|S )Nzunsupported index(Tensor)r  z;Dynamo does not support tracing builtin index() on a TensorrO  )	rY   r   rH   r   r)   rv   r   r6   r   )r   r   r2  Zconstant_valuerU   rU   rV   
call_index  s    
zBuiltinVariable.call_indexc                 O   s*   t t||tdgi }||||S )N	__round__r  )r   r   r2  r-  r  Zround_methodrU   rU   rV   
call_round  s    zBuiltinVariable.call_roundc                 G   s>   t |i rt|S | j| r:tdd |D }t|S d S )Nc                 s   s   | ]}t jt|V  qd S rX   )r   r6   r   r)   rv  rU   rU   rV   r`     s   z-BuiltinVariable.call_range.<locals>.<genexpr>)r#   r   ZRangeVariable_dynamic_argsr  r   r   r-  rU   rU   rV   
call_range  s    



zBuiltinVariable.call_rangec                 O   s(   t dd |D p&t dd | D S )Nc                 s   s   | ]}t |tV  qd S rX   r  r]   rU   rU   rV   r`     ra   z0BuiltinVariable._dynamic_args.<locals>.<genexpr>)rd   r   r,  rU   rU   rV   r    s    zBuiltinVariable._dynamic_argsc                 G   s
   t |S rX   )r   ZSliceVariabler  rU   rU   rV   
call_slice  s    zBuiltinVariable.call_slicec                 O   s0   ddl m} |||jjd| jgt||R  S r   )r   r   r   r   rO   r.   )r   r   r-  r  r   rU   rU   rV   
_dyn_proxy  s    zBuiltinVariable._dyn_proxyc                 O   s&  t |tjrJ | j|i |r8| j|g|R i |S tj| j}|d u r\|g t dS |	|r"|j
r
t|j
s
t |trt|j
tj npt|ddrt |trt|ts|jj|j
 t |tjrt|j
tj n t |tjs
t|j
tj |t||t dS d S )Nr   rQ   F)rY   r   IteratorVariabler  r  r?   cls_forrO   r3   r   rQ   r   rC   r   rR   r   ZTUPLE_ITERATOR_LENrj   r9   r,   r>   r   Zguard_on_key_orderr   ZMappingProxyVariableZMAPPING_KEYS_CHECKUnspecializedNNModuleVariableZSEQUENCE_LENGTHrp  r   r   r   rN  r-  r  rS   rU   rU   rV   _call_iter_tuple_list  sD    

z%BuiltinVariable._call_iter_tuple_listc                 O   s&   t j| j}|t||t dS r   )r   r?   r  rO   rp  r  r3   r  rU   rU   rV   _call_iter_tuple_generatorG  s
    z*BuiltinVariable._call_iter_tuple_generatorc                 O   sv   t |tjr2tj| j}|t||t dS t |tj	rX| j
||g|R i |S | j||g|R i |S d S r   )rY   r   r  r?   r  rO   rp  r  r3   ZLocalGeneratorObjectVariabler  r  r  rU   rU   rV   _call_tuple_listN  s    z BuiltinVariable._call_tuple_listc                 O   sH   t |tjr|}n| j||g|R i |}|d u rD||d||S |S )N__iter__)rY   r   r  r  r   )r   r   rN  r-  r  retrU   rU   rV   	call_iterZ  s    zBuiltinVariable.call_iterc                 C   s~   ddl m}m} ddlm} t|tj|||fr<tj	dS t|t
rXtj	t|jS t|ttttttfrztj	dS d S )Nr1   )r   FunctoolsPartialVariabler   TF)r  r   r  r  r   rY   r   r   r6   r   rK   rf   rT   rG   rH   rA   rD   r@   )r   r   r2  r   r  r   rU   rU   rV   call_callablek  s0    	
zBuiltinVariable.call_callablec                 O   s6   t |dkr|d S tdd| d| ddgd d S )	Nr   r1   zbad args to builtin cast()z	got args r\  z0Dynamo expects exactly 2 args to builtin cast().z3Ensure your call to cast() has exactly 2 arguments.rO  )rl   r   )r   rW  r-  r  rU   rU   rV   	call_cast  s    zBuiltinVariable.call_castc                 O   s   t j|tg|R i |S rX   )rN   call_custom_dictr  r   r   r-  r  rU   rU   rV   	call_dict  s    zBuiltinVariable.call_dictc                 O   s&   |  t| tjt| |g||S rX   )r   r4   r   r   Zconstruct_dict)r   user_clsr-  r  rU   rU   rV   r    s
    z BuiltinVariable.call_custom_dictc                 O   sb  |t tthv sJ |rZ|tu s"J t|dkrBt|dkrBd|v sFJ g ||dR }t|dkrpttdt|dkrg |td R }t|dksJ |\}}|turt	nt
}t|t rdd | D }|t |||t dS || r.|| }td	d
 |D r.|t |||t dS td|j d| d| d|j dddgd d S )Nr1   rT   r   z,fromkeys expected at least 1 argument, got 0r   c                 S   s   g | ]}t |qS rU   )r6   r   )r^   r@  rU   rU   rV   r>    ra   z=BuiltinVariable.call_custom_dict_fromkeys.<locals>.<listcomp>r   c                 s   s   | ]}t |V  qd S rX   )r=   rK  rU   rU   rV   r`     ra   z<BuiltinVariable.call_custom_dict_fromkeys.<locals>.<genexpr>zfailed to call dict.fromkeys()z.fromkeys(): r\  zFailed to call zo.fromkeys() because arguments could not be automatically converted to a list, or some dict key is not hashable.z(Manually convert the argument to a list.zEnsure all keys are hashable.rO  )r  r   r   rl   popr   r]  r6   r   r9   r:   rY   keysr  r3   r  r  rc   r   r   )r   r  r-  r  r2  rT   ZDictVariableTyper  rU   rU   rV   r    sD    $



z)BuiltinVariable.call_custom_dict_fromkeysc                 O   s  |rJ |st g t dS t|dkrHtt|tdt| dgd |d }t|tj rj|j	t dS |
|r||}t |t dS t|tjrt|jtr||d}t|tjr||||}t|t r|S tt||S tt|tdgd d S )	Nr   r1   z&set() takes 1 positional argument but  were givenri  r   r  z!failed to construct builtin set())r>   r3   rl   r   r]  r6   r   rY   r   cloner  r  rJ   rT   r   var_getattrZUserMethodVariabler   rN   setcall_set)r   r   r-  r  r2  r   Ziter_fnoutrU   rU   rV   r    s@    	



zBuiltinVariable.call_setc                 O   s   |rJ |st g S t|dkrBtt|tdt| dgd |d }t|tj rjt dd |jD S |	|r|
|}t |S tt|tdgd d S )	Nr1   z,frozenset() takes 1 positional argument but r  ri  r   c                 S   s   g | ]
}|j qS rU   )Zvtr]   rU   rU   rV   r>     ra   z2BuiltinVariable.call_frozenset.<locals>.<listcomp>z'failed to construct builtin frozenset())r<   rl   r   r]  r6   r   rY   r   	set_itemsr   r   )r   r   r-  r  r2  r   rU   rU   rV   call_frozenset  s.    	


zBuiltinVariable.call_frozensetc                    sL   |rt |dkrd|v sJ |dd} fdd|D }tj||t dS )Nr1   strictFc                    s$   g | ]}|  r| n|qS rU   r   r   rv  r   rU   rV   r>    s   z,BuiltinVariable.call_zip.<locals>.<listcomp>)r  r   )rl   r  r   ZZipVariabler3   )r   r   r-  r  r  rU   r   rV   call_zip
  s    
zBuiltinVariable.call_zipc              
   O   s`   z|d  |d|dd  |W S  tyZ } z$tt||t|jd W Y d }~n
d }~0 0 d S )Nr   __len__r1   ri  )r   r  r   rZ   rp  r-  )r   r   r-  r  rd  rU   rU   rV   call_len  s    zBuiltinVariable.call_lenc                 O   s   |d  |d|dd  |S )Nr   __getitem__r1   r   r  rU   rU   rV   call_getitem  s    zBuiltinVariable.call_getitemc              
      s  z   }W n< tyH   tdd  d| dd  g tjd Y n0 | }t tjr j	d ur fdd}tj
| |S t tjrt jtjrtd	d  d| dd
g tjd t tjrd|jjv rtj
|j| jS t tjrt
t||S t|ts0tt|dd r8|f}nXtjdkrZt|tjrZ|j}n6t|trtdd |D r|}ntt|dgd zt ||}W n ty   ||v }Y n0 tj
|S )Nz6builtin isinstance() cannot determine type of argumentzisinstance(r  r!  z=Dynamo doesn't have a rule to determine the type of argument rO  c                    s:   fdd t |tu r.t fdd|D S  |S d S )Nc                    sX   | t vrF  jjd }t|r8| tjjju r8t	|| S t
  | S t |  } j|v S )Nexample_value)r0   r&  nodemetar   r  nnZ	parameter	ParameterrY   r  python_typedtype)tyZexample_valZdtypesr2  rU   rV   
check_type/  s    
zOBuiltinVariable.call_isinstance.<locals>._tensor_isinstance.<locals>.check_typec                 3   s   | ]} |V  qd S rX   rU   )r^   r  r  rU   rV   r`   A  ra   zNBuiltinVariable.call_isinstance.<locals>._tensor_isinstance.<locals>.<genexpr>)rZ   r  rd   )Z
tensor_varZtensor_typer  r  rV   _tensor_isinstance.  s    z;BuiltinVariable.call_isinstance.<locals>._tensor_isinstancez<isinstance() called on user defined object with C extensionszeUser-defined object with C extensions can have torch.Tensor attributes; intentionally graph breaking.__instancecheck__)r  
   c                 s   s(   | ] }t |tptt|d dV  qdS )r  N)rY   rZ   rf   rj   )r^   tprU   rU   rV   r`   j  s   z2BuiltinVariable.call_isinstance.<locals>.<genexpr>z?isinstance() arg 2 must be a type, a tuple of types, or a unionri  )!r  r  r   r   rr  r   rY   r   rH   r  r6   r   rJ   rT   typesMemberDescriptorTyperU  r   r   r  Z!UserDefinedExceptionClassVariablerZ   rf   rj   sysversion_infoZ	UnionType__args__r  rc   r   r]  r  )r   r   r2  Zisinstance_typer5  r  Zisinstance_type_tupler  rU   r  rV   call_isinstance  sp    	



zBuiltinVariable.call_isinstancec              
   C   s^   z|  }|  }W n8 tyL   tdd| d| dddgtjd Y n0 tt||S )z,Checks if first arg is subclass of right argz(issubclass() with non-constant argumentszissubclass(r  r!  z7issubclass() with non-constant arguments not supported.z#Make sure your arguments are types.rO  )r   r  r   r   r  r   r6   r  )r   r   Zleft_tyZright_tyZ
left_ty_pyZright_ty_pyrU   rU   rV   call_issubclass  s    zBuiltinVariable.call_issubclassc                 C   s   t ||S rX   )r   ZSuperVariabler   r   r   r   rU   rU   rV   
call_super  s    zBuiltinVariable.call_superc              
   C   sb   z| |W S  ty\ } z8t|tjrF|  |jd W  Y d }~S  W Y d }~n
d }~0 0 d S r  )Znext_variabler   rY   r   r?   rc  r   )r   r   r2  exrU   rU   rV   	call_next  s    zBuiltinVariable.call_nextc                 C   s>   |  r:| }t|tjr.tt|j|S |||S d S rX   )	r  r   rY   r   rN   r6   r  rO   Zcall_obj_hasattr)r   r   rN  attrr"  rU   rU   rV   call_hasattr  s
    zBuiltinVariable.call_hasattrc                    s$    fdd|D }t j||t dS )Nc                    s$   g | ]}|  r| n|qS rU   r  )r^   r   r   rU   rV   r>    s   z,BuiltinVariable.call_map.<locals>.<listcomp>r   )r   ZMapVariabler3   )r   r   rO   ZseqsrU   r   rV   call_map  s    
zBuiltinVariable.call_mapc                 C   s*   | |r||n|}tj||t dS r   )r   r   r   ZFilterVariabler3   )r   r   rO   r   rU   rU   rV   call_filter  s    zBuiltinVariable.call_filter)r   rN  r  c              	      s  |  s.tdd| d| d| dddgd | }t|trL|dkrLd	}jj|rt|tj	r|d
v r|j
rjj|rtdd| d| d| ddg d jj||rƈjj||S |d ur| ||}| dv sJ | s|S |jo
t|j| |dv rz| }t|tr|dkr` fddt|jD }tj| dW S |dkr|t|j W S |dkrt|jW S W n ty   Y n0 t|tjr||S t|tjtjtjtjtj tj!frt|tj!rDt"|j#j$t%j&rDt'j(rD|dv rDtdd| d| dd| dgd t|tr|j)j*j+d }	t|	t,j-rt.|	rj/rt'j0stdddg t1j2d z||W S  ty   tj3|| d Y S 0 n.t|tj4r@t5|j#|}
t|
t,j6j7t,j6j8fr$t,j9j:;|
r$tj4|
 dS |t<v rtj3|| dS nt|t=r|j>sb||j#j?vrpt5|j#|}
n|j#j?| }
t'j@rjAB|j#||
 t|
 S tC|tjDr|d v rtt5|jE|S z||W S  ty    tj3|| d Y S 0 d S )!Nz)getattr() with non-constant name argumentzgetattr(r  r!  z:getattr() with non-constant name argument is not supportedz1Ensure the name argument of getattr() is a stringrO  _gradgrad)Znamed_parameters
parametersZnamed_buffersbuffersZnamed_modulesmodulesz,getattr() on nn.Module with pending mutationzPIntentionally graph breaking on getattr() on a nn.Module with a pending mutation)TF>   __base__	__bases__	__flags__r  c              	      s(   g | ] \}}t | o t |qS rU   )r4   r   r   )r^   r;  r   rQ   r   rU   rV   r>    s   z0BuiltinVariable.call_getattr.<locals>.<listcomp>rP   r  r  )assertRaisesRegexZassertNotWarnsZassertWarnsRegexZassertDictEqualZassertSequenceEqualZassertWarnszFailed to trace unittest methodzfunction: unittest.TestCase.z3Dynamo does not know how to trace unittest method `z` zAvoid calling `TestCase.z%`. Please report an issue to PyTorch.r  zAttempted to wrap sparse Tensorr  z-torch.compile does not support sparse Tensors)r   r)  )Fr  r   r   rY   rH   r   r   r  r   r  Zis_state_mutatedZhas_pending_mutationZhas_pending_mutation_of_attrZ	load_attrr  rQ   r   rZ   	enumerater  rD   r4   r   r  r6   r   r  r  r   r  NamedTupleVariableZDistributedVariabler   rJ   r  rT   r   unittestZTestCaser   Zenable_trace_unittestr  r  r  r  Tensorr   r  Zcapture_sparse_computer   rU  r0  r  rj   Z_opsZOpOverloadPacketZ
OpOverloadZ_dynamoZtrace_rulesZis_aten_op_or_tensor_methodr%   r   Zis_torchr   Zreplay_record_enabledZexec_recorderZrecord_module_accessr,   r   rO   )r   r   rN  r  defaultr"  Zhasattr_varrT   Z
tuple_argsZfake_valmemberrU   r  rV   call_getattr  s    







	

zBuiltinVariable.call_getattr)r   rN  r  r  c              	      s  t  tjtjtjtjtjfr2 |d||gi S |jj	
 r | r | }t  tjr
ddlm} |dkrtdd  d| d| dd	g tjd
 nf|dkr jd u rtdd  d| d| dddgd
  fdd|jjD }|D ]}|jj| qt|d t : |||jjdtjjgt |gi R  }	W d    n1 s`0    Y  W d    n1 s0    Y  dd }
|jd|
|	 fi  |
  jjd  |	S |dv rd}n8t |r
tdd  d| d| ddd| dgd
 |jj	! || |S t  tj"r|j# sBt$d| rt |tjrt%| j|}z &|| }W n t't(fy   d }Y n0 t |tjrt%| j|}t)* j+d}||u r|tj,j-j.u r|S  /| d S )Nr  r1   r   Zrequires_gradz!setattr() on Tensor.requires_gradzsetattr(r  r!  zsetattr() on Tensor.requires_grad not supported. Mutating requires_grad can introduce a new leaf from non-leaf or vice versa in the middle of the graph, which AOTAutograd does not currently know how to handle.rO  dataz&Failed to mutate tensor data attributezVDyanmo only supports mutating `.data` of tensor created outside `torch.compile` regionzWDon't mutate `.data` on this tensor, or move the mutation out of `torch.compile` regionc                    s   g | ]}|j  j kr|qS rU   rP   )r^   tfrM  rU   rV   r>    s   z0BuiltinVariable.call_setattr.<locals>.<listcomp>r   c                 S   s.   | j }|dkr|d }tjj| f|f | S r  )_versionr  Z_CZ	_autogradZ_unsafe_set_version_counter)r_   versionrU   rU   rV   _lower_version_count_by_1  s
    z?BuiltinVariable.call_setattr.<locals>._lower_version_count_by_1r  )r  r  r  zFailed to set tensor attributez6Dyanmo doesn't support setting these tensor attributeszDon't mutate attribute 'z@' on tensors, or move the mutation out of `torch.compile` regionz?Can't inplace modify module params/buffers inside HigherOrderOp)0rY   r   ZPlacementVariabler  rJ   ZNestedUserFunctionVariabler  r   r   r   r  r  r   rH   r   r   r   r   rU  rQ   Ztracked_fakesremovedynamo_disable_gradr  Zno_gradr   r  set_r.   r&  r  r  r*   Z
store_attrr   Zis_root_tracerr   r(   r  r  r   r^  getattr_staticmodule_typer  Moduler  Zconvert_to_unspecialized)r   r   rN  r  r  r"  r   Z	to_remover   r  r#  Zassigning_fake_valZgetattr_varZexisting_fake_attrZmod_setattrrU   rM  rV   call_setattrU  s    




D




zBuiltinVariable.call_setattrc                 C   s   | |d|gi S )N__delattr__r   )r   r   rN  r  rU   rU   rV   call_delattr  s    zBuiltinVariable.call_delattr)r   rN  c              
   C   s   z|  }W n: tyF } z"ttjt|ddd W Y d }~n
d }~0 0 |joVt|j}|d u rxt|t	j
rx|jrx|j}|tju r|jdt}tt|d}t|||S )NZunknown_python_type)Z	case_namer  r  )r  r  r   r   ZINVALID_INPUTrs   rQ   r    rY   r   rJ   Z
cls_sourcer  r  r   Zinstall_global_by_idr   r   r4   r   )r   r   rN  Zpy_typeerrorrQ   r"  rU   rU   rV   	call_type  s,    

zBuiltinVariable.call_typec                 C   s*   | |r&tt||}t|S d S rX   )r   rp  reversedr   r   rD   )r   r   rN  r   rU   rU   rV   call_reversed		  s    
zBuiltinVariable.call_reversed)r   rN  r  c                 K   sD   | |r@t|tjs@tj||t d}||dg | |S d S )Nr   sort)r  rY   r   rH   rA   r  r3   r   )r   r   rN  r  Zlist_varrU   rU   rV   call_sorted	  s    zBuiltinVariable.call_sortedc                 C   s(   t |tr$tj|t| d dS d S )Nr  )rY   rG   r   rv   rx   r&  r   r   r   rU   rU   rV   call_neg	  s    
zBuiltinVariable.call_negc                 O   s    |  }t|}tj|||S rX   )r   rs   r   ZStringFormatVariabler   )r   r   Z_format_stringr-  r  format_stringrU   rU   rV   call_format)	  s    zBuiltinVariable.call_formatc                 G   sf  t |dkrBt|d tjrB|d }|j|j}tjt	|S t |dkrt|d tj
tjfr|d jrt|d jtj t	|d j}tj|S t |dkrt|d tr|d }||S t|d tjrtjt	|d jS t|d tjrtjt	|d jS t|d tjr@tjt	|d jS tdt|d| dgtjd d S )Nr   r1   zid() with unsupported argsz5Dynamo doesn't know how to trace id() call with args zkSupported args are Tensors, and functions/nn.Modules/user-defined objects from outside the compiled region.rO  )rl   rY   r   r   r   r   r   r6   r   idr   rJ   rQ   r   rR   r   ZID_MATCHrT   rH   call_idr,   r   rO   ZSkipFunctionVariabler  Z
fake_valuer   rs   r   rU  )r   r   r-  Znn_mod_variabler   Zconstant_resultZtensor_variablerU   rU   rV   r9  .	  s<    


zBuiltinVariable.call_idc                 C   s$   t dd| dddgtjd d S )Nzcopy.deepcopy()zcopy.deepcopy(r!  z'Dynamo does not support copy.deepcopy()zAvoid calling copy.deepcopy()rO  )r   r   rU  )r   r   r_   rU   rU   rV   call_deepcopyP	  s    
zBuiltinVariable.call_deepcopyc           
      C   s  ddl m} ddlm} | j}|tjtjfv rt|t	odt|t	odt
t| jt
t| jk}|tju rzt|S t| S ||vrtd|j d| d| dd|j d	| d| g tjd
 t|t	rnt|t	rn|jo|jd urn|j|jkrnzt|j|j W nT tyl   td|j d| d| dd| d| d|j dg tjd
 Y n0 t|t	r~|n|}|jd|| | fi }	|t|||	S )Nr1   r  %supported_tensor_comparison_op_valuesz unsupported Tensor comparison opr   r  r!  *Dynamo does not support the comparison op z with Tensor arguments rO  z8failed to broadcast when attempting Tensor comparison opz.Dynamo was unable to broad cast the arguments z, when attempting to trace the comparison op .r   )r   r}  r   r<  rO   rv   r   is_notrY   rH   r8  r'   r&  r  r6   r   r   r   r   rU  sizer  Zbroadcast_shapesRuntimeErrorr  r   r   rZ   )
r   r   r  r  r}  r<  r   Z	is_resultZ
tensor_clsr  rU   rU   rV   r
  [	  sj    




z'BuiltinVariable._comparison_with_tensorc              	   C   s   ddl m} | j}||vrXtd|j d| d| dd|j d| d| g tjd	 t|tj	rvt
|t d S |jd
|| | fi }tj||d dS )Nr1   r;  z!unsupported SymNode comparison opr   r  r!  r=  z with SymNode arguments rO  r   r3  )r   r<  rO   r   r   r   rU  rY   r   r   r6   r	  r   r   r&  rG   r   )r   r   r  r  r<  r   r  rU   rU   rV   r  	  s.    
	z(BuiltinVariable._comparison_with_symnodec                 C   s   t |trt |trd S t |ttfrbt |ttfrbtj||jjdtjgt||gi R  d dS t	|drt	|drt
t|j|j@ S d S )Nr   r3  r  )rY   r6   rG   r   r   r   rv   r   r.   r  r>   rp  r  r  rU   rU   rV   	call_and_	  s     zBuiltinVariable.call_and_c                 C   s   t |trt |trd S t |ttfrbt |ttfrbtj||jjdtjgt||gi R  d dS t	|drt	|drt
t|j|jB S t |tr|j|d|gi dS d S )Nr   r3  r  __or__rC  )rY   r6   rG   r   r   r   rv   r   r.   r  r>   rp  r  r9   r   r  rU   rU   rV   call_or_	  s&    
zBuiltinVariable.call_or_c                 C   sl   t |tr6tj||jjdtjgt|gi R  d dS t |trF|j	}t |t
tfrhtt|jdkS d S )Nr   r3  r   )rY   rG   r   r   r   rv   ry   r.   r;   Zdv_dictrA   r9   r6   rl   r   r4  rU   rU   rV   	call_not_	  s    

	
zBuiltinVariable.call_not_r   c                 C   s   | |d|gi S )N__contains__r   r  rU   rU   rV   call_contains	  s    zBuiltinVariable.call_contains)N)N)N)ir   r)  __qualname____doc__r	  Z	_SENTINELr4   Z_nonvar_fieldsclassmethodrW   staticmethodr  cacher   r   r   r  r   r  rp  rs   r   r  r  r  r  r#  r   r&  r+  r.  r3  r6  r<  rD  rE  rZ   re   r|  r{  r  r   __annotations__r   r   r  Zcall_intZ
call_floatr  r  r  r  r  Zcall_minZcall_maxr  r  r  r  r  r  r  r  r  r  r  r  Z
call_tuple	call_listr  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r  r*  r,  r.  r0  r2  r5  r7  r9  r:  r
  r  rB  Z	call_iandrD  Zcall_iorrE  rG  __classcell__rU   rU   r  rV   rN      s.  


A,"%   


 K 

[7	c .
-$f		  ! 
"5rN   c              	   c   sH   ddl m} || d}z||  d V  W ||  n||  0 d S )Nr1   )GradModeVariableF)r  rP  r   Zenterexit)r   rP  ZgmvrU   rU   rV   r%  	  s    
r%  )rI  
contextlibr  r^  r9  loggingr[   rv   r  r  typingr  collectionsr   r   collections.abcr   r   r   r   r   r  r	   r
   Ztorch._subclasses.meta_utilsr   Ztorch.utils._python_dispatchr   r  r   r   r   r   re  r   r   r   r   r   r   r   Zguardsr   r   Zreplay_recordr   rQ   r   r   r   r   r    utilsr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   baser2   r3   r4   Zconstantr6   Zctx_managerr7   r8   Zdictsr9   r:   r;   r<   r=   r>   listsr?   r@   rA   rB   rC   rD   r   rE   rF   rG   rH   rI   r  rJ   rK   Ztorch._dynamo.codegenrL   Ztorch._dynamo.symbolic_convertrM   	getLoggerr   ra  r   r   r   r   r   r|   r   r~   r   r   r   r   r   r   r   r   r   r   rp   r   r   r   r   r   r   r  AnyZ_HandlerCallbackrZ   r  Z_TrackersTyper   Zcmp_eqr   Zcmp_ner   Zcmp_ltr   Zcmp_ler   Zcmp_gtr   Zcmp_ger   rN   contextmanagerr%  rU   rU   rU   rV   <module>   s   $	H  

                  l