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) using threadgroup-staged LUTs + short4 dots.
  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), complementing q4_qsa_attend_batch_mma already integrated in de0fe5b.

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

  • GR overhaul alone (Track 2 vs R2): --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 (~4x faster); 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, 53/53 Metal ctest -L poly passing on integrated R3 stack).

Related discussion

Tracking issue: #472. Rebased onto origin/main (7cef952, v0.2.20-poly.11), preserving de0fe5b (q4_qsa_attend_batch_mma), d72af68 (attn_piece), and looped Qwen4 declarations in qwen4_kernels.hpp.

Approach

  • Unifies q4_gr_down_silu_quant, q4_gr_up_gate_mean_quant (13-arg signature matching qwen4.cu), and q4_mul_sigmoid_q8_1 in engine/src/poly/qwen4_kernels.hpp while preserving Metal's 11-arg forwarding overload and non-Metal CUDA/HIP paths.
  • Keeps quantized.{metal,cpp} strictly scoped to native_mmvq_ids_gu_swiglu_q8 (using self-contained gu_split_* helpers so existing poly_mul_k1/k1g kernels remain untouched for the separate high-BW GEMV PR).
  • Intentionally omits Metal lin_mixed (measured slower on Apple Silicon in R2 testing).

Validation

  • Passed (Apple M4 16 GB, macOS, commit 37e7542 on origin/main 7cef952):
    • Metal Release configure + build (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 20 gu_swiglu_q8 cases, full 3-stage GR Metal Graph/ICB 20/20 bit-exact parity, q4_mul_sigmoid_q8_1, q4_qsa_score_batch_mma 12/12 + top-K exact match, q4_qsa_attend_batch_mma, q4_split_2d, and poly_mv_parity: 0 mismatches).
    • python3 tools/check --only docs: passed (6/6).
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host (CUDA/HIP signatures in qwen4_kernels.hpp were statically checked against qwen4.cu). Kept as Draft pending cross-backend CI (backends.yml / check.yml).

Limits

  • Cross-backend compilation on CUDA/HIP/SYCL/MSVC must be confirmed via CI before marking Ready for Review.

@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
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