Ë
    ÓGìj¢Õ  ã                  óØ  — d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	Z
d dlZd dlZd dlmZ d dlmZ dd	lmZ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&m'Z' ddl(m)Z)  G d„ de«      Z*e G d„ d«      «       Z+ G d„ d«      Z,e G d„ d«      «       Z-e G d„ d«      «       Z.e G d„ d«      «       Z/e G d„ d«      «       Z0e G d„ d«      «       Z1 G d„ de«      Z2y)é    )ÚannotationsN)Ú	dataclass)ÚEnum)ÚAny)ÚTensorProto)Úonnx_pbé   )ÚBaseQuantizerÚQuantizationParams)Ú
TensorData)ÚDEQUANT_OP_NAMEÚQUANT_OP_NAMEÚQuantizedValueÚQuantizedValueTypeÚ__producer__Ú__version__Úadd_dequant_output_suffixÚadd_dequant_suffixÚadd_quant_input_suffixÚadd_quant_output_suffixÚadd_quant_suffixÚcompute_scale_zpÚcompute_scale_zp_float8Úfind_by_nameÚget_qmin_qmax_for_qTypeÚ	ms_domainÚnormalize_axisÚtensor_proto_to_array)ÚCreateQDQQuantizerc                  ó   — e Zd ZdZdZdZy)ÚQDQQuantTensorTyper   r	   é   N)Ú__name__Ú
__module__Ú__qualname__Ú
ACTIVATIONÚWEIGHTÚBIAS© ó    újC:\Users\Stabi\Downloads\virtual-tryon(1)\venv\Lib\site-packages\onnxruntime/quantization/qdq_quantizer.pyr!   r!   ,   s   „ Ø€JØ€FØ�Dr*   r!   c                  ó"   — e Zd ZU ded<   ded<   y)ÚQDQQuantParamProviderÚstrÚ
input_nameÚ	node_nameN©r#   r$   r%   Ú__annotations__r)   r*   r+   r-   r-   5   s   … àƒOØ„Nr*   r-   c                  ó0   — e Zd Zej                  dddfd„Zy)ÚQDQTensorQuantInfoNc                óV   — || _         || _        || _        |d u| _        |€J ‚|| _        y ©N)Útensor_typeÚquant_para_providerÚaxisÚ	is_sharedÚ	data_type)Úselfr7   r8   r9   r;   s        r+   Ú__init__zQDQTensorQuantInfo.__init__>   s8   € Ø&ˆÔØ#6ˆÔ ØˆŒ	Ø,°DÐ8ˆŒØÐ$Ð$Ð$Ø"ˆ�r*   )r#   r$   r%   r!   r&   r=   r)   r*   r+   r4   r4   =   s   „ Ø#5×#@Ñ#@ÐVZÐaeÐquô #r*   r4   c                  ó6   — e Zd ZU ded<   ded<   ded<   ded<   y)ÚQDQBiasQuantInfor.   r0   r/   Úweight_nameÚfloatÚbetaNr1   r)   r*   r+   r?   r?   H   s   … àƒNØƒOØÓØ
„Kr*   r?   c                  ó,   — e Zd ZU ded<   ded<   ded<   y)ÚQDQTensorQuantParamsr   ÚoriginalzQuantizationParams | NoneÚ	convertedúset[str] | NoneÚconverted_recv_nodesNr1   r)   r*   r+   rD   rD   S   s   … à Ó Ø(Ó(Ø)Ô)r*   rD   c                  ó"   — e Zd ZU ded<   ded<   y)ÚQDQScaleZpInitializersr   ÚscaleÚ
zero_pointNr1   r)   r*   r+   rJ   rJ   [   s   … àÓØÔr*   rJ   c                  ó,   — e Zd ZU ded<   ded<   ded<   y)ÚQDQTensorScaleZpInitializersrJ   rE   zQDQScaleZpInitializers | NonerF   rG   rH   Nr1   r)   r*   r+   rN   rN   d   s   … à$Ó$Ø,Ó,Ø)Ô)r*   rN   c                  ó4   — e Zd ZU ded<   ded<   ded<   d	d„Zy)
ÚQDQTensorQuantizedValuer   rE   zQuantizedValue | NonerF   rG   rH   c                ó®   — | j                   €| j                  S | j                  €| j                   S || j                  v r| j                   S | j                  S r6   )rF   rE   rH   )r<   Úconsumer_node_names     r+   Úget_for_consumerz(QDQTensorQuantizedValue.get_for_consumert   sQ   € Ø�>‰>Ð!Ø—=‘=Ð à×$Ñ$Ð,Ø—>‘>Ð!ð
 #5¸×8QÑ8QÑ"Qˆt�~‰~ÐeÐX\×XeÑXeÐer*   N)Úreturnr   )r#   r$   r%   r2   rS   r)   r*   r+   rP   rP   n   s   … àÓØ$Ó$Ø)Ó)ô
fr*   rP   c                  ó–  — e Zd Z	 d!d„Zd„ Zd„ Zdej                  fd„Zd"d„Z	d#d„Z
d"d„Zd	„ Zd$d
„Zd„ Zd„ Zd„ Zd„ Z	 d!	 	 	 	 	 	 	 	 	 	 	 d%d„Z	 d!	 	 	 	 	 	 	 	 	 	 	 d&d„Z	 d!d„Zd!d„Zd!d„Zd„ Zd„ Zd„ Zd„ Zd"d„Z	 	 d'	 	 	 	 	 	 	 	 	 d(d„Z	 d!	 	 	 	 	 	 	 d)d„Z	 	 d*	 	 	 	 	 	 	 	 	 	 	 d+d„Zd,d„Z	 d-	 	 	 	 	 	 	 d.d„Z d/d„Z!d0d„Z"d1d „Z#y)2ÚQDQQuantizerNc                ó  ‡— t        j                  | |||||||||	|
«       i | _        i | _        g | _        |
j                  dg «      | _        |
j                  dd«      | _        |
j                  dd«      | _        |
j                  dd«      | _	        i | _
        |
j                  di «      | _        |
j                  dd«      rt        nd | _        | j                  d	k  r�t        j                   t        j"                  fŠt%        ˆfd
„| j&                  D «       «      }| j                  sF| j(                  ‰v s| j*                  ‰v s|r(t-        j.                  dt        › d�«       t        | _        | j1                  «       | _        i | _        y )NÚ"OpTypesToExcludeOutputQuantizationÚAddQDQPairToWeightFÚQuantizeBiasTÚDedicatedQDQPairÚ QDQOpTypePerChannelSupportToAxisÚUseQDQContribOpsé   c              3  ó:   •K  — | ]  }|j                   ‰v –— Œ y ­wr6   )r7   )Ú.0ÚtÚint16_typess     €r+   Ú	<genexpr>z(QDQQuantizer.__init__.<locals>.<genexpr>Ã   s   øè ø€ Ð&oÑMnÈ q§}¡}¸Ô'CÑMnùs   ƒzÃONNX QuantizeLinear and DequantizeLinear operators do not support 16-bit integer quantization types prior to opset 21. The domain of QuantizeLinear and DequantizeLinear operators will be set to 'z' to enable support.)r
   r=   Útensors_to_quantizeÚbias_to_quantizeÚnodes_to_removeÚgetÚ'op_types_to_exclude_output_quantizationÚadd_qdq_pair_to_weightÚquantize_biasÚdedicated_qdq_pairÚtensor_to_its_receiving_nodesÚ'qdq_op_type_per_channel_support_to_axisr   Úqdq_op_domainÚopset_versionr   ÚUINT16ÚINT16ÚanyÚtensor_quant_override_qtypesÚactivation_qTypeÚweight_qTypeÚloggingÚwarningÚcalc_graph_quant_paramsÚquantization_paramsÚquantized_value_map)r<   ÚmodelÚper_channelÚreduce_rangeru   rt   Útensors_rangeÚnodes_to_quantizeÚnodes_to_excludeÚop_types_to_quantizeÚextra_optionsÚoverrides_have_int16rb   s               @r+   r=   zQDQQuantizer.__init__‚   s™  ø€ ô 	×ÑØØØØØØØØØØ Øô	
ð $&ˆÔ Ø "ˆÔà!ˆÔð 8E×7HÑ7HÐImÐoqÓ7rˆÔ4ð
 '4×&7Ñ&7Ð8LÈeÓ&TˆÔ#ð +×.Ñ.¨~¸tÓDˆÔð #0×"3Ñ"3Ð4FÈÓ"NˆÔØ-/ˆÔ*ð 8E×7HÑ7HÐIkÐmoÓ7pˆÔ4à*7×*;Ñ*;Ð<NÐPUÔ*V�YÐ\`ˆÔð
 ×Ñ Ò"Ü&×-Ñ-¬{×/@Ñ/@ÐAˆKÜ#&Ó&oÈT×MnÒMnÓ&oÓ#oÐ Ø×%Ò%Ø×%Ñ%¨Ñ4¸×8IÑ8IÈ[Ñ8XÑ\pä—‘ðcäclÐbmð n&ð&ôô &/�Ô"à#'×#?Ñ#?Ó#AˆÔ ð $&ˆÕ r*   c                ó  — t        || j                  j                  «       «      }|�|j                  S || j                  v rJ| j                  |   }|j
                  j                  d«      r |j
                  j                  j                  S y)ú2
        Check if tensor can be quantized
        Nr7   )	r   r{   Úinitializerr;   Úvalue_infosÚtypeÚHasFieldr7   Ú	elem_type©r<   Útensor_nameÚweightÚvis       r+   Ú_get_tensor_typezQDQQuantizer._get_tensor_typeÔ   sx   € ô ˜k¨4¯:©:×+AÑ+AÓ+CÓDˆØÐØ×#Ñ#Ð#Ø˜D×,Ñ,Ñ,Ø×!Ñ! +Ñ.ˆBØ�w‰w×Ñ Ô.Ø—w‘w×*Ñ*×4Ñ4Ð4Ør*   c                óú  — t        || j                  j                  «       «      }|�B|j                  t        j
                  j                  t        j
                  j                  fv ryy|| j                  v rl| j                  |   }|j                  j                  d«      rA|j                  j                  j                  t
        j                  t
        j                  fv ryyt        j                  d|› d�«       y)r…   Tr7   z$failed to infer the type of tensor: z6. Skip to quantize it. Please check if it is expected.F)r   r{   r†   r;   Ú
onnx_protor   ÚFLOATÚFLOAT16r‡   rˆ   r‰   r7   rŠ   rv   rw   r‹   s       r+   Ú_is_tensor_quantizablez#QDQQuantizer._is_tensor_quantizableá   sá   € ô ˜k¨4¯:©:×+AÑ+AÓ+CÓDˆØÐØ×Ñ¤J×$:Ñ$:×$@Ñ$@Ä*×BXÑBX×B`ÑB`Ð#aÑaØð ð ˜D×,Ñ,Ñ,Ø×!Ñ! +Ñ.ˆBØ�w‰w×Ñ Ô.°2·7±7×3FÑ3F×3PÑ3PÜ×!Ñ!Ü×#Ñ#ðUñ 4ð ð ô	 �O‰OØ6°{°mÐCyÐzôð r*   c                óJ  — | j                  |«      r’|rUt        |t        «      st        dt	        |«      › d�«      ‚| j                  |«      }t        |||¬«      | j                  |<   y|| j                  vr,| j                  |«      }t        ||¬«      | j                  |<   yyy)a  
        Adds a tensor to the list (actually a dict) of tensors to quantize. Called indirectly by op quantizers that
        want to quantize a tensor (i.e., "mark" a tensor for quantization).

        If quant_sharing_provider is not None, tensor with name tensor_name will be quantized with the same
        quantization parameters as the node input specified in quant_sharing_provider. Ex: A Tranpose node's output
        will typically use the same quantization parameter initializers used at the Transpose node's input.

        Args:
            tensor_name: name of the tensor to quantize
            quant_sharing_provider: name of the tensor and node that provides quantization parameter
            tensor_type: QDQQuantTensorType default ACTIVATION
        zBquant_sharing_provider must be of type QDQQuantParamProvider, not Ú.)r7   r8   r;   )r7   r;   N)r”   Ú
isinstancer-   Ú	TypeErrorrˆ   r�   r4   rd   )r<   rŒ   Úquant_sharing_providerr7   r;   s        r+   Ú__quantize_tensorzQDQQuantizer.__quantize_tensor÷   s¶   € ð ×&Ñ& {Ô3Ù%Ü!Ð"8Ô:OÔPÜ#Ø\Ô]aÐbxÓ]yÐ\zÐz{Ð|óð ð !×1Ñ1°+Ó>�	Ü8JØ +ÐAWÐclô9�×(Ñ(¨Ò5ð  D×$<Ñ$<Ñ<Ø ×1Ñ1°+Ó>�	Ü8JÐWbÐnwÔ8x�×(Ñ(¨Ò5ð =ð 4r*   c                óD   — | j                  |dt        j                  «      S )zó
        Adds a tensor to the list of tensors to quantize. Called by op quantizers that
        want to quantize a tensor (i.e., "mark" a tensor for quantization).

        Args:
            tensor_name: name of the tensor to quantize
        N)Ú_QDQQuantizer__quantize_tensorr!   r&   ©r<   rŒ   s     r+   Úquantize_activation_tensorz'QDQQuantizer.quantize_activation_tensor  s    € ð ×%Ñ% k°4Ô9K×9VÑ9VÓWÐWr*   c                óX   — | j                  |t        ||«      t        j                  «      S )a˜  
        Adds a tensor to the list of tensors to quantize. Called by op quantizers that
        want to quantize an output tensor using the same quantization parameters as one of the node's inputs.

        Ex: A Tranpose node's output will typically use the same quantization parameter initializers used at
        the Transpose node's input.

        Args:
            output_name: name of the node output to quantize so that it uses the same quantization params as an input.
            input_name: name of the node input from which the output tensor will get its quantization params.
            node_name: name of the node that consumes `input_name`.
        )rœ   r-   r!   r&   )r<   Úoutput_namer/   r0   s       r+   Úquantize_output_same_as_inputz*QDQQuantizer.quantize_output_same_as_input  s-   € ð ×%Ñ%ØÔ.¨z¸9ÓEÔGY×GdÑGdó
ð 	
r*   c                óD   — | j                  |dt        j                  «      S )zú
        Adds a tensor to the list of weight tensors to quantize. Called by op quantizers that
        want to quantize a weight (i.e., "mark" a weight for quantization).

        Args:
            tensor_name: name of the weight to quantize
        N)rœ   r!   r'   r�   s     r+   Úquantize_weight_tensorz#QDQQuantizer.quantize_weight_tensor/  s    € ð ×%Ñ% k°4Ô9K×9RÑ9RÓSÐSr*   c                ól  — t        || j                  j                  «       «      }|ru|j                  t        j
                  j                  t        j
                  j                  fv r4t        t        j                  ||j                  ¬«      | j                  |<   y y t        j                  d|› d�«       y )N)r7   r9   r;   z9only support per-channel quantization on weight. Tensor: z is not quantized.)r   r{   r†   r;   r‘   r   r’   r“   r4   r!   r'   rd   rv   rw   )r<   rŒ   r9   r�   s       r+   Ú"quantize_weight_tensor_per_channelz/QDQQuantizer.quantize_weight_tensor_per_channel9  s“   € Ü˜k¨4¯:©:×+AÑ+AÓ+CÓDˆÙØ×Ñ¤J×$:Ñ$:×$@Ñ$@Ä*×BXÑBX×B`ÑB`Ð#aÑaÜ8JÜ 2× 9Ñ 9ÀÐPV×P`ÑP`ô9�×(Ñ(¨Ò5ð bô
 �O‰OÐWÐXcÐWdÐdvÐwÕxr*   c                ón  — | j                   j                  |«      rVt        j                  d|› d�«       | j	                  |d¬«      \  }}|r| j                  ||«       y| j                  |«       yt        || j                  j                  «       «      }|�…|j                  t        j                  j                  t        j                  j                  fv rD|| j                  vrt!        ||||«      | j                  |<   yt        j"                  d|› d�«       yyt        j"                  d|› d	�«       y)
a¾  
        Adds a bias tensor to the list of bias tensors to quantize. Called by op quantizers that
        want to quantize a bias with bias_zero_point = 0 and bias_scale = input_scale * weight_scale * beta.
        TODO: Explain the reasoning for using this formula.

        Args:
            node_name: name of the node that consumes the bias, input, and weight tensors.
            bias_name: name of the bias tensor to quantize.
            input_name: name of the input tensor whose scale is used to compute the bias's scale.
            weight_name: name of the weight tensor whose scale is used to compute the bias's scale.
            beta: Multiplier used to compute the bias's scale.
        zQuantizing bias tensor 'z=' as a weight due to the presence of user-specified overridesr   )Údefault_axisNzBias z) has already been marked for quantizationz	Expected z to be a weight)Útensor_quant_overridesrg   rv   ÚinfoÚis_tensor_per_channelr¥   r£   r   r{   r†   r;   r‘   r   r’   r“   re   r?   rw   )	r<   r0   Ú	bias_namer/   r@   rB   Úis_per_channelr9   r�   s	            r+   Úquantize_bias_tensorz!QDQQuantizer.quantize_bias_tensorC  s"  € ð ×&Ñ&×*Ñ*¨9Ô5Ü�L‰LØ*¨9¨+Ð5rÐsôð $(×#=Ñ#=¸iÐVWÐ#=Ó#XÑ ˆN˜DÙØ×7Ñ7¸	À4ÔHð ð ×+Ñ+¨IÔ6Øä˜i¨¯©×)?Ñ)?Ó)AÓBˆØÐØ×Ñ¤J×$:Ñ$:×$@Ñ$@Ä*×BXÑBX×B`ÑB`Ð#aÑaØ D×$9Ñ$9Ñ9Ü7GÈ	ÐS]Ð_jÐlpÓ7q�D×)Ñ)¨)Ò4ä—O‘O e¨I¨;Ð6_Ð$`Õað	 bô �O‰O˜i¨	 {°/ÐBÕCr*   c                ó:   — | j                   j                  |«       y r6   )rf   Úappend)r<   Únodes     r+   Úremove_nodezQDQQuantizer.remove_nodef  s   € Ø×Ñ×#Ñ# DÕ)r*   c                óN   — | j                   j                  | j                  «       y r6   )r{   Úremove_nodesrf   )r<   s    r+   r³   zQDQQuantizer.remove_nodesi  s   € Ø�
‰
×Ñ × 4Ñ 4Õ5r*   c                ó&  — | j                   j                  «       D ]|  }| j                  |«      sŒt        | |«      }|j	                  «        |j
                  D ]=  }|| j                  vrg | j                  |<   | j                  |   j                  |«       Œ? Œ~ | j                  «        | j                  «        | j                  r| j                  «        | j                  «        | j                  s| j                   j                  «        t        | j                   j                   _        t"        | j                   j                   _        | j&                  t(        k(  r | j                   j+                  t(        d«       | j                   j                   S )Nr	   )r{   ÚnodesÚshould_quantize_noder   ÚquantizeÚinputrl   r¯   Ú_quantize_normal_tensorsÚ_quantize_sharing_param_tensorsrj   Ú_quantize_bias_tensorsr³   ri   Úclean_initializersr   Úproducer_namer   Úproducer_versionrn   r   Úset_opset_import)r<   r°   Úop_quantizerrŒ   s       r+   Úquantize_modelzQDQQuantizer.quantize_modell  s.  € Ø—J‘J×$Ñ$Ö&ˆDØ×(Ñ(¨Õ.Ü1°$¸Ó=�Ø×%Ñ%Ô'à#'§:¤:�KØ"¨$×*LÑ*LÑLØJL˜×:Ñ:¸;ÑGØ×6Ñ6°{ÑC×JÑJÈ4ÕPñ $.ð 'ð 	×%Ñ%Ô'Ø×,Ñ,Ô.Ø×ÒØ×'Ñ'Ô)Ø×ÑÔØ×*Ò*Ø�J‰J×)Ñ)Ô+ä)5ˆ�
‰
×ÑÔ&Ü,7ˆ�
‰
×ÑÔ)Ø×Ñ¤Ò*Ø�J‰J×'Ñ'¬	°1Ô5à�z‰z×ÑÐr*   c                ó²  — || j                   v rÉ| j                   |   j                  €°| j                   |   j                  €—t        | j                  j	                  «       |   «      dk(  rn| j                  j                  |«      sS| j                  j                  |«      s8| j                  j                  ||«       || j                  v r| j                  |= yy)Nr	   TF)	ry   rF   Úlenr{   Úinput_name_to_nodesÚis_graph_outputÚis_graph_inputÚreplace_output_of_all_nodesrd   )r<   Úupstream_output_namer    s      r+   Útry_replacing_upstream_outputz*QDQQuantizer.try_replacing_upstream_output†  s½   € à˜4×3Ñ3Ñ3Ø×(Ñ(¨Ñ5×?Ñ?ÐGØ×(Ñ(Ð)=Ñ>×HÑHÐPÜ�D—J‘J×2Ñ2Ó4Ð5IÑJÓKÈqÒPØ—J‘J×.Ñ.Ð/CÔDØ—J‘J×-Ñ-Ð.BÔCà�J‰J×2Ñ2Ð3GÈÔUØ# t×'?Ñ'?Ñ?Ø×,Ñ,Ð-AÐBØØr*   c                óª   — t         j                  j                  t        |||g|g||| j                  ¬«      }| j
                  j                  |g«       y)zI
        Creates a QuantizeLinear node and adds it to the model.
        ©r9   ÚdomainN)ÚonnxÚhelperÚ	make_noder   rn   r{   Ú	add_nodes)r<   Úq_inputÚq_outputÚquant_node_nameÚ
scale_nameÚzp_namer9   Úqlinear_nodes           r+   Ú_create_q_nodezQDQQuantizer._create_q_node•  sT   € ô —{‘{×,Ñ,ÜØ�j 'Ð*ØˆJØØØ×%Ñ%ð -ó 
ˆð 	�
‰
×Ñ˜l˜^Õ,r*   c                óª   — t         j                  j                  t        |||g|g||| j                  ¬«      }| j
                  j                  |g«       y)zK
        Creates a DequantizeLinear node and adds it to the model.
        rË   N)rÍ   rÎ   rÏ   r   rn   r{   rÐ   )r<   Údq_inputÚ	dq_outputÚdequant_node_namerÔ   rÕ   r9   Údequant_nodes           r+   Ú_create_dq_nodezQDQQuantizer._create_dq_node«  sT   € ô —{‘{×,Ñ,ÜØ�z 7Ð+ØˆKØØØ×%Ñ%ð -ó 
ˆð 	�
‰
×Ñ˜l˜^Õ,r*   c
                ó  — t         j                  j                  t        |||g|g||	| j                  ¬«      }
t         j                  j                  t
        |||g|g||	| j                  ¬«      }| j                  j                  |
|g«       y )NrË   )rÍ   rÎ   rÏ   r   rn   r   r{   rÐ   )r<   rÑ   rÒ   rÓ   rÙ   rÚ   rÛ   rÔ   rÕ   r9   rÖ   rÜ   s               r+   Ú_create_qdq_nodeszQDQQuantizer._create_qdq_nodesÁ  s“   € ô —{‘{×,Ñ,ÜØ�j 'Ð*ØˆJØØØ×%Ñ%ð -ó 
ˆô —{‘{×,Ñ,ÜØ�z 7Ð+ØˆKØØØ×%Ñ%ð -ó 
ˆð 	�
‰
×Ñ˜l¨LÐ9Õ:r*   c                óh  — |j                   }|�•| j                  dk  rt        d«      ‚| j                  }| j                  t        j
                  j                  j                  k(  rt        j                  j                  }| j                  |||| j                  ¬«      \  }}}nJ| j                  ||t        j                  u r| j                  n| j                  | j                  ¬«      \  }}}t!        |«      }	| j"                  j%                  ||	«       | j                  r7t'        |«      }
| j)                  ||
t+        |«      |
|	t-        |«      |||«	       y t        j.                  j1                  t2        |||g|	gt-        |«      || j4                  ¬«      }| j"                  j7                  |«       y )Né   zLPer-Channel support with QDQ format requires onnx opset version 13 or above.)Úkeep_float_weightrË   )Únamero   Ú
ValueErrorrt   rÍ   r   r   ÚUINT8r‘   ÚINT8Úquantize_weight_per_channelri   Úquantize_initializerr!   r'   ru   r   r{   Úreplace_input_of_all_nodesr   rß   r   r   rÎ   rÏ   r   rn   Úadd_node)r<   Úweight_protor7   r9   r@   ÚqtypeÚq_weight_namerÕ   rÔ   Úweight_dequant_outputÚweight_quant_outputrÜ   s               r+   Ú_add_qdq_pair_for_initializerz*QDQQuantizer._add_qdq_pair_for_initializerÖ  s¢  € Ø"×'Ñ'ˆØÐØ×!Ñ! BÒ&Ü Ð!oÓpÐpØ×)Ñ)ˆEØ×$Ñ$¬¯©×(@Ñ(@×(FÑ(FÒFÜ"×.Ñ.×3Ñ3�à15×1QÑ1QØð ØØ"&×"=Ñ"=ð 2Ró 2Ñ.ˆM˜7¡Jð 26×1JÑ1JØØ%0Ô4F×4MÑ4MÑ%M�×!Ò!ÐSW×ShÑShØ"&×"=Ñ"=ð 2Kó 2Ñ.ˆM˜7 Jô !:¸+Ó FÐØ�
‰
×-Ñ-¨kÐ;PÔQØ×&Ò&Ü"9¸+Ó"FÐà×"Ñ"ØØ#Ü  Ó-Ø#Ø%Ü" ;Ó/ØØØõ
ô  Ÿ;™;×0Ñ0ÜØ 
¨GÐ4Ø&Ð'Ü" ;Ó/ØØ×)Ñ)ð 1ó ˆLð �J‰J×Ñ Õ-r*   c                ó  — | j                   �r|| j                  v �r
t        | j                  |   «      dkD  rït        | j                  |   «      }t        |«      D ]È  }d|dz   › �}t	        |«      |z   }t        |«      |z   }	t        |«      |z   }
t        |«      |z   }| j                  |||
||	|||«       | j                  |   |   }| j                  j                  |||	«       |dk(  sŒ�t        ||	||t        j                  |¬«      }t        |d d «      | j                  |<   ŒÊ y |}t        |«      }| j                  j!                  |«      r*t#        |«      }|}| j                  j%                  ||«       n| j                  j'                  ||«       | j                  |t	        |«      t        |«      t	        |«      |t        |«      ||«       t        ||||t        j                  |¬«      }t        |d d «      | j                  |<   y )Nr	   Ú_r   ©Ú
scale_type)rk   rl   rÃ   Úranger   r   r   r   rß   r{   Úreplace_node_inputr   r   ÚInputrP   rz   rÅ   r   rÇ   ré   )r<   rŒ   rÔ   rÕ   r;   Únum_dedicated_qdq_pairÚiÚpostfixÚ tensor_name_quant_output_postfixÚ"tensor_name_dequant_output_postfixÚquant_node_name_postfixÚdequant_node_name_postfixr°   Úquantized_valuerÑ   rÚ   s                   r+   Ú_add_qdq_pair_for_activationz)QDQQuantizer._add_qdq_pair_for_activation  s  € à×#Ó#Ø˜t×AÑAÒAÜ�D×6Ñ6°{ÑCÓDÀqÒHä%(¨×)KÑ)KÈKÑ)XÓ%YÐ"ÜÐ1Ö2�Ø˜a !™e˜W˜+�Ü3JÈ;Ó3WÐZaÑ3aÐ0Ü5NÈ{Ó5[Ð^eÑ5eÐ2Ü*:¸;Ó*GÈ'Ñ*QÐ'Ü,>¸{Ó,KÈgÑ,UÐ)Ø×&Ñ&ØØ4Ø+Ø4Ø6Ø-ØØô	ð ×9Ñ9¸+ÑFÀqÑI�Ø—
‘
×-Ñ-¨d°KÐAcÔdØ˜“6Ü&4Ø#Ø:Ø"ØÜ*×0Ñ0Ø#,ô'�Oô =TÐTcÐeiÐkoÓ<p�D×,Ñ,¨[Ò9ñ9 3ð< "ˆGÜ1°+Ó>ˆIØ�z‰z×)Ñ)¨+Ô6Ü0°Ó=�Ø'�	Ø—
‘
×6Ñ6°{ÀGÕLà—
‘
×5Ñ5°kÀ9ÔMà×"Ñ"ØÜ'¨Ó4Ü  Ó-Ü'¨Ó4ØÜ" ;Ó/ØØô	ô -ØØØØÜ"×(Ñ(Ø$ôˆOô 5LÈOÐ]aÐcgÓ4hˆD×$Ñ$ [Ò1r*   c                óò  — t        | j                  |   D �cg c]  }|j                  ‘Œ c}«      }	| j                  r4|| j                  v r&t	        | j                  |   «      dkD  rt        d«      ‚|	}
|€|	}t        «       }
n|
|z
  }
t	        |«      t	        |	«      k(  }| j                  j                  |«      }|}|r't        |«      }| j                  j                  ||«       t        |«      }| j                  ||t        |«      ||«       t        |«      }|r|s|}|
r"||k7  r| j                  j                  |||
«       | j                  ||t!        |«      ||«       |}|s/t        |› d�«      }| j                  ||t!        |› d�«      ||«       t        |› d�«      }| j                  ||t        |› d�«      ||«       t        |› d�«      }|r|r|}|r"||k7  r| j                  j                  |||«       | j                  ||t!        |› d�«      ||«       t#        ||||t$        j&                  |¬«      }t#        ||||t$        j&                  |¬«      }t)        |||«      | j*                  |<   yc c}w )aè  
        Adds Q and DQ ops to a tensor whose quantized data type is converted. That is, some consumers may use the
        original data type from the producer, while other consumers use the converted data type.
        This is generally done by adding a sequence of ops that convert from one data type (e.g., uint8) to another (e.g., uint16).

        T_float ---> Quant(to u8) ---> Convert(to u16) ---> Dequant(to float) ---> T_float'
        where Convert(to u16) is equivalent to: ---> Dequant(to float) ---> Quant(to u16) --->

        This function handles the following scenarios:

        1) Tensor T is not a graph output; all consumers use the converted type

            <Producer> ---> Q1 ---> DQ1 ---> Q2 ---> DQ2 ---> <Consumers>

        2) Tensor T is not a graph output; some consumers use the original type, others use the converted type

            <Producer> ---> Q1 -+-> DQ1 ---> <Consumers of original type>
                                |
                                +-> DQ1' ---> Q2 ---> DQ2 ---> <Consumers of converted type>

        3) Tensor T is a graph output; all consumers use the converted type

            <Producer> ---> Q1 ---> DQ1 ---> Q2 ---> DQ2 -+-> <Consumers>
                                                          |
                                                          +-> <Graph output>

        4) Tensor T is a graph output; some consumers use the original type, others use the converted type

            <Producer> ---> Q1 -+-> DQ1 -+-> <Consumers of original type>
                                |        |
                                |        +-> <Graph output>
                                |
                                +-> DQ1' ---> Q2 ---> DQ2 ---> <Consumers of converted type>
        r	   z|Do not currently support converted quant_types in TensorQuantOverrides when the `dedicated_qdq_pair` extra_option is enabledNÚ_convertÚ_convert_cloneró   )Úsetrl   rã   rk   rÃ   rä   r{   rÅ   r   rÇ   r   r×   r   r   Úreplace_input_of_nodesrÝ   r   r   r   r÷   rP   rz   )r<   rŒ   Úfirst_scale_nameÚfirst_zp_nameÚscale_data_typeÚconvert_scale_nameÚconvert_zp_nameÚconvert_recv_nodesr°   Útensor_recv_nodesÚoriginal_recv_nodesÚall_use_convertedrÅ   Úfirst_q_inputÚfirst_q_outputÚfirst_dq_outputÚsecond_q_inputÚsecond_q_outputÚsecond_dq_outputÚoriginal_quantized_valueÚconverted_quantized_values                        r+   Ú%_add_qdq_ops_for_converted_activationz2QDQQuantizer._add_qdq_ops_for_converted_activationP  sÔ  € ôX  °t×7YÑ7YÐZeÒ7fÓ gÑ7f¨t §£Ð7fÑ gÓhÐð ×#Ò#Ø˜t×AÑAÑAÜ�D×6Ñ6°{ÑCÓDÀqÒHô ð Oóð ð 0ÐØÐ%Ø!2ÐÜ"%£%Ñà"5Ð8JÑ"JÐäÐ 2Ó3´sÐ;LÓ7MÑMÐØŸ*™*×4Ñ4°[ÓAˆð $ˆÙÜ2°;Ó?ˆMØ�J‰J×2Ñ2°;ÀÔNä0°Ó=ˆØ×ÑØ˜>Ô+;¸KÓ+HÐJZÐ\iô	
ô
 4°KÓ@ˆÙÑ#4Ø)ˆOÙ ?°kÒ#AØ�J‰J×-Ñ-¨k¸?ÐL_Ô`à×ÑØ˜OÔ-?ÀÓ-LÐN^Ð`mô	
ð )ˆÙ Ü3°{°mÀ8Ð4LÓMˆNØ× Ñ ØØÜ" k ]°.Ð#AÓBØ Øôô 2°[°MÀÐ2JÓKˆØ×ÑØØÜ ˜}¨HÐ5Ó6ØØô	
ô 5¸°}ÀHÐ5MÓNÐÙÑ0Ø*ÐÙÐ"2°kÒ"AØ�J‰J×-Ñ-¨kÐ;KÐM_Ô`Ø×ÑØØÜ + ¨hÐ7Ó8ØØô	
ô $2ØØØØÜ×$Ñ$Ø&ô$
Ð ô %3ØØØØÜ×$Ñ$Ø&ô%
Ð!ô 1HØ$Ð&?ÐASó1
ˆ× Ñ  Ò-ùòS !hs   —I4c           
     ó  — | j                   j                  «       j                  «       D �]Ú  \  }}|| j                  v rŒ|j                  rŒ#t        || j                  j                  «       «      }|r)| j                  ||j                  |j                  «       �n\| j                  |«      }|st        d|› d�«      ‚|j                  €\| j                  ||j                  j                   j"                  |j                  j$                  j"                  |j&                  ¬«       nÒ|j&                  |j                  j                   j&                  k(  sJ ‚| j)                  ||j                  j                   j"                  |j                  j$                  j"                  |j&                  |j                  j                   j"                  |j                  j$                  j"                  |j*                  «       | j                   |= �ŒÝ y)z}
        Adds Q/DQ ops to tensors (activations and weights) that have been marked for quantization by op quantizers.
        z4Quantization parameters are not specified for param zb. In static mode quantization params for inputs and outputs of nodes to be quantized are required.N)r;   )rd   ÚcopyÚitemsrz   r:   r   r{   r†   rð   r7   r9   Ú"_make_tensor_scale_zp_initializersrä   rF   r   rE   rK   rã   rL   r;   r  rH   )r<   rŒ   Útensor_infor†   Útensor_qparam_initializerss        r+   r¹   z%QDQQuantizer._quantize_normal_tensorsé  sÁ  € ð )-×(@Ñ(@×(EÑ(EÓ(G×(MÑ(M×(OÑ$ˆK˜Ø˜d×6Ñ6Ñ6Øà×(Ó(ä*¨;¸¿
¹
×8NÑ8NÓ8PÓQ�ÙØ×6Ñ6°{ÀK×D[ÑD[Ð]h×]mÑ]mÖnà15×1XÑ1XÐYdÓ1eÐ.Ù5Ü(ØRÐS^ÐR_ð `ð óð ð
 2×;Ñ;ÐCà×9Ñ9Ø'Ø6×?Ñ?×EÑE×JÑJØ6×?Ñ?×JÑJ×OÑOØ&1×&;Ñ&;ð	 :õ ð  +×4Ñ4Ð8R×8[Ñ8[×8aÑ8a×8kÑ8kÒkÐkÐkØ×BÑBØ'Ø6×?Ñ?×EÑE×JÑJØ6×?Ñ?×JÑJ×OÑOØ'×1Ñ1Ø6×@Ñ@×FÑF×KÑKØ6×@Ñ@×KÑK×PÑPØ6×KÑKôð ×,Ñ,¨[Ò9ñQ )Pr*   c           
     óv  — | j                   �r¬| j                   j                  «       j                  «       D �]q  \  }}|j                  }|sŒ|j                  | j
                  v sŒ/| j                   |= | j
                  |j                     j                  |j                  «      }| j                  |«      rt        d«      ‚d}d}|| j                  v rD| j                  |   }|j                  r)| j                  ||j                  d«      }|j                  }|€)| j                  ||j                  |j                   «       �Œ| j#                  ||j                  |j                   |j$                  j&                  |j$                  j(                  |j*                  j(                  |«       �Œt | j                   r�Œ«yy)a{  
        Adds Q/DQ ops to tensors that have been marked for quantization by op quantizers.
        Only operates on tensors that want to use the quantization parameter initializers from an upstream tensor.
        For example, a Transpose node's output tensor will typically want to use the same quantization parameter
        initializers as the Transpose node's input.
        zBQuantization parameter shared mode is not supported for weight yetNr  )rd   r  r  r8   r/   rz   rS   r0   Úis_input_a_initializerrä   ry   rF   Ú_make_scale_zp_initializersrH   r   rÔ   rÕ   r  rK   r;   rã   rL   )r<   rŒ   r  Úquant_providerrÿ   Úconverted_qparam_initsrH   Útensor_paramss           r+   rº   z,QDQQuantizer._quantize_sharing_param_tensors  s”  € ð ×&Ó&Ø,0×,DÑ,D×,IÑ,IÓ,K×,QÑ,Q×,SÑ(�˜[Ø!,×!@Ñ!@�Ú! n×&?Ñ&?À4×C[ÑC[Ò&[Ø×0Ñ0°Ð=à&*×&>Ñ&>¸~×?XÑ?XÑ&Y×&jÑ&jØ&×0Ñ0ó'�Oð ×2Ñ2°;Ô?Ü(Ð)mÓnÐnð .2Ð*Ø+/Ð(Ø" d×&>Ñ&>Ñ>Ø(,×(@Ñ(@ÀÑ(M˜Ø(×2Ò2Ø59×5UÑ5UØ +¨]×-DÑ-DÀjó6Ð2ð 4A×3UÑ3UÐ0à-Ð5à×9Ñ9Ø'¨×)CÑ)CÀ_×E\ÑE\öð ×BÑBØ'Ø+×6Ñ6Ø+×3Ñ3Ø2×8Ñ8×BÑBØ2×8Ñ8×=Ñ=Ø2×=Ñ=×BÑBØ0öðA -Tð ×&Ö&r*   c           	     ó.  — | j                   j                  «       D �]w  \  }}|| j                  v rŒ| j                  ||«       t	        || j
                  j                  «       «      }| j
                  j                  |«       | j                  |   j                  }|j                  dk(  r�t        |j                  t        «      s.t        dt        |j                  «      › d|j                  ›�«      ‚t!        |«      }t"        j$                  j'                  d|j(                  g|g||j                  ¬«      }�n?|j                  dv �r|j*                  t"        j,                  j.                  t"        j,                  j0                  t"        j,                  j2                  hv rt5        d|j*                  › d�«      ‚|j(                  |j6                  |j8                  g}t!        |«      }|j:                  �;t"        j$                  j'                  d	||g||j:                  | j<                  ¬
«      }nIt"        j$                  j'                  d	||g|| j<                  ¬«      }nt5        d|j                  ›d�«      ‚| j
                  j?                  |«       �Œz y)zq
        Adds DQ ops (or Cast) for bias tensors that have been marked for quantization by op quantizers.
        ÚCastúUnexpected type z for input=)rã   Úto)NÚDequantizeLinearzUnexpected quantize type z for DequantizeLinear.Nr(  rË   )rÌ   zUnexpected operator type r–   ) re   r  rz   Úquantize_bias_staticr   r{   r†   Úremove_initializerrE   Ú	node_typer—   r;   Úintr˜   rˆ   r/   r   rÍ   rÎ   rÏ   Úq_nameÚ
node_qtyper   r“   ÚBFLOAT16r’   ÚRuntimeErrorrÔ   rÕ   r9   rn   rê   )r<   r«   Ú	bias_infoÚinitÚquant_valuer0   rÜ   Úinputss           r+   r»   z#QDQQuantizer._quantize_bias_tensorsI  sP  € ð %)×$9Ñ$9×$?Ñ$?×$AÑ ˆI�yØ˜D×4Ñ4Ñ4Øà×%Ñ% i°Ô;Ü 	¨4¯:©:×+AÑ+AÓ+CÓDˆDØ�J‰J×)Ñ)¨$Ô/Ø×2Ñ2°9Ñ=×FÑFˆKØ×$Ñ$¨Ò.ô " $§.¡.´#Ô6Ü#Ð&6´t¸D¿N¹NÓ7KÐ6LÈKÐXa×XlÑXlÐWoÐ$pÓqÐqÜ.¨yÓ9�	Ü#Ÿ{™{×4Ñ4ØØ ×'Ñ'Ð(Ø�KØ"Ø—~‘~ð  5ó  ’ð ×&Ñ&Ð*DÒDØ×)Ñ)Ü×$Ñ$×,Ñ,Ü×$Ñ$×-Ñ-Ü×$Ñ$×*Ñ*ð.ñ ô
 'Ð)BÀ;×CYÑCYÐBZÐZpÐ'qÓrÐrØ%×,Ñ,¨k×.DÑ.DÀk×FYÑFYÐZ�Ü.¨yÓ9�	Ø×#Ñ#Ð/Ü#'§;¡;×#8Ñ#8Ø*ØØ"˜Ø!Ø(×-Ñ-Ø#×1Ñ1ð $9ó $‘Lô $(§;¡;×#8Ñ#8Ø*ØØ"˜Ø!Ø#×1Ñ1ð $9ó $‘Lô #Ð%>¸{×?TÑ?TÐ>WÐWXÐ#YÓZÐZØ�J‰J×Ñ Ö-ñc %Br*   c                ó>   — || j                   v xs || j                  v S r6   )rd   re   r�   s     r+   Úis_tensor_quantizedz QDQQuantizer.is_tensor_quantized€  s#   € Ø˜d×6Ñ6Ð6Ò^¸+È×I^ÑI^Ð:^Ð^r*   c                ó�  — |j                   | j                  v rF| j                  |j                      j                  }|j                  |j                  |j
                  fS | j                  ||||«      \  }}}t        |j                   |||t        j                  d«      }t        |d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ã   rz   rE   r-  rÕ   rÔ   Úquantize_initializer_implr   r   ÚInitializerrP   )	r<   r�   ÚqTyper}   râ   rÿ   rí   rÕ   rÔ   s	            r+   rè   z!QDQQuantizer.quantize_initializerƒ  sÏ   € ð �;‰;˜$×2Ñ2Ñ2Ø"×6Ñ6°v·{±{ÑC×LÑLˆOà×&Ñ&Ø×'Ñ'Ø×*Ñ*ðð ð .2×-KÑ-KØ�E˜<Ð):ó.
Ñ*ˆ�w 
ô
 )Ø�K‰KØØØÜ×*Ñ*Øó
ˆô 1HÈÐY]Ð_cÓ0dˆ× Ñ  §¡Ñ-Ø˜g zÐ1Ð1r*   c                óü  — | j                   j                  |«      }|€y| j                  j                  |«      ry| j                  j	                  |«      }| j
                  s|sy|r| j                  j                  ||«      n|}|r#| j                  j                  |«      }|d   d   }t        |«      }t        |j                  «      }	t        ||	«      \  }
}|
st        j                  d|› d|› d|	› �«       yd|fS )aÞ  
        Checks if a given tensor is configured to be quantized per-channel. If so, also returns the channel axis.

        ORT only supports per-channel quantization on static weights (i.e., ONNX initializers). If the user did not provide
        tensor quantization overrides for this tensor, then the value of self.per_channel determines if the weight
        is to be quantized per-channel.

        Params:
            tensor_name: The name of the tensor to check.
            default_axis: The default channel axis. This method checks if the normalized axis is within bounds.
                          Can be overridden via the extra_options 'QDQOpTypePerChannelSupportToAxis'
                          and 'TensorQuantOverrides'.
            op_type: Optional, defaults to None. The operator type that is the only consumer of this weight.
                     Used to access the extra option 'QDQOpTypePerChannelSupportToAxis'.
        Returns:
            A tuple (is_per_channel, axis) in which the first element indicates whether the tensor is
            quantized per-channel and the second element is the channel axis.
            The returned axis is only None if the tensor is not per-channel or the axis is out of bounds.
        )FNr   r9   zAxis z is out-of-range for weight 'z' with rank T)Úinitializersrg   r¨   Úhas_per_tensor_overridesÚhas_per_channel_overridesr|   rm   Úget_per_channel_overridesr   rÃ   Úshaper   rv   rw   )r<   rŒ   r§   Úop_typeÚweight_initializerÚhas_per_chan_overridesr9   Úper_chan_overridesÚweight_nparrayÚweight_rankÚ
axis_valids              r+   rª   z"QDQQuantizer.is_tensor_per_channelª  s  € ð2 "×.Ñ.×2Ñ2°;Ó?ÐØÐ%Øà×&Ñ&×?Ñ?ÀÔLØà!%×!<Ñ!<×!VÑ!VÐWbÓ!cÐØ×ÒÑ(>ØáZaˆt×;Ñ;×?Ñ?ÀÈÔVÐgsˆÙ!Ø!%×!<Ñ!<×!VÑ!VÐWbÓ!cÐØ% aÑ(¨Ñ0ˆDä.Ð/AÓBˆÜ˜.×.Ñ.Ó/ˆÜ)¨$°Ó<Ñˆ
�DÙÜ�O‰O˜e D 6Ð)FÀ{ÀmÐS_Ð`kÐ_lÐmÔnØà�TˆzÐr*   c                óB  — || j                   v r<| j                   |   j                  }|j                  |j                  |j                  fS | j                  |||||«      \  }}}	t        |||	|t        j                  d «      }t        |d d «      | j                   |<   |||	fS r6   )
rz   rE   r-  rÕ   rÔ   Ú quantize_weight_per_channel_implr   r   r9  rP   )
r<   r@   ru   Úchannel_axisr}   râ   rÿ   rí   rÕ   rÔ   s
             r+   rç   z(QDQQuantizer.quantize_weight_per_channelÜ  sÁ   € ð ˜$×2Ñ2Ñ2Ø"×6Ñ6°{ÑC×LÑLˆOà×&Ñ&Ø×'Ñ'Ø×*Ñ*ðð ð .2×-RÑ-RØ˜ |°\ÐCTó.
Ñ*ˆ�w 
ô )ØØØØÜ×*Ñ*Øó
ˆô 1HÈÐY]Ð_cÓ0dˆ× Ñ  Ñ-à˜g zÐ1Ð1r*   c           
     óØ  — || j                   v r#| j                   |   j                  j                  S | j                   |j                     j                  j                  }t        || j                  j                  «       «      }t        |«      }| j                   |j                     j                  |j                  «      j                  }t        || j                  j                  «       «      }t        |«      }| j                  ||||j                  «      \  }	}
}}}}t        ||	|
|t        j                   |j"                  dkD  rdnd||¬«      }t%        |dd«      | j                   |<   |	S )z]
        Quantized the bias. Zero Point == 0 and Scale == Input_Scale * Weight_Scale
        r	   r   N)r+  r.  )rz   rE   r-  r@   rÔ   r   r{   r†   r   r/   rS   r0   Úquantize_bias_static_implrB   r   r   r9  ÚsizerP   )r<   r«   r1  Úweight_scale_namerB  Úweight_scaleÚinput_scale_nameÚinputscale_initializerÚinput_scaleÚquantized_bias_nameÚquantized_bias_scale_nameÚquantized_bias_zp_nameÚbias_scale_datar+  r.  rÿ   s                   r+   r)  z!QDQQuantizer.quantize_bias_staticü  se  € ð ˜×0Ñ0Ñ0Ø×+Ñ+¨IÑ6×?Ñ?×FÑFÐFð !×4Ñ4°Y×5JÑ5JÑK×TÑT×_Ñ_ÐÜ)Ð*;¸T¿Z¹Z×=SÑ=SÓ=UÓVÐÜ,Ð-?Ó@ˆð ×$Ñ$ Y×%9Ñ%9Ñ:×KÑKÈI×L_ÑL_Ó`×kÑkð 	ô ".Ð.>ÀÇ
Á
×@VÑ@VÓ@XÓ!YÐÜ+Ð,BÓCˆð ×*Ñ*¨9°kÀ<ÐQZ×Q_ÑQ_Ó`ñ	
ØØ%Ø"ØØØô )ØØØ%Ø"Ü×*Ñ*Ø ×%Ñ%¨Ò)‰A¨tØØ!ô	
ˆô /FÀoÐW[Ð]aÓ.bˆ× Ñ  Ñ+à"Ð"r*   c                ób  — t        j                  |d   g«      }t        |d   d«      r/|d   j                  t         j                  t         j
                  fvrt        dt        |d   «      › d|›�«      ‚t        j                  |d   g«      }|j                  t         j                  k7  sJ ‚|j                  j                  d| j                  «      }g }|dz   |z   }g }	|dz   |z   }
t        j                  j                  ||||j                  «       j!                  «       «      }| j"                  j%                  |«       |j                  t         j                  k(  rt&        j(                  j*                  }nS|j                  t         j
                  k(  rt&        j(                  j,                  }nt        d	|j                  › d
|›�«      ‚t        j                  j                  |
||	|j/                  d«      j!                  «       «      }| j"                  j%                  |«       t1        ||«      S )zù
        Creates and returns scale and zero-point initializers for the given quantization params. The initializers are
        named:
            - {param_name}_zero_point{init_name_suffix}
            - {param_name}_scale{init_name_suffix}
        rL   rK   Údtyper&  z and param_name=Ú
quant_typeÚ_zero_pointÚ_scalezUnexpected dtype=z for param_name=)éÿÿÿÿ)ÚnpÚarrayÚhasattrrX  Úfloat32Úfloat16rä   rˆ   Úfloat64Údatarg   rt   rÍ   rÎ   Úmake_tensorÚravelÚtolistr{   Úadd_initializerr‘   r   r’   r“   ÚreshaperJ   )r<   Ú
param_nameÚparamsÚinit_name_suffixÚzero_point_valuesÚscale_valuesÚzero_point_typeÚzero_point_shapeÚzero_point_nameÚscale_shaperÔ   Úinit_zprô   Ú
init_scales                 r+   r   z(QDQQuantizer._make_scale_zp_initializers(  sä  € ô ŸH™H f¨\Ñ&:Ð%;Ó<ÐÜ�v˜g‘¨Ô0°F¸7±O×4IÑ4IÔRT×R\ÑR\Ô^`×^hÑ^hÐQiÑ4iÜÐ/´°V¸G±_Ó0EÐ/FÐFVÐWaÐVdÐeÓfÐfÜ—x‘x ¨¡Ð 1Ó2ˆØ×!Ñ!¤R§Z¡ZÒ/Ð/Ð/Ø Ÿ+™+Ÿ/™/¨,¸×8MÑ8MÓNˆàÐØ$ }Ñ4Ð7GÑGˆØˆØ (Ñ*Ð-=Ñ=ˆ
ô —+‘+×)Ñ)Ø˜_Ð.>Ð@Q×@WÑ@WÓ@Y×@`Ñ@`Ó@bó
ˆð 	�
‰
×"Ñ" 7Ô+à×Ñ¤§¡Ò+Ü#×/Ñ/×5Ñ5‰JØ×Ñ¤2§:¡:Ò-Ü#×/Ñ/×7Ñ7‰JäÐ0°×1CÑ1CÐ0DÐDTÐU_ÐTbÐcÓdÐdÜ—[‘[×,Ñ,¨Z¸À[ÐR^×RfÑRfÐglÓRm×RtÑRtÓRvÓwˆ
Ø�
‰
×"Ñ" :Ô.ä% j°'Ó:Ð:r*   c                óš  — | j                   �|| j                   vrt        j                  d|› d�«       y| j                   |   }t        |t        «      st        dt        |«      › d|›d�«      ‚| j                  ||j                  «      }|j                  r| j                  ||j                  d«      nd}t        |||j                  «      S )a  
        Create and returns all scale/zero_point initializers for a given tensor. If the tensor is converted
        to a different quantization type, this function creates two pairs of zp/scale initializers. Otherwise,
        only one pair of zp/scale initializers is created.
        Nz$Quantization parameters for tensor:"z" not specifiedr&  ú for r–   r  )ry   rv   r©   r—   rD   r˜   rˆ   r   rE   rF   rN   rH   )r<   rŒ   r#  Úoriginal_initsÚconverted_initss        r+   r  z/QDQQuantizer._make_tensor_scale_zp_initializersN  sÐ   € ð ×#Ñ#Ð+¨{À$×BZÑBZÑ/ZÜ�L‰LÐ?À¸}ÈOÐ\Ô]Øà×0Ñ0°Ñ=ˆÜ˜-Ô)=Ô>ÜÐ.¬t°MÓ/BÐ.CÀ5ÈÈÐWXÐYÓZÐZà×9Ñ9¸+À}×G]ÑG]Ó^ˆð ×&Ò&ð ×,Ñ,¨[¸-×:QÑ:QÐS]Ô^àð 	ô ,¨N¸OÈ]×MoÑMoÓpÐpr*   c                ó*  — | j                   }d|v r|d   j                  }d|v rd|v r|d   |d   }}nÔ|t        j                  j                  k(  rt        ||j                  d   «      \  }}nš|j                  d|j                  d   «      }|j                  d|j                  d   «      }|j                  d| j                  «      }|j                  d	d
«      }	t        ||	|¬«      \  }
}t        |||
||| j                  «      \  }}t        |||¬«      S )z”
        Calculates quantization parameters (scale/zero-point) given a tensor's min/max range and optional
        user-provided overrides.
        rY  rK   rL   r	   Úrminr   ÚrmaxÚ	symmetricr}   F)r}   r{  )rL   rK   rY  )rt   r7   rÍ   r   ÚFLOAT8E4M3FNr   Úavg_stdrg   Úrange_valueÚis_activation_symmetricr   r   Úmin_real_ranger   )r<   Útensor_dataÚquant_overridesrY  ÚzerorK   ry  rz  r{  r}   ÚqminÚqmaxs               r+   Úcalc_quant_paramszQDQQuantizer.calc_quant_paramse  s  € ð
 ×*Ñ*ˆ
Ø˜?Ñ*Ø(¨Ñ6×BÑBˆJà�oÑ%¨,¸/Ñ*IØ)¨,Ñ7¸ÈÑ9Q�%‰DØœ4×+Ñ+×8Ñ8Ò8Ü1°*¸k×>QÑ>QÐRSÑ>TÓU‰KˆD‘%à"×&Ñ& v¨{×/FÑ/FÀqÑ/IÓJˆDØ"×&Ñ& v¨{×/FÑ/FÀqÑ/IÓJˆDØ'×+Ñ+¨K¸×9UÑ9UÓVˆIØ*×.Ñ.¨~¸uÓEˆLÜ0°È,ÐbkÔl‰JˆD�$Ü*¨4°°t¸TÀ9Èd×NaÑNaÓb‰KˆD�%ä!¨T¸È:ÔVÐVr*   c                ó¼  — | j                   €i S | j                  «        i }| j                   D ]¬  }| j                   |   }t        |t        «      st	        dt        |«      › d|›d�«      ‚| j                  j                  |i ¬«      }| j                  ||«      }d}d}d|v r)| j                  ||d   «      }|d   j                  d«      }t        |||«      ||<   Œ® |S )z´
        Calculates quantization parameters (scale/zero-point) for all tensors in the graph using each tensor's min/max range
        and optional user-provided overrides.
        Nr&  ru  r–   )Údefault_valÚconvertÚ
recv_nodes)r~   Úadjust_tensor_rangesr—   r   r˜   rˆ   r¨   Úget_per_tensor_overridesr†  rg   rD   )r<   ry   rŒ   Útdr‚  rE   rF   rH   s           r+   rx   z$QDQQuantizer.calc_graph_quant_params|  sý   € ð
 ×ÑÐ%ØˆIà×!Ñ!Ô#à ÐØ×-Ô-ˆKØ×#Ñ# KÑ0ˆBÜ˜b¤*Ô-ÜÐ"2´4¸³8°*¸EÀ+ÀÐPQÐ RÓSÐSà"×9Ñ9×RÑRÐS^ÐlnÐRÓoˆOØ×-Ñ-¨b°/ÓBˆHØˆIØ#'Ð à˜OÑ+Ø ×2Ñ2°2°ÀyÑ7QÓR�	Ø'6°yÑ'A×'EÑ'EÀlÓ'SÐ$ä/CÀHÈiÐYmÓ/nÐ Ò,ð .ð  #Ð"r*   r6   )rŒ   r.   )r    r.   r/   r.   r0   r.   )g      ð?)rÑ   r.   rÒ   r.   rÓ   r.   rÔ   r.   rÕ   r.   r9   ú
int | None)rÙ   r.   rÚ   r.   rÛ   r.   rÔ   r.   rÕ   r.   r9   rŽ  )FF)
r�   zonnx.TensorProtor:  úonnx.TensorProto.DataTyper}   Úboolrâ   r�  rT   útuple[str, str, str])rŒ   r.   r§   r,  rA  z
str | NonerT   ztuple[bool, int | None])TF)r@   r.   ru   r�  rJ  r,  r}   r�  râ   r�  rT   r‘  )r«   r.   r1  r?   rT   r.   )Ú )ri  r.   rj  r   rk  r.   rT   rJ   )rŒ   r.   rT   z#QDQTensorScaleZpInitializers | None)r�  r   r‚  zdict[str, Any]rT   r   )rT   zdict[str, QDQTensorQuantParams])$r#   r$   r%   r=   r�   r”   r!   r&   rœ   rž   r¡   r£   r¥   r­   r±   r³   rÁ   rÉ   r×   rÝ   rß   rð   r   r  r¹   rº   r»   r6  rè   rª   rç   r)  r   r  r†  rx   r)   r*   r+   rV   rV   �   s  „ ð óP&òdòð, EIÐVh×VsÑVsó yó:Xó
ó"Tòyó!DòF*ò6ò ò4ð,  ð-àð-ð ð-ð ð	-ð
 ð-ð ð-ð ó-ð:  ð-àð-ð ð-ð ð	-ð
 ð-ð ð-ð ó-ð. uyó;ó*5.ónAiòFW
òr,:ò\0òd5.ón_ð #Ø"'ð%2à ð%2ð )ð%2ð ð	%2ð
  ð%2ð 
ó%2ðV #ð	0àð0ð ð0ð ð	0ð
 
!ó0ðn "Ø"'ð2àð2ð 0ð2ð ð	2ð
 ð2ð  ð2ð 
ó2ó@*#ðZ TVð$;Øð$;Ø'9ð$;ØMPð$;à	ó$;óLqó.Wô.#r*   rV   )3Ú
__future__r   rv   Údataclassesr   Úenumr   Útypingr   Únumpyr]  rÍ   Úonnx.numpy_helperr   r   r‘   Úbase_quantizerr
   r   Ú	calibrater   Úquant_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Úregistryr   r!   r-   r4   r?   rD   rJ   rN   rP   rV   r)   r*   r+   Ú<module>r�     s  ðõ #ã Ý !Ý Ý ã Û Û Ý Ý &ç =Ý !÷÷ ÷ ÷ ÷ õ( )ô˜ô ð ÷ð ó ð÷#ñ #ð ÷ð ó ðð ÷*ð *ó ð*ð ÷ð ó ðð ÷*ð *ó ð*ð ÷fð fó ðfô$V#�=õ V#r*   