o
    eiy                    @   sJ  d 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mZ ddlmZ ddlmZmZmZ ddlmZ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!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/m0Z0 erddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 de	de9fddZ:	d:de-ded ddfddZ;de-de9fddZ<G d d! d!e-Z=G d"d# d#e-Z>G d$d% d%e=Z?G d&d' d'e=Z@G d(d) d)e=ZAG d*d+ d+e-ZBG d,d- d-eAZCG d.d/ d/eAZDG d0d1 d1eAZEG d2d3 d3e-ZFG d4d5 d5eFZGG d6d7 d7eFZHG d8d9 d9eFZId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)IterableSequence)AnyLiteralOptionalTYPE_CHECKINGUnion
OrderedSet   )graph_break_hints	polyfills	variables)create_call_functioncreate_call_methodcreate_dup_topcreate_instruction)raise_observed_exceptionunimplemented)GuardBuilderinstall_guard)
AttrSourceis_constant_sourceis_from_local_source)cmp_name_to_op_mapping
dict_items	dict_keysdict_valuesistyperaise_args_mismatchspecialize_symnode   )ValueMutationNewVariableTracker)CONSTANT_VARIABLE_NONEConstantVariable)	PyCodegenInstructionTranslator)BuiltinVariable)UserFunctionVariableobjreturnc                 C   s   t | jddS )N__instancecheck__F)type__dict__get)r+    r1   g/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/_dynamo/variables/dicts.pywas_instancecheck_overrideB   s   r3   argtxr(   c                 C   s   ddl m} |d u rddlm} | }z|  }W n ty'   t| }Y nw tt	||
|d|dt|   gd d S )Nr!   SourcelessBuilderr   r'   zunhashable type: z and variable tracker = args)builderr7   torch._dynamo.symbolic_convertr(   
current_txpython_type	Exceptionr.   r   	TypeErrorcreaterealize)r4   r5   r7   r(   arg_typer1   r1   r2   raise_unhashableF   s$   
rC   xc                 C   s(   t | tjr|  s|  rdS |  S )NT)
isinstancer   LazyVariableTrackeris_realizedis_hashableis_python_hashable)rD   r1   r1   r2   rH   `   s   
rH   c                       sJ  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e
deddf fdd	Zde
de
fd
dZde	eef fddZdefddZde	eef fddZde	eef fddZde
fddZdedefddZdddddedee de	eef defd d!Zdefd"d#Zdefd$d%Zd&ee d'edefd(d)ZdId,d-ZdId.d/Zddd0edefd1d2Zddd0edefd3d4Z d0edee fd5d6Z!d0eddfd7d8Z"dJd9d:Z#ddd;e$e ddfd<d=Z%ddd>ed;e$e de	eef def
 fd?d@Z&ddde$e fdAdBZ'ddd>ede(fdCdDZ)dedef fdEdFZ*defdGdHZ+  Z,S )KConstDictVariableuser_clsc                   @   sB   e Zd ZdZdeddfddZdefddZd	ede	fd
dZ
dS )"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
        vtr,   Nc                 C   s"   t |}t|st| || _d S N)r    rH   rC   rM   )selfrM   r1   r1   r2   __init__}   s   
z+ConstDictVariable._HashableTracker.__init__c                 C   s:   t | jtjr| j s| j rt| j S | j S )a  
            Computes the hash value for the wrapped VariableTracker.

            For unrealized LazyVariableTrackers, uses the hash of the original value
            to avoid realizing the tracker and inserting unnecessary guards.
            For all other cases, delegates to the VariableTracker's get_python_hash method.

            Returns:
                The hash value of the underlying variable tracker
            )	rE   rM   r   rF   rG   rH   hashoriginal_valueget_python_hashrO   r1   r1   r2   __hash__   s   
z+ConstDictVariable._HashableTracker.__hash__otherc                 C   s.   t |tjsdS | j|ju rdS | j|jS )a  
            Checks equality between two _HashableTracker instances.

            Delegates to the VariableTracker's is_python_equal method to compare
            the underlying variable trackers for Python-level equality.

            Args:
                other: Another _HashableTracker instance to compare with

            Returns:
                True if the underlying variable trackers are Python-equal, False otherwise
            FT)rE   rJ   _HashableTrackerrM   is_python_equalrO   rV   r1   r1   r2   __eq__   s
   z)ConstDictVariable._HashableTracker.__eq__)__name__
__module____qualname____doc__r#   rP   intrU   objectboolrZ   r1   r1   r1   r2   rW   v   s
    	rW   itemskwargsr,   Nc                    s   d|v r	| d d|v r| d t jdi | tj t fdd| D s-J dttdf ddf fdd	| 	|}|fd
d| D | _| j
rXt| j
 nd| _| | _|| _d S )Noriginal_itemsshould_reconstruct_allc                 3   s,    | ]\}}t |t fot |tV  qd S rN   )rE   r#   .0rD   vHashabler1   r2   	<genexpr>   s    
z-ConstDictVariable.__init__.<locals>.<genexpr>keyrL   r,   c                    s   t |  r| S  | S rN   )rE   )rl   ri   r1   r2   make_hashable   s   z1ConstDictVariable.__init__.<locals>.make_hashablec                    s   i | ]	\}} ||qS r1   r1   rf   )rm   r1   r2   
<dictcomp>       z.ConstDictVariable.__init__.<locals>.<dictcomp>Tr1   )popsuperrP   rJ   rW   allrb   r   r#   _get_dict_cls_from_user_clssourcer   re   copyrd   rK   )rO   rb   rK   rc   dict_cls	__class__)rj   rm   r2   rP      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 rN   r1   )rg   baseaccepted_dict_typesr1   r2   rk      s    z@ConstDictVariable._get_dict_cls_from_user_cls.<locals>.<genexpr>)dictcollectionsOrderedDictdefaultdictnext__mro__)rO   rK   rv   r1   rz   r2   rs      s   
z-ConstDictVariable._get_dict_cls_from_user_clsc                 C      dd | j   D S )Nc                 S       i | ]\}}|j  | qS r1   rM   as_proxyrg   krh   r1   r1   r2   rn           z.ConstDictVariable.as_proxy.<locals>.<dictcomp>rb   rT   r1   r1   r2   r         zConstDictVariable.as_proxyc                 C   s|   g }| j   D ]-\}}t|jdrt|jjn|j }t|dr&t|jn| }|| d|  qdd| d S )Nvaluez: {, }rb   hasattrrM   reprr   
debug_reprappendjoinrO   rb   r   rh   key_strval_strr1   r1   r2   r      s   "zConstDictVariable.debug_reprc                 C   r   )Nc                 S   r   r1   rM   as_python_constantr   r1   r1   r2   rn          z8ConstDictVariable.as_python_constant.<locals>.<dictcomp>r   rT   r1   r1   r2   r      s   z$ConstDictVariable.as_python_constantc                 C   s   |    dd | j D S )Nc                 S   s   i | ]
\}}|j  |qS r1   r   r   r1   r1   r2   rn      s    z=ConstDictVariable.keys_as_python_constant.<locals>.<dictcomp>install_dict_keys_match_guardrb   rT   r1   r1   r2   keys_as_python_constant   s   z)ConstDictVariable.keys_as_python_constantc                 C      | j S rN   rK   rT   r1   r1   r2   r=         zConstDictVariable.python_typerM   c                 C   sB   t |tsJ tj}| o ||| jv o t | j|| tj S rN   )rE   r#   rJ   rW   rI   rb   r   DeletedVariable)rO   rM   rj   r1   r1   r2   __contains__   s   zConstDictVariable.__contains__r5   r(   tree_map_fnr*   map_fnresttree_map_kwargsc                    s   g }|D ]%}|  }t|trt|jt| jkr$| |||||  S || qt| j  | j D ]6\}}	g }
|D ]"}z
|
|j|  W q= ty_   | ||||| Y     S w |		||||
| |< q5 fdd D }| j
 |dd t dS )Nc                    s   i | ]}|j  | qS r1   rM   )rg   key_trackernew_items_hashedr1   r2   rn   ,  s    
z:ConstDictVariable.call_tree_map_branch.<locals>.<dictcomp>T)rb   rd   re   rt   mutation_type)rA   rE   rJ   lenrb   _tree_map_fallbackr   r.   KeyErrorcall_tree_mapcloner"   )rO   r5   r   r   r   r   other_dicts	candidater   r   sibling_leavesupdated_original_itemsr1   r   r2   call_tree_map_branch  sP   



z&ConstDictVariable.call_tree_map_branchc                 C   s   t dd | j D S )Nc                 s   s    | ]
}t |tj V  qd S rN   )rE   r   r   rg   rD   r1   r1   r2   rk   :  s    
z(ConstDictVariable.len.<locals>.<genexpr>)sumrb   valuesrT   r1   r1   r2   r   9  s   zConstDictVariable.lenc                    s"    j pt fdd j D S )Nc                 3   s*    | ]\}}   j|j|V  qd S rN   )is_new_itemrd   r0   rM   )rg   rl   r   rT   r1   r2   rk   ?  s
    
z2ConstDictVariable.has_new_items.<locals>.<genexpr>)re   anyrb   rT   r1   rT   r2   has_new_items>  s   zConstDictVariable.has_new_itemsr   rV   c                 C   s<   |r|  r|  rt| t| kS t|t|kS rN   )rG   idrA   )rO   r   rV   r1   r1   r2   r   D  s   zConstDictVariable.is_new_itemcodegenr&   c                 C   sh   d}| j   D ]!\}}| j|j}| ||s| jr(||j || |d7 }q|td|d d S )Nr   r!   	BUILD_MAPr4   )rb   rd   r0   rM   r   re   append_outputr   )rO   r   num_argsrl   r   itemr1   r1   r2   reconstruct_kvs_into_new_dictL  s   
z/ConstDictVariable.reconstruct_kvs_into_new_dictc                    s   | j tju rS  fdd |  rD g tddt   |   	t   
d |    g tdtd d S |    tdd d S |  rx tddd	t g  |  |    	td
dd	 d S |   d S )Nc                            t dgS )Nr~   extend_outputcreate_load_python_moduler}   create_load_attrr1   r   r1   r2   <lambda>\  
    z/ConstDictVariable.reconstruct.<locals>.<lambda>r   Fupdater!   POP_TOPr   r   DICT_UPDATE)rK   r}   r~   add_push_null_contains_self_referencer   r   r   	add_cacher   load_methodr   r   r   rO   r   r1   r   r2   reconstructX  sF   







zConstDictVariable.reconstructr4   c                 C   sr   t |}|| jvr4zdt| d| }W n ty+   tdt| }Y nw tt	||gd | j| S )NzDict key lookup failed for z%. Debug representation of the key is r8   )
rJ   rW   rb   strr   r>   r%   r@   r   r   )rO   r5   r4   rl   error_messager1   r1   r2   'getitem_const_raise_exception_if_absent  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)rJ   rW   rb   r   r   r   
USER_ERROR)rO   r5   r4   rl   msgr1   r1   r2   getitem_const  s   



	zConstDictVariable.getitem_constc                 C   s"   t |}|| jvrd S | j| S rN   )rJ   rW   rb   )rO   r4   rl   r1   r1   r2   maybe_getitem_const  s   


z%ConstDictVariable.maybe_getitem_constc                 C   sX   || v sJ t |}t| j |}t| j | }t|tj	r*|
  d S d S rN   )rJ   rW   tuplerb   keysindexrd   rE   r   rF   rA   )rO   r4   rl   r   original_key_vtr1   r1   r2   realize_key_vt  s   
z ConstDictVariable.realize_key_vtc                 C   s   | j rt| tj d S d S rN   )rt   r   
make_guardr   DICT_KEYS_MATCHrT   r1   r1   r2   r     s   z/ConstDictVariable.install_dict_keys_match_guardr9   c                 C   s   | j sd S |jj| rd S |d | v }|d j d u r8|d  r8t| tjt	| j
|d  | d d S |d j rN|rH| |d  d S |   d S d S )Nr   )rl   invert)rt   outputside_effectsis_modifiedis_python_constantr   r   	functoolspartialr.   CONTAINS_GUARDr   r   r   )rO   r5   r9   containsr1   r1   r2   install_dict_contains_guard  s(   

	z-ConstDictVariable.install_dict_contains_guardnamec                    sN  ddl m}m  ddlm} tj|dkr5||tj	|g|R i |}|j
j|  | j|j tS |dkrSt|dkrKt||dt| d | ||d S |d	kr|s[|rlt||d
t| dt| d |   | jr{|j
j| j t| S |dkrt|rt||dt| d |   | jr|j
j| j t| S |dkr|s|rt||d
t| dt| d |   | jr|j
j| j |s|rtt| t| S |dkr|   |s|rt||d
t| dt| d | j| j t d dS |dkr-|s|r!t||d
t| dt| d |    t| jS |dkr}|  r}|o?t |d }|sJt!|d | |   |sXt|dkrit||dt| dt| d |j
j|  |d | j|d < tS |dkr|  r|ot |d }|r|   d| _"|j
j|  | j#|d  tS t$ %||||S |dkrt|dvrt||dt| d |ot |d }|st!|d | |d | vr| &|| t|dkrtS |d S | '||d S |dkrd|  rdt|dvr t||dt| d |o(t |d }|s3t!|d | |d | vrP| &|| t|dkrLtt(| |d S d| _"|j
j|  | j)|d S |dkr|  rt*| j+trt*| j+t,j-st|rt|| | js d}	tt(||	gd | j+t,j-u rt|dksd|v rt|dkr|d . r|d / }
n|0d }r|. r|/ }
nt|| | jj1|
d \}}n| j1 \}}d| _"|j
j|  t23|j4|gS |d!kr'|s|rt||d
t| dt| d d| _"|j
j|  | j5  tS |d"kr|  r|   t|dk}t|dk}|sG|r|j
j|  |rtt6|d trd|d   |d }n	|7|t|d }| j|j |r fd#d$| D }| j| tS t$ %||||S |d%krt|st||d&t| dt| d |ot |d }|st!|d | | &|| |d | v } |S |d'krU|  rUt|dvrt||d(t| dt| d |ot |d }|st!|d | |   |st|dkr't||d)t| dt| d | 8|d }|d ur5|S t|dkr?t}n|d }|j
j|  || j|d < |S |d*kr|   |j
j|  |d | vrqtt(| d}
t|dkr|d . r|d / }
|rd|v r|d . r|0d/ }
|d }| jj9||
d  tS |d+krt:| trt|dkrt||dt| d ||t;j<=|| |d gi S |d,kr | %|d+||j> S |d-krht|dkrt||dt| d |d }t:|tt2j?t2j@frO| j+tur | j+}| }nt6|ts(J |j+}|}|  |j| j t d |d.}|d   |j|d j |S d/| A jB d0|A jB d1}tt||gd d S |d2krw| %|d"|| | S |d3krdd4lCmD} | jrtE| js|j
j| j || F|t d5S t$ %||||S )6Nr!   )r)   r%   r6   rP   __getitem__1 args argsr   rb   0 args and 0 kwargs
 args and  kwargsr   z0 argsr   ru   )rb   r   rt   __len____setitem__r   z2 args and 0 kwargs__delitem__Tr0   )r!   r   z1 or 2 argsrp   popitemzpopitem(): dictionary is emptyr8   last)r   clearr   c                    s    i | ]\}}  ||qS r1   )r@   r   r%   rj   r1   r2   rn     r   z1ConstDictVariable.call_method.<locals>.<dictcomp>r   zmore than 1 args and 0 kwargs
setdefaultz1 or 2 args and 0 kwargszat most 2 args and 0 kwargsmove_to_endrZ   __ne____or__)rb   r   rt   rK   z$unsupported operand type(s) for |: 'z'and ''__ior____iter__ListIteratorVariabler   )G r)   r%   r:   r7   rJ   rW   r@   r|   	call_dictr   r   mutationrb   r   r$   r   r   r   r   rt   guard_on_key_orderaddDictItemsVariableDictKeysVariabler   r?   DictValuesVariabler   ru   r"   
is_mutablerH   rC   re   r   rq   call_methodr   r   r   rp   
issubclassrK   r}   r~   r   r   r0   r   r   TupleVariablerM   r   rE   call_custom_dictr   r  r   r   dict___eq__call_functionr   UserDefinedDictVariableDefaultDictVariabler=   r[   listsr  r   unpack_var_sequence)rO   r5   r   r9   rc   r)   r7   temp_dict_vtarg_hashabler   r   rh   r   has_arg
has_kwargsdict_vtkwargs_hashabler   r   rD   rl   rV   rK   to_cpynew_dict_vterr_msgr  rw   r   r2   r    s>  




















	

zConstDictVariable.call_methodc                 C   s   |    dd | jD S )Nc                 S      g | ]}|j qS r1   r   r   r1   r1   r2   
<listcomp>T      z9ConstDictVariable.unpack_var_sequence.<locals>.<listcomp>r   rO   r5   r1   r1   r2   r  R  s   z%ConstDictVariable.unpack_var_sequencec                    s|   t  fddttjtjfD r%t j|rtdS  jtu r%tdS d j d}t	dd j |d	gt
jd
 d S )Nc                 3   s    | ]} j |u V  qd S rN   r   )rg   trT   r1   r2   rk   [  s
    
z5ConstDictVariable.call_obj_hasattr.<locals>.<genexpr>TFzhasattr on z is not supportedzunsupported hasattr operationzClass z+Consider using a regular dictionary insteadr   )r   r|   r}   r~   r   r   rK   r%   r@   r   r   SUPPORTABLE)rO   r5   r   r   r1   rT   r2   call_obj_hasattrV  s"   




z"ConstDictVariable.call_obj_hasattrc                    s   |    t jdi |S Nr1   )r   rq   r   rO   rc   rw   r1   r2   r   o  s   zConstDictVariable.clonec                 C      dS )zP
        Dictionaries are mutable and therefore not hashable in Python.
        Fr1   rT   r1   r1   r2   rI   s     z$ConstDictVariable.is_python_hashabler   r&   r,   Nr,   N)-r[   r\   r]   r   DICT_CONTAINSr   r#   _nonvar_fieldsrW   r|   r.   r   rP   rs   r   r   r   r   r   r=   ra   r   r   r   r_   r   r   r   r   r   r   r   r   r   r   r   listr   r  r  r%   r,  r   rI   __classcell__r1   r1   rw   r2   rJ   n   s    9
'	

2


/


	
(
  p
rJ   c                
       s   e Zd Zdededdf fddZdefddZd	d
dee	 fddZ
dddZd	d
dedee	 deee	f de	f
ddZd	d
dedef fddZ  ZS )MappingProxyVariabledv_dictrc   r,   Nc                    s*   t  jdi | t|tsJ || _d S r-  )rq   rP   rE   rJ   r8  rO   r8  rc   rw   r1   r2   rP   |  s   
zMappingProxyVariable.__init__c                 C   s   t jS rN   )typesMappingProxyTyperT   r1   r1   r2   r=     r   z MappingProxyVariable.python_typer5   r(   c                 C   s   | j |S rN   )r8  r  r)  r1   r1   r2   r       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   r1   r   r1   r2   r     r   z2MappingProxyVariable.reconstruct.<locals>.<lambda>r!   F)rt   r   r   r+  r   r8  r   r   )rO   r   r   r1   r   r2   r     s"   
	

z MappingProxyVariable.reconstructr   r9   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   )rt   r   r   has_existing_dict_mutationr   r8  r  )rO   r5   r   r9   rc   r   r1   r1   r2   r    s   
	z MappingProxyVariable.call_methodc                    s.   |   tju rt|tjjv S t ||S rN   )r=   r:  r;  r%   r@   r/   rq   r,  rO   r5   r   rw   r1   r2   r,    s   z%MappingProxyVariable.call_obj_hasattrr1  )r[   r\   r]   rJ   r   rP   r.   r=   r5  r#   r  r   r   r|   r  r%   r,  r6  r1   r1   rw   r2   r7  z  s.    


r7  c                   @   s0   e Zd Zd
ddZdddee ddfdd	ZdS )NNModuleHooksDictVariabler,   Nc                 C      d S rN   r1   rT   r1   r1   r2   r        z7NNModuleHooksDictVariable.install_dict_keys_match_guardr5   r(   r9   c                 C   rA  rN   r1   rO   r5   r9   r1   r1   r2   r        z5NNModuleHooksDictVariable.install_dict_contains_guardr2  )r[   r\   r]   r   r5  r#   r   r1   r1   r1   r2   r@    s    
r@  c                       s   e Zd Z	ddeeef dedee deddf
 fddZde	f fd	d
Z
def fddZede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dddedef fddZdddZ  ZS )r  Nrb   rK   default_factoryrc   r,   c                    s:   t  j||fi | |tju sJ |d u rt}|| _d S rN   )rq   rP   r}   r   r$   rE  )rO   rb   rK   rE  rc   rw   r1   r2   rP     s
   
zDefaultDictVariable.__init__c                    s$   | j tttfvr| jsdS t  S )NF)rE  r5  r   r|   rb   rq   r   rT   rw   r1   r2   r     s   
z&DefaultDictVariable.is_python_constantc                    s,   | j d usJ d| j   dt   dS )Nzdefaultdict(r   ))rE  r   rq   rT   rw   r1   r2   r     s   zDefaultDictVariable.debug_reprr4   c                 C   s0   t | tjtjjtjjfpt | tjo| jd u S rN   )rE   r   r)   	functionsBaseUserFunctionVariablePolyfilledFunctionVariabler%   r   r   r1   r1   r2   is_supported_arg  s   z$DefaultDictVariable.is_supported_argr5   r(   r   r9   c                    s  |dkrSt |dkrt||dt | d |d | v r$| ||d S t| jtr<| jjd u r<tt||d gd d S | j	|g i }t
 |d|d |g| |S |dkr| jrt |d	krlt||d
t | d t|d tr|d jdkr| |d r|jj|  |d | _tS t
 ||||S |dkrt |dkrt||dt | d ttj	|| |d gi S t
 ||||S )Nr   r!   r   r   r   r8   r   __setattr__r   z2 argsrE  rZ   )r   r   r   r   rE  r%   r   r   r   r  rq   r  r  rJ  r   r   r  r$   r   r*   r   r  )rO   r5   r   r9   rc   default_varrw   r1   r2   r    sB   

zDefaultDictVariable.call_methodc                    s   |dkr| j S t ||S )NrE  )rE  rq   var_getattrr?  rw   r1   r2   rM  &  s   zDefaultDictVariable.var_getattrr   r&   c                    s~      fdd  | j  g tddt   |   t   d |    g t	dt
d d S )Nc                      r   )Nr   r   r1   r   r1   r2   r   2  r   z1DefaultDictVariable.reconstruct.<locals>.<lambda>r!   Fr   r   )r   rE  r   r   r   r   r   r   r   r   r   r   r1   r   r2   r   /  s,   




zDefaultDictVariable.reconstructrN   r1  )r[   r\   r]   r|   r#   r.   r   r   rP   ra   r   r   r   staticmethodrJ  r5  r  rM  r   r6  r1   r1   rw   r2   r    sH    


.	r  c                
       s$  e Zd ZdZejZdee de	ddf fddZ
defdd	Zeded
 fddZedefddZde	fddZdefddZde	fddZd*ddZddde	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d+d#d$Zddd%edefd&d'Zd,d(d)Z  ZS )-SetVariablez.We model a sets as dictionary with None valuesrb   rc   r,   Nc                    s\   g }|D ]}t |tjr|| q||  qt|t }t	 j
|fi | d S rN   )rE   rJ   rW   r   rA   r|   fromkeysrO  _default_valuerq   rP   )rO   rb   rc   realized_itemsr   rw   r1   r2   rP   U  s   zSetVariable.__init__c                 C   sh   | j sdS g }| j D ] }t|tjr|jn|}t|dr!t|jn| }|	| q
dd
| d S )Nzset()r   r   ,r   )rb   rE   rJ   rW   rM   r   r   r   r   r   r   )rO   rb   rh   rM   r   r1   r1   r2   r   k  s   
zSetVariable.debug_reprrL   c                 C   s   t | j S rN   )setrb   r   rT   r1   r1   r2   	set_itemsv  s   zSetVariable.set_itemsc                   C      t S rN   )r$   r1   r1   r1   r2   rQ  z  rD  zSetVariable._default_valuec                 C      dd | j D S )Nc                 S      h | ]}|j  qS r1   r   rg   r   r1   r1   r2   	<setcomp>      z'SetVariable.as_proxy.<locals>.<setcomp>rU  rT   r1   r1   r2   r        zSetVariable.as_proxyc                 C   rV  rN   )rT  rT   r1   r1   r2   r=     rB  zSetVariable.python_typec                 C   rW  )Nc                 S   rX  r1   r   rY  r1   r1   r2   rZ    r[  z1SetVariable.as_python_constant.<locals>.<setcomp>r\  rT   r1   r1   r2   r     r]  zSetVariable.as_python_constantr   r&   c                 C   s2   | dd | jD  |tdt| jd d S )Nc                 S   r&  r1   r   r   r1   r1   r2   r'    r(  z+SetVariable.reconstruct.<locals>.<listcomp>	BUILD_SETr   )foreachrU  r   r   r   r   r1   r1   r2   r     s   zSetVariable.reconstructr5   r(   fnr9   c              
   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 r1   r   r   r1   r1   r2   r'    s    z0SetVariable._fast_set_method.<locals>.<listcomp>c                 S   s   i | ]	\}}||  qS r1   ra  r   r1   r1   r2   rn     ro   z0SetVariable._fast_set_method.<locals>.<dictcomp>r8   )rb   r>   r   r.   r5  mapr%   r@   r9   r#   build)rO   r5   r`  r9   rc   resexcr1   r1   r2   _fast_set_method  s   
zSetVariable._fast_set_methodr   c              
      s  ddl m} ddlm} |dv r*|||r*|  tu r*|  }| |t||||S |dkrS||tj	|g|R i |}|j
j|  | j  | j|j tS |dkr||s_t|dkrpt||dt| d	t| d
 d}|d t g}nH|dkr|s|rt||dt| d	t| d
 z| j j}	W n  ty }
 ztt|tttj|
jd W Y d }
~
nd }
~
ww t |||	g| |	S |dkr|st|dkrt||dt| d	t| d
 ||t j!"|| |d gi S |dkr|rt||dt| d
 ||t j#"|| g|d| $ iS |dkrA|r2t||dt| d
 ||t j%"|| g|i S |dkrh|rUt||dt| d
 ||t j&"|| g|d| $ iS |dkr|r|t||dt| d
 ||t j'"|| g|d| $ iS |dkr|rt||dt| d
 ||t j("|| g|i S |dkr|st|dkrt||dt| d	t| d
 ||t j)"|| g|d| $ iS |dkr|st|dkrt||dt| d	t| d
 ||t j*"|| g|i S |dkr<| + r<|r-t||dt| d
 ||t j,"|| g|i S |dkrs|sKt|dkr\t||dt| d	t| d
 |d | vrjtt||d t |d||S |dkr|st|dkrt||dt| d	t| d
 |d | v rt |d||S tS |dv rt|dkrt||dt| d  t-j.t-j/d}|d 0 }t1|ts||t"||gi }|||2|"|| |gi S |d!v r2ddddd!2|}t3|d tt4j5fs#td"| d#| 6  d$|d 6  d%}tt7||gd |d us*J | ||||S |d&v rzt3|d tt4j5fs_td"| d#| 6  d$|d 6  d%}tt7||gd ddddd&2|}|d uspJ | |||| | S |d'krt3|d tt4j5fstd(S | |d||}tt|jdkS |t8v rt3|d tt4j5fstt9S tt8| | j|d jS t ||||S ))Nr   )check_constant_argsr!   r6   )
isdisjointunionintersection
differencesymmetric_differencerP   r  z1 args and 0 kwargsr   r   r   r   rp   r   r8   rh  rj  z0 kwargsclsintersection_updateri  rk  zExpect: 0 kwargs, Actual: difference_updaterl  symmetric_difference_updater   removediscard)issubset
issupersetr   r   )__and__r  __xor____sub__z unsupported operand type(s) for z: 'z' and 'r  )__iand__r  __ixor____isub__rZ   F):utilsrg  r:   r7   r=   rT  rf  getattrr@   call_setr   r   r  rb   r   r   r$   r   r   rO  rQ  rU  rp   rM   r   r   r5  rb  r%   r9   rq   r  r   set_isdisjointr  set_intersectionpython_type_varset_intersection_update	set_unionset_differenceset_difference_updateset_symmetric_differenceset_symmetric_difference_updater  
set_updateoperatorlegerA   r   r0   rE   r   UserDefinedSetVariablepython_type_namer?   r   NotImplemented)rO   r5   r   r9   rc   rg  r7   py_typetemp_set_vtresulteoprV   mr   rrw   r1   r2   r    s  	
















"
"



zSetVariable.call_methodr)   c                 C   
   t tS rN   )r   r)   rT  rT   r1   r1   r2   r       
zSetVariable.python_type_varr4   c                 C   s   t d)NzIllegal to getitem on a set)RuntimeError)rO   r5   r4   r1   r1   r2   r     s   zSetVariable.getitem_constc                 C   rA  rN   r1   rT   r1   r1   r2   r        z)SetVariable.install_dict_keys_match_guardr1  r,   r)   r2  )r[   r\   r]   r^   r   SET_CONTAINSr   r   r#   r   rP   r   r   propertyrT  rU  rN  rQ  r   r.   r=   r   r   r5  r|   rf  r  r  r   r   r6  r1   r1   rw   r2   rO  P  sf    



 
b
rO  c                
       s   e Zd Zdeddf fddZdeee  fddZdd	d
ede	f fddZ
dd	d
edee	 deee	f de	f
 fddZdd	dee	 deee	f ddfddZ  ZS )OrderedSetClassVariablerc   r,   Nc                    s   t  jdi | d S r-  )rq   rP   r.  rw   r1   r2   rP     s   z OrderedSetClassVariable.__init__c                 C   rV  rN   r	   rT   r1   r1   r2   r     rB  z*OrderedSetClassVariable.as_python_constantr5   r(   r   c                    sH   |dkrddl m} | jrt| j|}nd }|| ||dS t ||S )N__new__r!   )GetAttrVariable)rt   )miscr  rt   r   rq   rM  )rO   r5   r   r  attr_sourcerw   r1   r2   rM    s   z#OrderedSetClassVariable.var_getattrr9   c                    s   ddl m} |dkr+t|dks|r#t||dt| dt| d tjg t dS tt|}||v rJt	|d	 tj
rJ|d	 |||dd  |S t ||||S )
Nr!   )set_methodsr  r   z'OrderedSet.__new__ only accepts one argr   r   r	  r   )builtinr  r   r   r   OrderedSetVariabler"   r|  rT  rE   rO  r  rq   )rO   r5   r   r9   rc   r  resolved_fnrw   r1   r2   r    s"   
z#OrderedSetClassVariable.call_methodr  c                 C   sb   t |dks|rt|ddt | dt | d t |dkr"g }n|d |}tj|t dS )Nr!   r
   zOrderedSet only accepts one argr   r   r   r	  )r   r   force_unpack_var_sequencer   r  r"   )rO   r5   r9   rc   rb   r1   r1   r2   r    s   z%OrderedSetClassVariable.call_function)r[   r\   r]   r   rP   r.   r
   r   r   r#   rM  r5  r|   r  r   r  r6  r1   r1   rw   r2   r    s0    

r  c                   @   sZ   e Zd ZdefddZdee fddZdeee  fddZ	de
fdd	ZdddZdS )r  r,   c                 C   s\   | j sdS g }| j D ]\}}t|jdrt|jjn|j }|| q
dd| d S )NzOrderedSet([])r   zOrderedSet([rS  ])r   )rO   rb   r   rh   r   r1   r1   r2   r     s    zOrderedSetVariable.debug_reprc                 C      t dd | jD S )Nc                 S   s   g | ]}|j  qS r1   r   rY  r1   r1   r2   r'    r[  z9OrderedSetVariable.as_python_constant.<locals>.<listcomp>)r
   rU  rT   r1   r1   r2   r     r   z%OrderedSetVariable.as_python_constantc                 C   rV  rN   r	   rT   r1   r1   r2   r=     rB  zOrderedSetVariable.python_typec                 C   s   t  S rN   )r  rT   r1   r1   r2   r    r   z"OrderedSetVariable.python_type_varr   r&   Nc                    sT      fdd  dd | jD   tdt| jd  tdd d S )	Nc                      s     ddS )Nztorch.utils._ordered_setr
   )load_import_fromr1   r   r1   r2   r     s    z0OrderedSetVariable.reconstruct.<locals>.<lambda>c                 S   r&  r1   r   r   r1   r1   r2   r'    r(  z2OrderedSetVariable.reconstruct.<locals>.<listcomp>
BUILD_LISTr   r!   F)r   r_  rU  r   r   r   r   r   r   r1   r   r2   r     s   
zOrderedSetVariable.reconstructr1  )r[   r\   r]   r   r   r
   r   r   r.   r=   r  r  r   r1   r1   r1   r2   r    s    r  c                
       s   e Zd ZdefddZeded fddZdefddZ	d"d
dZ
defddZd#ddZdddedee deeef def
 fddZded fddZdefddZdedefd d!Z  ZS )$FrozensetVariabler,   c                 C   X   | j sdS g }| j D ]}t|jdrt|jjn|j }|| q
dd| d S )Nzfrozenset()r   r   rS  r   r   rO   rb   r   r   r1   r1   r2   r        
 zFrozensetVariable.debug_reprrL   c                 C   s
   | j  S rN   )rb   r   rT   r1   r1   r2   rU       
zFrozensetVariable.set_itemsc                 C   rV  rN   )	frozensetrT   r1   r1   r2   r=     rB  zFrozensetVariable.python_typer)   c                 C   r  rN   )r   r)   r  rT   r1   r1   r2   r    r  z!FrozensetVariable.python_type_varc                 C   r  )Nc                 S   rX  r1   r   rY  r1   r1   r2   rZ    r[  z7FrozensetVariable.as_python_constant.<locals>.<setcomp>)r  rU  rT   r1   r1   r2   r     r   z$FrozensetVariable.as_python_constantr   r&   Nc                    sP      fdd  dd | jD   tdt| jdgtdd d S )	Nc                      s      dgS )Nr  )r   create_load_globalr1   r   r1   r2   r     s    z/FrozensetVariable.reconstruct.<locals>.<lambda>c                 S   r&  r1   r   r   r1   r1   r2   r'    r(  z1FrozensetVariable.reconstruct.<locals>.<listcomp>r  r   r!   F)r   r_  rU  r   r   r   r   r   r1   r   r2   r   
  s   
zFrozensetVariable.reconstructr5   r(   r   r9   rc   c                    sZ   |dv rt d| d|dkrtS |dv r$t ||||}t|jS t ||||S )Nr  rp   r   rq  rr  r   Illegal call_method z on a frozensetrP   )ru   rk  rj  rl  )r  r$   rq   r  r  rb   )rO   r5   r   r9   rc   r  rw   r1   r2   r    s   
zFrozensetVariable.call_methodTc                 C   r/  )zB
        Frozensets are immutable and hashable in Python.
        Tr1   rT   r1   r1   r2   rI   6  r0  z$FrozensetVariable.is_python_hashablec                 C   s   t |  S rN   )rQ   r   rT   r1   r1   r2   rS   <  r<  z!FrozensetVariable.get_python_hashrV   c                 C   s   t |to|  | kS rN   )rE   r#   r   rY   r1   r1   r2   rX   ?  s   
z!FrozensetVariable.is_python_equalr  r1  )r[   r\   r]   r   r   r  rT  rU  r.   r=   r  r   r   r   r5  r#   r|   r  r   rI   r_   rS   r`   ra   rX   r6  r1   r1   rw   r2   r    s,    


r  c                
       s   e Zd ZdefddZdddZddd	ee ddfd
dZe	de
fddZdefddZ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 )DictKeySetVariabler,   c                 C   r  Nzdict_keys([])r   zdict_keys([rS  r  r   r  r1   r1   r2   r   G  r  zDictKeySetVariable.debug_reprNc                 C   rA  rN   r1   rT   r1   r1   r2   r   S  r  z0DictKeySetVariable.install_dict_keys_match_guardr5   r(   r9   c                 C   rA  rN   r1   rC  r1   r1   r2   r   W  r0  z.DictKeySetVariable.install_dict_contains_guardc                 C   r   rN   r   rT   r1   r1   r2   rU  ]  s   zDictKeySetVariable.set_itemsc                 C   rV  rN   r   rT   r1   r1   r2   r=   a  rB  zDictKeySetVariable.python_typec                 C   s   t dd | jD d  S )Nc                 S   rX  r1   r   rY  r1   r1   r2   rZ  f  r[  z8DictKeySetVariable.as_python_constant.<locals>.<setcomp>)r|   rP  rU  r   rT   r1   r1   r2   r   d  s
   z%DictKeySetVariable.as_python_constantr   rc   c                    s*   |dv rt d| dt ||||S )Nr  r  z on a dict_keys)r  rq   r  )rO   r5   r   r9   rc   rw   r1   r2   r  i  s   zDictKeySetVariable.call_methodr2  )r[   r\   r]   r   r   r   r5  r#   r   r  r   rU  r.   r=   r   r|   r  r6  r1   r1   rw   r2   r  F  s2    


r  c                
       s   e Zd ZU dZdZee ed< dede	ddf fddZ
ede	fd	d
Zedee fddZddde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e deeef def
 fddZ  ZS )DictViewVariablezu
    Models _PyDictViewObject

    This is an "abstract" class. Subclasses will override kv and the items method
    Nkvr8  rc   r,   c                    s8   t  jdi | | jdv sJ t|tsJ || _d S )N)r   r   rb   r1   )rq   rP   r  rE   rJ   r8  r9  rw   r1   r2   rP   ~  s   
zDictViewVariable.__init__c                 C   s    | j d usJ t| jj| j  S rN   )r  r|  r8  rb   rT   r1   r1   r2   
view_items  s   zDictViewVariable.view_itemsc                 C   s   t rN   )NotImplementedErrorrT   r1   r1   r2   view_items_vt  r0  zDictViewVariable.view_items_vtr5   r(   c                 C   r   rN   )r  r)  r1   r1   r2   r    r   z$DictViewVariable.unpack_var_sequencer   r&   c                 C   s2   | j d usJ || j || j  |d d S )Nr   )r  r8  r   r  r   r1   r1   r2   r     s   
zDictViewVariable.reconstructr   c                 C   s0   | j d usJ ||  jv rtdS tdS )NTF)r  r=   r/   r%   r@   r?  r1   r1   r2   r,    s   

z!DictViewVariable.call_obj_hasattrr9   c                    sf   |dkr| j ||||S |dkrddlm} || jt dS |dkr*t|  S t	 ||||S )Nr   r  r!   r  r	  __repr__)
r8  r  r  r  r  r"   r%   r@   r   rq   rO   r5   r   r9   rc   r  rw   r1   r2   r    s   zDictViewVariable.call_methodr1  )r[   r\   r]   r^   r  r   r   __annotations__rJ   r   rP   r  r  r5  r#   r  r  r   r%   r,  r|   r  r6  r1   r1   rw   r2   r  u  s8   
 


r  c                
       s   e Zd ZdZedee fddZedee fddZ	de
fddZ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 )r  r   r,   c                 C   
   t | jS rN   )rT  r  rT   r1   r1   r2   rU    r  zDictKeysVariable.set_itemsc                 C   rW  )Nc                 S   r&  r1   r   r   r1   r1   r2   r'    r(  z2DictKeysVariable.view_items_vt.<locals>.<listcomp>r  rT   r1   r1   r2   r       zDictKeysVariable.view_items_vtc                 C   rV  rN   r  rT   r1   r1   r2   r=     rB  zDictKeysVariable.python_typec                 C   r  r  r  r   rM   r   r   r   r   r   r  r1   r1   r2   r     r  zDictKeysVariable.debug_reprr5   r(   r   r9   rc   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   )ru  rx  r  r  rw  rz  rv  ry  r   )r8  r  r|  rU  rO  r   rE   r  r%   r@   r  rq   )rO   r5   r   r9   rc   r  r  rw   r1   r2   r    s   
zDictKeysVariable.call_method)r[   r\   r]   r  r  rT  r#   rU  r5  r  r.   r=   r   r   r|   r  r6  r1   r1   rw   r2   r    s&    
r  c                   @   sB   e Zd ZdZedee fddZdefddZ	de
fddZd	S )
r  r   r,   c                 C   r  rN   )r5  r  rT   r1   r1   r2   r    r  z DictValuesVariable.view_items_vtc                 C   rV  rN   )r   rT   r1   r1   r2   r=     rB  zDictValuesVariable.python_typec                 C   sR   | j sdS g }| j D ]}t|drt|jn| }|| q
dd| d S )Nzdict_values([])r   zdict_values([rS  r  )r  r   r   r   r   r   r   )rO   rb   rh   r   r1   r1   r2   r     s   
zDictValuesVariable.debug_reprN)r[   r\   r]   r  r  r5  r#   r  r.   r=   r   r   r1   r1   r1   r2   r    s    r  c                
       s   e Zd ZdZedee fddZdefddZ	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ded fddZ  ZS )r  rb   r,   c                 C   rW  )Nc                 S   s    g | ]\}}t |j|gqS r1   )r   r  rM   r   r1   r1   r2   r'    r   z3DictItemsVariable.view_items_vt.<locals>.<listcomp>r  rT   r1   r1   r2   r    r  zDictItemsVariable.view_items_vtc                 C   rV  rN   )r   rT   r1   r1   r2   r=     rB  zDictItemsVariable.python_typec                 C   s   | j sdS g }| j D ]/\}}t|jdrt|jjn|j }t|dr)t|jn| }|d| d| d q
dd| d S )	Nzdict_items([])r   (r   rF  zdict_items([rS  r  r  r   r1   r1   r2   r     s    zDictItemsVariable.debug_reprr5   r(   r   r9   rc   c                    s   |dkr/t |dkrt||dt | d t|d tr*| j|d|d jgi S tdS |dkrAddlm	} || j
t d	S t ||||S )
NrZ   r!   r   r   r   Fr  r  r	  )r   r   rE   r  r8  r  r%   r@   r  r  r  r"   rq   r  rw   r1   r2   r    s   	
zDictItemsVariable.call_methodFc                 C   r/  )zC
        Dictionary item views are not hashable in Python.
        Fr1   rT   r1   r1   r2   rI   4  r0  z$DictItemsVariable.is_python_hashable)r[   r\   r]   r  r  r5  r#   r  r.   r=   r   r   r|   r  r   rI   r6  r1   r1   rw   r2   r    s$    
r  rN   )Jr^   r}   r   r  r:  collections.abcr   r   typingr   r   r   r   r   torch.utils._ordered_setr
   r
  r   r   r   bytecode_transformationr   r   r   r   re  r   r   guardsr   r   rt   r   r   r   r{  r   r   r   r   r   r   r    ry   r"   r#   constantr$   r%   torch._dynamo.codegenr&   r;   r(   torch._dynamo.variables.builtinr)   rG  r*   ra   r3   rC   rH   rJ   r7  r@  r  rO  r  r  r  r  r  r  r  r  r1   r1   r1   r2   <module>   sh    $	
      M~  ?C V/?;