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

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 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 stack bfd5d1b):

  • 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, total peak memory footprint 8.39 GiB (while still keeping 3,294 routed experts in VRAM).
  • 16K NIAH recall: 119.1 tok/s prefill, 100% exact recall (AMBER-4921), gpu_inuse = 0.18 GiB on 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

  • 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 M4 16 GB, macOS, commit 265c374 on origin/main 7cef952):
    • Metal Release configure + build (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.
    • 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.
    • 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).
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host. Kept as Draft pending cross-backend CI (backends.yml / check.yml).

Limits

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

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