o
    °ƒðj@#  ã                   @   s(  d dl Z d dlZd dlZd dlZd dlmZ d dlZd dl	m
Z
mZmZmZ d dlmZ ddlmZ ddlmZ e e¡ZG dd„ dƒZd	d
„ Zedkr’eƒ ZejrXe ej¡ ejZejZ ej! "e ¡rue #de › d�¡ e$de › d�ƒ‚e %e¡Z&ee&ej'ej(ej)d�Z*e* +¡  e*j& ,e d¡ dS dS )é    N)Ú
GraphProtoÚ
ModelProtoÚ	NodeProtoÚTensorProto)Úquantize_matmul_bnb4é   )Ú	ONNXModel)Úattribute_to_kwargc                   @   sœ   e Zd ZdZdZdZddededefdd	„Ze	d
e
e deeef fdd„ƒZdejdejfdd„Zdede
e defdd„Zde
e fdd„Zdd„ ZdS )ÚMatMulBnb4QuantizerzMPerform 4b quantization of constant MatMul weights using FP4 or NF4 data typer   r   NÚmodelÚ
quant_typeÚ
block_sizec                 C   s@   |pg }|t jt jfv sJ ‚t|ƒ| _|| _|| _t|ƒ| _d S ©N)	r
   ÚFP4ÚNF4r   r   r   r   ÚsetÚnodes_to_exclude)Úselfr   r   r   r   © r   ú°/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/onnxruntime/quantization/matmul_bnb4_quantizer.pyÚ__init__%   s   
zMatMulBnb4Quantizer.__init__Ú
graph_pathÚreturnc                 C   sL   t t|ƒd ddƒD ]}|| }|jD ]}|j| kr"||f    S qq
dS )Nr   éÿÿÿÿ)NN)ÚrangeÚlenÚinitializerÚname)r   r   ÚgidÚgraphÚtensorr   r   r   Ú__get_initializer-   s   

ÿÿz%MatMulBnb4Quantizer.__get_initializerÚfpweightc                 C   s’   t |jƒdkrtdƒ‚| ¡  ¡ }|j\}}|| }| j}|| d | }|d d }tj|dd�}	tj||jd�}
t	|	||
|| j
||ƒ |	|
fS )z4b quantize fp32/fp16 weighté   z9Current bnb4 block quantization only supports 2D tensors!r   Úuint8)Údtype)r   ÚshapeÚ
ValueErrorÚ	transposeÚcopyr   ÚnpÚzerosr%   r   r   )r   r"   Ú
fpweight_tÚrowsÚcolsÚnumelr   Ú
num_blocksÚquantized_numelÚpackedÚabsmaxr   r   r   Úbnb4_block_quant6   s   
z$MatMulBnb4Quantizer.bnb4_block_quantÚnodeÚgraph_stackc                 C   s¢  |j dkr|S t d|j› d�¡ |j| jv r#t d|j› d�¡ |S |jd }t ||¡\}}|du r;t d¡ |S tj	 
|¡}t|jƒd	krOt d
¡ |S |  |¡\}}tj	 |¡}	|jd |	_|jD ]}
|
j|krt|j |
¡  nqetj	 |¡}|jd |_|j |	|g¡ i }|j\}}||d< ||d< | j|d< | j|d< tjj	d|jd |	j|jg|jd g|jr½|jd ndddœ|¤Ž}t d|j› d�¡ |S )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...r   Nz2MatMul doesn't have const weight. Skip to quantizer#   z)MatMul weight is not 2D. Skip to quantizeÚ_Bnb4Ú_absmaxÚKÚNr   r   Ú
MatMulBnb4r   Ú úcom.microsoft)ÚinputsÚoutputsr   Údomainzcomplete quantization of )r<   )Úop_typeÚloggerÚdebugr   r   Úinputr
   Ú%_MatMulBnb4Quantizer__get_initializerÚonnxÚnumpy_helperÚto_arrayr   r&   r4   Ú
from_arrayÚremover   Úextendr   r   ÚhelperÚ	make_nodeÚoutput)r   r5   r6   ÚinputBÚBÚBs_graphÚB_arrayr2   r3   ÚB_quantrE   Úabsmax_tensorÚkwargsr-   r.   Úmatmul_bnb4_noder   r   r   Ú_bnb4_matmul_node_weightL   sX   





þ


ÿ
ûú	z,MatMulBnb4Quantizer._bnb4_matmul_node_weightc                 C   s  g }|d }|j D ]q}dd„ |jD ƒ}|rqi }|jD ]D}|jtjjkr2| |j¡ |j|  	|¡i}n'|jtjj
krUg }	|jD ]}
| |
¡ |	 |  	|¡g¡ q>|j|	i}nt|ƒ}| |¡ qtjj|j|j|jfd|ji|¤Ž}| |  ||¡¡ q	| d¡ |j  |¡ | ¡  |S )Nr   c                 S   s,   g | ]}|j tjjks|j tjjkr|‘qS r   )ÚtyperG   ÚAttributeProtoÚGRAPHÚGRAPHS)Ú.0Úattrr   r   r   Ú
<listcomp>ˆ   s
    þz9MatMulBnb4Quantizer._process_subgraph.<locals>.<listcomp>r   r5   )r5   Ú	attributerY   rG   rZ   r[   ÚappendÚgr   Ú_process_subgraphr\   ÚgraphsrL   r	   ÚupdaterM   rN   rB   rE   rO   rX   Ú
ClearFieldÚpop)r   r6   Ú	new_nodesr   r5   Úgraph_attrsrV   r^   ÚkvÚvalueÚsubgraphr   r   r   rc   ƒ   s@   
þ


ÿÿÿ
z%MatMulBnb4Quantizer._process_subgraphc                 C   sd   | j  ¡ g}| j  ¡ }d}|D ]	}|jdkrd}q|s&| tj dd¡g¡ |  |¡ | j  	¡  d S )NFr>   Tr   )
r   r   Úopset_importrA   rL   rG   rM   Úmake_opsetidrc   Úclean_initializers)r   r6   rm   Úhas_ms_domainÚopsetr   r   r   Úprocess©   s   

€
zMatMulBnb4Quantizer.processr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úintr   ÚstaticmethodÚlistr   Útupler   rF   ÚnptÚ	ArrayLiker*   Úndarrayr4   r   rX   rc   rr   r   r   r   r   r
      s     7&r
   c                  C   s–   t jdd�} | jdddd� | jdddd� | jd	d
dtjtjgdd� | jdd
ddd� | jddd
dd� | jd
d� | jddtd
g dd� |  ¡ S )Na  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_typeFr   z&Quantization data type. 0: FP4, 1: NF4)r   ÚdefaultÚchoicesr€   z--block_sizeé@   zVBlock 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)ÚnargsrY   r   r�   r€   )	ÚargparseÚArgumentParserÚadd_argumentr
   r   r   Úset_defaultsÚstrÚ
parse_args)Úparserr   r   r   rŽ   ¹   s:   ÿ	
ûüú	rŽ   Ú__main__zfile z already exists)r   T)-r‰   ÚloggingÚosÚnumpyr*   Únumpy.typingÚtypingr{   rG   Úonnx.onnx_pbr   r   r   r   Úonnxruntime.capi._pybind_stater   Ú
onnx_modelr   Úquant_utilsr	   Ú	getLoggerrs   rC   r
   rŽ   Úargsr†   ÚsetLevelÚDEBUGÚinput_modelÚinput_model_pathÚoutput_modelÚoutput_model_pathÚpathÚexistsÚerrorÚ	ExceptionÚloadr   r   r   r   Úquantrr   Úsave_model_to_filer   r   r   r   Ú<module>   s8   
 "'
ñ