Repository navigation
feat(core): add macOS memory guardrails for large resident models - #2909
Merged
Merged
Conversation
The fit check compared against total RAM and Metal's working set, neither of which says what macOS can give now without compressing or swapping. Add mlxcel_core::host_memory: the available-now figure (free + purgeable + min(inactive + speculative, file-backed)), the reserve of max(7 GiB, 15% of RAM), the process footprint, and AvailabilityGate for stores that grow in steps. inspect and --estimate-memory print these on macOS and warn with numbers, never refusing. The server logs the footprint after load and exports it on /metrics. MLXCEL_WIRE_WEIGHTS=1 optionally mlocks the evaluated weights, default off. The qwen4_exp PLE row cache now stops growing before crossing the reserve. scripts/macos_memory_soak.sh is committed but not run, so the soak numbers are pending and all defaults are unchanged. Validated with unit tests, a real inspect of the OptiQ checkpoint, and a short server run with wiring on. Refs #2883
The kernel's vm_statistics64 free_count already includes speculative pages, as vm_stat's own subtraction shows, so the available-now figure counted them twice. On this host that overstated it by 25 GiB, which could suppress the load warning and let the row cache grow past the reserve. Subtract them when mapping the kernel counters. The soak script now cleans up on normal exit and SIGHUP, runs dd in the background so a signal stops a write at once, removes the write file before waiting on the server, and bounds that wait. It refuses a port that already answers, clears inherited wiring variables for baseline runs, skips the write when no first token arrived, and treats reasoning deltas as the first token. Validated with host_memory unit tests, clippy on mlxcel-core, bash -n, --dry-run, and a cleanup trap exercise on sleep children. The soak was not run. Refs #2883
Member
Author
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
|
`array_data_address` plus `array_nbytes` describes an array's bytes only when the array is contiguous. A strided view spans more memory than its element count, and with a negative stride its address is not the start of the range, so `MLXCEL_WIRE_WEIGHTS` could wire the wrong pages. The bridge now returns 0 unless the array is row or column contiguous, and the existing zero-address filter drops those arrays from the wiring pass. Validated with the `weight_wiring` tests, including a new check on a column slice and a row slice, plus lib and test clippy with metal and accelerate. Refs #2883
Member
Author
|
Security and performance review of the memory guardrails: no CRITICAL or HIGH findings. Fixed: Checked with no change needed:
Open, LOW or MEDIUM, not fixed here:
|
The PR added documentation pages and environment variable rows but no CHANGELOG entry. The Unreleased Added section now describes the available-now figure and reserve, the footprint metrics, the optional weight wiring, the row cache growth gate and the unrun soak script, and states that nothing was measured and no default changed. Validation: cargo fmt --check, make verify-env-var-docs, host_memory, weight_wiring, execution::memory_availability and ple_cache test selectors, clippy --lib --tests, and cargo check --bin mlxcel --no-default-features all pass. Refs #2883
inureyes
marked this pull request as ready for review
November 9, 2026 16:33
inureyes
added a commit
that referenced
this pull request
Nov 10, 2026
## Summary Records the timing and memory measurements that #2908, #2906, #2912, #2909, #2911 and #2918 deferred to a quiet host, in `docs/benchmark_results/qwen4-exp-deferred-measurements-m3ultra-2026-11-10.md` (Mac Studio M3 Ultra, 512 GB, `main` @ `60a3270c`). - Native MTP (#2880, #2917): 1.30x / 1.10x / 1.76x (code / prose / enumeration) for the OptiQ 2-bit target with the bf16 split head, 1.38x / 1.00x / 1.74x for the 4-bit target with its embedded head. - SSD expert streaming (#2882): decode 42.8 tok/s resident, 23.0 at a 24 GB arena (89.8% hit), 18.3 at 8 GB (74.9% hit). Router sync costs 8.0 s per run at both sizes, more than the I/O wait. Output identical across arms. - External PLE (#2881): 31 GB less resident (78.5 to 47.3 GB), same TTFT at 4K and 32K, identical output in every mode. - Prompt-cache second turn (#2885): 0.51 to 0.60 s cached vs 15.1 s (16K) and 28.6 to 28.9 s (32K) after a reset, byte-identical in 10/10 repetitions. - macOS memory soak (#2883): 0 compressions and 0 swap-outs in every setting. A 512 GB host cannot show the eviction the issue targets. ## Script changes - `scripts/bench_speculative.sh`: new `flashnext` and `flashnext4` pairings. - `scripts/macos_memory_soak.sh`: send the `model` field (every request got HTTP 400 without it), and default `--out` gets a `-wire-weights` suffix under `--wire-weights` so it no longer overwrites the plain run. ## Not covered A Mac with less memory than the checkpoint, the `prepare-expert-stream` contiguous file, and streaming combined with the external PLE table. Refs #2880, #2881, #2882, #2883, #2885, #2917
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.
The fit check compared a load against total RAM and Metal's working set, neither of which says what macOS can give now without compressing or swapping. This adds the "available now" figure and the guardrails built on it.
Behavior
mlxcel inspectand--estimate-memoryprint, on macOS, total RAM, Metal working set, available now and the reserve of max(7 GiB, 15% of RAM), and warn with numbers when weights + KV + activations exceed available-now minus the reserve. TheFITSverdict is unchanged and nothing is refused. Strict mode was left out.phys_footprintand compressed bytes after load, and/metricsexports them with MLX active/cache and wired-weight gauges. Off macOS these families are absent.MLXCEL_WIRE_WEIGHTS=1mlocks evaluated weights in 1 GiB chunks, re-reading the system wired total after each chunk and unwiring the one that crosses RAM minus the reserve. It needs a small new bridge call returning an array's host address, used only for mlock and zero unless the array is row or column contiguous. Multimodal towers and repacks made at the first token are not wired.qwen4_expPLE row cache stops growing before crossing the reserve, charging each step with its bookkeeping and reusing one query for 250 ms. The prompt-cache stores are not gated: entries exist before insert and a byte budget bounds them, so there is no step to refuse.mlxcel_core::host_memoryholds the reusable figure andAvailabilityGatefor feat(moe): SSD expert streaming arena for MoE checkpoints larger than unified memory #2882 and feat(models): prompt-cache snapshots for qwen4_exp, then mid-prompt checkpoints for model-owned recurrent families #2885.Measure-then-keep
Timed runs and the soak are deferred to a quiet machine, so
MLXCEL_WIRE_WEIGHTSstays off,MLXCEL_WIRED_LIMITkeeps its default, and no load is refused.docs/macos-memory.mdsays nothing has been measured.Verification
mlockof an evaluated MLX buffer.mlxcel inspectof the OptiQ checkpoint printed the new section (no warning on this 512 GB host, so the warning path is unit-tested only). That run predates the review fix for double-counted speculative pages, which is covered by a fixed-input test and was confirmed againstvm_stat.qwen3.5-4b-4bitwith wiring on: 2.20 GiB wired, footprint in the log and/metrics.cargo check --bin mlxcel --no-default-features --features metal,accelerate, fmt,make verify-env-var-docs,bash -nand--dry-runof the script. Shellcheck is not installed. Linux was not compiled here.Unverified acceptance criteria
MLXCEL_WIRE_WEIGHTSsoak criterion: deferred, not adopted as a default.Closes #2883