o
    j9:j                    @   sB  U 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mZmZ ddl
mZmZmZ ddlZddlmZ ddlmZ ddlmZ d	d
lmZm Z m!Z!m"Z" d	dl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* d	dl+m,Z,m-Z- d	dl.m/Z/ d	dl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d	dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZK ddlLmMZMmNZNmOZOmPZP ddlQmRZRmSZS ddlTmUZUmVZVmWZWmXZXmYZY ddlZm[Z[ ddl\m]Z]m^Z^m_Z_m`Z`maZambZbmcZc ddldmeZe ddlfmgZgmhZhmiZi ddljmkZkmlZlmmZmmnZnmoZo ddlpmqZqmrZrmsZsmtZt er>ddlumvZv ddlwmxZx eyezZ{ej|ej}ej~ejejejejejejejejejejejejejejejejejejejejejejejiZeeddheeddheeddhiZeeee f ed < ejd!kred"hee< ed#e
je
jgePdB f ZeeP eeeP d$f B Zeje!jeje!jeje!jeje!jeje!jeje!jiZejejejejejejej}ejejejejejejejejejejejejejej|ej~fZejejejejejejfZejfZejejejejejfZejejejejejfZeWeXeiesfZeeeP d$f ed%< i aeed$ef ed$ef f ed&< i aeed$ef ed$ef f ed'< d8d)d*ZG d+d, d,ePZG d-d. d.eZG d/d0 d0eZG d1d2 d2eZG d3d4 d4eZejd5d#d(e
jd fd6d7ZdS )9a\  
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)CallableIterableKeysViewSequence)AnycastTYPE_CHECKING)is_sparse_any)BaseTorchFunctionMode)is_traceable_wrapper_subclass   )configgraph_break_hints	polyfills	variables)ObservedAttributeErrorObservedUserStopIterationraise_observed_exceptionunimplementedUnsupported	UserErrorUserErrorType)GuardBuilderinstall_guard)DummyModule)
AttrSourceGetItemSourceGlobalSourceis_constant_source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frozenset_methodsget_fake_valueguard_if_dynis_tensor_getset_descriptoris_wrapper_or_member_descriptoristypenumpy_operator_wrapperproxy_args_kwargsraise_args_mismatchset_methodsspecialize_symnodestr_methodstensortype_to_dtype   )#AsPythonConstantNotImplementedErrorNO_SUCH_SUBOBJValueMutationNewVariableTracker)ConstantVariableFakeIdVariable)ConstDictVariableDefaultDictVariableDictItemsVariableDictKeysVariableDictViewVariableis_hashable)BaseListVariableListIteratorVariableListVariableRangeVariableSizeVariableTupleIteratorVariableTupleVariable)NullVariable)FrozensetVariableOrderedSetClassVariableSetVariable)FakeItemVariablesupported_comparison_opsSymNodeVariableTensorVariableUnspecializedPythonVariable)MutableMappingVariableUserDefinedDictVariableUserDefinedObjectVariableUserDefinedVariable)	PyCodegen)InstructionTranslator__new__
from_bytesfromhexhex"_BUILTIN_CONSTANT_FOLDABLE_METHODS      from_numberrZ   ._SET_LIKE_OP_SUPPORTBUILTIN_TO_TENSOR_FN_MAPBUILTIN_TO_TENSOR_RFN_MAPreturnc                     s  t tdkrd S d G fdddt} td tdtjdtjdtjdtjd|    fddtffddtf fd	dtffd
dt	ffddt
fg}|D ]\}}|D ]}|| d usmJ t|< qaq[fddtffddt	ffddt
fg}tjtjtjh}|D ]"\}}|D ]}||v rq|| d usJ t| krt|< qqW d    d S 1 sw   Y  d S )Nr   c                       sP   e Zd ZdZ		ddedef dedee deeef dB d	ef
 fd
dZ	dS )z8populate_builtin_to_tensor_fn_map.<locals>.GetMethodModez
        Mode to extract the correct methods from torch function invocations
        (Used to get the correct torch.Tensor methods from builtins)
         Nfunc.typesargskwargsrg   c                    s   |pi }| ||i |S Nrh   )selfri   rj   rk   rl   most_recent_funcrh   f/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/torch/_dynamo/variables/builtin.py__torch_function__   s   zKpopulate_builtin_to_tensor_fn_map.<locals>.GetMethodMode.__torch_function__)rh   N)
__name__
__module____qualname____doc__r   r   r   dictstrrr   rh   ro   rh   rq   GetMethodMode   s    	
ry   r7   )dtypec                       |  S rm   rh   o)inp0rh   rq   <lambda>      z3populate_builtin_to_tensor_fn_map.<locals>.<lambda>c                    r{   rm   rh   r|   inp0_intrh   rq   r     r   c                    
   |  S rm   rh   r|   )r~   inp1rh   rq   r        
 c                    r   rm   rh   r|   )r   inp1_intrh   rq   r     r   c                    s
   |  dS Nr   rh   r|   r   rh   rq   r     r   c                    
   | d S Nr7   rh   r|   )r   rh   rq   r     r   c                    r   r   rh   r|   )r   rh   rq   r   "  r   c                    r   r   rh   r|   r   rh   rq   r   #  r   )lenre   r   torchonesint32un_ops
un_int_opsbin_opsbin_int_opstensor_and_int_opsoperatormatmulimatmulgetitemrf   )ry   setups_and_oplistssetup_fnop_listoprsetups_and_oplistsrskipsrh   )r~   r   r   r   rp   rq   !populate_builtin_to_tensor_fn_map   sP   



	"r   c                   @   s   e Zd ZU dZdZeed< edededd fddZ	defd	d
Z
dddZdddedefddZdddedefddZdefddZdefddZdedefddZdS )BaseBuiltinVariablea  
    Common base class for all builtin variable trackers (BuiltinVariable,
    DictBuiltinVariable, IterBuiltinVariable, and future specialized builtins).

    Provides shared implementations for guard installation, hasattr tracing,
    and Python-level hashability/equality.

    Specialized subclasses (e.g. DictBuiltinVariable) set `_fn` as a class
    attribute. BuiltinVariable stores the callable on the instance as `self.fn`
    and overrides as_python_constant / reconstruct / var_getattr accordingly.
    N_fnvaluesourcerg   c                 C   s   t |tj | |dS Nr   r   
make_guardr   BUILTIN_MATCHclsr   r   rh   rh   rq   create_with_source@  s   
z&BaseBuiltinVariable.create_with_sourcec                 C      | j S rm   )r   rn   rh   rh   rq   as_python_constantE     z&BaseBuiltinVariable.as_python_constantcodegenrY   c                 C   s6   |   j}||jjvsJ d||j|dd d S )Nshadowed globalTadd)r   rs   tx	f_globalsappend_outputcreate_load_globalrn   r   namerh   rh   rq   reconstructH  s   
zBaseBuiltinVariable.reconstructr   rZ   r   c                 C   sH   | j ot| j |}t| j|d }tj| ||d urt||dS d |dS )Npy_typer   )r   r   getattrr   r   GetAttrVariabletype)rn   r   r   r   attrrh   rh   rq   var_getattrM  s   zBaseBuiltinVariable.var_getattrc                 C   s   t |t|  |S rm   )r;   buildhasattrr   )rn   r   r   rh   rh   rq   call_obj_hasattrT  s   z$BaseBuiltinVariable.call_obj_hasattrc                 C      dS )NTrh   r   rh   rh   rq   is_python_hashableY     z&BaseBuiltinVariable.is_python_hashablec                 C   s   t |  S rm   )hashr   r   rh   rh   rq   get_python_hash\  s   z#BaseBuiltinVariable.get_python_hashotherc                 C   s   t |to|  | u S rm   )
isinstancer   r   rn   r   rh   rh   rq   is_python_equal_  s   
z#BaseBuiltinVariable.is_python_equalr   rY   rg   N)rs   rt   ru   rv   r   r   __annotations__classmethodr!   r   r   r   rx   r;   r   r<   r   boolr   intr   objectr   rh   rh   rh   rq   r   1  s$   
 

r   c                       s	  e Zd ZU dZe ZdhejZede	de
dd fddZeejdeede	f  fd	d
ZdefddZeejdeede	f  f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eedef eeeee ef ef  f fddZedede	f dee dedee dB fddZdefddZe ee!ehZ"e e	 e#d< de	de	ddf fddZ$defdd Z%de	fd!d"Z&de	fd#d$Z'de	f fd%d&Z(dd)d*Z)d+ededefd,d-Z*d+edefd.d/Z+d0ee defd1d2Z,d+ededefd3d4Z-ed+e.e deeef deee	 eee	f f fd5d6Z/d+e.e deeef defd7d8Z0edede	f d0ee d9eded:ee eeef gedB f fd;d<Z1d=d:d+e	defd>d?Z2d=d:d+edefd@dAZ3ed=d:defdBdCZ4d=d:d+e.e deeef dedB fdDdEZ5i Z6eeedf ed:e.e eeef gef f e#dF< d=d:d+e.e deeef defdGdHZ7d=d:dIed+ee deeef def
 fdJdKZ8d=d:dLededB fdMdNZ9d=d:dLededB fdOdPZ:d=d:dLededB fdQdRZ;d=d:dLededB fdSdTZ<d=d:dLededB fdUdVZ=d=d:d+ededB fdWdXZ>d=d:dYe.e defdZd[Z?d=d:d\edB d]edB dedB fd^d_Z@e>ZAe>ZBd=d:dLedefd`daZCd=d:dLedefdbdcZDd=d:dLedefdddeZEd=d:dLed+ededef
dfdgZFd=d:d+ededB fdhdiZGd+ededefdjdkZHd=d:d+edefdldmZId=d:d+e	de	defdndoZJ	dd=d:dpedB d+edededB f
dqdrZKd=d:dped+ededef
dsdtZL	dd=d:dpedB d+edededB f
dudvZMeMZNd=d:dLededB fdwdxZOdye	d+edededB fdzd{ZPd=d:dLededB fd|d}ZQd=d:d+ededefd~dZRd=d:d+ededefddZSd=d:d+ededefddZTd=d:d+ededefddZUd=d:d+ededefddZVd=d:dLededefddZWd=d:dededefddZXd=d:d\ed]edefddZYd=d:d+edefddZZd=d:dpedededB fddZ[d=d:dedededef
ddZ\d=d:dededefddZ]d=d:dIedefddZ^	dd=d:dpedededB dedB f
ddZ_d=d:dpededededB f
ddZ`d=d:dpededefddZad=d:dpedefddZbd=d:dpededB fddZcd=d:dpedededB fddZdd=d:d\ededB fddZed=d:ded+ededef
ddZfd=d:d+edefddZgd=d:dedefddZhd=d:dededefddZid=d:dededefddZjd=d:d\ed]ededB fddZkd=d:d\ed]ededB fddZld=d:d\ed]ededB fddZmd=d:d\ed]ededB fddĄZnd=d:d\ed]ededB fddƄZod=d:d\ed]ededB fddȄZpd=d:d\ed]ededB fddʄZqd=d:d\ed]ededB fdd̄Zrd=d:d\ededB fdd΄Zsd=d:d\ed]edefddЄZtdedefddӄZu  ZvS )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.
    fnr   r   rg   c                 C   s   t |tj | ||dS r   r   r   rh   rh   rq   r   v  s   z"BuiltinVariable.create_with_source.c                  C   sh  h t 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j0tj1tj2tj3tj4tj5tj6tj7} ddl8m9} | :|;  | :dd t<j=; D  | S )Nr7   )rQ   c                 s   s$    | ]}t |ttjr|V  qd S rm   )r   r   mathsqrt.0xrh   rh   rq   	<genexpr>  s   " z;BuiltinVariable._constant_fold_functions.<locals>.<genexpr>)>absallanyr   callablechrcomplexdivmodfloatr   r   r   maxminordpowreprroundrx   formatsumr   r   posnegnot_truthinvertmulr   floordivtruedivmodr   subr   length_hintlshiftrshiftand_or_xoripowimulr   	ifloordivitruedivimodiaddisubilshiftirshiftiandixoriorindextensorrQ   updatevaluesr   __dict__)fnsrQ   rh   rh   rq   _constant_fold_functions{  s   	
 !"#$%&'()*+,-./0123456789;z(BuiltinVariable._constant_fold_functionsc                 C      | j |  v S rm   )r   r  r   rh   rh   rq   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 rm   )(r   r   r   r   r   r   r   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  rh   rh   rq   _fx_graph_functions  s   	
 !"#$%&')z#BuiltinVariable._fx_graph_functionsc                  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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__)__xor____rxor____ixor__)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r   r  rh   rh   rq   _binops  s2   zBuiltinVariable._binopsc                     sd  ddl m ddlm ddlm ddlm  ddlm	 i } t
  D ]\}\}}g | |< g | |< |\}}}||ddd	d
tdtdtdtdtffdd}| | tf|f | | tf|f |ddd	d
tdtdtdtf
dd}| | tf|f | | tf|f |ddd	d
tdtdtdtf dtf
dd}	| | ttf|	f | | ttf|	f | | ttf|	f | | ttf|	f q&dd	d
tdtdtfdd}
dd	d
tdtdtf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dtdtfd!d"}ttf|fttf|
fttf|
fg}| tj | dd	d#td$tdtfd%d&}ttf|fttf|fttf|fttf|fg}| tj | d'tdtf dtttt t f t!f  f fd(d)}t"# D ]}t$|sJ || vsJ ||| |< q| S )*Nr7   )BaseUserFunctionVariableNNModuleVariable)supported_const_comparison_ops)BaseTorchVariablerX   )forward_namereverse_namer   rZ   abr=  r>  rg   c                   s.   t | r|| ||gi S || ||gi S rm   )r   call_method)r   r?  r@  r=  r>  r<  rh   rq   user_defined_handler;  s   
z=BuiltinVariable._binop_handlers.<locals>.user_defined_handler)r=  c                S   s   | | ||gi S rm   rA  )r   r?  r@  r=  rh   rh   rq   user_defined_inplace_handler]  s   zEBuiltinVariable._binop_handlers.<locals>.user_defined_inplace_handlerr   r   .c                S   s2   ddl m} || | jjd|gt||gi R  S Nr7   wrap_fx_proxycall_function)builderrH  outputcreate_proxyr1   )r   r?  r@  r   rH  rh   rh   rq   dynamic_handlern  s   z8BuiltinVariable._binop_handlers.<locals>.dynamic_handlerc                 S      t g |j|| S rm   )rK   itemsunpack_var_sequencer   r?  r@  rh   rh   rq   tuple_add_handler     z:BuiltinVariable._binop_handlers.<locals>.tuple_add_handlerc                 S   rN  rm   )rI   rO  rP  rQ  rh   rh   rq   size_add_handler  rS  z9BuiltinVariable._binop_handlers.<locals>.size_add_handlerc                 S   s   t g || |jS rm   )rK   rP  rO  rQ  rh   rh   rq   r     s    z1BuiltinVariable._binop_handlers.<locals>.<lambda>c                 S   s   t g |j|| t dS Nmutation_type)rG   rO  rP  r:   rQ  rh   rh   rq   r     s    c                 S   s   t |g |j|jS rm   )r   rO  rQ  rh   rh   rq   r     s    c                 S   s>   |  s	|| sd S || }| jj| |j| |S rm   )is_immutablehas_unpack_var_sequencerP  rK  side_effectsmutationrO  extend)r   r?  r@  seqrh   rh   rq   list_iadd_handler  s   
z:BuiltinVariable._binop_handlers.<locals>.list_iadd_handlerlstconstc              
   S   s   t |ts| r||}}zt |tsJ |j|j|  t dW S  tyB } ztt	|| t
|jd W Y d }~d S d }~ww )N)rO  rW  rk   )r   rE   is_python_constant	__class__rO  r   r:   MemoryErrorr   r   listrk   )r   r_  r`  excrh   rh   rq   expand_list_like  s    
z9BuiltinVariable._binop_handlers.<locals>.expand_list_liker   c           
   	      s  dddt dt dt ffdd}ttf|fg}tv rPttf|fg}t}|tt f|jft tf|jfg dddt dt dt ffdd	}|t t f|f |S ttf|fg} v rЈj	
d
rЈt d  dddt dt dt f fdd}|}|}ttttf}||tf|ft|f|fg t}|tt f|jft tf|jftt f|jft tf|jfg dddt dt dt d B ffdd}	|t t f|	f |S )Nr   rZ   r?  r@  rg   c              
      sX   zt |  |j|jW S  ty+ } ztt|| t|jd W Y d }~d S d }~ww )Nra  )r;   r   r   	TypeErrorr   r   re  rk   )r   r?  r@  rf  r   rh   rq   compare_by_value  s   zYBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.compare_by_valuec                    s   |  t| t  ||gi S rm   )inline_user_function_returnr;   r   polyfill_fn_mappingrQ  ri  rh   rq   handlerE  s   zPBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.handleris_c                    s   t |  S rm   r;   r   rQ  )none_resultrh   rq   neverW     zNBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.neverleftrightc                    sH   ddl m} |||}|d u rd S | }t|  jdkr |S | S )Nr7   )vt_identity_comparern  )object_protocolru  r   r;   r   rs   )r   rs  rt  ru  resultis_sameri  rh   rq   	handle_is  s   
zRBuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers.<locals>.handle_is)r;   r<   rl  r   r\  rR   _comparison_with_symnodeappendr	  rs   
startswithr   rS   rE   r>   _comparison_with_tensor)
r   rj  rw  op_varrm  rq  obj_op_nonenone_op_objtypes_that_are_never_nonery  r;  r7  r9  rX   r:  )rp  r   rq   create_cmp_op_handlers  s   


z?BuiltinVariable._binop_handlers.<locals>.create_cmp_op_handlers)%	functionsr7  	nn_moduler9  r  r:  r   r;  user_definedrX   r   r6  rO  r;   rx   r{  r   r   rR   rE   rI   rK   r<   rG   rF   r   r   r\  r   r   re  tuple_TrackersType_HandlerCallbackrQ   r	  r   )op_handlersr   magic_method_namesin_place_opr=  r>  inplace_namerB  rD  rM  rR  rT  list_like_addition_handlersr^  list_like_iadd_handlersrg  list_like_expansion_handlersr  rh   r  rq   _binop_handlers  sT  



	
	










=






 zBuiltinVariable._binop_handlersr   a_typeb_typeNc                 C   sR   t  | }|d u rd S g }|D ]\\}}}t||r&t||r&|| q|S rm   )r   r  get
issubclassr{  )r   r  r  handlersmatchestype1type2rm  rh   rh   rq   _find_binop_handler  s   
z#BuiltinVariable._find_binop_handlerc                 C   r  rm   )r   r  r   rh   rh   rq   can_insert_in_graph  r  z#BuiltinVariable.can_insert_in_graphMUST_USE_SPECIALIZEDrl   c                    s6   || j vsJ d|dt jdi | || _d S )Nz!Use the specialized VT class for z9, not BuiltinVariable. E.g. DictBuiltinVariable for dict.rh   )r  super__init__r   )rn   r   rl   rc  rh   rq   r    s
   

zBuiltinVariable.__init__c                 C   s,   | j d u rd}n| j j}| jj d| dS )NNone())r   rs   rc  )rn   r   rh   rh   rq   __repr__  s   
zBuiltinVariable.__repr__c                 C   r   rm   rE  r   rh   rh   rq   r     r   z"BuiltinVariable.as_python_constantc                 C   r   rm   rE  r   rh   rh   rq   get_real_python_backed_value  r   z,BuiltinVariable.get_real_python_backed_valuec                    s4   t tj ttjttji}| j|v r|| j S t  S rm   )	r   r   r   int64r   float64r   r  as_proxy)rn   DTYPEr  rh   rq   r    s   


zBuiltinVariable.as_proxyr   rY   c                 C   sD   | j j}| j jdksJ ||jjvsJ d||j|dd d S )Nbuiltinsr   Tr   )r   rs   rt   r   r   r   r   r   rh   rh   rq   r     s   zBuiltinVariable.reconstructrk   c                 O   s
   t ||S rm   )r#   rn   rk   rl   rh   rh   rq   constant_args  s   
zBuiltinVariable.constant_argsc                 G   s0   d}|D ]}t |tjr dS |p| }q|S NF)r   r   r   	is_tensor)rn   rk   
any_tensorargrh   rh   rq   tensor_args  s   zBuiltinVariable.tensor_args	arg_typesc                 C   s4   d}|D ]}t |tjr dS |pt |tj}q|S r  )r  r   r   rS   )rn   r  r  arg_typerh   rh   rq   tensor_args_type  s   z BuiltinVariable.tensor_args_typec                 O   sX   g }g }t || D ]}| 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 r  )r   r    r   trh   rh   rq   r     s
    
zBBuiltinVariable.python_and_tensor_constant_only.<locals>.<genexpr>)	itertoolschainr	  r  r{  r   r  )rn   rk   rl   r  non_tensor_argsirh   rh   rq   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 rh   r   r   rh   rh   rq   
<listcomp>      z=BuiltinVariable.unwrap_unspec_args_kwargs.<locals>.<listcomp>c                 S      i | ]	\}}||  qS rh   r  r   kvrh   rh   rq   
<dictcomp>      z=BuiltinVariable.unwrap_unspec_args_kwargs.<locals>.<dictcomp>)rO  )rk   rl   rh   rh   rq   unwrap_unspec_args_kwargs  s   z)BuiltinVariable.unwrap_unspec_args_kwargsc                 C   s   |   ot||S rm   )r  r%   r  rh   rh   rq   has_constant_handler  s   
z$BuiltinVariable.has_constant_handler
has_kwargsrZ   c              	      s  ddl m  t	g t fddD r	fddS trBtts+tu rBddd	t	t
 d
ttt
f dt
ffdd}|S 	 rctju rStd tjrc	r[	jS rc	j tdkrstjgR  swn'tdkr\fdd nddd	tdtdtffdd}| t	dj d 

rddd	tt
 d
ttt
f dt
d B f	
fdd}| 	 rtdd D rsddd	tt
 d
ttt
f dt
d B ffdd}nddd	tt
 d
ttt
f dt
d B ffdd}| d	tt
 dd ffddtdkr(fddS tdkrL\ddd	tt
 d
ttt
f dt
d B ffdd}|S ddd	tt
 d
ttt
f dt
d B ffd d}|S )!Nr7   LazyVariableTrackerc                 3   s    | ]}t | V  qd S rm   )r  r  r  rh   rq   r   "      z0BuiltinVariable._make_handler.<locals>.<genexpr>c                    s     | dd |D |S )Nc                 S   r  rh   realizer   r  rh   rh   rq   r  $  r  zCBuiltinVariable._make_handler.<locals>.<lambda>.<locals>.<listcomp>)rI  r   rk   rl   objrh   rq   r   #  s    z/BuiltinVariable._make_handler.<locals>.<lambda>r   rZ   rk   rl   rg   c                    sB    t u rtdd |D stdt|dg tjd t ||S )Nc                 s   s&    | ]}|  ot| tV  qd S rm   )rb  r   r   rx   r   rh   rh   rq   r   4  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)AssertionErrorr   r   rx   r   SUPPORTABLEr   ExceptionVariabler  rE  rh   rq   create_exception_class_object/  s   zDBuiltinVariable._make_handler.<locals>.create_exception_class_objectr   r   c                    s    | g|R  S rm   rh   )r   rk   _)binop_handlerrh   rq   r   V  s    r  c                    s*    D ]}|| g|R  }|r|  S qd S rm   rh   )r   rk   r  r   rv)binop_handlersrh   rq   call_binop_handlersY  s   z:BuiltinVariable._make_handler.<locals>.call_binop_handlerscall_c                    s  z| g|R i |W S  t yh   ztj| g|R i | W   t yg } z7||}|s[t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||}|sx |  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  )rh  inspect	signaturebindr  logwarningr   rs   r   	DIFFICULTr   remove_from_stats)r   rk   rl   er  rf  )r   r  self_handlerrh   rq   call_self_handlerh  sB    
z8BuiltinVariable._make_handler.<locals>.call_self_handlerc                 s       | ]}t |tV  qd S rm   )r  r<   r   rh   rh   rq   r     r  c              
      s   z dd |D  }W nA t y) } ztt|| t|jd W Y d }~n(d }~w tyL } ztdd  d| dg tj|d W Y d }~nd }~ww t	
| |S )	Nc                 S   r  rh   r  r   rh   rh   rq   r    r  PBuiltinVariable._make_handler.<locals>.constant_fold_handler.<locals>.<listcomp>ra  constant fold exceptionattempted to run function  with arguments 7Encountered exception when attempting to constant fold.r  r  r  r  from_exc)	Exceptionr   r   re  rk   r8   r   r   
DYNAMO_BUGr;   r   r   rk   rl   resrf  rE  rh   rq   constant_fold_handler  s,   z<BuiltinVariable._make_handler.<locals>.constant_fold_handlerc              
      s   t ||rbz dd |D i dd | D }W nA ty> } ztdd  d| dg tj|d	 W Y d }~n"d }~w ty[ } ztt|| t	|j
d
 W Y d }~nd }~ww t| |S d S )Nc                 S   r  rh   r  r   rh   rh   rq   r    r  r  c                 S   r  rh   r  r  rh   rh   rq   r    r  zPBuiltinVariable._make_handler.<locals>.constant_fold_handler.<locals>.<dictcomp>r  r  r  r  r  ra  )r%   rO  r8   r   r   r  r  r   r   re  rk   r;   r   r  rE  rh   rq   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   r  rh   )python_type_namer   r  rh   rh   rq   r    r  zMBuiltinVariable._make_handler.<locals>.call_unimplemented.<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.r  )r   rs   )rk   real_arg_types)r  r   r  rh   rq   call_unimplemented  s    

z9BuiltinVariable._make_handler.<locals>.call_unimplementedc                    s    |S rm   rh   r  )r  rh   rq   r     r   c                    s    | ||}|r
|S  | |S rm   rh   )r   rk   rl   r  )r  rm  rh   rq   builtin_dispatch  s
   z7BuiltinVariable._make_handler.<locals>.builtin_dispatchc                    s2   d }D ]}|| ||}|r|  S q | |S rm   rh   )r   rk   rl   r  r   )r  r  rh   rq   r    s   )lazyr  r   r   r  isclassr  BaseExceptionGeneratorExitre  r;   rw   rx   r  r   r   r   rS   r  _handle_insert_op_in_graphr{  r   r  r   r   rs   r   r  r   )r   r  r  r  r  r  r  r  rh   )r  r  r  r  r  r   rm  r  r  r  r  rq   _make_handler  s   







&





zBuiltinVariable._make_handlerr   c                 G   sX   t |dkr| |S t |dksJ z	|d |dW S  ty+   tt| Y d S w )Nr   r7   r
  )r   _call_frame_locals_snapshotr   r   r   rh  rn   r   rk   rh   rh   rq   	call_vars  s   
zBuiltinVariable.call_varsc                 G   s    t |dkrtt| | |S r   )r   r   rh  r  r  rh   rh   rq   call_locals  s   

zBuiltinVariable.call_localsc                 C   s   t | jjt |  B }t |  }i }| j D ](\}}||vr"q||v r-| jj|}t	
t|s9t|tjr:q||t|< qt|tt dS rU  )setf_codeco_varnamescell_and_freevarssymbolic_localsrO  rK  rZ  	load_cellr   __instancecheck__rL   r   r   DeletedVariabler<   creater>   rw   r:   )r   frame_local_namesr
  frame_localsr   r   rh   rh   rq   r    s$   z+BuiltinVariable._call_frame_locals_snapshotc                 C   sl  ddl m}m} |r| jg || R  sd S ddl m} ddlm}m} ||||rm|j	s]t
  |d  sW| jtv rCt| j }	nt| j }	|d }
|d |d< |
|d< n	t| 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|d  r|d *|dt+|dd  |W S |j#j$d|gt&||R  }t"dd
 |D r1|t,||W S t-||r_| .||\}}||i |}t"dd
 t/0|| D }|t1||||dW S t2dd
 |D rqt||d W S |tj3u rt|d tj1rt+|}|d 4 |d< |||W S  t5y   t6dd|  d| d| d| j dg t7j8d Y d S w )Nr7   )rH  wrap_fx_proxy_clsSourcelessBuilder)can_dispatch_torch_functiondispatch_torch_functionr   r   )	unique_id)invoke_and_store_as_constantc                 s   s    | ]	}t |tu V  qd S rm   )r   rS   r  rh   rh   rq   r   z  s    
z=BuiltinVariable._handle_insert_op_in_graph.<locals>.<genexpr>rI  __eq__c                 s   r  rm   r   rP   r  rh   rh   rq   r     r  c                 s   "    | ]}t |tjr|jV  qd S rm   r   r   rT   need_unwrapr   rh   rh   rq   r         

)	raw_valuer  c                 s   r  rm   r   rR   r   rh   rh   rq   r     r  z,unimplemented builtin op on tensor argumentszpartial tensor op: r  z3Dynamo does not know how to trace builtin operator z with tensor argumentsr  )9rJ  rH  r  r  r	  r  torch_functionr  r  exportr   r  r   rf   re   r  r  bytecode_transformationr  r  r  rs   IN_PLACE_DESUGARING_MAPr   r   r<   r   r   rR   r   selectr;   r   r$   r   rK  rL  r0   r1   NumpyNdarrayVariabler  r   rA  re  rP   r&   r  r  r  rT   r   r   r   NotImplementedErrorr   r   r  )rn   r   rk   rl   rH  r  r  r  r  ri   tmpfn_varr   r  r  proxy_args_kwargsr  r  rh   rh   rq   r  *  s   




	
("
z*BuiltinVariable._handle_insert_op_in_graphcall_function_handler_cachec                 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|< }|d usHJ ||||S )	Nc                 S   r  rh   r  r  rh   rh   rq   r        z1BuiltinVariable.call_function.<locals>.<dictcomp>c                 s       | ]}t |V  qd S rm   r   r   rh   rh   rq   r         z0BuiltinVariable.call_function.<locals>.<genexpr>Tc                 s   r/  rm   r0  r   rh   rh   rq   r     r1  c                 S   s   g | ]}t |qS rh   r0  r   rh   rh   rq   r    r  z1BuiltinVariable.call_function.<locals>.<listcomp>)rO  r   r-  r  r  r   )rn   r   rk   rl   keyrm  rh   rh   rq   rI    s   zBuiltinVariable.call_functionr   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|d |r|st|d tr|d j tu r|d |}tj|t dS |jj| |d |dd  S |t| j dv rtd	d
 |D rtdd
 | D rz t| j |}	|	dd
 |D i dd | D }
t||
W S  ty } ztt||t|jd W Y d }~nd }~ww | j tu r|dkrtj !d S | j t"u rVt| j |}|t#v rVt|d tj$r?|d j%d us0J |d j%&|||dd  |S t|d tj'rV|d &|||dd  |S | j t(u r~t| j |}|t)v r~t|d tj*r~|d &|||dd  |S | j t+u rt|dkrt| j |}|t,v rt|d t r|d &|||dd  |S | j t-u rt|dkrt|d t rt|tt-||d . S |dkrt|dkr|sddl/m0} |||d S t1 &||||S )N__setattr__ra   r   r[   r7   r   rV  rh   c                 s       | ]}|  V  qd S rm   rb  r   r?  rh   rh   rq   r   	  r1  z.BuiltinVariable.call_method.<locals>.<genexpr>c                 s   r4  rm   r5  r  rh   rh   rq   r   	  s    
c                 s   r4  rm   r  r6  rh   rh   rq   r     r1  c                 S   r  rh   r  r  rh   rh   rq   r    r.  z/BuiltinVariable.call_method.<locals>.<dictcomp>ra  r  __len__generic_len)2r   r   r   r  r   rW   rK  rZ  is_attribute_mutationrb  method_setattr_standardtrack_new_user_defined_objectr  has_force_unpack_var_sequencer   force_unpack_var_sequencer   rK   r:   r_   r  r   r	  r   rO  r;   r   r  r   r   re  rk   r<   r  r  r3   UserDefinedSetVariable_base_vtrA  rO   	frozensetr*   rM   rx   r5   r   r   rv  r9  r  )rn   r   r   rk   rl   r  name_varval	init_argsr   r  r  resolved_fnr9  r  rh   rq   rA    s   





zBuiltinVariable.call_methodr  c                 C      ddl m} |||S )Nr7   )generic_int)rv  rG  )rn   r   r  rG  rh   rh   rq   call_intD     
zBuiltinVariable.call_intc                 C   rF  )Nr7   )generic_float)rv  rJ  )rn   r   r  rJ  rh   rh   rq   
call_floatK  rI  zBuiltinVariable.call_floatc                 C   rF  )Nr7   )generic_bool)rv  rL  )rn   r   r  rL  rh   rh   rq   	call_boolR  s   
zBuiltinVariable.call_boolc                 C   s  t |tjtjfrOztdd |jD }W n
 ty   Y dS w t|dkr.|jj	 d}nt|dkrA|jj	 d|d d}n|jj	 |}t
||S t |tjr|jj}t|jjtju rot
||}||g i S t|rtd	d
t|j ddgd n|j}t
||}|||gi S t |tjrt|jjtju rt
|t|jS t |tttttfrt
|| S dS )z&Handle repr() on user defined objects.c                 s   r4  rm   r  r6  rh   rh   rq   r   c  r1  z,BuiltinVariable.call_repr.<locals>.<genexpr>Nr   z()r7   r  r  z4Attempted to call repr() method implemented in C/C++ z6 has a C/C++ based repr method. This is not supported.zWrite the repr method in Pythonr  )r   r   r  "UserDefinedExceptionObjectVariabler  rk   r'  r   exc_typers   r;   r   rW   r   r  r   r   rI  r.   r   __func__UserDefinedClassVariabler   rH   r>   r?   rN   rB   
debug_repr)rn   r   r  
const_argsr   repr_methodfn_vtbound_methodrh   rh   rq   	call_reprZ  sX   

zBuiltinVariable.call_reprc                 C   s  t |tjtjfr<t|jdkrt|dS t|jdkr*tt	
||jd gi S tt|j}tt	
||gi S t |tjrKt|t	|jS t |tjrt|jdr\|jj}nt|jdrg|jj}ntdt	|dg tjd t|jjtju rzt|| W S  ty   Y d S w t|rtd	dt|j d
dgd d S |j}zt||}W n ty   tjddd Y d S w |
||gi S d S )Nr   rN  r7   __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 UserFunctionVariableT)exc_info)r   r   r  rO  r   rk   r;   r   r   rx   rI  rK   re  UserFunctionVariabler   rW   r   r   rY  r  r   r   
USER_ERRORr   r   AttributeErrorr.   rQ  r  r  r  )rn   r   r  	tuple_var
str_methodrW  user_func_variablerh   rh   rq   call_str  s\   


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 )Nr7   r   r   )r   r=  r>  _call_min_max_seq_call_min_max_binary)rn   r   rk   rO  rh   rh   rq   _call_min_max  s   zBuiltinVariable._call_min_maxrO  c                 C   s:   t |dksJ t |dkr|d S tt| j||S Nr   r7   )r   	functoolsreducepartialrc  )rn   r   rO  rh   rh   rq   rb    s   z!BuiltinVariable._call_min_max_seqr?  r@  c                 C   s  |d u s|d u r
d S |  ||r| s||}}| s J t|tr1ttj||gi }t|t	s;t|t	rYddl
m} |t|||jjd| jgt||gi R  dS | rt|tjrndd l}t|j}nttj}| jtu r}d|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	r=| jtu r#tj!ntj"}|jjd|gt||gi R  }t	#||d S t|t$rYt|t$rY| | | }t%&||S d S )Nr7   r  rI  )r   r*  r   r   r   c                 s   s"    | ]}t |tjtjfV  qd S rm   )r   r   rT   r<   )r   r  rh   rh   rq   r     s    
z7BuiltinVariable._call_min_max_binary.<locals>.<genexpr>c                 s   r  rm   r  )r   rC  rh   rh   rq   r   )  r  c                 s   r  rm   r  r   rh   rh   rq   r   6  r  )'r  r  r   rP   r   TorchInGraphFunctionVariabler   r  rI  rR   rJ  r  r   rK  rL  r   r1   rb  r&  numpyNumpyVariableclipclampr   maximumr   minimumr   r   from_tensor_variabler   r  rT   sym_maxsym_minr  r<   r;   r   )rn   r   r?  r@  r  npr   rl   rw  np_fntorch_fnraw_braw_resr  py_fnr*  r   rh   rh   rq   rc    s   






z$BuiltinVariable._call_min_max_binaryc                 C   <   ddl m} ||t||t|dgi }||g i S )Nr7   r  __abs__rJ  r  r  r   rI  r;   r   )rn   r   r  r  
abs_methodrh   rh   rq   call_absT  
   zBuiltinVariable.call_absc                 C   rz  )Nr7   r  __pos__r|  )rn   r   r  r  
pos_methodrh   rh   rq   call_pos_  r  zBuiltinVariable.call_posc                 C   s   t |}||S rm   )r4   nb_index_implrn   r   r  rh   rh   rq   
call_indexj  s   
zBuiltinVariable.call_indexc                 O   s<   ddl m} ||t||t|dgi }||||S )Nr7   r  	__round__r|  )rn   r   r  rk   rl   r  round_methodrh   rh   rq   
call_roundr  s
   zBuiltinVariable.call_roundc                    sB   t |i r
t|S | j| rt fdd|D }t|S d S )Nc                 3   s     | ]}t  t|V  qd S rm   )r;   r   r,   r  r   rh   rq   r     s    z-BuiltinVariable.call_range.<locals>.<genexpr>)r%   r   rH   _dynamic_argsr  r  rh   r  rq   
call_range  s   



zBuiltinVariable.call_rangec                 O   s(   t dd |D pt dd | D S )Nc                 s   r  rm   r   r   rh   rh   rq   r     r  z0BuiltinVariable._dynamic_args.<locals>.<genexpr>)r   r	  r  rh   rh   rq   r    s   zBuiltinVariable._dynamic_argsc                 G   s   t ||S rm   )r   SliceVariabler  rh   rh   rq   
call_slice  rr  zBuiltinVariable.call_slicec                 O   s0   ddl m} |||jjd| jgt||R  S rF  )rJ  rH  rK  rL  r   r1   )rn   r   rk   rl   rH  rh   rh   rq   
_dyn_proxy  s   zBuiltinVariable._dyn_proxyr  c                 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tjtjfrb|jj|j
 t |tjrrt|j
tj nt |tjst|j
tj |t||t dS d S )NrV  r   F)r   r   IteratorVariabler  r  rE   cls_forr   r:   rY  r   r    rJ   r   r   r   TUPLE_ITERATOR_LENr   r>   OrderedSetVariableDictKeySetVariablerK  guard_on_key_orderr   MappingProxyVariableMAPPING_KEYS_CHECKUnspecializedNNModuleVariableSEQUENCE_LENGTHre  rP  rn   r   r  rk   rl   r   rh   rh   rq   _call_iter_tuple_list  sH   

z%BuiltinVariable._call_iter_tuple_listc                 O   s&   t j| j}|t||t dS rU  )r   rE   r  r   re  r>  r:   r  rh   rh   rq   _call_iter_tuple_generator  s
   z*BuiltinVariable._call_iter_tuple_generatorc                 O   s   t |tjrtj| j}|t||t dS t |tj	s)t |t
r6||r6| j||g|R i |S | j||g|R i |S rU  )r   r   r  rE   r  r   re  r>  r:   LocalGeneratorObjectVariablerW   r=  r  r  r  rh   rh   rq   _call_tuple_list  s   z BuiltinVariable._call_tuple_listc                 C   s~   ddl m}m} ddlm} t|tj|||frtj	dS t|t
r,t|t|jS t|ttttttfr=tj	dS d S )Nr7   )r7  FunctoolsPartialVariabler8  TF)r  r7  r  r  r9  r   r   rR  r<   r  rX   r;   r   r   r   rR   rS   rG   rK   rF   )rn   r   r  r7  r  r9  rh   rh   rq   call_callable  s2   	
zBuiltinVariable.call_callabler  c                 O   s6   t |dkr
|d S tdd| d| ddgd d S )	Nr   r7   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.r  )r   r   )rn   r  rk   rl   rh   rh   rq   	call_cast  s   
zBuiltinVariable.call_castc                 C   sZ   t |tjrt|t|jS t |trt|t|jS |	 r+t|t|
 S d S rm   )r   r   rR  r;   r   dirr   r   r   rb  r   r  rh   rh   rq   call_dir+  s   
zBuiltinVariable.call_dirc           	      O   s  ddl m} |r
J |stg t dS t|dkr'tt|dt| dgd |d }t|tjr8|j	t dS |
|rI||}t|t dS t|tjryt|jtry||d}t|tjry||||}t|tro|S ||t||S tt|d	gd d S )
Nr7   r  rV  z&set() takes 1 positional argument but  were givenra  r   __iter__z!failed to construct builtin set())rJ  r  rO   r:   r   r   rh  r/   r   cloner=  r>  r   rW   r   r   r   UserMethodVariablerk  r  r  call_set)	rn   r   rk   rl   r  r  rO  iter_fnoutrh   rh   rq   r  7  s<   



zBuiltinVariable.call_setc                 O   s   |rJ |s
t g S t|dkrtt|dt| dgd |d }t|tj r2t dd |jD S ||r@||}t |S tt|dgd d S )	Nr7   z,frozenset() takes 1 positional argument but r  ra  r   c                 S   s   g | ]}|j qS rh   )vtr   rh   rh   rq   r  p  s    z2BuiltinVariable.call_frozenset.<locals>.<listcomp>z'failed to construct builtin frozenset())	rM   r   r   rh  r/   r   	set_itemsr=  r>  )rn   r   rk   rl   r  rO  rh   rh   rq   call_frozenset]  s*   


zBuiltinVariable.call_frozensetc                    sx   ddl m  |rt|dkrd|v stddt| d |dtd} fdd	|D }tj||	 t
 d
S )Nr7   r  strictzip1 kwargs (`strict`) kwargsFc                    s$   g | ]}  t|gi qS rh   )r  iterrI  r  r  r   rh   rq   r        z,BuiltinVariable.call_zip.<locals>.<listcomp>r  rW  )rJ  r  r   r2   popr<   r  r   ZipVariabler   r:   )rn   r   rk   rl   r  	iter_argsrh   r  rq   call_zipz  s$   zBuiltinVariable.call_zipc                 O   s   ddl m} |||d S )Nr7   r8  r   )rv  r9  )rn   r   rk   rl   r9  rh   rh   rq   call_len  s   zBuiltinVariable.call_lenc                 O   s    ddl m} |||d |d S )Nr7   )
vt_getitemr   )rv  r  )rn   r   rk   rl   r  rh   rh   rq   call_getitem  s   zBuiltinVariable.call_getitemisinstance_type_varc           	   
      s  z   }W n ty$   tdd  d| dd  g tjd Y nw | }t tjrJ j	d urJdt
dtd	tf fd
d}t
|| |S t tjrit jtjritdd  d| ddg tjd t tjrd|jjv rt
||j| jS t tjrt
|t||S t|tstt|dd r|f}n#t|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
||S )Nz6builtin isinstance() cannot determine type of argumentzisinstance(, r  z=Dynamo doesn't have a rule to determine the type of argument r  
tensor_vartensor_typerg   c                    s@   dt dtffdd t|tu rt fdd|D S  |S )Ntyrg   c                    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)r6   r  nodemetar   r   nn	parameter	Parameterr   r  python_typerz   )r  example_valdtypesr  rh   rq   
check_type  s   

zOBuiltinVariable.call_isinstance.<locals>._tensor_isinstance.<locals>.check_typec                 3   s    | ]} |V  qd S rm   rh   )r   r  r  rh   rq   r     r1  zNBuiltinVariable.call_isinstance.<locals>._tensor_isinstance.<locals>.<genexpr>)r   r   r   r  r   )r  r  r  r  rq   _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.r  c                 s   s*    | ]}t |tptt|d dV  qdS )r  N)r   r   r   r   )r   tprh   rh   rq   r     s
    
z2BuiltinVariable.call_isinstance.<locals>.<genexpr>z?isinstance() arg 2 must be a type, a tuple of types, or a unionra  )!r  r'  r   r   r  r   r   r   rS   rz   r;   r   r   r   rW   r   rj   MemberDescriptorTyper  rc  r
  r  !UserDefinedExceptionClassVariabler   r   r   	UnionType__args__r  r   r   rh  r  )	rn   r   r  r  r  isinstance_typer  isinstance_type_tuplerC  rh   r  rq   call_isinstance  sx   

	
zBuiltinVariable.call_isinstanceleft_tyright_tyc              
   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;   r   r  )rn   r   r  r  
left_ty_pyright_ty_pyrh   rh   rq   call_issubclass	  s   
zBuiltinVariable.call_issubclassc                 C   s   t ||S rm   )r   SuperVariablern   r   r?  r@  rh   rh   rq   
call_super-	  rr  zBuiltinVariable.call_superc              
   G   s   |d }z| |W S  ty   t|dkr|d  Y S   ty? } zt|tjr:|  |jd W  Y d }~S  d }~ww )Nr   r   r7   )	next_variabler   r   r   r   r   rE   r  rO  )rn   r   rk   r  exrh   rh   rq   	call_next2	  s   zBuiltinVariable.call_nextr   c                 C   s    |  r| }|||S d S rm   )rb  r   r   )rn   r   r  r   r   rh   rh   rq   call_hasattrB	  s   zBuiltinVariable.call_hasattrseqsc                    s   t d}|r8tjdkr,t|dkrd|v s"t ddt| d |dt d}nt ddt| d  fd	d
|D }tj|||	 t
 dS )NFr`   r7   r  mapr  r  z0 kwargsc                    s$   g | ]}|  r| n|qS rh   )rY  rP  )r   r]  r  rh   rq   r  d	  r  z,BuiltinVariable.call_map.<locals>.<listcomp>r  )r<   r  sysversion_infor   r2   r  r   MapVariabler   r:   )rn   r   r   r  rl   r  seq_listrh   r  rq   call_mapJ	  s4   


zBuiltinVariable.call_mapr]  c                 C   s*   | |r
||n|}tj||t dS rU  )rY  rP  r   FilterVariabler:   )rn   r   r   r]  seq_or_listrh   rh   rq   call_filtero	  s   zBuiltinVariable.call_filterc                 C   s   | j ot| j |}|dkrt|| jj|S | jtu r=zt| j|}W n ty1   t	t| Y nw t
|s=t|||S t| j|d }tj| ||d urSt||dS d |dS )Nrs   r   )r   r   r;   r   r   rs   r   r   r]  r   r   r   r   r   )rn   r   r   r   r   r   rh   rh   rq   r   {	  s"   
zBuiltinVariable.var_getattrrB  defaultc              	      s  |  stdd| d| d| dddgd | }| r%|dkr%d	}jj|rRt|tj	rR|d
v rR|j
rRjj|rRtdd| d| d| ddg d jj||rbjj||S |d ur| ||}|d ur|ddsyJ | s|S n|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rot|tj"r!t#|j$j%t&j'r!t(j)r!|dv r!tdd| d| dd| dgd | rM|* j+j,d }	t|	t-j.rMt/|	rMj0rBt(j1sMtddd g t2j3d z||W S  t4y]     tyn   tj5|| d Y S w t|tj6rzt7|j$|}
W n t8y   t9t8  w t|
t-j:j;t-j:j<frt-j=j>?|
rtj6|
 dS |t@v rtj5||t|
 d!S d S t|tAr|jBs||j$jCvrt7|j$|}
n|j$jC| }
t(jDrjEF|j$||
 t|
 S z||W S  ty   tj5|| 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)named_parameters
parametersnamed_buffersbuffersnamed_modulesmodulesz,getattr() on nn.Module with pending mutationzPIntentionally graph breaking on getattr() on a nn.Module with a pending mutationTF>   __base__	__bases__	__flags__r  c              	      s(   g | ]\}}t | ot |qS rh   )r;   r   r   )r   r  r@  r   r   rh   rq   r  	  s    z0BuiltinVariable.call_getattr.<locals>.<listcomp>r   r  r  )assertRaisesRegexassertNotWarnsassertWarnsRegex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 TensorrN  z-torch.compile does not support sparse Tensorsr   )Grb  r   r   r  rK  rZ  r:  r   r   r  is_state_mutatedhas_pending_mutationhas_pending_mutation_of_attr	load_attrr  is_constant_matchr   r   r   	enumerater  rK   r;   r   r  r  r'  r9  r   rS   r<   r?   DistributedVariablerR  rW   r  r   rc  unittestTestCaser   enable_trace_unittestr  r  r  r   Tensorr   r"  capture_sparse_computer   SPARSE_TENSORr8   r   rj  r   r]  r   _opsOpOverloadPacket
OpOverload_dynamotrace_rulesis_aten_op_or_tensor_methodr'   r   is_torchr
  replay_record_enabledexec_recorderrecord_module_access)rn   r   r  rB  r  r   hasattr_varr   
tuple_argsfake_valmemberrh   r  rq   call_getattr	  s  	





	


zBuiltinVariable.call_getattrrC  c              	      sF  t  tjtjtjtjtjfr |d||gi S |jj	
 r+| r+| }  r ddlm} |dkrOt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
 n j|jkrtdd  d| d| dddgd
  fdd|jjD }|D ]	}|jj| qt|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tjdtjfdd}
|jd|
|	 fi  |
  jj d  |	S |dv rd}nt!|r tdd  d| d| ddd| dgd
 |jj	" || |S t  tj#r|j$ sGtdd   d!d"gtj%d
 | rt |tj&rt'| j|}z
 (|| }W n t)t*fys   d }Y nw |d ur| rt'| j|}t+, j-d}||u r|tj.j/j0u r|S  1| d S )#Nr3  r7   rG  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` regionz9Failed to mutate tensor data attribute to different dtypezPDyanmo only supports mutating `.data` of tensor to a new one with the same dtypec                    s   g | ]
}|j  j kr|qS rh   r   )r   tfr  rh   rq   r  w
  s    z0BuiltinVariable.call_setattr.<locals>.<listcomp>rI  r   rg   c                 S   s.   | j }|dkr|d }tjj| f|f | S re  )_versionr   _C	_autograd_unsafe_set_version_counter)r   versionrh   rh   rq   _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#nn.Module mutation in HigherOrderOpznn.Module: zPInplace modifying nn.Module params/buffers inside HigherOrderOps is not allowed.z<Remove the mutation or move it outside of the HigherOrderOp.)2r   r   r?   rW   NestedUserFunctionVariabler  TracebackVariablerA  rK  rZ  r:  rb  r   r  rJ  rH  r   r   r  r   rz   tracked_fakesremovedynamo_disable_gradr   no_gradrL  r  set_r1   r  r  r  r-   
store_attrr9  is_root_tracerFUNDAMENTALrS   r+   r   r]  r   r  getattr_staticmodule_typer  Moduler3  convert_to_unspecialized)rn   r   r  rB  rC  r   rH  	to_remover  r  r!  assigning_fake_valgetattr_varexisting_fake_attrmod_setattrrh   r  rq   call_setattr3
  s   




 



	

zBuiltinVariable.call_setattrc                 C      | |d|gi S )N__delattr__rC  )rn   r   r  rB  rh   rh   rq   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 )Nunknown_python_type)	case_namerN  r  )r  r'  r   r   INVALID_INPUTrx   r   r"   r   r   rW   
cls_sourcer   r  rK  install_global_by_idr   r   r;   r   )rn   r   r  r   errorr   r   rh   rh   rq   	call_type
  s.   

zBuiltinVariable.call_typec                 C   s*   | |rtt||}t|S d S rm   )rY  re  reversedrP  r   rK   )rn   r   r  rO  rh   rh   rq   call_reversed
  s   

zBuiltinVariable.call_reversedc                 K   sD   | |r t|tjs tj||t d}||dg | |S d S )NrV  sort)r=  r   r   rS   rG   r>  r:   rA  )rn   r   r  rl   list_varrh   rh   rq   call_sorted  s   zBuiltinVariable.call_sortedc                 C   sP   t |trtj|t| d dS t |tr&||djr&|	|dg i S d S )Nsym_num__neg__)
r   rR   r  r   r   r  rW   r   r   rA  )rn   r   r?  rh   rh   rq   call_neg  s   
zBuiltinVariable.call_neg_format_stringc                 O   s    |  }t|}tj|||S rm   )r   rx   r   StringFormatVariabler  )rn   r   rI  rk   rl   format_stringrh   rh   rq   call_format(  s   zBuiltinVariable.call_formatc                 G   sH  t |dkr!t|d tjr!|d }|j|j}t|t	|S t |dkr9|d 
 r9tt|d }||S t|d tjrLt|t	|d jS t |dkr|d }t|tjtjfr{|jr{t|tjrrt|jtj n	t|jtj | }|turt|t	|S tt	|S tdt|d| dgtjd d S )Nr   r7   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  ) r   r   r   r9  rK  get_submodule
module_keyr;   r   idr  r	   rS   call_idr/   r  
fake_valuerR  rW   r   r   r   r   CLASS_MATCHID_MATCHr  r9   r=   r   rx   r   r  )rn   r   rk   nn_mod_variabler   tensor_variabler  real_valrh   rh   rq   rP  3  sD   

zBuiltinVariable.call_idr   c                 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   r  )rn   r   r   rh   rh   rq   call_deepcopy]  s   

zBuiltinVariable.call_deepcopyrs  rt  c           
      C   s  ddl m} ddlm} | j}|tjtjfv rC| o0| o0t	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 | r|n|}|jd|| | fi }	|t|||	S )Nr7   ri  %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 .rI  )rJ  r  r  rY  r   r   rn  is_notr  rO  r)   r  r  r;   r   r   rs   r   r  r   rS   sizer   broadcast_shapesRuntimeErrorr\  rK  rL  r   )
rn   r   rs  rt  r  rY  r   	is_result
tensor_clsr*  rh   rh   rq   r}  j  sh   


	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r3|n|}t|tt	t
fsGt||t d S |jd
|| | fi }tj||d dS )Nr7   rX  z!unsupported SymNode comparison opr  r  r  rZ  z with SymNode arguments r  rI  rE  )r  rY  r   r   rs   r   r  r   rR   r<   rS   r;   r   r   rK  rL  r  r  )rn   r   rs  rt  rY  r   non_symnoder*  rh   rh   rq   rz    s4   

z(BuiltinVariable._comparison_with_symnodec                 C   v   t |trt |trd S | r+| r+tj||jjdtjgt	||gi R  d dS t |t
r9||d|gi S d S )NrI  rE  r3  )r   r<   is_symnode_likerR   r  rK  rL  r   r   r1   rd   rA  r  rh   rh   rq   call_xor  s   
zBuiltinVariable.call_xorc                 C       t |tr||d|gi S d S )Nr5  r   rd   rA  r  rh   rh   rq   	call_ixor     
zBuiltinVariable.call_ixorc                 C   rf  )Nr  rg  r  rh   rh   rq   call_sub  ri  zBuiltinVariable.call_subc                 C   rf  )Nr  rg  r  rh   rh   rq   	call_isub  ri  zBuiltinVariable.call_isubc                 C   rc  )NrI  rE  __and__)r   r<   rd  rR   r  rK  rL  r   r   r1   rd   rA  r  rh   rh   rq   	call_and_  s   
zBuiltinVariable.call_and_c                 C   rc  )NrI  rE  __iand__)r   r<   rd  rR   r  rK  rL  r   r  r1   rd   rA  r  rh   rh   rq   	call_iand   s   
zBuiltinVariable.call_iandc                 C   s   t |trt |trd S z| }| }t |tr(t |tr(t|||B W S W n	 ty2   Y nw | rR| rRtj	||j
jdtjgt||gi R  d dS t |g ttttR ri||d|gi S d S )NrI  rE  __or__)r   r<   r   r   r;   r   r'  rd  rR   r  rK  rL  r   r   r1   rd   r>   rU   rV   rA  )rn   r   r?  r@  a_constb_constrh   rh   rq   call_or_  sF   	zBuiltinVariable.call_or_c                 C   s   t |trt |trd S | r+| r+tj||jjdtjgt	||gi R  d dS t |g t
ttR r@||d|gi S d S )NrI  rE  __ior__)r   r<   rd  rR   r  rK  rL  r   r  r1   rd   r>   rU   rA  r  rh   rh   rq   call_ior?  s0   	zBuiltinVariable.call_iorc                 C   s   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tfr6t|t|jdkS t |trI| ||}t||j S d S )NrI  rE  r   )r   rR   r  rK  rL  r   r   r1   rB   dv_dictrG   r>   rO   r;   r   r   rO  rW   rM  r   )rn   r   r?  bool_resultrh   rh   rq   	call_not_\  s$   


	
zBuiltinVariable.call_not_c                 C   r6  )N__contains__rC  r  rh   rh   rq   call_containss  s   zBuiltinVariable.call_containsr   c                 C   s   t |tjo| j|ju S rm   )r   r   r   r   r   rh   rh   rq   r   x  s   zBuiltinVariable.is_python_equalr   rm   )wrs   rt   ru   rv   r   	_SENTINELr;   _nonvar_fieldsr   r   r!   r   staticmethodrf  cacher  r   r  r   r  r  rw   r  re  rx   r6  r   r  r  r  r  r  rA  r  r  r   r  r  r   r  r  r   r  r  r  r  r   r  r  r  r  r  r  r  r-  rI  rA  rH  rK  rM  rX  ra  rd  rb  rc  call_mincall_maxr~  r  r  r  r  r  r  r  r  r  r  
call_tupler  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r5  r8  r?  rA  rD  rH  rL  rP  rW  r}  rz  re  rh  rj  rk  rm  ro  rs  ru  rx  rz  r   __classcell__rh   rh   r  rq   r   e  s  
 
B,"%
   









 r



 




i



5
A

	
l







6


!


&






j




%

 (
 '






*

7
$






-


r   c                       s   e Zd ZdZeZefdededdf fddZde	fdd	Z
d
ddee dee	ef defddZd
dde	dee dee	ef def
 fddZed
ddedededef
ddZed
ddedededef
ddZ  ZS )DictBuiltinVariablez4Variable tracker for the `dict` builtin constructor.r   rl   rg   Nc                    "   |t u sJ t jdi | d S Nrh   )rw   r  r  rn   r   rl   r  rh   rq   r       zDictBuiltinVariable.__init__c                 C   r   )NzDictBuiltinVariable()rh   r   rh   rh   rq   r    r   zDictBuiltinVariable.__repr__r   rZ   rk   c                 C   s   t j|tg|R i |S rm   )r  call_custom_dictrw   )rn   r   rk   rl   rh   rh   rq   rI    s   z!DictBuiltinVariable.call_functionr   c                    s  |dkr,t |dkr,|s,ti tt d}t|d tr|S |jj| |d |dd  S |dkr=tj	|tg|R i |S t
t|d }|d ur|tv rt|d tjrk|d jd us\J |d j|||dd  |S t|d tr|d |||dd  |S t ||||S )Nr[   r7   rV  r   fromkeys)r   r>   rw   r:   r   r  rK  rZ  r<  call_custom_dict_fromkeysr   r(   r   rV   r@  rA  r  )rn   r   r   rk   rl   dict_vtrE  r  rh   rq   rA    s0   zDictBuiltinVariable.call_methoduser_clsc                O   s.   t |}| t| tjt| |g||S rm   )re  rk  r;   r   r   construct_dict)r   r  rk   rl   	args_listrh   rh   rq   r    s   z$DictBuiltinVariable.call_custom_dictc          	         s  |t tthvr'td|j d| d| d|j d|j dd|j dgd	 |r\|tus=t|d
ks=t|d
ks=d|vrRt |j ddt| dt| d g ||dR }t|dkrrt |j ddt| d t|d
krg |t	d R }t|dkrt |j ddt| d |\}}|turt
nt}t|t r fd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 )Nz$Unsupported dict type for fromkeys()z.fromkeys(): r  zFailed to call z.fromkeys() because z5 is not any type of dict, OrderedDict, or defaultdictzEnsure z0 is a type of dict, OrderedDict, or defaultdict.r  r7   r   z	.fromkeysz1 args and 1 kwargs (`value`)z
 args and r  r   zat least 1 argsz argsr   z2 argsc                    s   g | ]}t  |qS rh   ro  )r   r  r  rh   rq   r    s    zADictBuiltinVariable.call_custom_dict_fromkeys.<locals>.<listcomp>rV  c                 s   r/  rm   rC   r  rh   rh   rq   r     r1  z@DictBuiltinVariable.call_custom_dict_fromkeys.<locals>.<genexpr>zfailed to call dict.fromkeys()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.)rw   r   r   r   rs   r   r2   r  r<   r  r>   r?   r   r  r:   r=  r>  r   )	r   r  rk   rl   r  r   DictVariableTypearg_listkeysrh   r  rq   r    s   
	








z-DictBuiltinVariable.call_custom_dict_fromkeys)rs   rt   ru   rv   rw   r   r   r   r  rx   r  r   r;   rI  re  rA  r}  r  r  r  rh   rh   r  rq   r  |  sb    


r  c                       sj   e Zd ZdZeZefdededdf fddZdefdd	Z	d
dde
e deeef defddZ  ZS )IterBuiltinVariablez(Variable tracker for the `iter` builtin.r   rl   rg   Nc                    r  r  )r  r  r  r  r  rh   rq   r    r  zIterBuiltinVariable.__init__c                 C   r   )NzIterBuiltinVariable()rh   r   rh   rh   rq   r    r   zIterBuiltinVariable.__repr__r   rZ   rk   c                 C   s   |st dddg tjd |^}}|s3|s3t|tjtjtjtjtj	tj
tjtjtf	r3||dg i S ttjj||g|i }|rIt|}|S )Nziter() with no argumentsziter()z%iter() requires at least one argumentr  r  )r   r   r\  r   r   rG   rH   r  r>   r9  rS   rK   rR  rB   rA  r[  r   r  iter_rI  ObjectIteratorVariable)rn   r   rk   rl   r  restretrh   rh   rq   rI    sF   
z!IterBuiltinVariable.call_function)rs   rt   ru   rv   r  r   r   r  rx   r  r   r;   rw   rI  r  rh   rh   r  rq   r    s    
r  c                
       s   e Zd ZdZeZefdededdf fddZde	fdd	Z
d
ddee dee	ef defddZd
dde	dee dee	ef def
 fddZ  ZS )ListBuiltinVariablez4Variable tracker for the `list` builtin constructor.r   rl   rg   Nc                    r  r  )re  r  r  r  r  rh   rq   r  T  r  zListBuiltinVariable.__init__c                 C   r   )NzListBuiltinVariable()rh   r   rh   rh   rq   r  X  r   zListBuiltinVariable.__repr__r   rZ   rk   c                 C   s\  ddl m} |r|d nd }t|tjtjfs!t||r-||r-tt|	|t
 dS |d u r8tg t
 dS ||r|jrt|jst|trTt|jtj n3t|trht|ttfsh|jj|j t|tjrxt|jtj nt|tjst|jtj tt||t
 dS dd |D }tdd| d	d
| g t j!d d S )Nr7   )rW   r   rV  c                 S   s   g | ]}t |jqS rh   )r   rs   r6  rh   rh   rq   r    s    z5ListBuiltinVariable.call_function.<locals>.<listcomp>zFailed to trace list()zlist(r  zADynamo does not know how to construct a list from argument types r  )"r  rW   r   r   r  r  r=  rG   re  r>  r:   rY  r   r    rJ   r   r   r   r  r>   r/   rO   rM   rK  r  r   r  r  r  r  rP  r   r   r  )rn   r   rk   rl   rW   r  r  rh   rh   rq   rI  [  sX   



z!ListBuiltinVariable.call_functionr   c                    sh   |dkr+t |dkr+|s+tg t d}t|d tr|S |jj| |d |dd  S t 	||||S )Nr[   r7   rV  r   )
r   rG   r:   r   r  rK  rZ  r<  r  rA  )rn   r   r   rk   rl   list_vtr  rh   rq   rA    s   zListBuiltinVariable.call_method)rs   rt   ru   rv   re  r   r   r   r  rx   r  r   r;   rw   rI  rA  r  rh   rh   r  rq   r  O  s2    

8
r  r   c              	   c   sH    ddl m} || d}z||  d V  W ||  d S ||  w )Nr7   )GradModeVariableF)rN  r  r  enterexit)r   r  gmvrh   rh   rq   r&    s   
r&  )rg   N)rv   
contextlibrf  r  r  loggingr   r   r  rj   typingr  collectionsr   r   collections.abcr   r   r   r   r   r	   r
   r   torch._subclasses.meta_utilsr   torch.overridesr   torch.utils._python_dispatchr   rN  r   r   r   r   rf  r   r   r   r   r   r   r   guardsr   r   replay_recordr   r   r   r   r   r    r!   r"   utilsr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   baser8   r9   r:   r;   constantr<   r=   dictsr>   r?   r@   rA   rB   hashablerD   listsrE   rF   rG   rH   rI   rJ   rK   miscrL   setsrM   rN   rO   r  rP   rQ   rR   rS   rT   r  rU   rV   rW   rX   torch._dynamo.codegenrY   torch._dynamo.symbolic_convertrZ   	getLoggerrs   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r   r   r   r  r   r  r   r  r   r$  r   rA  r   r   r_   rw   r   rx   r   r  r   r  r  r  r  cmp_eqr  cmp_ner  cmp_ltr  cmp_ler  cmp_gtr  cmp_gerl  r   r   r   r   r   r   r   r   r   r   r   r   r   rd   re   rf   r   r   r   r  r  r  contextmanagerIteratorr&  rh   rh   rh   rq   <module>   s,   $	 X$	

			$$
A4                      - =X