Skip to content

[Engine] metal: port opt-in BasinKV sparse KV offload (--basinkv) - #500

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

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-basinkv

Conversation

@NightRain233

@NightRain233 NightRain233 commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

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.cpp threw 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:

  1. Native Metal BasinKV pool & kernels: Quest INT8 + SphereIndex INT4 block indices, INT8 + WHT-INT4 host KV storage, UVA + staged non-UVA fallback, Metal Graph/ICB capture & replay, and plan_cap = max(transfer_cap, kMaxTransferCap) matching d72af68.
  2. Chunked attention ml_out on Metal (attention.{metal,cpp}, flash.metal) with zero-key safety for exact long-prefill chunk merging (merge_attn_piece).
  3. Full Inkling 2 BasinKV Metal integration (matching origin/main 4d7c4b3): BasinKvPool::d_gpu_to_cpu(), null-cache guards in ink_qkv/ink_qkv_win, and ink_attn<PER, G, 0, true> (*_hot) for hot_cap sparse attention.
  4. Qwen35 / Qwen4 / Engine balanced Metal BasinKV fixes: prevents Metal's KvFmt::kAuto -> kQ8_0 default from overriding BasinKV's FP16 hot slab, allocates part_out_/part_ml_ under --metal-memory balanced, and drops Qwen4's unused score_ scratch buffer when basinkv_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):

  • 128K context capacity (131,072 tokens) fits comfortably in 16 GB unified memory: GPU BasinKV 68.3 MiB (hot 44.0 + index 23.4 + meta 1.0) + pinned RAM 726.0 MiB, keeping 3,393 routed experts in VRAM.
  • True 16K NIAH recall: 119.1 tok/s prefill, peak footprint 8.39 GiB, 100% exact recall (AMBER-4921).
  • True 64K (65,536 tokens) NIAH recall (needle COBALT-7319 at ~50% depth): 60.1 tok/s prefill, 7.37 tok/s decode (9/9 MTP drafts accepted), peak memory footprint 9.56 GiB (8.47 GB max RSS, 0 swaps), PLE Direct 93.0% hit (246.30 MiB SSD reads), AGX gpu_inuse on exit 0.00 GiB, 100% exact recall (COBALT-7319).
  • True ~120K (122,880 tokens) NIAH recall (needle QUARTZ-6042 at ~50% depth): 28.6 tok/s prefill, 7.26 tok/s decode (7/8 MTP drafts accepted), peak memory footprint 9.57 GiB (8.51 GB max RSS, 0 swaps), PLE Direct 95.1% hit (338.86 MiB SSD reads), AGX gpu_inuse on exit 0.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

  • Strictly opt-in (--basinkv): when --basinkv is not passed, default Metal attention (HOT=false), KV allocation, and Qwen4/Qwen35/Inkling2 paths stay unchanged.
  • Adds comprehensive Metal parity test suite 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 2 ink_attend hot_cap parity vs FP32 CPU reference).

Validation

  • Passed (Apple M5 Ultra 256 GB, macOS, exact rebased commit 24ff792 on origin/main 0794bc5):
    • Clean Metal Release build (-DSTRATA_METAL_POLY=ON): 0 errors.
    • Full ctest -L poly --output-on-failure: 52 / 52 passed (100%) (including poly_basinkv_parity Tests 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.
  • Passed (Apple M4 16 GB, macOS):
    • Direct ./engine/build-metal/poly_basinkv_parity: Tests 0–10 all passed under set_scoped_residency(true).
    • End-to-end strata-poly on tiny-qwen35moe.gguf: short-context (--kv fp16 vs --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.
    • Physical 16 GB M4 true 16K (AMBER-4921), true 64K (65,536 tokens, COBALT-7319), and true ~120K (122,880 tokens, QUARTZ-6042) BasinKV NIAH: all 100% exact recall, 0 swaps, 0.00 GiB exit GPU memory.
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host. Kept as Draft per CONTRIBUTING.md pending maintainer review and run-backends cross-backend CI.

Limits

  • Strictly opt-in (--basinkv); default Metal runs do not activate BasinKV.
  • Cross-backend compilation on CUDA/HIP/SYCL/MSVC must be confirmed via run-backends CI before merging.

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