o
    i&                     @   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__selfZonnx_quantizerZ	onnx_node	__class__ a/home/kim/smarthome/.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   Z_bias_reshape_shapeZ_bias_reshape_output)ZdtypeZReshapeAddZ	_bias_add)node	quantizermodelr   inputZinitializer
ValueErroroutputnpZoneslendimsZint64onnxhelperZmake_tensor
onnx_protoTensorProtoZINT64Zadd_initializer	make_nodeappend)r   nodesZscaled_outputr   r   weightr!   Zreshape_input_dataZreshape_input_shapeZreshape_outputshapeZ
init_shapeZ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sJ |r|d }n|d d |d  d }t|| jj}|d u rt||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_rangeZ_output_quantized_quant r   T)	mandatoryZ_cast_outputZCastZ_cast)tor   Z_scales_mul__mulz:0   Zquant_scaled_outputZ_output_scale_mul)r   op_typer   quantize_activationquantize_weightr2   extendr!   name	attributeupdater   r%   r&   r)   r*   Zget_tensor_typer#   r   	new_nodesr	   r   r/   )r   r   quantized_input_nameszero_point_namesscale_namesr+   quantized_input_names_weightzero_point_names_weightscale_names_weightnodes_weightZconv_integer_outputZconv_integer_namekwargsr?   Zconv_integer_nodeZ	onnx_typeZcast_op_outputZ	cast_nodeZscales_mul_opZscales_mul_nodeZscales_mul_op_outputZhas_biasZscaled_output_nameZoutput_scale_mul_opr   r   r   quantize:   sz   









zConvInteger.quantize)__name__
__module____qualname__r   r/   rJ   __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 rt  S d}d}t|jdkr| jjt
jjkrtd	| j|jd |jd |jd }d
}|jd t }|jr|jd nd}i }|jD ]	}|t| qg }||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 )Nr0   r   r
   r   r1   r4   Fr9   z@Quantization to FLOAT8E4M3FN for operator Conv is not supported.Tr3   rO   )%r   r:   r   Z_get_quantization_paramsr!   Zis_input_a_initializerr   Zis_per_channelr;   Zquantize_weight_per_channelr'   r(   ZINT8r*   r<   r2   r=   r   rJ   r#   Zweight_qTypeZFLOAT8E4M3FNRuntimeErrorZquantize_bias_staticr   r>   r?   r@   r   r%   r&   r)   r   r   ZInputZquantized_value_maprA   )r   r   Z
data_foundZoutput_scale_nameZoutput_zp_namer7   rB   rC   rD   r+   Zquant_weight_tuplerE   rF   rG   rH   Zquantized_bias_nameZbias_presentZqlinear_conv_outputZqlinear_conv_namerI   r?   Zqlinear_conv_inputsZqlinear_conv_nodeZq_outputr   r   r   rJ      s   




"





zQLinearConv.quantizerK   rL   rM   r   rJ   rN   r   r   r   r   rO      s    rO   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 )Nr0   ZConvTransposer   r
   )Zdefault_axisr9   r   )r   r:   r   Zquantize_activation_tensorr   Zdisable_qdq_for_node_outputr!   Zis_tensor_per_channelZ"quantize_weight_tensor_per_channelZquantize_weight_tensorr#   Zquantize_bias_tensorr>   )r   r   Zis_weight_per_channelZweight_axisr   r   r   rJ      s   
*zQDQConv.quantizerQ   r   r   r   r   rR      s    rR   )numpyr"   r%   r   r'   Zquant_utilsr   r   r   r   r   r	   Zbase_operatorr   Zqdq_base_operatorr   r   rO   rR   r   r   r   r   <module>   s     xf