Skip to content

[Engine] qwen4/metal: fuse decode kernels, QSA score MMA, and overhaul Gated Residual - #475

Draft
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-metal-decode-fusion
Draft

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-metal-decode-fusion

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

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_down emitted ~20,800 seq_cst device memory fences per token across 80 GR blocks and quantized gr_lo_ to Q8_1 just to feed q4_gr_up.
  • q4_gr_up decoded Q6_K/Q4_K/Q8_0 at 31–47 GB/s on Apple M4 (120 GB/s peak).
  • MoE routed experts ran separate gate+up GEMV, SwiGLU, and Q8_1 quantize passes instead of fusing in SRAM, and QSA prompt scoring ran scalar instead of simdgroup_matrix MMA.

This PR fuses Qwen4 Metal decode & QSA score kernels and overhauls the Metal Gated Residual pipeline (100% lossless, numerically equivalent within 1.5e-5):

  1. Gated Residual Overhaul: removes all seq_cst device fences from q4_gr_down (NR=2 specialized r1/r2/r4/r8), feeds FP32 gr_lo_ directly into threadgroup memory in q4_gr_up (MAXR=1,2,4,8, 256-thread zero-redundancy Q6_K/Q4_K/Q8_0 decode, eliminating lo Q8_1 quantization), and adds vectorized q4_gr_norm_vec4.
  2. Decode & MoE Fusions: adds q4_mul_sigmoid_q8_1, vectorized q4_split_2d / q4_gr_gate_mean, and SRAM-fused MoE native_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-enabled grp_split_*.
  3. QSA Score MMA: adds q4_qsa_score_batch_mma (simdgroup_half8x8 -> simdgroup_float8x8), running 0.418 ms vs 6.124 ms scalar (14.6x faster in poly_qwen4_parity, exact top-K match).

Measured on real 16 GB Apple M4 (Qwen4-35B-A3B-UD-IQ2_XS, 192 tokens, frozen expert cache):

  • GR overhaul alone: --no-draft 13.95 -> 16.17 tok/s (+15.9%); q4_gr_up_q6_K (r=1) 46.9 -> 70.7 GB/s (+51%) and (r=2 MTP) 79.3 ms -> <17 ms; q4_gr_down_q8_0 (r=2 MTP) 46.8 -> 68.4 GB/s (+46%).
  • Verified zero regression on 256 GB Apple M5 Ultra (110.93 -> 111.24 tok/s no-draft).

Related discussion

Tracking issue: #472. Cleanly rebased onto origin/main (0794bc5, including #498, #464, #467, #504) at commit 35a0e6a.

Validation

  • Passed (exact rebased commit 35a0e6a on origin/main 0794bc5, Apple M5 Ultra 64-core Apple10):
    • Full Metal Release build (cmake --build engine/build-metal -j16): PASS.
    • ctest --test-dir engine/build-metal -L poly --output-on-failure: 51/51 (100%) PASS (includes all 20 gu_swiglu_q8 cases, full 3-stage GR Metal Graph/ICB 20/20 parity, q4_mul_sigmoid_q8_1, q4_qsa_score_batch_mma 12/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, and python3 tools/check --only docs (6/6): PASS.
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host. Kept as Draft per CONTRIBUTING.md pending maintainer run-backends CI.

@NightRain233
NightRain233 force-pushed the feat/qwen4-metal-decode-fusion branch from 2afcf13 to 37e7542 Compare December 6, 2026 05:38
@NightRain233 NightRain233 changed the title [Engine] qwen4/metal: fuse gr_down_silu_quant, mul_sigmoid_q8_1, and SRAM MoE gate_up+SwiGLU+Q8_1 [Engine] qwen4/metal: fuse decode kernels, QSA score MMA, and overhaul Gated Residual Dec 6, 2026
@NightRain233
NightRain233 force-pushed the feat/qwen4-metal-decode-fusion branch from 37e7542 to 35a0e6a Compare December 6, 2026 16:38
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