o
    °ƒðj×&  ã                   @   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
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dS )é    N)Úonnx_pbé   )ÚTENSOR_NAME_QUANT_SUFFIXÚQuantizedValueÚQuantizedValueTypeÚattribute_to_kwargÚfind_by_nameÚget_mul_nodeé   )ÚQuantOperatorBase)ÚQDQOperatorBasec                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚConvIntegerc                    ó   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/conv.pyr      ó   zConvInteger.__init__c                 C   sô   | j }| jj}t|jd | ¡ ƒ}|du r td|jd › d�ƒ‚|jd }|jd }|d }|d }	tj	t
|jƒtjd	�}
d
|
d< tj |tjjt
|jƒg|
¡}| |¡ tj d||g|	g¡}| |¡ tj d||	g|g|d ¡}| |¡ dS )a°  
        Given a node, this function handles bias add by adding a "reshape" node on bias and an "add" node
            parameter nodes: new nodes would be appended into nodes
            parameter node: current node (Conv)
            parameter scaled_output: output of quant conv without bias
            parameter output: output of Conv
            parameter bias_name: bias of Conv
            return: the name of output
        r
   Nz	Expected z to be an initializerr   r   Ú_bias_reshape_shapeÚ_bias_reshape_output)ÚdtypeéÿÿÿÿÚReshapeÚAddÚ	_bias_add)ÚnodeÚ	quantizerÚmodelr   ÚinputÚinitializerÚ
ValueErrorÚoutputÚnpÚonesÚlenÚdimsÚint64ÚonnxÚhelperÚmake_tensorÚ
onnx_protoÚTensorProtoÚINT64Úadd_initializerÚ	make_nodeÚappend)r   ÚnodesÚscaled_outputr#   r%   Úweightr)   Úreshape_input_dataÚreshape_input_shapeÚreshape_outputÚshapeÚ
init_shapeÚreshape_nodeÚadd_noder   r   r   Úadd_bias   s&   


ÿ

zConvInteger.add_biasc                 C   s  | j }|jdks
J ‚| j |dg¡\}}}}| jj|dg| jjd�\}}}}	| |¡ | |¡ | |¡ | |	¡ |jd d }
|jrI|jd nd}i }|j	D ]	}| 
t|ƒ¡ qPtjjd|| |
g|fi |¤Ž}| |¡ | jj|jd 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 }t|jƒdk}|sØ|jd n|jd d }|rå|d nd}| t||g||ƒ¡ |rú|  ||¡ | j j|7  _d S )NÚConvr   r
   ©Úreduce_rangeÚ_output_quantizedÚ_quantÚ r   T)Ú	mandatoryÚ_cast_outputÚCastÚ_cast)Útor   Ú_scales_mulÚ_Ú_mulz:0é   Úquant_scaled_outputÚ_output_scale_mul)r#   Úop_typer$   Úquantize_activationÚquantize_weightrE   Úextendr)   ÚnameÚ	attributeÚupdater   r/   r0   r6   r7   Úget_tensor_typer,   r   Ú	new_nodesr	   r&   rB   )r   r#   Úquantized_input_namesÚzero_point_namesÚscale_namesr8   Úquantized_input_names_weightÚzero_point_names_weightÚscale_names_weightÚnodes_weightÚconv_integer_outputÚconv_integer_nameÚkwargsrY   Úconv_integer_nodeÚ	onnx_typeÚcast_op_outputÚ	cast_nodeÚscales_mul_opÚscales_mul_nodeÚscales_mul_op_outputÚhas_biasÚscaled_output_nameÚoutput_scale_mul_opr   r   r   Úquantize:   sz   ûû




ÿÿ
û



ýÿzConvInteger.quantize)Ú__name__Ú
__module__Ú__qualname__r   rB   rq   Ú__classcell__r   r   r   r   r      s    %r   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚQLinearConvc                    r   r   r   r   r   r   r   r   Š   r   zQLinearConv.__init__c                    s”  | j }|jdks
J ‚| j |jd ¡\}}}}}| j |jd ¡rU| j ¡ rU| j |dg¡\}}}}	| j 	|jd t
jjd¡}
| |
d ¡ | |
d ¡ | |
d ¡ n0| j |dg¡\}}}}	| jj|dg| jjd�\}}}}| |¡ | |¡ | |¡ |	 |¡ |r‹|d u r�tƒ  ¡ S d}d}t|jƒdkrº| jjt
jjkr§td	ƒ‚| j |jd |jd |jd ¡}d
}|jd t }|jrÉ|jd nd}i }|jD ]	}| t|ƒ¡ qÐg }| |d ¡ | |d ¡ | |d ¡ | |d ¡ | |d ¡ | |d ¡ | |¡ | |¡ |�r| |¡ tjjd||g|fi |¤Ž}|	 |¡ t |jd |||t!j"ƒ}|| jj#|jd < | j j$|	7  _$d S )NrC   r   r
   r   rD   rH   FrQ   z@Quantization to FLOAT8E4M3FN for operator Conv is not supported.TrG   rv   )%r#   rT   r$   Ú_get_quantization_paramsr)   Úis_input_a_initializerr&   Úis_per_channelrU   Úquantize_weight_per_channelr2   r3   ÚINT8r7   rV   rE   rW   r   rq   r,   Úweight_qTypeÚFLOAT8E4M3FNÚRuntimeErrorÚquantize_bias_staticr   rX   rY   rZ   r   r/   r0   r6   r   r   ÚInputÚquantized_value_mapr\   )r   r#   Ú
data_foundÚoutput_scale_nameÚoutput_zp_namerO   r]   r^   r_   r8   Úquant_weight_tupler`   ra   rb   rc   Úquantized_bias_nameÚbias_presentÚqlinear_conv_outputÚqlinear_conv_namerf   rY   Úqlinear_conv_inputsÚqlinear_conv_nodeÚq_outputr   r   r   rq   �   s    úûýûû




"




ÿÿ
ûzQLinearConv.quantize©rr   rs   rt   r   rq   ru   r   r   r   r   rv   ‰   s    rv   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQDQConvc                    r   r   r   r   r   r   r   r   ð   r   zQDQConv.__init__c                 C   sØ   | j }|jdks|jdksJ ‚| j |jd ¡ | js$| j |jd ¡ | jj|jd |jdkr2dndd�\}}|rE| j |jd |¡ n	| j 	|jd ¡ t
|jƒdkrj| j |j|jd |jd |jd ¡ d S d S )NrC   ÚConvTransposer   r
   )Údefault_axisrQ   r   )r#   rT   r$   Úquantize_activation_tensorr&   Údisable_qdq_for_node_outputr)   Úis_tensor_per_channelÚ"quantize_weight_tensor_per_channelÚquantize_weight_tensorr,   Úquantize_bias_tensorrX   )r   r#   Úis_weight_per_channelÚweight_axisr   r   r   rq   ó   s   
ÿ*ÿzQDQConv.quantizer�   r   r   r   r   rŽ   ï   s    rŽ   )Únumpyr*   r/   r   r2   Úquant_utilsr   r   r   r   r   r	   Úbase_operatorr   Úqdq_base_operatorr   r   rv   rŽ   r   r   r   r   Ú<module>   s     xf