Skip to content

Flash Next on Metal: TF_FLASHNEXT_SCALE_INDEX, the 4-bit expert kernels read scales and biases through a 16-bit index - #2760

Merged
ashhart merged 3 commits into
mainfrom
lead/sb-index
Nov 10, 2026
Merged

ashhart merged 3 commits into
mainfrom
lead/sb-index

Conversation

@ashhart

@ashhart ashhart commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

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.

  • Issue: Refs Flash Next on Metal: scales and biases through a 16-bit index, 10% fewer weight bytes a pass #2751 (step 1 of its plan; steps 2 and 3 remain: the prompt gathers on the index with the originals freed and the default on, then the dense rows at one and two rows).
  • Stacked on: main.
  • atc overlap --json: run after the rebase onto 8a7838a65a: 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 onto 14dea6c6ad, which was for a changelog conflict.

Checks on this head

  • Head commit: a0ee780 on main 14dea6c (8a7a0b0859 is the change; 6b2195127f adds the changelog line and the ledger row; a0ee780740 is a four-line fix: the index keyed a buffer with @intFromPtr on its id, which the host tests' recording Metal boundary does not allow, so CI's zig build test failed 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, then 9f4fb8962c); between those bases and this one nothing landed under Flash Next's Metal sources, platform/environment.zig or the Metal contract.
  • Lean checker, 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.
  • Tests run, with the command and pass, fail and skip counts:
    • 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 and scale_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=ReleaseFast and zig build dev-tools -Dcpu=apple_m1: build.
    • zig fmt --check build.zig zig and git diff --check: clean.
    • zig build tf-kernel-contract -Dcpu=apple_m1, then tf-kernel-contract --only "Flash Next q4 expert" and --only "flashnext index" on the M3 Ultra: lines under Kernel contract.
    • The lead's local landing gate script on this head (scan, gitleaks, whitespace, lean, fmt, the tools' unit tests, test-kernel-contract, HIP host tests, dev tools, the Linux CUDA cross-compile, release paths): every step exit 0.
  • CI: the run on 6b2195127f was red in zig and zig-windows with 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 in a0ee780740; on that head zig build test-flashnext-fsr-owner-host test-flashnext-rotary-bound-host test-flashnext-bound-probes-host (the recording boundary) gives 48 passed, 0 failed, and test-flashnext-host 232 passed, 0 failed, 1 skipped. A new CI run started on the push; I will read it.
  • Not run, and why: zig build test test-golden as 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 full kernel-contract run 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 --only runs of the kernels and launches this change adds; they are not the full qualification receipt. No M1, M2, M4 or M5 device.

Receipt

  • Main against branch: cells and numbers, or why they can't be run: the tables below. One binary built from this head served every arm, with TF_FLASHNEXT_SCALE_INDEX=0 against =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 at max_tokens 4096, 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 on 87a2d83b57.

Cell Off On Change Off arms On arms
Code, drafted 192.6 197.0 +2.3% 194.5, 190.7 198.3, 195.7
Copy-heavy edit, drafted 262.8 273.2 +3.9% 262.2, 263.4 272.9, 273.5
Hard prompts, drafted 160.4 163.4 +1.9% 162.2, 158.5 164.2, 162.6
Prose, drafted 137.3 139.1 +1.3% 138.6, 136.0 139.8, 138.4
Long contexts, drafted 170.9 175.0 +2.4% 171.9, 169.9 174.7, 175.3
Prose at max_tokens 4096, drafted 135.3 137.3 +1.5% 135.5, 135.0 136.7, 137.9
Sampled code, drafted 187.7 190.7 +1.6% 189.5, 185.8 190.9, 190.6
Sampled prose, drafted 131.1 133.7 +2.0% 132.7, 129.5 134.7, 132.7
Code, "draft": false 100.0 101.4 +1.4% 101.3, 98.8 102.7, 100.1
Copy-heavy edit, "draft": false 97.2 98.4 +1.3% 98.4, 96.0 99.4, 97.5
Hard prompts, "draft": false 100.3 102.0 +1.7% 101.6, 99.0 102.9, 101.1
Prose, "draft": false 100.3 101.2 +0.8% 101.4, 99.3 101.9, 100.5
Long contexts, "draft": false 95.3 96.7 +1.5% 95.9, 94.7 96.6, 96.8
Prose at max_tokens 4096, "draft": false 97.6 99.7 +2.2% 97.1, 98.1 99.5, 99.9
Sampled code, "draft": false 100.0 101.1 +1.1% 101.0, 99.0 101.6, 100.5
Sampled prose, "draft": false 99.9 101.7 +1.8% 101.2, 98.7 102.7, 100.8

A 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": false 98.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:

Cell Off arms On arms Off mean On mean Change
Greedy, 1 stream 176.9, 174.3, 175.7, 176.2, 177.1 179.3, 183.7, 181.0, 180.5, 180.2 176.0 180.9 +2.8%
Greedy, 4 streams 196.4, 195.1, 196.7, 190.6, 197.7 173.8, 205.5, 204.5, 202.9, 202.8 195.3 197.9 +1.3% (medians 196.4 and 202.9, +3.3%)
Sampled, 1 stream 166.1, 166.4 172.4, 173.0 166.2 172.7 +3.9%
Sampled, 4 streams 185.3, 188.0 198.3, 191.8 186.7 195.1 +4.5%

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.

  • Boxes it reaches: Macs without tensor units (M1 through M4) serving an MLX 4-bit, group-32 Flash Next checkpoint, and only with the setting on. It does nothing with tensor units (M5), --ssd-experts or speed-up mode, and nothing on CUDA, HIP or XPU.
  • New switch? Default, accepted values, profile row: TF_FLASHNEXT_SCALE_INDEX, default 0, accepts exactly 0 or 1, read through platform/environment.zig (rule, environment_test.zig cases, a row in docs/environment.md, a paragraph in the Flash Next recipe). No profile row: docs/profiles.md says 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.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: head 6b21951; Zig 0.17.0; Xcode's Metal toolchain; Mac Studio, M3 Ultra (80-core GPU, no tensor units), 256 GB, macOS 27.0; Qwen3.8-Flash-Next-MLX-4bit-MTP revision 2b170fa6; --context 32768 --temperature 0 --no-thinking --prompt-cache-gib 0.
  • Paths and platforms the change reaches, which I ran and which I could not: the one-reply driver and shared rounds at --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.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: every drafted reply in every cell above equals its "draft": false reply (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 --serial reports alone equal 12 and 3 with 0 unequal and serial equal 4 with 0 unequal in every cell of every arm, greedy and sampled.
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): as above; the off arms are main's path on the same binary.
  • Decode speed before and after, from tools/bench_openai.py: tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0,1.0 against a fresh server an arm built from 9f4fb8962c, off / on / on / off after a discarded warm-up arm; decode tok/s, each arm its median of three:
Prompt Temperature Off arms On arms Off On Change
fibonacci-raw 0 176.4, 174.1 181.6, 181.9 175.3 181.8 +3.7%
fibonacci-raw 1 168.3, 166.9 172.5, 172.2 167.6 172.4 +2.9%
gpu-chat-no-think 0 155.1, 153.4 159.2, 159.2 154.3 159.2 +3.2%
gpu-chat-no-think 1 154.3, 153.1 158.6, 157.8 153.7 158.2 +2.9%
  • Prompt speed before and after: n/a. The change cannot reach prompt processing: prompt passes read the stored scales and biases through their own kernels, which this does not touch. The long-context cells above include their prompt time in neither column.
  • Licence of any new weights or drafter, or "n/a": n/a.

Costs 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
  • Kernels registered with the contract, and the kernel each one replaces: "Flash Next q4 expert scales and biases through the index" (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 recorded gate/up and down in core/contract/scale_index_records.zig.
  • A planted control for each new kernel, and each one fails as planted: "control: Flash Next q4 expert index, two table pairs swapped" is red at shapes, as planted.
  • Each backend's SUMMARY line 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 ms
  • Chips I could not run: M1, M2, M4 and M5 devices (the limits gate compiles the two launches against M1 to M5: 0 problems).

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled

@ashhart
ashhart marked this pull request as ready for review November 10, 2026 15:19
…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
ashhart merged commit b6e77ba into main Nov 10, 2026
2 of 10 checks passed
@ashhart
ashhart deleted the lead/sb-index branch November 10, 2026 21:33
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.
ashhart pushed a commit that referenced this pull request Nov 11, 2026
, #2634) and adds row 29, DSpine (#2633)

Rebased onto main after #2760's row 28 (Scale index), which stays as on main. DSpine is row 29 with #2633's
final GB10 ranges: agent 2-20 % faster, chat and code 10-19 % slower.
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).
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.

1 participant