o
    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rd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                
       sH  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deedef eee edef f f fddZe
ejdd Ze
dd Zdd Zd fddZdefddZdd Z fddZdd d!Zd"d# Zd$d% Zd&d' Z d(d) Z!e
d*d+ Z"d,d- Z#e
d.ee$ d/e%fd0d1Z&dd4d5Z'i Z(eeedf ed3e)e eeef gef f e*d6< d2d3d7e)d8 d9d:dd8fd;d<Z+	7	=	9	:		8d fd>d?Z,dd@dAZ-e-Z.e-Z/ddBdCZ0ddDdEZ1ddFdGZ2ddHdIZ3ddJdKZ4e2Z5e2Z6ddMdNZ7ddOdPZ8ddQdRZ9ddSdTZ:ddUdVZ;dWdX Z<ddYdZZ=dd[d\Z>	d	3dd]d^Z?d_d` Z@ddadbZAddcddZBeAZCeAZDddedfZEdgdh ZFddidjZGe
ddkdlZHe
	2	3ddmdnZIddodpZJddqdrZKddsdtZLddudvZMddwdxZNddydzZOdd{d|ZPdd}d~ZQd2d3dLefddZRdddZSdddZTdddZU	dd2d3dedefddZVd2d3dededefddZWd2d3dedefddZXd2d3defddZYd2d3defddZZd2d3ded9efddZ[dddZ\dddZ]dddZ^dddZ_dddZ`dddZadddZbebZcdddZdedZedddZfd2d3dedef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   e/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torch/_dynamo/variables/builtin.pycreate_with_source   s   z"BuiltinVariable.create_with_sourcec                  C   sd  h 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j0tj1tj2tj3tj4tj5tj6} 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>   s   " 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   s   	
 !"#$%&'()*+,-./012345678:z(BuiltinVariable._constant_fold_functionsc                 C      | j |  v S rX   )rO   r   selfrU   rU   rV   can_constant_fold_through      z)BuiltinVariable.can_constant_fold_throughc                  C   s   h 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'} | S rX   )(ru   ra   rv   rw   rx   rz   ro   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   s   	
 !"#$%&')z#BuiltinVariable._fx_graph_functionsreturn.c                  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__)ru   r   r   r   r   r{   r   r~   r   r}   r   r   r   ro   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}| | tf|f | | tf|f |dd dd}	| | ttf|	f | | ttf|	f | | ttf|	f | | ttf|	f q,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}ttf|fttf|
fttf|
fg}| tj | 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 |sSJ || vsZJ ||| |< qJ| S )!Nr1   )BaseUserFunctionVariableUserFunctionVariableNNModuleVariable)supported_const_comparison_ops)BaseTorchVariable)UserDefinedClassVariablerJ   rK   )forward_namereverse_namec                   s.   t | r|| ||gi S || ||gi 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   r   rM   c                S   s   | | ||gi S rX   r   )r   r   r   r   rU   rU   rV   user_defined_inplace_handler     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      t g |j|| S rX   )rD   itemsunpack_var_sequencer   r   r   rU   rU   rV   tuple_add_handler     z:BuiltinVariable._binop_handlers.<locals>.tuple_add_handlerc                 S   r   rX   )rB   r   r   r   rU   rU   rV   size_add_handler  r   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fdd}t t f|fg}tv r?t 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 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fdd}	|ttf|	f |S )Nr   rM   c                       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                    r   rX   )r6   rO   r   r   rU   rV   r         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                    sx   t |t |urt jdkS ||u rt ||S t|tjr6t|tjr8|j|jur:t ||S d S d 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r   rM   )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_handlersr  )!	functionsr   r   	nn_moduler   r   r   torchr   user_definedr   rJ   rK   rN   r   r   r  r4   rG   rB   rD   r6   rA   r?   r@   ru   r   r   r   r{   rF   r   re   )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   r   rX   )rO   r   r   rU   rU   rV   can_insert_in_graph  r   z#BuiltinVariable.can_insert_in_graphNc                    s   t  jdi | || _d S )NrU   )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   )	rd   r  rj   Zint64rh   Zfloat64rO   r!  as_proxy)r   ZDTYPEr$  rU   rV   r*    s   


zBuiltinVariable.as_proxycodegenrL   c                 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     
zBuiltinVariable.constant_argsc                 G   4   d}|D ]}t |tjr dS |pt |tj}q|S NF)rY   r   GetAttrVariablerH   )r   r1  
any_tensorargrU   rU   rV   tensor_args     zBuiltinVariable.tensor_argsc                 C   r4  r5  )r  r   r6  rH   )r   	arg_typesr7  arg_typerU   rU   rV   tensor_args_type  r:  z BuiltinVariable.tensor_args_typec                 O   s\   g }g }t || D ]}t|tjr|| q|| qtdd |D o-| j| S )Nc                 s   s(    | ]}|j d urt|j ndV  qd S r5  )rQ   r   r^   trU   rU   rV   r`     s
    
zBBuiltinVariable.python_and_tensor_constant_only.<locals>.<genexpr>)		itertoolschainr   rY   r   rH   r  rb   r2  )r   r1  r#  r9  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      g | ]}|  qS rU   r   r]   rU   rU   rV   
<listcomp>$      z=BuiltinVariable.unwrap_unspec_args_kwargs.<locals>.<listcomp>c                 S      i | ]	\}}||  qS rU   rE  r^   kvrU   rU   rV   
<dictcomp>$      z=BuiltinVariable.unwrap_unspec_args_kwargs.<locals>.<dictcomp>)r   r1  r#  rU   rU   rV   unwrap_unspec_args_kwargs"  s   z)BuiltinVariable.unwrap_unspec_args_kwargsc                 C   s   |   ot||S rX   )r   r#   r0  rU   rU   rV   has_constant_handler(  s   
z$BuiltinVariable.has_constant_handlerr;  
has_kwargsc                    s  ddl m  t	g t fddD r	fddS tr6tts+tu r6		dfd	d
}|S 		 rWt
ju rGtd tjrW	rO	jS rW	j tdkrstjgR  skntdkr~\fdd ndfdd}| t	dj d 

rd	
fdd}| 	 rtdd D rsdfdd}ndfdd}| fddtdkrۇfddS tdkr\dfdd}|S dfdd}|S )Nr1   LazyVariableTrackerc                 3   s    | ]}t | V  qd S rX   )r  r>  rR  rU   rV   r`   4      z0BuiltinVariable._make_handler.<locals>.<genexpr>c                    s     | dd |D |S )Nc                 S   rD  rU   realizer^   rK  rU   rU   rV   rF  6  rG  zCBuiltinVariable._make_handler.<locals>.<lambda>.<locals>.<listcomp>)r   r   r1  r#  objrU   rV   r   5  s    z/BuiltinVariable._make_handler.<locals>.<lambda>r   rM   c                    sH    t u rtdd |D st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   rr   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)AssertionErrorrb   r   rr   r   SUPPORTABLEr   r  rX  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   r1  _)binop_handlerrU   rV   r   g  s    c                    s*    D ]}|| g|R  }|r|  S qd S rX   rU   )r   r1  rc  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 d S  t yq   ztj| g|R i | W   t yp } z7||}|sdtd| tdd d| d| d j g t	j
d W Y d }~Y d S W Y d }~Y d S d }~ww  ty } z||}|s |  W Y d }~d S d }~ww )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 r[  )	TypeErrorinspect	signaturebindrP  logwarningr   r  r   Z	DIFFICULTr   remove_from_stats)r   r1  r#  r  erP  exc)rO   rZ  self_handlerrU   rV   call_self_handleru  sB    
z8BuiltinVariable._make_handler.<locals>.call_self_handlerc                 s       | ]}t |tV  qd S rX   )r  r6   r]   rU   rU   rV   r`     rT  c              
      s   z dd |D  }W nE t y- } ztt|| tttj|jd W Y d }~n(d }~w tyP } zt	dd  d| dg t
j|d W Y d }~nd }~ww t| |S )	Nc                 S   rD  rU   rE  r]   rU   rU   rV   rF    rG  PBuiltinVariable._make_handler.<locals>.constant_fold_handler.<locals>.<listcomp>r1  constant fold exceptionattempted to run function  with arguments 7Encountered exception when attempting to constant fold.r\  r]  r^  r_  Zfrom_exc)	Exceptionr   rZ   listmapr6   r  r1  r2   r   r   
DYNAMO_BUGr4   r   r   r1  r#  resrq  r   rU   rV   constant_fold_handler  s,   z<BuiltinVariable._make_handler.<locals>.constant_fold_handlerc              
      s   t ||rfz dd |D i dd | D }W nE ty> } ztdd  d| dg tj|d	 W Y d }~n&d }~w ty_ } ztt|| t	t
tj|jd
 W Y d }~nd }~ww t| |S d S )Nc                 S   rD  rU   rE  r]   rU   rU   rV   rF    rG  ru  c                 S   rH  rU   rE  rI  rU   rU   rV   rL    rM  zPBuiltinVariable._make_handler.<locals>.constant_fold_handler.<locals>.<dictcomp>rw  rx  ry  rz  r{  rv  )r#   r   r2   r   r   r  r|  r   rZ   r}  r~  r6   r  r1  r4   r   r  r   rU   rV   r    s8   

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   rD  rU   )Zpython_type_namer^   r8  rU   rU   rV   rF    rG  zPBuiltinVariable._make_handler.<locals>.call_unimplemented_v2.<locals>.<listcomp>z Failed to trace builtin operatorzbuiltin rh  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.r[  )r   r  )r1  Zreal_arg_types)r;  rO   rQ  rU   rV   call_unimplemented_v2  s    

z<BuiltinVariable._make_handler.<locals>.call_unimplemented_v2c                    s    |S rX   rU   rX  )r  rU   rV   r     s    c                    s    | ||}|r
|S  | d S rX   rU   )r   r1  r#  re  )r  r   rU   rV   builtin_dispatch  s   z7BuiltinVariable._make_handler.<locals>.builtin_dispatchc                    s.   D ]}|| ||}|r|  S q | d S rX   rU   )r   r1  r#  rO   re  )r  r  rU   rV   r    s   r  )ZlazyrS  rN   rc   rj  isclassr  r|  GeneratorExitr   ru   r   r   rH   r=  _handle_insert_op_in_graphr  rk   r  ri   r  r   rb   )rO   r;  rQ  rb  rg  rs  r  r  rU   )rS  r;  rd  rf  r  rO   r   r  rQ  rZ  rr  rV   _make_handler-  sj   



!
zBuiltinVariable._make_handlerr   rM   c                 C   sj  ddl m}m} |r| jg || R  sd S ddl m} ddlm}m}m	}	m
}
 |	|||ro|js_t|d tsY| j|v rE|| j }n|| j }|d }|d |d< ||d< qb|| j }n| j}|||}|
||||S | j}z| j|i |rddlm} ddlm} |||||j||W S |tv rt|d tjrt| |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 rt&|dkrt|d tjr|d '|d|dd  |W S |j j!d|gt#||R  }tdd
 |D r4|t(||W S t)||rb| *||\}}||i |}tdd
 t+,|| D }|t-||||dW S t.dd
 |D rtt||d W S |tj/u rt|d tj-r|d 0 |d< |||W S  t1y   t2dd|  d| d| d| j dg t3j4d Y d S w )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   r  rU   rU   rV   r`   D  s    
z=BuiltinVariable._handle_insert_op_in_graph.<locals>.<genexpr>r   __eq__c                 s   rt  rX   rY   rE   r  rU   rU   rV   r`   ]  rT  c                 s   "    | ]}t |tjr|jV  qd S rX   rY   r   rI   need_unwrapr]   rU   rU   rV   r`   g      

)	raw_valuer  c                 s   rt  rX   rY   rG   r]   rU   rU   rV   r`   t  rT  z,unimplemented builtin op on tensor argumentszpartial tensor op: rh  z3Dynamo does not know how to trace builtin operator z with tensor argumentsr[  )5r   r   r  r9  r   r  Ztorch_functionr  r  r  r  exportrY   rH   rO   r  rC  Zbytecode_transformationr  r  r  r  IN_PLACE_DESUGARING_MAPr   r6   ru   r   rG   r  selectr"   rc   r   r   r-   r.   NumpyNdarrayVariabler   rk   r   rE   r$   rO  r@  rA  rI   rb   r~   r   NotImplementedErrorr   r   ra  )r   r   r1  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_cacher1  r4   r#  dict[str, VariableTracker]c                 C   s   |rdd |  D }| jgdd |D dR }n| jgdd |D R }| j|}|sB| | jdd |D t| | j|< }||||S )	Nc                 S   rH  rU   rU  rI  rU   rU   rV   rL    s    z1BuiltinVariable.call_function.<locals>.<dictcomp>c                 s       | ]}t |V  qd S rX   rZ   r]   rU   rU   rV   r`         z0BuiltinVariable.call_function.<locals>.<genexpr>Tc                 s   r  rX   r  r]   rU   rU   rV   r`     r  c                 S   s   g | ]}t |qS rU   r  r]   rU   rU   rV   rF    rG  z1BuiltinVariable.call_function.<locals>.<listcomp>)r   rO   r  r  r  rd   )r   r   r1  r#  keyr   rU   rU   rV   r     s   zBuiltinVariable.call_functionlist[VariableTracker]c                    s:  | j tu r9|dkr9t|dksJ t|dksJ |\}}}| }t|tr9|jj|r9|	 r9|
|||S |dkr| j tu r_t|dkr_t|dksPJ |jj| |d |dd  S | j tu rt|dkr|st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 rt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 r&|d	kr&tj|tg|R i |S | j tu rft| j |}|tv rft|d tjrO|d j|||dd  |S t|d tjrf|d |||dd  |S | j tu rt|dkrt| j |}|tv rt|d tr|d |||dd  |S t  ||||S )
N__setattr__   r   __new__r1   r   r   r"  fromkeys)!rO   r  rk   rV  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   r}  rA   r6   call_custom_dict_fromkeysri   r&   ZUserDefinedDictVariableZ_dict_vtr   rr   r/   r!  )r   r   r(  r1  r#  rZ  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r8t |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   rj   r
   r	   Ztorch._dynamo.variables.builderr   r   r   r*  )r   r   r8  r  Zfn_r   rU   rU   rV   _call_int_float  s    zBuiltinVariable._call_int_floatc                 C   sL   t |tr$t |jtjr|S t|jtjtjfsJ t||	 dkS d S Nr   )
rY   rG   sym_numr  ZSymBoolr,   ZSymIntZSymFloatr  r*  )r   r   r8  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rt|j	dr!|j	j
}nt|j	dr,|j	j}ntdt|dg tjd t|j	j
tj
u rVz	tjj| dW S  tyU   Y d S w t|rktddt|j	 d	d
gd d S |j}zt|}W n ty } ztd| W Y d }~d S d }~ww |||gi S t |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__ methodr[  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   rE  )r^   r   rU   rU   rV   r`   `  r  z+BuiltinVariable.call_str.<locals>.<genexpr>)rY   r   r   r6   r  rr   rO   rJ   hasattrrT   r  r)  r   r   
USER_ERRORrZ   r  AttributeErrorr+   __func__r`  rm  rn  r   r  rk   r1  r  join)r   r   r8  Z
str_methodbound_methodZuser_func_variablerp  rT   rU   rU   rV   call_str,  sR   


	
zBuiltinVariable.call_strc                 G   sr   t |dkr|d |r|d |}| ||S t |dkr+| ||d |d S t |dkr7| ||S d S )Nr1   r   r   )rk   has_force_unpack_var_sequenceforce_unpack_var_sequence_call_min_max_seq_call_min_max_binary)r   r   r1  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   )rk   	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 |  ||rt|tjs||}}t|tjs$J t|tr5ttj||gi }t|t	s?t|t	r]ddl
m} |t|||jjd| jgt||gi R  dS | rt|tjrrdd l}t|j}nttj}| jtu rd|ind|i}|||g|}n4t|tjrd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 t|t	st|t	rA| jtu r'tj!ntj"}|jjd|gt||gi R  }t	#||d S t|t$r[t|t$r]| | | }t$|S d S d S )Nr1   r  r   r  r   rm   rl   c                 s   s"    | ]}t |tjtjfV  qd S rX   )rY   r   rI   r6   )r^   rB  rU   rU   rV   r`     s    
z7BuiltinVariable._call_min_max_binary.<locals>.<genexpr>c                 s   rt  rX   r  )r^   r  rU   rU   rV   r`     rT  c                 s   r  rX   r  r]   rU   rU   rV   r`     r  )%r9  rY   r   rH   rE   TorchInGraphFunctionVariabler  r   r   rG   r   r  rZ   r   r   rO   r.   r  r  numpyZNumpyVariableZclipclamprl   maximumrm   minimumrb   rc   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_binaryr8  c                 C   *   t t||tdgi }||g i S )N__abs__rN   ri   r   r6   r  )r   r   r8  Z
abs_methodrU   rU   rV   call_abs     zBuiltinVariable.call_absc                 C   r  )N__pos__r  )r   r   r8  Z
pos_methodrU   rU   rV   call_pos  r  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 Tensorr[  )	rY   r   rH   r   r)   ru   r   r6   r  )r   r   r8  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   r8  r1  r#  Zround_methodrU   rU   rV   
call_round  r  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)   r  rU   rU   rV   r`     s    
z-BuiltinVariable.call_range.<locals>.<genexpr>)r#   r   ZRangeVariable_dynamic_argsr  r   r   r1  rU   rU   rV   
call_range  s   



zBuiltinVariable.call_rangec                 O   s(   t dd |D pt dd | D S )Nc                 s   rt  rX   r  r]   rU   rU   rV   r`     rT  z0BuiltinVariable._dynamic_args.<locals>.<genexpr>)rc   r   r0  rU   rU   rV   r    s   zBuiltinVariable._dynamic_argsc                 G   s
   t |S rX   )r   ZSliceVariabler  rU   rU   rV   
call_slice  r3  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   r1  r#  r   rU   rU   rV   
_dyn_proxy  s   zBuiltinVariable._dyn_proxyc                 O   s  t |tjrJ | j|i |r| j|g|R i |S tj| j}|d u r.|g t dS |	|r|j
rt|j
st |trJt|j
tj n7t|ddrbt |trbt|tsb|jj|j
 t |tjrrt|j
tj nt |tjst|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_LENri   r9   r,   r>   r   Zguard_on_key_orderr   ZMappingProxyVariableZMAPPING_KEYS_CHECKUnspecializedNNModuleVariableZSEQUENCE_LENGTHr}  r   r   r   rZ  r1  r#  rS   rU   rU   rV   _call_iter_tuple_list  sF   


z%BuiltinVariable._call_iter_tuple_listc                 O   s&   t j| j}|t||t dS r   )r   r?   r  rO   r}  r  r3   r  rU   rU   rV   _call_iter_tuple_generatorG  s
   z*BuiltinVariable._call_iter_tuple_generatorc                 O   sr   t |tjrtj| j}|t||t dS t |tj	r,| j
||g|R i |S | j||g|R i |S r   )rY   r   r  r?   r  rO   r}  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 r"||d||S |S )N__iter__)rY   r   r  r  r   )r   r   rZ  r1  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
r,tj	t|jS t|ttttttfr=tj	dS d S )Nr1   )r   FunctoolsPartialVariabler   TF)r  r   r  r  r   rY   r   r   r6   r  rK   re   rT   rG   rH   rA   rD   r@   )r   r   r8  r   r  r   rU   rU   rV   call_callablek  s2   	
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 rh  z0Dynamo expects exactly 2 args to builtin cast().z3Ensure your call to cast() has exactly 2 arguments.r[  )rk   r   )r   rc  r1  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   r1  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_clsr1  r#  rU   rU   rV   r    s
   z BuiltinVariable.call_custom_dictc                 O   s^  |t tthv s	J |r-|tu sJ t|dkr!t|dkr!d|v s#J g ||dR }t|dkr8ttdt|dkrHg |td R }t|dksPJ |\}}|turZt	nt
}t|t rvd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^   rJ  rU   rU   rV   rF    s    z=BuiltinVariable.call_custom_dict_fromkeys.<locals>.<listcomp>r   c                 s   r  rX   )r=   rW  rU   rU   rV   r`     r  z<BuiltinVariable.call_custom_dict_fromkeys.<locals>.<genexpr>zfailed to call dict.fromkeys()z.fromkeys(): rh  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.r[  )r  r   r   rk   popr   ri  r6   r  r9   r:   rY   keysr  r3   r  r  rb   r   r  )r   r  r1  r#  r8  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r$tt|tdt| dgd |d }t|tj r5|j	t dS |
|rF||}t |t dS t|tjrtt|jtrt||d}t|tjrt||||}t|t rl|S tt||S tt|tdgd d S )	Nr   r1   z&set() takes 1 positional argument but  were givenrv  r   r  z!failed to construct builtin set())r>   r3   rk   r   ri  r6   r  rY   r   cloner  r  rJ   rT   r   var_getattrZUserMethodVariabler   rN   setcall_set)r   r   r1  r#  r8  r   Ziter_fnoutrU   rU   rV   r    s@   	




zBuiltinVariable.call_setc                 O   s   |rJ |s
t g S t|dkr!tt|tdt| dgd |d }t|tj r5t dd |jD S |	|rC|
|}t |S tt|tdgd d S )	Nr1   z,frozenset() takes 1 positional argument but r  rv  r   c                 S   s   g | ]}|j qS rU   )vtr]   rU   rU   rV   rF     r  z2BuiltinVariable.call_frozenset.<locals>.<listcomp>z'failed to construct builtin frozenset())r<   rk   r   ri  r6   r  rY   r   	set_itemsr   r   )r   r   r1  r#  r8  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                    $   g | ]}|  r| n|qS rU   r   r   r  r   rU   rV   rF        z,BuiltinVariable.call_zip.<locals>.<listcomp>)r  r   )rk   r  r   ZZipVariabler3   )r   r   r1  r#  r  rU   r  rV   call_zip
  s   
zBuiltinVariable.call_zipc              
   O   s^   z|d  |d|dd  |W S  ty. } ztt||t|jd W Y d }~d S d }~ww )Nr   __len__r1   rv  )r   r  r   rZ   r}  r1  )r   r   r1  r#  rp  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y$   tdd  d| dd  g tjd Y nw | }t tjrC j	d urC fdd}tj
| |S t tjrbt jtjrbtd	d  d| dd
g tjd t tjrzd|jjv rztj
|j| jS t tjrt
t||S t|tstt|dd r|f}n(tjdkrt|tjr|j}nt|trtdd |D r|}ntt|dgd zt ||}W n ty   ||v }Y nw 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 r[  c                    s6   fdd t |tu rt fdd|D S  |S )Nc                    sX   | t vr#  jjd }t|r| tjjju rt	|| 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r8  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  r  zNBuiltinVariable.call_isinstance.<locals>._tensor_isinstance.<locals>.<genexpr>)rZ   r  rc   )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   re   ri   )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 unionrv  )!r  r  r   r   r  r   rY   r   rH   r  r6   r  rJ   rT   typesMemberDescriptorTypera  r   r   r  Z!UserDefinedExceptionClassVariablerZ   re   ri   sysversion_info	UnionType__args__r  rb   r   ri  r  )r   r   r8  Zisinstance_typer<  r  Zisinstance_type_tupler  rU   r  rV   call_isinstance  st   

	

zBuiltinVariable.call_isinstancec              
   C   s^   z
|  }|  }W n ty&   tdd| d| dddgtjd Y nw 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.r[  )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   sR   z| |W S  ty( } zt|tjr#|  |jd W  Y d }~S  d }~ww r  )Znext_variabler   rY   r   r?   ro  r   )r   r   r8  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   rZ  attrr(  rU   rU   rV   call_hasattr  s   zBuiltinVariable.call_hasattrc                    s$    fdd|D }t j||t dS )Nc                    r  rU   r  )r^   r   r  rU   rV   rF    r  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_filterrZ  r  c              	      s  |  stdd| d| d| dddgd | }t|tr&|dkr&d	}jj|rSt|tj	rS|d
v rS|j
rSjj|rStdd| d| d| ddg d jj||rcjj||S |d ur|| ||}| dv svJ | s||S |jot|j| |dv rz<| }t|tr|dkr fddt|jD }tj| dW S |dkrt|j W S |dkrt|jW S W n	 ty   Y nw t|tjr||S t|tjtjtjtjtj tj!fr`t|tj!rt"|j#j$t%j&rt'j(r|dv rtdd| d| dd| dgd t|trF|j)j*j+d }	t|	t,j-rFt.|	rFj/r;t'j0sFtdddg t1j2d z||W S  ty_   tj3|| d Y S w t|tj4rt5|j#|}
t|
t,j6j7t,j6j8frt,j9j:;|
rtj4|
 dS |t<v rtj3|| dS d S t|t=r|j>s||j#j?vrt5|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 w )!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 stringr[  _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__r0  c              	      s(   g | ]\}}t | ot |qS rU   )r4   r   r   )r^   rB  r   rQ   r   rU   rV   rF    s    z0BuiltinVariable.call_getattr.<locals>.<listcomp>rP   r/  r1  )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   	enumerater0  rD   r4   r   r/  r6   r  r1  r  r   r  NamedTupleVariableZDistributedVariabler   rJ   r  rT   r   unittestZTestCaser   Zenable_trace_unittestr  r  r  r  Tensorr   r  Zcapture_sparse_computer   ra  r6  r  ri   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   rZ  r  defaultr(  Zhasattr_varrT   Z
tuple_argsZfake_valmemberrU   r2  rV   call_getattr  s   




	

zBuiltinVariable.call_getattrr  c              	      s  t  tjtjtjtjtjfr |d||gi S |jj	
 r	| r	| }t  tjrddlm} |dkrPtdd  d| d| dd	g tjd
 n|dkr܈ jd u rltdd  d| d| dddgd
  fdd|jjD }|D ]	}|jj| qyt|1 t  |||jjdtjjgt |gi R  }	W d    n1 sw   Y  W d    n1 sw   Y  dd }
|jd|
|	 fi  |
  jjd  |	S |dv rd}nt |rtdd  d| d| ddd| dgd
 |jj	! || |S t  tj"rs|j# st$d| rlt |tjrlt%| j|}z
 &|| }W n t't(fyF   d }Y nw t |tjrlt%| j|}t)* j+d}||u rl|tj,j-j.u rl|S  /| d 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.r[  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^   tfrY  rU   rV   rF    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PlacementVariabler5  rJ   ZNestedUserFunctionVariabler  r   r   r   r  r  r   rH   r   r   r   r   ra  rQ   Ztracked_fakesremovedynamo_disable_gradr  Zno_gradr   r7  Zset_r.   r*  r  r  r*   Z
store_attrr   Zis_root_tracerr   r(   r  r  r   rj  getattr_staticmodule_typer  Moduler  Zconvert_to_unspecialized)r   r   rZ  r  r  r(  r   	to_remover<  r  r?  Zassigning_fake_valZgetattr_varZexisting_fake_attrZmod_setattrrU   rY  rV   call_setattrU  s   


 

zBuiltinVariable.call_setattrc                 C      | |d|gi S )N__delattr__r   )r   r   rZ  r  rU   rU   rV   call_delattr  s   zBuiltinVariable.call_delattrc              
   C   s   z|  }W n ty } zttjt|ddd d }~ww |jo%t|j}|d u r6t|t	j
r6|jr6|j}|tju rI|jdt}tt|d}t|||S )NZunknown_python_type)Z	case_namer  r7  )r  r  r   r   ZINVALID_INPUTrr   rQ   r    rY   r   rJ   Z
cls_sourcer  r7  r   Zinstall_global_by_idr   r   r4   r   )r   r   rZ  Zpy_typeerrorrQ   r(  rU   rU   rV   	call_type  s0   

zBuiltinVariable.call_typec                 C   s*   | |rtt||}t|S d S rX   )r   r}  reversedr   r   rD   )r   r   rZ  r   rU   rU   rV   call_reversed		  s   

zBuiltinVariable.call_reversedc                 K   sH   | |r t|tjs"tj||t d}||dg | |S d S d S )Nr   sort)r  rY   r   rH   rA   r  r3   r   )r   r   rZ  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  ru   rw   r*  r   r   r   rU   rU   rV   call_neg	  s   
zBuiltinVariable.call_negc                 O   s    |  }t|}tj|||S rX   )r   rr   r   ZStringFormatVariabler  )r   r   Z_format_stringr1  r#  format_stringrU   rU   rV   call_format)	  s   zBuiltinVariable.call_formatc                 G   sb  t |dkr!t|d tjr!|d }|j|j}tjt	|S t |dkrOt|d tj
tjfrO|d jrBt|d jtj t	|d j}tj|S t |dkret|d tre|d }||S t|d tjrxtjt	|d jS t|d tjrtjt	|d jS t|d tjrt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.r[  )rk   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   rr   r   ra  )r   r   r1  Znn_mod_variabler   Zconstant_resultZtensor_variablerU   rU   rV   rV  .	  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()r[  )r   r   ra  )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 rCt|t	o2t|t	o2t
t| jt
t| jk}|tju r=t|S t| S ||vrftd|j d| d| dd|j d	| d| g tjd
 t|t	rt|t	r|jou|jd ur|j|jkrz
t|j|j W n) ty   td|j d| d| dd| d| d|j dg tjd
 Y nw 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 r[  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   rY  rO   ru   r   is_notrY   rH   rU  r'   r*  r  r6   r  r   r  r   ra  sizer  Zbroadcast_shapesRuntimeErrorr  r   r   rZ   )
r   r   r  r	  r  rY  r   Z	is_resultZ
tensor_clsr  rU   rU   rV   r  [	  sl   





	z'BuiltinVariable._comparison_with_tensorc              	   C   s   ddl m} | j}||vr,td|j d| d| dd|j d| d| g tjd	 t|tj	r;t
|t d S |jd
|| | fi }tj||d dS )Nr1   rX  z!unsupported SymNode comparison opr&  r  r'  rZ  z with SymNode arguments r[  r   rP  )r   rY  rO   r   r  r   ra  rY   r   r   r6   r  r   r   r*  rG   r  )r   r   r  r	  rY  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r1t |ttfr1tj||jjdtjgt||gi R  d dS t	|drEt	|drGt
t|j|j@ S d S d S )Nr   rP  r  )rY   r6   rG   r  r   r   ru   r   r.   r  r>   r}  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r1t |ttfr1tj||jjdtjgt||gi R  d dS t	|drEt	|drEt
t|j|jB S t |trT|j|d|gi dS d S )Nr   rP  r  __or__rN  )rY   r6   rG   r  r   r   ru   r   r.   r  r>   r}  r  r9   r   r"  rU   rU   rV   call_or_	  s&   
zBuiltinVariable.call_or_c                 C   sl   t |trtj||jjdtjgt|gi R  d dS t |tr#|j	}t |t
tfr4tt|jdkS d S )Nr   rP  r   )rY   rG   r  r   r   ru   rx   r.   r;   Zdv_dictrA   r9   r6   rk   r   rQ  rU   rU   rV   	call_not_	  s   


	zBuiltinVariable.call_not_r   r   c                 C   rG  )N__contains__r   r"  rU   rU   rV   call_contains	  r   zBuiltinVariable.call_contains)r   N)r+  rL   r  )r1  r  r#  r  r   r4   )r   rM   r8  r4   rX   )ir  r-  __qualname____doc__r  Z	_SENTINELr4   Z_nonvar_fieldsclassmethodrW   staticmethodr  cacher   r   r   r  r   r  r}  rr   r   r  r  r   r"  r)  r   r*  r/  r2  r9  r=  rC  rO  rP  rZ   rd   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:  rF  rI  rK  rM  rO  rR  rT  rV  rW  r  r  r_  Z	call_iandra  Zcall_iorrb  rd  __classcell__rU   rU   r$  rV   rN      sb  
 

A,"%   



 
K 



[


7
	
c







.




-
$




f

	

	
 
 






"

5


rN   c              	   c   sH    ddl m} || d}z||  d V  W ||  d S ||  w )Nr1   )GradModeVariableF)r  rm  r  Zenterexit)r   rm  ZgmvrU   rU   rV   rA  	  s   
rA  )rf  
contextlibr  rj  r@  loggingr[   ru   r  r  typingr6  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   rq  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  rm  r   r   r   r   r   r{   r   r}   r   r~   r   r   r   r   r   r   r   r   ro   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   contextmanagerrA  rU   rU   rU   rV   <module>   s   $	H  

                  l