Skip to content

qwen27 metal: opt-in idle weight release with Metal's purgeable states corrected - #5040

Merged
ashhart merged 7 commits into
mainfrom
lead/qwen27-idle-release
Nov 26, 2026
Merged

ashhart merged 7 commits into
mainfrom
lead/qwen27-idle-release

Conversation

@ashhart

@ashhart ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

An opt-in idle weight release for Qwen3.8-27B on Metal, with Metal's purgeable states corrected.

What it does: TF_Q27_IDLE_RELEASE takes whole quiet seconds, from 0 through 86400. The default, 0, keeps the weights wired as now. With N > 0, after N quiet seconds the server:

  1. writes the target weights it prepared once, to a snapshot under the user's cache;
  2. takes them out of wired residency;
  3. marks their buffers Volatile, so macOS may reclaim the memory.

The next request makes every released buffer NonVolatile before any read, and reloads only the buffers the OS emptied, checking each one's SHA-256 against the snapshot. A buffer the OS kept was never written while released.

The fix over the first version:

  • MTLPurgeableState is KeepCurrent 1, NonVolatile 2, Volatile 3, Empty 4. The first version passed 2 on release, so nothing was ever discardable, and 1 on restore, so a discarded buffer never became non-volatile again.
  • zig/src/metal/purgeable.zig now names the states by their ABI numbers.
  • Release asks for Volatile. Restore asks for NonVolatile and reads the returned previous state to see whether the contents were discarded.
  • An extent stays stale until its reload succeeds, so a refused restore retries it in full.

Off is main: with the setting unset or 0, no release state exists, lane_activity.restore returns at once, and the idle turn runs exactly as main's lane_rounds.zig did. One commit only moves a comment in lane_host.zig to keep it at 600 lines.

Tests:

  • test-qwen27-idle-release-host pins the four ABI values.
  • A restore with one buffer kept and one discarded touches only the discarded one.
  • Both new tests fail on the first version.
  • The existing tests still pass: twenty release cycles with exact bytes and request tokens, corrupt- and short-snapshot retries, and allocation failures.

Checks so far:

  • All 25 Qwen27 host steps pass.
  • The macOS lines touched type-check.
  • fmt, AST and the line caps are clean; lean is 164, as on main.

Device check to come: a real idle cycle on a Mac, with the setting unset, explicitly 0, and 40. Tokens must equal the serial reference in each.

  • Synthena

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Device checks on an M3 Ultra Mac with Qwen3.8-27B-MLX-4bit, main against this head:

  • Setting unset: prompt and decode tokens equal to main in all 8 rows; speed level.
  • TF_Q27_IDLE_RELEASE=0: tokens equal to main; speed level.
  • TF_Q27_IDLE_RELEASE=2, served cells: tokens equal to main. Those cells keep the server busy, so this run alone never went quiet long enough to release.
  • TF_Q27_IDLE_RELEASE=2, a live idle cycle: one request, 12 quiet seconds, the same request, 12 more quiet seconds, and the same request again. Each quiet period logged target prepared extents purgeable_bytes=14412349440, and all three replies match main's reply byte for byte. macOS had memory to spare and emptied nothing, so the reload of a discarded buffer stays covered by the host tests.

Main's environment_test.zig is at 599 lines, so this PR's three rows wait for #5089, which moves the valid-forms table to its own file. We'll rebase after that lands.

  • Synthena

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner Author

Rechecked at 7690e36, after we moved this change onto main's new lane loop (it now lives in lane_host_run.zig), on the same M3 Ultra Mac with Qwen3.8-27B-MLX-4bit:

  • Setting unset: tokens equal to main in all 8 rows; speed level (1 and 4 streams, chat and code, greedy and sampled, all within 0.3%).
  • TF_Q27_IDLE_RELEASE=2, a live idle cycle: three identical requests 12 seconds apart. Each quiet gap logged purgeable_bytes=14412349440, and all three replies match main's byte for byte.

This goes into a landing train now. Thank you!

  • Synthena

qwen27_idle.zig imported qwen27_engine.zig, the native file that picks the
27B or the pinned 2B engine for a checkpoint; it exports no model, so the
macOS native Metal build failed on q.model.IdleRelease. The file now
imports the family as its neighbours do, @import("tensorfold").qwen27, whose
model.IdleRelease the idle release commit already made public. No other
change; behaviour is unchanged.
…parison

std.Io's clock returns nanoseconds as i96, but the idle release owner took
and stored them as i64, so the native Metal server's two calls failed to
compile on macOS. The owner now takes and keeps i96 timestamps, as the clock
returns them; the configured quiet interval stays i64 and widens in the
comparison. Values, comparisons and behaviour are unchanged.
…'s main

Main's zig/src/core/lane_host.zig is 600 lines and the idle release's field and restore call make it
601. Main's comment on the queue's foreground-before-background order moves onto the statement it
describes; no code changes.
…d NonVolatile with a discard check on restore

The Metal bridge passed raw 2 (NonVolatile) to release and raw 1 (KeepCurrent) to restore, each one
step off MTLPurgeableState, so released weights never became discardable and a restore never made
them non-volatile again. metal/purgeable.zig names the states as the Metal ABI numbers them (KeepCurrent
1, NonVolatile 2, Volatile 3, Empty 4): makeVolatile asks for Volatile, and makeNonVolatile asks for
NonVolatile and returns whether the previous state was Empty, the OS having discarded the contents.

Restore now makes every released buffer NonVolatile before any read, then reloads and checks the
SHA-256 of only the buffers whose contents were discarded; a buffer the OS kept holds the bytes that
were serialized. An extent stays marked for reload until its reload succeeds, so a refused restore
(short or corrupt snapshot) retries it in full. Off (TF_Q27_IDLE_RELEASE unset) nothing changes, and
tokens stay those of the serial reference: every byte a GPU command reads is the serialized one.

test-qwen27-idle-release-host gains purgeable.zig's ABI test, which fails with the old 2 and 1, and a
restore test with one kept and one discarded buffer, which fails with the old reread-everything restore.
@ashhart
ashhart force-pushed the lead/qwen27-idle-release branch from 7690e36 to 04022a4 Compare November 26, 2026 02:50
@ashhart
ashhart merged commit 4855124 into main Nov 26, 2026
2 of 12 checks passed
@ashhart
ashhart deleted the lead/qwen27-idle-release branch November 26, 2026 02:50
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