Repository navigation
qwen27 Metal: K and V read in place from the mixed projection, no unstack copies (#2940 step 1) - #2945
Open
ashhart wants to merge 8 commits into
Open
qwen27 Metal: K and V read in place from the mixed projection, no unstack copies (#2940 step 1)#2945ashhart wants to merge 8 commits into
ashhart wants to merge 8 commits into
Conversation
ashhart
force-pushed
the
lead/q27-kv-in-place
branch
from
November 12, 2026 14:15
dc15a95 to
19a05bf
Compare
Owner
Author
|
Status on this PR. Two things were stopping it:
The new base merges #2342's current head (6022cc7) with main. This PR's commit sits on top with its diff unchanged, and both problems are gone. Still owed before it lands:
#2944 also adds
|
8 tasks done
ashhart
force-pushed
the
lead/q27-kv-in-place
branch
2 times, most recently
from
November 20, 2026 01:23
8d5f1d2 to
0167abe
Compare
4 of 8 tasks
ashhart
force-pushed
the
lead/q27-kv-in-place
branch
2 times, most recently
from
November 22, 2026 17:13
8366b64 to
45571df
Compare
8 tasks done
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.
ashhart
force-pushed
the
lead/q27-kv-in-place
branch
from
November 23, 2026 20:08
45571df to
7057fbc
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Qwen3.8-27B on Metal stops copying K and V out of the mixed projection before head norm, RoPE and the cache append.
q27_head_normreads grouped K heads at a2 * kvrow pitch, and both append layouts (contiguous and paged) read V frommixed + kvat the same pitch. The two unstack dispatches go, and so do the frame's separate key and value buffers. The kernels' arithmetic is unchanged: the default head arguments (heads = 1, zero row pitch meaning stride) keep today's Q and norm reads. Every chip and pass.49412a3d96) and qwen27 metal: drop q27_norm, q27_mlp and q27_gdn_post, which nothing launches (#2251) #2816 (2252756b0b), merged asec344c2bab. Review only the last commit.attention_views.zig, so whichever lands second merges the two.Checks (M5 Ultra, Qwen3.8-27B MLX 4-bit)
attentionForwardencodes noq27_slice.query,rotated_key, cache rows andqaat 1 and 16 rows, chain and tree, a 2,048-row prompt, image M-RoPE rows, paged and unpaged. A planted address edit is caught, and reversing the address edits recovers the parent shaders byte for byte.The M3 Ultra byte checks and the M5/M3 speed A/B are ours to do before landing; expect about +0.5-1% M5 decode and +0.3-0.5% on M3.