Skip to content

flash next pair: decode whole on each Mac, split only prompt processing - #3688

Draft
jcscocca wants to merge 14 commits into
ashhart:mainfrom
jcscocca:flashnext-pair-decode
Draft

jcscocca wants to merge 14 commits into
ashhart:mainfrom
jcscocca:flashnext-pair-decode

Conversation

@jcscocca

Copy link
Copy Markdown

What this changes

On the pair, a decode round paid one host handoff per split stage: 48 expert exchanges, 36 DeltaNet reduces and the picks, about 0.4 ms each on two M5 Ultra Studios, where one Mac computes a whole layer in about 0.2 ms. With this change each Mac computes the whole decode round itself, using the owning rank's kernels and arguments for each half and the pair's order of sums, so the replies are the ones the pair gives today and a decode round queues no job and encodes no seam. Prompt processing keeps its split. The two Macs stay on the same step through an acknowledged agreement word.

Why the tokens do not change:

  • Experts and the MoE branch. The expert kernels and tp_branch index every read and write by absolute row. In decode each Mac runs them for every row, writes both ranks' branch rows where the owner's send slot would hold them, and runs the same tp_merge with the same split.
  • DeltaNet. Each Mac runs both ranks' in-projection tiles, both ranks' head launches (rank 1's patched pipeline for heads 24 to 47, rank 0's recorded one for 0 to 23) and both out-projection partials, then the same tp_plain: rank 0's partial plus rank 1's, rounded once. Each rank's step runs right after its own in-projection.
  • Head and draft picks. Both vocabulary halves, two tp_argmax_part launches and the same tp_pick rule; tp_ids as before.
  • Guards. Local launches bind a constant pass word in a pair-owned buffer. Every kernel that reads peer-delivered data still checks the published epoch.
  • State. Prompt processing already leaves both Macs with complete state, and the prompt cache keeps only states taken while reading the prompt, so nothing has to cross at the prompt-to-decode boundary.
  • Assumption. Each Mac now computes values the other Mac used to send it, so equality rests on both Macs giving the same bits for the same pipeline and inputs. The pair already relied on that for attention, the router and the streams. The runs below check it on two M5 Ultras.

Step agreement: rank 0 used to overwrite one control word per step; the per-layer handoffs kept the Macs within a round of each other. Now rank 1 writes the last step it took to a new STEP_ACK word and rank 0 waits, up to the existing 10 s limit, until that is at most one step behind before it publishes the next step.

No new switch. TF_TP_TRACE, TF_TP_LOCAL and TF_TP_STATS see no decode jobs.

Checks on this head

  • Head commit: 4dc9a23bc.
  • Lean checker: 341 on 811c38473e, 321 on this head, 0 new. python3 tools/pr_check.py --base 811c38473e: 0 errors, one advisory warning (no Unreleased bullet).
  • Tests run on an M4 Pro, macOS 26.7.1, Zig 0.17.0:
    • zig fmt --check build.zig zig, git diff --check: pass.
    • zig build test-flashnext-pair-host test-flashnext-host test-flashnext-cleanup-host test-flashnext-batch-host test-metal-residency-owners-host test-kernel-contract test-tp -Dcpu=apple_m1 -j4 --summary all: 104/104 steps, 674 passed, 3 skipped.
    • zig build native tf-kernel-contract -Dcpu=apple_m1 -j4 --summary all: 69/69 steps.
    • zig build test test-golden -Dcpu=apple_m1 -j4 --summary all: 868/870 steps; the one failing test is core.paced_read "aligned spans copy the bytes asked for", which also fails on 811c38473e; test-golden passes. Most steps were served from the build cache in this run, so the pass count is not quoted.
  • New host tests (recording Metal): a decode window encodes both ranks' launches for every DeltaNet layer, the expert stage for every row, both vocabulary halves and one local pick, with no job, encoder end, event signal or event wait, and each launch's pipeline, arguments and grid equal the owning rank's exchanged launch; outside decode the exchange shape is unchanged; a one-row window's experts run on rank 1 too; the round model sends nothing in decode; rank 0 does not publish a step until rank 1 acknowledged the one before, an expired wait fails with TpPeerSilent, and the control words stay distinct. They fail with the production change neutralised.
  • Not run, and why: atc overlap --json; the tools/bench_*.py scripts (the runs use the sequential driver from Flash Next --speed-up on two M5 Ultras (macOS 27.2): warm-up fails PeerSilent; with shared-event handoffs it serves, but drafted replies intermittently drop to 1 to 5 tok/s #3408's archives); the full Metal numerical suite (no kernel source changes); any chip other than M5 Ultra and M4 Pro.

Receipt

Two M5 Ultra Mac Studios, 256 GiB each, macOS 27.2 26B118, one Thunderbolt 5 link, checkpoint TensorFold/Qwen3.8-Flash-Next-MLX-6bit-MTP at 45032a193a5d4126ba8534822cbaf8fb30b3e368, --no-thinking, temperature 0, one request at a time. 49 requests, replies of at least 32 tokens:

Build Drafted median tok/s Min Plain median tok/s Min Request seconds, all 49
Pair, #3480 b4ee01293 51.1 37.8 26.3 21.0 282
Pair, 1c0ab7d57 81.8 41.4 45.1 31.9 208
Pair, 4dc9a23bc 115.3 105.3 87.1 84.4 137
One Mac, 5d0b9bfe15 126.4 114.9 99.6 96.9 206
  • Exactness against the pair before this change (b4ee01293): the same reply hash and length on 18 of 18 (the exactness set), 49 of 49 with the prompt cache off and 49 of 49 with it on. Drafted equals "draft": false in every group, rank 1's hashes equal rank 0's on every reply, and eight requests sent at once equal the same eight alone.
  • Where the pair now stands against one Mac: decode at about 90% (115 against 126 drafted, 87 against 100 plain). Prompt processing about twice as fast (2.36 s against 5.17 s to first token on 16,400-token prompts). The 24 long-prompt requests take 92 s against 166 s; the 25 short-prompt requests take 44.7 s against 40.3 s.
  • Soak: the 49 requests three times in a row on one start (147 requests, cache off): every reply has the same hash and length as on b4ee01293, both ranks agree on all 147, drafted median 115.2 tok/s (min 105.3), plain median 86.6 (min 83.9), and wired memory stays at 169 to 179 GiB on both Macs for all 415 seconds.
  • A Mac killed mid-reply (SIGKILL 6 s into a 3,000-token plain reply): with rank 1 killed, rank 0's request ends with TpPeerSilent 10 s later (HTTP 500) and the server stays up and stops cleanly; with rank 0 killed, rank 1 reports TpPeerSilent and exits 12 s later. No process or lock is left. On b4ee01293 the same kill is noticed in about 2 s (TpTransferTimeout), because every layer sent something; here only the step agreement crosses the link in decode, so a dead peer is noticed at the agreement's 10 s limit.
  • New switch: none. Licence of new weights: n/a.

Not done, and known gaps

  • Decode on the pair is still about 10% behind one Mac. I have not measured where that goes; the pair keeps its own kernel choices (no two-head attention, no grouped experts) and adds one agreement round trip per step.
  • A dead peer is noticed in 10 s during decode instead of about 2 s.
  • Each Mac now builds the other rank's pipelines at first use of a width; warm-up covers the widths its short reply reaches.
  • Resumed prompts and concurrency were run once each; the kill tests once per rank.

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. The bits do not change.
  • 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. Prompt processing is faster than one Mac; decode is about 10% slower.

…line

Take only the deadline-bounded endpoint calls and their tests from ashhart#3293.
The hunks for these two files are byte-identical to ashhart#3293 so either change can land first.
Keep explicit residency and probes while allowing callers to omit automatic queue attachment.
Cover attachment policy and cleanup through the recording owner.
Give handoff tests independent event values and ordered encoder operations.
Retain the dispatch type when recording a reopened compute encoder.
End each producer encoder, post its full-width job epoch, and wait for the host relay before reopening the consumer.
Publish valid epochs only after bounded send and arrival phases; terminal releases leave payload epochs invalid.
Cover late and stale peer words, failed or expired sends, terminal silence, encoder order and kernel source guards.
Map all seven pair kernels to their production shader and launch records.
Add stale-epoch mutation controls and a real GPU handoff loopback under the metal flashnext tp selection.
…g owners

Coordinate idle keepalive with request ownership, stop followers before callbacks disappear, and detect terminal failures in the final drain.
Bound reciprocal startup and idle peer checks without changing request or step agreement words.
Request and retain the model set in pair mode without attaching it to the queue.
Keep single-Mac attachment and test both native inventory paths and cleanup.
TF_TP_LOCAL marks newly encoded jobs submitted after the existing stop and failure checks.
Skip the endpoint and peer-readiness clock, and cover a broken endpoint with a full-width job sequence.
Keep host-held timing and add per-kind release-to-post histograms, send time and peer-arrival wait under TF_TP_STATS.
Serialize publication with reply snapshots, close partial jobs on failure, and reset at every reply including short replies.
Leave the disabled service path without timing clocks or collector locks and preserve GPU work.
Shorten inherited comments and remove measurement history without changing executable statements.
Keep the leading fabric prerequisite unchanged; its later comment-only cleanup is separate.
The real relay uses the macOS clock and thread service class.
Skip those host probes on other platforms while keeping source, encoding and pure timing checks available.
… runs

A paired request left the model unused by any queue: the set is not attached to the engine queue and idle probes
stop at the request gate, so macOS unwired the model mid-reply and expert launches stalled on re-wiring.
Commit the busy probe through the set on a second queue at the served keepalive interval while the gate is held.
…hange

In decode each Mac computes every row's experts and both ranks' branch rows with the exchange's kernels and arguments,
then merges them locally: the stream update reads the same values and no exchange job or seam is encoded.
Prompt processing, the DeltaNet reduce and the picks are unchanged.
…p by acknowledged agreement

In decode each Mac makes both ranks' DeltaNet launches, partials and vocabulary halves with the owning rank's kernels and
arguments, sums the partials in rank order and picks locally, so a decode round queues no job and encodes no seam.
Rank 0 waits for rank 1's acknowledgement of the previous step before it publishes the next.

This branch has not been deployed

No deployments
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