Skip to content

[Engine] metal: lossless 256-K MMA256 for Q4_K/Q5_K/Q6_K/Q8_0, low-reg fast_r2..r8, and moe_sum_v4 - #511

Open
NightRain233 wants to merge 3 commits into
simplexai-labs:mainfrom
NightRain233:feat/metal-bw2-mma256
Open

NightRain233 wants to merge 3 commits into
simplexai-labs:mainfrom
NightRain233:feat/metal-bw2-mma256

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem & Result

Following #498, multi-row Metal decode GEMV (rows=2..8, used during speculative / MTP verification) still left significant memory bandwidth on the table across Apple9 (M4, 120 GB/s) and Apple10 (M5 Ultra, 64-core):

  • Q4_K/Q5_K/Q6_K multi-row _fast_r4 kernels shared one 4-row accumulator layout across rows=2..4 and hit scalar-ALU / register-pressure bottlenecks at rows=3..8 on large matrices (LM head 2048 -> 248320, dense MLP 2048 -> 8192).
  • Q8_0 multi-row decode fell back to scalar 1r4 / k4r4 on large matrices (43–53 GB/s on M4).
  • poly_gemv_v4_r8 allocated 8-row accumulators and shared memory even for rows=2..4, and moe_sum ran element-wise scalar loads/stores.

This PR adds lossless 256-K simdgroup_half8x8 -> simdgroup_float8x8 MMA kernels (_mma256), register-specialized _fast_r2/r3/r4/r8 kernels, vectorized Q8_0 fast/fast4 decode kernels (preserving k4/k4r4/k4r8 for small <= 1024-output matrices on high-core-count Apple10 so standalone Qwen4 Gated Residual 2048 -> 256 matrices never underfill), specialized poly_gemv_v4_r2/r4, and float4-vectorized poly_moe_sum_v4 (all pure engine/metal/ changes, standalone on main).

Standalone Same-Day Interleaved A/B vs origin/main (0794bc5 vs PR #511 b6ec657)

Apple M5 Ultra (256 GB unified memory, 64-core Apple10, full-VRAM 10,496/10,496 experts, Qwen4-35B-A3B UD-IQ2_XS, 256 tok, --kv q8_0):

Mode origin/main (0794bc5) r1 / r2 PR #511 (b6ec657) r1 / r2 Avg Gain
--no-draft --window 1 106.82 / 107.22 tok/s 108.37 / 108.65 tok/s +1.4%
--window 2 (MTP) 140.62 / 140.53 tok/s 147.26 / 146.67 tok/s +4.5%
--window 4 (chained MTP) 134.68 / 135.69 tok/s 139.30 / 139.70 tok/s +3.2%
Inkling-2-Mini UD-Q2_K_XL --no-draft --window 1 89.96 tok/s 90.69 tok/s (at c3c7360) +0.8%

Apple M4 (16 GB unified memory, 8-core Apple9, 120 GB/s, Qwen4-35B-A3B UD-IQ2_XS, 128 tok, frozen 4,000 experts, standalone without #474/#475):
(Commits 0f52617 and b6ec657 only touch is_apple10() guards; Apple9 M4 dispatch is identical to c3c7360.)

Mode origin/main (0794bc5) r1 / r2 PR #511 (c3c7360) r1 / r2 Avg Gain
--window 2 10.28 / 9.85 tok/s 10.72 / 10.25 tok/s +4.2%
--window 4 10.35 / 11.72 tok/s 11.67 / 12.34 tok/s +8.8%

Measured Microbenchmark Results (poly_mv_bench qwen4_bw2)

Apple M4 (8-core Apple9, 120 GB/s), Release:

Kernel / Shape Before (GB/s) After (GB/s) Gain
Q6_K 2048->248320 r=2 / r=3 / r=4 82.68 / 62.55 / 49.89 91.15 / 82.47 / 81.81 +10% / +32% / +64%
Q5_K 2048->248320 r=2 / r=3 / r=4 66.85 / 48.19 / 37.62 74.46 / 74.56 / 72.40 +11% / +55% / +92%
Q4_K 2048->248320 r=2 / r=3 / r=4 57.35 / 40.67 / 31.69 64.26 / 59.62 / 59.98 +12% / +47% / +89%
Q8_0 2048->8192 r=2 / r=3 / r=4 77.79 / 58.82 / 47.45 91.57 / 84.81 / 76.85 +18% / +44% / +62%
moe_sum n=2048 k=8 r=4 76.04 81.32 +6.9% (+62% vs opt=0)

Apple M5 Ultra (64-core Apple10), poly_mv_bench qwen4_bw2 (opt=0 vs opt=1):

  • Q6_K 2048->248320 r=2: 518.9 -> 1,110.6 GB/s
  • Q8_0 2048->8192 r=2: 848.4 -> 1,388.4 GB/s
  • Q5_K 2048->248320 r=2: 411.3 -> 860.7 GB/s
  • Q4_K 2048->248320 r=2: 403.5 -> 745.1 GB/s
  • moe_sum n=2048 k=8 r=4: 184.0 -> 254.0 GB/s

Approach

  1. Lossless 256-K Superblock-Tiled MMA (poly_mulr2_{q4_K,q5_K,q6_K,q8_0}_mma256) in engine/metal/src/poly/quantized.{metal,cpp}:
    • Uses simdgroup_half8x8 -> simdgroup_float8x8 MMA with exact integer-valued half operands and FP32 scales/mins/accumulation (NSG=4, NT=2, 64 outputs/TG).
    • Supports both single-matrix (kind == 0) and batched multi-matrix (kind == 9, same_x) dispatches with tail bounds guards via hoisted map_fast_group.
    • Architecture-aware thresholds in use_mma256(): min_no = is_apple10() ? 8192 : 2048, with Q4_K/Q5_K at rows >= 2 (Apple9) / rows >= 3 (Apple10), Q6_K at rows >= 3, and Q8_0 at rows >= 4.
  2. Low-Register Multi-Row SIMD (_fast_r2/r3/r4/r8) & Vectorized Q8_0 (_fast, _fast4):
    • Exact-row accumulator templates reduce register spills (NR=2 for Q4_K/Q5_K fast_r2, NR=4 for Q6_K/Q8_0 fast_r2, NSG=2).
    • On high-core-count Apple10, use_fast_mulr2() keeps < 2048-output matrices on 1-output/TG kernels (k4r4/k4r8 for Q8_0 <= 1024, 1r4 for Q4_K/Q5_K/Q6_K), and qrun() keeps poly_mul_q8_0_k4 for <= 1024-output Q8_0 matrices so small projections (2048 -> 256) stay fully saturated on 64-core GPUs.
  3. Specialized poly_gemv_v4_r2/r4/r8 and Vectorized poly_moe_sum_v4 in engine/metal/src/poly/basic.{metal,cpp}:
    • Templated poly_gemv_v4_r_t<MAXR> shrinks per-thread accumulators and threadgroup memory for rows=2..4.
    • poly_moe_sum_v4 uses float4 loads/FMAs with an unrolled K=8 fast path and 16-byte pointer + n % 4 == 0 alignment guard.
  4. Parity & Benchmark Coverage:
    • Extended quantized_parity.cpp, basic_parity.cpp, and mv_bench.cpp (qwen4_bw2).

Validation

Passed (exact PR head b6ec657, 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 (52.74 s, covers all parity tests and synthetic model protocols across LFM, Qwen3.5, Gemma 4, GLM5, DeepSeek 4, Inkling, Qwen4)
  • Interleaved same-day A/B vs origin/main (0794bc5) on Qwen4-35B-A3B UD-IQ2_XS — PASS (+1.4% no-draft, +4.5% w2, +3.2% w4)

Passed (Apple M4 8-core Apple9, commit c3c7360, Apple9 dispatch identical to b6ec657)

  • poly_basic_parity — PASS (0 mismatches)
  • poly_mv_parity — PASS (0 mismatches)
  • poly_quantized_parity — PASS (0 mismatches, max error <= 2.3e-5 vs FP32 reference, 0 OOB guard violations)
  • Interleaved same-day A/B vs origin/main (0794bc5) on Qwen4-35B-A3B UD-IQ2_XS — PASS (+4.2% w2, +8.8% w4)
  • git diff --check and python3 tools/test_dropped_paths.py — PASS

Failed

  • None.

Not Run

  • CUDA / HIP / SYCL builds (not touched; this PR modifies engine/metal/ only).

Limits & Default-Path Preservation

  • All new fast paths are gated on gpu::metal::optimize() (STRATA_METAL_OPT=0 restores the unoptimized fallback path unchanged).
  • All _mma256, _fast_r2..r8, gemv_v4_r2..r8, and moe_sum_v4 kernels are numerically lossless (FP32 accumulation; exact integer operands in MMA tiles).

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