o
    "ix@                     @   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m	Z	 d dl
ZddlmZ ddlmZmZmZmZmZmZ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 dd	lm Z  dd
l!m"Z" ddl#m$Z$m%Z%m&Z&m'Z' ddl(m)Z) ej*G dd dZ+G dd dZ,dS )    N)CounterDictListOptional   )utils)	create_call_functioncreate_call_methodcreate_dup_topcreate_instructioncreate_load_attrcreate_load_globalcreate_load_methodcreate_rot_nInstruction)unimplemented)
AttrSourceSource)is_safe_constantrot_n_helper)VariableTracker)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableTensorVariableUnspecializedPythonVariable)TensorWithTFOverrideVariablec                   @   s   e Zd ZU eed< eed< dS )GraphOutputEntryindexvariableN)__name__
__module____qualname__int__annotations__r    r%   r%   S/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/_dynamo/codegen.pyr   %   s   
 r   c                   @   s  e Zd ZdZ				d`deejj dee fddZ	ddd	d
Z
dd Zdd ZdaddZ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e fd"d#Zd!efd$d%Zd!efd&d'Zd!efd(d)Zdbd!efd+d,Zd!efd-d.Zd!efd/d0ZeZd1d2 Z d3d4 Z!d5d6 Z"d!efd7d8Z#d9d: Z$d;d< Z%d!efd=d>Z&d?d@ Z'dcdBdCZ(dDdE Z)dFdG Z*dHdI Z+dJe,dKe-fdLdMZ.dNdO Z/dPdQ Z0	AdcdRedSe1j2dKe-fdTdUZ3d!efdVdWZ4dRed!dfdXdYZ5dddZd[Z6d!ee fd\d]Z7d!efd^d_Z8dS )e	PyCodegenz<
    Helper class uses for constructing Python bytecode
    Nrootgraph_output_varc                 C   sj   || _ d | _t | _i | _g | _|pi | _|| _|| _	| jj
j| _| jj| _| jj
j| _d| _d| _d S )NFT)r(   top_of_stackcollectionsr   usesgraph_outputs_outputtempvarstxr)   outputcode_optionscell_and_freevarsnew_var mutable_side_effects_from_sourcevalue_from_source)selfr0   r(   r)   r/   r%   r%   r&   __init__0   s   



zPyCodegen.__init__T)r6   c                C   sL   | j }d| _ | j}|  j|M  _z| | W || _ || _d S || _ || _w )NT)r5   r6   foreach)r7   stack_valuesr6   priorprevr%   r%   r&   restore_stackE   s   
zPyCodegen.restore_stackc                 C   s   dd | j  D S )Nc                 S   s   g | ]}|j qS r%   )r   .0xr%   r%   r&   
<listcomp>Q   s    z/PyCodegen.graph_output_vars.<locals>.<listcomp>)r-   valuesr7   r%   r%   r&   graph_output_varsP      zPyCodegen.graph_output_varsc                 C   s$   | | }|d u sJ d| d S )Nzreconstruct!=None )reconstruct)r7   valueresr%   r%   r&   call_reconstructS   s   
zPyCodegen.call_reconstructc           	      C   s  t |tr| | |   dS t |tsJ | j}| j}| j|u r,|r,|t	  dS | j
rCddlm} t |j|rC| |jj dS |rw|jr_|j| jv r_|| | j|j  || _dS | j|durw|| | j|  || _dS |jdur|r| jr| |j n@| rt| r|| |  n*t |tr| |}| tjd | || j || j|| j ddd |!t"dd nt |t#r|$ t%kr| j j&s| |'| j }| || j |!| (d	gt"d
d  nt |t)t#t*t+frK| |}t |t+r| tjd | || j t |t+r2|!t"dd nt |t*rJ|j,rJ|!| (d	gt"d
d  n~t |t-r|j./d}|d
 | j0d v rr|| |d
  |dd }n| j1duszJ || 2| j1 |D ]}|| (| qn7| j3|  d7  < z| | W n t4y   t5d|  Y nw |r|| jv r| jt	  | 6| || _dS )z:Generate code such that top-of-stack (TOS) is set to valueNr   )MutableSideEffectsto_subclassFTadd   itemr   to_numpy_helper.co_varnameszreconstruct: )7
isinstancer   rI   	clear_tosr   r.   r-   r*   appendr
   r5   side_effectsrJ   mutable_localsourcer/   create_loadgetr6   is_python_constantr   as_python_constantcreate_load_constr   add_graph_outputload_import_fromr   r    load_graph_outputr   r   global_mangled_class_namer0   extendr   r   python_typefloatexport	as_tensorr   r   r   r   need_unwrapr   
module_keysplitr2   r(   create_load_outputr,   NotImplementedErrorr   	add_cache)	r7   rG   allow_cacher1   r-   rJ   graph_outputs_keypartspartr%   r%   r&   __call__W   s   






	

zPyCodegen.__call__c                 C   s0   t | }|| jvrtt| j|| j|< |S N)idas_proxyr-   r   len)r7   rG   rn   r%   r%   r&   r^      s   


zPyCodegen.add_graph_outputc                 C   s:   | j }|| | j || | |td d S )NBINARY_SUBSCR)r.   rU   rY   r)   _create_load_constr   )r7   r   r1   r%   r%   r&   r`      s   zPyCodegen.load_graph_outputc                 C   s:   |   }|| j|< |jr|| j|j< | j| | d S rr   )r4   r/   rW   r.   rU   create_store)r7   rG   varr%   r%   r&   rl      s
   
zPyCodegen.add_cachec                 C   s   |D ]}| | qd S rr   r%   )r7   itemsir%   r%   r&   r9      s   
zPyCodegen.foreachc                 C   sR   t dd|}| jj}||v rt|| t|ksJ n|||< | j||ddgS )zStore value in a new globalz[^a-zA-Z0-9_]+_TrL   )resubr0   	f_globalsrs   r   )r7   namerG   	push_nullr   r%   r%   r&   setup_globally_cached   s   zPyCodegen.setup_globally_cachedc                 C   s
   d | _ d S rr   )r*   rC   r%   r%   r&   rT      s   
zPyCodegen.clear_tosc                 C   s&   t |tsJ | j| |   d S rr   )rS   r   r.   rU   rT   )r7   instr%   r%   r&   append_output   s   zPyCodegen.append_outputc                 C   s.   t dd |D sJ | j| |   d S )Nc                 s   s    | ]}t |tV  qd S rr   )rS   r   r>   r%   r%   r&   	<genexpr>   s    z*PyCodegen.extend_output.<locals>.<genexpr>)allr.   rb   rT   )r7   instsr%   r%   r&   extend_output   s   zPyCodegen.extend_outputreturnc                 C   s   | j S rr   )r.   rC   r%   r%   r&   get_instructions   s   zPyCodegen.get_instructionsc                 C   s@   ||   v rtd|dS || jd v sJ | dtd|dS )N
LOAD_DEREFargvalrR   z missing	LOAD_FASTr3   r   r2   r7   r   r%   r%   r&   rY      s   zPyCodegen.create_loadc                 C   s   ||   v sJ td|dS )NLOAD_CLOSUREr   )r3   r   r   r%   r%   r&   create_load_closure  s   zPyCodegen.create_load_closurec                 C   s6   ||   v rtd|dS || jd v sJ td|dS )NSTORE_DEREFr   rR   
STORE_FASTr   r   r%   r%   r&   rx     s   zPyCodegen.create_storeFc                 C   s8   |r	| j j| || jd v sJ | dt||S )Nco_namesz not in co_names)r0   r1   update_co_namesr2   r   )r7   r   r   rM   r%   r%   r&   r     s   
zPyCodegen.create_load_globalc                 C   s    t |sJ d| | |S )Nzunsafe constant )r   rw   r7   rG   r%   r%   r&   r]     s   
zPyCodegen.create_load_constc                 C      t d|dS )N
LOAD_CONSTr   r   r   r%   r%   r&   rw        zPyCodegen._create_load_constc                 C   s   | j j| t|S rr   )r0   r1   r   r   r   r%   r%   r&   r     s   zPyCodegen.create_load_methodc                 C      |  | | d S rr   )r   r   r   r%   r%   r&   load_method  rE   zPyCodegen.load_methodc                 C   s   |  t| d S rr   )r   r	   )r7   nargsr%   r%   r&   call_method"     zPyCodegen.call_methodc                 C   s*   || j d vr| j d  |f7  < t|S )Nr   )r2   r   r   r%   r%   r&   r   %  s   zPyCodegen.create_load_attrc                 C   r   rr   )r   r   r   r%   r%   r&   	load_attr*  rE   zPyCodegen.load_attrc                    s    fdd| dD S )Nc                    s   g | ]}  |qS r%   )r   )r?   r   rC   r%   r&   rA   .  s    z/PyCodegen.create_load_attrs.<locals>.<listcomp>rQ   )ri   )r7   namesr%   rC   r&   create_load_attrs-  s   zPyCodegen.create_load_attrsc                 C   s.   || j d vr| j d  |f7  < td|dS )Nr   
STORE_ATTRr   )r2   r   r   r%   r%   r&   create_store_attr0  s   zPyCodegen.create_store_attrc                 C   r   rr   )r   r   r   r%   r%   r&   
store_attr5  rE   zPyCodegen.store_attrr   c                 C   sZ   g }|rt jdkr|tdg| |d  || j|dddg| |d  |S )z6Load the global fn_name on the stack num_on_stack down      	PUSH_NULLr   FTrL   )sysversion_inforb   r   rot_nr   )r7   fn_namer   num_on_stackr1   r%   r%   r&   load_function_name8  s   zPyCodegen.load_function_namec                 C   sX   zt |W S  ty+   td|d| t|gt dtdddtd|d Y S w )NBUILD_TUPLEargrN   CALL_FUNCTION_EXr   UNPACK_SEQUENCE)r   AttributeErrorr   rw   r   )r7   nr%   r%   r&   r   G  s   



zPyCodegen.rot_nc                 C   s0   t jdksJ | dd gtddtdS )Nr   c                   S   s   d S rr   r%   r%   r%   r%   r&   <lambda>Y  s    z$PyCodegen.pop_null.<locals>.<lambda>r   FPOP_TOP)r   r   rw   r   r   rC   r%   r%   r&   pop_nullT  s   zPyCodegen.pop_nullc                 C   s   |  td d S )Nr   )r   r   rC   r%   r%   r&   pop_top^  r   zPyCodegen.pop_topr   r   c                 C   s   |  t||d d S )N)r   )r   r   )r7   r   r   r%   r%   r&   call_functiona  s   zPyCodegen.call_functionc                 C   s   |  t  d S rr   )r   r
   rC   r%   r%   r&   dup_topd  s   zPyCodegen.dup_topc                 C   r   rr   )r   rx   )r7   varnamer%   r%   r&   storeg  rE   zPyCodegen.storer   codec                 C   s   |j }|sJ | j}tjdkr"|r"|td || |d  |D ]}||  v s.J |td|d q$|tdt	|d || 
| tjdk rX|| 
| |tdd	d || |d  |   d S )
Nr   r   r   r   r   r   r   MAKE_FUNCTION   )co_freevarsr.   r   r   rU   r   rb   r   r3   ru   r]   rT   )r7   r   r   r   r   freevarsr1   ry   r%   r%   r&   make_function_with_closurej  s    
z$PyCodegen.make_function_with_closurec                 C   sh   | j j}|j}tdd|j}||d|u r| j||ddS d| }| j j||}| j||ddS )zT
        Generate a LOAD_GLOBAL instruction to fetch a given python module.
        z^.*[.] NTrL   
___module_)	r0   r1   global_scoper}   r~   r    rZ   r   install_global_by_id)r7   modr   r1   r   r   prefixglobal_namer%   r%   r&   create_load_python_module~  s   
z#PyCodegen.create_load_python_modulec                 C   s   |  | |d | jjj}|D ]&}|jr1|  | td| dg | 	| |  t
dd q| 	| q|  t
t|d dS )z2Call the generated code function stored in fn_nameTrf   r   FN)r   r   r0   r1   	graphargspass_arg_as_tensorr   torchr   rI   r   ru   )r7   r   r   r   r%   r%   r&   make_call_generated_code  s   


z"PyCodegen.make_call_generated_codec                 C   s   | t | j|| d S rr   )r   r0   import_source)r7   module_nameobject_namer%   r%   r&   r_     s   zPyCodegen.load_import_fromc                 C   st   t jdkr/t||}t jdkrd}d}nd}d}|| j|ks!J td|d}||| |S | |td	|d
gS )Nr   )r      CALLPRECALLKW_NAMESr   CALL_FUNCTION_KWr   )r   r   r   opnamer   insertr]   )r7   r   kw_namesr   r1   idxexpected_instkw_names_instr%   r%   r&   create_call_function_kw  s   



z!PyCodegen.create_call_function_kwc                 C   r   )NDELETE_FASTr   r   r   r%   r%   r&   create_delete  r   zPyCodegen.create_delete)NNNN)T)F)r   )r   N)9r    r!   r"   __doc__r   r   nnModulestrr8   r=   rD   rI   rq   r^   r`   rl   r9   r   rT   r   r   r   r   r   rY   r   rx   r   r]   rw   rj   r   r   r   r   r   r   r   r   r   r   r   r   r#   boolr   r   r   typesCodeTyper   r   r   r_   r   r   r%   r%   r%   r&   r'   +   sv    


r




r'   )-r+   dataclassesr}   r   r   typingr   r   r   r   torch.nnr   r   r   bytecode_transformationr   r	   r
   r   r   r   r   r   r   excr   rX   r   r   r   r   variables.baser   variables.nn_moduler   variables.tensorr   r   r   r   variables.torch_functionr   	dataclassr   r'   r%   r%   r%   r&   <module>   s&   ,