Skip to content

[Engine] qwen4/metal: rewrite q4_qsa_attend_batch with simdgroup_matrix - #476

Closed
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-metal-qsa-mma
Closed

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

Conversation

@NightRain233

Copy link
Copy Markdown
Contributor

Problem and result

In STRATA_METAL_PROF=2 profiling of 4,096-token Qwen4-35B-A3B prefill on Apple Silicon Metal (df2c3ff, M5 Ultra), q4_qsa_attend_batch_q8_8 was the single largest GPU hotspot: 515.1 ms (29.3% of total 4K prefill GPU time) at only 1.34 TFLOP/s (1.48 GB/s), because it performed scalar per-element K/V reads (load_v1 inside the chunk loop) and scalar for (h = 0..G-1) FMAs instead of using Apple Silicon simdgroup_matrix hardware MMA.

This PR adds q4_qsa_attend_batch_mma_{q8,f16}_8 for dim == 256 && G <= 8 (strictly under gpu::metal::optimize(), keeping the original scalar kernel intact for dim != 256, G > 8, and --no-metal-optimize).

Measured on Apple M5 Ultra 256 GB (Qwen4-35B-A3B-UD-IQ2_XS, --kv q8_0 --no-draft --lookup 0):

  • Standalone microbenchmark (r512_G8_n2048 in poly_qwen4_parity):
    • FP16: 12.872 ms/call (1.33 TFLOP/s) -> 1.325 ms/call (12.97 TFLOP/s) (9.71x faster)
    • Q8_0: 13.043 ms/call (1.32 TFLOP/s) -> 1.348 ms/call (12.75 TFLOP/s) (9.68x faster)
  • End-to-end 4K prefill (STRATA_METAL_PROF=2):
    • q4_qsa_attend_batch: 492.0 ms (1.40 TFLOP/s) -> 48.2 ms (14.27 TFLOP/s) (10.20x faster; 10.67x vs 515.1 ms on the Chinese/code 4K prompt)
    • Total 4K prefill throughput (unprofiled same-day A/B): 2,488.2 tok/s -> 3,425.1 tok/s (+37.7%)
    • Greedy token parity after 4,096-token prefill: 16/16 exact match (and 4/4 exact match on standalone PR branch).

Related discussion

Approach

  • Strictly Metal-only changes across 3 files (engine/metal/src/poly/qwen4.metal, engine/metal/src/poly/qwen4.cpp, engine/metal/poly_tests/qwen4_parity.cpp).
  • 128 threads (4 SIMD-groups), grid (n_kv, rows, 1), total TG memory 16,224 B (<= 16 KB for 2 concurrent TGs/core with bank-conflict-free padded strides sq_h[8][264], skv[16][264], sp_h[8][72], sp_f[8][64]; sp_tmp aliases sp_h and warp-private o_tmp aliases skv).
  • Coalesced full 256-dim row loads (uint2 256 B burst + 16 B L1 scale line for Q8_0; uint4 512 B burst for FP16) with software-pipelined register prefetch overlapping the next 16 rows' device loads with 16 back-to-back 8×8×8 simdgroup_multiply_accumulate ops.
  • Exact FP32 online softmax; skips o_tmp accumulator rescaling whenever all(s_alpha[0..G-1] == 1.0f); explicit n_ids <= 0 zeroing fast-path; pads unused heads h >= G with 0.0h so any 1 <= G <= 8 works.

Validation

  • Passed (standalone commit bec6133 on df2c3ff, macOS Apple M5 Ultra 256 GB, AppleClang 21.0.0 Release):
    • cmake --build engine/build-metal -j --target strata-poly poly_qwen4_parity poly_quantized_parity poly_advanced_parity poly_memory_growth -> 0 errors.
    • ctest --test-dir engine/build-metal -L poly --output-on-failure -> 51/51 passed (100%).
    • poly_qwen4_parity QSA batch MMA matrix (rows = 1, 64, 512, G = 4, 8, dim = 256, n_ids = 0, 17, 64, 512, 2048, ring = 0 & 257, Q8_0 & FP16, strided kv_rs/v_rs, dim = 128 fallback, poisoned n_ids = 0): 0 mismatches (max err 0 to 1.39e-4).
    • python3 -m unittest tools.test_settings_register && python3 tools/settings_register.py --check -> 8/8 tests OK.
  • Failed: None observed.
  • Not run: CUDA/HIP/SYCL builds (not applicable — no non-Metal files touched).

Limits

  • Fast path targets dim == 256 && G <= 8 (covers Qwen4-35B-A3B and Qwen3.8-Flash-Next); other shapes use the existing scalar kernel.
  • Uses FP16 simdgroup_matrix inputs with FP32 accumulation and FP32 online softmax (max abs err <= 1.39e-4 vs FP32 reference, well within poly_qwen4_parity tolerances; --no-metal-optimize selects the original FP32 scalar kernel).

@VecSzn

VecSzn commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Integrated on main in commit de0fe5b9f (verified 87/87 Metal ctest on Apple Silicon; included in upcoming v0.2.20-poly.11). Thanks @NightRain233!

@VecSzn VecSzn closed this 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.

2 participants