Repository navigation
[Engine] qwen4/metal: fuse decode kernels, QSA score MMA, and overhaul Gated Residual - #475
Draft
NightRain233 wants to merge 1 commit into
Draft
NightRain233 wants to merge 1 commit into
NightRain233 wants to merge 1 commit into
Conversation
NightRain233
force-pushed
the
feat/qwen4-metal-decode-fusion
branch
from
December 6, 2026 05:38
2afcf13 to
37e7542
Compare
This was referenced Dec 6, 2026
Merged
NightRain233
force-pushed
the
feat/qwen4-metal-decode-fusion
branch
from
December 6, 2026 16:38
37e7542 to
35a0e6a
Compare
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.
Problem and result
On Apple Silicon Metal, Qwen4-35B-A3B decode previously spent a large fraction of GPU time in unfused attention/MoE epilogues and Gated Residual (GR) projections:
q4_gr_downemitted ~20,800seq_cstdevice memory fences per token across 80 GR blocks and quantizedgr_lo_toQ8_1just to feedq4_gr_up.q4_gr_updecodedQ6_K/Q4_K/Q8_0at31–47 GB/son Apple M4 (120 GB/speak).gate+upGEMV,SwiGLU, andQ8_1quantize passes instead of fusing in SRAM, and QSA prompt scoring ran scalar instead ofsimdgroup_matrixMMA.This PR fuses Qwen4 Metal decode & QSA score kernels and overhauls the Metal Gated Residual pipeline (100% lossless, numerically equivalent within
1.5e-5):seq_cstdevice fences fromq4_gr_down(NR=2specializedr1/r2/r4/r8), feeds FP32gr_lo_directly into threadgroup memory inq4_gr_up(MAXR=1,2,4,8, 256-thread zero-redundancyQ6_K/Q4_K/Q8_0decode, eliminatingloQ8_1quantization), and adds vectorizedq4_gr_norm_vec4.q4_mul_sigmoid_q8_1, vectorizedq4_split_2d/q4_gr_gate_mean, and SRAM-fused MoEnative_mmvq_ids_gu_swiglu_q8(q4_K,iq2_xs,iq2_s,iq3_xxs,nvfp4) wired directly into [Engine] metal: high-bandwidth short4/TG-LUT decode GEMV for Q5_K/Q6_K/Q4_K/IQ2/IQ3 #498's LUT-enabledgrp_split_*.q4_qsa_score_batch_mma(simdgroup_half8x8 -> simdgroup_float8x8), running 0.418 ms vs 6.124 ms scalar (14.6x faster inpoly_qwen4_parity, exact top-K match).Measured on real 16 GB Apple M4 (
Qwen4-35B-A3B-UD-IQ2_XS, 192 tokens, frozen expert cache):--no-draft13.95 -> 16.17 tok/s (+15.9%);q4_gr_up_q6_K(r=1)46.9 -> 70.7 GB/s(+51%) and (r=2MTP)79.3 ms -> <17 ms;q4_gr_down_q8_0(r=2MTP)46.8 -> 68.4 GB/s(+46%).110.93 -> 111.24 tok/sno-draft).Related discussion
Tracking issue: #472. Cleanly rebased onto
origin/main(0794bc5, including #498, #464, #467, #504) at commit35a0e6a.Validation
35a0e6aonorigin/main0794bc5, Apple M5 Ultra 64-core Apple10):cmake --build engine/build-metal -j16): PASS.ctest --test-dir engine/build-metal -L poly --output-on-failure: 51/51 (100%) PASS (includes all 20gu_swiglu_q8cases, full 3-stage GR Metal Graph/ICB 20/20 parity,q4_mul_sigmoid_q8_1,q4_qsa_score_batch_mma12/12 + top-K exact match,q4_split_2d, and all [Engine] metal: high-bandwidth short4/TG-LUT decode GEMV for Q5_K/Q6_K/Q4_K/IQ2/IQ3 #498 parity tests).python3 tools/test_dropped_paths.py,python3 tools/settings_register.py --check, andpython3 tools/check --only docs(6/6): PASS.CONTRIBUTING.mdpending maintainerrun-backendsCI.