Skip to content

fix(qwen27): warn when Metal startup exceeds available memory - #2886

Draft
garvrao80 wants to merge 2 commits into
ashhart:mainfrom
garvrao80:fix/metal-startup-memory-warning
Draft

garvrao80 wants to merge 2 commits into
ashhart:mainfrom
garvrao80:fix/metal-startup-memory-warning

Conversation

@garvrao80

@garvrao80 garvrao80 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Qwen3.8-27B Metal startup can pass its fixed working-set budget while other processes hold memory. Before allocating the streams and prompt scratch, startup warns if the reservation exceeds available memory or macOS reports elevated pressure. The warning estimates smaller --context and --parallel settings using the existing startup cost calculation.

Available memory is free plus reclaimable inactive pages from HOST_VM_INFO64. Free pages already include speculative pages. The checked availableBytes helper sums the counts before converting them to bytes. A pressure-only warning says the current settings fit available memory, and estimates appear only when below the requested settings.

Checks on this head

  • Head commit d5e4b4e7c855f55edfbe1a1d344c42eae8f2cae6.
  • Lean checker Same checker on merge base and head, 386 inherited findings each, zero new findings.
  • Tests zig build -Dfixed-cpu test-qwen27-memory-host -j2 --test-timeout 5m --summary all passed 15/15 on Windows x86-64. The same 15 tests passed directly on Linux x86-64 with Zig 0.17.0. Zero failures or skips.
  • Review regression evidence The pressure-only regression failed on 93cce3888 with 13 passes and one failure. On this revision it passes. A planted control that ignores inactive pages fails the fixed-count helper regression with exit 1.
  • Cases covered Free/inactive page counts, 4 KiB and 16 KiB pages, zero memory, invalid page size, overflow, reclaimable cache under normal pressure, exact fit, one byte short, unknown readings, warning/critical pressure, drafted streams and maximum fitting context. Pressure-only formatting is tested at exact fit and with spare room.
  • Hosted Mac validation Run 43402842520 verified this exact source head on macOS 26.7.2 arm64 with Zig 0.17.0. zig build test-qwen27-memory-host test-qwen27-startup-host check-qwen27-speed-host -j2 --test-timeout 5m --summary all passed all 13 build steps and 19 tests (15 memory tests plus two startup tests in each of two environment settings). The actual startup/encoding object compiled. The live reader probe returned available_bytes=3553869824 pressure=1.
  • Independent host review jishnuvenugopal reviewed this exact head on an M1 Pro. All seven review-local tests passed, including boundary/reader checks and deliberate failure controls. The review found no additional issue in its bounded host scope. Review receipt. Full-model startup remains pending.
  • Formatting Full-tree zig fmt --check build.zig zig and git diff --check passed locally. The upstream format job passed on this head.
  • CI All eight upstream PR checks passed on this head, including formatting, Linux and Windows host tests, golden checks and the macOS dev-tools build. No GPU results and format results. The scheduled nightly jobs skipped on the PR event.
  • Not run Full Qwen3.8-27B model startup, token comparisons and latency checks remain pending. The maintainers will check a quiet Mac start and a start with memory held by another process after these review fixes.

The direct Linux test command is

zig test --dep metal --dep core --dep sys \
  -Mroot=zig/src/qwen27_memory_test.zig \
  -Mmetal=zig/src/qwen27_speed_metal.zig \
  --dep metal -Mcore=zig/qwen27_speed_core.zig \
  -Msys=zig/src/platform/sys.zig \
  --test-filter 'qwen27 memory:'

Receipt

  • Main against branch Host and regression results above. Full-model measurements are pending.
  • Boxes it reaches Qwen3.8-27B Metal startup before stream and prompt-scratch allocation.
  • New switch n/a, startup diagnostic.
  • Environment Base/head above, Zig 0.17.0, Windows and Linux x86-64 hosts. Hosted macOS arm64 validation is linked above. Host validation uses no checkpoint.
  • Paths and platforms The revised warning and available-memory helper pass Windows/Linux host tests and native macOS arm64 tests. The macOS available-memory reader ran successfully, and the actual startup/encoding object compiled. Full-model Metal startup remains pending.
  • Exactness Pending full-model comparisons. This revision changes startup diagnostics and their memory reading.
  • Main against branch on a checkpoint Pending, no suitable local Mac available.
  • Decode speed Pending full-model run.
  • Prompt speed Pending full-model run.
  • Licence n/a, no new weights or drafter.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • The pressure-only regression fails on the previous head and passes on this revision. Ignoring inactive pages also fails the helper regression.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Full-model Mac startup warning validated, with the device/model receipt completed.

@garvrao80

Copy link
Copy Markdown
Contributor Author

CI for head 93cce38 was cancelled before any steps ran. Both rerun attempts were denied because this account lacks the required repository rights. Could you rerun No GPU (42998735844) and Zig format (42998735775)? The PR remains draft for live Mac startup validation. All 13 memory tests pass on Windows and Linux, and the macOS cross-compilation checks pass.

@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

@garvrao80 both are rerunning now (No GPU and Zig format on 93cce388). Thanks for keeping it a draft until the live Mac start is checked.

  • Synthena (Ash's Agent)

@garvrao80

Copy link
Copy Markdown
Contributor Author

Mac host validation is complete for head 93cce38: all 17 tests and 13 build steps passed on macOS arm64. The actual startup/encoding object compiled, and the live memory probe passed. Run and logs: https://github.com/garvrao80/TensorFold/actions/runs/43085028298

Could a maintainer with a suitable Mac check a full Qwen3.8-27B model startup, including the warning output when applicable? The hosted runner has 7 GiB RAM, so the PR remains draft for that check.

The Zig format rerun was cancelled before any steps ran again. Could that also be rerun?

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @garvrao80, and thanks for the hosted Mac run. All seven No GPU jobs pass on 93cce388, the planted control is a good touch, and the placement is right: the warning comes after the fixed-budget refusal and before the frame and streams are allocated, and its estimates use the same rows, drafter taps and ring as the startup cost.

Two things before we check it on a full model:

  1. Free pages understate what macOS can hand back. memory_policy.free() (zig/src/platform/memory_policy.zig:11-19) reads free_count from HOST_VM_INFO. That count leaves out inactive pages (much of them cached files) and purgeable pages, which macOS reclaims when something asks for memory. On a Mac that has been up for a while, free is often only a few GB even when most of RAM could be reclaimed. Your hosted probe read 0.9 GiB free on a 7 GiB runner at normal pressure. So prompt_memory.zig:70 would warn on most healthy starts, and people learn to ignore a warning that always prints. It's the same reason we kept the refusal on the fixed budget. Our wording in Metal: serve has no memory admission, so a 262k-context start on a swapping Mac reserves 67 GB in seconds and the kernel watchdog panics #2761 said "free", which was loose, sorry. Please compare the reservation against memory macOS can make available: for example free plus inactive pages from host_statistics64 with HOST_VM_INFO64 (its free_count already includes speculative pages, so don't add those twice), or the kernel's available-memory level (kern.memorystatus_level, a percentage of hw.memsize). Keep the pressure check as it is, and have the message say "available" so it names what was measured. Please put the sum in a small function that a host test can check with fixed page counts, and keep a live-reader test like the one free() has now.
  2. Estimates that equal the current settings. When the warning fires on pressure alone and the reservation fits, Warning.format (prompt_memory.zig:105-110) prints "An estimated --parallel N fits" and "--context M fits" with the user's own values, then "Consider a smaller --context or --parallel". Your test at qwen27_memory_test.zig:55-58 pins exactly that case. Please print an estimate only when it is below the current setting. When neither is, say the settings fit the available memory and the warning is about pressure.

Once those are in, we'll run a full Qwen3.8-27B start on a Mac: one at default settings on a quiet machine, where the warning should stay silent, and one with memory held by another process, where it should print. That's the check this draft is waiting on, so you don't need a Mac for it.

On Zig format: both runs were cancelled before any step ran, so there's no result yet rather than a failure. The four changed files pass zig fmt --check here, and your next push runs the job again.

  • Synthena (Ash's Agent)

@jishnuvenugopal

Copy link
Copy Markdown
Contributor

Thanks, @ashhart, for clarifying available-memory accounting and the pressure-only message, and @garvrao80 for the Mac host receipt. I can review the revised page-count helper and warning-format tests once those changes are up; the full-model run stays with the maintainers.

@garvrao80, would you accept review proposal 6800143356 for that bounded follow-up?

@garvrao80 garvrao80 changed the title fix(qwen27): warn when Metal startup exceeds live free memory fix(qwen27): warn when Metal startup exceeds available memory Nov 13, 2026
@jishnuvenugopal

Copy link
Copy Markdown
Contributor

Thanks, @ashhart, for the page-accounting and pressure-only wording checks,
and @garvrao80 for addressing both. I completed the bounded host review of
d5e4b4e7c and found no additional issue in that scope.

Verified outcome

  • Why: the two requested revisions need capacity advice to stay distinct from
    pressure-only warnings (Receipt, source review).
  • What was done: checked the revised reader, warning paths and estimator
    boundaries with host-only oracles and deliberate failure controls.
  • Result: all seven review-local tests pass; both deliberate breakages are
    detected, and the original source is restored (Receipt, host checks).

Receipt

  • Environment: Apple M1 Pro, 16 GiB, macOS 26.6, Zig 0.17.0, Xcode 27.2;
    reviewed head d5e4b4e7c855f55edfbe1a1d344c42eae8f2cae6.
  • Source review: free plus inactive pages without double-counting speculative
    pages; failed/short reads remain unknown and release the host port. The
    warning still follows fixed-budget admission and precedes frame/stream
    allocation. Pressure-only messages omit unchanged-setting suggestions.
  • Host checks: four boundary tests cover 294 arithmetic inputs, 105 warning
    combinations, 1,296 budgets over 108 fixtures and 272 page/chunk boundaries.
    Three reader tests cover ordinary/empty/wide counts, failed/truncated reads
    and cleanup. All seven pass with zero skips. The search oracle reuses the
    existing cost formula; it is not a physical-model-fit measurement.
  • Controls: changing the context-search <= to < fails three boundary
    tests; removing host-port cleanup fails all three reader tests. Both changes
    were restored and all seven tests passed again.
  • SDK probe: compiled with warnings-as-errors; the 64-word request returned
    62 words, and the SDK's typed interpretation of that same payload agrees
    with the selected free/inactive fields. Page size was 16,384 bytes.
  • Registration: traced the existing memory tests into the ordinary test
    build graph. The seven extra tests above are review-local; I did not repeat
    the author's aggregate host run.
  • Exactness, decode and cold-prefill speed: not run; no checkpoint loaded.
  • Not run: full-model startup, held-memory stress, full local CI or device
    qualification. The quiet/default and held-memory startup gate remains with
    the maintainers; this host review does not replace it.

The pinned source merges cleanly with main 948e74858 and #3268 6808133d34;
the latter retains the warning at the same startup boundary. That is a
textual/source interaction check, not a compiled combined-tree result.

@ashhart

ashhart commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you @garvrao80, and thanks to @jishnuvenugopal for the bounded host review. Both asks are resolved: the reservation now counts reclaimable macOS pages, and the warning only prints an estimate below the current setting, otherwise saying the settings fit and the pressure is the issue. What remains is ours: a short rebase, because main added the Linux pressure import at the top of memory_policy.zig (we keep that line and add your readers below it), then the quiet and pressured full-model starts on our side before it lands. Nothing further is needed from you.

  • Synthena

@ashhart
ashhart force-pushed the fix/metal-startup-memory-warning branch from d5e4b4e to 175d6c9 Compare November 20, 2026 05:11

This branch has not been deployed

No deployments
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.

3 participants