Skip to content

Qwen 3.5 CUDA: kept prompt states reuse a freed pinned buffer of their size - #2841

Merged
ashhart merged 7 commits into
mainfrom
lead/cuda-pinned-spares
Nov 18, 2026
Merged

ashhart merged 7 commits into
mainfrom
lead/cuda-pinned-spares

Conversation

@ashhart

@ashhart ashhart commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Qwen 3.5 on CUDA pins a new host buffer (cuMemHostAlloc) for every kept prompt state it saves and every disk-recall blank, and unpins it when the state is dropped. Pinning is slow, and a cold 8K-32K prompt that saves states pays it on every save.

What changes:

  • A dropped state's pinned buffer goes to a spare list of at most two buffers, but only once no copy into or out of it can still be queued (the save's wait succeeded and no restore is still reading it). A stuck restore or a failed save still sends the buffer to the copy-source quarantine, as before.
  • The next save or recall blank of exactly that size takes a spare instead of pinning new memory. Qwen's saves sit on a 4,096-row grid, so equal sizes recur.
  • The prompt cache counts spares inside its budget and frees them before it evicts any state; the dense and MoE entry points both declare the hooks. Spares are freed with the engine after the prompt cache closes. There are never more pins or unpins than on main.

Exactness: the copies are byte-for-byte the same; only the buffer they land in differs, and every byte is written before it is read. Two cache edge cases can differ from main (never replies, only which states stay cached):

  • When admission is checking room and nothing is promised yet, a family with spares evicts states first and then refuses, where main refused at once.
  • An unplanned fallback keep is skipped when held states plus spares plus its bytes pass the budget (core/lane_cache_queue.zig), without trimming spares or checking whether it could reuse one first. Right after a keep, spares sit in exactly that room, so a fallback checkpoint main kept can now be skipped. The disk probe's spare (sized at max_rows, held until the first keep or recall trims) can trigger this on the first requests, as can a debug-only TF_IMPRINT_VERIFY read-back.

Checks: nine spare tests and one hook-wiring test on a fake driver that moves real bytes and hands out pinned memory from the testing allocator (a leak fails the test): reuse after drain, none before, a size mismatch pinning new memory, the cap, teardown, a recall blank, a real prompt cache reusing an evicted state's buffer, and a save/restore round trip matching a save into new memory in bf16 and fp8, contiguous and paged, with the reused buffer pre-filled with other bytes. Fifteen planted breaks each turned a test red. test-cuda-host, compile-cuda-tests -Dtarget=aarch64-linux-gnu, check-native-cuda-host, zig fmt --check and the lean count pass.

Still to run before landing: one GB10, Qwen3.8-27B, main against this branch with a small --prompt-cache-gib (2 GiB for 8K prompts, 5 GiB for 32K) so saves start reusing early, with --learn-conversations and then --spill-gib: token_sha256 equal and first-token time after the budget fills. #2113 measured a 1.6-2.1% ceiling for this kind of save cost.

For #2833.

  • Synthena (Ash's Agent)

@ashhart
ashhart force-pushed the lead/cuda-pinned-spares branch from 0ee0c87 to c5dcbe0 Compare November 12, 2026 14:28
@tacosta

tacosta commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

I’m preparing the dense MTP work for #2485 as an unpublished local branch. ATC reports a merge conflict in zig/src/families/qwen3_5/cuda_native.zig, where the MTP path and prompt-state buffer reuse both touch CUDA state handling. Should I plan to rebase and resolve the MTP branch after this PR lands, or do you prefer another order? No #2485 code has been pushed; the 2K TTFT gate is still under investigation.

@tacosta

tacosta commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Update after rebasing the #2485 MTP stack onto upstream main 6bc82ac. ATC still reports a conflict with #2841 in zig/src/families/qwen3_5/cuda_native.zig. I’m opening a draft PR from my fork so the MTP/TTFT work can be reviewed; it is not merge-ready. Would you prefer the MTP branch to be restacked after #2841 lands, or should we coordinate a combined resolution?

@ashhart

ashhart commented Nov 17, 2026

Copy link
Copy Markdown
Owner Author

/atc exchange 61feb30bb52e3b340c3fca87c75804e681614b0ae97909602022e9ab18cfb067

…2833)

Every host save and disk-recall blank pinned new memory (cuMemHostAlloc) and
every drop unpinned it. A dropped copy's buffer now goes to a list of at most
two spares on the target, once no copy into or out of it can be queued: the
save synchronized, and no restore still reads it (a stuck one still goes to
the copy-source quarantine). The next save or blank of exactly that size takes
it instead of pinning. The prompt cache counts the spares inside its budget
(snap_spare), frees them when it needs the room (snap_trim, now wired through
cuda_snaps.zig) and keeps a state in a spare without evicting more
(snap_reuses); the dense and MoE entries both declare the hooks. The spares
are freed with the engine, after the host closes the prompt cache.

The bytes copied are the same; only their buffer changes. Host tests on a
mock driver that moves real bytes: reuse after a drain, none before, another
size pins, the cap, trim and teardown, byte-equal saves and restores in every
layout, a recall blank, and the store reusing an evicted state's buffer.
… stage

Main's stage geometry reads first and whole; a geometry built field by field from undefined left them unset,
so deltas() read past the layer table and every kept-spares test crashed on today's main.
…o spares

saveCache takes the spare list now; the checker's three saves passed five arguments, so the Linux checker
(compile-qwen35-rounds-host, and tf-qwen35-cuda on a device) no longer compiled.
…imported inline, so cuda_native.zig stays under 600 lines
@ashhart
ashhart force-pushed the lead/cuda-pinned-spares branch from 0977ee9 to 573f97c Compare November 18, 2026 20:45
@ashhart
ashhart merged commit b357fc5 into main Nov 18, 2026
2 of 11 checks passed
@ashhart
ashhart deleted the lead/cuda-pinned-spares branch November 18, 2026 20:46
joshuaswarren pushed a commit to joshuaswarren/TensorFold that referenced this pull request Nov 19, 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