Skip to content

generate: residency-table uploads wait for their own copy before the non-blocking streams read the table - #1043

Closed
sergqwer wants to merge 1 commit into
Niko1221:mainfrom
sergqwer:pr/res-upload-order
Closed

sergqwer wants to merge 1 commit into
Niko1221:mainfrom
sergqwer:pr/res-upload-order

Conversation

@sergqwer

@sergqwer sergqwer commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Replaces #550. GitHub closed it on 2026-10-05, when my fork was made private by mistake: that took the fork out of the network for good, so it can no longer open pull requests. This is the same branch and commit (000f288), opened from a new fork; the discussion and the measurements are in #550.


Every upload of the expert residency table, host_res to d_res and each stage's, is a plain cudaMemcpy (src/program/generate.cpp, 9 call sites). The kernels that read the table run on streams created cudaStreamNonBlocking.

  • A pageable host-to-device cudaMemcpy may return once the data is staged, before its DMA has landed.
  • A non-blocking stream does not wait for the legacy stream the copy runs on.

So nothing orders a kernel enqueued right after an upload (the next verify window after an adaptive swap, a trim, a refill) behind the copy. That kernel could read a table half old, half new: an expert computed by both the GPU and the CPU, or by neither.

The table is ~100 KB and usually lands in microseconds, so this rarely fires. It is the same class as #532 and #536, where the tests' uploads raced their non-blocking streams. Ours made one of those tests fail in 6 of 20 runs.

Fix: the uploads go through res_put(), which waits for the copy on the current device's legacy stream (cudaStreamSynchronize(cudaStreamLegacy)): that copy only, not the device.

Measured on an RTX 5090, IQ2_XS (ISTA), --expert-cache 14900, v0.1.37 against this PR:

  • First-token logits after a 2K and a 32K prompt: byte-identical.
  • Decode (a 32K prompt, then up to 600 tokens, --stop-eos), 6 interleaved pairs: 15.82 ms a round against 15.93 (+0.7%, within noise). One more pair is left out: 19.80 ms on a different token path, a desktop stall.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VZy1yKaDDiA8a7svdwaHio

…non-blocking streams read the table

Every upload of the expert residency table (host_res -> d_res, and each stage's) was a plain cudaMemcpy: a pageable
copy on the legacy stream, which may return before its DMA has landed, while the kernels that read the table run on
streams created cudaStreamNonBlocking, which do not wait for the legacy stream. A kernel enqueued right after could
read a table half old, half new: an expert computed by both the GPU and the CPU, or by neither. The ~100 KB copy
lands in microseconds, so this rarely fires, but nothing ordered it. The uploads now go through res_put(), which
waits for the copy on the current device's legacy stream only. Same values, same output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZy1yKaDDiA8a7svdwaHio
@sergqwer

sergqwer commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Closing: this went into 0.1.40 from #550 (see the comment there). It was reopened here only because my old fork had gone private; thank you for taking it.

@sergqwer sergqwer closed this Oct 6, 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.

1 participant