Skip to content

qwen27 Metal: plain greedy picks ride the verify's last command buffer (#2930) - #2936

Open
ashhart wants to merge 5 commits into
lead/qwen27-qk-glue-metal4from
lead/q27-greedy-picks-in-verify
Open

ashhart wants to merge 5 commits into
lead/qwen27-qk-glue-metal4from
lead/q27-greedy-picks-in-verify

Conversation

@ashhart

@ashhart ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Fixes #2930. Stacked on #2342 (rebased today); review the top commit.

A plain greedy Qwen3.8-27B round on Metal picked its tokens with a separate argmaxRows pass after the verify's last command buffer. Every greedy token paid one more command buffer and a host wait.

When a round is plain and greedy (the same condition that called argmaxRows), the argmax over the round's rows (#2342's argmaxChunks) is now encoded into the verify's last command buffer as its tail, and the separate pass is skipped. A sampling profile scope stays around the pick, so profiles still show the step. Sampled rows still draw on the host. A non-finite row still raises NonfiniteLogits, and mixed greedy-and-sampled rounds keep today's path.

Exact: the same argmax over the same logits, with ties going to the lower id.

Tests (host, recording fakes): a plain greedy round commits no command buffer after the verify's last; a planted NaN row still raises NonfiniteLogits; a mixed round keeps the host draw.

Not yet checked: greedy replies byte-equal on an M5 and an M3, and the A/B (expected +0.2-0.5% greedy, one and four streams).

@ashhart

ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner Author

Device check on an M3 Ultra (base = #2342's head, new = this PR's top commit at 52277cf): GREEN, tokens unchanged.

Greedy code prompts, aggregate tok/s:

streams base new change
1 33.0 34.5 +4.5%
2 51.3 53.8 +4.8%
4 82.2 84.7 +3.0%
8 147.3 150.4 +2.1%

Sampled prompts (temperature 1.0) are level, +0.3% to +1.8%, as expected: the change only touches plain greedy rounds.

I restacked this onto #2342's new head. #2342 gained one commit, the kernel-contract entries for its two pick launches (7917a84). This PR's commit is byte-for-byte the same patch, now f09e9c0. Both are next in the landing queue.

  • Synthena (Ash's Agent)

@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch from f09e9c0 to 9dd124b Compare November 14, 2026 00:58
@ashhart
ashhart force-pushed the lead/qwen27-qk-glue-metal4 branch from 7917a84 to 174645f Compare November 14, 2026 00:58
@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch from 9dd124b to 1e4e801 Compare November 14, 2026 02:18
@ashhart
ashhart force-pushed the lead/qwen27-qk-glue-metal4 branch from 1744e68 to 179ed9b Compare November 14, 2026 20:09
@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch from 1e4e801 to ed6f32b Compare November 14, 2026 20:09
@ashhart
ashhart force-pushed the lead/qwen27-qk-glue-metal4 branch from 179ed9b to 4f06405 Compare November 17, 2026 09:04
@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch from ed6f32b to c9125e6 Compare November 17, 2026 09:05
@ashhart
ashhart force-pushed the lead/qwen27-qk-glue-metal4 branch from 4f06405 to d7620df Compare November 17, 2026 17:45
@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch from c9125e6 to fa7d530 Compare November 18, 2026 02:00
@ashhart
ashhart force-pushed the lead/qwen27-qk-glue-metal4 branch from d7620df to d72f3a5 Compare November 18, 2026 02:19
@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch 2 times, most recently from bbf56a9 to 79159c4 Compare November 19, 2026 04:35
@ashhart
ashhart force-pushed the lead/qwen27-qk-glue-metal4 branch 2 times, most recently from e2b318c to 843d0e9 Compare November 21, 2026 15:41
@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch from 79159c4 to 958600b Compare November 22, 2026 02:15
@ashhart
ashhart force-pushed the lead/qwen27-qk-glue-metal4 branch from 843d0e9 to 0402836 Compare November 22, 2026 03:27
@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch 2 times, most recently from dab3cf3 to 7768fa7 Compare November 23, 2026 19:40
@ashhart
ashhart force-pushed the lead/qwen27-qk-glue-metal4 branch from 0402836 to 5439065 Compare November 23, 2026 19:55
@ashhart
ashhart force-pushed the lead/q27-greedy-picks-in-verify branch from 7768fa7 to 335b52d Compare November 23, 2026 21:36
A Metal 4 context for one model (zig/src/metal/metal4.zig): its own MTL4
queue, one command allocator, 32 reused command buffers, an argument
table, an 8 MB ring standing in for setBytes, and a residency set of its
own. command.zig's queue, command buffer and encoder take an optional
context and route every call through it.

- Residency: a buffer joins the context's set the first time a Metal 4
  encoder binds it (setBuffer, useResource, indirect arguments), with its
  GPU address cached; Buffer.deinit reports frees, and the buffer leaves
  the set at the context's next idle command buffer.
- Each commit takes new MTL4CommitOptions with a feedback handler, a
  block literal in Zig, that reports Metal's error and GPU start and end.
  Options reused for a second commit never call back.
- Barriers and fences map to the stage-based calls with device
  visibility; a barrier with no dispatch since the last is skipped, and a
  serial encoder barriers after each dispatch.
- Only Qwen27 verify and decode passes of up to 64 rows take it
  (decode_round.passQueue): prompt chunks ran 3.5% slower on Metal 4 with
  qk. Keeps, drafter frames, snapshots, profile traces, the Neural Engine
  share and seams stay on Metal 3; CPU waits order the two queues.
- On by default on chips with tensor units; TF_Q27_METAL4=0 turns it off
  and 1 turns it on elsewhere (untested there).
- core/fence.zig's Metal 4 branch compiles only where the encoder has the
  field, so recording fakes in tests still build.
- tf-qwen27-metal4-ab: Metal 3 against Metal 4 passes, interleaved.

Checked on an M5 Max: golden 1024 of 1024 alone on main and with qk and
the glue; metal4-ab equal over 68 rounds a side on either kernel set;
metal4.zig's unit test with no leaks; zig build test exit 0. It saves 0.3
to 0.5 ms a verify.
…and Metal 4

Two runners on one model, interleaved round by round: one on
tf_lane_reg, TensorFold's glue and Metal 3, the other on qk, the flat
glue and Metal 4. Every logit and pick must be equal; it prints median
wall and GPU time by width.

On an M5 Max: equal over 68 rounds a side cool and 244 hot; a 16-row
verify is 4.2% faster cool and 5.5% hot. Served streams, the same binary
with the switches on and off, were about 5% faster (93.1 against 98.3
tok/s with 120 s rests), and one-row decode about 2.5%, with every reply
equal to stock. Prompts stay at stock speed.
… analyze this file compile

Buffer.deinit calls metal4.released, so every build that compiles the Metal module (the Qwen4 projection bodies on
Linux among them) analyzes the registry lock. std.c.os_unfair_lock asserts it is on Darwin at compile time. Off Apple
the lock is now an empty struct whose hold and drop do nothing; on Apple the calls are the same os_unfair_lock ones.
Route the existing Qwen decode and verify passes through main generic Metal 4 timeline while keeping the lane core Drafter authoritative. Host Qwen A/B roots opt into that runtime, and pass selection moves into a small helper to retain the source cap.

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