Skip to content

Inkling 2 Mini on Metal: the forward on a concurrent encoder (bit-identical; plain decode +21%, 4 drafted streams +47%) - #5435

Open
sethforprivacy wants to merge 3 commits into
ashhart:mainfrom
sethforprivacy:inkling2-mini-metal-concurrent
Open

sethforprivacy wants to merge 3 commits into
ashhart:mainfrom
sethforprivacy:inkling2-mini-metal-concurrent

Conversation

@sethforprivacy

Copy link
Copy Markdown
Contributor

The part of #4448 that main has not taken yet, cut fresh from main: the Mini forward on a concurrent encoder. No kernel and no arithmetic change; every reply is main's, bit for bit.

What changes

The forward encoded every launch into one serial encoder, so each waited for the one before it, though many read only what an earlier step wrote. It now runs on a concurrent encoder with a barrier after every step a later launch reads. Launches between two barriers run side by side:

  • a visit's six input projections (wq, wk, wv, wr, sink, gate),
  • its three norms,
  • the three roles' gates and depth-mix reads (one launch each at one row),
  • the K and V staging copies, and the K and V convolutions where they are separate launches.

Everything else stays a chain, with a barrier after each step.

Three helpers issued dependent launches back to back and relied on the serial encoder's order; they now put a barrier between them:

  • glue.routerSplit: the scores, then the selection,
  • loop.gatesSplit: the norm, the down projection, then the apply,
  • forward_conv.fusedAttention: the staging, then the dynamic convolutions.

On a serial encoder these barriers change nothing, so the other callers are unaffected.

routedExperts moves to forward_experts.zig unchanged apart from its barriers, so forward.zig stays within the 600-line rule (590 lines).

#4448's other proposals stay out: the five command buffers (the forward now hands its last buffer to the sampler and backs up the convolution state in a blit first, and the encoder alone is the measured part), and the 32-key/adaptive attention chunks, which would change the order of the merges.

Bits

All on a Mac Studio M5 Ultra with 256 GB and the released checkpoint, served by tensorfold-native on this branch against main at the same commit (6345c5bb8), --parallel 4 --context 16384 --prompt-cache-gib 0, greedy:

Check Plain --drafter
24 prompts, 256 tokens: this branch against main 0 of 24 differ 0 of 24 differ
The same 24, this branch run twice 0 of 24 differ 0 of 24 differ
Drafted against plain 0 of 24 differ
4 chat prompts: solo, again, 4 at once, draft: false all main's hashes all main's hashes
bench_concurrent.py --levels 4 --alone --serial 0 failed 0 failed

A missing barrier would show as a reply that changes between runs or under load; none did.

Speed

Same machine, same settings, the builds alternated:

main this branch
Plain, one stream, bench_openai.py (fibonacci-raw / gpu-chat-no-think) 36.8–37.0 / 36.9–37.1 tok/s 44.6–44.8 / 44.7–44.8 +21%
--drafter, one stream 67.7 / 53.4 69.4 / 55.2 +3%
--drafter, 4 chat prompts at once 96.2 total 118.1 total +23%

With the drafter at --parallel 8 --max-tokens 32768 --prompt-cache-gib 16 --decode-share 0.5, 800 tokens each:

main this branch
Code, one stream 64.0 tok/s 65.8
Prose, one stream 54.1 55.4
45K-token prompt 854 tok/s 902
4 streams, total 101.3 tok/s 148.5

The gain is largest where a step is one row's small launches (plain decode, several streams); a drafted round already has several rows a launch.

Checks on this head

  • zig build native -Dcpu=apple_m1 compiles.
  • tf-kernel-contract --only inkling2: 127 kernels: 126 pass gates 1-4, 1 fail; 152 controls: 152 red as planted, 0 missed. The one failure, tf_inkling2_conv_dynamic_segs ("a run changed its inputs"), fails the same way on main at 6345c5bb8; no kernel is changed here.
  • The six Inkling host steps with the real checkpoint: 257 of 265 pass, 7 skipped, 1 fails: Mini Metal opt-in DSpark drafting preserves target tokens and rolls back exactly at drafted.drafted > 0, which fails the same way on main at 6345c5bb8.
  • zig fmt --check, git diff --check clean; lean 93 on the base and 93 here, 0 new.

Main has moved on by nine commits since 6345c5bb8; none touches zig/src/families/inkling2/, and this branch merges onto current main cleanly.

@ashhart

ashhart commented Dec 1, 2026

Copy link
Copy Markdown
Owner

@sethforprivacy, the rebase onto main is pushed, at cbcec9882c.

  • Both commits are kept, with authorship and messages as they were: the concurrent encoder for every forward, with dependency barriers, and the routed expert ordering that completes the TFQ3 and TFQ5 paths and adds their pipeline barriers. Main's forward is still serial, so nothing here was already covered.
  • Since 27 Nov, main split the Inkling Metal forward into forward_attention.zig and forward_dispatch.zig, and added relative-bias reuse in attention.zig. The encoder change now spans those files too. Main's behaviour is kept wherever your change didn't mean to alter it.

Next we review this head and run it on an M5 against main: identical tokens on every row, with the drafter on and off, plus decode and prompt speed. Nothing is needed from you in the meantime.

  • Synthena

@ashhart

ashhart commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Thanks @sethforprivacy. #5435 is the forward on a concurrent encoder, independent launches side by side, followed by our own commit preserve routed expert ordering.

It doesn't merge onto today's main. The conflicts are in zig/src/families/inkling2/metal/attention.zig and forward.zig. Main now reports each kernel choice through TF_EXPLAIN (explain.reportProject, reportGate, reportRoles, reportExpert in forward.zig). When we rebase, reportExpert has to move with the routed experts into forward_experts.zig, and the others have to stay where the split files make those choices.

Exactness

  • The claim. The commit says no kernel and no arithmetic change, and that holds if every launch that reads what an earlier launch wrote has a barrier between them. At zig/src/families/inkling2/metal/forward.zig:377, every pass runs on the concurrent encoder: decode rounds, verify windows and prompt chunks.
  • A missed barrier already showed up. The TFQ5 and TFQ3 expert paths in forward_experts.zig:28 issued their dispatch and the combine with no barrier between them. Our follow-up commit adds those barriers (and removes a second copy of routedExperts the rebase left in forward.zig). That's exactly the kind of gap that's easy to miss by reading.
  • The ask. Please add a host test that records the forward's launches and fails when a launch reads a buffer an earlier launch in the same span wrote, with a planted control that drops one barrier. It should cover a decode round, a verify window, a prompt chunk and each expert format. The Qwen3.5 Metal schedule test in Qwen3.5-2B Metal: concurrent encoder, fused same-input launches, GPU greedy pick #5674 has this shape.
  • Your Inkling 2 Mini on Metal: routed experts on the M5 tensor units at every width (16 rows 49 to 36 ms, 64 rows 138 to 84 ms) #4523 branch does the same job differently. It applies the concurrent encoder only to prompt chunks and commits them in five command buffers; this PR applies it to every pass in one command buffer. The split into forward_attention.zig, forward_experts.zig and forward_dispatch.zig is the same in both, so only one can land as is. We suggest this PR carries the encoder, and Inkling 2 Mini on Metal: routed experts on the M5 tensor units at every width (16 rows 49 to 36 ms, 64 rows 138 to 84 ms) #4523's commit shrinks to what it adds on top: the command-buffer split. The hazard test above would then cover both.

Repo checks we ran on this branch:

  • zig fmt and ast-check are clean.
  • The lean check is unchanged.
  • forward.zig goes from 596 lines to 410, and no Zig file is over 600.
  • No new environment variables, and no pins touched.

The M5 check we'd like to see, main against this branch at default settings:

  • Greedy tokens and logits for a 2K prompt and 128 decode steps, compared bit for bit, with drafts on and off. Include a TFQ3 or TFQ5 checkpoint.

  • The same long prompt run 20 times, with identical replies each time.

  • Decode tokens per second and prompt tokens per second at 1K and 8K, which is what the concurrency is for.

  • Synthena

@sethforprivacy

Copy link
Copy Markdown
Contributor Author

Thanks, @ashhart, and for catching the TFQ5/TFQ3 barrier. Agreed: this PR carries the encoder. #4523 is closed, and I don't plan to bring its five-command-buffer split back unless the M5 numbers show it's worth a PR of its own.

I'll add the hazard test in the shape of #5674's: it records the forward's launches and fails when a launch in a concurrent span reads a buffer written earlier in that span, with a planted control that drops one barrier. It will cover a decode round, a verify window, a prompt chunk and each expert format. When you rebase onto TF_EXPLAIN, I'll write it against that head. After that come the M5 checks you listed: a 2K prompt plus 128 steps equal to the bit, with drafts on and off, including a TFQ3 or TFQ5 checkpoint; 20 identical runs of one long prompt; and decode and prompt rates at 1K and 8K.

@ashhart

ashhart commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Thanks for agreeing to carry the encoder, and for catching the TFQ5/TFQ3 barrier before the hazard test existed. That catch is the argument for the test.

One thing is yours: the hazard test itself, since it follows your design. The device cells are ours, on our M5 Ultra: a 2K prompt plus 128 decode steps bit for bit with drafts on and off (including a TFQ3 or TFQ5 checkpoint), 20 identical runs of one long prompt, and prompt and decode rates at 1K and 8K. The 24-prompt token set and the 4-chat hash cross-check are ours too. #4523 stays closed unless the numbers justify its command-buffer split.

  • Synthena

ashhart added a commit to sethforprivacy/TensorFold that referenced this pull request Dec 3, 2026
…it for the concurrent encoder

ashhart#5435 runs the forward on a concurrent encoder with a barrier between dependent launches; merged with main, the
tree attention splits into prefix and partials calls, and a visit's segments go through forward_attention's
attendEach, which keeps main's depth dispatch. attention.zig, forward.zig and forward_visit.zig keep main's notes
with ashhart#5435's added, and each pin is the file's hash as it ends up.
@ashhart
ashhart force-pushed the inkling2-mini-metal-concurrent branch from cbcec98 to 74bc069 Compare December 3, 2026 06:57
@ashhart ashhart added kind:perf Classified by Synthena as performance improvement synthena:needs-receipt Speed PR awaiting verified perf_receipt.py hash run labels Dec 3, 2026
@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Synthena Initial Review — Performance receipt required (kind:perf)

Runs the Inkling 2 Mini forward on a concurrent Metal encoder with barriers between dependent launches, extracting scheduling helpers into new files, and claims +21% plain decode and +47% with 4 drafted streams.

  • Performance claim detected without a verified tf-receipt-v1 receipt.

Performance PRs require a verifiable benchmark receipt before running project CI hardware. See CONTRIBUTING.md (Performance PRs: verifiable benchmark receipt) and attach the output of:

python3 tools/perf_receipt.py run --base-bin /path/to/base-tf --head-bin /path/to/head-tf --model /path/to/model.tfold --out receipt.json

Findings:

  • [WARNING] zig/src/families/inkling2/metal/forward_visit.zig: The diff shows a barrier inserted before transition(..., .attention, ...) in attendSegments, but the original code relied on the serial encoder ordering; on a concurrent encoder, the transition's own launches (e.g., cache staging or state resets) may read buffers written by the preceding attention prep, and the barrier placement needs verification that it covers all producer-consumer pairs, not just the ones listed in the PR description. Suggested fix: Confirm the barrier covers the transition's reads of q_raw/k_conv/v_conv written by the preceding conv or prep launches; if transition reads additional buffers, add a barrier after those producers too.
  • [WARNING] zig/src/families/inkling2/metal/forward_attention.zig: attendDepth is called inside the per-segment loop without a barrier between iterations; on a concurrent encoder, if attendDepth writes to shared scratch (e.g., part_o/part_ml at overlapping offsets or depth partials) while the next segment's attendDepth reads its own inputs, there is no ordering guarantee between the two launches. Suggested fix: Verify attendDepth's writes are strictly per-segment disjoint scratch regions; if any shared buffer is written, add e.barrier() between loop iterations or after the loop before the merge phase.
  • [WARNING] zig/src/families/inkling2/metal/forward_attention.zig: treePrefix launches for multiple tree segments are issued back-to-back with no barrier between them; if treePrefix writes to part_o/part_ml at per-segment offsets this is fine, but tree_tail_o/tree_tail_ml are also per-segment so this appears safe only if all buffers are strictly disjoint per segment. Suggested fix: Confirm tree_tail_o and tree_tail_ml scratch regions are sized and offset per segment with no overlap; if they share a global region, add a barrier between treePrefix launches.
  • Synthena

sethforprivacy and others added 3 commits December 3, 2026 11:34
…ches side by side

The forward ran every launch in one serial encoder, so each waited for the one before it, though many read only
what an earlier step wrote. It now runs on a concurrent encoder with a barrier after every step a later launch
reads. Launches between two barriers run side by side: a visit's six input projections, its three norms, the three
roles' gates and depth-mix reads, the K and V convolutions, and the K and V staging copies.

Three helpers issued dependent launches back to back and relied on the serial encoder's order; they now put a
barrier between them: the router's scores and selection (routerSplit), the split gates' norm, down and apply
(gatesSplit), and the convolution staging before the dynamic convolutions (fusedAttention). On a serial encoder these
barriers change nothing.

The routed experts move to forward_experts.zig, so forward.zig stays within the 600-line rule.

No kernel and no arithmetic changes: every reply is main's, bit for bit.
…it for the concurrent encoder

ashhart#5435 runs the forward on a concurrent encoder with a barrier between dependent launches; merged with main, the
tree attention splits into prefix and partials calls, and a visit's segments go through forward_attention's
attendEach, which keeps main's depth dispatch. attention.zig, forward.zig and forward_visit.zig keep main's notes
with ashhart#5435's added, and each pin is the file's hash as it ends up.
@ashhart
ashhart force-pushed the inkling2-mini-metal-concurrent branch from 74bc069 to f0af440 Compare December 3, 2026 11:38
@ashhart ashhart added synthena:needs-ash Needs owner decision and removed synthena:needs-receipt Speed PR awaiting verified perf_receipt.py hash run labels Dec 3, 2026
@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Synthena Initial Review — Needs maintainer review (kind:perf)

Runs the Inkling 2 Mini forward on a concurrent Metal encoder with barriers between dependent launches, extracting scheduling helpers into new files, and reports bit-identical outputs with +21% plain decode and +47% four-stream throughput.

  • Failing CI check(s): verify.

Findings:

  • [WARNING] zig/src/families/inkling2/metal/forward_attention.zig: The depth path writes att_ref directly (no merge), but the following unconditional e.barrier() only orders execution; the subsequent visit's project(m, ..., at(s.att, 0), ...) reads s.att on the same concurrent encoder, which is fine only because the barrier is there. However, when a segment takes the depth path and another takes partials+mergeRaw, mergeRaw writes s.att for its segment while depth writes its own rows; both are ordered by the barrier before the next stage, so this is correct, but the depth kernel's contract (raw states, fold and merge in one call) is asserted only in a comment and is not covered by any test in the diff. Suggested fix: Add a unit or fixture test covering a mixed segment set (one depth segment, one partials segment) asserting bit-identical output against the serial-encoder path, since the depth path bypasses mergeRaw and a missed barrier would only surface as nondeterminism.
  • [WARNING] zig/src/families/inkling2/metal/forward_visit.zig: The barrier before copyBf16 orders the preceding mlp_partial/mlp_partial_ar launch against the prelude backup, but the backup itself is not followed by a barrier before the next visit's launches read s.prelude (via bankAndReset or the next visit's prepareAttentionInputs). If the next visit's first launch reads s.prelude on the concurrent encoder, the copy may not have completed. Suggested fix: Verify the consumer of s.prelude is always separated by a later barrier (e.g. the visit-end e.barrier() or bankAndReset's own barriers); if any path reads s.prelude immediately after this copy without an intervening barrier, add one.
  • Synthena

@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

READY to land on this head. Device A/B on an M5 Ultra with this rebased head against its base on Inkling 2 Mini: tokens equal in every cell; decode +16.3% at 1 stream, +5.1% at 4 and +22.7% at 8 (37.1 to 43.2 and 105.8 to 129.9 tok/s), prompt +7% from 2,048 tokens, repetitions agreeing.

  • Synthena

@ashhart ashhart added the synthena:ready Reviewed by Synthena and ready for group train label Dec 3, 2026

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

kind:perf Classified by Synthena as performance improvement synthena:needs-ash Needs owner decision synthena:ready Reviewed by Synthena and ready for group train

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants