+
    9&Ïj‰7 ã            
      óž  € ^ RI Ht ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	H
t ^ RIt^ RIt^ RIHtHtHtHt ^ RIHtHtHtHt ^RIHt ^RIHtHt ^RIHt ^RI H!t!H"t" ]PF                  ! R	]PH                  R
7       ]PJ                  ! ]&4      t' ! R R4      t( ! R R](4      t) ! R R](4      t* ! R R](4      t+ ! R R](4      t, ! R R](4      t- ! R R](4      t.R t/ ! R R4      t0R R lt1R R lt2R  R! lt3R" t4 ! R# R$4      t5 ! R% R&4      t6 ! R' R(4      t7R) t8R* t9R+ t:]&R,8X  Ed    ]:! 4       t;];Px                  '       d   ]'P{                  ]P|                  4       ];P~                  t@];P‚                  tB]!];P†                  ,          tC];Pˆ                  '       d   ]E! ];Pˆ                  4      MRCtD];PŒ                  '       d   ]E! ];PŒ                  4      MRtF]PŽ                  P‘                  ]B4      '       d"   ]'P“                  R-]B R.24       ]J! R-]B R.24      h];P–                  '       d*   ];P˜                  R/8X  d   ]'P›                  R04       R1];nK        ]Pœ                  ! ]@4      tO];P˜                  R/8X  d#   ],! ];P                   ];P¢                  ]D]FR27      tREMN];P˜                  R38X  d:   ]-! ];P                   ];P–                  ];P¦                  ]C]D]F];P¢                  R47      tREM];P˜                  R58X  d   ])! ]DR67      tRMé];P˜                  R78X  d   ]*! ]DR67      tRMÎ];P˜                  R88X  d   ]+! ];P                   ]DR97      tRM¨];P˜                  R:8X  dƒ   ]C]!P¨                  8X  d   ]'P›                  R;4       ]!Pª                  tC]@tO];P¬                  e   ];P¬                  R<8X  d   R=tVMR>tVMR=tV].! ];P®                  ];P°                  ];P²                  ]VR?7      tRM]Z! R@];P˜                   24      h]7! ]O];P¢                  ];P¦                  ];P¶                  ];P¸                  ]RRA7      t]]]P½                  4        ]]Pž                  P¿                  ]BRB4       R# R# )Dé    )ÚannotationsN)Ú
GraphProtoÚ
ModelProtoÚ	NodeProtoÚTensorProto)Úquantize_matmul_2bitsÚquantize_matmul_4bitsÚquantize_matmul_8bitsÚquantize_qdq_matmul_4bits)ÚCalibrationDataReader)Úgptq_quantizeÚrtn_quantize)Ú	ONNXModel)ÚQuantFormatÚattribute_to_kwargz2%(asctime)s %(name)s [%(levelname)s] - %(message)s)ÚformatÚlevelc                  ó&   € ] tR t^%tRR R lltRtR# )ÚWeightOnlyQuantConfigNc          
     ó,   € V ^8„  d   QhRRRRRRRRR	R
/# )é   Ú	algorithmÚstrÚquant_formatr   Úop_types_to_quantizeútuple[str, ...] | NoneÚ
quant_axesú"tuple[tuple[str, int], ...] | NoneÚcustomized_weight_configúdict | None© )r   s   "Úi/opt/data/cache/scratch/pip-target-elquwxdg/lib/python/onnxruntime/quantization/matmul_nbits_quantizer.pyÚ__annotate__Ú"WeightOnlyQuantConfig.__annotate__&   sC   € ÷ Añ AàðAð "ðAð 5ð	Að
 7ðAð #.ñAó    c                óœ   € Wn         W n        V'       d   \        V4      MR0V n        V'       d   \	        V4      MR^ R^/V n        WPn        R# )a  This is the Base class for Weight Only blockwise quantization Configuration.

Args:
    algorithm:
        weight only quantize algorithm name.
    quant_format: QuantFormat{QOperator, QDQ}.
        QOperator format quantizes the model with quantized operators directly.
        QDQ format quantize the model by inserting QuantizeLinear/DeQuantizeLinear on the tensor.
    op_types_to_quantize (optional):
        set of operator types to quantize. Default {MatMul}
    quant_axes (dict[str, int], optional):
        op:axis, which axis to quantize for an op. Default {MatMul: 0, Gather: 1}
    customized_weight_config:
        customized weight config for nodes if needed. It is dictionary with node name as key,
        and the value is a dict of customized config.
ÚMatMulÚGatherN)r   r   Úsetr   Údictr   r   )Úselfr   r   r   r   r   s   &&&&&&r"   Ú__init__ÚWeightOnlyQuantConfig.__init__&   sE   € ð0 #ŒØ(ÔßAU¤CÐ(<Ô$=Ð\dÐ[eˆÔ!ß.8œ$˜zÔ*¸xÈÈHÐVWÐ>XˆŒØ(@Ö%r%   )r   r   r   r   r   )NNN)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r,   Ú__static_attributes__r!   r%   r"   r   r   %   s   † ÷Aó Ar%   r   c                  óN   a € ] tR t^EtR]P
                  RR3R V 3R llltRtV ;t# )ÚRTNWeightOnlyQuantConfigNc               ó    € V ^8„  d   QhRRRR/# ©r   r   r   r   r    r!   )r   s   "r"   r#   Ú%RTNWeightOnlyQuantConfig.__annotate__F   s    € ÷ "ñ "ð 5ð	"ð
 #.ñ"r%   c                ó~   <€ V\         P                  8X  g   Q R4       hVf   / p\        SV `  RVVVR7       Wn        R# )aø  
This is a class for round-to-nearest (RTN) algorithm Weight Only Quant Configuration.
RTN is the most straightforward way to quantize weight using scale maps.

Args:
    ratios:
        percentile of clip. Defaults to {}.
    quant_format (QuantFormat{QOperator, QDQ}, optional):
        QOperator format quantizes the model with quantized operators directly.
        QDQ format quantize the model by inserting QuantizeLinear/DeQuantizeLinear on the tensor.
        Defaults to QuantFormat.QOperator.
    op_types_to_quantize (optional):
        set of operator types to quantize.
    customized_weight_config:
        customized weight config for nodes if needed. It is dictionary with node name as key,
        and the value is a dict of customized config.
z"RTN only supports QOperator formatNÚRTN©r   r   r   r   ©r   Ú	QOperatorÚsuperr,   Úratios©r+   r>   r   r   r   Ú	__class__s   &&&&&€r"   r,   Ú!RTNWeightOnlyQuantConfig.__init__F   sN   ø€ ð0 œ{×4Ñ4Ô4ÐZÐ6ZÓZÐ4àŠ>ØˆFÜ‰ÑØØ%Ø!5Ø%=ð	 	ô 	
ð Žr%   ©r>   ©	r.   r/   r0   r1   r   r<   r,   r2   Ú__classcell__©r@   s   @r"   r4   r4   E   s&   ø† ð Ø ×*Ñ*Ø7;Ø04÷"÷ "ð "r%   r4   c                  óN   a € ] tR t^ktR]P
                  RR3R V 3R llltRtV ;t# )ÚKQuantWeightOnlyQuantConfigNc               ó    € V ^8„  d   QhRRRR/# r6   r!   )r   s   "r"   r#   Ú(KQuantWeightOnlyQuantConfig.__annotate__l   s    € ÷ ñ ð 5ð	ð
 #.ñr%   c                ó~   <€ V\         P                  8X  g   Q R4       hVf   / p\        SV `  RVVVR7       Wn        R# )aî  
This is a class for k-quant algorithm Weight Only Quant Configuration.

Args:
    ratios:
        percentile of clip. Defaults to {}.
    quant_format (QuantFormat{QOperator, QDQ}, optional):
        QOperator format quantizes the model with quantized operators directly.
        QDQ format quantize the model by inserting QuantizeLinear/DeQuantizeLinear on the tensor.
        Defaults to QuantFormat.QOperator.
    op_types_to_quantize (optional):
        set of operator types to quantize.
z&k-quant only supports QOperator formatNÚk_quantr:   r;   r?   s   &&&&&€r"   r,   Ú$KQuantWeightOnlyQuantConfig.__init__l   sN   ø€ ð( œ{×4Ñ4Ô4Ð^Ð6^Ó^Ð4àŠ>ØˆFÜ‰ÑØØ%Ø!5Ø%=ð	 	ô 	
ð Žr%   rB   rC   rE   s   @r"   rG   rG   k   s&   ø† ð Ø ×*Ñ*Ø7;Ø04÷÷ ð r%   rG   c                  óV   a € ] tR t^�tRR^€RRR]P
                  R3R V 3R llltRtV ;t# )ÚGPTQWeightOnlyQuantConfigNg{®Gáz„?FTc               ó    € V ^8„  d   QhRRRR/# )r   Úcalibration_data_readerzCalibrationDataReader | Noner   r   r!   )r   s   "r"   r#   Ú&GPTQWeightOnlyQuantConfig.__annotate__Ž   s   € ÷ /%ñ /%à!=ð/%ð 5ñ/%r%   c	                ó¬   <€ V\         P                  8X  g   Q R4       h\        S	V `  RVVR7       Wn        W n        W0n        W@n        WPn        W`n	        R# )af  
This is a class for GPTQ algorithm Weight Only Quant Configuration.
GPTQ algorithm provides more accurate quantization but requires more computational resources.

Args:
    calibration_data_reader:
        a calibration data reader. It enumerates calibration data and generates inputs for the original model.
    percdamp:
        percent of the average Hessian diagonal to use for dampening.
    block_size (int, optional):
        channel number in one block to execute a GPTQ quantization iteration.
    actorder (bool, optional):
        whether rearrange Hessian matrix considering the diag's value.
    mse (bool, optional):
        whether get scale and zero point with mse error.
    perchannel (bool, optional):
        whether quantize weight per-channel.
    quant_format (QuantFormat{QOperator, QDQ}, optional):
        QOperator format quantizes the model with quantized operators directly.
        QDQ format quantize the model by inserting QuantizeLinear/DeQuantizeLinear on the tensor.
        Defaults to QuantFormat.QOperator.
    op_types_to_quantize (optional):
        set of operator types to quantize.
z#GPTQ only supports QOperator formatÚGPTQ)r   r   r   N)
r   r<   r=   r,   rP   ÚpercdampÚ
block_sizeÚactorderÚmseÚ
perchannel)
r+   rP   rT   rU   rV   rW   rX   r   r   r@   s
   &&&&&&&&&€r"   r,   Ú"GPTQWeightOnlyQuantConfig.__init__Ž   s\   ø€ ðF œ{×4Ñ4Ô4Ð[Ð6[Ó[Ð4ä‰ÑØØ%Ø!5ð 	ô 	
ð
 (?Ô$Ø ŒØ$ŒØ ŒØŒØ$Žr%   )rV   rU   rP   rW   rT   rX   rC   rE   s   @r"   rN   rN   �   s4   ø† ð AEØØØØØØ ×*Ñ*Ø7;÷/%÷ /%ð /%r%   rN   c                  óR   a € ] tR t^Àt^€^^]P
                  RR3R V 3R llltRtV ;t# )ÚHQQWeightOnlyQuantConfigNc               ó    € V ^8„  d   QhRRRR/# )r   r   r   r   r   r!   )r   s   "r"   r#   Ú%HQQWeightOnlyQuantConfig.__annotate__Á   s    € ÷ 'ñ 'ð 5ð'ð 7ñ'r%   c                óŠ   <€ V\         P                  8X  g   Q R4       h\        SV `  RVVVR7       Wn        W n        W0n        R# )aƒ  
This is a class for HQQ algorithm Weight Only Quant Configuration.
HQQ algorithm quant weight without needing calibrate data.

Args:
    block_size (int, optional):
        channel number in one block to execute a HQQ quantization iteration.
    bits (int, optional):
        how many bits to represent weight.
    axis (int, optional):
        0 or 1. which axis to quantize. https://arxiv.org/pdf/2309.15531.pdf
    quant_format (QuantFormat{QOperator, QDQ}, optional):
        QOperator format quantizes the model with quantized operators directly.
        QDQ format quantize the model by inserting QuantizeLinear/DeQuantizeLinear on the tensor.
        Defaults to QuantFormat.QOperator.
    op_types_to_quantize (optional):
        set of operator types to quantize.
    quant_axes (dict[str, int], optional):
        op:axis, which axis to quantize for an op. Default {MatMul: 0, Gather: 1}
z"HQQ only supports QOperator formatÚHQQ©r   r   r   r   N)r   r<   r=   r,   rU   ÚbitsÚaxis)r+   rU   ra   rb   r   r   r   r@   s   &&&&&&&€r"   r,   Ú!HQQWeightOnlyQuantConfig.__init__Á   sN   ø€ ð: œ{×4Ñ4Ô4ÐZÐ6ZÓZÐ4ä‰ÑØØ%Ø!5Ø!ð	 	ô 	
ð %ŒØŒ	ØŽ	r%   )rb   ra   rU   rC   rE   s   @r"   r[   r[   À   s,   ø† ð ØØØ ×*Ñ*Ø7;Ø9=÷'÷ 'ð 'r%   r[   c                  óV   a € ] tR t^ët^€RR]P
                  RR^R3R V 3R llltRtV ;t# )ÚDefaultWeightOnlyQuantConfigFNc               ó4   € V ^8„  d   QhRRRRRRRRR	R
RRRR/# )r   rU   ÚintÚis_symmetricÚboolÚaccuracy_levelú
int | Noner   r   r   r   ra   Úchannel_wised_quantizer!   )r   s   "r"   r#   Ú)DefaultWeightOnlyQuantConfig.__annotate__ì   sY   € ÷ .kñ .kàð.kð ð.kð #ð	.kð 5ð.kð 7ð.kð ð.kð !%ñ.kr%   c	                ó¾   <€ \         S	V `  RVVVR7       Wn        W n        Wpn        W0n        W€n        V'       d#   V\        P                  8X  d   \        R4      hR# R# )ax  
This is a class for weight only affine quantization configuration.

Args:
    block_size (int, optional):
        channel number in one block to execute an affine quantization iteration.
    is_symmetric (bool, optional):
        whether quantize weight symmetrically.
    accuracy_level (int, optional):
        Accuracy level of the 4-bit quantized MatMul computation.
        Refer to the MatMulNBits contrib op's 'accuracy_level' attribute for details.
        (https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftmatmulnbits)
    quant_format (QuantFormat{QOperator, QDQ}, optional):
        QOperator format quantizes the model with quantized operators directly.
        QDQ format quantize the model by inserting QuantizeLinear/DeQuantizeLinear on the tensor.
        Defaults to QuantFormat.QOperator.
    op_types_to_quantize (optional):
        set of operator types to quantize.
    quant_axes (dict[str, int], optional):
        op:axis, which axis to quantize for an op. Default {MatMul: 0, Gather: 1}
    bits (int, optional):
        number of bits per element after quantization. Default 4.
ÚDEFAULTr`   zAQuantFormat.QOperator is not supported channel_wised_quantize yetN)
r=   r,   rU   rh   ra   rj   rl   r   r<   ÚNotImplementedError)
r+   rU   rh   rj   r   r   r   ra   rl   r@   s
   &&&&&&&&&€r"   r,   Ú%DefaultWeightOnlyQuantConfig.__init__ì   sg   ø€ ôD 	‰ÑØØ%Ø!5Ø!ð	 	ô 	
ð %ŒØ(ÔØŒ	Ø,ÔØ&<Ô#ß! l´k×6KÑ6KÔ&KÜ%Ð&iÓjÐjñ 'LÑ!r%   )rj   ra   rU   rl   rh   rC   rE   s   @r"   re   re   ë   s5   ø† ð Ø"Ø%)Ø ×*Ñ*Ø7;Ø9=ØØ',÷.k÷ .kð .kr%   re   c                  ó8   a € ] tR tRtRV 3R lltR tR tRtV ;t# )ÚNVAWQWeightOnlyQuantConfigi  c                ó€  <€  ^ RI p^ RIHp WPn         W`n         ^ RIHp Ypn         ^ RIHpH	p	 Y€n        Y�n	        \        ST `-  R\        P                  RRR7       T P                   P                  T P                   P                  P!                  4       '       d   RMR4      p
T P#                  TTT^ ^RT
RRR^€RR7      pY°n        Y@n        R#   \         d    \	        R4       \        R4      Rhi ; i  \         d    \	        R4       \        R4      Rhi ; i  \         d    \	        R	4       \        R
4      Rhi ; i)a  
Configuration for the nvidia_awq quantization method.

Args:
    tokenizer_dir (str): pathof the tokenizer dir.
    dataset_name (str): Name of the dataset.
    cache_dir (str): Directory for caching.
    calibration_method (str): calib method for nvidia_awq.
N)Ú
DataLoaderzfError: The 'torch' library is required but not installed. Please install it using 'pip install torch'.z torch is not installed. Exiting.)Úload_datasetzlError: The 'datasets' library is required but not installed. Please install it using 'pip install datasets'.z#datasets is not installed. Exiting.)Ú
AutoConfigÚAutoTokenizerztError: The 'transformers' library is required but not installed. Please install it using 'pip install transformers'.z'transformers is not installed. Exiting.Ú
nvidia_awqr`   ÚcudaÚcpui   TF)Údataset_nameÚ
model_nameÚ	cache_dirÚ
calib_sizeÚ
batch_sizerU   ÚdeviceÚuse_fp16Úuse_buffer_shareÚadd_past_kv_inputsÚmax_calib_rows_to_loadÚadd_position_ids)ÚtorchÚtorch.utils.dataru   ÚImportErrorÚprintÚdatasetsrv   Útransformersrw   rx   r=   r,   r   ÚQDQr�   rz   Úis_availableÚget_calib_inputsrP   Úcalibration_method)r+   Útokenizer_dirr|   r~   r�   r‡   ru   rv   rw   rx   r�   Úcalib_inputsr@   s   &&&&&       €r"   r,   Ú#NVAWQWeightOnlyQuantConfig.__init__  sb  ø€ ð"
	LÛÝ3àŒJØ(ŒOð	OÝ-à ,Ôð		Sß>à(ŒOØ!.Ôô 	‰ÑØ"Ü$Ÿ™Ø!%Øð	 	ô 	
ð —‘×"Ñ"¨T¯Z©Z¯_©_×-IÑ-I×-KÒ-K¡6ÐQVÓWˆà×,Ñ,Ø%Ø$ØØØØØØØ"Ø#Ø#&Ø!ð -ó 
ˆð (4Ô$Ø"4Öøôo ô 	LÜØxôô Ð@ÓAÀtÐKð		Lûô ô 	OÜØ~ôô ÐCÓDÈ$ÐNð		Oûô ô 	SÜð Gôô ÐGÓHÈdÐRð		Sús!   ƒC šC4 §D Ã#C1Ã4#DÄ#D=c	           
     	ó¼  € V P                   p	Tp
Tp\        V\        4      '       d;   V	P                  W%V	P                  R 7      p
V	P                  W5V	P                  R 7      pRV
P                  4       RVP                  4       /pV'       dO   VP                  4       P                  R4      ^,
          pVP                  V^ 8H  ^4       VP                  4       VR&   V'       dü   V'       d   V	P                  MV	P                  pV
P                  w  ppVP                  pVP                  VP                  VP                  ,          pp\!        VP"                  4       F~  pT	P%                  TTV'       d   TM^ VVVR 7      pT	P%                  TTV'       d   TM^ VVVR 7      pVP'                  RV R2VP                  4       RV R2VP                  4       /4       K€  	  V# ))r�   ÚdtypeÚ	input_idsÚattention_maskÚposition_idszpast_key_values.z.keyz.valueéÿÿÿÿ)r‡   Ú
isinstanceÚlistÚtensorÚint64Ú
contiguousÚlongÚcumsumÚmasked_fill_Úfloat16Úfloat32ÚshapeÚmax_position_embeddingsÚnum_key_value_headsÚhidden_sizeÚnum_attention_headsÚrangeÚnum_hidden_layersÚzerosÚupdate)r+   ÚconfigÚinput_ids_argÚattention_mask_argr„   r�   r‚   rƒ   r†   r‡   r–   r—   Úinputsr˜   Útorch_dtyper€   Úsequence_lengthÚmax_sequence_lengthÚ	num_headsÚ	head_sizeÚiÚpast_keyÚ
past_values   &&&&&&&&&              r"   Úmake_model_inputÚ+NVAWQWeightOnlyQuantConfig.make_model_inputn  sË  € ð —
‘
ˆà!ˆ	Ø+ˆä�m¤T×*Ò*ØŸ™ ]ÈÏÉ˜ÓUˆIØ"Ÿ\™\Ð*<ÐSX×S^ÑS^˜\Ó_ˆNð ˜×-Ñ-Ó/Ø˜n×7Ñ7Ó9ð
ˆ÷
 Ø)×.Ñ.Ó0×7Ñ7¸Ó;¸aÕ?ˆLØ×%Ñ% n¸Ñ&9¸1Ô=Ø%1×%<Ñ%<Ó%>ˆF�>Ñ"çß+3˜%Ÿ-š-¸¿¹ˆKØ*3¯/©/Ñ'ˆJ˜Ø"(×"@Ñ"@Ðà×*Ñ*Ø×"Ñ" f×&@Ñ&@Õ@ð !ˆIô ˜6×3Ñ3Ö4�Ø Ÿ;™;ØØß+;Ñ'ÀØØ!Ø%ð 'ó �ð #Ÿ[™[ØØß+;Ñ'ÀØØ!Ø%ð )ó �
ð —‘à*¨1¨#¨TÐ2°H×4GÑ4GÓ4IØ*¨1¨#¨VÐ4°j×6KÑ6KÓ6Mðöñ# 5ð0 ˆr%   c                	ó   € V P                   pV P                  pV P                  pVP                  VR VR R7      pVP                  VR VR R7      pVP	                  RR/4       VP
                  Vn        WK8:  g   Q R4       hRV9   d'   V! RRRR	7      P                  \        V4      4      pR
pM#RV9   d   V! RRR7      pRpM\        RV R24      hVV,          RV pVP                  VRR R VR7      pVP                  V4      pVR,          pVR,          pV P                  pV! VVRR7      pV! VVRR7      p\        VP                  4      \        VP                  4      8X  g   Q h\        V4      \        V4      8X  g   Q hWE,          p. p\        V4       F'  w  ppVP!                  V4       VV^,
          8X  g   K'   M	  . p\        V4       F'  w  ppVP!                  V4       VV^,
          8X  g   K'   M	  \#        RV R\        V4       R\        V4       R24       . p\        V4       F  p VV ,          p!VV ,          p"V P%                  VV!V"V
VVV	V4      p#V#P'                  4        U$U%u/ uF%  w  p$p%V$V%P)                  4       P+                  4       bK'  	  p#p$p%VP!                  V#4       K�  	  \#        R\        V4       R24       V# u up%p$i )T)Úuse_auth_tokenr~   Útrust_remote_codeÚ	pad_tokenz[PAD]z8calib size should be no more than max_calib_rows_to_loadÚcnnÚcnn_dailymailz3.0.0Útrain)ÚnameÚsplitÚarticleÚpilezmit-han-lab/pile-val-backupÚ
validation)rÃ   Útextz	dataset "z" not supportedNÚpt)Úreturn_tensorsÚpaddingÚ
truncationÚ
max_lengthr–   r—   F)r€   Úshufflez/
--Quantize-Script-- number_of_batched_samples=z, batch-input-ids-list-len=z, batched_attention_mask=Ú
z0
--Quantize-Script-- number of batched inputs = )rw   rx   rv   Úfrom_pretrainedÚadd_special_tokensÚ	eos_tokenr¾   Úselectr©   Ú
ValueErrorÚbatch_encode_plusÚtoru   ÚlenÚdatasetÚ	enumerateÚappendrŠ   r¹   Úitemsr{   Únumpy)&r+   r|   r}   r~   r   r€   rU   r�   r‚   rƒ   r„   r…   r†   Úauto_configÚauto_tokenizerrv   r­   Ú	tokenizerÚdataset2ÚcolumnÚbatch_encodedÚbatch_encoded_input_idsÚbatch_encoded_attention_maskÚdata_loaderÚcalib_dataloader_input_idsÚcalib_dataloader_attention_maskÚnumber_of_batched_samplesÚbatched_input_idsÚidxÚdataÚbatched_attention_maskÚbatched_inputs_listr¶   r–   r—   r°   Ú
input_nameÚtorch_tensors&   &&&&&&&&&&&&&                         r"   r�   Ú+NVAWQWeightOnlyQuantConfig.get_calib_inputs¯  sC  € ð  —o‘oˆØ×+Ñ+ˆØ×(Ñ(ˆà×,Ñ,Ø t°yÐTXð -ó 
ˆð #×2Ñ2Ø t°yÐTXð 3ó 
ˆ	ð 	×$Ñ$ k°7Ð%;Ô<Ø'×1Ñ1ˆ	ÔàÔ3ÐoÐ5oÓoÐ3à�LÔ Ù# O¸'ÈÔQ×XÑXÔY^Ð_uÓYvÓwˆHØ‰FØ�|Ô#Ù#Ð$AÈÔVˆHØ‰Fä˜y¨¨°oÐFÓGÐGà˜FÕ# K ZÐ0ˆØ!×3Ñ3Ø T°4ÀDÐU_ð 4ó 
ˆð &×(Ñ(¨Ó0ˆØ"/°Õ"<ÐØ'4Ð5EÕ'FÐ$ð —o‘oˆá%0Ð1HÐU_ÐinÔ%oÐ"Ù*5Ø(°ZÈô+
Ð'ô Ð-×5Ñ5Ó6¼#Ð>]×>eÑ>eÓ:fÔfÐfÐfÜÐ-Ó.´#Ð6UÓ2VÔVÐVÐVà$.Õ$<Ð!àÐÜ"Ð#=Ö>‰IˆC�Ø×$Ñ$ TÔ*ØÐ0°1Õ4Ö5Ùñ ?ð
 "$ÐÜ"Ð#BÖC‰IˆC�Ø"×)Ñ)¨$Ô/ØÐ0°1Õ4Ö5Ùñ Dô
 	Ø>Ð?XÐ>Yð Z(Ü(+Ð,=Ó(>Ð'?Ð?XÔY\Ð]sÓYtÐXuÐuwðyô	
ð
 !ÐÜÐ0Ö1ˆAØ)¨!Õ,ˆIØ3°AÕ6ˆNà×*Ñ*ØØØØ"ØØØ Ø ó	ˆFð ^d×]iÑ]iÔ]kÔlÑ]kÑAYÀÈ\�j ,×"2Ñ"2Ó"4×":Ñ":Ó"<Ò<Ñ]kˆFÑlØ×&Ñ& vÖ.ñ 2ô" 	ÐAÄ#ÐFYÓBZÐA[Ð[]Ð^Ô_Ø"Ð"ùó	 ms   É-+K
)rw   rx   ru   rP   r�   rv   r‡   )r¿   ú./cacheÚawq_lite)	r.   r/   r0   r1   r,   r¹   r�   r2   rD   rE   s   @r"   rs   rs     s   ø† ÷N5ò`?÷Ba#ð a#r%   rs   c                ó`   € \        V\        P                  ! W,          4      ,          4      V 8H  # ©N)rg   ÚnpÚceil)Úval1Úval2s   &&r"   Úis_divisiblerø     s"   € Üˆt”b—g’g˜d�kÓ*Õ*Ó+¨tÑ3Ð3r%   c                  ób   € ] tR tRtR R lt]RR R ll4       t]R 4       tRR ltR	 R
 lt	Rt
R# )ÚHQQWeightOnlyQuantizeri  c               ó   € V ^8„  d   QhRR/# )r   r­   r[   r!   )r   s   "r"   r#   Ú#HQQWeightOnlyQuantizer.__annotate__  s   € ÷ ñ à(ñr%   c                	ó   € Wn         R # ró   ©r­   ©r+   r­   s   &&r"   r,   ÚHQQWeightOnlyQuantizer.__init__  ó	   € ð Žr%   Nc               ó$   € V ^8„  d   QhRRRRRR/# )r   Úmin_maxz	list[int]rb   rg   Ú
opt_paramsr    r!   )r   s   "r"   r#   rü      s*   € ÷ 2ñ 2ð ð	2ð
 ð2ð  ñ2r%   c                	óX  a€ ^ RI oVf   RRRRRRR^/MTpVR,          VR,          VR,          VR,          3w  rxršV P                  '       d   SP                  MSP                  pV P	                  V4      pVP	                  V4      pVP	                  V4      pV3V3R	 llpR
p\        V
4       Fó  pSP                  WÁ,          V,           4      P                  V^ ,          V^,          4      pVV,
          V,          pV! VV,
          V4      pSP                  VVV,
          V,          ,
          VRR7      pW‰,          p\        SP                  VV,
          4      P                  4       4      pV'       d"   \        V\        P                  ! V^4      4       VV8  d   TpKó   M	  ????W3# )r   NÚlp_normgffffffæ?Úbetag      $@Úkappag)\�Âõ(ð?Úitersc           
     óÚ  <€ V^8X  dY   SP                  V 4      SP                  P                  P                  SP	                  V 4      RV,          ,
          4      ,          # SP                  V 4      SP                  P                  P                  SP	                  V 4      RV,          SP                  SP	                  V 4      R,           V^,
          4      ,          ,
          4      ,          # )é   ç      ð?g:Œ0âŽyE>)ÚsignÚnnÚ
functionalÚreluÚabsÚpow)Úxr  Úpr‡   s   &&&€r"   Ú	shrink_opÚ:HQQWeightOnlyQuantizer.optimize_weights.<locals>.shrink_op8  s¨   ø€ Ø�AŒvØ—z‘z !“} u§x¡x×':Ñ':×'?Ñ'?ÀÇ	Á	È!ÃÈsÐUYÍzÕ@YÓ'ZÕZÐZà—z‘z !“} u§x¡x×':Ñ':×'?Ñ'?Ø—I‘I˜a“L C¨$¥J°%·)±)¸E¿I¹IÀa»LÈ4Õ<OÐQRÐUVÕQVÓ2WÕ#WÕWó(õ ð r%   g     ˆÃ@T©rb   Úkeepdim)r‡   Úis_cudar¢   r£   rÕ   r©   ÚroundÚclampÚmeanÚfloatr  rŠ   rô   )rœ   ÚscaleÚzeror  rb   r  Úverboser  r  r  r	  r•   Úw_fr  Ú
best_errorr¶   Úw_qÚw_rÚw_eÚcurrent_errorr‡   s   &&&&&&&             @r"   Úoptimize_weightsÚ'HQQWeightOnlyQuantizer.optimize_weights  s|  ø€ ó 	àR\ÒRd�i  f¨c°7¸DÀ'È2ÑNÐjtˆ
à�yÕ!Ø�vÕØ�wÕØ�wÕð	'
Ñ#ˆ�uð "(§§ �—’°U·]±]ˆØ�i‰i˜ÓˆØ—‘˜“ˆØ�w‰w�u‹~ˆà!(÷ 	ð ˆ
Ü�u–ˆAØ—+‘+˜c�k¨DÕ0Ó1×7Ñ7¸À½
ÀGÈAÅJÓOˆCØ˜•: Õ&ˆCÙ˜C #�I tÓ,ˆCØ—:‘:˜c S¨3¥Y°%Õ$7Õ7¸dÈD�:ÓQˆDØ�MˆDä! %§)¡)¨C°#­IÓ"6×";Ñ";Ó"=Ó>ˆMßÜ�aœŸš -°Ó3Ô4Ø˜zÔ)Ø*’
áñ ð ��c˜3àˆ{Ðr%   c                	ób  € V P                   ^ ,          VP                   ^ ,          8X  d   VP                  pV P                  p VR9   d^   V P                  4       ^,          V,          p\        V4       F.  pV R;;,          WRV1,          W$,          ,          ,          uu&   K0  	  R# \	        R4      h)r   :r   NNNzOnly 2,4,8 bits are supported.©r   é   é   )r¤   ÚTÚelement_sizer©   rp   )Úpack_tensorÚori_int_tensorra   Úcompress_ratioÚjs   &&&  r"   Úpack_on_row_fast_248bitÚ.HQQWeightOnlyQuantizer.pack_on_row_fast_248bitT  s‘   € à×Ñ˜QÕ >×#7Ñ#7¸Õ#:Ô:Ø+×-Ñ-ˆNØ%Ÿ-™-ˆKØ�9ÔØ(×5Ñ5Ó7¸!Õ;¸tÕCˆNÜ˜>Ö*�Ø˜B— >Ð2C°^Ð2CÕ#DÈÍÕ#TÕT•ó +ô &Ð&FÓGÐGr%   c                	ó   € ^ RI pVP                  4       p	V	P                  p
WJV,          V,          ,
          V,          pV^8X  d,   VP                  P                  P                  V	^ V3R^ 4      p	M,VP                  P                  P                  V	^ ^ ^ V3R^ 4      p	V	P                  pVe6   V'       d.   V^8X  d   V	P                  R
V.4      MV	P                  VR
.4      p	VRJ d#   V	P                  4       V	P                  4       ríRpM4V	P                  VRR7      ^ ,          pV	P                  VRR7      ^ ,          p^V,          ^,
          p^ pVV.pWþV,
          ,          P                  RR7      pWí,
          pV^ 8H  P                  4       P                  4       ^ 8”  d"   VVV^ 8H  &   VV,          P                  RR7      pV) V,          pV'       d   VP                  V4      pV'       d   V P                  V	VVVVR7      w  ppVP                  V	V,          V,           4      P                  V^ ,          V^,          4      pVP                  V4      P                  4       pR	V,          pV^8X  d4   VP                  V^ ,          R
4      pVP                  V^ ,          R
4      pM2VP                  R
VR
,          4      pVP                  R
VR
,          4      p?	??VVP                  VP                   4      VP                  VP                   4      3# )r   NÚconstantFTr  g     ˆÓ@)Úmax)rœ   r  r  r  rb   r  r™   )r‡   r  r¤   r  r  ÚpadÚreshapeÚminr7  r  ÚsumÚitemr  r'  rg   rÕ   r•   )r+   rœ   ra   Úchannel_wiseÚ
group_sizeÚoptimizeÚ
round_zerorb   r‡   ÚweightÚ	ori_shapeÚpad_lenr¤   Ú_minÚ_maxÚmax_vÚmin_vr  r  Úmin_max_axisr  r#  s   &&&&&&&&              r"   Úquantize_internalÚ(HQQWeightOnlyQuantizer.quantize_internala  s®  € ó 	à—‘“ˆØ—L‘Lˆ	à¨$¥°*Õ <Õ<À
ÕJˆØ�1Œ9Ø—X‘X×(Ñ(×,Ñ,¨V°a¸°\À:ÈqÓQ‰Fà—X‘X×(Ñ(×,Ñ,¨V°a¸¸A¸wÐ5GÈÐUVÓWˆFØ—‘ˆð Ò"¯Ø:>À!¼)�V—^‘^ R¨Ð$4Ô5È&Ï.É.ÐZdÐfhÐYiÓJjˆFð ˜5Ó ØŸ™› v§z¡z£|�$Ø‰Hà—:‘: 4°�:Ó6°qÕ9ˆDØ—:‘: 4°�:Ó6°qÕ9ˆDà�4•˜!•ˆØˆØ˜%�.ˆð  �+Õ&×-Ñ-°#Ð-Ó6ˆà•{ˆØ˜AÑ×"Ñ"Ó$×)Ñ)Ó+¨aÔ/Ø.3ˆL˜¨Ñ*Ñ+Ø˜\Õ)×0Ñ0°SÐ0Ó9ˆEØˆu�u�}ˆçØ—;‘;˜tÓ$ˆD÷ Ø×/Ñ/°vÀUÐQUÐ_fÐmqÐ/Ór‰KˆE�4ð �k‰k˜& 5�.¨4Õ/Ó0×6Ñ6°w¸qµzÀ7È1Å:ÓNˆØ�k‰k˜%Ó ×$Ñ$Ó&ˆà�e•ˆØ�1Œ9Ø—M‘M %¨¥(¨BÓ/ˆEØ—<‘<  a¥¨"Ó-‰Dà—M‘M " e¨B¥iÓ0ˆEØ—<‘<  E¨"¥IÓ.ˆDà�D˜$à�E—H‘H˜VŸ\™\Ó*¨D¯G©G°F·L±LÓ,AÐAÐAr%   c               ó$   € V ^8„  d   QhRRRRRR/# ©r   Únoder   Úgraph_stackúlist[GraphProto]Úreturnúlist[NodeProto]r!   )r   s   "r"   r#   rü   ¢  s'   € ÷ Eñ E˜Yð EÐ5Eð EÈ/ñ Er%   c                ój  € VP                   R8X  d   \        R4      h^ RIp\        P	                  RVP
                   R24       VP                  ^,          p\        WB4      w  rVVf   \        P	                  R4       V.# \        P                  P                  V4      pVP                  p\        V4      ^8w  d«   \        V4      ^8  d   \        P	                  R4       V.# VRR p	\        ;QJ d    R V	 4       F  '       g   K   R	M	  R
M! R V	 4       4      '       d#   \        P	                  R\        V	4      4       V.# VP                  VR,          VR,          4      pMRpVP!                  V4      p
VP"                  P%                  4       '       d   V
P#                  4       p
V P&                  P(                  pV P+                  V
P,                  W°P&                  P.                  R7      w  rÍpVP1                  4       pVP1                  4       pVP1                  4       p^V,          pVP3                  VP                  ^ ,          VP                  ^,          V,          3VP4                  VP6                  R7      pV P9                  VWË4       VP;                  4       P=                  4       pVP;                  4       P=                  4       pVP                  R4      pVP                  R4      pV
P                  w  ppV P&                  P.                  pVV,          pVV,           ^,
          V,          pVP                  VVV4      p\        P                  P?                  VP;                  4       P=                  4       4      pVP
                  R,           \A        V4      ,           Vn        VP                   F1  pVP
                  V8X  g   K  VP                  PC                  V4        M	  \        P                  P?                  V4      pVP
                  R,           Vn        VPD                  PG                  VV.4       VP                  ^ ,          VP
                  VP
                  .p\        P                  P?                  V4      pVP
                  R,           Vn        VPD                  PG                  V.4       VPI                  VP
                  4       / pVP                  w  ppVVR&   VVR&   VVR&   V P&                  P.                  VR&   \K        VP                  ^ ,          V4      pVe   \        V4      M^ pVRJ;'       d    VRJ ;'       g    VV8  p V '       g   VPL                  ^ ,          MVPL                  ^ ,          R,           p!\        PN                  PP                  ! R RTRV!.RVP
                  '       d$   VP
                  R,           \A        V4      ,           MRRR/VB p"V".p#V '       d™   T#PG                  \S        VP                  ^ ,          TTVP
                  '       d$   VP
                  R,           \A        V4      ,           M"VP
                  R,           \A        V4      ,           V!VPL                  ^ ,          R7      4       \        P	                  RVP
                   R24       V## )!ám  
Target node:        QOperator node:            QDQ nodes:
MatMul              MatMulNBits                DeQuantizeLinear -> MatMul
Gather              GatherBlockQuantized       Gather, Gather, Gather (optional) -> DequantizeLinear
If the node is target node with fp32 or fp16 const weight, quantize the weight to int4 and
return the new nodes.
If QOperator format, return the corresponding QOperator nodes.
If QDQ format, return the corresdponging QDQ nodes.
Gather (quantized data) + Gather (scales) + Gather (optional, zero points) -> DequantizeLinear is
not supported yet because Gather does not support int4 data.
r(   z/Gather quantization is not supported yet in HQQNústart to quantize ú ...ú2MatMul doesn't have const weight. Skip to quantizeú<MatMul weight has fewer than 2 dimensions. Skip to quantize.c              3  ó*   "  € T F	  q^8g  x € K  	  R# 5i©r  Nr!   ©Ú.0Úds   & r"   Ú	<genexpr>Ú2HQQWeightOnlyQuantizer.quantize.<locals>.<genexpr>Â  ó   é € Ð+¡7˜a˜–6£7ùó   ‚TFúcMatMul weight has non-unit batch dims %s; N-D batched quantization not supported. Skip to quantize.)ra   r>  )r•   r�   Ú_QÚ_scalesÚ_zero_pointsÚKÚNra   rU   Ú_pre_reshaper°   ÚoutputsrÂ   Ú Údomainúcom.microsoft©Úa_input_nameÚb_original_shapeÚtarget_graphÚname_prefixÚpre_reshape_outputÚfinal_outputúcomplete quantization of éþÿÿÿr™   ©ÚMatMulNBits)*Úop_typerp   r‡   ÚloggerÚinforÂ   ÚinputÚget_initializerÚonnxÚnumpy_helperÚto_arrayr¤   rÖ   Úanyr›   r9  Ú
from_numpyrz   rŽ   r­   ra   rI  r-  rU   rž   r«   Úuint8r�   r3  r{   rÛ   Ú
from_arrayr   ÚremoveÚinitializerÚextendrÙ   Ú_get_static_rankÚoutputÚhelperÚ	make_nodeÚ_build_nbits_output_reshape)$r+   rM  rN  r‡   Úinput_bÚb_pbÚbs_graphÚb_arrayrn  ÚleadingÚb_array_torchra   Úquant_weight_torchÚscales_torchÚzero_points_torchÚpacked_sizeÚpacked_torchÚscalesÚzero_pointsÚrowsÚcolsrU   Ú	blob_sizeÚk_blocksÚb_quantrz  Úscales_tensorÚinput_namesÚ	zp_tensorÚkwargsÚa_static_rankÚrank_b_origÚneeds_reshapeÚmatmul_q_outputÚmatmul_q_nodeÚoutput_nodess$   &&&                                 r"   ÚquantizeÚHQQWeightOnlyQuantizer.quantize¢  s•  € ð �<‰<˜8Ô#Ü%Ð&WÓXÐXãä�‰Ð(¨¯©¨°4Ð8Ô9Ø—*‘*˜Q•-ˆÜ(¨Ó>‰ˆØŠ<Ü�K‰KÐLÔMØ�6ˆMä×#Ñ#×,Ñ,¨TÓ2ˆØ"Ÿ=™=ÐÜÐÓ  AÔ%ÜÐ#Ó$ qÔ(Ü—‘ÐZÔ[Ø�v�Ø& s¨Ð+ˆGß‹sÑ+¡7Ó+�s�sŠsÑ+¡7Ó+×+Ò+Ü—‘ð(ä˜“Môð
 �v�à—o‘oÐ&6°rÕ&:Ð<LÈRÕ<PÓQ‰Gà#ÐØ×(Ñ(¨Ó1ˆØ�:‰:×"Ñ"×$Ò$Ø)×.Ñ.Ó0ˆMà�{‰{×ÑˆØ>B×>TÑ>TØ�O‰O $·;±;×3IÑ3Ið ?Uó ?
Ñ;ÐÐ*;ð 0×:Ñ:Ó<ÐØ#×.Ñ.Ó0ˆØ-×8Ñ8Ó:Ðà˜4•iˆà—{‘{Ø×%Ñ% aÕ(Ð*<×*BÑ*BÀ1Õ*EÈÕ*TÐUØ—+‘+Ø%×,Ñ,ð #ó 
ˆð
 	×$Ñ$ \Ð3EÔLØ×!Ñ!Ó#×)Ñ)Ó+ˆØ'×+Ñ+Ó-×3Ñ3Ó5ˆà—‘ Ó#ˆØ!×)Ñ)¨"Ó-ˆØ"×(Ñ(‰
ˆˆdØ—[‘[×+Ñ+ˆ
Ø +Õ-ˆ	Ø˜:Õ%¨Õ)¨jÕ8ˆØ#×+Ñ+¨D°(¸IÓFˆä×#Ñ#×.Ñ.¨|×/?Ñ/?Ó/A×/GÑ/GÓ/IÓJˆØ—y‘y 4Õ'¬#¨d«)Õ3ˆŒØ—^”^ˆEØ�z‰z˜WÖ$Ø—‘×%Ñ% eÔ,Ùñ $ô
 ×)Ñ)×4Ñ4°VÓ<ˆØ!ŸY™Y¨Õ2ˆÔØ×Ñ×#Ñ# W¨mÐ$<Ô=à—z‘z !•} g§l¡l°M×4FÑ4FÐGˆÜ×%Ñ%×0Ñ0°Ó=ˆ	ØŸ™ ^Õ3ˆ	ŒØ×Ñ×#Ñ# Y KÔ0Ø×Ñ˜9Ÿ>™>Ô*àˆØ—]‘]‰
ˆˆdØˆˆs‰Øˆˆs‰Øˆˆv‰Ø#Ÿ{™{×5Ñ5ˆˆ|Ñô )¨¯©°A­¸ÓDˆØ/?Ò/K”cÐ*Ô+ÐQRˆØ(°Ð4×oÐo¸-È4Ð:O×:nÐ:nÐS`ÐcnÑSnˆß0=˜$Ÿ+™+ až.À4Ç;Á;ÈqÅ>ÐTbÕCbˆÜŸ™×-Ò-ñ 
àð
ð %Ð%ð
ð 26··°�—‘˜TÕ!¤C¨£IÖ-Àð	
ð
 #ð
ð ñ
ˆð &�ˆßð ×ÑÜ+Ø!%§¡¨A¥Ø%5Ø!)ØBFÇ)Ç)À) §¡¨TÕ!1´C¸³IÖ!=ÐRV×R[ÑR[Ð^bÕRbÔehÐimÓenÕRnØ'6Ø!%§¡¨Q¥ôô	ô 	�‰Ð/°·	±	¨{¸$Ð?Ô@àÐr%   rþ   )r   NF)r+  Té@   TTr  )r.   r/   r0   r1   r,   Ústaticmethodr'  r3  rI  r§  r2   r!   r%   r"   rú   rú     sE   † õð ö2ó ð2ðh ñ	Hó ð	Hô?B÷BEñ Er%   rú   c               ó    € V ^8„  d   QhRRRR/# )r   Ú
graph_pathrO  rP  ztuple[TensorProto, GraphProto]r!   )r   s   "r"   r#   r#   *  s   € ÷ ñ Ð&6ð Ð;Yñ r%   c                ó´   € \        \        V4      ^,
          RR4       F6  pW,          pVP                   F  pVP                  V 8X  g   K  WC3u u # 	  K8  	  R# )r  r™   )NN)r©   rÖ   r„  rÂ   )rÂ   r¬  ÚgidÚgraphrœ   s   &&   r"   r{  r{  *  sQ   € Ü”S˜“_ qÕ(¨"¨bÖ1ˆØ•ˆØ×'Ô'ˆFØ�{‰{˜dÖ"Ø�}Ô$ó (ñ 2ð
 Ðr%   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Útensor_namer   r¬  rO  rP  rk   r!   )r   s   "r"   r#   r#   3  s"   € ÷ ñ  #ð Ð3Cð È
ñ r%   c                óÈ  € \        \        V4      ^,
          RR4       FÀ  pW,          p\        VP                  4      \        VP                  4      ,           \        VP
                  4      ,            Fh  pVP                  V 8X  g   K  VP                  P                  pVP                  R4      '       d$   \        VP                  P                  4      u u #   R# 	  KÂ  	  R# )ur  Return the static rank of a tensor if its shape is known, else None.

Searches graph inputs, value_info, and outputs in the graph stack (inner-most
graph first).  A known shape requires ``HasField('shape')`` to be true on the
tensor_type; the rank is then ``len(shape.dim)``.  Individual dim sizes may
still be symbolic â€” only the rank (dimension count) matters here.
r¤   Nr™   )r©   rÖ   r›   rz  Ú
value_infor‡  rÂ   ÚtypeÚtensor_typeÚHasFieldr¤   Údim)r±  r¬  r®  r¯  ÚviÚtts   &&    r"   r†  r†  3  s¡   € ô ”S˜“_ qÕ(¨"¨bÖ1ˆØ•ˆÜ�u—{‘{Ó#¤d¨5×+;Ñ+;Ó&<Õ<¼tÀEÇLÁLÓ?Q×QÐQˆBØ�w‰w˜+Ö%Ø—W‘W×(Ñ(�Ø—;‘;˜w×'Ò'Ü˜rŸx™xŸ|™|Ó,Ô,Ûó Rñ 2ñ r%   c               ó4   € V ^8„  d   QhRRRRRRRRRRR	RR
R/# )r   rm  r   rn  Útuplero  r   rp  rq  rr  rP  rQ  r!   )r   s   "r"   r#   r#   F  sW   € ÷ ñ Øðàðð ðð ð	ð
 ðð ðð ñr%   c                óŒ
  € \        V4      pVR0,          pVR,           V,           pVR,           p	VR,           p
VR,           pVR,           pVR,           pVR,           pVR,           pVR	,           pVP                  P                  \        P                  P                  \        P                  ! ^ \        P                  R
7      V	R7      \        P                  P                  \        P                  ! ^\        P                  R
7      V
R7      \        P                  P                  \        P                  ! ^\        P                  R
7      VR7      \        P                  P                  \        P                  ! ^.\        P                  R
7      VR7      \        P                  P                  \        P                  ! V\        P                  R
7      VR7      \        P                  P                  \        P                  ! V.\        P                  R
7      VR7      \        P                  P                  \        P                  ! ^ .\        P                  R
7      VR7      .4       VR,           pVR,           pVR,           pVR,           pVR,           pVR,           pVR,           pVR,           pVR,           pVR,           pVR,           pVR,           p\        P                  P                  RV .V.VR,           R7      \        P                  P                  RV.V.VR,           R7      \        P                  P                  RVV	.V.VR,           R7      \        P                  P                  RVV.V.VR,           R7      \        P                  P                  RVV.V.VR ,           R7      \        P                  P                  RVV	.V.VR!,           R7      \        P                  P                  R"VV.V.VR#,           R7      \        P                  P                  R$V.V.VR%,           \        P                  P                  V\        P                  ^.^.R&7      R'7      \        P                  P                  RVV
.V.VR(,           R7      \        P                  P                  R"VV.V.VR),           R7      \        P                  P                  R*VVVV.V.VR+,           R7      \        P                  P                  R,VVV.V.VR-,           ^ R.7      \        P                  P                  R"VV.V.VR/,           R7      .pV# )1u‡  Build the reshape chain that restores the ONNX MatMul-broadcast output shape.

MatMulNBits produces shape ``[...A_batch_dims, M, N]`` (rank = rank(A)). To match
the original ``MatMul(A, B_orig)`` output, where B_orig has all-unit leading
dims, we need:

    a_rank_eff = max(rank(A), 2)   # ONNX promotes 1-D A to rank-2
    out_shape = [1] * max(rank(B_orig) - a_rank_eff, 0) + A.shape[:-1] + [N]

This is built dynamically via Shape/Gather/Max/Sub/Max/ConstantOfShape/Slice/Concat
so it works regardless of A's static rank (handles rank(A) == 1, rank(A) == 2
â€” the common transformer case â€” as well as rank(A) >= rank(B_orig) where no
leading-1 prepending is needed). All ops used are valid from opset 11 onward.

Args:
    a_input_name: name of the activation input edge (A) feeding MatMulNBits.
    b_original_shape: the original (pre-squeeze) shape of B, e.g. ``(1, K, N)``.
    target_graph: graph proto to append helper initializers into.
    name_prefix: unique prefix for generated node/initializer names.
    pre_reshape_output: name of the MatMulNBits output edge (the input of the
        generated Reshape).
    final_output: name of the final edge produced by the generated Reshape
        (must match the original MatMul output edge).

Returns:
    List of nodes to append to the consumer's ``output_nodes`` after the
    MatMulNBits node. Initializers are appended to ``target_graph`` in place.
Ú_Ú_zeroÚ_oneÚ_twoÚ_one_vecÚ_rank_bÚ_n_vecÚ	_zero_vecÚ
_one_value©r•   ©rÂ   Ú_a_shapeÚ_a_shape_of_shapeÚ_a_rankÚ_a_rank_effÚ
_extra_rawÚ_extra_countÚ_extra_count_vecÚ_extra_onesÚ
_a_rank_m1Ú_a_rank_m1_vecÚ_a_prefix_shapeÚ_target_shapeÚShapeÚ_shape_aÚ_shape_of_a_shaper(   Ú_gather_rankÚMaxÚ_max_rank_effÚSubÚ_subrE  ÚReshapeÚ_reshape_extraÚConstantOfShapeÚ_const_ones)rÂ   Ú	data_typeÚdimsÚvals)rÂ   ÚvalueÚ_sub_oneÚ_reshape_rank_m1ÚSliceÚ_slice_a_prefixÚConcatÚ_concat_target)rÂ   rb   Ú_reshape_outr™   )rÖ   r„  r…  r|  r}  r‚  rô   Úarrayr�   rˆ  r‰  Úmake_tensorr   ÚINT64)rm  rn  ro  rp  rq  rr  r¢  Ún_dimr  Ú	init_zeroÚinit_oneÚinit_twoÚinit_one_vecÚinit_rank_bÚ
init_n_vecÚinit_zero_vecÚinit_one_value_templateÚa_shapeÚa_shape_of_shapeÚa_rankÚ
a_rank_effÚ	extra_rawÚextra_countÚextra_count_vecÚ
extra_onesÚa_rank_minus_oneÚa_rank_minus_one_vecÚa_prefix_shapeÚtarget_shapeÚnodess   &&&&&&                        r"   rŠ  rŠ  F  s³  € ôH Ð&Ó'€KØ˜RÕ €Eð 	�cÕ˜LÕ(€AØ�G•€IØ�6�z€HØ�6�z€HØ�z•>€LØ�i•-€KØ�X•€JØ˜•O€MØ ,Õ.Ðà×Ñ×#Ñ#ä×Ñ×(Ñ(¬¯ª°!¼2¿8¹8Ô)DÈ9Ð(ÓUÜ×Ñ×(Ñ(¬¯ª°!¼2¿8¹8Ô)DÈ8Ð(ÓTÜ×Ñ×(Ñ(¬¯ª°!¼2¿8¹8Ô)DÈ8Ð(ÓTÜ×Ñ×(Ñ(¬¯ª°1°#¼R¿X¹XÔ)FÈ\Ð(ÓZÜ×Ñ×(Ñ(¬¯ª°+ÄRÇXÁXÔ)NÐU`Ð(ÓaÜ×Ñ×(Ñ(¬¯ª°5°'ÄÇÁÔ)JÐQ[Ð(Ó\Ü×Ñ×(Ñ(¬¯ª°1°#¼R¿X¹XÔ)FÈ]Ð(Ó[ð	
ô
ð �*�n€GØÐ.Õ.ÐØ��]€FØ�]Õ"€JØ�LÕ €IØ�nÕ$€KØÐ,Õ,€OØ�]Õ"€JØ˜<Õ'ÐØÐ/Õ/ÐØÐ*Õ*€NØ�Õ&€Lô 	�‰×Ñ˜g¨ ~¸°yÀqÈ:Å~ÐÓVô
 	�‰×Ñ˜g¨ yÐ3CÐ2DÈ1ÐObÕKbÐÓcÜ�‰×Ñ˜hÐ)9¸9Ð(EÈÀxÐVWÐZhÕVhÐÓiô 	�‰×Ñ˜e f¨hÐ%7¸*¸ÈAÐP_ÕL_ÐÓ`Ü�‰×Ñ˜e k°:Ð%>ÀÀÐRSÐV\ÕR\ÐÓ]Ü�‰×Ñ˜e i°Ð%;¸k¸]ÐQRÐU[ÕQ[ÐÓ\Ü�‰×Ñ˜i¨+°|Ð)DÀÐFWÐ^_ÐbrÕ^rÐÓsÜ�‰×ÑØØÐØˆLØ�]Õ"Ü—+‘+×)Ñ)Ø,Ü%×+Ñ+Ø�SØ�Sð	 *ó ð 	ó 	
ô 	�‰×Ñ˜e f¨hÐ%7Ð:JÐ9KÐRSÐV`ÕR`ÐÓaÜ�‰×ÑØÐ(¨,Ð7Ð:NÐ9OÐVWÐZlÕVlð 	ó 	
ô 	�‰×ÑØØ�mÐ%9¸=ÐIØÐØÐ&Õ&ð	 	ó 	
ô 	�‰×ÑØØ˜¨Ð4ØˆNØÐ%Õ%Øð 	ó 	
ô 	�‰×ÑØØ Ð.ØˆNØ�^Õ#ð	 	ó 	
ðY2€Eðf €Lr%   c                ó”  € W,          pV ^,	          ^,          pV ^,          p\         P                  ! V\         P                  R7      pWVR&   WFR&   VP                  W34      pVP                  pVP                  R4      p	V	R,          ^,          ^ð,          V	R,          ^,          ,          p V P                  \         P                  4      # )r+  rÆ  :r   Nr   ºr  Nr   r™   )rô   Úemptyr�  r9  r-  Úastype)
Úpackedr˜  r™  ÚtotalÚhighÚlowÚ	int4_valsÚint4_matrixÚint4_matrix_transposedÚflats
   &&&       r"   Útranspose_packed_int4_matrixr  É  s¥   € à�K€EØ�a�K˜4Õ€DØ
�4�-€CÜ—’˜¤b§h¡hÔ/€IØˆd�OØˆd�OØ×#Ñ# T LÓ1€Kð )Ÿ]™]Ðð "×)Ñ)¨"Ó-€DØ�D�z˜Q� $Õ&¨4°­:¸Õ+<Õ=€FØ�=‰=œŸ™Ó"Ð"r%   c                  óª   € ] tR tRtR R ltR R ltR R lt]R R	 l4       t]R
 R l4       t	]R R l4       t
]R R l4       tR R ltR R ltRtR# )ÚDefaultWeightOnlyQuantizeriÜ  c               ó   € V ^8„  d   QhRR/# )r   r­   re   r!   )r   s   "r"   r#   Ú'DefaultWeightOnlyQuantizer.__annotate__Ý  s   € ÷ ñ Ð;ñ r%   c                	ó   € Wn         R # ró   rþ   rÿ   s   &&r"   r,   Ú#DefaultWeightOnlyQuantizer.__init__Ý  s   € ØŽr%   c               ó    € V ^8„  d   QhRRRR/# )r   Ú
fp32weightznpt.ArrayLikerP  ú)tuple[np.ndarray, np.ndarray, np.ndarray]r!   )r   s   "r"   r#   r  à  s   € ÷ 0,ñ 0,¨Mð 0,Ð>gñ 0,r%   c           
     óž  € V P                   P                  p^V,          p\        VP                  4      ^8w  d   \	        R4      hVP                  w  rEV P                   P
                  pWF,           ^,
          V,          pV P                   P                  \        P                  8X  Ed.   Wc,           ^,
          V,          pWv,          p	W”,
          p
V
^ 8”  d   \        P                  ! V^ V
3R3R4      p\        P                  ! WWV3RR7      p\        P                  ! WWV,           ^,
          V,          3RR7      p\        P                  ! WW3VP                  R7      pV^8X  d%   \        W±WÜWeW@P                   P                  4       EMCV^8X  d%   \        W±WÜWeW@P                   P                  4       EM\!        W±WÜWeW@P                   P                  4       MõV P                   P"                  '       d   TMV P                   P
                  pWF,           ^,
          V,          pV^8X  g   Q R4       h\        P                  ! WE,          ^,           ^,          RR7      p\        P                  ! WW,          ^,           ^,          RR7      p\        P                  ! Wu3VP                  R7      p\%        W±WÜWeW@P                   P                  4       W½V3# )z54b/8b quantize fp32 weight to int4 using C++ kernels.z9Current int4 block quantization only supports 2D tensors!r6  r�  rÆ  z+QDQ format only support 4 bits quantization)r   r   )r­   ra   rÖ   r¤   rÓ   rU   r   r   r<   rô   r8  r«   r•   r   rh   r
   r	   rl   r   )r+   r  ÚqbitsÚkpackr˜  r™  rU   r›  rš  Úpadded_rowsrC  r  Ú
zero_pointr–  s   &&            r"   Úqbits_block_quantÚ,DefaultWeightOnlyQuantizer.qbits_block_quantà  sA  € ð —‘× Ñ ˆØ�U•
ˆÜˆz×ÑÓ  AÔ%ÜÐXÓYÐYØ×%Ñ%‰
ˆà—[‘[×+Ñ+ˆ
ØÕ%¨Õ)¨jÕ8ˆà�;‰;×#Ñ#¤{×'<Ñ'<Õ<Ø#Õ+¨aÕ/°EÕ9ˆIØ"Õ/ˆKØ!Õ(ˆGØ˜Œ{ÜŸVšV J°!°W°¸vÐ0FÈ
ÓS�
ô —X’X˜t¨yÐ9ÀÔIˆFÜŸš 4°eÕ+;¸aÕ+?ÀEÕ*IÐ"KÐSZÔ[ˆJÜ—X’X˜tÐ.°j×6FÑ6FÔGˆFØ˜ŒzÜ%Ø¨¸JÈd×T_ÑT_×TlÑTlöð ˜!”Ü%Ø¨¸JÈd×T_ÑT_×TlÑTlöô &Ø¨¸JÈd×T_ÑT_×TlÑTlõð
 "&§¡×!C×!CÐ!C™ÈÏÉ×I_ÑI_ˆJØÕ)¨AÕ-°*Õ<ˆHà˜A”:ÐLÐLÓL�:Ü—X’X˜t�{¨Q�°1Õ4¸GÔDˆFÜŸš 4¥?°QÕ#6¸1Õ"<ÀGÔLˆJÜ—X’X˜xÐ.°j×6FÑ6FÔGˆFÜ%Ø F¸
È$×P[ÑP[×PhÑPhôð  
Ð+Ð+r%   c               ó$   € V ^8„  d   QhRRRRRR/# rL  r!   )r   s   "r"   r#   r    s(   € ÷ wñ w Ið wÐ<Lð wÐQ`ñ wr%   c                ó  € V P                   P                  pV^8X  d>   V P                   P                  '       d   \        P                  M\        P
                  pM<V P                   P                  '       d   \        P                  M\        P                  pVP                  ^,          p\        WR4      w  rgVf   \        P                  R4       V.# \        P                  ! V4      P                  4       pVP                  p	\!        V	4      ^8w  d«   \!        V	4      ^8  d   \        P                  R4       V.# V	RR% p
\"        ;QJ d    R V
 4       F  '       g   K   RM	  RM! R V
 4       4      '       d#   \        P                  R\%        V
4      4       V.# VP'                  V	R%,          V	R&,          4      pMRp	VP(                  R8H  pV'       d    VP+                  \,        P.                  4      pV P1                  V4      w  rÍpV'       d    VP+                  \2        P4                  4      pV P                   P6                  \8        P:                  8X  d�   \        P<                  P?                  \        P@                  ! WÆPB                  R	V 2,           R
7      4      p\        P<                  P?                  \        P@                  ! WÖPB                  R,           R
7      4      pM“\D        PF                  PI                  VPB                  RV 2,           WHP                  VPK                  4       R4      p\        P<                  P?                  \        P@                  ! WÖPB                  R,           R
7      4      pV P                   P6                  \8        PL                  8H  ;'       d5    V P                   PN                  ;'       d    V P                   P                  pV'       d¼   VP                  w  pp\Q        VVV4      pVP'                  V^34      p\D        PF                  PI                  VPB                  RV 2,           VVV.VPK                  4       R4      p\        P<                  P?                  \        P@                  ! WÖPB                  R,           R
7      4      pVP                   F1  pVPB                  V8X  g   K  VP                  PS                  V4        M	  VPT                  PW                  VV.4       . pV P                   P6                  \8        P:                  8X  Ed†   VP                  ^ ,          VPB                  VPB                  .pV P                   P                  '       gh   \D        PX                  P[                  WæPB                  R,           4      pVP]                  VPB                  4       VPT                  PW                  V.4       / pVP                  w  ppVVR&   VVR&   VVR&   V P                   P^                  VR&   V P                   P`                  '       d   V P                   P`                  VR&   \c        VP                  ^ ,          V4      pV	e   \!        V	4      M^ pV	RJ;'       d    VRJ ;'       g    VV8  pV'       g   VPd                  ^ ,          MVPd                  ^ ,          R,           p\D        PF                  Pf                  ! R'RTRV.RVPB                  '       d   VPB                  R	V 2,           MRRR/VB pVP]                  V4       V'       d   TPW                  \i        VP                  ^ ,          T	TVPB                  '       d   VPB                  R	V 2,           MVPB                  R	V 2,           VVPd                  ^ ,          R7      4       V# VPB                  VPB                  .pVPB                  R,           .pV^ ,          .p V^ ,          R,           .p!VP                  ^ ,          V'       d
   V!^ ,          MV^ ,          .p"\c        VP                  ^ ,          V4      pV	e   \!        V	4      M^ pV	RJ;'       d    VRJ ;'       g    VV8  pV'       g   VPd                  ^ ,          MVPd                  ^ ,          R,           p#V#.p$V P                   P                  '       gƒ   \D        PF                  PI                  VPB                  R,           WMP                  VPK                  4       R4      pVP]                  VPB                  4       VPT                  PW                  V.4       VP                  w  ppRV'       d   ^M^ RV P                   PN                  '       d   TMV P                   P^                  /p%\D        PF                  Pf                  ! R(RTRTRVPB                  '       d   VPB                  RV 2,           MR/V%B p&\D        PF                  Pg                  R T"T$VPB                  '       d   VPB                  R!V 2,           MRR"7      p'V'       d;   \D        PF                  Pg                  R#V V!^^ .R$7      p(VPW                  V&V(V'.4       MVPW                  V&V'.4       V'       d   TPW                  \i        VP                  ^ ,          T	TVPB                  '       d   VPB                  RV 2,           MVPB                  RV 2,           V#VPd                  ^ ,          R7      4       V# ))zá
Quantize weight B of MatMul node to int4 or int8.
Supports 2D constant matrix, and N-D constant matrices whose leading dimensions are all 1
(which are squeezed to 2D before quantization). Axis 0 blockwise quantization only.
NrV  rW  c              3  ó*   "  € T F	  q^8g  x € K  	  R# 5irY  r!   rZ  s   & r"   r]  Ú=DefaultWeightOnlyQuantizer.quantize_matmul.<locals>.<genexpr>*  r_  r`  TFra  Úbfloat16rb  rÇ  rc  Ú_DQ_QÚ
_DQ_scalesrd  re  rf  ra   rU   rj   rg  r°   rh  rÂ   ri  rj  rk  rl  Ú_outputÚ_transposedÚ_DQ_zero_pointsrb   r'   Ú	_matmul_Q)r°   rh  rÂ   Ú	Transpose)r°   rh  Úpermrt  r™   ru  )ÚDequantizeLinear)5r­   ra   rh   r   ÚINT8ÚUINT8ÚINT4ÚUINT4rz  r{  rx  ry  ÚirÚ
from_protorÛ   r¤   rÖ   r  r›   r9  r•   r  rô   r£   r  Ú	ml_dtypesr%  r   r   r<   ÚserdeÚserialize_tensorÚTensorrÂ   r|  rˆ  rì  Útobytesr�   rl   r  rƒ  r„  r…  r}  r‚  rÙ   rU   rj   r†  r‡  r‰  rŠ  ))r+   rM  rN  ra   Úqtyper‹  Úb_tensorÚb_graphÚ	b_ndarrayrn  r�  r%  r  r–  r—  rœ  r�  Úqdq_opt_for_intel_npu_enabledr˜  r™  rz  r¦  rž  rŸ  r   r¡  r¢  r£  Ú
qop_outputÚmatmul_qbit_nodeÚdq_input_namesÚdq_output_namesÚtp_input_namesÚtp_output_namesÚmatmul_input_namesÚqdq_matmul_outÚmatmul_output_namesÚ	dq_kwargsÚdq_nodeÚmatmul_nodeÚtp_nodes)   &&&                                      r"   Úquantize_matmulÚ*DefaultWeightOnlyQuantizer.quantize_matmul  sß  € ð �{‰{×ÑˆØ�1Œ9Ø(,¯©×(@×(@Ð(@”K×$Ò$Äk×FWÑFW‰Eà(,¯©×(@×(@Ð(@”K×$Ò$Äk×FWÑFWˆEØ—*‘*˜Q•-ˆÜ+¨GÓAÑˆØÒÜ�K‰KÐLÔMØ�6ˆMä—M’M (Ó+×1Ñ1Ó3ˆ	Ø$Ÿ?™?ÐÜÐÓ  AÔ%ÜÐ#Ó$ qÔ(Ü—‘ÐZÔ[Ø�v�Ø& s¨Ð+ˆGß‹sÑ+¡7Ó+�s�sŠsÑ+¡7Ó+×+Ò+Ü—‘ð(ä˜“Môð
 �v�à!×)Ñ)Ð*:¸2Õ*>Ð@PÐQSÕ@TÓU‰Ià#Ðà—?‘? jÑ0ˆßØ!×(Ñ(¬¯©Ó4ˆIà&*×&<Ñ&<¸YÓ&GÑ#ˆ˜ßØ—]‘]¤9×#5Ñ#5Ó6ˆFà�;‰;×#Ñ#¤{×'<Ñ'<Ô<Ü—h‘h×/Ñ/´·	²	¸&Ç}Á}ÐY[Ð\`Ð[aÐWbÕGbÔ0cÓdˆGÜŸH™H×5Ñ5´b·i²iÀÏ]É]Ð]fÕMfÔ6gÓh‰Mä—k‘k×-Ñ-Ø—‘ %¨ v Õ.°·±ÈÏÉÓHXÐZ^óˆGô ŸH™H×5Ñ5´b·i²iÀÏ]É]Ð]iÕMiÔ6jÓkˆMð �K‰K×$Ñ$¬¯©Ñ7÷ )ð )Ø—‘×2Ñ2÷)ð )à—‘×(Ñ(ð 	&÷
 )Ø"Ÿ™‰JˆD�$Ü1°&¸$ÀÓEˆFØ—^‘^ T¨1 IÓ.ˆFÜ—k‘k×-Ñ-Ø—‘ %¨ v Õ.°¸¸d°|ÀVÇ^Á^ÓEUÐW[óˆGô ŸH™H×5Ñ5´b·i²iÀÏ]É]Ð]iÕMiÔ6jÓkˆMà—]”]ˆEØ�z‰z˜WÖ$Ø—‘×$Ñ$ UÔ+Ùñ #ð
 	×Ñ×"Ñ" G¨]Ð#;Ô<àˆà�;‰;×#Ñ#¤{×'<Ñ'<Õ<ØŸ:™: a�=¨'¯,©,¸×8JÑ8JÐKˆKØ—;‘;×+×+Ð+Ü ×-Ñ-×8Ñ8¸ÇmÁmÐVdÕFdÓe�	Ø×"Ñ" 9§>¡>Ô2Ø×#Ñ#×*Ñ*¨I¨;Ô7ØˆFØ"Ÿ™‰JˆD�$ØˆF�3‰KØˆF�3‰KØ!ˆF�6‰NØ#'§;¡;×#9Ñ#9ˆF�<Ñ ð �{‰{×)×)Ð)Ø+/¯;©;×+EÑ+E�Ð'Ñ(ô -¨T¯Z©Z¸­]¸KÓHˆMØ3CÒ3Oœ#Ð.Ô/ÐUVˆKØ,°DÐ8×sÐs¸mÈtÐ>S×>rÐ>rÐWdÐgrÑWrˆMß/<˜Ÿ™ QžÀ$Ç+Á+ÈaÅ.ÐSaÕBaˆJÜ#Ÿ{™{×4Ò4ñ  à"ð ð $˜ð ð 15·	·	°	�T—Y‘Y 2 d V Ö,¸rð	 ð
 'ð ð ñ Ðð ×ÑÐ 0Ô1ßØ×#Ñ#Ü/Ø%)§Z¡Z°¥]Ø)9Ø%,ØAEÇÇÀ T§Y¡Y°2°d°V°Ö%<ÐQY×Q^ÑQ^ÐceÐfjÐekÐalÕQlØ+5Ø%)§[¡[°¥^ôô	ðP Ðð{ &Ÿl™l¨M×,>Ñ,>Ð?ˆNØ&Ÿ|™|¨iÕ7Ð8ˆOØ-¨aÕ0Ð1ˆNØ.¨qÕ1°MÕAÐBˆOà—
‘
˜1•ß&C� Ö"ÈÐYZÕI[ð"Ðô -¨T¯Z©Z¸­]¸KÓHˆMØ3CÒ3Oœ#Ð.Ô/ÐUVˆKØ,°DÐ8×sÐs¸mÈtÐ>S×>rÐ>rÐWdÐgrÑWrˆMß3@˜TŸ[™[¨ž^ÀdÇkÁkÐRSÅnÐWeÕFeˆNØ#1Ð"2ÐØ—;‘;×+×+Ð+Ü ŸK™K×3Ñ3Ø—M‘MÐ$5Õ5°u¿l¹lÈK×L_ÑL_ÓLaÐcgó�	ð ×%Ñ% i§n¡nÔ5Ø×#Ñ#×*Ñ*¨I¨;Ô7Ø"Ÿ™‰JˆD�$à×:™ÀØ d§k¡k×&H×&HÐ&H™dÈdÏkÉk×NdÑNdðˆIô —k‘k×+Ò+ñ à%ðð (ðð 48·9·9°9�T—Y‘Y 5¨¨ Ö/À"ð	ð
 ñˆGô Ÿ+™+×/Ñ/ØØ)Ø+Ø7;·y·y°y�T—Y‘Y 9¨T¨FÐ!3Ö3Àbð	 0ó ˆK÷ -ÜŸ+™+×/Ñ/ØØ)Ø+Ø˜Q˜ð	 0ó �ð ×#Ñ# W¨g°{Ð$CÕDà×#Ñ# W¨kÐ$:Ô;ßØ×#Ñ#Ü/Ø%)§Z¡Z°¥]Ø)9Ø%,ØDHÇIÇIÀI T§Y¡Y°5¸¸°Ö%?ÐT\×TaÑTaÐfkÐlpÐkqÐdrÕTrØ+9Ø%)§[¡[°¥^ôô	ð Ðr%   c               ó    € V ^8„  d   QhRRRR/# )r   rê   ú
np.ndarrayrP  ztuple[np.ndarray, np.ndarray]r!   )r   s   "r"   r#   r  Ì  s   € ÷ &ñ & Jð &Ð3Pñ &r%   c                	óº  € \         P                  ! V ^RR7      p\         P                  ! V ^RR7      p\         P                  ! \         P                  ! V4      \         P                  ! V4      8„  W4      pVR,          p\         P                  ! V^ 8H  ^ W,          4      P                  4       P                  R^4      P                  \         P                  4      pWT3# )r  T©rb   Úkeepdimsg       Àiøÿÿÿ)	rô   r7  r:  Úwherer  r  Úclipr  Úint8)rê   Úmax_valÚmin_valÚabs_maxr  Úquantized_slices   &     r"   Úquant_slice_symmetricÚ0DefaultWeightOnlyQuantizer.quant_slice_symmetricË  sž   € ä—&’&˜ A°Ô5ˆÜ—&’&˜ A°Ô5ˆÜ—(’(œ2Ÿ6š6 '›?¬R¯VªV°G«_Ñ<¸gÓOˆà˜$•ˆÜŸ(š( 5¨A¡:¨q°$µ,Ó?×EÑEÓG×LÑLÈRÐQRÓS×ZÑZÔ[]×[bÑ[bÓcˆàÐ%Ð%r%   c               ó    € V ^8„  d   QhRRRR/# )r   rê   rO  rP  r  r!   )r   s   "r"   r#   r  ×  s   € ÷ 2ñ 2 Zð 2Ð4]ñ 2r%   c                	óJ  € \         P                  ! V P                  ^RR7      ^ 4      p\         P                  ! V P	                  ^RR7      ^ 4      pW!,
          R,          p\         P
                  ! V^ 8H  ^V) V,          4      P                  4       P                  ^ ^4      P                  \         P                  4      p\         P
                  ! V^ 8H  ^W,          V,           4      P                  4       P                  ^ ^4      P                  \         P                  4      pWSV3# )r  TrQ  g      .@)
rô   Úminimumr:  Úmaximumr7  rS  r  rT  r  r�  )rê   rW  rV  r  r  rY  s   &     r"   Úquant_slice_asymmetricÚ1DefaultWeightOnlyQuantizer.quant_slice_asymmetricÖ  sà   € ä—*’*˜TŸX™X¨1°t˜XÓ<¸aÓ@ˆÜ—*’*˜TŸX™X¨1°t˜XÓ<¸aÓ@ˆàÕ" dÕ*ˆÜ—X’X˜e q™j¨!¨g¨X¸Õ-=Ó>×DÑDÓF×KÑKÈAÈrÓR×YÑYÔZ\×ZbÑZbÓcˆ
ÜŸ(š( 5¨A¡:¨q°$µ,ÀÕ2KÓL×RÑRÓT×YÑYÐZ[Ð]_Ó`×gÑgÔhj×hpÑhpÓqˆà zÐ1Ð1r%   c               ó    € V ^8„  d   QhRRRR/# )r   rê   rO  rP  r!   )r   s   "r"   r#   r  â  s   € ÷ /ñ / 
ð /¨zñ /r%   c                óú   € V P                  R4      p\        V4      ^,          ^ 8w  d   \        P                  ! V^ 4      pVR,          ^,          VR,          ^,          ^,          ,          pVP	                  R4      # )z2Pack int8 data to int4 and store in uint8 ndarray.:NNr   r  r�  r™   )r9  rÖ   rô   rÙ   r  )rê   Ú	data_flatÚquant_data_int4s   &  r"   Úpack_int8_to_int4Ú,DefaultWeightOnlyQuantizer.pack_int8_to_int4á  se   € ð —L‘L Ó$ˆ	Üˆy‹>˜AÕ Ô"ÜŸ	š	 )¨QÓ/ˆIØ$ S�>¨CÕ/°Y¸tµ_ÀsÕ5JÈqÕ4PÕQˆà×%Ñ% gÓ.Ð.r%   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR	/# )
r   rê   rO  Úquantize_axisrg   rU   rh   ri   rP  z0tuple[np.ndarray, np.ndarray, np.ndarray | None]r!   )r   s   "r"   r#   r  ì  s:   € ÷ 58ñ 58Øð58àð58ð ð58ð ð	58ð
 
:ñ58r%   c                ó  € ^pV P                   V,          p^p\        V P                   4       F%  w  rxWq8  d   WH,          pK  Wq8”  g   K  Wh,          pK'  	  WR,           ^,
          V,          p	\        V P                   4      p
WšV&   V P                  WEV34      p\        P
                  ! WIV3V P                  R7      pV'       d   \        P
                  ! WEV3RR7      pM4\        P
                  ! WEV3RR7      p\        P
                  ! WIV3RR7      p\        ^ WR4       F™  p\        Wr,           V4      pVRW1R3,          pV'       d   \        P                  V4      w  ppM\        P                  V4      w  pppVVRW1R3&   Wr,          pVVRVV^,           1R3&   V'       d   Kˆ  XXRVV^,           1R3&   K›  	  \        P                  V4      pRpV'       g   \        P                  X4      pVP                  V
4      pVVV3# )zXQuantize ndarray data to int4 using numpy, return (quantized data, scales, zero points).rÆ  rU  r�  :NNNN)r¤   rØ   r›   r9  rô   r«   r•   r©   r:  r  rZ  r`  rf  )rê   ri  rU   rh   ÚmÚkÚnr¶   r·  r›  Úscales_shapeÚdata_reshaper–  Úquant_data_int8Úzero_point_int8Úend_idxÚsliceÚquantized_slice_int8Úscale_sliceÚzero_point_slice_int8r2  re  Úzero_point_int4s   &&&&                   r"   Úquantize_ndarrayÚ+DefaultWeightOnlyQuantizer.quantize_ndarrayë  sâ  € ð ˆØ�J‰J�}Õ%ˆØˆÜ §
¡
Ö+‰FˆAØÔ Ø•’ØÖ"Ø•’ñ	 ,ð •N QÕ&¨:Õ5ˆÜ˜DŸJ™JÓ'ˆØ&.�]Ñ#à—|‘| Q¨1 IÓ.ˆÜ—’˜1¨Ð*°$·*±*Ô=ˆßÜ Ÿhšh¨¨a y¸Ô?‰Oä Ÿhšh¨¨a y¸Ô@ˆOÜ Ÿhšh¨°QÐ'7¸wÔGˆOô �q˜!Ö(ˆAÜ˜!�.¨!Ó,ˆGØ   A I¨q Õ1ˆEçÜ4N×4dÑ4dÐejÓ4kÑ1Ð$¡kô /×EÑEÀeÓLñ IÐ$ kÐ3Hð 0DˆO˜A˜q˜y¨!˜OÑ,Ø•ˆAØ(3ˆF�1�a˜1˜q�5�k 1Ð$Ñ%ß‘<Ø5J�  1¨¨A­ ;°Ð 1Ó2ñ )ô$ 5×FÑFÀÓWˆØˆßÜ8×JÑJÈ?Ó[ˆOØ—‘ Ó-ˆØ ¨Ð7Ð7r%   c               ó$   € V ^8„  d   QhRRRRRR/# rL  r!   )r   s   "r"   r#   r  #  s#   € ÷ > ñ >  Ið > Ð<Lð > ÐQ`ñ > r%   c                óŽ  € V P                   P                  \        P                  8X  g   Q R4       hV P                   P                  '       d   \
        P                  M\
        P                  pVP                  ^ ,          p\        WB4      w  rVVf   \        P                  R4       V.# \        P                  P                  V4      p\        VP                   4      pV P                   P"                  P%                  R^4      p	V P                   P&                  p
W˜8  d   W˜) 8¼  g   Q R4       hV
^8¼  d   V
^,
          V
,          ^ 8X  g   Q R4       hW˜,           V,          p	V P)                  WyW P                   P                  4      w  r¼pVP                   F1  pVP*                  V8X  g   K  VP                  P-                  V4        M	  \        P.                  P1                  VP*                  R,           W7P                   VP3                  4       R4      p\        P                  P5                  WÅP*                  R,           4      pVP*                  VP                  ^,          VP*                  .pVP6                  P9                  VV.4       V P                   P                  '       gƒ   \        P.                  P1                  VP*                  R	,           W<P                   VP3                  4       R4      pVP;                  VP*                  4       VP6                  P9                  V.4        \        P.                  P=                  VR
4      pRVRV	RV
/p\        P.                  P@                  ! RRTRVPB                  ^ ,          .RVP*                  '       d   VP*                  R,           MRRR/VB pV.#   \>         d    ^ p Lyi ; i)z,Quantize weight data of Gather node to int4.z0Gather only supports QOperator format currently.z4Gather doesn't have const weight. Skip quantization.r(   z&Invalid quantize axis for Gather node.z#Invalid block size for Gather node.Ú_Q4Trc  rd  rb   Úgather_axisri  rU   r°   rh  rÂ   ri  rj  rk  )ÚGatherBlockQuantized)"r­   r   r   r<   rh   r   r1  r2  rz  r{  rx  ry  r|  r}  r~  rÖ   r¤   r   ÚgetrU   rx  rÂ   rƒ  rˆ  rì  r9  r‚  r„  r…  rÙ   Úget_node_attr_valuerÓ   r‰  r‡  )r+   rM  rN  r:  Údata_argÚdata_tensorprotoÚdata_graphprotoÚdata_ndarrayÚ	data_rankri  rU   Úquantized_datar–  r—  rz  Úquantized_data_tensorprotoÚscales_tensorprotorž  Úzp_tensorprotor}  r   Úgather_q4_nodes   &&&                   r"   Úquantize_gatherÚ*DefaultWeightOnlyQuantizer.quantize_gather#  s9  € à�{‰{×'Ñ'¬;×+@Ñ+@Ô@ÐtÐBtÓtÐ@à$(§K¡K×$<×$<Ð$<”× Ò Ä+×BSÑBSˆØ—:‘:˜a•=ˆÜ,;¸HÓ,RÑ)ÐØÒ#Ü�K‰KÐNÔOØ�6ˆMä×(Ñ(×1Ñ1Ð2BÓCˆÜ˜×*Ñ*Ó+ˆ	ØŸ™×.Ñ.×2Ñ2°8¸QÓ?ˆØ—[‘[×+Ñ+ˆ
àÔ(¨]¸jÔ-HÐrÐJrÓrÐHØ˜RÔ j°1¥n¸
Õ%BÀaÔ%GÐoÐJoÓoÐHà&Õ2°iÕ?ˆØ.2×.CÑ.CØ¨·[±[×5MÑ5Mó/
Ñ+ˆ ð %×*Ô*ˆEØ�z‰z˜XÖ%Ø×%Ñ%×,Ñ,¨UÔ3Ùñ +ô
 &*§[¡[×%<Ñ%<Ø×!Ñ! EÕ)¨5×2DÑ2DÀn×F\ÑF\ÓF^Ð`dó&
Ð"ô "×.Ñ.×9Ñ9¸&×BWÑBWÐZcÕBcÓdÐØ1×6Ñ6¸¿
¹
À1½ÐGY×G^ÑG^Ð_ˆØ×#Ñ#×*Ñ*Ð,FÐHZÐ+[Ô\Ø�{‰{×'×'Ð'Ü!Ÿ[™[×4Ñ4Ø ×%Ñ%¨Õ6¸¿|¹|È[×M`ÑM`ÓMbÐdhóˆNð ×Ñ˜~×2Ñ2Ô3Ø×'Ñ'×.Ñ.°Ð/?Ô@ð	ÜŸ+™+×9Ñ9¸$ÀÓGˆKð
 ˜;Ø˜]Ø˜*ð
ˆô Ÿ™×.Ò.ñ 
àð
ð —[‘[ •^Ð$ð
ð '+§i§i i�—‘˜UÖ"°Rð	
ð
 #ð
ð ñ
ˆð ÐÐøô% ô 	ØŠKð	ús   Ì* N4 Î4OÏOc               ó$   € V ^8„  d   QhRRRRRR/# rL  r!   )r   s   "r"   r#   r  c  s"   € ÷ ñ ˜Yð Ð5Eð È/ñ r%   c                óº  € \         P                  RVP                   R24       V P                  P                  pVP
                  R8X  d[   V^8X  dB   V P                  P                  \        P                  8X  d   \         P                  R4       V.# V P                  W4      pM|VP
                  R8X  dF   V P                  P                  ^8w  d   \         P                  R4       V.# V P                  W4      pM&\         P                  RVP
                   R24       V.# \         P                  R	VP                   R
V P                  P                   R24       V# )rS  rT  rU  r'   z>MatMul only supports QOperator format for 8 bits quantization.r(   z)Gather only supports 4 bits quantization.zUnsupported operator z1 for weight only quantization. Skip quantization.rs  z with z	 bits ...)rx  ry  rÂ   r­   ra   rw  r   r   r�   ÚerrorrL  r‹  )r+   rM  rN  ra   Úresultss   &&&  r"   r§  Ú#DefaultWeightOnlyQuantizer.quantizec  s  € ô 	�‰Ð(¨¯©¨°4Ð8Ô9à�{‰{×ÑˆØ�<‰<˜8Ô#Ø�qŒy˜TŸ[™[×5Ñ5¼¿¹ÔHÜ—‘Ð]Ô^Ø�v�Ø×*Ñ*¨4Ó=‰GØ�\‰\˜XÔ%Ø�{‰{×Ñ 1Ô$Ü—‘ÐHÔIØ�v�à×*Ñ*¨4Ó=‰Gä�L‰LÐ0°·±°Ð>oÐpÔqØ�6ˆMä�‰Ð/°·	±	¨{¸&ÀÇÁ×AQÑAQÐ@RÐR[Ð\Ô]Øˆr%   rþ   N)r.   r/   r0   r1   r,   r  rL  rª  rZ  r`  rf  rx  r‹  r§  r2   r!   r%   r"   r  r  Ü  su   † õõ0,õdwðr ô&ó ð&ð ô2ó ð2ð ô/ó ð/ð ô58ó ð58õn> ÷@ñ r%   r  c                  ó.   € ] tR tRtR R ltR R ltRtR# )ÚNVAWQWeightOnlyQuantizeri…  c               ó   € V ^8„  d   QhRR/# )r   r­   rs   r!   )r   s   "r"   r#   Ú%NVAWQWeightOnlyQuantizer.__annotate__†  s   € ÷ ñ à*ñr%   c                	ó   € Wn         R # ró   rþ   rÿ   s   &&r"   r,   Ú!NVAWQWeightOnlyQuantizer.__init__†  r  r%   c               ó    € V ^8„  d   QhRRRR/# )r   ÚmodelúModelProto | strrP  r   r!   )r   s   "r"   r#   r•  Œ  s   € ÷ !ñ !Ð"2ð !°zñ !r%   c                ó  €  ^ RI Hp \        P                  R4       T P                  P                  pT! TT P                  P                  TR7      p\        P                  R4       T#   \         d    \        R4       \        R4      Rhi ; i)z¹
Perform nvidia_awq quantization using ModelOpt's int4 quantize function.

Args:
    model (ModelProto): The ONNX model to quantize.

Returns:
    ModelProto: The quantized ONNX model.
)r§  zlPlease ensure that the 'modelopt' package is installed. Please install it using pip install nvidia_modelopt.zXmodelopt is not installed. Please install it using pip install nvidia_modelopt. Exiting.Nz#Starting nvidia_awq quantization...)r�   rP   ú"Completed nvidia_awq quantization.)	Úmodelopt.onnx.quantization.int4r§  r‰   rŠ   rx  ry  r­   rP   r�   )r+   r™  Úquantize_int4r’   Úquantized_models   &&   r"   Úquantize_awqÚ%NVAWQWeightOnlyQuantizer.quantize_awqŒ  s�   € ð	ÝQô 	�‰Ð9Ô:ð —{‘{×:Ñ:ˆñ (ØØ#Ÿ{™{×=Ñ=Ø$0ô
ˆô 	�‰Ð8Ô9ØÐøô+ ô 	ÜØ~ôô Øjóàðð		ús   ‚A) Á)#Brþ   N)r.   r/   r0   r1   r,   r   r2   r!   r%   r"   r“  r“  …  s   † õ÷!ñ !r%   r“  c                  ór   € ] tR tRtRt^^€RRRR]P                  RRRR3R R lltR R ltR	 t	R
 t
R tRtR# )ÚMatMulNBitsQuantizeri°  ax  
Target node:        QOperator node:            QDQ nodes:
MatMul              MatMulNBits                DeQuantizeLinear -> MatMul
Gather              GatherBlockQuantized       Gather, Gather, Gather (optional) -> DequantizeLinear

Perform 2/4/8 bits quantization of constant weights for target nodes.
If algo_config.quant_format is QOperator:
  - nodes are replaced by the corresponding QOperator nodes.
  - quantized weights are stored in the contrib ops.
If algo_config.quant_format is QDQ:
  - the quantized weight is stored in a standard onnx node. For MatMul, it is DequantizeLinear. For Gather,
    it is the three Gathers, one for quantized data, one for scales and one for optional zero points.
  - The nodes are replaced by the corresponding QDQ nodes.
  - currently Gather is not supported in QDQ because Gather does not support int4 yet.
Note:
  - for quantized gather, the memory usage of "DequantizeLinear + Gather" is the same as the original Gather
    during runtime. Therefor it is not recommended.
  - when a node is in nodes_to_exclude, and the node configuration in algo_config.customized_weight_config will be ignored.
FNc               ó@   € V ^8„  d   QhRRRRRRRRRR	R
RRRRRRRRR/
# )r   r™  rš  ra   rg   rU   rh   ri   rj   rk   Únodes_to_includezlist[str] | Noner   r   r   r   rl   Úalgo_configzWeightOnlyQuantConfig | Noner!   )r   s   "r"   r#   Ú!MatMulNBitsQuantizer.__annotate__Å  sz   € ÷ 0Mñ 0Màð0Mð ð0Mð ð	0Mð
 ð0Mð #ð0Mð +ð0Mð 5ð0Mð 7ð0Mð !%ð0Mð 2ñ0Mr%   c                	ó0  € Vf   . p\        V\        4      '       d    \        \        P                  ! V4      4      M
\        V4      V n        \        V\        4      '       d   TMR V n        W n        W0n        W@n	        WPn
        \        V4      V n        V'       d   \        V4      MR V n        R V n        Vf   \        VVVVV	V
VVR7      pWÀn        \#        V P                   R4      '       d#   V P                   P                  R9   g   Q R4       hVP$                  R8X  d   \'        V P                   4      V n        R # VP$                  R8X  d   \)        V P                   4      V n        R # VP$                  R8X  d   \+        V P                   4      V n        R # R # )N)rU   rh   rj   r   r   r   ra   rl   ra   z(Only support 2, 4 or 8 bits quantizationr_   ro   ry   r*  )rš   r   r   r|  Úloadr™  Ú
model_pathra   rU   rh   rj   r)   Únodes_to_excluder¥  Únode_quantizerre   r¦  Úhasattrr   rú   r  r“  )r+   r™  ra   rU   rh   rj   r«  r¥  r   r   r   rl   r¦  s   &&&&&&&&&&&&&r"   r,   ÚMatMulNBitsQuantizer.__init__Å  sX  € ð Ò#Ø!ÐÜ4>¸uÄc×4JÒ4J”YœtŸyšy¨Ó/Ô0ÔPYÐZ_ÓP`ˆŒ
Ü#-¨e´S×#9Ò#9™%¸tˆŒØŒ	Ø$ŒØ(ÔØ,ÔÜ #Ð$4Ó 5ˆÔß9I¤Ð$4Ô 5ÈtˆÔØ"ˆÔàÒÜ6Ø%Ø)Ø-Ø)Ø%9Ø%ØØ'=ô	ˆKð 'ÔÜ�4×#Ñ# V×,Ò,Ø×#Ñ#×(Ñ(¨IÔ5ÐaÐ7aÓaÐ5à× Ñ  EÔ)Ü"8¸×9IÑ9IÓ"JˆDÖØ×"Ñ" iÔ/Ü"<¸T×=MÑ=MÓ"NˆDÖØ×"Ñ" lÔ2Ü":¸4×;KÑ;KÓ"LˆDÖñ 3r%   c               ó   € V ^8„  d   QhRR/# )r   rN  rO  r!   )r   s   "r"   r#   r§  ÷  s   € ÷ .ñ .Ð-=ñ .r%   c                	óL  € . pVR,          pVP                    EFÆ  pVP                   Uu. uFY  pVP                  \        P                  P
                  8X  g,   VP                  \        P                  P                  8X  g   KW  VNK[  	  ppV'       Edb   / pVP                   EF  pVP                  \        P                  P
                  8X  d:   VP                  VP                  4       VP                  V P                  V4      /pMŠVP                  \        P                  P                  8X  dW   . p	VP                   F5  p
VP                  V
4       V	P                  V P                  V4      .4       K7  	  VP                  V	/pM\        V4      pVP                  V4       EK  	  \        P                  P                   ! VP"                  VP$                  VP&                  3RVP                  /VB p. pVP                  V P(                  9   d(   \*        P-                  RVP                   R24       V.pM”V P.                  '       d   VP                  V P.                  9   g&   VP"                  V P0                  P2                  9   d   V P4                  P7                  WA4      pM&\*        P-                  RVP                   R24       V.pVP                  V4       EKÉ  	  VP9                  R4       VP                   P                  V4       VP;                  4        V# u upi )r  rÂ   zexclude to quantize z$ as specified by nodes_to_exclude...zskip to quantize rU  rM  r™   )rM  Ú	attributer´  r|  ÚAttributeProtoÚGRAPHÚGRAPHSrÙ   ÚgrÂ   Ú_process_subgraphÚgraphsr…  r   r¬   rˆ  r‰  rw  rz  r‡  r«  rx  ry  r¥  r¦  r   r¬  r§  Ú
ClearFieldÚpop)r+   rN  Ú	new_nodesr¯  rM  ÚattrÚgraph_attrsr   Úkvrã  ÚsubgraphÚ	out_nodess   &&          r"   r¶  Ú&MatMulNBitsQuantizer._process_subgraph÷  sv  € Øˆ	Ø˜B•ˆà—J•JˆDð !ŸNšNóá*�DØ—9‘9¤× 3Ñ 3× 9Ñ 9Ô9¸T¿Y¹YÌ$×J]ÑJ]×JdÑJdÑ=d÷ �Ù*ð ð ÷
 ˆ{Ø�Ø ŸN�N�DØ—y‘y¤D×$7Ñ$7×$=Ñ$=Ô=à#×*Ñ*¨4¯6©6Ô2Ø"Ÿi™i¨×)?Ñ)?ÀÓ)LÐM™ØŸ™¤d×&9Ñ&9×&@Ñ&@Ô@Ø "˜Ø(,¯¬˜Hà'×.Ñ.¨xÔ8Ø!ŸL™L¨$×*@Ñ*@ÀÓ*MÐ)NÖOñ )4ð #Ÿi™i¨Ð/™ä/°Ó5˜Ø—M‘M "×%ñ +ô —{‘{×,Ò,Ø—L‘L $§*¡*¨d¯k©kñØ@DÇ	Á	ðØMSñ�ð ˆIØ�y‰y˜D×1Ñ1Ô1Ü—‘Ð2°4·9±9°+Ð=aÐbÔcØ!˜F‘	Ø×'×'Ð'¨D¯I©I¸×9NÑ9NÔ,NØ—‘ × 0Ñ 0× EÑ EÔEà ×/Ñ/×8Ñ8¸ÓK‘	ä—‘Ð/°·	±	¨{¸$Ð?Ô@Ø!˜F�	Ø×Ñ˜Y×'ñK ðN 	×Ñ˜Ô Ø�
‰
×Ñ˜)Ô$Ø�‰ÔØˆùòSs   ªAL!ÂL!c                óÂ  a € / pS P                   P                   P                  P                   EF,  pVP                  R	9   g   K  \        ;QJ d)    V 3R lVP
                   4       F  '       d   K   RM	  RM! V 3R lVP
                   4       4      '       d   Km  R^RS P                  RS P                  '       d   RMR/pS P                  P                  '       dp   VP                  S P                  P                  9   dK   S P                  P                  VP                  ,          P                  4        F  w  rEWC9   g   K  WSV&   K  	  W1VP                  &   EK/  	  V# )
z3Generate weight only quant configuration for nodes.c              3  ó^   <"  € T F"  pSP                   P                  V4      R J x € K$  	  R # 5iró   )r™  r{  )r[  r¶   r+   s   & €r"   r]  Ú@MatMulNBitsQuantizer._generate_q4_node_config.<locals>.<genexpr>,  s%   øé € ÐUÉ*ÀQ˜4Ÿ:™:×5Ñ5°aÓ8¸DÕ@Ë*ùs   ƒ*-FTra   r>  ÚschemeÚsymÚasym©r'   )r™  r¯  rM  rw  Úallrz  rU   rh   r¦  r   rÂ   rÚ   )r+   Úq4_node_configrM  Útemplate_config_q4Úkeyrã  s   f     r"   Ú_generate_q4_node_configÚ-MatMulNBitsQuantizer._generate_q4_node_config'  s  ø€ àˆØ—J‘J×$Ñ$×*Ñ*×/Õ/ˆDØ�|‰|˜zÖ)ß“sÔUÈ$Ï*Ê*ÓU—s—s’sÔUÈ$Ï*Ê*ÓU×UÔUà Ø$ d§o¡oØ ¨4×+<×+<Ð+<¡%À&ð*Ð&ð ×(Ñ(×A×AÐAØ ŸI™I¨×)9Ñ)9×)RÑ)RÔRà*.×*:Ñ*:×*SÑ*SÐTX×T]ÑT]Õ*^×*dÑ*dÖ*f™J˜CØ"Ö8Ø:?°3Ó 7ñ +gð 1C 4§9¡9Ô-ñ 0ð  Ðr%   c           	     ó¨  a € V 3R lp/ pS P                   e   S P                   VR&   S P                  4       pS P                  P                  p\        P                  RV R24       VR9   dy   S P                  P                  VR&   WBR&    S P                   F  pRW5&   K	  	  \        RR	S P                  e   S P                  MS P                  P                  R
V/VB S n
        MÔVR8X  dÎ   S P                  P                  VR&   S P                  P                  VR&   S P                  P                  VR&   S P                  P                  VR&   S P                  P                  VR&   RVR&   V! 4       p\!        RR	S P                  e   S P                  MS P                  P                  R
VRV/VB S n
        \        P                  RV R24       R# )uí   4b quantize a model with RTN or GPTQ algorithm. Please refer to
https://github.com/intel/neural-compressor/blob/master/docs/source/quantization_weight_only.md
for more details on weight only quantization using IntelÂ® Neural Compressor.
c               3  ó‚   <"  € \         P                  ! SP                  P                  4      p V  F	  pVR 3x € K  	  R # 5iró   )ÚcopyÚdeepcopyr¦  rP   )Údata_readerrê   r+   s     €r"   Úinc_dataloaderÚ<MatMulNBitsQuantizer.int4_quant_algo.<locals>.inc_dataloaderB  s4   øé € ÜŸ-š-¨×(8Ñ(8×(PÑ(PÓQˆKÛ#�Ø˜D�jÔ ó $ùs   ƒ<?Nrj   zstart to quantize model with z algorithm...r>   r   Úfp32r™  Úweight_configrS   rT   Ú	blocksizerV   rW   rX   Ú	n_samplesÚ
dataloaderz$complete quantization of model with z algorithm.)r9   rK   r!   r™   )rj   rÌ  r¦  r   rx  ry  r>   r«  r   rª  r™  rT   rU   rV   rW   rX   r   )r+   rÓ  r   Úweight_only_node_configr   rm  rÙ  s   f      r"   Úint4_quant_algoÚ$MatMulNBitsQuantizer.int4_quant_algo<  sÔ  ø€ õ	!ð
 ˆØ×ÑÒ*Ø'+×':Ñ':ˆFÐ#Ñ$Ø"&×"?Ñ"?Ó"AÐà×$Ñ$×.Ñ.ˆ	Ü�‰Ð3°I°;¸mÐLÔMØÐ*Ô*Ø#×/Ñ/×6Ñ6ˆF�8ÑØ"+�;Ñðð ×*Ô*�Ø-3Ð'Ó*ñ +ô &ñ Ø)-¯©Ò)D�d—o’oÈ$Ï*É*×JZÑJZðà5ðð ñˆD�Jð
 ˜&Ô Ø!%×!1Ñ!1×!:Ñ!:ˆF�:ÑØ"&×"2Ñ"2×"=Ñ"=ˆF�;ÑØ!%×!1Ñ!1×!:Ñ!:ˆF�:ÑØ ×,Ñ,×0Ñ0ˆF�5‰MØ#'×#3Ñ#3×#>Ñ#>ˆF�<Ñ Ø"$ˆF�;ÑÙ'Ó)ˆJä&ñ Ø)-¯©Ò)D�d—o’oÈ$Ï*É*×JZÑJZðà5ðð &ðð ñ	ˆDŒJô 	�‰Ð:¸9¸+À[ÐQÖRr%   c                	óP  € V P                   P                  R
9   Ed±   V P                  P                  4       .pV P                   P                  \
        P                  8X  d   V P                  P                  R^4       V P                   P                  \
        P                  8X  g   RV P                   P                  9   dà   V P                  P                  4       pV Fd  pVP                  R9   g   K  VP                  ^8  g   K)  \        P                  R4       V P                  P                  VP                  ^4       Kf  	  V P                  P                  P                  pV^
8  d5   \        P                  RV R24       ^
V P                  P                  n        V P!                  V4       V P                  P#                  4        R# V P                   P                  R8X  d¯   \        P%                  R4       V P&                  P)                  V P*                  f   V P                  P                  MV P*                  4      V n        \        P%                  R	4       \-        V P                  4      V n        V P                  P#                  4        R# V P/                  4        R# )r_   rk  r(   NzžThe opset of the input model is under 21 and doesn't support int4 data type. Force to update it to opset 21, but the generated model may not be a valid model.z%The IR version of the input model is z> and doesn't support int4 data type. Force to update it to 10.ry   z%Processing nvidia_awq quantization...rœ  )r_   ro   )Nzai.onnxri  )r¦  r   r™  r¯  r   r   r<   Úset_opset_importr�   r   Úopset_importrj  Úversionrx  ÚwarningÚ
ir_versionr¶  Úclean_initializersry  r¬  r   rª  r   rÛ  )r+   rN  rß  Úopsetrâ  s   &    r"   ÚprocessÚMatMulNBitsQuantizer.processn  sè  € Ø×Ñ×%Ñ%Ð);Õ;àŸ:™:×+Ñ+Ó-Ð.ˆKð ×Ñ×,Ñ,´×0EÑ0EÔEØ—
‘
×+Ñ+¨O¸QÔ?à×Ñ×,Ñ,´·±Ô?À8Èt×O_ÑO_×OtÑOtÔCtØ#Ÿz™z×6Ñ6Ó8�Û)�EØ—|‘|Ð'<Ö<ÀÇÁÐQSÖASÜŸ™ðpôð Ÿ
™
×3Ñ3°E·L±LÀ"ÖEñ *ð "ŸZ™Z×-Ñ-×8Ñ8�
Ø ”?Ü—N‘NØ?À
¸|ð L4ð 4ôð 35�D—J‘J×$Ñ$Ô/à×"Ñ" ;Ô/Ø�J‰J×)Ñ)Ö+Ø×Ñ×'Ñ'¨<Ô7ä�K‰KÐ?Ô@Ø×,Ñ,×9Ñ9Ø$(§O¡OÒ$;�—
‘
× Ò ÀÇÁóˆDŒJô �K‰KÐ<Ô=Ü" 4§:¡:Ó.ˆDŒJØ�J‰J×)Ñ)Ö+ð × Ñ Ö"r%   )
rj   r¦  ra   rU   rh   r™  rª  r¬  r«  r¥  )r.   r/   r0   r1   Ú__doc__r   r<   r,   r¶  rÌ  rÛ  rå  r2   r!   r%   r"   r£  r£  °  sN   † ñð. ØØ"Ø%)ØØ-1Ø ×*Ñ*Ø7;Ø9=Ø',Ø48÷0Mõd.ò`ò*0Söd*#r%   r£  c                ó(   € V P                  4       R9   # )Útrue)ré  Ú1)Úlower)rã  s   &r"   Úort_convert_str_to_boolrì  ›  s   € Ø�;‰;‹=˜MÑ)Ð)r%   c                óB   € V P                  R 4      w  rV\        V4      3# )Ú:)rÃ   rg   )ÚsrË  rã  s   &  r"   Úparse_key_value_pairrð     s   € Ø—‘˜“�J€CØ”�E“
ˆ?Ðr%   c                 óü  € \         P                  ! R R7      p V P                  RRRR7       V P                  RRRR7       V P                  RR	^ \        R
R7       V P                  RR\        . RCORR7       V P                  R^\        RR7       V P                  RR	RRR\
        RR	.RR7       V P                  RR	\        RR7       V P                  RRR	RR7       V P                  R	R7       V P                  R R!\        R	. R"R#7       V P                  R$R!\        R	R%R&7       V P                  R'R(\        R(R).R*R7       V P                  R+\        R!R,R-.R.R/7       V P                  R0\        R!R	R1R27       V P                  RR34      pVP                  R4\        R5R6R77       VP                  R8\        R	R9R:7       VP                  R;\        R	R<R=.R>R?7       VP                  R@\        RARBR77       V P                  4       # )Da
  Blockwise int4 quantization for MatMul 2D weight matrices.

A weight matrix is partitioned into into blocks, where each block is a
continguous subset inside each column. Each block is quantized into a
set of 4b integers with a scaling factor and an optional offset.
)Údescriptionz--input_modelTzPath to the input model file)ÚrequiredÚhelpz--output_modelzPath to the output model filez--block_sizeFzBlock size for quantization)ró  Údefaultr´  rô  z--quant_methodrõ  ry   uW   the algorithm used to quantize weight, 
rtn and gptq leverage IntelÂ® Neural Compressor)rõ  r´  Úchoicesrô  z--bitsz#the target bits to represent weight)rõ  r´  rô  z--symmetricÚ?zWIndicate whether to quantize the model symmetrically, symmetric is not supported by hqq)ró  rõ  ÚconstÚnargsr´  rö  rô  z--accuracy_levelzíAccuracy level of the 4-bit quantized MatMul computation. Refer to the MatMulNBits contrib op's 'accuracy_level' attribute for details (https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftmatmulnbits).)ró  r´  rô  z-vz	--verboseÚ
store_true)ró  Úaction)r   z--nodes_to_excludeÚ+zBSpecify the nodes to be excluded from quantization with node names)rù  r´  ró  rõ  rô  z--nodes_to_includezKSpecify the specific nodes to be included from quantization with node names)rù  r´  ró  rô  z--quant_formatr<   r�   z±QuantFormat {QOperator, QDQ}QOperator format quantizes the model with quantized operators directly.QDQ format quantize the model by inserting DeQuantizeLinear before the MatMul.z--op_types_to_quantizer'   r(   zPop_types_to_quantize {MatMul, Gather}. Operators to quantize. Default is MatMul.)r´  rù  rö  rô  z--quant_axeszªKey-value pairs in op_type:axis_to_quantize separated by space.Specify the axis to quantize for an op. Default {MatMul:0, Gather:1}Example: --quant_axes MatMul:0 Gather:1)r´  rù  ró  rô  z-Arguments specific to nvidia_awq quantizationz--calib_dataset_namer¿   z/Name of the calibration dataset for nvidia_awq.)r´  rõ  rô  z--tokenizer_dirzPath of the tokenizer dir.)r´  ró  rô  z--calibration_methodÚawqÚawq_clipz<Support two options, awq implementation and weight clipping.)r´  ró  rö  rô  z--cache_dirrð   z%Cache directory for calibration data.)rõ  ÚhqqÚrtnrK   Úgptqry   )
ÚargparseÚArgumentParserÚadd_argumentrg   r   rì  Úset_defaultsrð  Úadd_argument_groupÚ
parse_args)ÚparserÚnv_awq_configs     r"   r  r  ¥  s�  € Ü×$Ò$ðô€Fð ×Ñ˜°$Ð=[ÐÔ\Ø
×ÑÐ(°4Ð>]ÐÔ^Ø
×Ñ˜°ÀÌÐSpÐÔqØ
×ÑØØÜÚJØgð ô ð ×Ñ˜¨!´#Ð<aÐÔbØ
×ÑØØØØØÜ$Ø�u�Øfð ô 	ð ×ÑØØÜðqð	 ô ð ×Ñ˜˜k°EÀ,ÐÔOØ
×Ñ ÐÔ&Ø
×ÑØØÜØØØQð ô ð ×ÑØØÜØØZð ô ð ×ÑØØÜØ˜eÐ$ðYð ô ð ×ÑØ ÜØØ˜8Ð$Ø_ð ô ð ×ÑØÜ!ØØð2ð ô ð ×-Ñ-¨lÐ<kÓl€MØ×ÑØÜØØ>ð	 ô ð ×ÑØÜØØ)ð	 ô ð ×ÑØÜØØ˜
Ð#ØKð ô ð ×ÑØÜØØ4ð	 ô ð ×ÑÓÐr%   Ú__main__zfile z already existsrÿ  zAsymmetric is not supportted by hqq, will force to symmetric=FalseF)rU   ra   r   r   rõ  )rU   rh   rj   r   r   r   ra   r   )r   rK   r  )rU   r   ry   zFQOperator is not applicable to nvidia_awq. overriding the value to QDQrý  rñ   rþ  )r|   r‘   r~   r�   z!Unsupported quantization method: )r™  ra   rj   r«  r¥  r¦  TrÇ  )`Ú
__future__r   r  rÐ  ÚloggingÚosr5  rÛ   rô   Únumpy.typingÚtypingÚnptr|  Úonnx_irr3  Úonnx.onnx_pbr   r   r   r   Úonnxruntime.capi._pybind_stater   r	   r
   r   Ú	calibrater   Úneural_compressorr   r   Ú
onnx_modelr   Úquant_utilsr   r   ÚbasicConfigÚINFOÚ	getLoggerr.   rx  r   r4   rG   rN   r[   re   rs   rø   rú   r{  r†  rŠ  r  r  r“  r£  rì  rð  r  Úargsr   ÚsetLevelÚDEBUGÚinput_modelÚinput_model_pathÚoutput_modelÚoutput_model_pathr   r   r»  r   ÚpathÚexistsr�  Ú	ExceptionÚ	symmetricÚquant_methodrá  r©  r™  rU   ra   Úquant_configrj   r<   r�   r�   Úcalib_dataset_namer‘   r~   rÓ   r«  r¥  Úquantrå  Úsave_model_to_filer!   r%   r"   Ú<module>r+     sí  ðõ #ã Û Û Û 	ã Û Ý Û Û ß GÓ G÷ó õ -ß :Ý !ß 8à × Ò ÐOÐW^×WcÑWcÕ dØ	×	Ò	˜8Ó	$€÷Añ Aô@#Ð4ô #ôLÐ"7ô ôD0%Ð 5ô 0%ôf(Ð4ô (ôV/kÐ#8ô /kôds#Ð!6ô s#òl4÷Pñ Põfõõ&òF#÷&fñ f÷R(ñ (÷Vh#ñ h#òV*ò
ò
lð^ ˆzÕÙ‹<€DØ‡|‡|€|Ø�‰˜Ÿ™Ô&à×'Ñ'ÐØ×)Ñ)ÐØ˜t×0Ñ0Õ1€LØ?C×?X×?XÐ?X™5 ×!:Ñ!:Ô;Ð^iÐØ+/¯?¯?¨?‘�t—‘Ô'À€Jà	‡w�w‡~�~Ð'×(Ò(Ø�‰�uÐ.Ð/¨Ð?Ô@Ù˜%Ð 1Ð2°/ÐBÓCÐCà‡~‡~€~˜$×+Ñ+¨uÔ4Ø�‰ÐZÔ[ØˆŒà�IŠIÐ&Ó'€EØ×Ñ˜EÔ!Ù/Ø—‘¨T¯Y©YÐMaÐnxô
Šð 
×	Ñ	˜iÔ	'Ù3Ø—‘ØŸ™Ø×.Ñ.Ø%Ø!5Ø!Ø—‘ô
Šð 
×	Ñ	˜eÔ	#Ù/ÐEYÔZ‰Ø	×	Ñ	˜iÔ	'Ù2ÐH\Ô]‰Ø	×	Ñ	˜fÔ	$Ù0¸D¿O¹OÐbvÔw‰Ø	×	Ñ	˜lÔ	*Ø˜;×0Ñ0Ô0Ø�N‰NÐcÔdØ&Ÿ?™?ˆLà ˆØ×"Ñ"Ò.Ø×&Ñ&¨%Ô/Ø%/Ñ"à%/Ñ"à!+Ðá1Ø×0Ñ0Ø×,Ñ,Ø—n‘nØ1ô	
‰ñ Ð<¸T×=NÑ=NÐ<OÐPÓQÐQá ØØ�Y‰YØ×*Ñ*Ø×.Ñ.Ø×.Ñ.Ø ô€Eð 
‡M�M„OØ	‡K�K×"Ñ"Ð#4°dÖ;ñQ r%   