
    `i2$                         S SK r S SKrS SKJr  S SKrS SKrS SKrS SKrS SKJ	r
  S SKJr  S SKJrJrJr  SSKJrJrJrJr  SSKJrJrJrJrJrJrJr  SS	KJrJrJrJr  SS
K J!r!  SSK"J#r#  SSK$J%r%   " S S\%5      r&g)    N)Path)onnx_pb)TensorProto)SessionOptionsInferenceSessionGraphOptimizationLevel   )QuantizationModeQuantizedValueTypeQuantizedInitializerQuantizedValue)find_by_nameget_elem_indexget_mul_nodegenerate_identified_filenameattribute_to_kwargtype_to_namequantize_nparray)	QuantTypeonnx_domain__producer____version__)CreateQDQQuantizer)	ONNXModel)ONNXQuantizerc                   V    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rSrg)QDQQuantizer   c                 r    [         R                  " XX#XEXgXX5        / U l        / U l        / U l        / U l        g N)r   __init__tensors_to_quantizetensors_to_quantize_per_channelbias_to_quantizenodes_to_remove)selfmodelper_channelreduce_rangemodestaticweight_qTypeinput_qTypetensors_rangenodes_to_quantizenodes_to_excludeop_types_to_quantizes               k/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxruntime_tools/quantization/qdq_quantizer.pyr!   QDQQuantizer.__init__   s@    tKtUa,AQ	i#% /1, "!    c                 \   [        XR                  R                  5       5      nUbE  UR                  [        R
                  R                  :X  a  U R                  R                  U5        g g XR                  R                  5       ;   a  U R                  U   nUR                  R                  S5      (       aO  UR                  R                  R                  [
        R                  :X  a  U R                  R                  U5        g g g [        R                   " SR#                  U5      5        g )Ntensor_typez\failed to infer the type of tensor: {}. Skip to quantize it. Please check if it is expected.)r   r'   initializer	data_type
onnx_protor   FLOATr"   appendvalue_infoskeystypeHasFieldr6   	elem_typeloggingwarningformat)r&   tensor_nameweightvis       r2   quantize_tensorQDQQuantizer.quantize_tensor&   s    k::+A+A+CD:#9#9#?#??((//< @,,1133!!+.Bww..2773F3F3P3PT_TeTe3e((//< 4f. OOnuu!"r4   c                 H   [        XR                  R                  5       5      nUbF  UR                  [        R
                  R                  :X  a  U R                  R                  X45        g g [        R                  " SR                  U5      5        U R                  U5        g )Nz]only support per-channel quantization on weight. Quantize tensor: {} with per-tensor instead.)r   r'   r7   r8   r9   r   r:   r#   r;   rA   rB   rC   rG   )r&   rD   axisrE   s       r2   quantize_tensor_per_channel(QDQQuantizer.quantize_tensor_per_channel4   s    k::+A+A+CD:#9#9#?#??44;;[<OP @ OOovv!"   -r4   c                 (   [        XR                  R                  5       5      nUbG  UR                  [        R
                  R                  :X  a  U R                  R                  XU45        g g [        R                  " SR                  U5      5        g )NzExpected {} to be a weight)r   r'   r7   r8   r9   r   r:   r$   r;   rA   rB   rC   )r&   	bias_name
input_nameweight_namerE   s        r2   quantize_bias_tensor!QDQQuantizer.quantize_bias_tensor?   sr    i)?)?)AB:#9#9#?#??%%,,i[-QR @ OO8??	JKr4   c                 :    U R                   R                  U5        g r    )r%   r;   )r&   nodes     r2   remove_nodeQDQQuantizer.remove_nodeG   s    ##D)r4   c                 N    U R                   R                  U R                  5        g r    )r'   remove_nodesr%   )r&   s    r2   rX   QDQQuantizer.remove_nodesJ   s    

 4 45r4   c                    U R                   R                  5        H6  nU R                  U5      (       d  M  [        X5      nUR	                  5         M8     U R                  5         U R                  5         U R                  5         U R                  5         U R                   R                  5         [        U R                   R                   l        [        U R                   R                   l        U R                   R                   $ r    )r'   nodesshould_quantizer   quantizequantize_tensorsquantize_weights_per_channelquantize_bias_tensorsrX   remove_unused_constantr   producer_namer   producer_version)r&   rT   op_quantizers      r2   quantize_modelQDQQuantizer.quantize_modelM   s    JJ$$&D##D))1$=%%' '
 	))+""$

))+)5

&,7

)zzr4   c                 
   X R                   R                  5       ;   af  [        U R                  R	                  5       U   5      S:X  a<  U R                  R                  U5      (       d  U R                  R                  X5        gg)Nr	   TF)quantization_paramsr=   lenr'   input_name_to_nodesis_graph_outputreplace_output_of_all_nodes)r&   upstream_output_nameoutput_names      r2   try_replacing_upstream_output*QDQQuantizer.try_replacing_upstream_output^   se    227799tzz--/0DEF!Kzz))+66JJ223GUr4   c                    U R                    GH  nXR                  R                  5       ;   a  M#  [        XR                  R                  5       5      nUb  U R                  X R                  5      u  p4nX5U/nUS-   n[        R                  R                  SXg/US-   5      nU R                  R                  U5        U R                  R                  XS-   5        M  U R                  U5      u  pn  n
U	S:X  a  [        SR                  U5      5      e[        R                  R                  SXU/US-   /US-   5      n[        R                  R                  SUS-   XT/US-   /US-   5      nU R                  R                  XS-   5        U R                  R!                  X/5        [#        XS-   XT[$        R&                  5      nXR                  U'   GM     g )N_DequantizeLinearDequantizeLinearFzQuantization parameters are not specified for param {}.In static mode quantization params for inputs and outputs of nodes to be quantized are required.QuantizeLinear_QuantizeLinear)r"   quantized_value_mapr=   r   r'   r7   quantize_weightr,   onnxhelper	make_nodeadd_nodereplace_input_of_all_nodes_get_quantization_params
ValueErrorrC   	add_nodesr   r   Input)r&   rD   r7   q_weight_namezp_name
scale_nameinputsrn   rT   
data_found_qlinear_nodedequant_nodequantized_values                 r2   r^   QDQQuantizer.quantize_tensorsf   s   33K66;;==&{JJ4J4J4LMK&595I5I+WhWh5i2
'W=),??{{,,-?-8;N-NP

##D)

55kQdCde8<8U8UVa8b5
A&${,. .
  ${{445EahGi6ADU6U5VXcfwXw y#{{445G6ADU6UWa5k6ADW6W5X5@CV5V X 

55kQdCde

$$l%AB"0L]>]_i1C1I1I#K8G((5G 4r4   c           	      j   U R                    GH"  u  pnXR                  R                  5       ;   a  M&  U R                  XU5        U R                  R                  [        XR                  R                  5       5      5        U R                  U   nUR                  UR                  UR                  /nUR                  b/  [        R                  R                  SXQ/US-   UR                  S9nO%[        R                  R                  SXQ/US-   5      nU R                  R                  U5        GM%     g )Nrs   rr   rJ   )r$   rv   r=   quantize_bias_staticr'   remove_initializerr   r7   q_namer   r   rJ   rx   ry   rz   r{   )r&   rN   rO   rP   quant_valuer   r   s          r2   r`   "QDQQuantizer.quantize_bias_tensors   s   262G2G.I;4499;;%%i[IJJ)),y**BXBXBZ*[\229=K!((+*@*@+BUBUVF+#{{445G5;[5>AT5T:E:J:J  5  L
  ${{445GQ\5>AT5T VJJ-! 3Hr4   c           	         U R                   S:  a$  [        U R                  5      S:  a  [        S5      eU R                   H  u  pU R	                  U[
        R                  R                  U5      u  p4nX5U/nUS-   n[        R                  R                  SXg/US-   US9nU R                  R                  U5        U R                  R                  X5        M     g )N   r   zLPer-Channel support with QDQ format requires onnx opset version 13 or above.rr   rs   r   )opset_versionri   r#   r~   quantize_weight_per_channelr9   r   INT8rx   ry   rz   r'   r{   r|   )	r&   rP   rJ   r   r   r   r   rn   rT   s	            r2   r_   )QDQQuantizer.quantize_weights_per_channel   s    "s4+O+O'PST'Tkll!%!E!EK*.*J*J;XbXnXnXsXsKO+Q'FZ '2F%(;;K;;(();)/)47J)J.2 ) 4D JJ% JJ11+K "Fr4   )r$   r%   r"   r#   N)__name__
__module____qualname____firstlineno__r!   rG   rK   rQ   rU   rX   re   ro   r^   r`   r_   __static_attributes__ r4   r2   r   r      s=    ""	.L*6 "$HL.&Lr4   r   )'osstructpathlibr   numpynprA   rx   onnx.numpy_helperr   r9   r   onnxruntimer   r   r   quant_utilsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   registryr   
onnx_modelr   onnx_quantizerr   r   r   r4   r2   <module>r      s_    
       &  P P c c V  V  V J J ( ! )TL= TLr4   