Views
No views yet
| Operation | Description |
|---|---|
quantize_4bit | Blockwise 4-bit quantization (NF4/FP4) with per-block absmax |
dequantize_4bit | Blockwise 4-bit dequantization using codebook lookup |
gemv_4bit | Fused dequantize + matrix-vector multiply (batch_size=1 inference) |
gemm_4bit | Fused dequantize + matrix-matrix multiply (larger batch inference) |
linear_4bit | Auto-selecting linear layer (GEMV for vectors, GEMM for matrices) |
absmax (float32) per block of blocksize elementsvalue = codebook[4bit_index] * absmax1import torch
2from bitsandbytes_mps import quantize_4bit, dequantize_4bit, gemv_4bit, gemm_4bit, NF4
3
4# Quantize a weight matrix
5weight = torch.randn(4096, 4096, dtype=torch.float16, device="mps")
6packed, absmax = quantize_4bit(weight.flatten(), blocksize=64, quant_type=NF4)
7
8# Dequantize
9weight_deq = dequantize_4bit(packed, absmax, blocksize=64, quant_type=NF4,
10 numel=weight.numel(), output_dtype=torch.float16)
11
12# Fused GEMV (single vector)
13x = torch.randn(4096, dtype=torch.float16, device="mps")
14packed_w = packed.view(4096, -1) # [N, K/2]
15absmax_w = absmax.view(4096, -1) # [N, K_groups]
16y = gemv_4bit(x, packed_w, absmax_w, output_features=4096, blocksize=64, quant_type=NF4)
17
18# Fused GEMM (batch of vectors)
19X = torch.randn(8, 4096, dtype=torch.float16, device="mps")
20Y = gemm_4bit(X, packed_w, absmax_w, output_features=4096, blocksize=64, quant_type=NF4)scale * q + bias with codebook[q] * absmaxBnBQuantizedBlockLoader: Custom block loader for tiled GEMM that dequantizes on-the-fly using codebook lookup1pip install kernel-builder
2kernel-builder build .