o
    "i                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZ d dl	m
Z
 ddlmZmZ ddlmZmZmZmZ ddlmZ ddl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 m!Z! ddl"m#Z#m$Z$ ddl%m&Z& 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+Z,G dd de+Z-de.fddZ/de.fd d!Z0d"e1dd#fd$d%Z2G d&d' d'e(Z3G d(d) d)e(Z4e5dd*d+ Z6G d,d- d-e$Z7G d.d/ d/e$Z8dS )0    N)DictListOptional)is_fake   )polyfill	variables)create_call_functioncreate_call_methodcreate_instructioncreate_load_method)	skip_code)unimplemented)GuardBuilderinstall_guard)
AttrSourceGetItemSource)	dict_keysdict_valuesistypespecialize_symnode   )MutableLocalVariableTrackerConstantVariablec                 C   s   t | tjr|  jjdd uS t | tjr!tdd | j	D S t | tj
tjtjtjtjjtjtjtjjtjtjtjtjtjtjfS )Nexample_valuec                 s   s    | ]}t |V  qd S N)is_hashable.0e r"   [/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/_dynamo/variables/dicts.py	<genexpr>'   s    zis_hashable.<locals>.<genexpr>)
isinstancer   TensorVariableas_proxynodemetagetTupleVariableallitemsBuiltinVariableSymNodeVariabler   EnumVariableuser_definedUserDefinedClassVariableUserFunctionVariableSkipFunctionVariablemiscNumpyVariableNNModuleVariableUnspecializedNNModuleVariableMethodWrapperVariableTorchInGraphFunctionVariableTypingVariableFunctoolsPartialVariablexr"   r"   r#   r       s,   r   c                       s   e Zd ZdhejZG dd dZefdeeef f fddZ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" fddZd d! Z  ZS )#ConstDictVariableuser_clsc                   @   sJ   e Zd ZdZ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
        c                 C   s2   t |}t|stdt| d|  || _d S )NzDict key of type z. Key: )r   r   r   typevt)selfrB   r"   r"   r#   __init__K   s   
z+ConstDictVariable._HashableTracker.__init__c                    s   t | jtjr| j jjd }|S t | jtjr+tj	 t
 fdd| jjD }|S t | jtjr6| jjS t | jtjrA| jjS t | jtjrM| j S | j }|S )Nr   c                 3   s    | ]} |j V  qd S r   )underlying_valuer   Hashabler"   r#   r$   Z       zFConstDictVariable._HashableTracker.underlying_value.<locals>.<genexpr>)r%   rB   r   r&   r'   r(   r)   r+   r?   _HashableTrackertupler-   r7   moduler8   valuer3   get_functionas_python_constant)rC   r>   r"   rF   r#   rE   T   s   	

z3ConstDictVariable._HashableTracker.underlying_valuec                 C   
   t | jS r   )hashrE   rC   r"   r"   r#   __hash__e   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 r   )_eq_impl)r    uvrF   r"   r#   r$   o   s    
z>ConstDictVariable._HashableTracker._eq_impl.<locals>.<genexpr>)	rA   r%   rJ   r?   rI   lenr,   zipr   )abr"   rF   r#   rS   h   s   
z+ConstDictVariable._HashableTracker._eq_implotherreturnc                 C   sN   t j}t||st|sJ t|t||r || j|jS || j|S r   )r?   rI   r%   r   
is_literalrA   rS   rE   )rC   rZ   rG   r"   r"   r#   __eq__w   s   
z)ConstDictVariable._HashableTracker.__eq__N)__name__
__module____qualname____doc__rD   propertyrE   rR   staticmethodrS   boolr]   r"   r"   r"   r#   rI   D   s    	

rI   r-   c                    sd   t  jdi | tj t fdd| D sJ  fddfdd| D | _|| _d S )Nc                 3   s,    | ]\}}t |t fot |tV  qd S r   )r%   r   r    r>   rU   rF   r"   r#   r$      s    
z-ConstDictVariable.__init__.<locals>.<genexpr>c                    s   t |  r| S  | S r   )r%   )keyrF   r"   r#   make_hashable   s   z1ConstDictVariable.__init__.<locals>.make_hashablec                    s   i | ]	\}} ||qS r"   r"   re   )rg   r"   r#   
<dictcomp>   s    z.ConstDictVariable.__init__.<locals>.<dictcomp>r"   )superrD   r?   rI   r,   r-   r@   )rC   r-   r@   kwargs	__class__)rG   rg   r#   rD      s   
zConstDictVariable.__init__c                 C      dd | j   D S )Nc                 S       i | ]\}}|j  | qS r"   rB   r'   r    krU   r"   r"   r#   rh      s     z.ConstDictVariable.as_proxy.<locals>.<dictcomp>r-   rQ   r"   r"   r#   r'         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rB   
debug_reprrp   r"   r"   r#   r$      s    
z/ConstDictVariable.debug_repr.<locals>.<genexpr>})joinr-   rQ   r"   r"   r#   rw      s   
zConstDictVariable.debug_reprc                 C   rm   )Nc                 S   rn   r"   rB   rN   rp   r"   r"   r#   rh      s    z8ConstDictVariable.as_python_constant.<locals>.<dictcomp>rr   rQ   r"   r"   r#   rN      s   z$ConstDictVariable.as_python_constantc                 C   rm   )Nc                 S   s   i | ]
\}}|j  |qS r"   rz   rp   r"   r"   r#   rh          z=ConstDictVariable.keys_as_python_constant.<locals>.<dictcomp>rr   rQ   r"   r"   r#   keys_as_python_constant   rs   z)ConstDictVariable.keys_as_python_constantc                 C   s   | j S r   )r@   rQ   r"   r"   r#   python_type   s   zConstDictVariable.python_typec                 C   s*   t |tsJ tj}t|o||| jv S r   )r%   r   r?   rI   r   r-   )rC   rB   rG   r"   r"   r#   __contains__   s   zConstDictVariable.__contains__c                 C   s   | j tju r||td|dg | j D ]\}}||j || q| j tju rA|tdt	| jdgt
dd d S |tdt	| jd d S )NTOrderedDict	BUILD_MAPargr   F)r@   collectionsr   extend_outputcreate_load_python_modulecreate_load_attrr-   rB   r   rV   r	   append_output)rC   codegenrf   rL   r"   r"   r#   reconstruct   s"   


zConstDictVariable.reconstructr   c                 C   s.   t |}|| jvrtd|j  | j| S )Nzdict KeyError: )r?   rI   r-   r   rL   )rC   r   rf   r"   r"   r#   getitem_const   s   


zConstDictVariable.getitem_constargsList[VariableTracker]rj   Dict[str, VariableTracker]r[   r   c           
         s  ddl m}m m}m}m tj|ot|d }|dkr,t	|dks%J | 
|d S |dkrQ|s4|r6J | jrC|jj| j  fdd| j D S |dkrl| jrb|jj| j  |sf|rhJ t| S |d	kr| jr}|jj| j  |s|rJ t| S |d
kr|s|rJ | j| j t dS |dkr|s|rJ  t	| jS |dkr|r| jr|st	|dksJ |jj|  |d | j|d <  d S |dkr|r| jr|jj|  | j|d   d S |dv rt	|dv r|d | vrt	|dkr d S |d S |dkr6|r6| jr6|jj|  | j|d S |dkrL|jj|  | j   d S |dkrt	|dkrt|d t||fr| jr|jj|  t|d tr||d }	n	||t|d }	| j |	j  fdd| D }| j |  d S |dv r|d | v r| 
|d S |dkrt	|dkrˈ |d | v S t! "||||S )Nr   )r.   r   ListIteratorVariableListVariabler+   r   __getitem__r-   c                    s   g | ]\}} |j |gqS r"   rB   rp   )r+   r"   r#   
<listcomp>   s    z1ConstDictVariable.call_method.<locals>.<listcomp>keysvaluescopy)r-   mutable_local__len____setitem__r   __delitem__)popr*   )r   r   r   clearupdatec                    s    i | ]\}}  ||qS r"   )createrp   )r   rG   r"   r#   rh   *      z1ConstDictVariable.call_method.<locals>.<dictcomp>)r*   __getattr__r~   )# r.   r   r   r   r+   r?   rI   r   rV   r   sourceoutputguard_on_key_orderaddnamer-   DictKeys
DictValuescloner   r   r   r   side_effectsmutationr   r   r   r%   call_custom_dictdictr   ri   call_method)
rC   txr   r   rj   r.   r   r   arg_hashabledict_vtrk   )r   rG   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#   r   7      z9ConstDictVariable.unpack_var_sequence.<locals>.<listcomp>)r-   r   rC   r   r"   r"   r#   unpack_var_sequence6  rs   z%ConstDictVariable.unpack_var_sequencer   r   rj   r   r[   r   )r^   r_   r`   r   _nonvar_fieldsrI   r   r   rD   r'   rw   rN   r|   r}   r~   r   r   r   r   __classcell__r"   r"   rk   r#   r?   >   s4    ?
	dr?   c                       s\   e Zd Zd fdd	Z fddZ fddZedd	 Z	
					d fddZ  Z	S )DefaultDictVariableNc                    s.   t  j||fi | |tju sJ || _d S r   )ri   rD   r   defaultdictdefault_factory)rC   r-   r@   r   rj   rk   r"   r#   rD   ;  s   
zDefaultDictVariable.__init__c                    s$   | j tttfvr| jsdS t  S )NF)r   listrJ   r   r-   ri   is_python_constantrQ   rk   r"   r#   r   @  s   
z&DefaultDictVariable.is_python_constantc                    s   d| j   dt   dS )Nzdefaultdict(ru   ))r   rw   ri   rQ   rk   r"   r#   rw   G  s   zDefaultDictVariable.debug_reprc                 C   s*   t | tjr| jtttfv S t | tjjS r   )	r%   r   r.   fnr   rJ   r   	functionsBaseUserFunctionVariabler   r"   r"   r#   is_supported_argL  s   z$DefaultDictVariable.is_supported_argr   r   rj   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   )rV   r   r   KeyErrorcall_functionri   r   )rC   r   r   r   rj   default_varrk   r"   r#   r   S  s   
zDefaultDictVariable.call_methodr   r   )
r^   r_   r`   rD   r   rw   rc   r   r   r   r"   r"   rk   r#   r   :  s    
r   c                       s   e Zd ZdZdee 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 Zdee deeef ddf fddZdefddZ  ZS )SetVariablez-We model a sets as dictonary with None valuesr-   c                    s(   t |t }t j|fi | d S r   )r   fromkeysr   _default_valueri   rD   )rC   r-   rj   rk   r"   r#   rD   o  s   zSetVariable.__init__c                 C   s,   | j sdS dddd | j  D  d S )Nzset()rt   ,c                 s   s    | ]}|j  V  qd S r   rv   r    rq   r"   r"   r#   r$   {  rH   z)SetVariable.debug_repr.<locals>.<genexpr>rx   )r-   ry   r   rQ   r"   r"   r#   rw   w  s   "zSetVariable.debug_reprc                 C   s   t | j S r   )setr-   r   rQ   r"   r"   r#   	set_items}     zSetVariable.set_itemsc                   C   s
   t d S r   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"   ro   r   r"   r"   r#   	<setcomp>      z'SetVariable.as_proxy.<locals>.<setcomp>r   rQ   r"   r"   r#   r'        zSetVariable.as_proxyc                 C      t S r   )r   rQ   r"   r"   r#   r}        zSetVariable.python_typec                 C   r   )Nc                 S   r   r"   rz   r   r"   r"   r#   r     r   z1SetVariable.as_python_constant.<locals>.<setcomp>r   rQ   r"   r"   r#   rN     r   zSetVariable.as_python_constantc                 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   rV   rC   r   r"   r"   r#   r     s   zSetVariable.reconstructr   rj   r[   r   c           	         sB  ddl m}m} |dkr#|rJ t|dksJ d}|d t f}nu|dkrA|r+J |r/J | j j}t	 
|||f| |S |dkr`|rIJ t|dksQJ ttj|| |d gi S |dkrt|dkrt|d t||fr| jrt|d ||frt|d |}n|d }t	 
|d|f|S t	 
||||S )	Nr   )r   r+   r   r   r   r   
isdisjointr   )r   r   r+   rV   r   r   r   r   rB   ri   r   r   r3   r   set_isdisjointr   r%   r   r   )	rC   r   r   r   rj   r   r+   resultr   rk   r"   r#   r     sJ   	zSetVariable.call_methodr   c                 C   s   t d)NzIllegal to getitem on a set)RuntimeError)rC   r   r"   r"   r#   r     s   zSetVariable.getitem_const)r^   r_   r`   ra   r   r   rD   rw   rb   r   rc   r   r'   r}   rN   r   r   strr   r   r   r"   r"   rk   r#   r   l  s,    


0r   c                       s   e Zd ZU dZdZee ed< def fddZ	e
dd Ze
d	d
 Zdd Zdd Zded deedf ddf fddZ  ZS )DictViewzu
    Models _PyDictViewObject

    This is an "abstract" class. Subclasses will override kv and the items method
    Nkvdv_dictc                    s8   t  jdi | | jdv sJ t|tsJ || _d S )N)r   r   r"   )ri   rD   r   r%   r?   r   )rC   r   rj   rk   r"   r#   rD     s   
zDictView.__init__c                 C   s   t | jj| j S r   )getattrr   r-   r   rQ   r"   r"   r#   
view_items  s   zDictView.view_itemsc                 C      t r   NotImplementedErrorrQ   r"   r"   r#   view_items_vt  s   zDictView.view_items_vtc                    s     fddfdd j D S )Nc                    s    j dkr| jS | S )Nr   )r   rB   r=   rQ   r"   r#   unwrap  rs   z,DictView.unpack_var_sequence.<locals>.unwrapc                    s   g | ]} |qS r"   r"   r   )r   r"   r#   r     s    z0DictView.unpack_var_sequence.<locals>.<listcomp>r   r   r"   )rC   r   r#   r     s   zDictView.unpack_var_sequencec                 C   s(   || j  |t| jgtd d S )Nr   )r   r   r   r   r
   r   r"   r"   r#   r     s   
zDictView.reconstructr   r   rj   r[   c                    ,   |dkr| j ||||S t ||||S )Nr   r   r   ri   rC   r   r   r   rj   rk   r"   r#   r        zDictView.call_method)r^   r_   r`   ra   r   r   r   __annotations__r?   rD   rb   r   r   r   r   r   r   r   r   r"   r"   rk   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   rO   r   )r   r   rQ   r"   r"   r#   r        
zDictKeys.set_itemsc                 C   r   )Nc                 S   r   r"   r   r   r"   r"   r#   r     r   z*DictKeys.view_items_vt.<locals>.<listcomp>r   rQ   r"   r"   r#   r     s   zDictKeys.view_items_vtc                 C   r   r   )r   rQ   r"   r"   r#   r}     r   zDictKeys.python_typer   r   rj   r[   c                    r   )Nr~   r   r   rk   r"   r#   r   
  r   zDictKeys.call_method)r^   r_   r`   r   rb   r   r   r}   r   r   r   r   r   r"   r"   rk   r#   r     s    


r   c                   @   s$   e Zd ZdZedd Zdd ZdS )r   r   c                 C   rO   r   )r   r   rQ   r"   r"   r#   r     r   zDictValues.view_items_vtc                 C   r   r   )r   rQ   r"   r"   r#   r}     r   zDictValues.python_typeN)r^   r_   r`   r   rb   r   r}   r"   r"   r"   r#   r     s
    
r   r[   c                 C       t jd}|d uot| |jS )Nztransformers.file_utils)sysmodulesr*   
issubclassModelOutputclsmodr"   r"   r#   _is_matching_transformers_cls"     r   c                 C   r   )Nzdiffusers.utils)r   r   r*   r   
BaseOutputr   r"   r"   r#   _is_matching_diffusers_cls'  r   r   r   r   c              	   C   s   || j v st| j|rtdS t| jtr| jdu rtdS | jdu rL| jrLz|jj	
| j}tt| j|tj tt||W S  tyK   Y nw td| jj d| d| j d| j  dS )zXShared method between DataClassVariable and CustomizedDictVariable where items are attrsTNFzhasattr(ru   z)  )r-   hasattrr@   r   r   r   r   r   r   root_txget_example_valuer   r   
make_guardr   HASATTRr   r   rl   r^   )rC   r   r   exampler"   r"   r#   _call_hasattr_customobj,  s"   "r  c                       s   e Zd ZdZdZeeddd Zedd Z	e
dd	 Ze
d
d Ze
dd Z fddZdd Zdd Z						d fddZdeddf fddZeZ  ZS ) DataClassVariablez
    This is a bit of a hack to deal with
    transformers.file_utils.ModelOutput() from huggingface.

    ModelOutput causes trouble because it a a mix of a dataclass and a
    OrderedDict and it calls super() methods implemented in C.
    FNc                  C   s   zddl m}  | j D ]}t|rt|j qW n	 ty"   Y nw zddlm	} |j D ]}t|r:t|j q/W d S  tyG   Y d S w )Nr   )r   )r   )
transformers.file_utilsr   __dict__r   callabler   __code__ImportErrordiffusers.utilsr   )r   objr   r"   r"   r#   _patch_onceN  s(   

zDataClassVariable._patch_oncec                 C   s   t | pt| S r   )r   r   r   r"   r"   r#   is_matching_clsc  s   z!DataClassVariable.is_matching_clsc                 C      |  t|S r   r  rA   r   r  r"   r"   r#   is_matching_objectg  r   z$DataClassVariable.is_matching_objectc           
      C   s  t   t|jj dd t|D }t|j	|i |}|
  t|j t|ks0J i }|D ]5}|j| }	t|}t|	trJ|	||< q4| jr^tj|	sUJ tj|	||< q4|	d u siJ d|	 q4t|dkr~t||d  tjs~td | ||fi |S )Nc                 S   r   r"   r   r    fr"   r"   r#   r   p  r   z,DataClassVariable.create.<locals>.<listcomp>zunexpected r   r   z&DataClassVariable iterator constructor)r  r  r   rD   r  dataclassesfieldsinspect	signaturebindapply_defaultsr   	argumentsr   r   r   r%   r   include_noner   r\   rV   r&   r   )
r   r@   r   rj   optionsr   boundr-   rf   valr"   r"   r#   r   k  s&   



 zDataClassVariable.createc           
      C   s   t |}dd t|D }g }i }|D ]0}t||rDt||}|j|jt|j|d|}	|d us5| j	r?t
|}|	||< q||	 q| ||S )Nc                 S   r   r"   r  r  r"   r"   r#   r     r   z*DataClassVariable.wrap.<locals>.<listcomp>)r   r   )rA   r  r  r   r   rl   r   r   r   r  r   r   append)
r   builderr  r@   r   excludedr-   rf   r   varr"   r"   r#   wrap  s$   





zDataClassVariable.wrapc                    (   t  j||fi | | |sJ d S r   ri   rD   r  rC   r-   r@   r  rk   r"   r#   rD        zDataClassVariable.__init__c                 C   r   r   r   rQ   r"   r"   r#   r'     r   zDataClassVariable.as_proxyc                 C   sR   | || jg |  }||  t| }| |t	||d d S )NT)
r   _create_load_constr@   r|   r   r   rJ   r   create_call_function_kwrV   )rC   r   dr   r"   r"   r#   r     s
   zDataClassVariable.reconstructr   r   rj   r   r[   r   c                    s   |dkr+|st |dksJ |d }| tkr| |S | |dg i |d||S |dkr?|s3|r5J tt| j	 S |dkrEd}t
 ||||S )Nr   r   r   to_tuple__setattr__r   )rV   r}   r   r   r   r   r+   r   r-   r   ri   )rC   r   r   r   rj   r   rk   r"   r#   r     s   
zDataClassVariable.call_methodr   c                    s~   t |}|| v r| |d|gi S | js6dd t| jD }||v r6tj || s.J tj || S t	 
|| d S )Nr   c                 S   s   i | ]}|j |jqS r"   )r   defaultr  r"   r"   r#   rh     r   z1DataClassVariable.var_getattr.<locals>.<dictcomp>)r   r   r   r  r  r  r@   r   r\   ri   var_getattr)rC   r   r   name_vtdefaultsrk   r"   r#   r0    s   
zDataClassVariable.var_getattrr   )r^   r_   r`   ra   r  rc   	functools	lru_cacher  r  classmethodr  r   r%  rD   r'   r   r   r   r0  r  call_hasattrr   r"   r"   rk   r#   r  B  s4    	



r  c                       s   e Zd Zedd Zedd Zedd Zedd Z fd	d
Z	dd Z
dd Z						d fddZdeddf fddZeZ  ZS )CustomizedDictVariablec                 C   s8   t | tjr| jtjju rt| dsdS t| pt| S )N__post_init__T)r   r   r   rD   r   r   r   r  r"   r"   r#   r    s   
z&CustomizedDictVariable.is_matching_clsc                 C   r  r   r  r  r"   r"   r#   r    r   z)CustomizedDictVariable.is_matching_objectc                    sB  dD ]!}t ||r#t||}t|sJ d| t |dr#t|j qt|rst|j	|i |}|
  dd  i }t|sFt|rd|j D ]\}	}
t|
trY|
jd u s]|
d u r^qK|
||	< qKn|j} fdd| D }n%|sdd | D }nt|d	krt|d
 tr|s|d
 j}ntd | ||fi |S )N)rD   r8  r.  r   zexpect callable attr r  c                 S   s.   t | tr| S t| rt| S td d S )Nz5expect VariableTracker or ConstantVariable.is_literal)r%   r   r   r\   r   r   r=   r"   r"   r#   make_var  s   


z/CustomizedDictVariable.create.<locals>.make_varc                    s    i | ]\}}t | |qS r"   r   rp   r9  r"   r#   rh   
  r   z1CustomizedDictVariable.create.<locals>.<dictcomp>c                 S   s   i | ]
\}}t ||qS r"   r   rp   r"   r"   r#   rh     r{   r   r   z/custom dict init with args/kwargs unimplemented)r   r   r  r   r  r  is_dataclassr  r  r  r  r   r   r  r-   r%   r   rL   rV   r?   r   )r   r@   r   rj   r  	attr_namer   r  
bound_argsrq   rU   r-   r"   r:  r#   r     s<   








zCustomizedDictVariable.createc                 C   r   r   r   )r   r"  r  r"   r"   r#   r%    s   zCustomizedDictVariable.wrapc                    r&  r   r'  r(  rk   r"   r#   rD     r)  zCustomizedDictVariable.__init__c                 C   r   r   r   rQ   r"   r"   r#   r'   !  r   zCustomizedDictVariable.as_proxyc                 C   s   t | jp	t| j}|r'||jdddd ||d ||d ||| jg |r;|tdd | 	 }|
|  t| }||t||d d S )NtorchT)r   _dynamodisabler   F)r   r@   r   r   create_load_globalr   r   r*  r	   r|   r   r   rJ   r   r+  rV   )rC   r   is_hf_model_outputr,  r   r"   r"   r#   r   &  s    z"CustomizedDictVariable.reconstructr   r   rj   r   r[   r   c                    s   t | j|}| jd u rd nt| j|}t|dr)|jttjfv r)t	 
||||S |dv r>|tj||d| gt| |S td| d S )N__objclass__)r   r-  r   r.  )r   z.custom dict: call_method unimplemented name=%s)r   r@   r   r   r   rC  r   r   r   ri   r   inline_user_function_returnr   r3   r   r   )rC   r   r   r   rj   r   r   rk   r"   r#   r   C  s   z"CustomizedDictVariable.call_methodr   c                    s6   t |}|| v r| |d|gi S t || d S )Nr   )r   r   r   ri   r0  )rC   r   r   r1  rk   r"   r#   r0  ]  s   
z"CustomizedDictVariable.var_getattrr   )r^   r_   r`   rc   r  r5  r  r   r%  rD   r'   r   r   r   r0  r  r6  r   r"   r"   rk   r#   r7    s*    


3
r7  c                  C   s   dd l } dd }|| jj_d S )Nr   c                 S   s   t |dsdS | j|jkS )Nr  F)r   r  )rC   rZ   r"   r"   r#   !_dynamo_overriden_transformers_eqm  s   
zJ_install_PretrainedConfig_patch.<locals>._dynamo_overriden_transformers_eq)transformersconfiguration_utilsPretrainedConfigr]   )rF  rE  r"   r"   r#   _install_PretrainedConfig_patchf  s   rI  c                       s\   e Zd ZdZedd Zedd Z fddZde	d	d
fddZ
de	d	d
fddZ  ZS )HFPretrainedConfigVariablez/
    Hack for HuggingFace PretrainedConfig
    c                 C   s.   t jd}|d uot| |j}|rt  |S )Nz transformers.configuration_utils)r   r   r*   r   rH  rI  )r   r   is_matchr"   r"   r#   r  |  s
   z*HFPretrainedConfigVariable.is_matching_clsc                 C   r  r   r  r  r"   r"   r#   r    r   z-HFPretrainedConfigVariable.is_matching_objectc                    s.   t  jdi | || _| t|sJ d S )Nr"   )ri   rD   r  r  rA   )rC   r  rj   rk   r"   r#   rD     s   z#HFPretrainedConfigVariable.__init__r   r[   r   c                 C   s   ddl m} |t| j|S )Nr   r   )r   r   r   r   r  )rC   r   r   r   r"   r"   r#   r0    s   z&HFPretrainedConfigVariable.var_getattrc                 C   s   t jt| j|S r   )r   r   r   r   r  )rC   r   r   r"   r"   r#   r6    rs   z'HFPretrainedConfigVariable.call_hasattr)r^   r_   r`   ra   rc   r  r5  r  rD   r   r0  r6  r   r"   r"   rk   r#   rJ  w  s    
	
rJ  c                   @   s   e Zd ZdZdd Zdd Zdee dee	ef fdd	Z
d
efddZd
efddZ	dd
edee fddZd
efddZdS )PythonSysModulesVariablezSpecial case for sys.modules.

    Without this we will guard on the exact set of modules imported in the
    lifetime of the python program.
    c                 C   r   r   )r   rQ   r"   r"   r#   r}     r   z$PythonSysModulesVariable.python_typec                 C   s    | |td|dg d S )NTr   )r   r   r   r   r   r"   r"   r#   r     s
   
z$PythonSysModulesVariable.reconstructr   rj   c                 C   s   |dkr| j |g|R i |S |dkr | j|g|R i |S |dkr0| j|g|R i |S td| d| d| d d S )Nr   r*   r~   zsys.modules.z(*z, **r   )call_getitemcall_getcall_containsr   r   r"   r"   r#   r     s    z$PythonSysModulesVariable.call_methodrf   c                 C   s8   |  }|tjv }t| tjtj|| d ||fS )N)rf   invert)	rN   r   r   r   r   r3  partialr   DICT_CONTAINSrC   r   rf   rq   has_keyr"   r"   r#   _contains_helper  s   
z)PythonSysModulesVariable._contains_helperc                 C   s   |  ||\}}tj|dS )NrL   )rU  r   r   rS  r"   r"   r#   rO    s   z&PythonSysModulesVariable.call_containsNr/  c                 C   sT   ddl m} | ||\}}|r||t| j|tj| S |d ur$|S tjd dS )Nr   VariableBuilderrV  )	r"  rX  rU  r   r   r   r   r   r   )rC   r   rf   r/  rX  rq   rT  r"   r"   r#   rN    s   
z!PythonSysModulesVariable.call_getc                 C   s8   ddl m} | ||\}}||t| j|tj| S )Nr   rW  )r"  rX  rU  r   r   r   r   )rC   r   rf   rX  rq   rT  r"   r"   r#   rM    s   
z%PythonSysModulesVariable.call_getitemr   )r^   r_   r`   ra   r}   r   r   r   r   r   r   rU  rO  r   rN  rM  r"   r"   r"   r#   rL    s$    



rL  )9r   r  r3  r  r   typingr   r   r   torch._subclasses.fake_tensorr   r   r   r   bytecode_transformationr	   r
   r   r   
eval_framer   excr   guardsr   r   r   r   r   utilsr   r   r   r   baser   r   constantr   r   r?   r   r   r   r   r   rd   r   r   r   r  r  r7  r4  rI  rJ  rL  r"   r"   r"   r#   <module>   sF    }2[4  
!