Repository navigation
[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
Open
[Engine] metal: lossless 256-K MMA256 for Q4_K/Q5_K/Q6_K/Q8_0, low-reg fast_r2..r8, and moe_sum_v4#511NightRain233 wants to merge 3 commits into
NightRain233 wants to merge 3 commits into
Conversation
…g fast_r2..r8, and moe_sum_v4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_Kmulti-row_fast_r4kernels shared one 4-row accumulator layout acrossrows=2..4and hit scalar-ALU / register-pressure bottlenecks atrows=3..8on large matrices (LM head2048 -> 248320, dense MLP2048 -> 8192).Q8_0multi-row decode fell back to scalar1r4/k4r4on large matrices (43–53 GB/son M4).poly_gemv_v4_r8allocated 8-row accumulators and shared memory even forrows=2..4, andmoe_sumran element-wise scalar loads/stores.This PR adds lossless 256-K
simdgroup_half8x8 -> simdgroup_float8x8MMA kernels (_mma256), register-specialized_fast_r2/r3/r4/r8kernels, vectorizedQ8_0fast/fast4decode kernels (preservingk4/k4r4/k4r8for small<= 1024-output matrices on high-core-count Apple10 so standalone Qwen4 Gated Residual2048 -> 256matrices never underfill), specializedpoly_gemv_v4_r2/r4, andfloat4-vectorizedpoly_moe_sum_v4(all pureengine/metal/changes, standalone onmain).Standalone Same-Day Interleaved A/B vs
origin/main(0794bc5vs PR #511b6ec657)Apple M5 Ultra (256 GB unified memory, 64-core Apple10, full-VRAM
10,496/10,496experts, Qwen4-35B-A3BUD-IQ2_XS, 256 tok,--kv q8_0):origin/main(0794bc5) r1 / r2b6ec657) r1 / r2--no-draft --window 1--window 2(MTP)--window 4(chained MTP)UD-Q2_K_XL--no-draft --window 1c3c7360)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
0f52617andb6ec657only touchis_apple10()guards; Apple9 M4 dispatch is identical toc3c7360.)origin/main(0794bc5) r1 / r2c3c7360) r1 / r2--window 2--window 4Measured Microbenchmark Results (
poly_mv_bench qwen4_bw2)Apple M4 (8-core Apple9, 120 GB/s), Release:
Q6_K 2048->248320 r=2 / r=3 / r=4Q5_K 2048->248320 r=2 / r=3 / r=4Q4_K 2048->248320 r=2 / r=3 / r=4Q8_0 2048->8192 r=2 / r=3 / r=4moe_sum n=2048 k=8 r=4opt=0)Apple M5 Ultra (64-core Apple10),
poly_mv_bench qwen4_bw2(opt=0vsopt=1):Q6_K 2048->248320 r=2:518.9 -> 1,110.6 GB/sQ8_0 2048->8192 r=2:848.4 -> 1,388.4 GB/sQ5_K 2048->248320 r=2:411.3 -> 860.7 GB/sQ4_K 2048->248320 r=2:403.5 -> 745.1 GB/smoe_sum n=2048 k=8 r=4:184.0 -> 254.0 GB/sApproach
poly_mulr2_{q4_K,q5_K,q6_K,q8_0}_mma256) inengine/metal/src/poly/quantized.{metal,cpp}:simdgroup_half8x8 -> simdgroup_float8x8MMA with exact integer-valuedhalfoperands and FP32 scales/mins/accumulation (NSG=4,NT=2, 64 outputs/TG).kind == 0) and batched multi-matrix (kind == 9,same_x) dispatches with tail bounds guards via hoistedmap_fast_group.use_mma256():min_no = is_apple10() ? 8192 : 2048, withQ4_K/Q5_Katrows >= 2(Apple9) /rows >= 3(Apple10),Q6_Katrows >= 3, andQ8_0atrows >= 4._fast_r2/r3/r4/r8) & VectorizedQ8_0(_fast,_fast4):NR=2forQ4_K/Q5_Kfast_r2,NR=4forQ6_K/Q8_0fast_r2,NSG=2).use_fast_mulr2()keeps< 2048-output matrices on 1-output/TG kernels (k4r4/k4r8forQ8_0 <= 1024,1r4forQ4_K/Q5_K/Q6_K), andqrun()keepspoly_mul_q8_0_k4for<= 1024-outputQ8_0matrices so small projections (2048 -> 256) stay fully saturated on 64-core GPUs.poly_gemv_v4_r2/r4/r8and Vectorizedpoly_moe_sum_v4inengine/metal/src/poly/basic.{metal,cpp}:poly_gemv_v4_r_t<MAXR>shrinks per-thread accumulators and threadgroup memory forrows=2..4.poly_moe_sum_v4usesfloat4loads/FMAs with an unrolledK=8fast path and 16-byte pointer +n % 4 == 0alignment guard.quantized_parity.cpp,basic_parity.cpp, andmv_bench.cpp(qwen4_bw2).Validation
Passed (exact PR head
b6ec657, Apple M5 Ultra 64-core Apple10)cmake --build engine/build-metal -j16— PASSctest --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)origin/main(0794bc5) on Qwen4-35B-A3BUD-IQ2_XS— PASS (+1.4%no-draft,+4.5%w2,+3.2%w4)Passed (Apple M4 8-core Apple9, commit
c3c7360, Apple9 dispatch identical tob6ec657)poly_basic_parity— PASS (0 mismatches)poly_mv_parity— PASS (0 mismatches)poly_quantized_parity— PASS (0 mismatches, max error<= 2.3e-5vs FP32 reference, 0 OOB guard violations)origin/main(0794bc5) on Qwen4-35B-A3BUD-IQ2_XS— PASS (+4.2%w2,+8.8%w4)git diff --checkandpython3 tools/test_dropped_paths.py— PASSFailed
Not Run
engine/metal/only).Limits & Default-Path Preservation
gpu::metal::optimize()(STRATA_METAL_OPT=0restores the unoptimized fallback path unchanged)._mma256,_fast_r2..r8,gemv_v4_r2..r8, andmoe_sum_v4kernels are numerically lossless (FP32 accumulation; exact integer operands in MMA tiles).