Skip to content

qwen27 Metal (M5): fused norm and RoPE write K and V into the cache and Q into qa directly (#2940 step 2b) - #2960

Open
ashhart wants to merge 14 commits into
mainfrom
lead/q27-fused-kv-write
Open

ashhart wants to merge 14 commits into
mainfrom
lead/q27-fused-kv-write

Conversation

@ashhart

@ashhart ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

What this changes

On M5-class Macs (fused attention plans), Qwen3.8-27B's q27_qk_norm_rope now writes rotated K and V straight into the KV cache (contiguous and paged) and Q into the packed qa buffer, filling every packed slot that maps to its (node, head), padding slots included. That removes the producer's intermediate barrier and the two append and pack dispatches; the consumer barrier stays. The arithmetic is #2955's kernel unchanged. The diagnostic norm/query/key outputs remain for the byte checks. Other chips and unfused plans keep #2955's path.

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

The M5/M3 speed A/B against #2955 is ours to run before landing.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Status on this PR.

Rebased with #2945 and #2955. The new base merges #2342's current head (6022cc7) with main. That fixes the lean failure in zig/src/metal/metal4.zig that turned the zig check red, and it clears the conflict with main's #2816 in the Qwen27 glue files. This PR's commit is unchanged.

Still owed before it lands:

  1. qwen27 metal: qk projections, flat glue and Metal 4 verify passes, each bit-identical and switchable #2342, qwen27 Metal: K and V read in place from the mixed projection, no unstack copies (#2940 step 1) #2945 and qwen27 Metal: one q27_qk_norm_rope dispatch for Q and K head norm and RoPE (#2940 step 2a) #2955 land first.
  2. The M5 speed A/B against qwen27 Metal: one q27_qk_norm_rope dispatch for Q and K head norm and RoPE (#2940 step 2a) #2955, covering prompt and decode. On the M5, prompt chunks are fused chains too, so they also take the direct cache writes.
  3. A pre-M5 run to show nothing moves there, since those chips keep qwen27 Metal: one q27_qk_norm_rope dispatch for Q and K head norm and RoPE (#2940 step 2a) #2955's path.

#2944 also changes the attention entry points. If it lands after this PR, its summing merge kernel moves to io[13], behind this PR's q27_qk_cache_rope at io[12].

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

On main d2b3722 (#2913: XS sums compile out before tensor units),
q27_mlp_rows writes XS only under Q27_XS and its key carries main's .xs,
so M1-M4 keep the flat activation without sums; main's norm and MLP test
now runs with the flat glue off and on.
…the dropped Metal 4 commits

Main's 739ed5d left zig/src/families/qwen27/decode_round.zig at 596 lines, and #2342's flat glue adds seven,
which its Metal 4 commit (no longer part of this PR) had offset. Three blank lines between top-level declarations
go; nothing else changes.
…nd packed Q

Keep both native norm roundings and the original rotary expressions.
Fill every canonical packed slot, including each stream's repeated padding.
Retain the separate append and pack for trees and pre-tensor plans.

Refs #2940.
@ashhart
ashhart force-pushed the lead/q27-fused-kv-write branch from e93c31f to 358d89e Compare November 23, 2026 20:10

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