Skip to content

[Engine] add opt-in Metal GPU-native dynamic expert pruning (--gpu-skip-weight, --gpu-route-top, --tail-renorm) - #460

Draft
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-gpu-dynamic-pruning
Draft

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-gpu-dynamic-pruning

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem and result

On memory-bandwidth-bound Apple Silicon GPUs (e.g., 8-core Apple M4 at 120 GB/s), MoE decode reads all top-k selected experts every token (k=8 on Inkling-2-Mini), even when tail experts carry very low normalized routing weights (< 8–10%). Existing CPU-offload pruning (--skip-weight / --tail-skip) only skips uncached host-side experts and does not apply to fully VRAM-resident GPU routing on Metal (moe_card()).

This PR adds strictly opt-in, zero-CPU-sync GPU-native dynamic expert pruning directly inside Metal poly_route (--gpu-skip-weight, --gpu-route-top, --gpu-skip-min, --tail-renorm), with safe id < 0 / w == 0.0f early exits across downstream Metal kernels (including origin/main's grouped Qwen4/MoE kernels poly_mmvq_ids_group and poly_mul_k1g). When none of these flags are passed (default), the execution path stays 100% byte-identical to main.

Measured speed-vs-quality sweep on Inkling-2-Mini-UD-Q2_K_XL (16 GiB Apple M4 Mac16,12 decode speed on top of the fused decode path; 128-step teacher-forced top-1 agreement + hard math/logic/coding eval on Apple M5 Ultra 119):

Config Skipped experts M4 Decode vs Lossless 128-step Top-1 Agree Mean Gold Prob Math Trap (s=4.5) Liar Logic LRUCache exec edit_distance exec
Default (off, 8/8) 0.00% 22.13 tok/s baseline 100.0% 0.8562 PASS PASS (B) PASS PASS
--gpu-skip-weight 0.08 --tail-renorm 21.25% 23.52 tok/s +6.3% 96.9% 0.8564 PASS PASS (B) PASS PASS
--gpu-skip-weight 0.08 --no-tail-renorm 20.24% — — 100.0% 0.8485 PASS FAIL (A) PASS PASS
--gpu-route-top 6 --tail-renorm 25.00% 23.90 tok/s +8.0% 99.2% 0.8537 PASS PASS (B) PASS PASS
--gpu-route-top 5 --tail-renorm 37.50% 24.45 tok/s +10.5% 99.2% 0.8491 PASS PASS (B) PASS PASS
--gpu-skip-weight 0.10 --tail-renorm 44.96% 26.10 tok/s +17.9% 96.9% 0.8490 PASS PASS (B) PASS PASS
--gpu-route-top 4 --tail-renorm 50.00% 27.47 tok/s +24.1% 98.4% 0.8572 PASS FAIL (stalled) Class PASS PASS

On partial-VRAM Inkling-2-Mini-GSQ-RCO-IQ3_S (11.4 GB, ~2,563/3,072 experts in VRAM) on 16 GiB M4, --gpu-skip-weight 0.08 --tail-renorm boosts cold-start decode from 9.66 tok/s to 12.03 tok/s (+24.5%) by also cutting uncached CPU expert misses (36.9 -> 23.7 ms/tok) and VRAM swaps (254 -> 104).

Related discussion

Approach

  • GPU-native routing & downstream safety (engine/metal/src/poly/{basic.{cpp,metal},inkling.metal,quantized.metal}):
    • poly_route preserves its exact original fast path when route_top <= 0 && skip_weight <= 0.0f. When opt-in pruning is enabled, pruned slots receive ids[k] = -1 and weights[k] = 0.0f, optional --tail-renorm rescales remaining kept probabilities before escale/w_scale, and an optional device counter d_skip_cnt_ records [n_skipped, n_used] atomically with zero per-token host sync.
    • Added id < 0 early exits in poly_mul (kind == 1), poly_mul_k1, poly_mmvq_ids_group (valid = tid < count && my_id >= 0, g->start[ng] = s_sg[0] + s_sg[1]), poly_mul_k1g, poly_expert_rows, ink_situ, ink_situ_half, and w == 0.0f / id < 0 guards in moe_sum (poly_elementwise kind 7) and ink_moe_* accumulators so uncomputed slots can never read OOB or turn uninitialized NaNs into 0 * NaN = NaN.
  • Engine, CLI, Gemma 4 escale parity & all 10 model architectures:
    • Added Options::{gpu_skip_weight, gpu_route_top, gpu_skip_min, tail_renorm}, CLI flags, and env overrides (STRATA_GPU_SKIP_WEIGHT, STRATA_GPU_ROUTE_TOP, STRATA_GPU_SKIP_MIN, STRATA_TAIL_RENORM), all registered in terminal/data/settings-register.json.
    • Wired Engine::route_rows_card(...) across all 10 route_card() overrides (deepseek4, flint, gemma4, glm5next, inkling2, kolibri1, laguna, lfm2moe, qwen35, and qwen4).
    • Added Engine::expert_route_scale(il) (overridden in Gemma4) so host moe() / moe_defer() evaluate relative thresholds and --tail-renorm on pre-escale probabilities, matching GPU poly_route.
    • Fail-fast on non-Metal builds (#if !defined(STRATA_GPU_METAL) in Engine::load() and in kernels.cu::route_rows()) when --gpu-skip-weight or --gpu-route-top is passed, while allowing --tail-renorm on all backends for existing host --skip-weight / --tail-skip / --route-top.
    • Default trailing parameters on route_rows(...) in engine/include/strata/poly/kernels.hpp keep all existing callers source-compatible.

Validation

Passed:

  • Local macOS 26.6.2 / Apple M4 16 GiB (Mac16,12), commit f36c013 (rebased onto main df2c3ff), all under MTL_DEBUG_LAYER=1 (0 Metal API validation warnings):

    • CMake configure & build of strata-poly, poly_basic_parity, poly_quantized_parity, poly_inkling_parity, poly_qwen4_parity, poly_route_metal_test -> exit 0 (0 warnings).
    • ./engine/build-metal/poly_basic_parity -> 0 mismatches (includes test_route_pruning Tests 1, 2A–2D and Test 2E non-uniform/negative/zero escale ordering parity).
    • ./engine/build-metal/poly_quantized_parity -> 0 mismatches (includes pruned -1 grouped poly_mmvq_ids_group / _k1g, ungrouped _k1, and all--1 NaN-poisoned tests).
    • ./engine/build-metal/poly_inkling_parity -> all passed (pruned NaN-safety tests on Q4_K & NVFP4).
    • ./engine/build-metal/poly_qwen4_parity -> PASS: all Qwen4 Metal parity checks.
    • ./engine/build-metal/poly_route_metal_test -> 0 picks differ.
    • python3 -m unittest tools.test_settings_register -> Ran 8 tests in 0.795s — OK.
  • Remote macOS / Apple M5 Ultra (119), commit f36c013:

    • Full Metal CMake build -> exit 0.
    • ctest --test-dir engine/build-pr -L poly --output-on-failure -> 51 / 51 passed (100%, 50.30s).
    • ./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 262f6a6 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 poly_mul_k1/poly_mul_k1g with uniform if (eid < 0) return; before Lut::load, poly_route degenerate-row NaN/-inf guard, and bumping kPolyEngineAbiRev 2→3 for expert_route_scale()):

    • Clean Metal Release build (-DSTRATA_METAL_POLY=ON): exit 0.
    • Full ctest -L poly --output-on-failure: 51 / 51 passed (100%, 56.78s) (including poly_basic_parity, poly_inkling_parity, poly_quantized_parity, poly_qwen4_parity, poly_memory_growth, poly_metal_defaults).
    • python3 tools/test_dropped_paths.py, python3 tools/settings_register.py --check, 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; kernels.hpp and kernels.cu signatures were audited to match with default trailing arguments and non-Metal fail-fast guards, pending CI verification).
  • End-to-end quality sweeps on non-Inkling models (speed/quality sweep was measured on Inkling-2-Mini; other 9 models including Qwen4 and Gemma 4 were verified by kernel/routing unit tests and 51-test ctest -L poly).

Limits

@NightRain233
NightRain233 force-pushed the feat/metal-gpu-dynamic-pruning branch from f36c013 to 262f6a6 Compare December 6, 2026 16:52
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