Skip to content

[Engine] metal: fix <=24 GiB balanced expert budget with XNU reclaimable VM headroom - #499

Draft
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-16gb-balanced-budget
Draft

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/metal-16gb-balanced-budget

Conversation

@NightRain233

Copy link
Copy Markdown
Contributor

Problem and result

On 16 GB Apple Silicon Macs running large MoE models such as Qwen4-35B-A3B (UD-IQ2_XS), --metal-memory balanced (the default on macOS) previously loaded 0 routed experts into VRAM even on a healthy 16 GB machine with ~50% free RAM:

  1. The default 4 GiB host reserve (kMetalHostReserve) was sized for 32+ GB Macs, leaving insufficient budget on <= 24 GiB hosts after dense weights loaded.
  2. available_host_bytes() on macOS excludes reclaimable inactive anonymous pages, causing metal_host_headroom() to report near-zero headroom once dense weights and GGUF mmap populated memory.
  3. fill_expert_cache() subtracted a second 512 MiB growth cushion on top of the host reserve, wiping out the remaining expert slots on 16 GB Macs.

This PR fixes the <= 24 GiB balanced Metal auto-budget:

  • Applies a 48% working-set cap on <= 24 GiB Macs and lowers the host reserve on <= 16 GiB Macs from 4 GiB to 1,536 MiB (overridable via STRATA_METAL_HOST_RESERVE_MIB).
  • Queries XNU host_statistics64(HOST_VM_INFO64) for reclaimable VM headroom (free_count + purgeable_count + inactive_count, explicitly not double-counting speculative_count which XNU already accounts for in free_count per <mach/vm_statistics.h>), gated by a hard wired > 55% of RAM OOM backoff that falls back immediately to conservative available_host_bytes().
  • Reduces the initial fill_expert_cache() cushion on <= 24 GiB balanced hosts from 512 MiB to 128 MiB.

Measured on real 16 GB Apple M4 (Qwen4-35B-A3B-UD-IQ2_XS, out-of-the-box --metal-memory balanced with ZERO manual --vram-reserve-mib flags, integrated R3 stack bfd5d1b):

  • Before (R2 balanced): 0 of 10,496 routed experts in VRAM (triggered 100% CPU offload).
  • After (R3 balanced): 4,045 of 10,496 routed experts in VRAM (3.8 GiB, 93.7% VRAM hit rate), 19.12 tok/s out-of-the-box (peak memory footprint 7.56 GiB, gpu_inuse = 0.47 GiB after exit — zero leak).
  • With 32,768-token context (--max-context 36864 --metal-memory balanced): 4,045 experts in VRAM, 165.1 tok/s prompt prefill, peak memory 8.82 GiB, exact NIAH recall.

Related discussion

Tracking issue: #472. Strictly isolated to the <= 24 GiB balanced Metal budget (engine/src/poly/engine.{hpp,cpp}, engine/metal/poly_tests/memory_growth.cpp, terminal/data/settings-register.json).

Approach

  • Keeps > 24 GiB Macs (32 GB / 64 GB / 128 GB / 256 GB M5 Ultra) completely unchanged (metal_small_ram_cap() == SIZE_MAX, kMetalHostReserve == 4 GiB, 512 MiB init cushion).
  • Respects strata::core::memory_limit_bytes() > 0 (explicit memory limit disables VM reclaim expansion) and preserves origin/main's bell_pending_ and all poly.11 changes.
  • Registers STRATA_METAL_HOST_RESERVE_MIB in terminal/data/settings-register.json so tools/check --only docs passes.

Validation

  • Passed (Apple M4 16 GB, macOS, commit 6dbd185 on origin/main 7cef952):
    • Metal Release configure + build (strata-poly, poly_memory_growth, poly_memory_test, poly_runtime_safety, poly_tiny_model_fixture): 0 errors, 0 warnings.
    • ctest --test-dir engine/build-metal -R '^(poly_memory_growth|poly_memory_test|poly_runtime_safety|poly_balanced_qwen35moe_protocol|poly_balanced_qwen_q8_protocol|poly_metal_defaults)$' --output-on-failure: 12/12 passed (including unit tests for metal_small_ram_avail_from_vm, wired > 55% backoff, metal_host_headroom/metal_mind_memory consistency, 128 MiB vs 512 MiB init cushion, and STRATA_METAL_HOST_RESERVE_MIB).
    • python3 tools/check --only docs: passed (6/6).
    • python3 -m unittest tools.test_settings_register: passed (8/8).
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host (#if defined(STRATA_GPU_METAL) / #if defined(__APPLE__) guards balanced). Kept as Draft pending cross-backend CI (backends.yml / check.yml).

Limits

  • Applies only to --metal-memory balanced on <= 24 GiB Apple Silicon Macs; --metal-memory legacy and > 24 GiB hosts keep existing behavior unchanged.
  • 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