Repository navigation
Flash Next on Metal: TF_FLASHNEXT_SCALE_INDEX, the 4-bit expert kernels read scales and biases through a 16-bit index - #2760
Merged
Merged
Conversation
ashhart
added a commit
that referenced
this pull request
Nov 10, 2026
ashhart
marked this pull request as ready for review
November 10, 2026 15:19
ashhart
added a commit
that referenced
this pull request
Nov 10, 2026
ashhart
force-pushed
the
lead/sb-index
branch
from
November 10, 2026 15:31
79c446a to
6b21951
Compare
This was referenced Nov 10, 2026
8 tasks done
ashhart
force-pushed
the
lead/sb-index
branch
from
November 10, 2026 20:26
a0ee780 to
f90752b
Compare
ashhart
added a commit
that referenced
this pull request
Nov 10, 2026
…read scales and biases through a 16-bit index In an MLX 4-bit group-32 checkpoint a group's bf16 scale and bias are a fifth of its bytes, and a tensor holds few distinct pairs. With the setting on (off by default; GPUs without tensor units), the loader indexes each expert projection's pairs and the recorded 4/32 expert kernels read each group's pair through one 16-bit index into the tensor's table: the same two values into the same sums, from 10% fewer weight bytes. The kernels register with the kernel contract as a bit-preserving replacement of the direct reads, with a planted control and their launch records. Refs #2751
The key converted the buffer's id with @intFromPtr, which the recording Metal boundary of the host tests does not allow: its id is an integer. The key now holds the id as the Metal module declares it. Refs #2751
ashhart
force-pushed
the
lead/sb-index
branch
from
November 10, 2026 21:32
f90752b to
ab07fd3
Compare
ashhart
added a commit
that referenced
this pull request
Nov 10, 2026
jschmied
added a commit
to jschmied/TensorFold
that referenced
this pull request
Nov 10, 2026
, ashhart#2464, ashhart#2375, ashhart#2634) and adds row 29, DSpine (ashhart#2633) Rebased onto main after ashhart#2760's row 28 (Scale index), which stays as on main. DSpine is row 29 with ashhart#2633's final GB10 ranges: agent 2-20 % faster, chat and code 10-19 % slower.
8 tasks done
ashhart
pushed a commit
that referenced
this pull request
Nov 11, 2026
ashhart
added a commit
that referenced
this pull request
Nov 11, 2026
Scope and figures corrected to the PRs (#2700, #2670, #2338, #2491, #2680, #2557, #2597, #2760's load time, #2753's verify-row wait, #2681's ledger row), #2831's and #2682's lines rewritten to what the code does, #2709 given its own line above #2609's, the duplicate #2404, #2576, #2597 and #2699 entries and the copy of #2609's line under 1.2.6 removed, entries spaced one blank line apart, and the released sections restored to the v1.2.6 tag's text (#1651's 1.2.2 line and six blank lines in 1.2.5 and older).
This was referenced Nov 13, 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 this changes
Flash Next on Metal gains
TF_FLASHNEXT_SCALE_INDEX(0 or 1, default 0). Before: the 4-bit expert kernels read each group's bf16 scale and bias from the checkpoint's own arrays, a fifth of every weight read. After, with the setting on and no tensor units: the loader indexes each expert projection's (scale, bias) pairs, and the recorded 4/32 gate/up and down kernels read each group's pair through one 16-bit index into the tensor's table. The kernels get the same two values into the same sums from 10% fewer weight bytes, so every reply keeps its tokens. Off, the engine is unchanged: no kernel text, buffer or launch differs.atc overlap --json: run after the rebase onto8a7838a65a: the snapshot reported itself incomplete, with 9 pull requests clean, the rest untested and none conflicting. An earlier snapshot named a few conflicting pull requests; merging each of their heads with main and with this branch showed they already conflict with main and none conflicts with this branch alone. Not rerun after the last rebase onto14dea6c6ad, which was for a changelog conflict.Checks on this head
8a7a0b0859is the change;6b2195127fadds the changelog line and the ledger row;a0ee780740is a four-line fix: the index keyed a buffer with@intFromPtron its id, which the host tests' recording Metal boundary does not allow, so CI'szig build testfailed to compile on Linux and Windows. The key now holds the id in the Metal module's own type; the served path is unchanged). The measurements below were taken on the same change before two rebases (87a2d83b57, then9f4fb8962c); between those bases and this one nothing landed under Flash Next's Metal sources,platform/environment.zigor the Metal contract.python3 tools/zig/lean_check.py, on the base and on this head: 386 problems on the base and 386 on this head with the same checker, 0 new (lean_new.py);tools/pr_check.py --base origin/main --pr 2760: 0 errors, 0 warnings.zig build test-flashnext-host -Dcpu=apple_m1 --summary all: 232 passed, 0 failed, 1 skipped. With the two new tests' expectations broken on purpose it reports 230 passed and 2 failed (experts4's index patch test andscale_pairs' index test), so the step reaches both.zig build test-environment-host: 13 passed, 0 failed, 0 skipped.zig build native -Dcpu=apple_m1 -Doptimize=ReleaseFastandzig build dev-tools -Dcpu=apple_m1: build.zig fmt --check build.zig zigandgit diff --check: clean.zig build tf-kernel-contract -Dcpu=apple_m1, thentf-kernel-contract --only "Flash Next q4 expert"and--only "flashnext index"on the M3 Ultra: lines under Kernel contract.test-kernel-contract, HIP host tests, dev tools, the Linux CUDA cross-compile, release paths): every step exit 0.6b2195127fwas red inzigandzig-windowswith that one compile error (scale_index.zig:12), which I had not caught because the steps I ran locally do not compile Flash Next against the recording boundary. Fixed ina0ee780740; on that headzig build test-flashnext-fsr-owner-host test-flashnext-rotary-bound-host test-flashnext-bound-probes-host(the recording boundary) gives 48 passed, 0 failed, andtest-flashnext-host232 passed, 0 failed, 1 skipped. A new CI run started on the push; I will read it.zig build test test-goldenas a whole: on macOS it runs Metal device checks, which share the GPU with the timing cells, so I ran the steps above and leave the rest to CI. The fullkernel-contractrun stops before its gates on this Mac (UnrecordedRotaryCompiler: it has no rotary accuracy receipt for this compiler, as on main), so the SUMMARY lines below are from--onlyruns of the kernels and launches this change adds; they are not the full qualification receipt. No M1, M2, M4 or M5 device.Receipt
TF_FLASHNEXT_SCALE_INDEX=0against=1(off is main's path: the patch, the index build and the buffer swap all sit behind the setting). Arms alternate off, on, on, off after one discarded warm-up arm, a fresh server an arm.One stream (
--parallel auto), tok/s; four prompts of 512 tokens for prose, two for code and hard, one 768-token edit of a 1.6k-token file (copy), prose atmax_tokens4096, and 8.7k- and 17.5k-token prompts (long). Sampled cells are T 1.0, top_k 20, top_p 0.95 with a seed a prompt. Measured on87a2d83b57."draft": false"draft": false"draft": false"draft": false"draft": false"draft": false"draft": false"draft": falseA second, shorter run of the prose cell alone (off / on / on / off): 134.8, 135.0 off against 138.3, 139.0 on (+2.8%);
"draft": false98.7 against 100.4 (+1.7%). The first prototype's run of the same cell gave +3.2%. The landing queue was building on the machine's CPU during every run, and the off arms move more with that load than the on arms.Shared rounds (
--parallel 4,tools/bench_concurrent.py --tokens 256 --reps 3 --levels 1,4 --alone --serial --prompts tools/bench_prompts.json), aggregate tok/s, every arm listed:One on arm of the greedy four-stream cell ran low on all three of its repeats (164.6, 174.7, 173.8) while that server's other cells were level with the other on arms; five further arms did not repeat it. I list it rather than drop it.
The engine's own timing of shared forwards at load, 1 / 2 / 4 / 8 / 16 rows: 11.06 / 13.01 / 17.77 / 28.22 / 48.61 and 11.10 / 13.38 / 17.93 / 28.47 / 49.03 ms off; 10.70 / 12.83 / 17.20 / 27.36 / 47.54 and 10.53 / 12.72 / 17.32 / 27.38 / 47.48 ms on.
--ssd-expertsor speed-up mode, and nothing on CUDA, HIP or XPU.TF_FLASHNEXT_SCALE_INDEX, default 0, accepts exactly0or1, read throughplatform/environment.zig(rule,environment_test.zigcases, a row indocs/environment.md, a paragraph in the Flash Next recipe). No profile row:docs/profiles.mdsays the registry does not hold every setting yet, and none of Flash Next's Metal settings is in it. It is not part of a persisted prompt state's key: prompt passes do not read the index, and the values the decode kernels read are the stored ones.Qwen3.8-Flash-Next-MLX-4bit-MTPrevision2b170fa6;--context 32768 --temperature 0 --no-thinking --prompt-cache-gib 0.--parallel 4, the MTP head's experts, greedy and sampled, on an M3 Ultra. Not run: M1, M2 and M4 (no such machine here; the launch records pass the contract's M1 to M5 limits). M5 is unreached by design.tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false: every drafted reply in every cell above equals its"draft": falsereply (token_sha256), greedy and sampled, and every hash is equal between the off and on arms, prompt by prompt. At--parallel 4,bench_concurrent.py --alone --serialreports alone equal 12 and 3 with 0 unequal and serial equal 4 with 0 unequal in every cell of every arm, greedy and sampled.tools/bench_openai.py:tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0,1.0against a fresh server an arm built from9f4fb8962c, off / on / on / off after a discarded warm-up arm; decode tok/s, each arm its median of three:fibonacci-rawfibonacci-rawgpu-chat-no-thinkgpu-chat-no-thinkCosts with the setting on, as built: load 23.1 to 25.8 s off against 26.6 to 30.4 s on (the index is hashed on one thread), and 129.6 GiB in use once loaded off against 136.8 to 136.9 GiB on (the index and tables, held beside the stored scales and biases that prompt passes still read). Both go away, and the memory turns into a saving, when the prompt gathers read the index too (#2751 step 2).
Kernel contract, only when a GPU kernel is new or changed
tests/contract/metal_flashnext_index.zig): the recorded gate/up and down kernels as the loader patches them before tensor units (4/32 constants, half nibbles, the balanced down, the gate/up's row count) with the index patch, replacing the same kernels without it on the same stored bytes, routes and rows, non-finite scales included. Launch records:flashnext index recordedgate/up and down incore/contract/scale_index_records.zig.SUMMARYline and exit status: exit 0 for both runs;SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 275 ms, limits 23 ms;SUMMARY 0 kernels: 0 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 0 ms, limits 348 msChecklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.--drafter, no slower than without it, greedy and sampled