Repository navigation
[Engine] optimize Metal Inkling decode fusion and Apple9 prefill GEMM - #458
Draft
NightRain233 wants to merge 1 commit into
Draft
NightRain233 wants to merge 1 commit into
NightRain233 wants to merge 1 commit into
Conversation
NightRain233
force-pushed
the
feat/metal-inkling-decode-fusion
branch
from
December 6, 2026 16:50
c6f2f7e to
9953d89
Compare
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 Apple9 GPUs (e.g., 8-core Apple M4, 120 GB/s unified memory),
strata-polyMetal decode and long-context prefill onInkling-2-Minileft substantial hardware throughput unused:gate_up -> float write -> SiTU -> Q8_1 quantizememory round-trips per MoE layer, ran high-VGPR kernels sized for larger Apple10 GPUs, and paid extra scalar GEMV / control-gate / output-head / blit-copy overhead.poly_mmtiles (64x64x64) and coarsekTile={32,128}classes that under-occupied 8-core Apple9 GPUs (~81 tok/sat 4K on 16 GB M4UD-Q2_K_XL).After this change (100% mathematically lossless):
Mac16,12, 16 GiB,Inkling-2-Mini-UD-Q2_K_XL, GPU-only):18.6 tok/s(no-draft baseline) /20.6–20.7 tok/s(after prefill/K1 stage) to22.1–22.5 tok/s(+20% lossless decode gain; single-variable ladder on M4 confirmed+2.9%from low-VGPRk0/k9,+3.8%from on-chip SRAMgate_up+SiTU+Q8fusion, and+1.9%from vectorized ctrl/gemv + fused output head + UMA zero-blitd_picks_).~81 tok/s(~51s) to160.1 tok/s(25.1s, ~2.0× faster).119, Apple10 GPU,Inkling-2-Mini-UD-Q2_K_XL, no-draft):83.3–85.9 tok/sto87.7–90.5 tok/s, while 4K/8K prefill remains2,176–2,215 tok/swith zero regression on Qwen3.6 or Qwen4 Metal kernels.Related discussion
Approach
poly_mul_k1_gu_situ_q8):gateandup, applies optional per-expert scales andSiTU(beta, lb)in threadgroup registers/SRAM, and writesQ8_1blocks directly toq8_outwithout writing intermediateg_/h_floats (STRATA_METAL_FUSED_GU_SITU=0disables).Resources::indirect_table2residency, wave-hazard tracking, and ICB segment support inengine/metal/src/gpu/runtime.cpp+metal.hpp+launch.hppfor split-table safety.situ_rows, skips the deadh_float store (out=nullptr) on Metal sincedownreadsq8_directly.dot_q5_K,dot_iq3_s,dot_iq4_nl,dot_iq4_xs), a 2 KBQ2_0LUT (kq2_0_lut),float4 dot()vectorization (q4_K,q5_K,q6_K,iq1_s,signed_grid4), 2-byte aligneduint16_tloads (iq2_s,iq3_xxs,iq1_s), and splitpoly_mul_f16.poly_mul_k1_nr(LPR=8/32,NR=2/4/8) for ungrouped Apple9 MoE decode, pluspoly_mul_k0/poly_mul_k9for dense/attention/LM-head decode, while keepingorigin/main's grouped_k1g,poly_mul_q8_0_k4,split_q5_K_fast, and Qwen4 kernels completely intact.ink_control3_gate_v4(STRATA_METAL_FAST_CTRL), 4-row/TG zero-barrierpoly_gemv_v4(STRATA_METAL_FAST_GEMV, leavingpoly_gemv_v4_1/v4_r8intact for Apple10 & Qwen4no=1/ multi-row paths), fusedInkling2::output_rows_q8()(STRATA_METAL_FUSED_OUT_HEAD), and direct UMA host read ofd_picks_after stream sync (STRATA_METAL_UMA_PICKS).poly_mmtiles (64x64x32x4,32x64x32x2,16x64x64x2,16x64x32x2) with MetalkTile={16,32}size classes,16x64x64x2/GEMM64defaults, and register-levelsimd_shuffle_xorreduction inink_flash_128_g.native_mmvq.hpp,engine.{hpp,cpp},inkling2.{hpp,cpp}) are guarded under#if defined(STRATA_GPU_METAL)with clean non-Metal inline/default fallbacks.STRATA_METAL_*env controls are registered interminal/data/settings-register.json.Validation
Passed:
Local macOS 26.6.2 / Apple M4 16 GiB (
Mac16,12), commitc6f2f7e(rebased ontomaindf2c3ff), all run underMTL_DEBUG_LAYER=1(0 Metal API validation warnings):cmake -S engine/metal -B engine/build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release -DSTRATA_METAL_POLY=ON -DMETAL_CPP_DIR=...& build ofstrata-poly+ 9 parity/bench targets -> exit 0 (0 compiler warnings)../engine/build-metal/poly_basic_parity->poly basic CPU-reference mismatches: 0../engine/build-metal/poly_quantized_parity-> 0 mismatches (including_k1g,mma/k4,DqStore)../engine/build-metal/poly_mv_parity-> 0 mismatches (22 types,k0+lm_head200058,k9)../engine/build-metal/poly_inkling_parity->all passed(situ_rows(out=nullptr),ink_control3_gate_v4,gu_situ_q8fused/split/ICB/neg-id)../engine/build-metal/poly_memory_growth->OK../engine/build-metal/poly_qwen4_parity->PASS: all Qwen4 Metal parity checks../engine/build-metal/poly_dflash2_metal_test->ALL SYNTHETIC TESTS PASSED../engine/build-metal/q36_metal_parity-> 0 mismatches (all 8 checks)../engine/build-metal/poly_runtime_safety->OK../engine/build-metal/poly_mm_bench 1 32 inkling-> all 6 shapes & tilesdiff 0.python3 -m unittest tools.test_settings_register->Ran 8 tests in 0.749s — OK.Remote macOS / Apple M5 Ultra (
119), commitc6f2f7e:ctest --test-dir engine/build-pr -L poly --output-on-failure-> 51 / 51 passed (100%, 56.76s)../engine/build-pr/q36_metal_parity-> 0 mismatches../engine/build-pr/poly_qwen4_parity->PASS: all Qwen4 Metal parity checks.Remote macOS / Apple M5 Ultra (
119), exact rebased commit9953d89onorigin/main(0794bc5, preserving [Engine] metal: high-bandwidth short4/TG-LUT decode GEMV for Q5_K/Q6_K/Q4_K/IQ2/IQ3 #498's high-BW_fast/_fast4/_k1/_k1gkernels and bumpingkPolyEngineAbiRev2→3 foroutput_rows_q8()):-DSTRATA_METAL_POLY=ON): exit 0.ctest -L poly --output-on-failure: 51 / 51 passed (100%, 63.34s) (includingpoly_mv_parity,poly_inkling_parity,poly_qwen4_parity,poly_quantized_parity,poly_basic_parity,poly_memory_growth,poly_metal_defaults).python3 tools/test_dropped_paths.py,python3 tools/settings_register.py --check,python3 tools/test_settings_register.py(8/8),python3 tools/check --only docs(6/6): all passed.Failed:
Not run:
native_mmvq.hpp,engine.{hpp,cpp}, andinkling2.{hpp,cpp}are guarded under#if defined(STRATA_GPU_METAL)with non-Metal fallbacks, pending CI verification).Limits
=0).