Skip to content

qwen27 Metal (M5): out_proj group sums written by the GDN post and attention merge kernels (#2931) - #2944

Open
ashhart wants to merge 8 commits into
mainfrom
lead/q27-outproj-sums
Open

ashhart wants to merge 8 commits into
mainfrom
lead/q27-outproj-sums

Conversation

@ashhart

@ashhart ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

What this changes

On M5-class Macs (tensor units), Qwen3.8-27B's decode and verify rounds give out_proj its group sums from the kernels that write its input, instead of a separate tf_lane_sums launch and barrier. The GDN post (deltanet_sums.metal) and the attention merge (attention_mpp.metal) each write their BF16 outputs as before, then one lane a 64-wide group re-reads the stored words and adds them from +0 in index order, tf_lane_xsum's order. Prompt passes, rank seams and pre-M5 chips keep their own sums. No arithmetic, precision, default or memory change.

Checks (M5 Ultra, Qwen3.8-27B MLX 4-bit)

  • Kernel contract for the two producers: gates 1-4 pass, and both reversed-order controls go red. They are checked against tf_lane_sums and an FP64 sum of the stored BF16 outputs, at 1, 5, 16 and 128 rows, with guarded buffers and nonfinite inputs.
  • Every real layer at 1, 5 and 16 rows: 192 cases and 135,168 group sums equal tf_lane_sums bit for bit, and each out_proj output is byte-equal with fused and with own sums.
  • Served: ten replies (160 tokens) match qwen27 metal: qk projections, flat glue and Metal 4 verify passes, each bit-identical and switchable #2342's head, covering one and four greedy streams, four mixed streams and one sampled stream.
  • Host: 597/598 (1 skipped); static contract 240/240; no new lean problems.

An M3 Ultra run and the M5 speed A/B are ours to do before landing; expect about +0.4-0.8% decode on M5.

@ashhart
ashhart force-pushed the lead/q27-outproj-sums branch from e7909b4 to 96e5913 Compare November 12, 2026 14:15
@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Status on this PR.

The zig check was red on the lean rule only: a two-line comment in zig/src/metal/metal4.zig from #2342's base commit. #2342 fixed it in 6022cc7. This branch now sits on #2342's current head, with this PR's commit unchanged, and it still merges cleanly with main.

Still owed before it lands:

  1. qwen27 metal: qk projections, flat glue and Metal 4 verify passes, each bit-identical and switchable #2342 lands first.
  2. A pre-M5 Mac run to show replies are equal and decode is level. Those chips keep their own sums, so nothing should move.
  3. The M5 speed A/B against qwen27 metal: qk projections, flat glue and Metal 4 verify passes, each bit-identical and switchable #2342, where we expect about +0.4-0.8% decode.

Overlap with #2945, #2955 and #2960: #2945 also adds zig/src/families/qwen27/attention_views.zig, and all three change the attention entry points, so whichever side lands second has to merge them. A plain merge is not enough here. This PR picks the summing merge kernel as io[11] (zig/src/families/qwen27/attention.zig:517), but from #2955 on, io[11] is q27_qk_norm_rope, and #2960 puts q27_qk_cache_rope at io[12]. On top of that stack, the sums variant becomes io[13]. The sums also have to reach the merge through the fused prepare path (prepareAndEncode), because that is the path M5 decode takes after #2960.

  • Synthena (Ash's Agent)

A tensor-op projection kernel for 4-bit group-64 matrices on chips with
tensor units. Load leaves those matrices in projection.weight_layout and
skips their reorder for tf_lane_reg, so qk reads the codes and the
metadata_layout scales and biases where they sit: no extra memory. Every
output word equals tf_lane_reg's on the same K split.

- zig/kernels/metal/core/qk.metal: the kernel, any rows a dispatch.
- zig/src/core/qk_projection.zig: pipelines by K split and block rows,
  the encode, and a compile check for runtimes that refuse uint4b_format.
- Plain matrices take qk for decode and prompt rows; the Neural Engine
  share's skipped-tile and cut-group forms take tf_lane_pair.
- TF_Q27_QK=0 at load restores the reorder and tf_lane_reg.
- tf-qwen27-qk-ab: tf_lane_reg against qk, interleaved in one process.

Checked on an M5 Max: public golden 1024 of 1024; qk-ab equal over 1,204
hot rounds a side; 16 served replies equal to stock; streams, marks,
lanes and lean_check as on main; zig build test exit 0. A verify is 1.2%
to 2.9% faster hot.
…and argmax

Four glue kernels for Qwen27 rounds, each bit-identical to the kernel it
replaces, with TensorFold's own kept wherever a round does not fit them.

- tf_delta_gdn_columns_1/2/4 replace tf_delta_gdn_tree_slots_* for lone
  trees of up to 4 live states, reading the slot schedule as flow words
  (tree_slots.flows, a new frame field).
- tf_delta_gdn_pre_vectors replaces tf_delta_gdn_pre for four taps over
  128-wide heads with bf16 conv, A_log and dt.
- q27_mlp_rows replaces custom_kernel_q27_mlp_xs where 256 threads run.
- tf_round_pick_chunks/rows replace tf_round_bf16_argmax for Qwen27's two
  callers through Ops.argmaxChunks; other families keep Ops.argmax.
- TF_Q27_FLAT_GLUE=0 at load, or decode_round.setFlatGlue, keeps every
  round on TensorFold's kernels.
- tf-qwen27-glue-ab: the general glue against the flat kernels.

Checked on an M5 Max, alone on 1.2.2: glue-ab equal over 1,204 rounds;
golden, streams, marks, reuse, lanes and served replies equal; a 16-row
verify 3.2% to 4.2% faster. Rebased here with one conflict (main's routed
block fields in gpu_frame.zig) and checked stacked on qk: golden 1024 of
1024.
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.
A repeated M5 A/B lost 6.9% at four sampled streams. Keep the sums kernels and bindings, but make outSums return false so quant_gpu uses encodeOwnSums, GDN uses tf_delta_gdn_post and attention uses q27_attn_merge_single. Token equality was reported; M5 performance proof remains owed.
@ashhart
ashhart force-pushed the lead/q27-outproj-sums branch from 4aac7cd to 2d874d0 Compare November 23, 2026 20:43

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.

qwen27 Metal (M5): out_proj group sums written by the GDN post and attention merge kernels

1 participant