Skip to content

Qwen3.8-27B Metal: kept states reuse spare buffers, saves are not waited for, and slot 0's hand-over leaves the release path - #1304

Closed
jschmied wants to merge 3 commits into
ashhart:mainfrom
jschmied:q27-metal-host
Closed

jschmied wants to merge 3 commits into
ashhart:mainfrom
jschmied:q27-metal-host

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

Stacked on #1303: the first commit here is #1303's.

When a stream finished, its release ran on the engine thread at the end of a round:

  • its prompt-cache keep made a new private MTLBuffer and waited for the blit;
  • slot 0's hand-over (moveSlot) then blitted the next stream in, synchronously.

Prompt marks inside a prompt pass paid the same new-buffer-and-wait between chunks, and every other live stream's next round waited behind all of this.

A streamed reply's close (last token to the end of the response) was already about 0.4 ms on main, because the reply is out before the sweep runs. What this saves is engine-thread time the other streams were waiting on.

Receipts: same machine, models and test-only setting as #1303. All replies are identical to main; drafted == plain, concurrent == solo and resumed == fresh hold; zig build test passes on macOS and Linux; tf-qwen27-marks passes on real weights at every mark. The timing comes from an uncommitted timer around the same code in both builds, DFlash2, --parallel 2, over 10 saves (reply ends and prompt marks, up to a 6.8k-token state).

main this branch
a kept state's save median 9.41 ms, max 40.7 ms median 0.14 ms, max 0.33 ms
a finished stream's release from slot 0 (keep + hand-over) max 33.8 ms max 6.9 ms
slot 0's hand-over at the next round's entry (part of the release) max 4.8 ms
last token to response close 0.3-0.5 ms 0.3-0.5 ms

Design:

  • Spare pool: snapshot.Spares puts a dropped state's buffer in a four-entry pool. Buffers are sized n + n/8, in 32 MiB steps, and a save takes a spare up to 1.5x its size. The store sees the pool through charged, spare, trim and reuses, and the prompt-mark room check counts a reused buffer as no new bytes.
  • Saves don't wait: saveSlot commits its blit and returns, and the state holds the retained command buffer. State.landed() waits before any read of it: restore, the disk gather and drop.
  • Writers wait instead: Runner.reading[slot] makes the slot's next writer wait for the blit. Those writers are its reset (which covers rebinding, restore and the hand-over), the next prompt chunk, and the end of the prompt before its first round.
  • Why not an MTLEvent: every later 27B command buffer already commits and waits, so a deferred wait sits off the delivery path and usually finds the blit done. An event would need a wait in each of the runner's encoders.
  • Hand-over: release marks promote_due, and the move runs at the next draft, verify, queue or prefill call, once no round is pending.

Not covered: --learn disk writes of a just-saved state. The gather waits for the save first, but this wasn't exercised.

…ke rounds while a prompt fills

The prompt pass splits into an opening (restore, images) and one chunk a step on the same cut grid; cancel_prefill keeps
the last landed chunk. A stream mid-fill is never promoted into slot 0, and one image prompt fills at a time.
…e not waited for on the engine thread

A dropped state's buffer goes to a size-class spare pool (the store's charged/spare/trim/reuses). saveSlot commits its
blit; the slot's next write (reset, next prompt chunk, first round, move) and the state's first read wait for it.
@ashhart

ashhart commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Landed on main (94d9a6a..0941f86), stacked on #1303, under your authorship. Thank you! One conflict with #1262's change on main (cacheRules in qwen27_served.zig): we kept main's rules and added your four store hooks. On an M3 Ultra against main, tokens are identical and prefill, the sweep and chat resume are unchanged.

- Synthema, Ash's AI Assistant.

@ashhart ashhart closed this Oct 25, 2026
ashhart added a commit that referenced this pull request Oct 25, 2026
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.

2 participants