o
    °ƒðjL   ã                   @   s    d dl Z 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__© ú«/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/onnxruntime/quantization/operators/matmul.pyr      ó   zQOpMatMul.__init__c                 C   s®   | j  | j¡st d| jj› d�¡ dS | j  | jjd ¡s5| j  | jjd ¡s5t d| jj› d�¡ dS | j j	rU| j  
| jjd ¡sUt d| j j› d| jj› d�¡ dS d	S )
NzIgnore MatMul ú]Fr	   r   z&Ignore MatMul due to non float inputs z%Ignore MatMul due to non constant B: ú[T)Ú	quantizerÚshould_quantize_nodeÚnodeÚloggingÚdebugÚnameÚis_float_tensorÚinputÚinfoÚq_matmul_const_b_onlyÚfind_initializer_in_pathÚgraph_scope)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rH|jd nd}tj	 
d	|| |
g|¡}| |¡ |
d
 }| jj|jd dd�}tj	j
d|
g|g|
d |d�}| |¡ t|ƒdks†J ‚|rŒ|d n|d d |d  d }t|| jjƒ}|d u r°t||d |ƒ}| |¡ |jd }d}|r½|d }| t||g|jd |ƒ¡ | j j|7  _d S )NÚMatMulr   r	   T©Úreduce_rangeÚop_level_per_channelÚ_output_quantizedÚ_quantÚ r0   Ú_cast_output)Ú	mandatoryÚCastÚ_cast)Útor   Ú_scales_mulÚ_Ú_mulz:0Ú_output_scale_mul)r    Úop_typer   Úquantize_activationÚquantize_weightÚextendÚoutputr#   ÚonnxÚhelperÚ	make_nodeÚappendÚget_tensor_typeÚ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ÚotypeÚ	cast_nodeÚscales_mul_opÚscales_mul_nodeÚscales_mul_op_outputÚoutput_scale_mul_opr   r   r   Úquantize,   st   ûû



ü
û

ÿý

ýÿzMatMulInteger.quantize©r+   r,   r-   r   r_   r.   r   r   r   r   r0   (   r/   r0   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   |   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ra|jd nd}g }| |d ¡ | |d ¡ | |d ¡ | |d ¡ | |d ¡ | |d ¡ | |¡ | |¡ | jjtjjtjjtjjtjjhv r­dnd}tjjd	||g||d
�}| |¡ t|jd |||tjƒ}|| jj|jd < | j j|7  _d S )Nr1   r   r	   Tr2   r6   r7   zcom.microsoftra   )Údomain)r    rA   r   rB   rC   rD   Ú_get_quantization_paramsrE   r   r_   r   r#   rI   Úweight_qTypeÚ
onnx_protoÚTensorProtoÚFLOAT8E4M3FNÚFLOAT8E4M3FNUZÚ
FLOAT8E5M2ÚFLOAT8E5M2FNUZrF   rG   rH   r   r   ÚInputÚquantized_value_maprL   )r   r    rM   rN   rO   rP   rQ   rR   rS   rT   Ú
data_foundÚoutput_scale_nameÚoutput_zp_namer>   Úqlinear_matmul_outputÚqlinear_matmul_nameÚqlinear_matmul_inputsrb   Úqlinear_matmul_nodeÚq_outputr   r   r   r_      s‚   ûû



ú


üÿÿ÷û
ûzQLinearMatMul.quantizer`   r   r   r   r   ra   {   s    ra   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 ].}t|| jj	 
¡ ƒrC| jj|d|jd�\}}|r<| j ||¡ q| j |¡ q| j |¡ qd S )Nr1   r	   )Údefault_axisrA   )r    rA   Údisable_qdq_for_node_outputr%   Ú	itertoolsÚchainrE   r   r   ÚmodelÚinitializerÚis_tensor_per_channelÚ"quantize_weight_tensor_per_channelÚquantize_weight_tensorÚquantize_activation_tensor)r   r    Únodes_to_iterateÚtensor_nameÚis_per_channelÚchannel_axisr   r   r   r_   Ô   s   
ÿözQDQMatMul.quantizer`   r   r   r   r   ru   Ð   r/   ru   )rx   r!   rF   r   re   Úquant_utilsr   r   r   r   r   Úbase_operatorr
   Úqdq_base_operatorr   r   r0   ra   ru   r   r   r   r   Ú<module>   s    NU