Skip to content

[Engine] optimize Metal Inkling decode fusion and Apple9 prefill GEMM - #458

Draft
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-inkling-decode-fusion
Draft

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-inkling-decode-fusion

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem and result

On Apple9 GPUs (e.g., 8-core Apple M4, 120 GB/s unified memory), strata-poly Metal decode and long-context prefill on Inkling-2-Mini left substantial hardware throughput unused:

  • Decode issued unfused gate_up -> float write -> SiTU -> Q8_1 quantize memory 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.
  • Prefill used large Apple10 poly_mm tiles (64x64x64) and coarse kTile={32,128} classes that under-occupied 8-core Apple9 GPUs (~81 tok/s at 4K on 16 GB M4 UD-Q2_K_XL).

After this change (100% mathematically lossless):

  • Apple M4 (Mac16,12, 16 GiB, Inkling-2-Mini-UD-Q2_K_XL, GPU-only):
    • Short decode improves from 18.6 tok/s (no-draft baseline) / 20.6–20.7 tok/s (after prefill/K1 stage) to 22.1–22.5 tok/s (+20% lossless decode gain; single-variable ladder on M4 confirmed +2.9% from low-VGPR k0/k9, +3.8% from on-chip SRAM gate_up+SiTU+Q8 fusion, and +1.9% from vectorized ctrl/gemv + fused output head + UMA zero-blit d_picks_).
    • GPU-only 4K prefill improves from ~81 tok/s (~51s) to 160.1 tok/s (25.1s, ~2.0× faster).
  • Apple M5 Ultra (119, Apple10 GPU, Inkling-2-Mini-UD-Q2_K_XL, no-draft):
    • Short/daily decode improves from 83.3–85.9 tok/s to 87.7–90.5 tok/s, while 4K/8K prefill remains 2,176–2,215 tok/s with zero regression on Qwen3.6 or Qwen4 Metal kernels.

Related discussion

Approach

  • On-chip SRAM MoE decode fusion (poly_mul_k1_gu_situ_q8):
    • Reads the quantized input vector once per expert row for both gate and up, applies optional per-expert scales and SiTU(beta, lb) in threadgroup registers/SRAM, and writes Q8_1 blocks directly to q8_out without writing intermediate g_/h_ floats (STRATA_METAL_FUSED_GU_SITU=0 disables).
    • Adds Resources::indirect_table2 residency, wave-hazard tracking, and ICB segment support in engine/metal/src/gpu/runtime.cpp + metal.hpp + launch.hpp for split-table safety.
    • When falling back to unfused situ_rows, skips the dead h_ float store (out=nullptr) on Metal since down reads q8_ directly.
  • Low-VGPR Apple9 decode kernels & custom quantized dots:
    • Adds custom Metal dot helpers (dot_q5_K, dot_iq3_s, dot_iq4_nl, dot_iq4_xs), a 2 KB Q2_0 LUT (kq2_0_lut), float4 dot() vectorization (q4_K, q5_K, q6_K, iq1_s, signed_grid4), 2-byte aligned uint16_t loads (iq2_s, iq3_xxs, iq1_s), and split poly_mul_f16.
    • Adds low-VGPR poly_mul_k1_nr (LPR=8/32, NR=2/4/8) for ungrouped Apple9 MoE decode, plus poly_mul_k0/poly_mul_k9 for dense/attention/LM-head decode, while keeping origin/main's grouped _k1g, poly_mul_q8_0_k4, split_q5_K_fast, and Qwen4 kernels completely intact.
  • Vectorized auxiliary decode ops & UMA zero-blit output:
    • Adds ink_control3_gate_v4 (STRATA_METAL_FAST_CTRL), 4-row/TG zero-barrier poly_gemv_v4 (STRATA_METAL_FAST_GEMV, leaving poly_gemv_v4_1/v4_r8 intact for Apple10 & Qwen4 no=1 / multi-row paths), fused Inkling2::output_rows_q8() (STRATA_METAL_FUSED_OUT_HEAD), and direct UMA host read of d_picks_ after stream sync (STRATA_METAL_UMA_PICKS).
  • Apple9 prefill GEMM & Flash Attention tuning:
    • Adds smaller non-indexed poly_mm tiles (64x64x32x4, 32x64x32x2, 16x64x64x2, 16x64x32x2) with Metal kTile={16,32} size classes, 16x64x64x2/GEMM64 defaults, and register-level simd_shuffle_xor reduction in ink_flash_128_g.
  • Cross-backend safety & settings register:
    • All shared-file hooks (native_mmvq.hpp, engine.{hpp,cpp}, inkling2.{hpp,cpp}) are guarded under #if defined(STRATA_GPU_METAL) with clean non-Metal inline/default fallbacks.
    • All 14 new STRATA_METAL_* env controls are registered in terminal/data/settings-register.json.

Validation

Passed:

  • Local macOS 26.6.2 / Apple M4 16 GiB (Mac16,12), commit c6f2f7e (rebased onto main df2c3ff), all run under MTL_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 of strata-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_head 200058, k9).
    • ./engine/build-metal/poly_inkling_parity -> all passed (situ_rows(out=nullptr), ink_control3_gate_v4, gu_situ_q8 fused/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 & tiles diff 0.
    • python3 -m unittest tools.test_settings_register -> Ran 8 tests in 0.749s — OK.
  • Remote macOS / Apple M5 Ultra (119), commit c6f2f7e:

    • Full Metal CMake build -> exit 0.
    • 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 commit 9953d89 on origin/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/_k1g kernels and bumping kPolyEngineAbiRev 2→3 for output_rows_q8()):

    • Clean Metal Release build (-DSTRATA_METAL_POLY=ON): exit 0.
    • Full ctest -L poly --output-on-failure: 51 / 51 passed (100%, 63.34s) (including poly_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:

  • none observed

Not run:

  • CUDA, HIP, and SYCL engine builds (no NVIDIA/AMD/Intel build hosts locally; shared-file changes in native_mmvq.hpp, engine.{hpp,cpp}, and inkling2.{hpp,cpp} are guarded under #if defined(STRATA_GPU_METAL) with non-Metal fallbacks, pending CI verification).
  • Apple M1/M2/M3 hardware benchmarks (Apple9 tile/K1 heuristics were tuned on Apple M4 and regression-verified on Apple M5 Ultra).

Limits

@NightRain233
NightRain233 force-pushed the feat/metal-inkling-decode-fusion branch from c6f2f7e to 9953d89 Compare December 6, 2026 16:50
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