o
    ¢Äi
  ã                   @   sF   d dl Z d dl mZ ddlmZmZ ddlmZ 	 G dd„ deƒZdS )	é    N)Úonnx_pbé   )Úattribute_to_kwargÚ	ms_domainé   )ÚQuantOperatorBasec                       s0   e Zd Z‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )ÚAttentionQuantc                    s   t ƒ  ||¡ d S ©N)ÚsuperÚ__init__)ÚselfZonnx_quantizerZ	onnx_node©Ú	__class__© úf/home/kim/smarthome/.venv/lib/python3.10/site-packages/onnxruntime/quantization/operators/attention.pyr      s   zAttentionQuant.__init__c                 C   s   | j  | j¡S r	   )Ú	quantizerZshould_quantize_nodeÚnode)r   r   r   r   Úshould_quantize   s   zAttentionQuant.should_quantizec                    s˜  | j }|jdks
J ‚|jD ]}|jdkrtƒ  ¡   S q| j |dg¡\}}}}| jj|dgddd\}}}	}
| 	|¡ | 	|¡ | 	|	¡ | 	|
¡ |du rTtƒ  ¡ S |jsYdn|jd	 }g }| 	|¡ | 	|j
d
 g¡ | 	|¡ | 	t|j
ƒdkr|j
d ndg¡ | 	|¡ | 	t|j
ƒdkr˜|j
d ndg¡ i }|jD ]	}| t|ƒ¡ q¡t|d< tjjd||j|fi |¤Ž}| |¡ | j j|7  _dS )zé
        parameter node: Attention 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	AttentionZqkv_hidden_sizesr   r   T)Zreduce_rangeZop_level_per_channelNÚ Z_quantr   é   é   ÚdomainZ
QAttention)r   Zop_typeÚ	attributeÚnamer
   Úquantizer   Zquantize_activationZquantize_weightÚextendÚinputÚlenÚupdater   r   ÚonnxÚhelperZ	make_nodeÚoutputÚappendZ	new_nodes)r   r   ÚattrZquantized_input_namesZzero_point_namesZscale_namesÚnodesZquantized_input_names_weightZzero_point_names_weightZscale_names_weightZnodes_weightZqattention_nameZinputsÚkwargsr   Zqattention_noder   r   r   r      sN   

ÿûû






$
$

zAttentionQuant.quantize)Ú__name__Ú
__module__Ú__qualname__r   r   r   Ú__classcell__r   r   r   r   r      s    r   )	r   r   Z
onnx_protoZquant_utilsr   r   Zbase_operatorr   r   r   r   r   r   Ú<module>   s    