
     Ng                     `    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e	          Z
dS )	    N)onnx_pb   )	QuantTypeattribute_to_kwarg	ms_domain   )QuantOperatorBasec                   (     e Zd Z fdZ fdZ xZS )	LSTMQuantc                 L    t                                          ||           d S )N)super__init__)selfonnx_quantizer	onnx_node	__class__s      c/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnxruntime/quantization/operators/lstm.pyr   zLSTMQuant.__init__   s#    33333    c                 h   | 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    st          |j
                  dk    r"t                                                       dS |j
        \  }}}|j
        \  }}	}
| j                                        r*|j
        d= |j
        d= ||z  |j
        d<   ||	z  |j
        d<   | j                            |j        d         t          j        j        d          }| j                            |j        d         t          j        j        d          }|                    |d                   }|                    |d                   }t           j                            |          }t           j                            |          }t'          j        ||||f          }t'          j        |||	|
f          }t'          j        |d          }t'          j        |d          }t           j                            ||d                   }t           j                            ||d                   }|                    ||g           |                    |           |                    |           |                    |d                   }|                    |d                   }|                    |d                   }|                    |d                   }| j                                        r8||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 ]0}|j        dk    r|                    t;          |                     1t<          |d<   |j        sdn	|j        dz   }t!          j        j         d||j!        |fi |}| j        j"        #                    |           | j        $                    |j        d                   }|!| 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kwargsr:   quant_lstm_namequant_lstm_nodedequantize_noder   s                                 r   r&   zLSTMQuant.quantize   s    y|v%%%%~66tz!}EE 	T^MtMtJqMN
 N
 	 GGF$!!$*Q-00!!$*Q-00qv;;!s16{{a//GGF56V2O\67f3O]>((** 	4q	q	!O3AF1I!O3AF1I#'>#M#MJqM:16$
 $
  (,~'Q'QJqM:16(
 (
$ ../G/JKK../KA/NOO)22>BB)22>BBmiR^5_``miR_5`aayAAyAA -88H`abHcdd -88HdefHghh!!>>"BCCC/000/000**+CA+FGG
**+G+JKK
--.Fq.IJJ--.J1.MNN>((** 	A"+_!=JOAAA"+_!=JOAAA%.$@Mqqq!%.$@Mqqq!
OO	tz!}o&&&/24PQR4STUUU	Atz!}}2>???	Atz!}}2>???	Atz!}}2>???	Atz!}}2>???	Atz!}}2>???(+(+,Q/,Q/		
 	
 	
  	9 	9I~))MM,Y778888$x$(IG""49x3G+/0Evt{\kvvouvv ''888.::4:a=II&N$++O<<<<< '&r   )__name__
__module____qualname__r   r&   __classcell__)r   s   @r   r   r      sZ        4 4 4 4 4d= d= d= d= d= d= d= d= d=r   r   )r3   r0   r   r-   quant_utilsr   r   r   base_operatorr	   r    r   r   <module>rd      s      & & & & & & B B B B B B B B B B , , , , , ,
h= h= h= h= h=! h= h= h= h= h=r   