o
    °ƒðjý  ã                   @   sR   d dl Z d dlZd dlmZ ddlmZmZmZ ddlm	Z	 	 G dd„ de	ƒZ
dS )	é    N)Úonnx_pbé   )Ú	QuantTypeÚattribute_to_kwargÚ	ms_domainé   )ÚQuantOperatorBasec                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )Ú	LSTMQuantc                    s   t ƒ  ||¡ d S )N)ÚsuperÚ__init__)ÚselfÚonnx_quantizerÚ	onnx_node©Ú	__class__© ú©/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/onnxruntime/quantization/operators/lstm.pyr      s   zLSTMQuant.__init__c                    sþ  | j }|jdks
J ‚| j |jd ¡r| j |jd ¡s#tƒ  ¡  dS | jj}| |jd ¡}| |jd ¡}t	|j
ƒdksEt	|j
ƒdkrLtƒ  ¡  dS |j
\}}}|j
\}}	}
| j ¡ rs|j
d= |j
d= || |j
d< ||	 |j
d< | j |jd tjjd¡}| j |jd tjjd¡}| |d ¡}| |d ¡}tj |¡}tj |¡}t ||||f¡}t |||	|
f¡}t |d¡}t |d¡}tj ||d ¡}tj ||d ¡}| ||g¡ | |¡ | |¡ | |d ¡}| |d ¡}| |d ¡}| |d ¡}| j ¡ �r.||g|j
dd…< ||	g|j
dd…< ||g|j
dd…< ||	g|j
dd…< g }t	|jƒ}| |jd g¡ | |d |d g¡ | |dk�rU|jd ndg¡ | |d	k�re|jd	 ndg¡ | |d
k�ru|jd
 ndg¡ | |dk�r…|jd ndg¡ | |dk�r•|jd ndg¡ | |d |d |d |d g¡ i }|jD ]}|jdk�r¹�q¯| t|ƒ¡ �q¯t|d< |j�sÌdn|jd }tjj d||j!|fi |¤Ž}| jj" #|¡ | j $|jd ¡}|du�rý| jj" #|¡ dS dS )zä
        parameter node: LSTM node.
        parameter new_nodes_list: List of new nodes created before processing this node.
        return: a list of nodes in topological order that represents quantized Attention node.
        ÚLSTMr   r   Né   r   )r   r   r   Ú é   é   é   é   ÚlayoutÚdomainÚ_quantÚDynamicQuantizeLSTM)%ÚnodeÚop_typeÚ	quantizerÚis_valid_quantize_weightÚinputr
   ÚquantizeÚmodelÚget_initializerÚlenÚdimsÚis_per_channelÚquantize_weight_per_channelÚ
onnx_protoÚTensorProtoÚINT8ÚonnxÚnumpy_helperÚto_arrayÚnumpyÚreshapeÚ	transposeÚ
from_arrayÚremove_initializersÚadd_initializerÚextendÚ	attributeÚnameÚupdater   r   ÚhelperÚ	make_nodeÚoutputÚ	new_nodesÚappendÚ_dequantize_value)r   r   r$   ÚWÚRÚ	W_num_dirÚW_4_hidden_sizeÚW_input_sizeÚ	R_num_dirÚR_4_hidden_sizeÚR_hidden_sizeÚquant_input_weight_tupleÚquant_recurrent_weight_tupleÚW_quant_weightÚR_quant_weightÚW_quant_arrayÚR_quant_arrayÚW_quant_tranposedÚR_quant_tranposedÚ
W_quant_zpÚ
R_quant_zpÚW_quant_scaleÚR_quant_scaleÚinputsÚ	input_lenÚkwargsr7   Úquant_lstm_nameÚquant_lstm_nodeÚdequantize_noder   r   r   r#      s¢   ÿ


ýý


     üÿ	

ÿzLSTMQuant.quantize)Ú__name__Ú
__module__Ú__qualname__r   r#   Ú__classcell__r   r   r   r   r	      s    r	   )r0   r-   r   r*   Úquant_utilsr   r   r   Úbase_operatorr   r	   r   r   r   r   Ú<module>   s    