o
    ;ήc                     @   s   d dl Z d dlZd dlmZ ddlmZmZmZmZm	Z	 ddl
mZ ddlmZ G dd	 d	eZ	 G d
d deZ	 G dd deZG dd deZdS )    N)onnx_pb   )TENSOR_NAME_QUANT_SUFFIXQuantizedValueQuantizedValueTypefind_by_nameget_mul_node   )QuantOperatorBase)QDQOperatorBasec                       $   e Zd Z fddZdd Z  ZS )	QOpMatMulc                       t  || d S Nsuper__init__selfonnx_quantizer	onnx_node	__class__ P/tmp/pip-target-vg8gfxp4/lib/python/onnxruntime/quantization/operators/matmul.pyr         zQOpMatMul.__init__c                 C   sz   | j | js	dS | j | jjd s| j | jjd sdS | j jr;| j | jjd s;td| j j	| jj
 dS dS )NFr	   r   z+Ignore MatMul due to non constant B: {}[{}]T)	quantizershould_quantize_nodenodeis_float_tensorinputq_matmul_const_b_onlyfind_initializer_in_pathprintformatgraph_scopename)r   r   r   r   should_quantize   s   zQOpMatMul.should_quantize)__name__
__module____qualname__r   r'   __classcell__r   r   r   r   r          r   c                       r   )MatMulIntegerc                    r   r   r   r   r   r   r   r   &   r   zMatMulInteger.__init__c                 C   s  | j }|jdks
J | j|dg\}}}}| jj|dgddd\}}}}	|| || || ||	 |jd d }
|jdkrJ|jd nd}tj	
d	|| |
g|}|| |
d
 }tj	j
d|
g|g|
d tjjd}|| t|dksJ |dkr|d n|d d |d  d }t|| jj}|d u rt||d |}|| |jd }d}|dkr|d }|t||g|jd | | j j|7  _d S )NMatMulr   r	   Treduce_rangeop_level_per_channel_output_quantized _quantr-   _cast_outputCast_cast)tor   _scales_mul__mulz:0_output_scale_mul)r   op_typer   quantize_activationquantize_weightextendoutputr&   onnxhelper	make_nodeappend
onnx_protoTensorProtoFLOATlenr   	new_nodesr   )r   r   quantized_input_nameszero_point_namesscale_namesnodesquantized_input_names_weightzero_point_names_weightscale_names_weightnodes_weightmatmul_integer_outputmatmul_integer_namematmul_integer_nodecast_op_output	cast_nodescales_mul_opscales_mul_nodescales_mul_op_outputoutput_scale_mul_opr   r   r   quantize)   sr   








zMatMulInteger.quantizer(   r)   r*   r   r\   r+   r   r   r   r   r-   %   r,   r-   c                       s(   e Zd Z fddZ fddZ  ZS )QLinearMatMulc                    r   r   r   r   r   r   r   r   x   r   zQLinearMatMul.__init__c                    s  | j }|jdks
J | j|dg\}}}}| jj|dgddd\}}}}	|| || || ||	 | j|jd \}
}}}}|
rM|d u rRt 	 S |jd t
 }|jdkrc|jd nd}g }||d  ||d  ||d  ||d  ||d  ||d  || || tjd||g|}|| t|jd |||tj}|| jj|jd < | j j|7  _d S )	Nr.   r   r	   Tr/   r3   r4   r^   )r   r=   r   r>   r?   r@   _get_quantization_paramsrA   r   r\   r   r&   rE   rB   rC   rD   r   r   Inputquantized_value_maprJ   )r   r   rK   rL   rM   rN   rO   rP   rQ   rR   
data_foundoutput_scale_nameoutput_zp_namer:   qlinear_matmul_outputqlinear_matmul_nameqlinear_matmul_inputsqlinear_matmul_nodeq_outputr   r   r   r\   {   sl   







zQLinearMatMul.quantizer]   r   r   r   r   r^   w   s    r^   c                       r   )	QDQMatMulc                    r   r   r   r   r   r   r   r      r   zQDQMatMul.__init__c                 C   s   | j }|jdks
J | jr|j}nt|j|j}|D ]'}| j r<t	|| jj
 r<| jj|jd}| j|| q| j| qd S )Nr.   r	   )r   r=   disable_qdq_for_node_outputr    	itertoolschainrA   r   is_per_channelr   modelinitializer'qdq_op_type_per_channel_support_to_axisget"quantize_weight_tensor_per_channelquantize_activation_tensor)r   r   nodes_to_iteratetensor_namechannel_axisr   r   r   r\      s   zQDQMatMul.quantizer]   r   r   r   r   rj      r,   rj   )rl   rB   r   rF   quant_utilsr   r   r   r   r   base_operatorr
   qdq_base_operatorr   r   r-   r^   rj   r   r   r   r   <module>   s    MI