Skip to content

Flash Next on M5: TF_FLASHNEXT_M5_ROWS=1 decodes on the pre-M5 path, 16% faster at one stream - #3227

Closed
ashhart wants to merge 5 commits into
mainfrom
lead/m5-pre-decode
Closed

ashhart wants to merge 5 commits into
mainfrom
lead/m5-pre-decode

Conversation

@ashhart

@ashhart ashhart commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

On a Mac with tensor units (M5), Flash Next's decode skips every choice the M1-M4 decode makes: the row kernels for dense projections, grouped experts, the cost depth rule and gated lanes at tau 0.4. On an M5 Ultra that left one stream at about the speed of an M3 Ultra. TF_FLASHNEXT_M5_ROWS=1 turns all four on for an M5. It is opt-in because four streams get slower.

  • TF_FLASHNEXT_M5_ROWS is a .metal flag in platform/environment.zig (exactly 0 or 1; any other value is refused at startup), read once at load into Run.m5_rows. rows_load.preM5Decode(run) decides it: true on every GPU without tensor units, and on one with them when the flag is 1. rows_load.on, the grouped-experts check and the gate default in engine_load.zig, and the cost depth rule in engine_generate.zig read it in place of !tensorUnits().
  • Prompts stay on the tensor units. A blanket switch can't work, because the M1-M4 prompt kernels refuse to load on an M5 (nax.h's fragment layout is wrong).
  • The lane template (lane_qmm_bytes_grouped's -lanes variant) is unchanged. On an M5 it accepts no drafts at all, so it stays off.
  • Unset, nothing changes: on the M5 Ultra all 48 replies below (greedy, sampled and "draft": false) are byte-identical to main before this change.

Measured on an M5 Ultra (512 GB), Qwen3.8-Flash-Next MLX 4-bit MTP at 2b170fa6, one server per arm, base/new/new/base, on an otherwise idle machine:

One stream, 16 prompts (tools/bench_prompts.json), 512 tokens Default TF_FLASHNEXT_M5_ROWS=1
Greedy, median tok/s 200.7 235.4 (+16.0% mean rate, 0 of 16 prompts slower)
Sampled (T 1.0, seed 1234), median tok/s 179.7 226.7 (+16.8%)
"draft": false, median tok/s 108.4 133.1 (+22%)
  • Drafted equals "draft": false on 16 of 16 prompts in both modes, and repeated arms give the same tokens 16 of 16 times.
  • At --parallel 4 with distinct prompts staggered 150 ms apart (bench_concurrent.py --alone), every concurrent reply equals its solo run in both modes. Sampled: +23.6% at one stream, +3.1% at two, −6.4% at four. Greedy on the copy-heavy quicksort prompt: −3.8% / −5.6% / −6.4% at one, two and four streams. The row kernels' cost grows a row at a time, while a tensor-unit op costs the same up to 16 rows. So narrow rounds favour rows, and wide rounds (several streams, long copy windows) favour the tensor units. Switching per round would break drafted against "draft": false, because the two kernels add in different orders. That is why this is opt-in.
  • Tokens differ from the tensor-unit decode on 14 of 16 prompts, from that summation order. The R1 quality cell (rt/QUALITY.md, chat mode) passes: 39 of 40 exact answers on both, the same miss, the same 64K needle result on both (raw-completion needle, a one-token stop with the same hash on both). Greedy agreement is 16 of 64, reported only.
  • Wired memory 128.5 → 129.0 GiB.
  • zig build test-native passes on the M5 Ultra.

Docs: a TF_FLASHNEXT_M5_ROWS row in docs/environment.md (and TF_FLASHNEXT_GROUPED_ROWS now says where it applies), a known-issues line on the token difference, and a changelog line.

Next: a tensor-unit decode kernel that adds in the row kernels' order. Narrow rounds could then take rows and wide ones the tensor units with the same bits, and M5 would be faster by default at every width.

  • Synthena (Ash's Agent)

…re-M5 path

On GPUs with tensor units, Flash Next's decode skipped every M1-M4 decode
choice: the row kernels for dense projections, grouped experts, the cost
depth rule and gated lanes at tau 0.4. TF_FLASHNEXT_M5_ROWS=1 turns all four
on for an M5 through rows_load.preM5Decode; prompts stay on the tensor units
(their nax fragments are what an M5 needs) and the lane template is
unchanged. Unset, an M5 decodes as before.

On an M5 Ultra (512 GB), Qwen3.8-Flash-Next MLX 4-bit MTP, 16 prompts of
512 tokens, one stream, base/new/new/base:
- greedy median 200.7 -> 235.4 tok/s (+16.0% mean rate, 0 of 16 slower)
- sampled median 179.7 -> 226.7 tok/s (+16.8%)
- "draft": false 108.4 -> 133.1 tok/s (+22%)
- drafted == "draft": false on 16 of 16 in both modes; repeats 16 of 16
- unset: all 48 replies byte-identical to main before this change

At --parallel 4 with staggered distinct prompts, every concurrent reply
equals its solo run, but 4 streams are up to 6% slower (the row kernels'
cost grows a row at a time where a tensor block is flat to 16 rows), so it
stays opt-in. Tokens differ from the tensor-unit decode's (different
summation order); the R1 quality cell (chat mode) passes, 39 of 40 exact
answers on both, the same 64K needle result on both.
…ig, one-line comments

The lean check refused rows_load.zig's direct getenv of TF_FLASHNEXT_M5_ROWS
and its multi-line comments. The setting is now a .metal flag in
environment.zig's table (0 or 1; any other value is refused at startup with
its name and forms), read once in finishLoad into Run.m5_rows beside
sb_index. rows_load.on and preM5Decode take the run instead of the device.
environment_test covers the new row; docs/environment.md says exactly 0 or 1.

On an M5 Ultra the replies are unchanged: default and TF_FLASHNEXT_M5_ROWS=1
each match the previous build on 32 of 32 replies (16 prompts, greedy and
sampled), and TF_FLASHNEXT_M5_ROWS=2 is refused. lean: 0 new. zig build
native, test-native and test-environment-host pass; zig build -Dfixed-cpu
test fails only the dashboard test that also fails on main on that Mac.
@ashhart

ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner Author

Closing. TF_FLASHNEXT_M5_ROWS=1 changes output tokens: 14 of 16 prompts differ from the tensor-unit decode, from the different summation order. TensorFold takes no mode that changes output tokens, even opt-in (docs/pr/SKILL.md). The one-stream gain is worth having, so an exact version is welcome: the pre-M5 row path on M5 would need to give the same tokens as the default decode on every prompt.

  • Synthena

@ashhart ashhart closed this Nov 17, 2026
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