NF4 grouped GEMM for MoE experts
A Triton kernel that multiplies tokens by the experts of a mixture-of-experts layer, with the expert weights stored in bitsandbytes' NF4 format. All experts run in one launch, as torch._grouped_mm does for bf16.
Why
There is no grouped NF4 kernel for MoE experts. bf16 grouped GEMM is native and heavily optimized, and NVFP4 is native on Blackwell, but on A100 and H100 a 4-bit MoE has nothing in between. With load_in_4bit, transformers leaves fused expert weights in bf16, and looping over experts with bitsandbytes is slow. This kernel stores experts at ~4.13 bits per weight (4Γ less memory than bf16) and runs them in a single grouped launch.
Usage
from kernels import get_kernel
nf4 = get_kernel("LegumMagister/nf4-grouped-gemm")
w = nf4.NF4Weights(experts) # [E, N, K] bf16 -> NF4 (bitsandbytes' quantize_4bit, double-quantized)
out = nf4.nf4_grouped_mm(x, offsets, w) # x sorted by expert; offsets = cumulative rows per expert
out = nf4.nf4_moe_mm(x, offsets, w) # same, with autograd and the fastest path per batch size
dense = nf4.nf4_dequantize(w) # [E, N, K] bf16, all experts in one launch
nf4_moe_mm is the one to use in a model. Up to 4 rows per expert (decode) it runs the grouped NF4 kernel; above that it dequantizes all experts to bf16 in one launch and runs torch._grouped_mm, which is faster there (see below). Its backward gives the input gradient through the same dequantization (frozen NF4 weights, as in QLoRA).
out[rows of expert e] = x[rows of e] @ W[e].T. The weights are bitsandbytes' own bytes, so w.dequantize() equals bitsandbytes.functional.dequantize_4bit exactly.
Benchmarks
One NVIDIA H100 80GB HBM3, Qwen3-30B-A3B expert shapes (128 experts, top-8), milliseconds per projection. "unpack + grouped" is nf4_dequantize followed by bf16 torch._grouped_mm: same NF4 storage, and the strongest baseline at larger batches.
| projection | tokens | bf16 grouped | bnb, one expert at a time | NF4 grouped kernel | unpack + grouped | nf4_moe_mm | nf4_moe_mm fwd+bwd |
|---|---|---|---|---|---|---|---|
| gate_up 128 Γ 1536 Γ 2048 | 1 | 0.033 | 0.357 | 0.062 | 0.414 | 0.061 | 0.483 |
| 8 | 0.116 | 2.251 | 0.263 | 0.502 | 0.263 | 0.766 | |
| 64 | 0.238 | 4.458 | 0.586 | 0.625 | 0.582 | 1.214 | |
| 512 | 0.300 | 5.660 | 1.111 | 0.682 | 0.691 | 1.404 | |
| 4096 | 0.532 | 5.968 | 2.020 | 0.916 | 0.943 | 2.014 | |
| down 128 Γ 2048 Γ 768 | 1 | 0.022 | 0.384 | 0.044 | 0.217 | 0.045 | 0.271 |
| 8 | 0.076 | 2.056 | 0.140 | 0.268 | 0.141 | 0.410 | |
| 64 | 0.141 | 4.541 | 0.303 | 0.333 | 0.303 | 0.637 | |
| 512 | 0.165 | 5.650 | 0.578 | 0.362 | 0.360 | 0.729 | |
| 4096 | 0.297 | 5.817 | 1.046 | 0.489 | 0.491 | 1.025 |
- Decode (up to ~4 rows per expert): the grouped kernel wins, 2β7Γ over unpack + grouped, because it reads only the 4-bit weights of the experts that have tokens.
- Prefill and training (β₯ ~32 rows per expert): unpack + grouped wins, up to 2.1Γ over the kernel. bf16 grouped GEMM is fast enough there that writing the bf16 weights once costs less than unpacking them again for every row tile. The kernel reads weights at 110β330 GB/s against the H100's 3.35 TB/s; a Marlin/FLUTE-style version (asynchronous copies, offline tensor-core layout) would be needed to win this range.
- Against running bitsandbytes NF4 expert by expert,
nf4_moe_mmis 6β16Γ faster at every size.
In training: Beam-1.0-501B-A23B QLoRA
nf4_dequantize + bf16 grouped GEMM (the nf4_moe_mm path at training sizes) on 4Γ H100, expert parallel (64 experts per GPU, ~512 rows per expert), LoRA r16 on attention and dense MLPs, gradient checkpointing, 4k tokens per GPU. bitsandbytes baseline: dequantize_4bit per expert + bf16 grouped GEMM.
| bitsandbytes | this format + one-launch dequantize (+ a fused Triton SwiGLU) | |
|---|---|---|
| MoE expert block, forward + recompute + backward, one layer | 36.3 ms | 23.0 ms (1.58Γ) |
| Full training step, 52 layers | 4.23 s | 3.73 s (1.13Γ) |
| Throughput | 3,870 tok/s | 4,390 tok/s |
| Peak memory per GPU | 73.3 GiB | 74.8 GiB (bf16 per-block scales) |
Losses match step by step. The one-launch dequantize alone gives 1.28Γ on the expert block; the fused SwiGLU (not part of this repository) takes it to 1.58Γ. That run stored the per-block scales in bf16 to save memory; NF4Weights keeps them in fp32 (bit-exact with bitsandbytes).
Correctness
tests/test_and_benchmark.py compares against bitsandbytes' dequantize_4bit followed by a bf16 matmul, for uneven and empty experts, N and K not multiples of the tile, bf16 and fp16 inputs, with every case run twice. The worst relative error is 3.5e-3 (bf16 rounding), and the dequantized values (in the kernel and from nf4_dequantize) are bit-identical to bitsandbytes'. The input gradient of nf4_moe_mm matches a dense reference on both paths.
How it works
- Weights: bitsandbytes NF4. A 4-bit index into 16 normal-distribution quantiles, an 8-bit absmax per 64 values, and an FP32 scale per 256 absmax. The per-block scales are precomputed once in FP32 (exactly as bitsandbytes computes them).
- Decode (few rows per expert): one program per expert, N tile and K slice (split-K), with fp32 atomic accumulation; no tile map.
- Prefill: tiles of 16β128 rows of one expert, found in-kernel by a binary search over per-expert tile counts; no host synchronization.
- In both, each 64-wide K tile is unpacked and looked up in registers and fed to bf16 tensor cores (
tl.dot). nf4_dequantize: one Triton launch over the whole packed stack, 4096 bytes per program, same arithmetic as the kernel.