o
    i                     @   s   d dl Z d dlZd dlZddlmZ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d	 Zd
d Zdd ZG dd deZG dd deZdS )    N   )TENSOR_NAME_QUANT_SUFFIXQuantizedValueQuantizedValueTypeattribute_to_kwargfind_by_nameget_mul_node	ms_domain   )QuantOperatorBase)	QOpMatMul)QDQOperatorBasec                 C   s,   dd | j D }|rtj|d dkS dS )Nc                 S      g | ]	}|j d kr|qS )ZtransBname.0attr r   a/home/kim/smarthome/.venv/lib/python3.10/site-packages/onnxruntime/quantization/operators/gemm.py
<listcomp>       z#is_B_transposed.<locals>.<listcomp>r   F	attributeonnxhelperZget_attribute_value)	gemm_nodeZtransB_attributer   r   r   is_B_transposed   s   r   c                 C   s(   dd | j D }|rtj|d S dS )Nc                 S   r   betar   r   r   r   r   r      r   zget_beta.<locals>.<listcomp>r         ?r   r   Zbeta_attributer   r   r   get_beta   s   r"   c                 C   s"   dd | j D }|rd|d _dS )Nc                 S   r   r   r   r   r   r   r   r   %   r   z$set_default_beta.<locals>.<listcomp>r    r   )r   fr!   r   r   r   set_default_beta$   s   
r$   c                       s(   e Zd Z fddZ fddZ  ZS )QLinearGemmc                       t  || d S Nsuper__init__selfZonnx_quantizerZ	onnx_node	__class__r   r   r*   -      zQLinearGemm.__init__c              	      s  | j }|jdks
J | j|jd \}}}}}| j|jd r[| j r[| j|dg\}}}}	| j	|jd | jj
t|rBdnd}
||
d  ||
d  ||
d  n0| j|dg\}}}}	| jj|dg| jjd\}}}}|| || || |	| |r|d u rt  S d}t|jdkr| j|jd st  S | j|jd |jd |jd t| j }|jd t }|jr|jd nd}i }|jD ]}|jd	kr|t| qt|d
< g }tdD ]}||| || || g q||||g tjjd||g|fi |}|	| t|jd |||t j!|j| jj
d}|| jj"|jd < | j j#|	7  _#d S )NGemmr   r
   r   )reduce_range    Z_quantr   domainZQGemm)Z	node_typeZ
node_qtype)$nodeop_type	quantizerZ_get_quantization_paramsoutputis_input_a_initializerinputZis_per_channelZquantize_activationZquantize_weight_per_channelZweight_qTyper   appendZquantize_weightr1   extendr)   quantizelenZquantize_bias_staticr"   r   r   r   updater   r	   ranger   r   Z	make_noder   r   ZInputZquantized_value_mapZ	new_nodes)r,   r5   Z
data_foundZoutput_scale_nameZoutput_zp_name_Zquantized_input_namesZzero_point_namesZscale_namesnodesZquant_weight_tupleZquantized_input_names_weightZzero_point_names_weightZscale_names_weightZnodes_weightZquantized_bias_nameZqgemm_outputZ
qgemm_namekwargsr   Zqgemm_inputsiZ
qgemm_nodeZq_outputr-   r   r   r=   0   s   





 


	zQLinearGemm.quantize__name__
__module____qualname__r*   r=   __classcell__r   r   r-   r   r%   ,   s    r%   c                       s$   e Zd Z fddZdd Z  ZS )QDQGemmc                    r&   r'   r(   r+   r-   r   r   r*      r/   zQDQGemm.__init__c                 C   s
  | j }|jdks
J | j|jd  | js| j|jd  | jj|jd t|r,dndd\}}|r?| j	|jd | n	| j
|jd  t|jdkr| j|jd rv| j|j|jd |jd |jd t| j  t| j  d S td| j j d d S d S )	Nr0   r   r
   )Zdefault_axisr3   r   zBias of Gemm node 'zC' is not constant. Please exclude this node for better performance.)r5   r6   r7   Zquantize_activation_tensorr:   Zdisable_qdq_for_node_outputr8   Zis_tensor_per_channelr   Z"quantize_weight_tensor_per_channelZquantize_weight_tensorr>   r9   Zquantize_bias_tensorr   r"   r$   loggingwarning)r,   r5   Zis_weight_per_channelZweight_axisr   r   r   r=      s*   
$zQDQGemm.quantizerE   r   r   r-   r   rJ      s    rJ   )rK   numpynpr   Zquant_utilsr   r   r   r   r   r   r	   Zbase_operatorr   matmulr   Zqdq_base_operatorr   r   r"   r$   r%   rJ   r   r   r   r   <module>   s    $	c