Skip to content

feat: lane kernels for 3- and 2-bit weights (exact drafting on smaller Qwen3.8-27B checkpoints, 3.4x on 3.50 bpw) - #34

Closed
jasontitus wants to merge 2 commits into
ashhart:mainfrom
jasontitus:lanes-lowbit-v2
Closed

jasontitus wants to merge 2 commits into
ashhart:mainfrom
jasontitus:lanes-lowbit-v2

Conversation

@jasontitus

@jasontitus jasontitus commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

What: TensorFold's M5 lane kernels, the fast path with exact DFlash2 drafting, accept only 4-bit weights. So the smaller Qwen3.8-27B conversions, the ones that fit a 24 GB Mac, run with no drafts or with inexact drafts. This PR adds 3- and 2-bit weights to the lane matmul. On an M5 Max, a 3.50 bpw checkpoint goes from 30.5 to 103.5 tok/s (3.4x) at 16.2 GiB, against 19.4 GiB for 4-bit. Mixed-width checkpoints get 1.6-1.9x, and 4-bit is unchanged (byte-identical output). Drafted output stays byte-identical to serial decoding on every checkpoint.

Stacked on #33 (the kernel-signature fix): the first commit here is that PR's; this PR's own change is the last commit. Rebased onto 0.3.4.1: the full suite passes there; the speed figures below were measured on 0.3.4, and a re-check on 0.3.4.1 will follow as a comment.

Why

With the lane kernels and the DFlash2 drafter, Qwen3.8-27B at 4 bits uses 19.4 GiB steady and 21.0 GiB peak (phys_footprint, 16k context, prompt cache and MLX buffer cache off). That is more than a 24 GB Mac can spare.

On 0.3.4, smaller MLX conversions run their 3- and 2-bit layers on MLX's kernels:

  • A whole 3-bit checkpoint gets neither the lane kernels nor the row decoder, so the drafter is not loaded and it decodes one token a round (about 30 tok/s).
  • A mixed checkpoint with a 4-bit top level keeps lanes for its 4-bit layers only. Its drafted rows are checked at width rather than bit-identical, and its rounds take 100-128 ms.

What

  • lane_qmm._MAIN_LOWBIT, a 3- and 2-bit lane matmul (MLX affine, groups of 64):
    • The tensor op reads 4-bit operands but not 3- or 2-bit ones. So each simdgroup widens its 32 columns' 64-input group to nibbles in threadgroup memory, then runs the 4-bit kernel's op and per-group arithmetic.
    • The widening is exact, and a row's bits still depend only on the weight's shape.
    • The 4-bit kernels' sources are unchanged.
  • Width-aware helpers:
    • tile_weight/untile_weight take a required bits=.
    • weight_bits, supports, install, warm and uninstall handle the width.
    • takes(module) decides which layers install routes to the lane matmul. supports() remains the per-call check.
    • lane_fuse stacks only groups whose members share a width.
  • Loading:
    • The family enables lanes for checkpoints whose top-level quantization is 4, 3 or 2 bits in groups of 64.
    • At load it names the layers the lanes do not take (lane_qmm.uncovered: other widths, unquantized). Before, a mixed checkpoint ran those on MLX's kernels silently.
  • Docs: a new recipe section, 3-bit weights, covering speed, the kernel's cost, the accuracy of the available conversions, and limits.

Performance vs main (0.3.4)

Setup: M5 Max 128 GB, one server at a time, launches interleaved with 0.3.4's, 512-token replies, thinking off, prompt cache and MLX buffer cache off, 16k context. Tok/s is the median over 3 launches per arm (4-bit: 4) of each launch's median; tokens and ms a round are the code-T0 cell's; memory is the median launch's steady / peak phys_footprint. 0.3.4 in parentheses.

Checkpoint (size) Code T1 Code T0 Prose T1 Prose T0 Tokens / round ms / round Steady / peak GiB
Vontra 4-bit (16.1 GB) 139.4 (139.5) 143.2 (145.1) 74.8 (75.5) 72.2 (72.7) 7.53 (7.53) 54.2 (53.5) 19.4 / 21.0 (19.4 / 21.0)
AtomicChat 3.50 bpw DWQ, 3-bit (12.7 GB) 106.7 (30.7) 103.5 (30.5) 67.4 (27.4) 63.8 (26.5) 6.48 (1.12) 64.4 (37.1) 16.2 / 17.8 (11.8 / 12.0)
AtomicChat 3.70 bpw DWQ, 3/4-bit (13.4 GB) 113.2 (49.7) 123.3 (64.2) 69.6 (31.0) 69.9 (31.5) 7.21 (8.13) 60.4 (128.4) 16.9 / 18.5 (14.9 / 16.5)
rapid-mlx mixed 3.5 bpw, 2/3/4-bit (14.8 GB) 125.9 (72.6) 120.3 (77.1) 79.0 (46.4) 65.2 (38.4) 6.83 (7.64) 58.7 (100.7) 17.4 / 19.0 (16.4 / 18.2)

What the table shows:

  • 4-bit: replies are byte-identical to 0.3.4 in every launch, and the speed is within noise (means over the 4 launches: -0.7% to +0.4%).
  • Memory: on the 3.50 bpw checkpoint 0.3.4 loads no drafter at all. With lanes, each layer they take keeps a second, interleaved copy of its scales and biases (1/8 of 4-bit weight bytes, 1/6 of 3-bit, 1/4 of 2-bit), and the stacked projections add their own.
  • Tokens a round on the mixed checkpoints: 0.3.4's replies differ from the branch's (its 3- and 2-bit layers ran MLX's arithmetic), so tokens a round compare different texts; the gain is in ms a round.

Kernel cost: one 17408x5120 projection in ms, lane kernel at the tile width it is installed with, then MLX's kernel.

Rows 1 4 16 33
4-bit, 64-column tiles 0.088 / 0.059 0.090 / 0.141 0.103 / 0.311 0.228 / 0.301
4-bit, 32-column tiles 0.090 0.099 0.113 0.281
3-bit, 32-column tiles 0.121 / 0.051 0.124 / 0.177 0.123 / 0.304 0.330 / 0.295
2-bit, 32-column tiles 0.104 / 0.044 0.107 / 0.149 0.107 / 0.293 0.324 / 0.281

The 3- and 2-bit kernel is slower than the 4-bit one: at the same 32-column tiles 3-bit is 9-34% slower (widening), and 4-bit tiles 64 columns wide (at 32 columns 4-bit itself costs 1-24% more). So a 3-bit round costs more than a 4-bit one (64 vs 54 ms, code T0). What the change buys is exact drafting on smaller checkpoints.

Against other engines on the same Mac

Measured on this change before its rebase onto 0.3.4 (lane kernels unchanged since). Same prompts and replies (512 tokens, thinking off), each server alone on the GPU, 2 launches per configuration. Decode tok/s is measured on the client from each streamed reply's first token to its last, the same way for every engine.

Engine Checkpoint Code T1 Code T0 Prose T1 Prose T0
TensorFold, this change 4-bit 151.1 151.6 78.7 75.8
mlx-serve 26.9.2 + DFlash2 drafter 4-bit 73.3 80.2 41.0 40.7
TensorFold, this change 3.50 bpw DWQ 118.6 116.0 74.5 70.5
mlx-serve + DFlash2 3.50 bpw DWQ 59.2 53.4 33.2 31.1
TensorFold, this change 3.70 bpw DWQ 117.2 127.0 72.8 74.0
mlx-serve + DFlash2 3.70 bpw DWQ 54.0 64.3 35.0 33.0
TensorFold, this change rapid-mlx 3.5 bpw 128.3 123.3 82.6 68.5
mlx-serve + DFlash2 rapid-mlx 3.5 bpw 56.2 58.6 35.2 33.7
llama.cpp b10964 Unsloth UD-Q3_K_XL 25.4 24.9 23.6 23.1
llama.cpp + DFlash2 GGUF drafter Unsloth UD-Q3_K_XL 21.8 28.2 13.0 14.6
llama.cpp Unsloth UD-IQ3_XXS 26.4 26.2 25.4 24.8
llama.cpp + DFlash2 Unsloth UD-IQ3_XXS 22.9 24.2 12.4 14.1
llama.cpp Unsloth UD-Q2_K_XL 27.4 27.4 26.5 25.8
llama.cpp + DFlash2 Unsloth UD-Q2_K_XL 21.0 24.2 13.8 15.6

With the same drafter, TensorFold runs these 3-bit-class checkpoints about 2x faster than mlx-serve, and about 4-5x faster than llama.cpp on GGUF quants of similar size. In llama.cpp the DFlash2 drafter did not pay: 46-64% of drafted tokens were accepted, but a multi-row verify there costs about five one-row steps. Memory is comparable across engines, but llama.cpp maps its weights from the file, so its phys_footprint is not.

Coding accuracy of the MLX checkpoints (EvalPlus, greedy pass@1, thinking off, generated through this change's lanes):

Checkpoint HumanEval HumanEval+ MBPP MBPP+
4-bit 93.3% 90.2% 89.2% 76.2%
3.70 bpw DWQ 91.5% 88.4% 85.7% 72.2%
3.50 bpw DWQ 90.9% 86.6% 87.3% 73.3%

On MBPP+ (378 problems), both 3-bit-class checkpoints lose 3-4 points to 4-bit, and a paired per-problem test puts that loss beyond chance (p = 0.02 and 0.04). Their KL divergence ranks them the same way.

SWE-bench Verified Mini (50 tasks; mini-swe-agent, thinking off, Qwen's non-thinking sampling, 100 steps): 4-bit 10/50, 3.70 bpw 12/50 (both solved 8 of the same tasks; p = 0.69). With thinking off both loop on 36 of 50 tasks until the step limit, so this measures the agent loop more than the weights; it shows no measurable loss.

Exactness

  • Drafted vs "draft": false: identical in 10 of 10 cases (5 prompts × T 0/1) on each of the four checkpoints, on this commit.
  • First request vs second on a fresh server: equal on every launch.
  • Nemotron (uses lane_qmm on M5 since 0.3.4): loads with its MTP head, and its greedy replies are byte-identical to 0.3.4's.

Accuracy

Context for choosing a checkpoint: mlx-lm's kernels, scored like llama.cpp's llama-perplexity, against the 8-bit MLX conversion lukaskremla/Qwen3.8-27B-8bit-MLX-TextOnly.

Checkpoint KLD
4-bit (Vontra) 0.045
rapid-mlx mixed 3.5 bpw 0.122
3.70 bpw DWQ 0.133
3.50 bpw DWQ 0.168
uniform 3-bit (lukaskremla/Qwen3.8-27B-3bit-MLX-TextOnly) 0.191
uniform 2-bit ~1.5 (earlier scoring pass; not re-run)

Unsloth's GGUF quants on the same tokens and reference: UD-Q3_K_XL (13.1 GB) 0.024, UD-IQ3_XXS (10.9 GB) 0.051, UD-Q2_K_XL (9.8 GB) 0.085.

Tests

  • Full suite: 475 passed, 1 skipped on 0.3.4.1.
  • Lane matmul (test_lane_qmm.py) at 4, 3 and 2 bits:
    • Rows don't depend on the row count.
    • Accuracy vs MLX at the model's shapes.
    • Every packed value lands where MLX's dequantize puts it, including 48-column untiled weights.
    • Repeated calls give the same bits.
    • Tiled equals untiled.
    • Other widths are refused and reported.
  • Lane decoder (test_lane_fuse.py) at 4, 3 and 2 bits:
    • Fused rounds equal unfused rounds.
    • A 16-row chain equals 16 one-row steps under the production install.
    • Mixed-width groups stay separate.
    • Models of different widths run in one process.
    • One kernel signature per kernel.
  • test_qwen3_5_load.py: the load gate, the note on uncovered layers, and what loads without the lane kernels.
  • test_nemotron_draft_head.py: Nemotron's draft head takes token rows from a lane-tiled head at its width.

Not included / known

  • Stacked on the kernel-signature fix (separate PR). Its padding replaces this branch's earlier fix, which padded only the attention placeholders and depths.
  • Tuning. 3- and 2-bit weights use 32-column tiles only, and the tree sizes and fused row ranges are 4-bit's.
  • The gate reads the checkpoint's top-level width (4, 3 or 2 bits, groups of 64), as main did for 4-bit:
    • layers of other widths run MLX's kernels and are named at load (a tied embedding head is not named);
    • quality is the checkpoint's: uniform 2-bit measured KLD ~1.5.
  • Memory: the lanes' interleaved copy of scales and biases costs 1/8 of 4-bit weight bytes, 1/6 of 3-bit and 1/4 of 2-bit.
  • Metal validation mode. MTL_SHADER_VALIDATION charges threadgroup memory twice. Under it the SK = 8 low-bit variant is rejected (22.5 KB real, within the 32 KB limit), as main's _TAIL and _COOP already are.
  • Prefix snapshots. kernel_version changes, so snapshots stored by an earlier version are not reused.
  • M5-generation GPUs only: on M1 to M4 the row decoder reads 4-bit weights only, so 3- and 2-bit checkpoints serve there without drafts (and say so at load).
  • Not measured on a 24 GB machine or on an M5 Pro. The 3.50 bpw checkpoint's 16.2 / 17.8 GiB phys_footprint may exceed a 24 GB Mac's default GPU wired limit (sysctl iogpu.wired_limit_mb). The widening's cost may also differ on a smaller GPU.

🤖 Generated with Claude Code

jasontitus and others added 2 commits September 27, 2026 07:36
mx.fast.metal_kernel declares an input `constant` below 8 elements and
`device` from 8 up, but MLX 0.31.2 (the pyproject floor, and the version
the README tests on M5) caches the compiled kernel by name and template
only. A call with the other signature clears the library and recompiles;
with both signatures in one graph, dispatches already encoded lose their
pipeline and never run (NaN logits). Fixed upstream in ml-explore/mlx#3662
(v0.32.0).

The window-sized inputs of both Qwen3.8 dense decoders now keep at least 8
elements, padded at the end (kernels/metal_inputs.device_ints): the lane
decoder's tree parents, attention depths and placeholders, replay rows and
gdn_pre windows; and the row decoder's tree parents, gdn_pre windows and
replay rows (shared lane_tree code), and row_attention's depth and path.
No kernel reads the padding; bits are unchanged. On 0.3.0 (M5 Max), one
serving launch recompiled lane kernels 585 times; with this change, 0.

Tests: tests/kernel_signatures.py records each call's signature; the lane
decoder (install, warm-ups, rounds of several widths), the row decoder
with both matmul backends, and row_attention must keep one signature per
kernel. All five fail without this change.

Not covered: Qwen3.8 Flash Next's kernels and engine/gpu_sampling (inputs
sized by rows), and mlx_lm's masked gated_delta kernel in multi-stream
rounds (built inside mlx_lm).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The lane matmul took 4-bit weights only, so smaller MLX conversions of
Qwen3.8-27B ran their 3- and 2-bit layers on MLX's kernels: a whole 3-bit
checkpoint had no lanes and no DFlash2 drafts, and a mixed one checked
drafted rows at width rather than bit-identical.

_MAIN_LOWBIT widens each 64-input group from MLX's 3- or 2-bit packing to
nibbles in threadgroup memory and runs the 4-bit kernel's tensor op and
per-group arithmetic: exact, and a row's bits depend only on the weight's
shape. tile_weight/untile_weight take a required bits= (Nemotron's draft
head untiles at its head's width); install, warm, supports and lane_fuse
handle the width (stacks need one width); takes() decides which layers the
lanes run. On an M5 the family enables lanes for 4-, 3- and 2-bit
checkpoints and names the layers they do not take; without tensor units a
3- or 2-bit checkpoint serves without drafts (the row decoder is 4-bit
only) and says so.

M5 Max, code prompt at T 0, 0.3.4 -> this change: 3.50 bpw DWQ 30.5 ->
103.5 tok/s, 3.70 bpw DWQ 64.2 -> 123.3, rapid-mlx mixed 3.5 bpw 77.1 ->
120.3; 4-bit unchanged and byte-identical. Docs: recipe section "3-bit
weights".

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ashhart

ashhart commented Sep 27, 2026

Copy link
Copy Markdown
Owner

Thanks @jasontitus for the 3- and 2-bit lane kernels. They're in final testing for 0.3.5, which ships tonight, as a commit authored by you, extended to 5-, 6- and 8-bit weights and mixed-precision checkpoints. This PR closes when the release is published.

ashhart pushed a commit that referenced this pull request Sep 27, 2026
The lane matmul took 4-bit weights only, so smaller MLX conversions of
Qwen3.8-27B had no lanes and no DFlash2 drafts. _MAIN_LOWBIT widens each
64-input group from MLX's 3- or 2-bit packing to nibbles in threadgroup
memory and runs the 4-bit kernel's tensor op and per-group arithmetic:
exact, and a row's bits depend only on the weight's shape.
tile_weight/untile_weight take a required bits= (Nemotron's draft head and
Flash Next's decode pass theirs); install, warm, supports and lane_fuse
handle the width (a stack needs one width); takes() and uncovered() say
which layers the lanes run. The 4-bit kernel sources are unchanged, so
4-bit output is byte-identical.

From jasontitus's pull request #34. Adapted by the maintainer to
release-0.3.5: the kernel joins the release's constants-in-source kernels
(_Baked) beside its group-of-32 4-bit path, and the new K and N checks
apply to the new widths only. The family follows the release's rule of
refusing what the lane decoders cannot read: on an M5 it loads 4-, 3- and
2-bit checkpoints and refuses one with any projection the lanes don't
take (the pull request served those on MLX's kernels, checked at width),
and without tensor units a 3- or 2-bit checkpoint is refused rather than
served without drafts. The load tests are rewritten for the family's
load(); the other tests and the recipe section follow the pull request.
@ashhart

ashhart commented Sep 27, 2026

Copy link
Copy Markdown
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants