+
    G-j@#  ã                   ó²  € ^ RI t ^ RIt^ RIt^ RIt^ RIHt ^ RIt^ RI	H
t
HtHtHt ^ RIHt ^RIHt ^RIHt ]P(                  ! ]4      t ! R R4      tR t]R	8X  dù   ]! 4       t]P4                  '       d   ]P7                  ]P8                  4       ]P:                  t]P>                  t ]PB                  PE                  ] 4      '       d"   ]PG                  R
]  R24       ]$! R
]  R24      h]PJ                  ! ]4      t&]! ]&]PN                  ]PP                  ]PR                  R7      t*]*PW                  4        ]*PL                  PY                  ] R4       R# R# )é    N)Ú
GraphProtoÚ
ModelProtoÚ	NodeProtoÚTensorProto)Úquantize_matmul_bnb4)Ú	ONNXModel)Úattribute_to_kwargc                   óš   a € ] tR t^t o Rt^ t^tRV 3R lR llt]V 3R lR l4       t	V 3R lR lt
V 3R	 lR
 ltV 3R lR ltR tRtV tR# )ÚMatMulBnb4QuantizerzMPerform 4b quantization of constant MatMul weights using FP4 or NF4 data typeNc                ó,   <€ V ^8„  d   QhRS[ RS[RS[/# )é   ÚmodelÚ
quant_typeÚ
block_size)r   Úint)ÚformatÚ__classdict__s   "€Ú/Volumes/fast/ai/experiments/nudenet-smoke/.venv/lib/python3.14/site-packages/onnxruntime/quantization/matmul_bnb4_quantizer.pyÚ__annotate__Ú MatMulBnb4Quantizer.__annotate__%   s"   ø€ ÷ 6ñ 6™jð 6±cð 6Ásñ 6ó    c                óÄ   € T;'       g    . pV\         P                  \         P                  39   g   Q h\        V4      V n        W n        W0n        \        V4      V n        R # ©N)	r   ÚFP4ÚNF4r   r   r   r   ÚsetÚnodes_to_exclude)Úselfr   r   r   r   s   &&&&&r   Ú__init__ÚMatMulBnb4Quantizer.__init__%   sU   € Ø+×1Ð1¨rÐØÔ1×5Ñ5Ô7J×7NÑ7NÐOÔOÐOÐOÜ˜uÓ%ˆŒ
Ø$ŒØ$ŒÜ #Ð$4Ó 5ˆÖr   c                óL   <€ V ^8„  d   QhRS[ S[,          RS[S[S[3,          /# )r   Ú
graph_pathÚreturn)Úlistr   Útupler   )r   r   s   "€r   r   r   .   s+   ø€ ÷ ñ ©D±Õ,<ð ÁÁ{ÑT^ÐG^ÕA_ñ 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# )é   éÿÿÿÿ)NN)ÚrangeÚlenÚinitializerÚname)r,   r"   ÚgidÚgraphÚtensors   &&   r   Ú__get_initializerÚ%MatMulBnb4Quantizer.__get_initializer-   sQ   € äœ˜Z›¨1Õ,¨b°"Ö5ˆCØ•OˆEØ×+Ô+�Ø—;‘; $Ö&Ø!˜=Ô(ó ,ñ 6ð
 Ðr   c                óN   <€ V ^8„  d   QhRS[ P                  RS[P                  /# )r   Úfpweightr#   )ÚnptÚ	ArrayLikeÚnpÚndarray)r   r   s   "€r   r   r   6   s#   ø€ ÷  ñ  ©¯©ð  ¹2¿:¹:ñ  r   c           	     óÀ  € \        VP                  4      ^8w  d   \        R4      hVP                  4       P	                  4       pVP                  w  r4W4,          pV P
                  pWV,           ^,
          V,          pV^,           ^,          p\        P                  ! VRR7      p	\        P                  ! WqP                  R7      p
\        W’W¦V P                  WC4       Wš3# )z4b quantize fp32/fp16 weightz9Current bnb4 block quantization only supports 2D tensors!Úuint8)Údtype)r*   ÚshapeÚ
ValueErrorÚ	transposeÚcopyr   r6   Úzerosr:   r   r   )r   r3   Ú
fpweight_tÚrowsÚcolsÚnumelr   Ú
num_blocksÚquantized_numelÚpackedÚabsmaxs   &&         r   Úbnb4_block_quantÚ$MatMulBnb4Quantizer.bnb4_block_quant6   s³   € ô ˆx�~‰~Ó !Ô#ÜÐXÓYÐYð ×'Ñ'Ó)×.Ñ.Ó0ˆ
à—^‘^‰
ˆØ•ˆØ—_‘_ˆ
ØÕ(¨1Õ,°Õ;ˆ
Ø  1�9¨Õ*ˆä—’˜/°Ô9ˆÜ—’˜*¯N©NÔ;ˆä˜V°ÀTÇ_Á_ÐVZÔaàÐÐr   c                ó<   <€ V ^8„  d   QhRS[ RS[S[,          RS[ /# )r   ÚnodeÚgraph_stackr#   )r   r$   r   )r   r   s   "€r   r   r   L   s(   ø€ ÷ 5 ñ 5 ©Yð 5 ÁTÉ*ÕEUð 5 ÑZcñ 5 r   c                ó˜  € VP                   R8w  d   V# \        P                  RVP                   R24       VP                  V P                  9   d&   \        P                  RVP                   R24       V# VP
                  ^,          p\        P                  W24      w  rEVf   \        P                  R4       V# \        P                  P                  V4      p\        VP                  4      ^8w  d   \        P                  R4       V# V P                  V4      w  rx\        P                  P                  V4      p	VP                  R,           V	n        VP
                   F1  p
V
P                  V8X  g   K  VP
                  P                  V
4        M	  \        P                  P                  V4      pVP                  R	,           Vn        VP                   P#                  W›.4       / pVP                  w  rÞWÜR
&   WìR&   V P$                  VR&   V P&                  VR&   \        P(                  P*                  ! RRVP
                  ^ ,          V	P                  VP                  .RVP,                  ^ ,          .RVP                  '       d   VP                  R,           MRRR/VB p\        P                  RVP                   R24       V# )zdIf the node is MatMul with fp32 const weight, quantize the weight with int4, and return the new nodeÚMatMulzstart to quantize z ...zexclude to quantize z$ as specified by nodes_to_exclude...z2MatMul doesn't have const weight. Skip to quantizez)MatMul weight is not 2D. Skip to quantizeÚ_Bnb4Ú_absmaxÚKÚNr   r   ÚinputsÚoutputsr,   Ú Údomainúcom.microsoftzcomplete quantization of )Ú
MatMulBnb4)Úop_typeÚloggerÚdebugr,   r   Úinputr   Ú%_MatMulBnb4Quantizer__get_initializerÚonnxÚnumpy_helperÚto_arrayr*   r;   rH   Ú
from_arrayÚremover+   Úextendr   r   ÚhelperÚ	make_nodeÚoutput)r   rK   rL   ÚinputBÚBÚBs_graphÚB_arrayrF   rG   ÚB_quantr\   Úabsmax_tensorÚkwargsrA   rB   Úmatmul_bnb4_nodes   &&&             r   Ú_bnb4_matmul_node_weightÚ,MatMulBnb4Quantizer._bnb4_matmul_node_weightL   sO  € ð �<‰<˜8Ô#ØˆKä�‰Ð)¨$¯)©)¨°DÐ9Ô:Ø�9‰9˜×-Ñ-Ô-Ü�L‰LÐ/°·	±	¨{Ð:^Ð_Ô`ØˆKà—‘˜A•ˆÜ)×;Ñ;¸FÓP‰ˆØŠ9Ü�L‰LÐMÔNØˆKä×#Ñ#×,Ñ,¨QÓ/ˆÜˆw�}‰}Ó Ô"Ü�L‰LÐDÔEØˆKà×.Ñ.¨wÓ7‰ˆÜ×#Ñ#×.Ñ.¨vÓ6ˆØ—v‘v Õ'ˆŒØ—^”^ˆEØ�z‰z˜VÖ#Ø—‘×%Ñ% eÔ,Ùñ $ô
 ×)Ñ)×4Ñ4°VÓ<ˆØŸV™V iÕ/ˆÔà×Ñ×#Ñ# WÐ$<Ô=àˆØ—]‘]‰
ˆØˆs‰Øˆs‰Ø#Ÿ™ˆˆ|ÑØ#Ÿ™ˆˆ|ÑäŸ;™;×0Ò0ñ 
à—J‘J˜q•M 7§<¡<°×1CÑ1CÐDð
ð —[‘[ •^Ð$ð
ð )-¯	¯	¨	�—‘˜WÖ$°rð	
ð
 #ð
ð ñ
Ðô 	�‰Ð0°·±°¸4Ð@ÔAàÐr   c                ó0   <€ V ^8„  d   QhRS[ S[,          /# )r   rL   )r$   r   )r   r   s   "€r   r   r   ƒ   s   ø€ ÷ $ñ $©T±*Õ-=ñ $r   c                óº  € . 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VP                  V P)                  WA4      4       EK   	  VP+                  R4       VP                   P                  V4       VP-                  4        V# u upi )r'   r,   rK   r(   )rK   Ú	attributeÚtyper^   ÚAttributeProtoÚGRAPHÚGRAPHSÚappendÚgr,   Ú_process_subgraphÚgraphsrc   r	   Úupdaterd   re   rY   r\   rf   ro   Ú
ClearFieldÚpop)r   rL   Ú	new_nodesr.   rK   ÚattrÚgraph_attrsrm   ÚkvÚvalueÚsubgraphs   &&         r   rz   Ú%MatMulBnb4Quantizer._process_subgraphƒ   sÙ  € Øˆ	Ø˜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ñ�ð ×Ñ˜T×:Ñ:¸4ÓM×Nñ7 ð: 	×Ñ˜Ô Ø�
‰
×Ñ˜)Ô$Ø�‰ÔØˆùò?s   ªAIÂIc                óv  € V P                   P                  4       .pV P                   P                  4       pR pV F  pVP                  R8X  g   K  RpK  	  V'       g1   VP	                  \
        P                  P                  R^4      .4       V P                  V4       V P                   P                  4        R# )FrW   TN)
r   r.   Úopset_importrV   rc   r^   rd   Úmake_opsetidrz   Úclean_initializers)r   rL   r‡   Úhas_ms_domainÚopsets   &    r   ÚprocessÚMatMulBnb4Quantizer.process©   sŽ   € à—z‘z×'Ñ'Ó)Ð*ˆØ—z‘z×.Ñ.Ó0ˆàˆÛ!ˆEØ�|‰|˜Ö.Ø $’ñ "÷ Ø×Ñ¤§¡×!9Ñ!9¸/È1Ó!MÐ NÔOà×Ñ˜{Ô+Ø�
‰
×%Ñ%Ö'r   )r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r   Ústaticmethodr]   rH   ro   rz   rŒ   Ú__static_attributes__Ú__classdictcell__)r   s   @r   r   r      s^   ø‡ € ÙWð €Cð €C÷6ò 6ð ÷ó ð÷ ð  ÷,5 ð 5 ÷n$ð $÷L(ð (r   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	^\        P                  \        P
                  .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                  4       # )a  Blockwise FP4/NF4 quantization for MatMul 2D weight matrices.

A weight matrix is partitioned into blocks, where each block is a contiguous
subset inside the flattened transposed weight matrix. Each block is quantized
into a set of 4b integers with an absolute value scaling factor.
)Údescriptionz--input_modelTzPath to the input model file)ÚrequiredÚhelpz--output_modelzPath to the output model filez--quant_typeFz&Quantization data type. 0: FP4, 1: NF4)r˜   ÚdefaultÚchoicesr™   z--block_sizezVBlock size for blockwise quantization. Note: bnb.nn.Linear4bit only uses block_size=64)r˜   rš   r™   z-vz	--verboseÚ
store_true)r˜   Úaction)Úverbosez--nodes_to_excludeÚ+zBSpecify the nodes to be excluded from quantization with node names)Únargsrt   r˜   rš   r™   )	ÚargparseÚArgumentParserÚadd_argumentr   r   r   Úset_defaultsÚstrÚ
parse_args)Úparsers    r   r¦   r¦   ¹   sû   € Ü×$Ò$ðô€Fð ×Ñ˜°$Ð=[ÐÔ\Ø
×ÑÐ(°4Ð>]ÐÔ^Ø
×ÑØØØÜ$×(Ñ(Ô*=×*AÑ*AÐBØ5ð ô ð ×ÑØØØØeð	 ô ð ×Ñ˜˜k°EÀ,ÐÔOØ
×Ñ ÐÔ&Ø
×ÑØØÜØØØQð ô ð ×ÑÓÐr   Ú__main__zfile z already exists)r   T)-r¡   ÚloggingÚosÚnumpyr6   Únumpy.typingÚtypingr4   r^   Úonnx.onnx_pbr   r   r   r   Úonnxruntime.capi._pybind_stater   Ú
onnx_modelr   Úquant_utilsr	   Ú	getLoggerrŽ   rZ   r   r¦   Úargsrž   ÚsetLevelÚDEBUGÚinput_modelÚinput_model_pathÚoutput_modelÚoutput_model_pathÚpathÚexistsÚerrorÚ	ExceptionÚloadr   r   r   r   ÚquantrŒ   Úsave_model_to_file© r   r   Ú<module>rÂ      s  ðó Û Û 	ã Ý Û ß GÓ Gå ?å !Ý +à	×	Ò	˜8Ó	$€÷^(ñ ^(òB$ðN ˆzÔÙ‹<€DØ‡|‡|€|Ø�‰˜Ÿ™Ô&à×'Ñ'ÐØ×)Ñ)Ðà	‡w�w‡~�~Ð'×(Ò(Ø�‰�uÐ.Ð/¨Ð?Ô@Ù˜%Ð 1Ð2°/ÐBÓCÐCà�IŠIÐ&Ó'€EÙ  t§¡¸¿¹ÐZ^×ZoÑZoÔp€EØ	‡M�M„OØ	‡K�K×"Ñ"Ð#4°dÖ;ñ r   