o
    ,hQ                     @   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# ddl$m%Z%m&Z& ddl'm(Z( er~ddl)m*Z* ddl+m,Z, dd Z-dd Z.d0d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 )1a  
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specialize_symnode   )ValueMutationNewVariableTrackerConstantVariable)	PyCodegenInstructionTranslatorc                 C   s    t t| td| dgd d S )Nzwrong number of arguments for z() callargs)r   	TypeErrorr   )txname r%   X/var/www/html/scripts/venv/lib/python3.10/site-packages/torch/_dynamo/variables/dicts.pyraise_args_mismatch<   s
   
r'   c                 C   s   t | jddS )N__instancecheck__F)type__dict__get)objr%   r%   r&   was_instancecheck_overrideD   s   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: r    )torch._dynamo.symbolic_convertr   
current_txr   r"   r   r)   )argr#   r   r%   r%   r&   raise_unhashableH   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rRt| jsRt| jdtju rRt | jtrRt | jtS t | 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$fS )NTexample_valuec                 s   s    | ]}t |V  qd S N)is_hashable.0er%   r%   r&   	<genexpr>d   s    zis_hashable.<locals>.<genexpr>__hash__)%
isinstancer
   LazyVariableTrackeris_realizedr4   TensorVariableas_proxynodemetar+   TupleVariableallitemsUserDefinedObjectVariabler-   valueinspectgetattr_staticintr9   py_HashableBuiltinVariableSymNodeVariabler   EnumVariableUserDefinedClassVariableUserFunctionVariableSkipFunctionVariablemiscNumpyVariableNNModuleVariableUnspecializedNNModuleVariableMethodWrapperVariableTorchInGraphFunctionVariableTypingVariableFunctoolsPartialVariableWeakRefVariable)xr%   r%   r&   r4   R   sL   


r4   c                       s2  e Zd 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efddZdd Z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d.d/ Z	0	1	2	3		4d> fd5d6Zd7d8 Zd9d: Z fd;d<Z  ZS )?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 r3   )r   r4   r1   vt)selfr]   r%   r%   r&   __init__   s   
z+ConstDictVariable._HashableTracker.__init__c                    s  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r| jjS | j }|S )Nr2   c                 3   s    | ]} |j V  qd S r3   )underlying_valuer5   r   r%   r&   r8          zFConstDictVariable._HashableTracker.underlying_value.<locals>.<genexpr>)r:   r]   r
   r;   r<   r4   original_valuer=   r>   r?   r@   rA   rZ   _HashableTrackertuplerC   rR   rE   rS   rN   get_functionrX   referent_vtr`   rD   as_python_constant)r^   rY   r%   r   r&   r`      s6   


z3ConstDictVariable._HashableTracker.underlying_valuec                 C   
   t | jS r3   )hashr`   r^   r%   r%   r&   r9      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 r3   )_eq_impl)r6   uvr   r%   r&   r8      s    
z>ConstDictVariable._HashableTracker._eq_impl.<locals>.<genexpr>)	r)   r:   rd   rZ   rc   lenrB   zipr   )abr%   r   r&   rk      s   
z+ConstDictVariable._HashableTracker._eq_implotherc                 C   sN   t j}t||st|sJ t|t||r || j|jS || j|S r3   )rZ   rc   r:   r   
is_literalr)   rk   r`   )r^   rr   r   r%   r%   r&   __eq__   s   
z)ConstDictVariable._HashableTracker.__eq__r\   N)__name__
__module____qualname____doc__r_   propertyr`   r9   staticmethodrk   boolrt   r%   r%   r%   r&   rc      s    
	

rc   rC   r\   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 r3   )r:   r   r6   rY   rm   r   r%   r&   r8      s    
z-ConstDictVariable.__init__.<locals>.<genexpr>c                    s   t |  r| S  | S r3   )r:   )keyr   r%   r&   make_hashable   s   z1ConstDictVariable.__init__.<locals>.make_hashablec                    s   i | ]	\}} ||qS r%   r%   r   )r   r%   r&   
<dictcomp>   s    z.ConstDictVariable.__init__.<locals>.<dictcomp>r%   )popsuperr_   rZ   rc   rB   rC   r   sourcer~   copyr}   r[   )r^   rC   r[   kwargs	__class__)r   r   r&   r_      s   



zConstDictVariable.__init__c                 C      dd | j   D S )Nc                 S       i | ]\}}|j  | qS r%   r]   r>   r6   krm   r%   r%   r&   r           z.ConstDictVariable.as_proxy.<locals>.<dictcomp>rC   rj   r%   r%   r&   r>      s   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r]   
debug_reprr   r%   r%   r&   r8      s    
z/ConstDictVariable.debug_repr.<locals>.<genexpr>})joinrC   rj   r%   r%   r&   r      s   
zConstDictVariable.debug_reprc                 C   r   )Nc                 S   r   r%   r]   rg   r   r%   r%   r&   r         z8ConstDictVariable.as_python_constant.<locals>.<dictcomp>r   rj   r%   r%   r&   rg     s   z$ConstDictVariable.as_python_constantc                 C   s   |    dd | j D S )Nc                 S   s   i | ]
\}}|j  |qS r%   r   r   r%   r%   r&   r     s    z=ConstDictVariable.keys_as_python_constant.<locals>.<dictcomp>)install_dict_keys_match_guardrC   rj   r%   r%   r&   keys_as_python_constant
     z)ConstDictVariable.keys_as_python_constantc                 C      | j S r3   )r[   rj   r%   r%   r&   python_type     zConstDictVariable.python_typec                 C   sB   t |tsJ tj}t|o ||| jv o t | j|| tj S r3   )r:   r   rZ   rc   r4   rC   r
   DeletedVariable)r^   r]   r   r%   r%   r&   __contains__  s   zConstDictVariable.__contains__c                 C   s   t dd | j D S )Nc                 S   s   g | ]
}t |tjs|qS r%   )r:   r
   r   r6   rY   r%   r%   r&   
<listcomp>  s    
z)ConstDictVariable.len.<locals>.<listcomp>)rn   rC   valuesrj   r%   r%   r&   rn     s
   zConstDictVariable.lenc                    s&    j rdS t fdd j D S )NTc                 3   s*    | ]\}}   j|j|V  qd S r3   )is_new_itemr}   r+   r]   )r6   r   rE   rj   r%   r&   r8   &  s
    
z2ConstDictVariable.has_new_items.<locals>.<genexpr>)r~   anyrC   rj   r%   rj   r&   has_new_items#  s
   zConstDictVariable.has_new_itemsc                 C   s<   |r|  r|  rt| t| kS t|t|kS r3   )r<   idrealize)r^   rE   rr   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r0   )rC   r}   r+   r]   r   r~   append_outputr   )r^   codegennum_argsr   rE   itemr%   r%   r&   reconstruct_kvs_into_new_dict1  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 )NOrderedDictextend_outputcreate_load_python_modulecollectionscreate_load_attrr%   r   r%   r&   <lambda>A  
    z/ConstDictVariable.reconstruct.<locals>.<lambda>r   F)r[   r   r   add_push_nullr   r   r   r^   r   r%   r   r&   reconstruct=  s   

zConstDictVariable.reconstructr#   r   r0   c                 C   s(   t |}|| jvrtt| | j| S r3   )rZ   rc   rC   r   KeyError)r^   r#   r0   r   r%   r%   r&   'getitem_const_raise_exception_if_absentM  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)rZ   rc   rC   rE   r   r   
USER_ERROR)r^   r#   r0   r   msgr%   r%   r&   getitem_constU  s   



	zConstDictVariable.getitem_constc                 C   s"   t |}|| jvrd S | j| S r3   )rZ   rc   rC   )r^   r0   r   r%   r%   r&   maybe_getitem_constd  s   


z%ConstDictVariable.maybe_getitem_constc                 C   sX   || v sJ t |}t| j |}t| j | }t|tj	r*|
  d S d S r3   )rZ   rc   rd   rC   keysindexr}   r:   r
   r;   r   )r^   r0   r   r   original_key_vtr%   r%   r&   realize_key_vtj  s   
z ConstDictVariable.realize_key_vtc                 C   s   | j rt| tj d S d S r3   )r   r   
make_guardr   DICT_KEYS_MATCHrj   r%   r%   r&   r   s  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rR|rL| |d  d S |   d S d S )Nr   r   r   )r   invert) r   r   outputside_effectsis_modifiedr:   r   r   	functoolspartialr   DICT_CONTAINSrE   r   r   )r^   r#   r!   r   containsr%   r%   r&   install_dict_contains_guardw  s*   
	z-ConstDictVariable.install_dict_contains_guardr!   list[VariableTracker]r   dict[str, VariableTracker]r   c                    s>  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rMt|dksEJ | ||d S |dkrj|sU|rWJ |   | jrf|j	j| j t| S |dkr|   | jr}|j	j| j |s|rJ t| S |dkr|   | jr|j	j| j |s|rJ t| S |d	kr|   |s|rJ | j| j t d d
S |dkr|s|rJ |    t| jS |dkr|  r|st|d  |   |st|dksJ |j	j
|  |d | j|d <  d S |dkr5|r5|  r5|   d| _|j	j
|  | j|d   d S |dv rgt|dv rg|d | vrg| || t|dkrc|dkr_t t!|  d S |d S |dkr|r|  rd| _|j	j
|  | j"|d S |dkrd| _|j	j
|  | j#   d S |dkr|  r|   t|dk}t|dk}	|s|	r|j	j
|  |rt$|d tr|d   |d }
n	|%|t|d }
| j|
j |	r fdd| D }| j|  d S t& '||||S |dv r%|d | v r%| (||d S |dkrKt|dkrK|s:t|d  | || |d | v } |S |dkr|r|  r|   |raJ t|dksjJ | )|d }|d urx|S t|dkr d }n|d }|j	j
|  || j|d < |S |dkr|   |st|dksJ |j	j
|  |d }| j| }| j"| || j|<  d S |dkrt|dksJ t$|d tst*d|d + j, d|   | j| j t d d
}|d   |j|d j |S t& '||||S )Nr   )rJ   r   r   r_   __getitem__rC   r   r   r   )rC   mutation_typer   __len____setitem__r   __delitem__T)r   r+   )r   r   r   clearupdatec                    s    i | ]\}}  ||qS r%   )creater   r   r   r%   r&   r     r   z1ConstDictVariable.call_method.<locals>.<dictcomp>)r+   __getattr__r   
setdefaultmove_to_end__or__z/unsupported operand type(s) for |: 'dict' and '')-r   rJ   r   rZ   rc   r4   r
   dict	call_dictr   r   mutationrC   r   r   rn   r   r   r   guard_on_key_orderaddDictItemsVariableDictKeysVariableDictValuesVariablecloner   r   
is_mutabler1   r~   r   r   r   r   r   r   r:   call_custom_dictr   call_methodr   r   r"   r   rv   )r^   r#   r$   r!   r   rJ   arg_hashabletemp_dict_vthas_arg
has_kwargsdict_vtr   rE   rY   r   val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%   r]   r   r%   r%   r&   r   M      z9ConstDictVariable.unpack_var_sequence.<locals>.<listcomp>)r   rC   r   r^   r#   r%   r%   r&   unpack_var_sequenceK  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   )r[   r   r*   r   r   r   r   SUPPORTABLE)r^   r#   r$   r   r%   r%   r&   call_obj_hasattrO  s   




z"ConstDictVariable.call_obj_hasattrc                    s   |    t jdi |S Nr%   )r   r   r   )r^   r   r   r%   r&   r   b  s   zConstDictVariable.cloner   r   r!   r   r   r   r\   r   )rv   rw   rx   r   _nonvar_fieldsrc   r   r_   r>   r   rg   r   r   r|   r   rn   r   r   r   r   r   r   r   r   r   r   r   r   r  r   __classcell__r%   r%   r   r&   rZ      sX    P
"			

	' .rZ   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_dictr\   Nc                    s*   t  jdi | t|tsJ || _d S r  )r   r_   r:   rZ   r  r^   r  r   r   r%   r&   r_   i  s   
zMappingProxyVariable.__init__c                 C   s   t jS r3   )typesMappingProxyTyperj   r%   r%   r&   r   n  r   z MappingProxyVariable.python_typec                 C   s   | j |S r3   )r  r   r   r%   r%   r&   r   q  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   )r^   r   r   r%   r   r&   r   t  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   )r^   r#   r$   r!   r   r   r%   r%   r&   r     s   
	z MappingProxyVariable.call_methodr  )rv   rw   rx   rZ   r_   r   r   r   listr   strr   r  r%   r%   r   r&   r  g  s    

r  c                   @   s   e Zd Zdd Zdd ZdS )NNModuleHooksDictVariablec                 C      d S r3   r%   rj   r%   r%   r&   r        z7NNModuleHooksDictVariable.install_dict_keys_match_guardc                 C   r  r3   r%   r^   r#   r!   r%   r%   r&   r     r  z5NNModuleHooksDictVariable.install_dict_contains_guardN)rv   rw   rx   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 )DefaultDictVariableNr\   c                    s.   t  j||fi | |tju sJ || _d S r3   )r   r_   r   defaultdictdefault_factory)r^   rC   r[   r  r   r   r%   r&   r_     s   
zDefaultDictVariable.__init__c                    s$   | j tttfvr| jsdS t  S )NF)r  r  rd   r   rC   r   is_python_constantrj   r   r%   r&   r    s   
z&DefaultDictVariable.is_python_constantc                    s   d| j   dt   dS )Nzdefaultdict(r   ))r  r   r   rj   r   r%   r&   r     s   zDefaultDictVariable.debug_reprc                 C   s,   t | tjr| jttttfv S t | tjj	S r3   )
r:   r
   rJ   fnr  rd   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   )rn   r   r  r   call_functionr   r   )r^   r#   r$   r!   r   default_varr   r%   r&   r     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   r  r   r   r   r   r%   r   r&   r     s   


zDefaultDictVariable.reconstructr3   ru   r  )rv   rw   rx   r_   r  r   r{   r  r   r   r  r%   r%   r   r&   r    s    
r  c                       s   e Zd Zd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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d#d$ Z  ZS )&SetVariablez.We model a sets as dictionary with None valuesrC   r\   Nc                    s(   t |t }t j|fi | d S r3   )r   fromkeysr   _default_valuer   r_   r^   rC   r   r   r%   r&   r_     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 r3   r   r6   r   r%   r%   r&   r8     ra   z)SetVariable.debug_repr.<locals>.<genexpr>r   rC   r   r   rj   r%   r%   r&   r        "zSetVariable.debug_reprc                 C   s   t | j S r3   )r  rC   r   rj   r%   r%   r&   	set_items
  s   zSetVariable.set_itemsc                   C   s
   t d S r3   )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   r'  r%   r%   r&   	<setcomp>      z'SetVariable.as_proxy.<locals>.<setcomp>r*  rj   r%   r%   r&   r>        zSetVariable.as_proxyc                 C      t S r3   )r  rj   r%   r%   r&   r     r  zSetVariable.python_typec                 C   r+  )Nc                 S   r,  r%   r   r'  r%   r%   r&   r-    r.  z1SetVariable.as_python_constant.<locals>.<setcomp>r/  rj   r%   r%   r&   rg     r0  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   )foreachr*  r   r   rn   r   r%   r%   r&   r     s   zSetVariable.reconstructr!   r   r   c           
   
      s  |dkr*t tj|g||R  }|jj|  | j  | j	|j t
d S |dkrI|r2J t|dkr=t|| d}|d t f}n|dkr|rQJ |rUJ 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r|rJ t|dksJ 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|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>t|dkr+t|| |r0J t tj#|| g|i S |dkrbt|dkrOt|| |rTJ t tj$|| g|i S |dkr| % r|rqJ 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'j(t'j)d}|d * }	t+|	tst t||	gi }	t |,||| |	gi S t ||||S )Nr_   r   r   r   r   r   r    
isdisjointintersectionintersection_updateunion
differencedifference_updatesymmetric_differencesymmetric_difference_updater   removediscard)rE   )issubset
issuperset)-r
   rJ   r  call_setr   r   r   rC   r   r   r   r   rn   r'   r   r"  r*  r   r]   r   r   r  mapr!   r   r   rN   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^   r#   r$   r!   r   temp_set_vtresultr7   oprr   r   r%   r&   r      s   


















zSetVariable.call_methodr#   r   r0   c                 C   s   t d)NzIllegal to getitem on a set)RuntimeError)r^   r#   r0   r%   r%   r&   r     s   zSetVariable.getitem_constc                 C   r  r3   r%   rj   r%   r%   r&   r        z)SetVariable.install_dict_keys_match_guardc                 C   r  r3   r%   r  r%   r%   r&   r     rR  z'SetVariable.install_dict_contains_guardr  )rv   rw   rx   ry   r  r   r_   r   rz   r*  r{   r"  r>   r   rg   r   r   r  r   r   r   r   r  r%   r%   r   r&   r     s4    



l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 )FrozensetVariablerC   r\   Nc                       t  j|fi | d S r3   r   r_   r#  r   r%   r&   r_        zFrozensetVariable.__init__c                 C   r$  )Nzfrozenset()r   r%  c                 s   r&  r3   r   r'  r%   r%   r&   r8     ra   z/FrozensetVariable.debug_repr.<locals>.<genexpr>r   r(  rj   r%   r%   r&   r     r)  zFrozensetVariable.debug_reprc                 C   s
   | j  S r3   )rC   r   rj   r%   r%   r&   r*       
zFrozensetVariable.set_itemsc                 C   r1  r3   )	frozensetrj   r%   r%   r&   r     r  zFrozensetVariable.python_typec                 C   r+  )Nc                 S   r,  r%   r   r'  r%   r%   r&   r-    r.  z7FrozensetVariable.as_python_constant.<locals>.<setcomp>r/  rj   r%   r%   r&   rg     r0  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 )NrX  )r   create_load_globalr%   r   r%   r&   r     s    z/FrozensetVariable.reconstruct.<locals>.<lambda>r   F)r3  r*  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 t ||||S )Nr   r   r   r<  r=  r   Illegal call_method z on a frozensetr_   )rQ  r   r   r   r   r^   r#   r$   r!   r   r   r%   r&   r     s
   
zFrozensetVariable.call_methodr  )rv   rw   rx   r  r   r_   r   rz   r*  r   rg   r   r   r  r   r  r%   r%   r   r&   rS    s&    


rS  c                       st   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ee deeef ddf fddZ  ZS )DictKeySetVariablerC   r\   Nc                    rT  r3   rU  r#  r   r%   r&   r_     rV  zDictKeySetVariable.__init__c                 C   r$  )Nzdict_keys([])zdict_keys([r%  c                 s   r&  r3   r   r'  r%   r%   r&   r8     ra   z0DictKeySetVariable.debug_repr.<locals>.<genexpr>z])r(  rj   r%   r%   r&   r     s   zDictKeySetVariable.debug_reprc                 C   r   r3   r   rj   r%   r%   r&   r*    s   zDictKeySetVariable.set_itemsc                 C   r1  r3   r   rj   r%   r%   r&   r     r  zDictKeySetVariable.python_typec                 C   s   t dd | jD d  S )Nc                 S   r,  r%   r   r'  r%   r%   r&   r-    r.  z8DictKeySetVariable.as_python_constant.<locals>.<setcomp>)r   r!  r*  r   rj   r%   r%   r&   rg     s
   z%DictKeySetVariable.as_python_constantr!   r   r   c                    s*   |dv rt d| dt ||||S )NrZ  r[  z on a dict_keys)rQ  r   r   r\  r   r%   r&   r     s   zDictKeySetVariable.call_method)rv   rw   rx   r  r   r_   r   rz   r*  r   rg   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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  r\   c                    s8   t  jdi | | jdv sJ t|tsJ || _d S )N)r   r   rC   r%   )r   r_   r`  r:   rZ   r  r	  r   r%   r&   r_     s   
zDictViewVariable.__init__c                 C   s   t | jj| j S r3   )getattrr  rC   r`  rj   r%   r%   r&   
view_items	  s   zDictViewVariable.view_itemsc                 C   s   t r3   )NotImplementedErrorrj   r%   r%   r&   view_items_vt  s   zDictViewVariable.view_items_vtc                 C   r   r3   )rd  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.reconstructr!   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  )rv   rw   rx   ry   r`  r   r  __annotations__rZ   r_   rz   rb  rd  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   rh   r3   )r  rb  rj   r%   r%   r&   r*  *  rW  zDictKeysVariable.set_itemsc                 C   r+  )Nc                 S   r   r%   r   r   r%   r%   r&   r   1  r   z2DictKeysVariable.view_items_vt.<locals>.<listcomp>rb  rj   r%   r%   r&   rd  .     zDictKeysVariable.view_items_vtc                 C   r1  r3   r^  rj   r%   r%   r&   r   3  r  zDictKeysVariable.python_typer!   r   r   r\   c                    sl   |dkr| j ||||S |tv r-t|d ttfsttS tt| | j	|d j	S t
 ||||S )Nr   r   )r  r   r   r:   r   r   r   r   NotImplementedr*  r   r\  r   r%   r&   r   6  s   
zDictKeysVariable.call_method)rv   rw   rx   r`  rz   r*  rd  r   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   r   c                 C   rh   r3   )r  rb  rj   r%   r%   r&   rd  L  rW  z DictValuesVariable.view_items_vtc                 C   r1  r3   )r   rj   r%   r%   r&   r   P  r  zDictValuesVariable.python_typeNrv   rw   rx   r`  rz   rd  r   r%   r%   r%   r&   r   H  s
    
r   c                   @   rj  )r   rC   c                 C   r+  )Nc                 S   s    g | ]\}}t |j|gqS r%   )r
   rA   r]   r   r%   r%   r&   r   Z  r   z3DictItemsVariable.view_items_vt.<locals>.<listcomp>rg  rj   r%   r%   r&   rd  W  rh  zDictItemsVariable.view_items_vtc                 C   r1  r3   )r   rj   r%   r%   r&   r   \  r  zDictItemsVariable.python_typeNrk  r%   r%   r%   r&   r   T  s
    
r   r3   )<ry   r   r   rF   rK  r
  collections.abcr   rI   typingr   r   torch._subclasses.fake_tensorr   r   r   r	   r
   bytecode_transformationr   r   excr   r   guardsr   r   r   r   utilsr   r   r   r   r   r   baser   r   constantr   torch._dynamo.codegenr   r.   r   r'   r-   r1   r4   rZ   r  r  r  r   rS  r]  r_  r   r   r   r%   r%   r%   r&   <module>   sN    

1   gF	C  8*-!