U
    U?hË  ã                   @   sP   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__)ÚselfZonnx_quantizerZ	onnx_node©Ú	__class__© úY/var/www/html/venv/lib/python3.8/site-packages/onnxruntime/quantization/operators/lstm.pyr      s    zLSTMQuant.__init__c                    sö  | j }|jdkst‚| j |jd ¡r8| j |jd ¡sFtƒ  ¡  dS | jj}| 	|jd ¡}| 	|jd ¡}t
|jƒdksŠt
|jƒdkr˜tƒ  ¡  dS |j\}}}|j\}}	}
| j ¡ ræ|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rª|jd ndg¡ | |d	krÊ|jd	 ndg¡ | |d
krê|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rrq^| t|ƒ¡ q^t|d< |js˜dn|jd }tj j!d||j"|f|Ž}| jj# $|¡ | j %|jd ¡}|dk	rò| jj# $|¡ 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.
        ZLSTMr   r   Né   r   )r   r   r   Ú é   é   é   é   ZlayoutÚdomainZ_quantZDynamicQuantizeLSTM)&ÚnodeZop_typeÚAssertionErrorZ	quantizerZis_valid_quantize_weightÚinputr
   ÚquantizeÚmodelZget_initializerÚlenZdimsZis_per_channelZquantize_weight_per_channelÚ
onnx_protoZTensorProtoZINT8ÚonnxZnumpy_helperZto_arrayÚnumpyZreshapeZ	transposeZ
from_arrayZremove_initializersZadd_initializerÚextendÚ	attributeÚnameÚupdater   r   ÚhelperZ	make_nodeÚoutputZ	new_nodesÚappendZ_dequantize_value)r   r   r   ÚWÚRZ	W_num_dirZW_4_hidden_sizeZW_input_sizeZ	R_num_dirZR_4_hidden_sizeZR_hidden_sizeZquant_input_weight_tupleZquant_recurrent_weight_tupleZW_quant_weightZR_quant_weightZW_quant_arrayZR_quant_arrayZW_quant_tranposedZR_quant_tranposedZ
W_quant_zpZ
R_quant_zpZW_quant_scaleZR_quant_scaleÚinputsZ	input_lenÚkwargsr"   Zquant_lstm_nameZquant_lstm_nodeZdequantize_noder   r   r   r      s     ÿ


  ÿ  ÿ


     üÿ	

zLSTMQuant.quantize)Ú__name__Ú
__module__Ú__qualname__r   r   Ú__classcell__r   r   r   r   r	      s   r	   )r    r   r   r   Zquant_utilsr   r   r   Zbase_operatorr   r	   r   r   r   r   Ú<module>   s
   