Repository navigation
[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
Conversation
…kip-weight, --gpu-route-top, --tail-renorm)
NightRain233
force-pushed
the
feat/metal-gpu-dynamic-pruning
branch
from
December 6, 2026 16:52
f36c013 to
262f6a6
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 memory-bandwidth-bound Apple Silicon GPUs (e.g., 8-core Apple M4 at 120 GB/s), MoE decode reads all top-
kselected experts every token (k=8onInkling-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 safeid < 0/w == 0.0fearly exits across downstream Metal kernels (includingorigin/main's grouped Qwen4/MoE kernelspoly_mmvq_ids_groupandpoly_mul_k1g). When none of these flags are passed (default), the execution path stays 100% byte-identical tomain.Measured speed-vs-quality sweep on
Inkling-2-Mini-UD-Q2_K_XL(16 GiB Apple M4Mac16,12decode speed on top of the fused decode path; 128-step teacher-forced top-1 agreement + hard math/logic/coding eval on Apple M5 Ultra119):s=4.5)LRUCacheexecedit_distanceexec0.00%22.13 tok/s100.0%0.8562B)--gpu-skip-weight 0.08 --tail-renorm21.25%23.52 tok/s+6.3%96.9%0.8564B)--gpu-skip-weight 0.08 --no-tail-renorm20.24%100.0%0.8485A)--gpu-route-top 6 --tail-renorm25.00%23.90 tok/s+8.0%99.2%0.8537B)--gpu-route-top 5 --tail-renorm37.50%24.45 tok/s+10.5%99.2%0.8491B)--gpu-skip-weight 0.10 --tail-renorm44.96%26.10 tok/s+17.9%96.9%0.8490B)--gpu-route-top 4 --tail-renorm50.00%27.47 tok/s+24.1%98.4%0.8572On 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-renormboosts cold-start decode from9.66 tok/sto12.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
engine/metal/src/poly/{basic.{cpp,metal},inkling.metal,quantized.metal}):poly_routepreserves its exact original fast path whenroute_top <= 0 && skip_weight <= 0.0f. When opt-in pruning is enabled, pruned slots receiveids[k] = -1andweights[k] = 0.0f, optional--tail-renormrescales remaining kept probabilities beforeescale/w_scale, and an optional device counterd_skip_cnt_records[n_skipped, n_used]atomically with zero per-token host sync.id < 0early exits inpoly_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, andw == 0.0f/id < 0guards inmoe_sum(poly_elementwisekind 7) andink_moe_*accumulators so uncomputed slots can never read OOB or turn uninitialized NaNs into0 * NaN = NaN.escaleparity & all 10 model architectures: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 interminal/data/settings-register.json.Engine::route_rows_card(...)across all 10route_card()overrides (deepseek4,flint,gemma4,glm5next,inkling2,kolibri1,laguna,lfm2moe,qwen35, andqwen4).Engine::expert_route_scale(il)(overridden inGemma4) so hostmoe()/moe_defer()evaluate relative thresholds and--tail-renormon pre-escaleprobabilities, matching GPUpoly_route.#if !defined(STRATA_GPU_METAL)inEngine::load()and inkernels.cu::route_rows()) when--gpu-skip-weightor--gpu-route-topis passed, while allowing--tail-renormon all backends for existing host--skip-weight/--tail-skip/--route-top.route_rows(...)inengine/include/strata/poly/kernels.hppkeep all existing callers source-compatible.Validation
Passed:
Local macOS 26.6.2 / Apple M4 16 GiB (
Mac16,12), commitf36c013(rebased ontomaindf2c3ff), all underMTL_DEBUG_LAYER=1(0 Metal API validation warnings):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->0mismatches (includestest_route_pruningTests 1, 2A–2D and Test 2E non-uniform/negative/zeroescaleordering parity)../engine/build-metal/poly_quantized_parity->0mismatches (includes pruned-1groupedpoly_mmvq_ids_group/_k1g, ungrouped_k1, and all--1NaN-poisoned tests)../engine/build-metal/poly_inkling_parity->all passed(pruned NaN-safety tests onQ4_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), commitf36c013:ctest --test-dir engine/build-pr -L poly --output-on-failure-> 51 / 51 passed (100%, 50.30s)../engine/build-pr/q36_metal_parity->0mismatches../engine/build-pr/poly_qwen4_parity->PASS: all Qwen4 Metal parity checks.Remote macOS / Apple M5 Ultra (
119), exact rebased commit262f6a6onorigin/main(0794bc5, preserving [Engine] metal: high-bandwidth short4/TG-LUT decode GEMV for Q5_K/Q6_K/Q4_K/IQ2/IQ3 #498'spoly_mul_k1/poly_mul_k1gwith uniformif (eid < 0) return;beforeLut::load,poly_routedegenerate-row NaN/-infguard, and bumpingkPolyEngineAbiRev2→3 forexpert_route_scale()):-DSTRATA_METAL_POLY=ON): exit 0.ctest -L poly --output-on-failure: 51 / 51 passed (100%, 56.78s) (includingpoly_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:
Not run:
kernels.hppandkernels.cusignatures were audited to match with default trailing arguments and non-Metal fail-fast guards, pending CI verification).Inkling-2-Mini; other 9 models includingQwen4andGemma 4were verified by kernel/routing unit tests and 51-testctest -L poly).Limits
--gpu-skip-weight/--gpu-route-top) is Metal-only for now and fails fast on CUDA/HIP/SYCL (--tail-renormworks on host pruning paths across all backends).--tail-renormis strongly recommended when pruning (omitting it degraded multi-step logic at 20% skip).