Skip to content

flash next pair: hand off through shared events and host-published epochs - #3480

Draft
jcscocca wants to merge 12 commits into
ashhart:mainfrom
jcscocca:flashnext-pair-main
Draft

jcscocca wants to merge 12 commits into
ashhart:mainfrom
jcscocca:flashnext-pair-main

Conversation

@jcscocca

@jcscocca jcscocca commented Nov 15, 2026 •

Copy link
Copy Markdown

What this changes

On main, the Flash Next pair's kernels wait for the peer by polling a window flag inside a running command buffer, and on two M5 Ultra Studios the pair fails warm-up with PeerSilent. With this change each producer ends its encoder and signals a Metal shared event, a host relay sends the payload and waits for the peer's word, then publishes a full-width 64-bit epoch that the consumer kernels check before reading. In pair mode the model's residency set is still requested but is no longer attached to the command queue.

This is a draft. It fixes the slow replies reported in #3408; the pair is still slower than one Mac. The slow replies were macOS unwiring the model during a paired request: the residency set is not attached to the engine queue and the idle probe stops at the request gate, so nothing used the set while a request ran. b4ee01293 commits the existing busy probe through the set on a second command queue every 400 ms while the request gate is held. With it, 49 requests on the pair have no reply under 10 tok/s (11 before), at a drafted median of 51.1 tok/s and a plain median of 26.3. One Mac does 126.4 and 99.6 on the same requests.

Commits:

  1. 3684408f5 fabric mcdma: bound payload admission and peer flushes by a call deadline
  2. 70f722e62 metal residency: request a set independently of queue attachment
  3. 43cf092de metal tests: record shared events and encoder boundaries on the host
  4. 5ea5efa4c flash next tp: release peer consumers through shared-event handoffs
  5. 9ffbe507f metal contract: register Flash Next pair epoch guards and relay controls
  6. 14e0f191c flash next pair: bound startup and drain request work before releasing owners
  7. 4aff995b1 flash next pair: keep requested residency off the command queue
  8. b0c25d9da flash next tp: complete local submission without touching the peer
  9. ba93fe229 flash next tp: report release gaps and relay phases for each reply
  10. 71454c079 native comments: keep touched source within the lean comment limits
  11. 5d0b9bfe1 flash next tests: skip relay probes without the native host clock
  12. b4ee01293 flash next pair: keep the residency set in use while a paired request runs

Commits 8 and 9 exist for the measurements asked for in #3408. TF_TP_LOCAL now also completes the per-command-buffer submit barrier locally, so nothing is sent per command buffer. TF_TP_STATS prints, per reply and per exchange kind, the host-held time, the release-to-next-post gap with a log2 histogram, the send time and the wait for the peer's word. With both switches unset the service loop takes one branch into a path with no timing reads.

How this meets the terms in #3408:

  • Arithmetic. The pair kernels' non-comment source is byte-identical to the version the issue's numbers came from. The sums, their order and the rounding points are unchanged: partials are added rank 0 then rank 1 and rounded once.
  • File sizes. 50 files touched, the largest 595 lines (zig/src/native/flashnext_host.zig). tp.zig goes from 592 to 505. The handoff, relay, deadline, submit barrier and statistics are in tp_handoff.zig, tp_service.zig, tp_deadline.zig, tp_submit.zig and tp_stats.zig.
  • Switches. None added. TF_TP_TRACE, TF_TP_LOCAL, TF_TP_STATS and FZ_BUSY are read exactly as on main.
  • Tests that fail on main. See below.

Checks on this head

  • Head commit: b4ee0129375a01a4ec2dcb7dc922accbf8ef1b53. The three entries that follow are at this head; the rest of this section was run at 5d0b9bfe15, the previous head.
  • At b4ee01293, on an M4 Pro: zig fmt --check build.zig zig and git diff --check pass; python3 tools/pr_check.py --base 811c38473e --pr 3480 reports lean 321 against 341, 0 new, 0 errors and one advisory warning (no Unreleased bullet names this pull request); zig build test-flashnext-pair-host test-flashnext-cleanup-host test-metal-residency-owners-host test-flashnext-host native tf-kernel-contract -Dcpu=apple_m1 -j4 --summary all passes 83/83 steps. zig build test test-golden -Dcpu=apple_m1 -j4 --summary all: 868/870 steps, 13,854 passed, 112 skipped, 2 failed; the two failures are the same two as on 811c38473e, and test-golden passes.
  • New host tests for b4ee01293 (recording Metal, no GPU): a step under a held request gate commits one probe on the second queue under the owner's lock with no flush and nothing on the engine queue; with no request a step commits nothing; stopping joins the thread; the paired host starts the probe and the one-Mac host makes no second queue; the gate reports active only between begin and end. Four of the five fail with the production change neutralised.
  • Receipt for b4ee01293: 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 (comment), with the runs attached: resumed prompts (43 of 49 resumed, all 49 equal to the cache-off replies), concurrent against solo (8 of 8 equal, cache off and on), both ranks equal throughout, and one Mac on the branch (126.4 drafted, 99.6 plain).
  • Lean checker, python3 tools/zig/lean_check.py: 341 findings on 811c38473e, 321 on this head, 0 new (tools/zig/lean_new.py). python3 tools/pr_check.py --base 811c38473e: 0 errors, 1 advisory warning (no --pr number yet for the Unreleased line).
  • Tests run on an M4 Pro, macOS 26.7.1, Zig 0.17.0:
    • zig fmt --check build.zig zig and git diff --check: pass.
    • zig build native tf-kernel-contract -Dcpu=apple_m1 -j4 --summary all: 69/69 steps.
    • zig build test-flashnext-pair-host test-flashnext-cleanup-host test-flashnext-host test-flashnext-batch-host test-mcdma-host test-benchmark-group-host test-metal-residency-owners-host test-kernel-contract test-metal-launch-map-host test-tp test-tp-suite-host test-tp-fate-host test-cluster test-verbs -Dcpu=apple_m1 -j4 --summary all: 132/132 steps, 1044 passed, 15 skipped, 0 failed.
    • zig build test test-golden -Dcpu=apple_m1 -j4 --summary all on this head: 868/870 steps, 13,758 passed, 106 skipped, 2 failed. On 811c38473e: 868/870 steps, 14,117 passed, 113 skipped, 2 failed. The two failures are the same on both: families.glm.weights_fix_test "GLM copies and transforms join before shard eviction" and core.paced_read "aligned spans copy the bytes asked for". The head's lower pass count is five test steps that were already cached from the command above and print no count. test-golden passes on both.
  • New tests against main: 49 new or changed named host tests pass on this head. Compiled against a pristine export of 811c38473e with only tests and test fixtures overlaid, 3 fail by assertion ("pair kernel source never polls host-written memory", "every pair kernel checks a full-width epoch before reading payload", and the cleanup test "the pair owns borrowed aliases until all queue work is drained, then closes its endpoint"), 45 cannot compile because main has no host-observable handoff to drive, and 1 comment-only MCDMA test passes on both. The late-peer cases are "relay late peer word releases once with the complete job epoch" and "relay stale peer word does not release the next job" in zig/flashnext_relay_tests.zig; they drive the real service loop and handoff with an epoch above 2^32.
  • CI: not started when this was opened.
  • Not run, and why: atc overlap --json; tools/bench_concurrent.py, tools/bench_openai.py and tools/bench_cache.py (the Studio runs used a sequential driver that records token IDs from both ranks instead); the full Metal numerical suite (only the filtered pair selection ran); concurrent requests; a resumed prompt against a fresh one (all Studio runs had the prompt cache off).

Receipt

  • Main against branch: on the pair, main does not start, so the comparison is one Mac on main against the pair on this branch, same 49 requests, prompt cache off, --no-thinking, temperature 0.

    Build Drafts Replies Median tok/s Min Max Under 10 tok/s
    One Mac, main 811c38473e on 25 126.0 115.0 212.1 0
    One Mac, main 811c38473e off 23 100.8 97.0 101.5 0
    Pair, this branch on 25 24.5 2.6 135.8 9
    Pair, this branch off 23 24.1 3.7 31.0 2

    Replies of at least 32 tokens. The pair is slower than one Mac at the median in both modes, and 11 of its 48 replies ran under 10 tok/s. On main the pair fails warm-up on both ranks with PeerSilent (rank 0 at sequence 2, rank 1 at sequence 3), gave_up 0 in every trace line.

  • Boxes it reaches: native Metal, Flash Next, --speed-up over one Thunderbolt 5 link. CUDA, HIP and XPU are outside it.

  • New switch? None.

  • Environment: TensorFold 811c38473e and 5d0b9bfe15, Zig 0.17.0, built on an M4 Pro with Xcode 27.2 and Metal Toolchain 27B47. Two M5 Ultra Mac Studios, 256 GiB each, macOS 27.2 build 26B118. Checkpoint TensorFold/Qwen3.8-Flash-Next-MLX-6bit-MTP revision 45032a193a5d4126ba8534822cbaf8fb30b3e368. Both ranks load the same MCDMA library, SHA-256 ae63dd35624573a647d8fd23b0a29c436a5d3d3de97d40c29d880ce179ac7679.

  • Paths and platforms the change reaches, which I ran and which I could not: ran the pair path on the two Studios and the one-Mac path's host tests. Not run: one Mac on this branch on a Studio (the one-Mac receipts are main), so "single-Mac attachment unchanged" rests on host tests; any chip other than M5 Ultra and M4 Pro.

  • Exactness: on the pair at this head, 18 replies in three groups (two 61-token prompts three times each and a long prompt twice, each drafted and with "draft": false): token IDs identical within every group, and rank 1's reply hashes equal rank 0's for all 18. In the 49-request run, all 24 groups of identical bodies have identical token IDs across draft modes and both ranks agree on all 49; the same holds with FZ_BUSY=1 TF_TP_STATS=1. In a 150-request run with --no-drafts, both ranks agree on all 150. A pair reply can differ from one Mac's, as docs/speed-up-mode.md says: for example the 61-token prose prompt hashes 29e668310177 on the pair and 3fd180517008 on one Mac.

  • Main against this branch, same machine and checkpoint: the table above.

  • Decode speed before and after: the table above; the pair has no "before" because main's pair does not start.

  • Prompt speed before and after: not measured with tools/bench_cache.py.

  • Licence of any new weights or drafter: n/a.

Kernel contract
  • Kernels registered with the contract: the seven pair kernels tp_argmax_part, tp_pick, tp_ids, tp_copy, tp_plain, tp_branch, tp_merge, mapped to their production shader, plus a GPU loopback of the handoff with a reused receive slot. They replace the polled versions of the same kernels.
  • A planted control for each: a low-word control per epoch probe, in which a kernel that compares only the low 32 bits consumes a stale payload. Each is red as planted.
  • SUMMARY on an M5 Ultra, tf-kernel-contract --only 'metal flashnext tp', exit 0: 8 kernels: 8 pass gates 1-4, 0 fail, 0 known findings; 7 controls: 7 red as planted, 0 missed; 0 limit problems.
  • Chips I could not run: M1 to M4 on a device. The limits gate checks the seven launches against M1 to M5 offline.

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. Not met: the pair is slower than one Mac. See the receipt and 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.

…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.

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