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
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 real 16 GB Apple M4 (
Qwen4-35B-A3B-UD-IQ2_XS, out-of-the-box--metal-memory balanced --max-context 131072 --basinkv --basinkv-index sphere --basinkv-kv-quant wht_int4, integrated R3 stackbfd5d1b):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, total peak memory footprint 8.39 GiB (while still keeping 3,294 routed experts in VRAM).119.1 tok/sprefill, 100% exact recall (AMBER-4921),gpu_inuse = 0.18 GiBon exit.Related discussion
Tracking issue: #472. Rebased onto
origin/main(7cef952,v0.2.20-poly.11), preserving Inkling 2 BasinKV (4d7c4b3),kMaxTransferCap(d72af68), and the UMA iGPU BasinKV guard (b7255be/ #489).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
265c374onorigin/main7cef952):strata-poly,poly_basinkv_parity,poly_qwen4_parity,poly_inkling_parity,poly_memory_growth,poly_runtime_safety,poly_attention_parity,poly_tiny_model_fixture): 0 errors, 0 warnings.ctest --test-dir engine/build-metal -R '^(poly_basinkv_parity|poly_qwen4_parity|poly_inkling_parity|poly_memory_growth|poly_runtime_safety|poly_metal_defaults)$' --output-on-failure: 12/12 passed.ctest --test-dir engine/build-metal -R '^(poly_attention_parity|poly_tiny_qwen35moe_protocol|poly_balanced_qwen35moe_protocol)$' --output-on-failure: 4/4 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.python3 terminal/tools/test_setup_basinkv.py: 6/6 passed.c++ -std=c++20 -fsyntax-only -Wall -Wextra -DSTRATA_BASINKV_HOST_ONLY -Iengine/src/poly engine/src/poly/basinkv.cpp: passed.python3 tools/check --only docs: passed (6/6).backends.yml/check.yml).Limits
--basinkv); default Metal runs do not activate BasinKV.