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
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) using threadgroup-staged LUTs +short4dots.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), complementingq4_qsa_attend_batch_mmaalready integrated inde0fe5b.Measured on real 16 GB Apple M4 (
Qwen4-35B-A3B-UD-IQ2_XS, 192 tokens, identical frozen expert cache):Track 2vs R2):--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(~4x faster);q4_gr_down_q8_0(r=2MTP)46.8 -> 68.4 GB/s(+46%).110.93 -> 111.24 tok/sno-draft,53/53Metalctest -L polypassing on integrated R3 stack).Related discussion
Tracking issue: #472. Rebased onto
origin/main(7cef952,v0.2.20-poly.11), preservingde0fe5b(q4_qsa_attend_batch_mma),d72af68(attn_piece), and looped Qwen4 declarations inqwen4_kernels.hpp.Approach
q4_gr_down_silu_quant,q4_gr_up_gate_mean_quant(13-arg signature matchingqwen4.cu), andq4_mul_sigmoid_q8_1inengine/src/poly/qwen4_kernels.hppwhile preserving Metal's 11-arg forwarding overload and non-Metal CUDA/HIP paths.quantized.{metal,cpp}strictly scoped tonative_mmvq_ids_gu_swiglu_q8(using self-containedgu_split_*helpers so existingpoly_mul_k1/k1gkernels remain untouched for the separate high-BW GEMV PR).lin_mixed(measured slower on Apple Silicon in R2 testing).Validation
37e7542onorigin/main7cef952):strata-poly,poly_qwen4_parity,poly_quantized_parity,poly_mv_parity,poly_runtime_safety,poly_tiny_model_fixture): 0 errors, 0 warnings.ctest --test-dir engine/build-metal -R '^(poly_qwen4_parity|poly_quantized_parity|poly_mv_parity|poly_runtime_safety|poly_metal_defaults)$' --output-on-failure: 11/11 passed (all 20gu_swiglu_q8cases, full 3-stage GR Metal Graph/ICB 20/20 bit-exact parity,q4_mul_sigmoid_q8_1,q4_qsa_score_batch_mma12/12 + top-K exact match,q4_qsa_attend_batch_mma,q4_split_2d, andpoly_mv_parity: 0 mismatches).python3 tools/check --only docs: passed (6/6).qwen4_kernels.hppwere statically checked againstqwen4.cu). Kept as Draft pending cross-backend CI (backends.yml/check.yml).Limits