Repository navigation
[Engine] metal: high-bandwidth short4/TG-LUT decode GEMV for Q5_K/Q6_K/Q4_K/IQ2/IQ3 - #498
Open
NightRain233 wants to merge 1 commit into
Open
NightRain233 wants to merge 1 commit into
NightRain233 wants to merge 1 commit into
Conversation
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 and result
On Apple Silicon (especially Apple9 / 10-core M4 with
120 GB/speak unified memory bandwidth),strata-polyMetal decode GEMV previously achieved only45–60 GB/sonQ5_K/Q6_K/Q4_Kdense projections and29–40 GB/sonIQ2_XS/IQ2_S/IQ3_XXSMoE expert projections (whileq4_gdn_state_reg_128_kernelsustained112.8 GB/son the same M4).This pure-
engine/metal/PR adds lossless high-bandwidth Metal decode GEMV kernels (numerically equivalent within1.5e-5/ 0 bit diffs across all 22 quant formats):short4multi-SIMDgroup decode GEMV forQ5_K,Q6_K, andQ4_Kacross both single-row (r=1,kind=0&kind=9native_mmvq_many) and multi-row MTP/draft verification (r=2..8).NR=4dispatch tuning: keeps Apple10 (M5 Ultra) threshold at8192outputs while lowering Apple9 (!is_apple10()) to2048outputs, with explicitnr == 1 && total_outputs <= 1024guarding on 1-output-per-TGpoly_mul_q8_0_k4(verified by 10NaN-sentinelQ8_0regression tests acrossno = {1024, 2048, 3072, 4096}forkind=0andkind=9).short4NSG=8, NR=8MoE kernels forIQ2_XS(8 KiB + 128 B TG),IQ2_S(8 KiB TG), andIQ3_XXS(2 KiB + 128 B TG) onpoly_mul_*_k1/_k1g.Measured on Apple M4 16 GB (macOS,
poly_mv_bench qwen4and realQwen4-35B-A3B-UD-IQ2_XS):poly_mv_benchmicrobenchmarks (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_XXSMoE (k1): +34% to +124% across shapes.--no-draft:13.95 -> 15.05 tok/s(+7.9%); realSTRATA_METAL_PROF=2:poly_mul_q5_K56.2 -> 84.0 GB/s,poly_mul_q6_K54.0–76.7 -> 83.7–95.0 GB/s.18.03 -> 19.04 tok/s(+5.6%);poly_mulr2_q6_K40.8 -> 56.1 GB/s,poly_mulr2_q5_K33.6 -> 45.8 GB/s.bfd5d1b):110.93 -> 111.24 tok/sno-draft,53/53Metalctest -L polypassing).Related discussion
Tracking issue: #472. Strictly scoped to
engine/metal/(5 files only); zero non-Metal backend files touched.Approach
unpack_k4_scales, 2-byte alignedpacked_ushort2for 210-byteblock_q6_K) and exactint16short4dot products (verified overflow-safe: max8*31*128 = 31744 <= 32767forQ5_K,4*128*62 = 31744 <= 32767forIQ3_XXS) with horizontal reduction cast toint.Q5_1NR=1policy and Apple108192threshold; precompiles every new kernel 1:1 inMmq::Mmq().native_mmvq_ids_gu_swiglu_q8(PR [Engine] qwen4/metal: fuse decode kernels, QSA score MMA, and overhaul Gated Residual #475) and negative expert-id doorbell guards (PR [Engine] metal: add MTLSharedEvent doorbell with defer-rank and watchdog-safe CB chunking #474) out of this PR so it merges cleanly and independently ontomain.Validation
fcb0728onorigin/main7cef952):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 tailsno={1,63,516,2052,2053},r={1,2,4,8},kind=9native_mmvq_many, MoE_k1/_k1gtails + guard columns, all 10Q8_0NaN-sentinelnr=4/k4checks, and 0 bit diffs across all 22 quant types inpoly_mv_parity)../engine/build-metal/poly_mv_bench 5 "12,13,14,17,18,22" qwen4 1: all casesOK(up to105.6 GB/son M4).engine/metal/**only).Limits
M5 Ultra) keeps the8192NR=4threshold based on prior measurements showing smaller dense matrices regress atNR=4on 80-core Apple10 GPUs; further per-family tuning on M1/M2/M3 Max/Ultra GPUs can be evaluated separately.