o
    °ƒðjFÀ  ã                   @   sÀ   d dl Z d dlZd dlZd dlZd dlmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddl m!Z! G d	d
„ d
eƒZ"dS )é    N)Úonnx_pbé   )ÚBaseQuantizerÚQuantizationParams)Ú
TensorData)Ú	ONNXModel)ÚTENSOR_NAME_QUANT_SUFFIXÚQuantizationModeÚQuantizedValueÚQuantizedValueTypeÚ__producer__Ú__version__Úadd_infer_metadataÚattribute_to_kwargÚcompute_scale_zpÚcompute_scale_zp_float8Úfind_by_nameÚget_qmin_qmax_for_qTypeÚget_qrange_for_qTypeÚ	ms_domainÚquantize_onnx_initializerÚ&save_and_reload_model_with_shape_inferÚtensor_proto_to_array)ÚCreateOpQuantizerc                   @   sd  e Zd Z	dGdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dHdd„Zdd„ Zdd„ Zdd„ Zdd„ ZdIdd„Z	dJdd „Zd!d"„ Z	dGd#d$„Zd%ejd&ejd'ed(ejd)ed*eeejdB f fd+d,„Zd'ed-ejd*dfd.d/„ZdKd1d2„Zd3d4„ ZdHd5d6„Z			7	dLd8d9„Z 	:			7	dMd;d<„Z!dNd=d>„Z"	:	dOd?d@„Z#dAdB„ Z$dCdD„ Z%dEdF„ Z&dS )PÚONNXQuantizerNc                 C   sx  t  | |||||||	|
||¡ |sE| j ¡  t| jjƒ}dd„ |jjD ƒ| _| j dd„ |jj	D ƒ¡ | j dd„ |jj
D ƒ¡ t|ƒ| _|| _|| _| jdk| _d| jv oZ| jd | _g | _d| _i | _| j dd„ |jj	D ƒ¡ | j d	d„ |jj
D ƒ¡ | jjjjD ]}| j t |j	d
¡¡ q…| jtvr td| j› �ƒ‚|  ¡ | _d| _d| _d| _d| _i | _ | j !¡ | _"d S )Nc                 S   ó   i | ]}|j |“qS © ©Úname)Ú.0Úvir   r   ú©/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/onnxruntime/quantization/onnx_quantizer.pyÚ
<dictcomp>H   ó    z*ONNXQuantizer.__init__.<locals>.<dictcomp>c                 S   r   r   r   ©r   Úotr   r   r!   r"   I   r#   c                 S   r   r   r   ©r   Úitr   r   r!   r"   J   r#   é
   ÚMatMulConstBOnlyú/c                 S   ó   i | ]}|j d “qS ©r   r   r$   r   r   r!   r"   V   r#   c                 S   r+   r,   r   r&   r   r   r!   r"   W   r#   r   zunsupported quantization mode Úfixed_quantization_range_uint8Úfixed_quantization_range_int8Ú
fixed_zeroÚfixed_zero_zp)#r   Ú__init__ÚmodelÚreplace_gemm_with_matmulr   ÚgraphÚ
value_infoÚvalue_infosÚupdateÚoutputÚinputr   ÚmodeÚstaticÚopset_versionÚfuse_dynamic_quantÚextra_optionsÚq_matmul_const_b_onlyÚ	new_nodesÚgraph_scopeÚtensor_namesÚnodeÚdictÚfromkeysr	   Ú
ValueErrorÚcalculate_quantization_paramsÚquantization_paramsÚfixed_qrange_uint8_nameÚfixed_qrange_int8_nameÚfixed_zero_nameÚfixed_zero_zp_nameÚquantized_value_mapÚget_non_initializer_inputsÚgenerated_value_names)Úselfr2   Úper_channelÚreduce_ranger:   r;   Úweight_qTypeÚactivation_qTypeÚtensors_rangeÚnodes_to_quantizeÚnodes_to_excludeÚop_types_to_quantizer>   rC   r   r   r!   r1   '   sP   õ



zONNXQuantizer.__init__c                 C   s~   t jj|d| jjjd�}t|ƒ t|| j| j| j	| j
| j| j| j| j| j| j| jƒ}| |_| j› |› d�|_| ¡  |jjjS )z¾
        generate submodel for the subgraph, so that we re-utilize current quantization implementation.
        quantize the submodel
        update subgraph and set it back to node
        úonnx-quantizer)Úproducer_nameÚopset_importsr*   )ÚonnxÚhelperÚ
make_modelr2   Úopset_importr   r   rQ   rR   r:   r;   rS   rT   rU   rV   rW   rX   r>   ÚparentrA   Úquantize_modelr4   )rP   ÚsubgraphÚ	graph_keyÚwarped_modelÚsub_quantizerr   r   r!   Úquantize_subgraphp   s0   ýô
zONNXQuantizer.quantize_subgraphc           	      C   s  dd„ |j D ƒ}t|ƒdkr|S |jr|jn
|j› dt| jƒ› �}i }|j D ]M}|jtjjkr@|j|  	|j
|› d|j› �¡i}n.|jtjjkrjg }|jD ]}| |  	||› d|j› dt|ƒ› �¡g¡ qL|j|i}nt|ƒ}| |¡ q&tjj|j|j|jfd|ji|¤ŽS )z|
        Check subgraph, if any, quantize it and replace it.
        return new_nodes added for quantizing subgraph
        c                 S   s,   g | ]}|j tjjks|j tjjkr|‘qS r   )Útyper\   ÚAttributeProtoÚGRAPHÚGRAPHS)r   Úattrr   r   r!   Ú
<listcomp>”   s
    þz>ONNXQuantizer.quantize_node_with_sub_graph.<locals>.<listcomp>r   Ú_node_count_ú:r   )Ú	attributeÚlenr   Úop_typer@   rg   r\   rh   ri   rf   Úgrj   ÚgraphsÚextendr   r7   r]   Ú	make_noder9   r8   )	rP   rC   Úgraph_attrsÚ	node_nameÚkwargsrk   ÚkvÚvaluerb   r   r   r!   Úquantize_node_with_sub_graph�   s0   þ"
"
þÿÿ$z*ONNXQuantizer.quantize_node_with_sub_graphc                 C   s   t dd„ | j ¡ D ƒƒS )zQ
        Detect if model already has QuantizeLinear or DequantizeLinear.
        c                 s   s$   � | ]}|j d kp|j dkV  qdS )ÚQuantizeLinearÚDequantizeLinearN)rq   ©r   rC   r   r   r!   Ú	<genexpr>µ   s   € 
ÿz.ONNXQuantizer.has_QDQ_nodes.<locals>.<genexpr>)Úanyr2   Únodes)rP   r   r   r!   Úhas_QDQ_nodes±   s   ÿzONNXQuantizer.has_QDQ_nodesc                 C   s2   t || j ¡ ƒd urdS | jd ur| j |¡S dS )NTF)r   r2   Úinitializerr`   Úfind_initializer_in_path)rP   Úinitializer_namer   r   r!   r„   ¹   s
   
z&ONNXQuantizer.find_initializer_in_pathc                 C   s2   | j  |¡ |D ]}|jD ]}| j |¡ qqd S ©N)r@   rt   r8   rO   Úadd)rP   r�   rC   Úoutput_namer   r   r!   Úadd_new_nodesÀ   s   
ÿÿzONNXQuantizer.add_new_nodesc                 C   sD  |   ¡ r	t d¡ | j ¡ D ]2}| jr|  |¡}t| jƒ}t	| |ƒ}| 
¡  t|t| jƒƒD ]}| j| jD ]}| j |¡ q6q.q|  ¡  | j ¡  d¡ | j ¡ j | j¡ | jd u rq| j ¡ \}}t|ƒdkrqtdt|ƒ ƒ‚t| jj_t| jj_dd„ | jjjD ƒ}|sždd„ | jD ƒ}	|	rž| jjj ¡ }
d|
_t|
_| jjS )	Nz‹Please check if the model is already quantized. Note you don't need to quantize a QAT model. OnnxRuntime support to run QAT model directly.rC   r   z0Invalid model with unknown initializers/tensors.c                 S   s   g | ]	}|j tkr|‘qS r   )Údomainr   )r   Úopsetr   r   r!   rl   é   ó    z0ONNXQuantizer.quantize_model.<locals>.<listcomp>c                 S   s   g | ]	}|j d kr|‘qS )zcom.microsoft)rŠ   r~   r   r   r!   rl   ë   rŒ   r   ) r‚   ÚloggingÚwarningr2   r�   Úenable_subgraph_quantizationr{   rp   r@   r   ÚquantizeÚranger8   rO   r‡   Ú_dequantize_outputsr4   Ú
ClearFieldrC   rt   r`   Úclean_initializersÚRuntimeErrorÚstrr   rZ   r   Úproducer_versionr_   Úversionr   rŠ   )rP   rC   Únumber_of_existing_new_nodesÚop_quantizerÚirˆ   Ú_Úinitializers_not_foundÚms_opsetÚms_nodesr‹   r   r   r!   ra   Æ   s@   ÿ


ÿÿ


zONNXQuantizer.quantize_modelc                 C   s8   d| j v rt d|| j d ¡ | j d S td|›d�ƒ‚)NÚDefaultTensorTypezDget_tensor_type returns DefaultTensorType for tensor name %r, use %dz)Unable to find data type for weight_name=a7  . shape_inference failed to return a type probably this node is from a different domain or using an input produced by such an operator. This may happen if you quantize a model already quantized. You may use extra_options `DefaultTensorType` to indicate the default weight type, usually `onnx.TensorProto.FLOAT`.)r>   r�   Úinfor•   ©rP   Útensor_namer   r   r!   Ú_get_default_tensor_typeó   s   
ý

ÿz&ONNXQuantizer._get_default_tensor_typeFc                 C   sÔ   t || j ¡ ƒ}|d ur|jS || jv r2| j| }|j d¡r2|r-|jjjdkr-|  	|¡S |jjjS | j
r:| jd u rC|rA|  	|¡S d S | j |¡}|d urO|S | j
ra| jra| j |¡}|d ura|S |rh|  	|¡S d S )NÚtensor_typer   )r   r2   rƒ   Ú	data_typer6   rg   ÚHasFieldr¥   Ú	elem_typer¤   r�   r`   Úis_valid_quantize_weightÚget_tensor_type)rP   r£   Ú	mandatoryÚweightr    ÚotypeÚresr   r   r!   rª     s.   





zONNXQuantizer.get_tensor_typec                 C   sž   |   |¡r
|  |¡S || jv r8| j| }|j d¡r)|jjjtjj	tjj
fv r)dS t d|›d|j› d�¡ dS | jrD| jrD| j |¡S t d|›d�¡ dS )	Nr¥   Tz<Inference failed or unsupported type to quantize for tensor z
, type is Ú.Fz%Failed to infer data type of tensor: zS. Please add data type info for this tensor if your model has customized operators.)Úis_input_a_initializerr©   r6   rg   r§   r¥   r¨   Ú
onnx_protoÚTensorProtoÚFLOATÚFLOAT16r�   rŽ   r�   r`   Úis_float_tensor)rP   r£   r    r   r   r!   rµ     s&   



þÿ
ÿzONNXQuantizer.is_float_tensorc                 C   sD   |t jjkr|  |||¡S |t jjkr|  |||¡S td|› d�ƒ‚)a”  
        Create nodes for dynamic quantization of input and add them to nodes_list.
            parameter input_name: Name of the input.
            parameter nodes_list: new nodes are appended to this list.
            parameter qType: type to quantize to.
            parameter initial_type: type to quantize from
            return: scale_name, zero_point_name, scale_shape, zero_point_shape.
        zUnexpected value for qType=r¯   )r±   r²   ÚINT8Ú+_get_dynamic_input_quantization_params_int8ÚUINT8Ú,_get_dynamic_input_quantization_params_uint8rF   )rP   Ú
input_nameÚ
nodes_listÚqTypeÚinitial_typer   r   r!   Ú&_get_dynamic_input_quantization_params6  s
   	z4ONNXQuantizer._get_dynamic_input_quantization_paramsc                 C   sš  t jj}|d }|d }tjjd|g|d g|dd�}| |¡ |d }tjjd|g|d g|dd�}	| |	¡ |d	 }
tj d
|jd g|
d g|
¡}| |¡ |d	 }tj d
|	jd g|d g|¡}| |¡ |d }tj d|jd |jd g|d g|¡}| |¡ tj | j	|g t
|ƒd g¡}| j |¡ |d }tj d|jd | j	g|g|¡}| |¡ tj | j|g dg¡}| j |¡ || jg g fS )az  
        Create nodes for dynamic quantization of input to int8 and add them to nodes_list
            parameter input_name: Name of the input.
            parameter nodes_list: new nodes are appended to this list.
            parameter initial_type: initial weight type (FLOAT or FLOAT16)
            return: scale_name, zero_point_name, scale_shape, zero_point_shape.
        Ú_scaleÚ
_ReduceMinÚ	ReduceMinú:0r   ©ÚkeepdimsÚ
_ReduceMaxÚ	ReduceMaxÚ_AbsÚAbsÚ_Abs_MaxÚMaxç       @Ú	scale_DivÚDiv)r±   r²   r¶   r\   r]   ru   Úappendr8   Úmake_tensorrJ   r   r2   Úadd_initializerrL   )rP   rº   r»   r½   r¼   Úinput_scale_nameÚreduce_min_nameÚreduce_min_nodeÚreduce_max_nameÚreduce_max_nodeÚreduce_min_abs_nameÚreduce_min_abs_nodeÚreduce_max_abs_nameÚreduce_max_abs_nodeÚabs_max_nameÚabs_max_nodeÚinitializer_divÚscale_div_nameÚscale_div_nodeÚinitializer_zpr   r   r!   r·   E  s|   û
û

ü

ü
ü
üü
z9ONNXQuantizer._get_dynamic_input_quantization_params_int8c                 C   sú  t jj}|d }|d }|d }tjjd|g|d g|dd�}| |¡ |d }	tjjd	|g|	d g|	dd�}
| |
¡ tj | j|g t	|ƒg¡}| j
 |¡ tj | j|g d
g¡}| j
 |¡ |d }tj d|
jd |jd g|d g|¡}| |¡ |d }tj d|jd | jg|g|¡}| |¡ |d }tj d| j|jd g|d g|¡}| |¡ |d }tj d|jd |g|d g|¡}| |¡ |d }tj d|j|d g|¡}| |¡ |d }tjjd|j|g||d�}| |¡ ||g g fS )a{  
        Create nodes for dynamic quantization of input to uint8 and add them to nodes_list
            parameter input_name: Name of the input.
            parameter nodes_list: new nodes are appended to this list.
            parameter initial_type: initial weight type (FLAOT or FLOAT16)
            return: scale_name, zero_point_name, scale_shape, zero_point_shape.
        r¿   Ú_zero_pointrÀ   rÁ   rÂ   r   rÃ   rÅ   rÆ   ç        Ú
_scale_SubÚSubÚ
_scale_DivrÍ   Ú_zero_point_SubÚ_zero_point_DivÚ_zero_point_FloorÚFloorÚ_zero_point_CastÚCast)Úto)r±   r²   r¸   r\   r]   ru   rÎ   rÏ   rI   r   r2   rÐ   rK   r8   )rP   rº   r»   r½   r¼   rÑ   Úinput_zp_namerÒ   rÓ   rÔ   rÕ   Úinitializer_qrangeÚinitializer_qvalueÚscale_sub_nameÚscale_sub_noderÝ   rÞ   Úzp_sub_nameÚzp_sub_nodeÚzp_div_nameÚzp_div_nodeÚzp_floor_nameÚzp_floor_nodeÚzp_cast_nameÚzp_cast_noder   r   r!   r¹   ™  sŠ   û
û
üü
ü
ü
ü


z:ONNXQuantizer._get_dynamic_input_quantization_params_uint8c                 C   s  | j }|du s|du rˆ| jdu s|| jvr t d|› d�¡ dS | j| }t|tƒs7tdt|ƒ› d|›d�ƒ‚|du sAt|ƒdkrKt	d	|› d
|› �ƒ‚t
 |d g¡}t|d dƒre|d jt
jt
jfvrst	dt|d ƒ› d|›�ƒ‚t
 |d g¡}|jt
jksƒJ ‚|d }n't
 |g¡}t
 |g¡}| j| }d|v r§|d j}| |¡}|jt
jks¯J ‚g }	|d }
g }|d }tj |
||	| ¡  ¡ ¡}| j |¡ |jt
jkrÙtjj}n|jt
jkrätjj}nt	d|j› d|›�ƒ‚tj |||| d¡ ¡ ¡}| j |¡ d||
||	fS )a\  
        Create initializers and inputs in the graph for zero point and scale of output.
        Zero point and scale values are obtained from self.quantization_params if specified.
            parameter param_name: Name of the quantization parameter.
            return: result, scale_name, zero_point_name, scale_shape, zero_point_shape.
        Nz$Quantization parameters for tensor:"z" not specified)FÚ rù   rù   rù   úUnexpected type ú for r¯   é   zbQuantization parameters should contain zero point, scale, quant type. Specified values for output z: Ú
zero_pointÚscaleÚdtypez and param_name=Ú
quant_typerà   r¿   zUnexpected dtype=z for param_name=)éÿÿÿÿT)rT   rH   r�   r¡   Ú
isinstancer   Ú	TypeErrorrg   rp   rF   ÚnpÚarrayÚhasattrrÿ   Úfloat32Úfloat16Úfloat64Úastyper\   r]   rÏ   ÚravelÚtolistr2   rÐ   r±   r²   r³   r´   Úreshape)rP   Ú
param_nameÚ	use_scaleÚuse_zeropointÚzero_point_typeÚparamsÚzero_point_valuesÚscale_valuesrÿ   Úzero_point_shapeÚzero_point_nameÚscale_shapeÚ
scale_nameÚinit_zpÚ
scale_typeÚ
init_scaler   r   r!   Ú_get_quantization_params÷  sZ   

ÿÿÿ$



ÿ

z&ONNXQuantizer._get_quantization_paramsc              	   C   sJ  |j | }|dksJ dƒ‚|t }|d }	|dur&|dur&d||}
}}n
|  |¡\}
}}}}g }|
rBtj d|||g|g|	¡}nR| jrGdS | jrf|tj	j
krf|d }|d }tj d	|g|||g|	¡}n.|duszJ d
|›d|› d|› d|› �ƒ‚| j||||d�\}}}}tj d|||g|g|	¡}t|||||ƒ| j|< g |¢|‘S )aÊ  
        Given an input for a node (which is not a initializer), this function

        - add nodes to compute zero point and scale for this input if they don't exist.
        - add new QuantizeLinear node to quantize the input.

        :param node: node being quantized in NodeProto format.
        :param input_index: index of input in node.input.
        :param qType: type to quantize to.
        :param given_scale_name: if those inputs need to be quanitzed using this scale tensor.
        :param given_zp_name: if those inputs to be quantized using this zeropoint tensor.
        :param initial_type: type of the weight to quantize
        :return: List of newly created nodes in NodeProto format.
        rù   z*Cannot access undefined variable in graph.Ú_QuantizeLinearNTr|   r¿   rà   ÚDynamicQuantizeLinearzCCannot quantize input without knowing the initial type, input_name=z, input_index=z, qType=z, node=©r½   )r9   r   r  r\   r]   ru   r;   r=   r±   r²   r¸   r¾   r
   rM   )rP   rC   Úinput_indexr¼   Úgiven_scale_nameÚgiven_zp_namer½   rº   rˆ   Úql_node_nameÚ
data_foundr  Úzp_namerœ   r�   Úqlinear_noder  Úzp_shaper   r   r!   Ú_get_quantize_input_nodes2  sf   
üü
ÿÿÿÿÿ	ûüz'ONNXQuantizer._get_quantize_input_nodesc                 C   s.   || j v r
| j | S | jd ur| j |¡S d S r†   )rM   r`   Úfind_quantized_value)rP   rº   r   r   r!   r)  x  s
   


z"ONNXQuantizer.find_quantized_valuec
                 C   sÜ   t  |¡}
|d|
  | }t j| ¡ t jd�}t j| ¡ t jd�}|| }||k rj|dkrj|| }|| }|	du rQt d|› d|› d|› d�¡ d	t j||d�fS t d
|	› d|› d|› d|› d�	¡ d	| |¡fS d|fS )zHAdjust a single weight scale to ensure the int32 bias does not overflow.rË   ©rÿ   rá   NzIncreasing scale for weight `z` by the ratio z to ensure bias `z` has a valid scale.TzIncreased scale[z] for weight `z` by ratio F)r  Úabsr  Úitemr	  r�   r¡   r
  )rP   Úbias_valÚinput_scaleÚweight_scaleÚweight_scale_dtypeÚweight_nameÚ	bias_nameÚqrangeÚmultiplicative_epsilonÚidxÚabsmaxÚbias_smallest_valid_scaleÚinput_scale_fp64Úweight_scale_fp64Úbias_candidate_scaleÚratioÚ	new_scaler   r   r!   Ú$adjust_single_weight_scale_if_needed  s,   
ÿÿÿÿz2ONNXQuantizer.adjust_single_weight_scale_if_neededr.  r/  r1  Úbias_tpÚis_per_channelÚreturnc                 C   sD  |j sdS t|ƒ}t tj¡}d}tj|jtjd�tj|jd tjd� }	|j	}
d}|smt 
| ¡ tjdtjd�¡}t | ¡ tjdtjd�¡}t t |¡t |¡¡}|  ||||
||j|	|¡\}}|ri|}d}||fS |jržt|jƒdkržt|jd ƒD ]}| j|| ||| |
||j|	||d�	\}}|r�|||< d}q~||fS )	zOChecks if the bias scale is too small and increases the weight scale if needed.)FNgq¬‹Ûh ð?r*  r   Fr   T)r5  )Úsizer   r  ÚiinfoÚint32r  Úmaxr	  Úminrÿ   ÚminimumÚmaximumr+  r=  r   Úshaperp   r‘   )rP   r.  r/  r1  r>  r?  Úbias_float_dataÚ
int32_infor4  r3  r0  ÚupdatedÚrminÚrmaxr6  Úchangedr<  r›   r   r   r!   Ú#_adjust_weight_scale_for_int32_bias¤  sX   
(ø
ï
÷€z1ONNXQuantizer._adjust_weight_scale_for_int32_biasr<  c                 C   s  || j vrdS | j | }t|| j ¡ ƒ}t|j| j ¡ ƒ}t|j| j ¡ ƒ}t|j| j ¡ ƒ}|du s?|du s?|du s?|du rAdS | j |¡ | j |¡ tj	 
|¡}|j}	tj|tj |j¡d�}
tj	 |
 |j¡|j¡}| j |¡ t|| j||
|	|jd�}| j |¡ dS )zCRe-quantizes the given weight initializer using the provided scale.Nr*  )Úquant_weight_name)rM   r   r2   rƒ   r  r%  Úq_nameÚremove_initializerr\   Únumpy_helperÚto_arrayÚaxisr  Úasarrayr]   Útensor_dtype_to_np_dtyper¦   Ú
from_arrayr  ÚdimsrÐ   r   rS   )rP   r1  r<  ÚqvÚ	weight_tpÚ
scale_initÚzp_initÚq_weight_initÚweight_zero_pointrU  Úscale_npÚnew_scale_initÚnew_q_weightr   r   r!   Ú_requantize_weightÜ  s2   

 úz ONNXQuantizer._requantize_weightç      ð?c              
   C   s”  || j v r| j | jS | j | j}t|| j ¡ ƒ}t|ƒ}|| j v r)| j | j}n|| jv r9|  |¡\}	}}	}	}	nt	d|› d�ƒ‚t|| j ¡ ƒ}
t|
ƒ}| j | j
}t|| j ¡ ƒ}|duretj |¡nd}| j}|durš|jrš| ¡ sš| jtjjfv ršt|| j ¡ ƒ}|  |||||¡\}}|rš|  ||¡ |}|  ||||¡\}}}}}}|| j vs¯J ‚t||||tj|jdkr½dnd||d�}|| j |< |S )z]
        Quantized the bias. Zero Point == 0 and Scale == Input_Scale * Weight_Scale
        z	Expected z5 to be in quantized value map for static quantizationNr   r   )Ú	node_typeÚ
node_qtype)rM   rQ  r  r   r2   rƒ   r   rH   r  rF   r%  r\   rS  rT  rQ   rA  r€   rS   r±   r²   r¶   rO  rc  Úquantize_bias_static_implr
   r   ÚInitializer)rP   r2  rº   r1  ÚbetaÚweight_scale_nameÚweight_initializerr/  rÑ   rœ   Úinputscale_initializerr.  Úweight_zp_nameÚweight_zp_initr_  r?  Úbias_initializerÚ
did_updateÚnew_weight_scaleÚquantized_bias_nameÚquantized_bias_scale_nameÚquantized_bias_zp_nameÚbias_scale_datare  rf  Úquantized_valuer   r   r!   Úquantize_bias_static  sl   


ÿþû	ùø

z"ONNXQuantizer.quantize_bias_staticc                 C   s   || j v p|| jv p|| jv S )zq
        only check for value info and newly generated tensor names, initializers are checked separately
        )r6   rB   rO   r¢   r   r   r!   Úcontains_tensorJ  s
   
ÿýzONNXQuantizer.contains_tensorc              	   C   s   | j ||dddd|d�S )NFr  ©rC   ÚindicesÚinitializer_use_weight_qTyperR   Úop_level_per_channelrU  Úfrom_subgraph©Ú_ONNXQuantizer__quantize_inputs)rP   rC   rz  r}  r   r   r!   Úquantize_activationT  s   ùz!ONNXQuantizer.quantize_activationr  c              	   C   s   | j ||d||||d�S )NTry  r~  )rP   rC   rz  rR   r|  rU  r}  r   r   r!   Úquantize_weighta  s   	ùzONNXQuantizer.quantize_weightTc              
   C   sî  g }g }	g }
g }|D �]e}|j | }|| jv r/| j| }| |j¡ |	 |j¡ |
 |j¡ q
|sA|
 d¡ | d¡ |	 d¡ q
t|| j ¡ ƒ}|dur…| j	re|re|  
|j|r[| jn| j||¡\}}}n|  ||rm| jn| j|¡\}}}|
 |¡ |	 |¡ | |¡ q
|  |¡�r8| j |d | j| j ¡ ¡}|du �r|j | }|| jv rÎ| j| }| d¡sºJ d|› d�ƒ‚|j d¡sÈJ d|› d�ƒ‚|jjj}n|| jv sÛJ d	|›d
�ƒ‚| j| }| j||| j|d�}|du rñ dS |rù|  |¡ n| |¡ |d }|jdk�r|
 |j¡ | |j d ¡ |	 |j d ¡ q
|
 |jd ¡ | |jd ¡ |	 |jd ¡ q
| jdu�rf| jj||g||||dd�\}}}}|
 |d ¡ | |d ¡ |	 |d ¡ q
t d|› d| j!› �ƒ‚|
|	||fS )a›  
        Given a node, this function quantizes the inputs as follows:
            - If input is an initializer, quantize the initializer data, replace old initializer
              with new initializer
            - Else, add QuantizeLinear nodes to perform quantization
            parameter node: node being quantized in NodeProto format.
            parameter indices: input indices to quantize.
            return: (List of quantized input names,
                     List of zero point names used for input quantization,
                     List of scale names used for input quantization,
                     List of new QuantizeLinear nodes created)
        rù   Nr  rg   zvalue_info=z has no type.r¥   z is not a tensor.zshape inference failed for zF and attribute 'tensor_names' does not have any value for this tensor.r  )NNNNr  r|   r   é   r   T)r{  rR   r|  rU  r}  z!Invalid tensor name to quantize: z @graph scope)"r9   rM   rÎ   r  r%  rQ  r   r2   rƒ   rQ   Úquantize_weight_per_channelr   rS   rT   Úquantize_initializerrx  Úfind_node_by_namer@   r4   r6   r§   rg   r¥   r¨   rB   r(  r‰   rt   rq   r8   r`   r  rF   rA   )rP   rC   rz  r{  rR   r|  rU  r}  Úscale_namesÚzero_point_namesÚquantized_input_namesr�   r   Ú
node_inputrv  rƒ   Úq_weight_namer%  r  r&  rº   r5   r½   Úquantize_input_nodesÚparent_quantized_input_namesÚparent_zero_point_namesÚparent_scale_namesrœ   r   r   r!   Ú__quantize_inputst  s²   







üü
ý

ÿ




ÿ

ÿ
ùû
zONNXQuantizer.__quantize_inputsc           	      C   sj   |j | jv r| j|j  }|j|j|jfS |  ||||¡\}}}t|j |||tjdƒ}|| j|j < |||fS )aš  
        :param weight: TensorProto initializer
        :param qType: type to quantize to
        :param keep_float_weight: Whether to quantize the weight. In some cases, we only want to qunatize scale and zero point.
                                  If keep_float_weight is False, quantize the weight, or don't quantize the weight.
        :return: quantized weight name, zero point name, scale name
        N)	r   rM   rQ  r%  r  Úquantize_initializer_implr
   r   rh  )	rP   r¬   r¼   rR   Úkeep_float_weightrv  rŠ  r%  r  r   r   r!   r„  ÷  s&   	ý
ÿú
z"ONNXQuantizer.quantize_initializerc           
      C   sd   || j v r| j | }|j|j|jfS |  |||||¡\}}}	t|||	|tjd ƒ}|| j |< |||	fS r†   )rM   rQ  r%  r  Ú quantize_weight_per_channel_implr
   r   rh  )
rP   r1  rS   Úchannel_axisrR   r‘  rv  rŠ  r%  r  r   r   r!   rƒ    s&   
	
ý

ÿú

z)ONNXQuantizer.quantize_weight_per_channelc                 C   sØ   || j v rj|| jvrj| j | }t|j| j ¡ ƒ}| jjjdks*| jjjdkr9|dur9|du s9tj 	|¡j
dks9J ‚|d }| j || j| j ¡ ¡}|du ra|j|j|jg}tj d||g|¡}|S ||jd ksjJ ‚dS )a¶  
        Given a value (input/output) which is quantized, add a DequantizeLinear node to dequantize
        it back to float32 or float16
            parameter value_name: value to dequantize
            parameter new_nodes_list: List of new nodes created before processing current node
            return: None if there is already a DequantizeLinear node that dequantizes it
                    A DequantizeLinear node otherwise
        rY   Nr   Ú_DequantizeLinearr}   r   )rM   rO   r   r  r2   rƒ   rZ   r\   rS  rT  rA  r…  r@   r4   rQ  r%  r]   ru   r8   )rP   Ú
value_namerv  r\  Údqlinear_nameÚdqlinear_nodeÚdqlinear_inputsÚdequantize_noder   r   r!   Ú_dequantize_value8  s&   	
ý
ÿzONNXQuantizer._dequantize_valuec                 C   s6   | j  ¡ jD ]}|  |j¡}|dur| j |¡ qdS )zÃ
        Dequantize output if it is quantized
            parameter new_nodes_list: List of new nodes created before processing current node
            return: List of new nodes created
        N)r2   r4   r8   rš  r   r@   rÎ   )rP   r8   r™  r   r   r!   r’   _  s   €ýz!ONNXQuantizer._dequantize_outputsc                 C   s@  | j d u rd S |  ¡  i }| j D ]�}| j | }t|tƒs)tdt|ƒ› d|›d�ƒ‚| jj|i d�}| j}d|v r=|d j	}d|v rOd|v rO|d |d }}nE|t
jjkr`t||jd ƒ\}}n4| d	|jd
 ¡}| d|jd ¡}	| d| j¡}
| dd¡}t|||
d�\}}t||	|||
| jƒ\}}t|||d�||< q|S )Nrú   rû   r¯   )Údefault_valr   rþ   rý   r   rL  r   rM  Ú	symmetricrR   F)rR   rœ  )rý   rþ   r   )rU   Úadjust_tensor_rangesr  r   r  rg   Útensor_quant_overridesÚget_per_tensor_overridesrT   r¥   r\   r²   ÚFLOAT8E4M3FNr   Úavg_stdÚgetÚrange_valueÚis_activation_symmetricr   r   Úmin_real_ranger   )rP   rH   r£   ÚtdÚquant_overridesr   Úzerorþ   rL  rM  rœ  rR   ÚqminÚqmaxr   r   r!   rG   k  s0   




z+ONNXQuantizer.calculate_quantization_paramsr†   )F)NN)NNN)rd  )FFr  F)TFFr  F)FF)TF)'Ú__name__Ú
__module__Ú__qualname__r1   rf   r{   r‚   r„   r‰   ra   r¤   rª   rµ   r¾   r·   r¹   r  r(  r)  r=  r  Úndarrayr–   r\   r²   ÚboolÚtuplerO  rc  rw  rx  r€  r�  r  r„  rƒ  rš  r’   rG   r   r   r   r!   r   &   sr    
óI"-
T
^<
ÿF
ö%þýüûú
ù8
&H


ù
ø 
&
ú 'r   )#r�   Únumpyr  r\   Úonnx.numpy_helperr   r±   Úbase_quantizerr   r   Ú	calibrater   Ú
onnx_modelr   Úquant_utilsr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Úregistryr   r   r   r   r   r!   Ú<module>   s   L