Repository navigation
qwen27 Metal (M5): out_proj group sums written by the GDN post and attention merge kernels (#2931) - #2944
qwen27 Metal (M5): out_proj group sums written by the GDN post and attention merge kernels (#2931)#2944ashhart wants to merge 8 commits into
Conversation
e7909b4 to
96e5913
Compare
|
Status on this PR. The Still owed before it lands:
Overlap with #2945, #2955 and #2960: #2945 also adds
|
16c6127 to
4aac7cd
Compare
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.
4aac7cd to
2d874d0
Compare
What this changes
On M5-class Macs (tensor units), Qwen3.8-27B's decode and verify rounds give
out_projits group sums from the kernels that write its input, instead of a separatetf_lane_sumslaunch 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.49412a3d96). Review only the last commit.forward.zig; the two merge cleanly. qwen27 Metal: K and V read in place from the mixed projection, no unstack copies #2940's PR also addsattention_views.zig, so whichever lands second merges the two.Checks (M5 Ultra, Qwen3.8-27B MLX 4-bit)
tf_lane_sumsand an FP64 sum of the stored BF16 outputs, at 1, 5, 16 and 128 rows, with guarded buffers and nonfinite inputs.tf_lane_sumsbit for bit, and eachout_projoutput is byte-equal with fused and with own sums.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.