Repository navigation
[Engine] metal: port opt-in BasinKV sparse KV offload (--basinkv) - #500
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-basinkv
branch
from
December 6, 2026 16:32
265c374 to
24ff792
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
--basinkv(BasinKV sparse KV offload with Quest INT8 / SphereIndex INT4 GPU block indexing and INT8 / WHT-INT4 host KV tier) was previously available only on CUDA/HIP (engine/metal/src/poly/unported.cppthrew unsupported on Metal). On 16 GB Apple Silicon Macs, full KV caches at 64K–128K context exhaust unified memory and prevent long-context usage on large MoE models like Qwen4-35B-A3B.This PR ports BasinKV natively to Metal (
engine/metal/src/poly/basinkv.{metal,cpp}), including:plan_cap = max(transfer_cap, kMaxTransferCap)matchingd72af68.ml_outon Metal (attention.{metal,cpp},flash.metal) with zero-key safety for exact long-prefill chunk merging (merge_attn_piece).origin/main4d7c4b3):BasinKvPool::d_gpu_to_cpu(), null-cache guards inink_qkv/ink_qkv_win, andink_attn<PER, G, 0, true>(*_hot) forhot_capsparse attention.KvFmt::kAuto -> kQ8_0default from overriding BasinKV's FP16 hot slab, allocatespart_out_/part_ml_under--metal-memory balanced, and drops Qwen4's unusedscore_scratch buffer whenbasinkv_on().Measured on physical 16 GB Apple M4 (
Mac16,12,Qwen4-35B-A3B-UD-IQ2_XS, out-of-the-box--metal-memory balanced --ple-direct --max-context 131072 --basinkv --basinkv-index sphere --basinkv-kv-quant wht_int4, integrated stack):131,072tokens) fits comfortably in 16 GB unified memory: GPU BasinKV 68.3 MiB (hot44.0+ index23.4+ meta1.0) + pinned RAM 726.0 MiB, keeping 3,393 routed experts in VRAM.119.1 tok/sprefill, peak footprint8.39 GiB, 100% exact recall (AMBER-4921).65,536tokens) NIAH recall (needleCOBALT-7319at ~50% depth):60.1 tok/sprefill,7.37 tok/sdecode (9/9MTP drafts accepted), peak memory footprint9.56 GiB(8.47 GBmax RSS,0 swaps), PLE Direct93.0%hit (246.30 MiBSSD reads), AGXgpu_inuseon exit0.00 GiB, 100% exact recall (COBALT-7319).122,880tokens) NIAH recall (needleQUARTZ-6042at ~50% depth):28.6 tok/sprefill,7.26 tok/sdecode (7/8MTP drafts accepted), peak memory footprint9.57 GiB(8.51 GBmax RSS,0 swaps), PLE Direct95.1%hit (338.86 MiBSSD reads), AGXgpu_inuseon exit0.00 GiB, 100% exact recall (QUARTZ-6042).Related discussion
Tracking issue: #472. Rebased cleanly onto
origin/main(0794bc5), preserving Inkling 2 BasinKV (4d7c4b3),kMaxTransferCap(d72af68), the UMA iGPU BasinKV guard (b7255be/ #489), and #464.Approach
--basinkv): when--basinkvis not passed, default Metal attention (HOT=false), KV allocation, and Qwen4/Qwen35/Inkling2 paths stay unchanged.poly_basinkv_parity(Tests 0–10 covering Quest/SphereIndex, INT8/WHT-INT4, UVA/non-UVA staging, Metal Graph capture/replay,scoped_residency(true), and Inkling 2ink_attendhot_capparity vs FP32 CPU reference).Validation
24ff792onorigin/main0794bc5):-DSTRATA_METAL_POLY=ON): 0 errors.ctest -L poly --output-on-failure: 52 / 52 passed (100%) (includingpoly_basinkv_parityTests 0–10).python3 terminal/tools/test_setup_basinkv.py(6/6),python3 tools/test_dropped_paths.py,python3 tools/settings_register.py --check,python3 tools/check --only docs(6/6): all passed../engine/build-metal/poly_basinkv_parity: Tests 0–10 all passed underset_scoped_residency(true).strata-polyontiny-qwen35moe.gguf: short-context (--kv fp16vs--basinkv --basinkv-resident 256) bit-identical; long-context balanced run passed.c++ -std=c++20 -fsyntax-only -Wall -Wextra -DSTRATA_BASINKV_HOST_ONLY -Iengine/src/poly engine/src/poly/basinkv.cpp: passed.AMBER-4921), true 64K (65,536tokens,COBALT-7319), and true ~120K (122,880tokens,QUARTZ-6042) BasinKV NIAH: all 100% exact recall, 0 swaps,0.00 GiBexit GPU memory.CONTRIBUTING.mdpending maintainer review andrun-backendscross-backend CI.Limits
--basinkv); default Metal runs do not activate BasinKV.run-backendsCI before merging.