Repository navigation
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
Closed
jasontitus wants to merge 2 commits into
jasontitus wants to merge 2 commits into
Conversation
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]>
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.
Owner
|
Thanks @jasontitus! This landed in 0.3.5 as 9475735, authored by you, and ships in https://github.com/ashhart/TensorFold/releases/tag/v0.3.5. |
This was referenced Oct 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
What
lane_qmm._MAIN_LOWBIT, a 3- and 2-bit lane matmul (MLX affine, groups of 64):tile_weight/untile_weighttake a requiredbits=.weight_bits,supports,install,warmanduninstallhandle the width.takes(module)decides which layersinstallroutes to the lane matmul.supports()remains the per-call check.lane_fusestacks only groups whose members share a width.lane_qmm.uncovered: other widths, unquantized). Before, a mixed checkpoint ran those on MLX's kernels silently.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.What the table shows:
Kernel cost: one 17408x5120 projection in ms, lane kernel at the tile width it is installed with, then MLX's kernel.
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.
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_footprintis not.Coding accuracy of the MLX checkpoints (EvalPlus, greedy pass@1, thinking off, generated through this change's lanes):
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
"draft": false: identical in 10 of 10 cases (5 prompts × T 0/1) on each of the four checkpoints, on this commit.lane_qmmon 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.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
test_lane_qmm.py) at 4, 3 and 2 bits:test_lane_fuse.py) at 4, 3 and 2 bits: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
depths.MTL_SHADER_VALIDATIONcharges 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_TAILand_COOPalready are.kernel_versionchanges, so snapshots stored by an earlier version are not reused.phys_footprintmay 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