Skip to content

[Engine] metal: high-bandwidth short4/TG-LUT decode GEMV for Q5_K/Q6_K/Q4_K/IQ2/IQ3 - #498

Merged
VecSzn merged 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-high-bw-decode-gemv
Dec 6, 2026
Merged

VecSzn merged 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-high-bw-decode-gemv

Conversation

@NightRain233

Copy link
Copy Markdown
Contributor

Problem and result

On Apple Silicon (especially Apple9 / 10-core M4 with 120 GB/s peak unified memory bandwidth), strata-poly Metal decode GEMV previously achieved only 45–60 GB/s on Q5_K/Q6_K/Q4_K dense projections and 29–40 GB/s on IQ2_XS/IQ2_S/IQ3_XXS MoE expert projections (while q4_gdn_state_reg_128_kernel sustained 112.8 GB/s on the same M4).

This pure-engine/metal/ PR adds lossless high-bandwidth Metal decode GEMV kernels (numerically equivalent within 1.5e-5 / 0 bit diffs across all 22 quant formats):

  1. Superblock-aligned short4 multi-SIMDgroup decode GEMV for Q5_K, Q6_K, and Q4_K across both single-row (r=1, kind=0 & kind=9 native_mmvq_many) and multi-row MTP/draft verification (r=2..8).
  2. Apple9 vs Apple10 NR=4 dispatch tuning: keeps Apple10 (M5 Ultra) threshold at 8192 outputs while lowering Apple9 (!is_apple10()) to 2048 outputs, with explicit nr == 1 && total_outputs <= 1024 guarding on 1-output-per-TG poly_mul_q8_0_k4 (verified by 10 NaN-sentinel Q8_0 regression tests across no = {1024, 2048, 3072, 4096} for kind=0 and kind=9).
  3. Threadgroup-staged LUT + short4 NSG=8, NR=8 MoE kernels for IQ2_XS (8 KiB + 128 B TG), IQ2_S (8 KiB TG), and IQ3_XXS (2 KiB + 128 B TG) on poly_mul_*_k1 / _k1g.

Measured on Apple M4 16 GB (macOS, poly_mv_bench qwen4 and real Qwen4-35B-A3B-UD-IQ2_XS):

  • poly_mv_bench microbenchmarks (Apple M4 16 GB):
    • Q5_K 2048->6144 r=1: 49.58 -> 89.05–96.68 GB/s (+80–95%); 2048->12288 r=1: 61.61 -> 100.40–102.97 GB/s (+63–67%); r=2: +66–76%.
    • Q6_K 2048->248320 r=1: 85.95 -> 104.31 GB/s (+21%); r=2 (MTP LM head): 44.55 -> 81.61 GB/s (+83%, 9.41 -> 5.14 ms); 2048->4096 r=1: 60.03 -> 98.33 GB/s (+64%).
    • Q4_K 2048->6144 r=1: 45.91 -> 97.62 GB/s (+113%); r=2/4: +125–142%; 2048->248320 r=1: 58.49 -> 104.39 GB/s (+78%, 2.75 ms).
    • IQ2_XS/IQ2_S/IQ3_XXS MoE (k1): +34% to +124% across shapes.
  • Real 35B Qwen4-35B-A3B-UD-IQ2_XS (Apple M4 16 GB, 192 tokens, frozen profile, standalone Track 1 vs R2):
    • --no-draft: 13.95 -> 15.05 tok/s (+7.9%); real STRATA_METAL_PROF=2: poly_mul_q5_K 56.2 -> 84.0 GB/s, poly_mul_q6_K 54.0–76.7 -> 83.7–95.0 GB/s.
    • Embedded MTP: 18.03 -> 19.04 tok/s (+5.6%); poly_mulr2_q6_K 40.8 -> 56.1 GB/s, poly_mulr2_q5_K 33.6 -> 45.8 GB/s.
  • Apple M5 Ultra 256 GB (Apple10, integrated R3 stack bfd5d1b):
    • Zero regression (110.93 -> 111.24 tok/s no-draft, 53/53 Metal ctest -L poly passing).

Related discussion

Tracking issue: #472. Strictly scoped to engine/metal/ (5 files only); zero non-Metal backend files touched.

Approach

Validation

  • Passed (Apple M4 16 GB, macOS, commit fcb0728 on origin/main 7cef952):
    • Metal Release configure + build (strata-poly, poly_quantized_parity, poly_mv_parity, poly_nvfp4_parity, poly_qwen4_parity, poly_mv_bench, poly_tiny_model_fixture): 0 errors, 0 warnings.
    • ctest --test-dir engine/build-metal -R '^(poly_quantized_parity|poly_mv_parity|poly_nvfp4_parity|poly_qwen4_parity|poly_metal_defaults)$' --output-on-failure: 11/11 passed (0 mismatches across dense unaligned tails no={1,63,516,2052,2053}, r={1,2,4,8}, kind=9 native_mmvq_many, MoE _k1/_k1g tails + guard columns, all 10 Q8_0 NaN-sentinel nr=4/k4 checks, and 0 bit diffs across all 22 quant types in poly_mv_parity).
    • ./engine/build-metal/poly_mv_bench 5 "12,13,14,17,18,22" qwen4 1: all cases OK (up to 105.6 GB/s on M4).
  • Failed: None observed.
  • Not run: Non-Metal backends (CUDA/HIP/SYCL) are completely untouched by this PR (engine/metal/** only).

Limits

  • Apple10 (M5 Ultra) keeps the 8192 NR=4 threshold based on prior measurements showing smaller dense matrices regress at NR=4 on 80-core Apple10 GPUs; further per-family tuning on M1/M2/M3 Max/Ultra GPUs can be evaluated separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-backends Run the backend compile checks (CUDA, HIP, SYCL, Metal) on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants