o
    "ibT                     @   s  d dl Z d dlmZmZmZmZmZmZmZ d dl	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 d dlmZ d dlmZ d dlmZ de	jjfd	d
ZdedefddZdedefddZe j e j!e j"e j#e j$dZ%de	j&j'fddZ(G dd dZ)G dd dZ*dS )    N)AnyDictListOptionalSetTupleUnion)ExportedProgram)ConstantArgument	InputKind	InputSpec
OutputKind
OutputSpecTensorArgument)subgraph_rewriter)_create_jit_graph)FunctionSchemagmc                 C   s"   dd }dd }t | ||}d S )Nc                 S   sJ   t jjj| |}t jj|}t jjjj||dd}t jjj	|}|S )Ntrunc)rounding_mode)
torchopsatensym_sizeintscalar_tensordivScalar_modeIntTensor)imdimscalesym_size_intr   div_scalar_mode
int_tensor r&   U/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/_export/converter.pypattern   s   
z.inplace_optimize_sym_size_div.<locals>.patternc                 S   s   t jjj| |}|| S N)r   r   r   r   r   )r    r!   r"   r#   r&   r&   r'   replacement"   s   z2inplace_optimize_sym_size_div.<locals>.replacement)r   replace_pattern)r   r(   r*   replaced_patternsr&   r&   r'   inplace_optimize_sym_size_div   s   	r-   namereturnc                 C   s   |  ddS )N._)replacer.   r&   r&   r'   normalize_name)   s   r4   c                 C   s   |  d}dd| S )zprim::If -> convert_prim_If::convert_r1   )splitjoin)r.   	name_listr&   r&   r'   ir_name_to_func_name-   s   
r:   )zprim::TupleIndexzaten::__is__zaten::__isnot__zaten::__not__zaten::__contains__nodec           
   
   C   s   |   }t|}t|jjd\}}|jj}zttj	|}t||}|r.t||}W |S |j
}W |S  tyM }	 ztd|   d| j  |	d }	~	ww )Nr5   zUnable to find operator z with schema )schemar   parsestrr.   r7   overload_namegetattrr   r   default	ExceptionRuntimeErrorkind)
r;   
schema_strr<   nsop_nameoverrideop_overload_modop_overload_packetop_overloader&   r&   r'   get_op_overload?   s(   

rM   c                   @   sP  e Zd Zdeejjejjf dee	 dee	 fddZ
de	fddZd	ejjfd
dZdejjfddZdejjfddZdd Zd	ejjfddZd	ejjfddZd	ejjfddZd	ejjfddZd	ejjfddZd	ejjfddZd	ejjfdd Zd	ejjfd!d"Zd	ejjfd#d$Zd	ejjfd%d&Zd	ejjfd'd(Zd	ejjfd)d*Zd	ejjfd+d,Z d	ejjfd-d.Z!d	ejjfd/d0Z"d	ejjfd1d2Z#d	ejjfd3d4Z$d	ejjfd5d6Z%d	ejjfd7d8Z&d	ejjfd9d:Z'd	ejjfd;d<Z(d	ejjfd=d>Z)d	ejjfd?d@Z*d	ejjfdAdBZ+d	ejjfdCdDZ,dEdF Z-dGS )HTS2FXGraphConverterts_graphparam_namesbuffer_namesc                    sv   | _ | _| _tj  _g  _g  _i  _	i  _
i  _i  _i  _t D ]}t|}t | fdd q(d S )Nc                    s
     | S r)   )_convert_standard_operators)r;   selfr&   r'   <lambda>t   s   
 z.TS2FXGraphConverter.__init__.<locals>.<lambda>)rO   rP   rQ   r   fxGraphfx_graphinput_specsoutput_specsname_to_nodeconstant_mapattribute_maptensor_constants	subgraphskind_to_standard_operatorskeysr:   setattr)rT   rO   rP   rQ   khandler_func_namer&   rS   r'   __init__U   s(   
zTS2FXGraphConverter.__init__r/   c                 C   s   dt | j }|| j|< |S )N	subgraph_)lenr_   )rT   subgraphr.   r&   r&   r'   add_subgraphw   s   
z TS2FXGraphConverter.add_subgraphr;   c                 C   sV   g }i }t | |jD ]\}}|jr| |||j< q|| | qt||fS r)   )zipinputs	arguments
kwarg_onlyget_fx_valuer.   appendtuple)rT   r;   r<   argskwargsinput
schema_argr&   r&   r'   get_args_kwargs|   s   z#TS2FXGraphConverter.get_args_kwargsvaluec                 C   s   |  }|| jv r| j| }|S || jv r.| j| }|| jv r&| j| }|S td| d|| jv r8| j| S td| d)NzValue 
 not foundzInput )	debugNamer[   r]   
ValueErrorr\   )rT   rv   
value_name
input_node	attr_namer&   r&   r'   rn      s   







z TS2FXGraphConverter.get_fx_valuec                 C   sR   |    | j D ]}| | q	|   tj| j| j	}t
| |j  |S r)   )convert_graph_inputsrO   nodesconvert_nodeconvert_graph_outputsr   rV   GraphModuler_   rX   r-   graphlint)rT   r;   r   r&   r&   r'   convert   s   
zTS2FXGraphConverter.convertc              	   C   s   | j  D ]O}| }t|}| j|}|| j|< || jv r/| j	t
tjt|d|d q|| jv rE| j	t
tjt|d|dd q| j	t
tjt|d|d qd S )Nr3   argtargetT)r   r   
persistent)rO   rk   rx   r4   rX   placeholderr[   rP   rY   ro   r   r   	PARAMETERr   rQ   BUFFER
USER_INPUT)rT   graph_inputr.   normalized_namefx_noder&   r&   r'   r}      s<   


	z(TS2FXGraphConverter.convert_graph_inputsc                 C   s   |   }d }|drr|d}|dkr|d}nX|dkr&|d}nN|dkr0|d}nD|dkr^d| }| j|}|| j	|< |
d| j|< | jttjt|d|d |}n|d	krh|d}ntd
|d d }|| j|< d S )Nrv   ifst	constant_r3   r   ivalzUnsupported constant type: )outputrx   hasAttributekindOfr   r   r   rX   r   r[   r   r^   rY   ro   r   r   CONSTANT_TENSORr   r   ry   r\   )rT   r;   r.   rv   constant_kindplaceholder_namer   r&   r&   r'   convert_prim_Constant   s8   



z)TS2FXGraphConverter.convert_prim_Constantc                 C   sP   |   }|tjj r | }| 	 }|| j
|< d S td| d)NzUnsupported JitType (z) when get device)rs   typeisSubtypeOfr   _C
TensorTypegetdevicer   rx   r\   ry   )rT   r;   
input_typer   output_namer&   r&   r'   convert_prim_device   s   z'TS2FXGraphConverter.convert_prim_devicec                 C   s*   |    }|  }|| j|< d S r)   )rs   r   dtyper   rx   r\   )rT   r;   r   r   r&   r&   r'   convert_prim_dtype   s   z&TS2FXGraphConverter.convert_prim_dtypec                    s\   dt f fdd}|  }|d}|  }||}|r'| d| n| j|< d S )Nr.   c                    s$   |  j v r
 j |  S td|  d)Nz
Attribute rw   )r]   ry   r3   rS   r&   r'   get_attr  s   

z:TS2FXGraphConverter.convert_prim_GetAttr.<locals>.get_attrr0   )r>   r   rx   r   rs   r]   )rT   r;   r   r   r|   
input_nameroot_attr_namer&   rS   r'   convert_prim_GetAttr  s   
z(TS2FXGraphConverter.convert_prim_GetAttrc                 C   s`   t |}|tjjjju rtjjjj}| ||j\}}| j	
|||}|  }|| j|< d S r)   )rM   r   r   r   sizer   r   ru   _schemarX   call_functionr   rx   r[   rT   r;   r   rq   rr   r   r   r&   r&   r'   convert_call_function_op  s   z,TS2FXGraphConverter.convert_call_function_opc                 C      |  | d S r)   _convert_prim_iteratorrT   r;   r&   r&   r'   convert_prim_TupleConstruct"     z/TS2FXGraphConverter.convert_prim_TupleConstructc                 C   r   r)   r   r   r&   r&   r'   convert_prim_ListConstruct%  r   z.TS2FXGraphConverter.convert_prim_ListConstructc                 C   s<   g }|  D ]
}|| | q|  }|| j|< d S r)   )rk   ro   rn   r   rx   r[   )rT   r;   output_listinpr   r&   r&   r'   r   (  s
   z*TS2FXGraphConverter._convert_prim_iteratorc                 C   s   i }d\}}t | D ])\}}|d dkr| |}q| |}|d ur)|d us-J d|||< d\}}q|d u r>|d u sBJ d|  }|| j|< d S )N)NN   r   z*DictConstruct has an empty key value pair.zGDictConstruct has an odd number of elements (violating our assumption).)	enumeraterk   rn   r   rx   r[   )rT   r;   output_dictrc   vr   r   r   r&   r&   r'   convert_prim_DictConstruct0  s    

z.TS2FXGraphConverter.convert_prim_DictConstructc                 C   r   r)   _convert_prim_unpack_iteratorr   r&   r&   r'   convert_prim_ListUnpackG  r   z+TS2FXGraphConverter.convert_prim_ListUnpackc                 C   r   r)   r   r   r&   r&   r'   convert_prim_TupleUnpackJ  r   z,TS2FXGraphConverter.convert_prim_TupleUnpackc                 C   sN   t | D ]\}}| }| | }| jtj||f}|| j	|< qd S r)   )
r   outputsrx   rn   rs   rX   r   operatorgetitemr[   )rT   r;   r   outp	outp_namer   r   r&   r&   r'   r   M  s   z1TS2FXGraphConverter._convert_prim_unpack_iteratorc                    sn   t jjjj}t fdd| D } j||dt j	i} jt jjj
j|f}|  }| j|< d S )Nc                 3       | ]}  |V  qd S r)   rn   .0rs   rS   r&   r'   	<genexpr>X      z7TS2FXGraphConverter.convert_aten_Int.<locals>.<genexpr>r   )r   r   r   _to_copyrA   rp   rk   rX   r   int32_local_scalar_denser   rx   r[   )rT   r;   r   rq   to_copy_noder   r   r&   rS   r'   convert_aten_IntU  s   z$TS2FXGraphConverter.convert_aten_Intc                    L   t jjj}t fdd| D } j||}| 	 }| j
|< d S )Nc                 3   r   r)   r   r   rS   r&   r'   r   h  r   z?TS2FXGraphConverter.convert_prim_NumToTensor.<locals>.<genexpr>)r   r   r   r   rp   rk   rX   r   r   rx   r[   rT   r;   r   rq   r   r   r&   rS   r'   convert_prim_NumToTensore  s
   
z,TS2FXGraphConverter.convert_prim_NumToTensorc                 C   s   |   }d| j|< d S )N )r   rx   r]   )rT   r;   r   r&   r&   r'   convert_prim_CreateObjecto  s   z-TS2FXGraphConverter.convert_prim_CreateObjectc                 C   sH   t jjjj}| ||j\}}| j|||}|	 
 }|| j|< d S r)   )r   r   r   convolutionrA   ru   r   rX   r   r   rx   r[   r   r&   r&   r'   convert_aten__convolutions  s
   z-TS2FXGraphConverter.convert_aten__convolutionc                 C   s   t |}|j}| ||\}}|jdkrQ|d j}|| jv rQ| j| }| dkrQt|}| j|  |d< | j	
tjjjjt||}	|  }
|	| j|
< d S | | d S )NTensor_mode   )rM   r   ru   r?   r.   r^   numellistitemrX   r   r   r   r   r   r   rp   r   rx   r[   r   )rT   r;   r   r<   rq   rr   	arg1_nametensor_constantupdated_argsr   r   r&   r&   r'   convert_aten_div~  s&   




	
z$TS2FXGraphConverter.convert_aten_divc                    sL   t  fdd| D \}} jtj||f}|  }| j|< d S )Nc                 3   r   r)   r   r   rS   r&   r'   r     s    

z?TS2FXGraphConverter.convert_aten___getitem__.<locals>.<genexpr>)	rp   rk   rX   r   r   r   r   rx   r[   )rT   r;   input_containerindexr   r   r&   rS   r'   convert_aten___getitem__  s   
z,TS2FXGraphConverter.convert_aten___getitem__c                    sj  t | }t|dksJ  |d }t }| D ]&}t }| D ]}| D ]}|  jv r;|	|  q+q%|
| qt |}g }	| D ]4}t|t t }
 j|
_|D ]}t|}|
j|}||
j|< q]|
 } |}|	 j| qMt|	dksJ  fdd|D }||	d |	d t|f} jtj|i }|  }| j|< d S )Nr   r   r   c                    s   g | ]} j | qS r&   )r[   )r   arg_namerS   r&   r'   
<listcomp>  s    z7TS2FXGraphConverter.convert_prim_If.<locals>.<listcomp>)r   rk   rg   rn   setblocksr~   rx   r[   addupdaterN   r\   r4   rX   r   r   ri   ro   r   rp   r   r   condr   )rT   r;   rk   	predicaterl   block
block_args
block_nodeblock_node_insubgraph_nodessubgraph_converter	block_argnormalized_block_arg_nameplaceholder_noderh   subgraph_namefx_block_argsrq   	cond_noder   r&   rS   r'   convert_prim_If  sJ   
z#TS2FXGraphConverter.convert_prim_Ifc                 C   r   r)   )_convert_as_noopr   r&   r&   r'   convert_aten_Bool  r   z%TS2FXGraphConverter.convert_aten_Boolc                 C   s<   t |}|j}| ||\}}|  }|d | j|< d S )Nr   )rM   r   ru   r   rx   r[   )rT   r;   r   r<   rq   rr   r   r&   r&   r'   r     s
   z$TS2FXGraphConverter._convert_as_noopc                    r   )Nc                 3   r   r)   r   r   rS   r&   r'   r     r   zRTS2FXGraphConverter.convert_profiler__record_function_enter_new.<locals>.<genexpr>)r   r   profiler_record_function_enter_newrp   rk   rX   r   r   rx   r[   r   r&   rS   r'   +convert_profiler__record_function_enter_new  s
   
z?TS2FXGraphConverter.convert_profiler__record_function_enter_newc                    s6   t jjj}t fdd| D } j|| d S )Nc                 3   r   r)   r   r   rS   r&   r'   r     r   zMTS2FXGraphConverter.convert_profiler__record_function_exit.<locals>.<genexpr>)r   r   r   _record_function_exitrp   rk   rX   r   )rT   r;   r   rq   r&   rS   r'   &convert_profiler__record_function_exit  s   
z:TS2FXGraphConverter.convert_profiler__record_function_exitc                    sN   t |  }t fdd| D } j||}|  }| j|< d S )Nc                 3   r   r)   r   r   rS   r&   r'   r     r   zBTS2FXGraphConverter._convert_standard_operators.<locals>.<genexpr>)	r`   rD   rp   rk   rX   r   r   rx   r[   r   r&   rS   r'   rR     s
   z/TS2FXGraphConverter._convert_standard_operatorsc                 C   s*   |  }t|}t| || j}|| d S r)   )rD   r:   r@   r   )rT   r;   	node_kindrd   handler_funcr&   r&   r'   r      s   z TS2FXGraphConverter.convert_nodec              	   C   s   g }| j  D ]K}| }|| jv r*|| j|  | jttjt	|d|d q|| j
v rK|| j
|  | jttjt|| j
| d|d qtd| d| j|d  d S )Nr3   r   )r.   rv   zOutput rw   r   )rO   r   rx   r[   ro   rZ   r   r   USER_OUTPUTr   r\   r
   ry   rX   r   )rT   rq   graph_outputr   r&   r&   r'   r   
  s6   



z)TS2FXGraphConverter.convert_graph_outputsN).__name__
__module____qualname__r   r   r   rW   Blockr   r>   re   ri   Noderu   Valuern   rV   r   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rR   r   r   r&   r&   r&   r'   rN   T   sN    
"&%	

5
rN   c                   @   sV   e Zd Z	ddeedf deeeef  fddZde	fdd	Z
d
ejjfddZdS )TS2EPConverterNsample_args.sample_kwargsc                 C   sT   || _ t||\| _| _}}|| _|| _dd | D | _dd | D | _	d S )Nc                 S      h | ]\}}|qS r&   r&   r   r.   r1   r&   r&   r'   	<setcomp>8      z*TS2EPConverter.__init__.<locals>.<setcomp>c                 S   r  r&   r&   r  r&   r&   r'   r  9  r  )
ts_modelr   rO   paramsr  r  named_parametersrP   named_buffersrQ   )rT   r  r  r  r1   r&   r&   r'   re   ,  s   zTS2EPConverter.__init__r/   c                 C   s,   t | j| j| j}| }| ||j}|S r)   )rN   rO   rP   rQ   r   retrace_as_exported_programr^   )rT   graph_converterr   epr&   r&   r'   r   ;  s   zTS2EPConverter.convertr   c                 C   s6   g | j | j| }tjjj|t|ddd}|S )NFT)strictpre_dispatch)r  r  valuesr   export_trace_exportrp   )rT   r   r^   rk   r  r&   r&   r'   r  C  s   z*TS2EPConverter.retrace_as_exported_programr)   )r  r  r  r   r   r   r   r>   re   r	   r   r   rV   r   r  r&   r&   r&   r'   r  *  s    

r  )+r   typingr   r   r   r   r   r   r   r   torch.export._tracetorch.export.exported_programr	   torch.export.graph_signaturer
   r   r   r   r   r   torch.fxr   torch.onnx.utilsr   torchgen.modelr   rV   r   r-   r>   r4   r:   r   is_is_notnot_containsr`   r   r
  rM   rN   r  r&   r&   r&   r'   <module>   s0   $ 
	   Y