o
    i                     @   s  d Z ddlZddlZddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZ ddlmZmZmZ ddlmZmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) erddl*m+Z+ ddl,m-Z- dd Z.d.ddZ/dd Z0G dd de'Z1G dd de'Z2G dd de1Z3G dd de1Z4G d d! d!e1Z5G d"d# d#e5Z6G d$d% d%e5Z7G d&d' d'e'Z8G d(d) d)e8Z9G d*d+ d+e8Z:G d,d- d-e8Z;dS )/a  
Dictionary-related variable tracking classes for PyTorch Dynamo.

This module implements variable tracking for different types of dictionary-like objects:
- Regular Python dictionaries (dict)
- Ordered dictionaries (collections.OrderedDict)
- Default dictionaries (collections.defaultdict)
- Dictionary views (keys and values)
- Sets and frozensets (implemented internally using dictionaries)

These classes are responsible for tracking dictionary operations during graph compilation,
maintaining proper guards for dictionary mutations and key existence checks. They handle
dictionary creation, modification, key/value access, and view operations while ensuring
correct behavior in the compiled code through appropriate guard installation.

The implementation uses a special _HashableTracker wrapper to handle dictionary keys
while preserving proper aliasing semantics. Sets are implemented as dictionaries with
None values for efficiency and code reuse.
    NHashable)OptionalTYPE_CHECKING)is_fake   )graph_break_hints	polyfills	variables)create_call_functioncreate_instruction)raise_observed_exceptionunimplemented_v2)GuardBuilderinstall_guard)is_from_local_source)cmp_name_to_op_mapping
dict_items	dict_keysdict_valuesistyperaise_args_mismatchspecialize_symnode   )ValueMutationNewVariableTrackerConstantVariable)	PyCodegenInstructionTranslatorc                 C   s   t | jddS )N__instancecheck__F)type__dict__get)obj r&   S/home/ubuntu/vllm_env/lib/python3.10/site-packages/torch/_dynamo/variables/dicts.pywas_instancecheck_override=      r(   c                 C   s>   |d u rddl m} | }tt|tdt|  gd d S )Nr   r   zunhashable type: args)torch._dynamo.symbolic_convertr    
current_txr   	TypeErrorr   r"   )argtxr    r&   r&   r'   raise_unhashableA   s   
r1   c                 C   s  t | tjr|  s|  rdS t | tjr!|  jj	dd uS t | tj
r1tdd | jD S t | tjrCtdd | j D S t | tjrdt| jsdt| jdtju rdt | jtrdt | jtS t | tjtjtjtjtjtjtjtjtj j!tj"tj#tj$tj%tj&tj'tj(tj)fS )NTexample_valuec                 s       | ]}t |V  qd S Nis_hashable.0er&   r&   r'   	<genexpr>]       zis_hashable.<locals>.<genexpr>c                 s   r3   r4   r5   r7   r&   r&   r'   r:   _   r;   __hash__)*
isinstancer
   LazyVariableTrackeris_realizedr6   TensorVariableas_proxynodemetar$   TupleVariableallitemsFrozenDataClassVariablefieldsvaluesUserDefinedObjectVariabler(   valueinspectgetattr_staticintr<   py_HashableBuiltinVariableSymNodeVariabler   EnumVariableFrozensetVariableUserDefinedClassVariableUserFunctionVariableSkipFunctionVariablemiscNumpyVariableNNModuleVariableUnspecializedNNModuleVariableMethodWrapperVariableTorchInGraphFunctionVariableTypingVariableFunctoolsPartialVariableWeakRefVariable TorchHigherOrderOperatorVariable)xr&   r&   r'   r6   K   sT   


r6   c                       sL  e Zd ZejZdhejZG dd dZe	fde	eef ddf fddZ
d	d
 Zdd Zdd Zdd Zdd Zdd ZdefddZdefddZdefddZdd Zdd Zd?d!d"Zd#d$d%efd&d'Zd#d$d%efd(d)Zd%efd*d+Zd%efd,d-Zd.d/ Zd0d1 Z	2	3	4	5		6d@ fd7d8Zd9d: Z d;d< Z! fd=d>Z"  Z#S )AConstDictVariableuser_clsc                   @   sL   e Zd ZdZdddZedd Zdd	 Zed
d Z	dd de
fddZdS )z"ConstDictVariable._HashableTrackeraB  
        Auxiliary opaque internal class that wraps a VariableTracker and makes it hashable
        This should not be seen or touched by anything outside of ConstDictVariable and its children
        Note that it's also fine to put VTs into dictionaries and sets, but doing so does not take into account aliasing
        returnNc                 C   s"   t |}t|st| || _d S r4   )r   r6   r1   vt)selfre   r&   r&   r'   __init__   s   
z+ConstDictVariable._HashableTracker.__init__c                    sN  t | jtjr| j s| j r| j S t | jtjr(| j j	j
d }|S t | jtjrAtj t fdd| jjD }|S t | jtjrL| jjS t | jtjrW| jjS t | jtjrc| j S t | jtjrttj  | jjjS t | jtjrtj  fdd| jj D }tj| j |S t | jtjr| jjS | j }|S )Nr2   c                 3   s    | ]} |j V  qd S r4   underlying_valuer7   r   r&   r'   r:          zFConstDictVariable._HashableTracker.underlying_value.<locals>.<genexpr>c                    s   i | ]
\}}| |j qS r&   rh   r8   kvr   r&   r'   
<dictcomp>   s    zGConstDictVariable._HashableTracker.underlying_value.<locals>.<dictcomp>)r=   re   r
   r>   r?   r6   original_valuer@   rA   rB   rC   rD   rb   _HashableTrackertuplerF   rY   rK   rZ   rU   get_functionr_   referent_vtri   rG   rH   HashWrapperpython_typerJ   as_python_constant)rf   ra   fields_valuesr&   r   r'   ri      sF   





z3ConstDictVariable._HashableTracker.underlying_valuec                 C   
   t | jS r4   )hashri   rf   r&   r&   r'   r<      s   
z+ConstDictVariable._HashableTracker.__hash__c                    sh   t | t |kr
dS t| tr(tj t| t|ko't fddt| |D S t| r0| |u S | |kS )NFc                 3   s     | ]\}}  ||V  qd S r4   )_eq_impl)r8   urm   r   r&   r'   r:      s    
z>ConstDictVariable._HashableTracker._eq_impl.<locals>.<genexpr>)	r"   r=   rq   rb   rp   lenrE   zipr   )abr&   r   r'   r{      s   
z+ConstDictVariable._HashableTracker._eq_implotherc                 C   sN   t j}t||st|sJ t|t||r || j|jS || j|S r4   )rb   rp   r=   r   
is_literalr"   r{   ri   )rf   r   r   r&   r&   r'   __eq__   s   
z)ConstDictVariable._HashableTracker.__eq__rd   N)__name__
__module____qualname____doc__rg   propertyri   r<   staticmethodr{   boolr   r&   r&   r&   r'   rp      s    
	
'
rp   rF   rd   Nc                    s   d|v r	| d d|v r| d t jd	i | tj t fdd| D s-J  fdd| |}|fdd| D | _t| j	 | _
| | _|| _d S )
Noriginal_itemsshould_reconstruct_allc                 3   s,    | ]\}}t |t fot |tV  qd S r4   )r=   r   r8   ra   rm   r   r&   r'   r:      s    
z-ConstDictVariable.__init__.<locals>.<genexpr>c                    s   t |  r| S  | S r4   )r=   )keyr   r&   r'   make_hashable   s   z1ConstDictVariable.__init__.<locals>.make_hashablec                    s   i | ]	\}} ||qS r&   r&   r   )r   r&   r'   rn          z.ConstDictVariable.__init__.<locals>.<dictcomp>r&   )popsuperrg   rb   rp   rE   rF   _get_dict_cls_from_user_clsr   sourcer   copyr   rc   )rf   rF   rc   kwargsdict_cls	__class__)r   r   r'   rg      s   




zConstDictVariable.__init__c                    sV   t tjtjf | v r|}nt fdd|jD }| v s"J ||tju r)t }|S )Nc                 3   s    | ]	}| v r|V  qd S r4   r&   )r8   baseaccepted_dict_typesr&   r'   r:     s    z@ConstDictVariable._get_dict_cls_from_user_cls.<locals>.<genexpr>)dictcollectionsOrderedDictdefaultdictnext__mro__)rf   rc   r   r&   r   r'   r      s   
z-ConstDictVariable._get_dict_cls_from_user_clsc                 C      dd | j   D S )Nc                 S       i | ]\}}|j  | qS r&   re   rA   rk   r&   r&   r'   rn          z.ConstDictVariable.as_proxy.<locals>.<dictcomp>rF   rz   r&   r&   r'   rA        zConstDictVariable.as_proxyc                 C   s"   dd dd | j D  d S )N{, c                 s   s,    | ]\}}|j   d |  V  qdS )z: Nre   
debug_reprrk   r&   r&   r'   r:     s    
z/ConstDictVariable.debug_repr.<locals>.<genexpr>})joinrF   rz   r&   r&   r'   r     s   
zConstDictVariable.debug_reprc                 C   r   )Nc                 S   r   r&   re   rv   rk   r&   r&   r'   rn          z8ConstDictVariable.as_python_constant.<locals>.<dictcomp>r   rz   r&   r&   r'   rv     s   z$ConstDictVariable.as_python_constantc                 C   s   |    dd | j D S )Nc                 S   s   i | ]
\}}|j  |qS r&   r   rk   r&   r&   r'   rn   '  s    z=ConstDictVariable.keys_as_python_constant.<locals>.<dictcomp>)install_dict_keys_match_guardrF   rz   r&   r&   r'   keys_as_python_constant%     z)ConstDictVariable.keys_as_python_constantc                 C      | j S r4   )rc   rz   r&   r&   r'   ru   )     zConstDictVariable.python_typec                 C   sB   t |tsJ tj}t|o ||| jv o t | j|| tj S r4   )r=   r   rb   rp   r6   rF   r
   DeletedVariable)rf   re   r   r&   r&   r'   __contains__,  s   zConstDictVariable.__contains__c                 C   s   t dd | j D S )Nc                 s   s    | ]
}t |tj V  qd S r4   )r=   r
   r   r8   ra   r&   r&   r'   r:   6  s    
z(ConstDictVariable.len.<locals>.<genexpr>)sumrF   rI   rz   r&   r&   r'   r}   5  s   zConstDictVariable.lenc                    s"    j pt fdd j D S )Nc                 3   s*    | ]\}}   j|j|V  qd S r4   )is_new_itemr   r$   re   )r8   r   rK   rz   r&   r'   r:   ;  s
    
z2ConstDictVariable.has_new_items.<locals>.<genexpr>)r   anyrF   rz   r&   rz   r'   has_new_items:  s   zConstDictVariable.has_new_itemsc                 C   s<   |r|  r|  rt| t| kS t|t|kS r4   )r?   idrealize)rf   rK   r   r&   r&   r'   r   @  s   zConstDictVariable.is_new_itemc                 C   sh   d}| j   D ]!\}}| j|j}| ||s| jr(||j || |d7 }q|td|d d S )Nr   r   	BUILD_MAPr/   )rF   r   r$   re   r   r   append_outputr   )rf   codegennum_argsr   rK   itemr&   r&   r'   reconstruct_kvs_into_new_dictF  s   
z/ConstDictVariable.reconstruct_kvs_into_new_dictr   r   c                    sJ   | j tju r  fdd |    tdd d S |   d S )Nc                            t dgS )Nr   extend_outputcreate_load_python_moduler   create_load_attrr&   r   r&   r'   <lambda>V  
    z/ConstDictVariable.reconstruct.<locals>.<lambda>r   F)rc   r   r   add_push_nullr   r   r   rf   r   r&   r   r'   reconstructR  s   

zConstDictVariable.reconstructr0   r    r/   c                 C   s(   t |}|| jvrtt| | j| S r4   )rb   rp   rF   r   KeyError)rf   r0   r/   r   r&   r&   r'   'getitem_const_raise_exception_if_absentb  s   



z9ConstDictVariable.getitem_const_raise_exception_if_absentc                 C   sL   t |}|| jvr!d|j d}tdd|j |dgtjd | j| S )NzDictionary key z not found during tracingzkey not found in dictzKey z>Check if the key exists in the dictionary before accessing it.gb_typecontextexplanationhints)rb   rp   rF   rK   r   r   
USER_ERROR)rf   r0   r/   r   msgr&   r&   r'   getitem_constj  s   



	zConstDictVariable.getitem_constc                 C   s"   t |}|| jvrd S | j| S r4   )rb   rp   rF   )rf   r/   r   r&   r&   r'   maybe_getitem_consty  s   


z%ConstDictVariable.maybe_getitem_constc                 C   sX   || v sJ t |}t| j |}t| j | }t|tj	r*|
  d S d S r4   )rb   rp   rq   rF   keysindexr   r=   r
   r>   r   )rf   r/   r   r   original_key_vtr&   r&   r'   realize_key_vt  s   
z ConstDictVariable.realize_key_vtc                 C   s   | j rt| tj d S d S r4   )r   r   
make_guardr   DICT_KEYS_MATCHrz   r&   r&   r'   r     s   z/ConstDictVariable.install_dict_keys_match_guardc                 C   s   ddl m} | jsd S |jj| rd S |d | v }|d jd u r>t|d |r>t| t	j
t| j|d j| d d S |d jrT|rN| |d  d S |   d S d S )Nr   r   r   )r   invert) r   r   outputside_effectsis_modifiedr=   r   r   	functoolspartialr"   CONTAINS_GUARDrK   r   r   )rf   r0   r+   r   containsr&   r&   r'   install_dict_contains_guard  s*   
	z-ConstDictVariable.install_dict_contains_guardr+   list[VariableTracker]r   dict[str, VariableTracker]r   c                    sV	  ddl m}m  tj|ot|d }|dkr9ttj|g|R i |}|j	j
|  | j|j  d S |dkrPt|dkrHt|| | ||d S |dkrp|sX|r]t|| |   | jrl|j	j| j t| S |dkrt|r}t|| |   | jr|j	j| j t| S |dkr|s|rt|| |   | jr|j	j| j |s|rtt| t| S |d	kr|   |s|rt|| | j| j t d d
S |dkr|s|rt|| |    t| jS |dkr)|  r)|st|d  |   |st|dksJ |j	j
|  |d | j|d <  d S |dkrS|rS|  rS|   d| _ |j	j
|  | j!|d   d S |dkrt|dvrdt|| |smt|d  |d | vr| "|| t|dkr d S |d S | #||d S |dkr|  rt|dvrt|| |st|d  |d | vr| "|| t|dkrtt$| |d S d| _ |j	j
|  | j%|d S |dkrx|  rxt&| j'trt&| j't(j)st|rt|| | js d}tt$||gd | j't(j)u r_t|dks)d|v r_t|dkr>t*|d  r>|d j+}	n|,d }
rPt*|
 rP|
j+}	nt|| | jj-|	d\}}
n| j- \}}
d| _ |j	j
|  t.|j/|
gS |dkr|s|rt|| d| _ |j	j
|  | j0   d S |dkr|  r|   t|dk}t|dk}|s|r|j	j
|  |rt*|d tr|d   |d }n	|1|t|d }| j|j |r fdd| D }| j|  d S t2 3||||S |dkr5t|st|| |s$t|d  | "|| |d | v } |S |dkr|  rt|dvrKt|| |sTt|d  |   |r]J t|dksfJ | 4|d }|d urt|S t|dkr d }n|d }|j	j
|  || j|d < |S |dkr|   |j	j
|  |d | vrtt$| d}	t|dkrt*|d  r|d j+}	|rd|v rt*|d  r|,dj+}	|d }| jj5||	d  d S |dkrt6| trt|dkr	t|| t7t8j9:|| |d gi S |d kr* | 3|d||j+ S |d!krt|dks8J |d }t6|ttj;fs\d"| < j= d#|< j= d$}tt||gd | j'|j'h}t>d%d& |D rot(j)nt}|   | j| j t d |d'}|d   |j|d j |S |d(kr| 3|d|| | S t2 3||||S ))Nr   )rP   r   r   rg   __getitem__rF   r   rI   r   )rF   mutation_typer   __len____setitem__r   __delitem__Tr$   )r   r   r   popitemzpopitem(): dictionary is emptyr*   last)r   clearupdatec                    s    i | ]\}}  ||qS r&   )createrk   r   r   r&   r'   rn   ]  r   z1ConstDictVariable.call_method.<locals>.<dictcomp>r   
setdefaultmove_to_endr   __ne____or__z$unsupported operand type(s) for |: 'z'and ''c                 s   s    | ]	}t |tjV  qd S r4   )
issubclassr   r   )r8   tr&   r&   r'   r:     s    z0ConstDictVariable.call_method.<locals>.<genexpr>)rF   r   r   rc   __ior__)?r   rP   r   rb   rp   r6   r
   r   	call_dictr   r   mutationrF   r   r   r}   r   r   r   r   guard_on_key_orderaddDictItemsVariableDictKeysVariabler   r.   DictValuesVariablecloner   r   
is_mutabler1   r   r   r   r   r   r   r   rc   r   r   r=   rK   r$   r   rD   re   r   call_custom_dictr   call_methodr   r   r   rU   r	   dict___eq__call_functionUserDefinedDictVariableru   r   r   )rf   r0   namer+   r   rP   arg_hashabletemp_dict_vtr   r   rm   rl   has_arg
has_kwargsdict_vtr   rK   ra   r   r   tsrc   new_dict_vtr   r   r'   r    s  










































	
zConstDictVariable.call_methodc                 C   s   |    dd | j D S )Nc                 S      g | ]}|j qS r&   re   r   r&   r&   r'   
<listcomp>      z9ConstDictVariable.unpack_var_sequence.<locals>.<listcomp>)r   rF   r   rf   r0   r&   r&   r'   unpack_var_sequence  r   z%ConstDictVariable.unpack_var_sequencec                 C   s\   | j tu r|| j jv rtdS tdS d| j  d}tdd| j  |dgtjd d S )	NTFzhasattr on z is not supportedzunsupported hasattr operationzClass z+Consider using a regular dictionary insteadr   )rc   r   r#   r   r   r   r   SUPPORTABLE)rf   r0   r  r   r&   r&   r'   call_obj_hasattr  s   




z"ConstDictVariable.call_obj_hasattrc                    s   |    t jdi |S Nr&   )r   r   r	  )rf   r   r   r&   r'   r	    s   zConstDictVariable.cloner   r   r+   r   r   r   rd   r   )$r   r   r   r   DICT_CONTAINSr   r   _nonvar_fieldsrp   r   rg   r   rA   r   rv   r   ru   r   r   rN   r}   r   r   r   r   r   r   r   r   r   r   r  r  r  r	  __classcell__r&   r&   r   r'   rb      s^    X
#		

	'  -rb   c                       sb   e Zd Zdeddf fddZdd Zdd	 ZdddZded de	e
df ddfddZ  ZS )MappingProxyVariabledv_dictrd   Nc                    s*   t  jdi | t|tsJ || _d S r   )r   rg   r=   rb   r'  rf   r'  r   r   r&   r'   rg     s   
zMappingProxyVariable.__init__c                 C   s   t jS r4   )typesMappingProxyTyperz   r&   r&   r'   ru     r   z MappingProxyVariable.python_typec                 C   s   | j |S r4   )r'  r  r  r&   r&   r'   r    s   z(MappingProxyVariable.unpack_var_sequencer   r   c                    sd   | j rd| j  d}tdd| j  |dgtjd   fdd  | j  td	d
 d S )Nz*Preexisting MappingProxyVariable (source: zS) cannot be reconstructed because the connection to the original dict will be lost.z%mapping proxy cannot be reconstructedSource: zCUse a mapping proxy constructed in the same `torch.compile` region.r   c                      r   )Nr*  )r   r   r)  r   r&   r   r&   r'   r     r   z2MappingProxyVariable.reconstruct.<locals>.<lambda>r   F)r   r   r   r  r   r'  r   r   )rf   r   r   r&   r   r'   r     s"   
	

z MappingProxyVariable.reconstructr+   r   r   c                 C   sF   | j r|jj rd}tdd| j  d|ddgd | j||||S )Na  A dict has been modified while we have an existing mappingproxy object. A mapping proxy object, as the name suggest, proxies a mapping object (usually a dict). If the original dict object mutates, it is reflected in the proxy object as well. For an existing proxy object, we do not know the original dict it points to. Therefore, for correctness we graph break when there is dict mutation and we are trying to access a proxy object.z-mapping proxy affected by dictionary mutationr+  z, Dict mutation detectedzNAvoid modifying dictionaries that might be referenced by mapping proxy objectszROr avoid using the mapping proxy objects after modifying its underlying dictionaryr   )r   r   r   has_existing_dict_mutationr   r'  r  )rf   r0   r  r+   r   r   r&   r&   r'   r  "  s   
	z MappingProxyVariable.call_methodr!  )r   r   r   rb   rg   ru   r  r   listr   strr  r%  r&   r&   r   r'   r&    s    

r&  c                   @   s   e Zd Zdd Zdd ZdS )NNModuleHooksDictVariablec                 C      d S r4   r&   rz   r&   r&   r'   r   B     z7NNModuleHooksDictVariable.install_dict_keys_match_guardc                 C   r0  r4   r&   rf   r0   r+   r&   r&   r'   r   E  r1  z5NNModuleHooksDictVariable.install_dict_contains_guardN)r   r   r   r   r   r&   r&   r&   r'   r/  @  s    r/  c                       sf   e Zd Zdd fddZ fddZ fddZed	d
 Z						d fddZdd Z	  Z
S )DefaultDictVariableNrd   c                    s.   t  j||fi | |tju sJ || _d S r4   )r   rg   r   r   default_factory)rf   rF   rc   r4  r   r   r&   r'   rg   J  s   
zDefaultDictVariable.__init__c                    s$   | j tttfvr| jsdS t  S )NF)r4  r-  rq   r   rF   r   is_python_constantrz   r   r&   r'   r5  O  s   
z&DefaultDictVariable.is_python_constantc                    s   d| j   dt   dS )Nzdefaultdict(r   ))r4  r   r   rz   r   r&   r'   r   V  s   zDefaultDictVariable.debug_reprc                 C   s,   t | tjr| jttttfv S t | tjj	S r4   )
r=   r
   rP   fnr-  rq   r   set	functionsBaseUserFunctionVariabler   r&   r&   r'   is_supported_arg[  s   z$DefaultDictVariable.is_supported_argr+   r   r   r   r   c                    s   |dkr=t |dksJ |d | v r| ||d S | jd u r&t|d  | j|g i }t |d|d |f| |S t ||||S )Nr   r   r   r   )r}   r   r4  r   r  r   r  )rf   r0   r  r+   r   default_varr   r&   r'   r  b  s   
zDefaultDictVariable.call_methodc                    s:      fdd  | j |    tdd d S )Nc                      r   )Nr   r   r&   r   r&   r'   r   }  r   z1DefaultDictVariable.reconstruct.<locals>.<lambda>r   F)r   r4  r   r   r   r   r&   r   r'   r   z  s   


zDefaultDictVariable.reconstructr4   r   r"  )r   r   r   rg   r5  r   r   r;  r  r   r%  r&   r&   r   r'   r3  I  s    
r3  c                       s   e Zd ZdZejZdee ddf fddZ	dd Z
ed	d
 Zedd Zdd Zdd Zdd Zd'ddZdd Zdee deeef ddf fddZddd efd!d"Zd#d$ Z fd%d&Z  ZS )(SetVariablez.We model a sets as dictionary with None valuesrF   rd   Nc                    s(   t |t }t j|fi | d S r4   )r   fromkeysr=  _default_valuer   rg   rf   rF   r   r   r&   r'   rg     s   zSetVariable.__init__c                 C   ,   | j sdS dddd | j  D  d S )Nzset()r   ,c                 s       | ]}|j  V  qd S r4   r   r8   rl   r&   r&   r'   r:     rj   z)SetVariable.debug_repr.<locals>.<genexpr>r   rF   r   r   rz   r&   r&   r'   r        "zSetVariable.debug_reprc                 C   s   t | j S r4   )r8  rF   r   rz   r&   r&   r'   	set_items  s   zSetVariable.set_itemsc                   C   s
   t d S r4   )r   r   r&   r&   r&   r'   r?    s   
zSetVariable._default_valuec                 C      dd | j D S )Nc                 S      h | ]}|j  qS r&   r   rD  r&   r&   r'   	<setcomp>      z'SetVariable.as_proxy.<locals>.<setcomp>rG  rz   r&   r&   r'   rA        zSetVariable.as_proxyc                 C      t S r4   )r8  rz   r&   r&   r'   ru     r1  zSetVariable.python_typec                 C   rH  )Nc                 S   rI  r&   r   rD  r&   r&   r'   rJ    rK  z1SetVariable.as_python_constant.<locals>.<setcomp>rL  rz   r&   r&   r'   rv     rM  zSetVariable.as_python_constantr   r   c                 C   s2   | dd | jD  |tdt| jd d S )Nc                 S   r  r&   r  r   r&   r&   r'   r    r  z+SetVariable.reconstruct.<locals>.<listcomp>	BUILD_SETr   )foreachrG  r   r   r}   r   r&   r&   r'   r     s   zSetVariable.reconstructc              
   C   s   z|dd | g|D i dd |  D }W n" ty: } ztt||tttj|jd W Y d }~nd }~ww t	
||S )Nc                 S   s   g | ]}|  qS r&   rv   r   r&   r&   r'   r    s    z0SetVariable._fast_set_method.<locals>.<listcomp>c                 S   s   i | ]	\}}||  qS r&   rQ  rk   r&   r&   r'   rn     r   z0SetVariable._fast_set_method.<locals>.<dictcomp>r*   )rF   	Exceptionr   r"   r-  mapr   r   r+   r   build)rf   r0   r7  r+   r   resexcr&   r&   r'   _fast_set_method  s   
zSetVariable._fast_set_methodr+   r   r   c              
      s  ddl m} |dv r$|||r$|  tu r$|  }| |t||||S |dkrNttj|g||R  }|j	j
|  | j  | j|j td S |dkrm|rVJ t|dkrat|| d}|d t f}n|d	kr|ruJ |ryJ z| j j}W n  ty }	 ztt|tttj|	jd
 W Y d }	~	nd }	~	ww t |||f| |S |dkrt|dkrt|| |rJ tt j!"|| |d gi S |dkr|rJ tt j#"|| g|i S |dkr|rJ tt j$"|| g|i S |dkr|rJ tt j%"|| g|i S |dkr,|rJ tt j&"|| g|i S |dkrD|r6J tt j'"|| g|i S |dkrht|dkrUt|| |rZJ tt j("|| g|i S |dkrt|dkryt|| |r~J tt j)"|| g|i S |dkr| * r|rJ tt j+"|| g|i S |dkr|rJ t|dksJ |d | vrtt||d
 t |d	||S |dkr|rJ t|dksJ |d | v rt |d	||S tjd dS |dv r:t|dkrt|| t,j-t,j.d}
|d / }t0|ts+tt"||gi }t|
1|"|| |gi S |dv ryddddd1|}t2|d ttj3fsqtd| d| 4  d|d 4  d}tt5||gd
 | ||||S |dv rt2|d ttj3fstd| d| 4  d|d 4  d}tt5||gd
 ddddd1|}| |||| | S |dkrt2|d ttj3fstdS | |d||}tt|jdkS |t6v rt2|d ttj3fstt7S tt6| | j|d jS t ||||S ) Nr   )check_constant_args)
isdisjointunionintersection
differencesymmetric_differencerg   r  r   r   r   r   r*   rY  r[  intersection_updaterZ  r\  difference_updater]  symmetric_difference_updater   removediscard)rK   )issubset
issuperset)__and__r   __xor____sub__z unsupported operand type(s) for z: 'z' and 'r   )__iand__r  __ixor____isub__r   F)8utilsrX  ru   r8  rW  getattrr
   rP   call_setr   r   r  rF   r   r   r   r   r}   r   r=  r?  rG  r   re   r   r   r-  rS  r+   r   r  rU   r	   set_isdisjointr  set_intersectionset_intersection_update	set_unionset_differenceset_difference_updateset_symmetric_differenceset_symmetric_difference_updater
  
set_updateoperatorleger   r   r$   r=   UserDefinedSetVariablepython_type_namer.   r   NotImplemented)rf   r0   r  r+   r   rX  py_typetemp_set_vtresultr9   opr   mr   rr   r&   r'   r    sH  	
























"
"



zSetVariable.call_methodr0   r    r/   c                 C   s   t d)NzIllegal to getitem on a set)RuntimeError)rf   r0   r/   r&   r&   r'   r   h  s   zSetVariable.getitem_constc                 C   r0  r4   r&   rz   r&   r&   r'   r   k     z)SetVariable.install_dict_keys_match_guardc                    s   t  || d S r4   )r   r   r2  r   r&   r'   r   o  r)   z'SetVariable.install_dict_contains_guardr!  )r   r   r   r   r   SET_CONTAINSr   r-  r   rg   r   r   rG  r   r?  rA   ru   rv   r   rW  r   r.  r  r   r   r   r%  r&   r&   r   r'   r=    s:    



 (r=  c                       s~   e Zd Zdee ddf fddZdd Zedd	 Zd
d Z	dd Z
dddZdee deeef ddf fddZ  ZS )rS   rF   rd   Nc                       t  j|fi | d S r4   r   rg   r@  r   r&   r'   rg   t     zFrozensetVariable.__init__c                 C   rA  )Nzfrozenset()r   rB  c                 s   rC  r4   r   rD  r&   r&   r'   r:     rj   z/FrozensetVariable.debug_repr.<locals>.<genexpr>r   rE  rz   r&   r&   r'   r   {  rF  zFrozensetVariable.debug_reprc                 C   s
   | j  S r4   )rF   r   rz   r&   r&   r'   rG       
zFrozensetVariable.set_itemsc                 C   rN  r4   )	frozensetrz   r&   r&   r'   ru     r1  zFrozensetVariable.python_typec                 C   s   t dd | jD S )Nc                 S   rI  r&   r   rD  r&   r&   r'   rJ    rK  z7FrozensetVariable.as_python_constant.<locals>.<setcomp>)r  rG  rz   r&   r&   r'   rv     r   z$FrozensetVariable.as_python_constantr   r   c                    s<     dd | jD    fdd  tdd d S )Nc                 S   r  r&   r  r   r&   r&   r'   r    r  z1FrozensetVariable.reconstruct.<locals>.<listcomp>c                      s      dgS )Nr  )r   create_load_globalr&   r   r&   r'   r     s    z/FrozensetVariable.reconstruct.<locals>.<lambda>r   F)rP  rG  r   r   r   r   r&   r   r'   r     s
   
zFrozensetVariable.reconstructr+   r   r   c                    s`   |dv rt d| d|dkrtd S |dv r't ||||}t|jS t ||||S )Nr  r   r   ra  rb  r   Illegal call_method z on a frozensetrg   )r   r\  r[  r]  )r  r   r   r   r  rS   rF   )rf   r0   r  r+   r   r  r   r&   r'   r    s   

zFrozensetVariable.call_methodr!  )r   r   r   r-  r   rg   r   r   rG  ru   rv   r   r   r.  r  r%  r&   r&   r   r'   rS   s  s&    


rS   c                       s   e Zd Zdee ddf fddZdd Zdd	 Zd
d Ze	dd Z
dd Zdd Zdee deeef ddf fddZ  ZS )DictKeySetVariablerF   rd   Nc                    r  r4   r  r@  r   r&   r'   rg     r  zDictKeySetVariable.__init__c                 C   rA  )Nzdict_keys([])zdict_keys([rB  c                 s   rC  r4   r   rD  r&   r&   r'   r:     rj   z0DictKeySetVariable.debug_repr.<locals>.<genexpr>z])rE  rz   r&   r&   r'   r     s   zDictKeySetVariable.debug_reprc                 C   r0  r4   r&   rz   r&   r&   r'   r     r  z0DictKeySetVariable.install_dict_keys_match_guardc                 C   r0  r4   r&   r2  r&   r&   r'   r     r  z.DictKeySetVariable.install_dict_contains_guardc                 C   r   r4   r   rz   r&   r&   r'   rG    s   zDictKeySetVariable.set_itemsc                 C   rN  r4   r   rz   r&   r&   r'   ru     r1  zDictKeySetVariable.python_typec                 C   s   t dd | jD d  S )Nc                 S   rI  r&   r   rD  r&   r&   r'   rJ    rK  z8DictKeySetVariable.as_python_constant.<locals>.<setcomp>)r   r>  rG  r   rz   r&   r&   r'   rv     s
   z%DictKeySetVariable.as_python_constantr+   r   r   c                    s*   |dv rt d| dt ||||S )Nr  r  z on a dict_keys)r  r   r  rf   r0   r  r+   r   r   r&   r'   r    s   zDictKeySetVariable.call_method)r   r   r   r-  r   rg   r   r   r   r   rG  ru   rv   r   r.  r  r%  r&   r&   r   r'   r    s(    


r  c                       s   e Zd ZU dZdZee ed< deddf fddZ	e
dd	 Ze
d
d Zdd ZdddZdd Zded deedf ddf fddZ  ZS )DictViewVariablezu
    Models _PyDictViewObject

    This is an "abstract" class. Subclasses will override kv and the items method
    Nkvr'  rd   c                    s8   t  jdi | | jdv sJ t|tsJ || _d S )N)r   rI   rF   r&   )r   rg   r  r=   rb   r'  r(  r   r&   r'   rg     s   
zDictViewVariable.__init__c                 C   s   t | jj| j S r4   )rl  r'  rF   r  rz   r&   r&   r'   
view_items  s   zDictViewVariable.view_itemsc                 C   s   t r4   )NotImplementedErrorrz   r&   r&   r'   view_items_vt  s   zDictViewVariable.view_items_vtc                 C   r   r4   )r  r  r&   r&   r'   r    r   z$DictViewVariable.unpack_var_sequencer   r   c                 C   s$   || j  || j |d d S )Nr   )r'  load_methodr  r  r   r&   r&   r'   r     s   
zDictViewVariable.reconstructc                 C   s"   ||   jv rtdS tdS )NTF)ru   r#   r   r   )rf   r0   r  r&   r&   r'   r    s   

z!DictViewVariable.call_obj_hasattrr+   r   r   c                    s,   |dkr| j ||||S t ||||S )Nr   )r'  r  r   r  r   r&   r'   r    s   zDictViewVariable.call_methodr!  )r   r   r   r   r  r   r.  __annotations__rb   rg   r   r  r  r  r   r  r-  r   r  r%  r&   r&   r   r'   r    s$   
 



r  c                       sZ   e Zd ZdZedd Zedd Zdd Zded	 d
e	e
d	f dd	f fddZ  ZS )r  r   c                 C   rx   r4   )r8  r  rz   r&   r&   r'   rG    r  zDictKeysVariable.set_itemsc                 C   rH  )Nc                 S   r  r&   r  r   r&   r&   r'   r  !  r  z2DictKeysVariable.view_items_vt.<locals>.<listcomp>r  rz   r&   r&   r'   r       zDictKeysVariable.view_items_vtc                 C   rN  r4   r  rz   r&   r&   r'   ru   #  r1  zDictKeysVariable.python_typer+   r   r   rd   c                    s   |dkr| j ||||S |dv r"t| j|}||d j}t|S |tv rBt|d ttfs4t	t
S t	t| | j|d jS t ||||S )Nr   )re  rh  r   r  rg  rj  rf  ri  r   )r'  r  rl  rG  r=  r   r=   r  r   r   r|  r   )rf   r0   r  r+   r   r  r  r   r&   r'   r  &  s   
zDictKeysVariable.call_method)r   r   r   r  r   rG  r  ru   r-  r   r.  r  r%  r&   r&   r   r'   r    s    


r  c                   @   $   e Zd ZdZedd Zdd ZdS )r  rI   c                 C   rx   r4   )r-  r  rz   r&   r&   r'   r  J  r  z DictValuesVariable.view_items_vtc                 C   rN  r4   )r   rz   r&   r&   r'   ru   N  r1  zDictValuesVariable.python_typeNr   r   r   r  r   r  ru   r&   r&   r&   r'   r  F  s
    
r  c                   @   r  )r  rF   c                 C   rH  )Nc                 S   s    g | ]\}}t |j|gqS r&   )r
   rD   re   rk   r&   r&   r'   r  X  r   z3DictItemsVariable.view_items_vt.<locals>.<listcomp>r  rz   r&   r&   r'   r  U  r  zDictItemsVariable.view_items_vtc                 C   rN  r4   )r   rz   r&   r&   r'   ru   Z  r1  zDictItemsVariable.python_typeNr  r&   r&   r&   r'   r  R  s
    
r  r4   )<r   r   r   rL   rw  r)  collections.abcr   rO   typingr   r   torch._subclasses.fake_tensorr   r   r   r	   r
   bytecode_transformationr   r   rV  r   r   guardsr   r   r   r   rk  r   r   r   r   r   r   r   r   r   r   constantr   torch._dynamo.codegenr   r,   r    r(   r1   r6   rb   r&  r/  r3  r=  rS   r  r  r  r  r  r&   r&   r&   r'   <module>   sN   $	

5    ~F	C h@22/