Repository navigation
[Engine] metal: fix <=24 GiB balanced expert budget with XNU reclaimable VM headroom - #499
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
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:4 GiBhost reserve (kMetalHostReserve) was sized for 32+ GB Macs, leaving insufficient budget on<= 24 GiBhosts after dense weights loaded.available_host_bytes()on macOS excludes reclaimable inactive anonymous pages, causingmetal_host_headroom()to report near-zero headroom once dense weights and GGUF mmap populated memory.fill_expert_cache()subtracted a second512 MiBgrowth cushion on top of the host reserve, wiping out the remaining expert slots on 16 GB Macs.This PR fixes the
<= 24 GiBbalanced Metal auto-budget:48%working-set cap on<= 24 GiBMacs and lowers the host reserve on<= 16 GiBMacs from4 GiBto1,536 MiB(overridable viaSTRATA_METAL_HOST_RESERVE_MIB).host_statistics64(HOST_VM_INFO64)for reclaimable VM headroom (free_count + purgeable_count + inactive_count, explicitly not double-countingspeculative_countwhich XNU already accounts for infree_countper<mach/vm_statistics.h>), gated by a hard wired > 55% of RAM OOM backoff that falls back immediately to conservativeavailable_host_bytes().fill_expert_cache()cushion on<= 24 GiBbalanced hosts from512 MiBto128 MiB.Measured on real 16 GB Apple M4 (
Qwen4-35B-A3B-UD-IQ2_XS, out-of-the-box--metal-memory balancedwith ZERO manual--vram-reserve-mibflags, integrated R3 stackbfd5d1b):peak memory footprint 7.56 GiB,gpu_inuse = 0.47 GiBafter exit — zero leak).--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 GiBbalanced Metal budget (engine/src/poly/engine.{hpp,cpp},engine/metal/poly_tests/memory_growth.cpp,terminal/data/settings-register.json).Approach
> 24 GiBMacs (32 GB / 64 GB / 128 GB / 256 GB M5 Ultra) completely unchanged (metal_small_ram_cap() == SIZE_MAX,kMetalHostReserve == 4 GiB,512 MiBinit cushion).strata::core::memory_limit_bytes() > 0(explicit memory limit disables VM reclaim expansion) and preservesorigin/main'sbell_pending_and allpoly.11changes.STRATA_METAL_HOST_RESERVE_MIBinterminal/data/settings-register.jsonsotools/check --only docspasses.Validation
6dbd185onorigin/main7cef952):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 formetal_small_ram_avail_from_vm,wired > 55%backoff,metal_host_headroom/metal_mind_memoryconsistency,128 MiBvs512 MiBinit cushion, andSTRATA_METAL_HOST_RESERVE_MIB).python3 tools/check --only docs: passed (6/6).python3 -m unittest tools.test_settings_register: passed (8/8).#if defined(STRATA_GPU_METAL)/#if defined(__APPLE__)guards balanced). Kept as Draft pending cross-backend CI (backends.yml/check.yml).Limits
--metal-memory balancedon<= 24 GiBApple Silicon Macs;--metal-memory legacyand> 24 GiBhosts keep existing behavior unchanged.