
     Ng&                         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	e          Z G d
 de          Z G 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                   *     e Zd Z fdZd Zd Z xZS )ConvIntegerc                 L    t                                          ||           d S Nsuper__init__selfonnx_quantizer	onnx_node	__class__s      c/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnxruntime/quantization/operators/conv.pyr   zConvInteger.__init__   #    33333    c                    | j         }| j        j        }t          |j        d         |                                          }|t          d|j        d          d          |j        d         }|j        d         }|dz   }|dz   }	t          j	        t          |j                  t          j        	          }
d
|
d<   t          j                            |t           j        j        t          |j                  g|
          }|                    |           t          j                            d||g|	g          }|                    |           t          j                            d||	g|g|dz             }|                    |           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_nodes                 r   add_biaszConvInteger.add_bias   sy    y$djmU->->-@-@AA>MAMMMNNN Q!Z]$'<<"88V[))"(;;;a[,,!7!=FK@P@P?QSX
 

 	j))){,,Y9KM`8adrcstt\""" ;((0ORXQY[ado[oppXr   c                    | j         }|j        dk    sJ | j                            |dg          \  }}}}| j                            |dg| j        j                  \  }}}}	|                    |           |                    |           |                    |           |                    |	           |j        d         dz   }
|j        r
|j        dz   nd}i }|j	        D ]$}|
                    t          |                     %t          j        j        d||z   |
g|fi |}|                    |           | j                            |j        d         d	
          }|
dz   }t          j                            d|
g|g|
dz   |          }|                    |           t#          |          dk    sJ |r|dz   }n|d         dz   |d         z   dz   }t%          || j        j                  }|)t)          ||dz   |          }|                    |           |j        d         }t#          |j                  dk    }|s|j        d         n|j        d         dz   }|r|dz   nd}|                    t)          ||g||                     |r|                     ||           | j        xj        |z  c_        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_weightrH   extendr+   name	attributeupdater   r1   r2   r8   r9   get_tensor_typer.   r	   	new_nodesr
   r(   rD   )r   r%   quantized_input_nameszero_point_namesscale_namesr:   quantized_input_names_weightzero_point_names_weightscale_names_weightnodes_weightconv_integer_outputconv_integer_namekwargsr\   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_ops                           r   quantizezConvInteger.quantize:   sD   y|v%%%% N..taS99	
! N**4!4>C^*__	
(#$$%ABBB 7888-...\""""k!n/BB48IEDI002 	9 	9IMM,Y778888 K103CCFYEZ\m
 
qw
 
 	&''' N224;q>T2RR	,~=K)) !') * 
 
	 	Y ;1$$$$ 	K-=MM'NS0;q>AFJM&}dn6NOO"*;8Lm\\OLL))).5a8tz??a'3;gT[^^QRgAg J[b/2EEE`b!56"# 	
 	
 	
  	5MM%!3444  E)    r   )__name__
__module____qualname__r   rD   rt   __classcell__r   s   @r   r   r      s^        4 4 4 4 4# # #JL* L* L* L* L* L* L*r   r   c                   (     e Zd Z fdZ fdZ xZS )QLinearConvc                 L    t                                          ||           d S r   r   r   s      r   r   zQLinearConv.__init__   r   r   c                 v   | j         }|j        dk    sJ | j                            |j        d                   \  }}}}}| j                            |j        d                   r| j                                        r| j                            |dg          \  }}}}	| j        	                    |j        d         t          j        j        d          }
|                    |
d                    |                    |
d                    |                    |
d                    n| j                            |dg          \  }}}}	| j                            |dg| j        j                  \  }}}}|                    |           |                    |           |                    |           |	                    |           |r| t#                                                      S d}d}t'          |j                  dk    rm| j        j        t          j        j        k    rt-          d	          | j                            |j        d         |j        d         |j        d                   }d
}|j        d         t0          z   }|j        r
|j        dz   nd}i }|j        D ]$}|                    t9          |                     %g }|                    |d                    |                    |d                    |                    |d                    |                    |d                    |                    |d                    |                    |d                    |                    |           |                    |           |r|                    |           t;          j        j        d||g|fi |}|	                    |           tA          |j        d         |||tB          j"                  }|| j        j#        |j        d         <   | j        xj$        |	z  c_$        d S )NrF   r   r   r   rG   rK   FrT   z@Quantization to FLOAT8E4M3FN for operator Conv is not supported.TrJ   r{   )%r%   rW   r&   _get_quantization_paramsr+   is_input_a_initializerr(   is_per_channelrX   quantize_weight_per_channelr4   r5   INT8r9   rY   rH   rZ   r   rt   r.   weight_qTypeFLOAT8E4M3FNRuntimeErrorquantize_bias_staticr   r[   r\   r]   r   r1   r2   r8   r   r   Inputquantized_value_mapr_   )r   r%   
data_foundoutput_scale_nameoutput_zp_namerR   r`   ra   rb   r:   quant_weight_tuplerc   rd   re   rf   quantized_bias_namebias_presentqlinear_conv_outputqlinear_conv_nameri   r\   qlinear_conv_inputsqlinear_conv_nodeq_outputr   s                           r   rt   zQLinearConv.quantize   sa   y|v%%%% N33DKNCC	
 >00A?? 	'DNDaDaDcDc 	' 224!==% !%!K!K
1z5:A" " "(();A)>???##$6q$9:::1!45555 224!==%  ..taSt~Gb.cc,'"!(()EFFF##$;<<<1222LL&&& 	&2:77##%%% tz??a~*j.D.QQQ"#efff"&."E"EdjQRmUYU_`aUbdhdnopdq"r"rL"k!n/GG48IEDI002 	9 	9IMM,Y778888 ""#8#;<<<"";q>222""#3A#6777""#8#;<<<"";q>222""#3A#6777 	""#4555"">222 	<&&':;;; K1.1D0EGX
 
\b
 
 	&''' "KN$
 
 >F*4;q>:  E)    r   ru   rv   rw   r   rt   rx   ry   s   @r   r{   r{      sZ        4 4 4 4 4]* ]* ]* ]* ]* ]* ]* ]* ]*r   r{   c                   $     e Zd Z fdZd Z xZS )QDQConvc                 L    t                                          ||           d S r   r   r   s      r   r   zQDQConv.__init__   r   r   c                    | j         }|j        dk    s|j        dk    sJ | j                            |j        d                    | j        s%| j                            |j        d                    | j                            |j        d         |j        dk    rdnd          \  }}|r'| j                            |j        d         |           n%| j        	                    |j        d                    t          |j                  dk    rE| j                            |j        |j        d         |j        d         |j        d                    d S d S )NrF   ConvTransposer   r   )default_axisrT   r   )r%   rW   r&   quantize_activation_tensorr(   disable_qdq_for_node_outputr+   is_tensor_per_channel"quantize_weight_tensor_per_channelquantize_weight_tensorr.   quantize_bias_tensorr[   )r   r%   is_weight_per_channelweight_axiss       r   rt   zQDQConv.quantize   sH   y|v%%)H)H)H)H11$*Q-@@@/ 	FN55dk!nEEE-1^-Q-QJqMT\V-C-C .R .
 .
*{ ! 	AN==djm[YYYYN11$*Q-@@@tz??aN//	4:a=$*UV-Y]YcdeYfggggg  r   r   ry   s   @r   r   r      sN        4 4 4 4 4h h h h h h hr   r   )numpyr,   r1   r   r4   quant_utilsr   r   r   r   r	   r
   base_operatorr   qdq_base_operatorr   r   r{   r    r   r   <module>r      sH        & & & & & &                - , , , , , . . . . . .u* u* u* u* u*# u* u* u*pa* a* a* a* a*# a* a* a*Hh h h h ho h h h h hr   