fix(core/kv): order host-KV copy streams behind the caller's default stream - #313
Open
Andrewxu313 wants to merge 1 commit into
Open
fix(core/kv): order host-KV copy streams behind the caller's default stream#313Andrewxu313 wants to merge 1 commit into
Andrewxu313 wants to merge 1 commit into
Conversation
…stream The worker runs the model on PyTorch's default stream (cudaStream_t 0). The host-KV view's copy streams come from the PyTorch pool and are cudaStreamNonBlocking, so nothing orders them implicitly. WaitForProducerStream returned early for a null producer handle, so: * a d2h prefill offload issued right after the kernel producing its source copied the buffer's PREVIOUS contents (Kimi-K3: the first sequence of every MLA layer's offload; measured host != source on all 8 ranks at the first wave, and NaN garbage after a re-configure freed the buffers); * a d2h decode append could read the token's K before it was written; * an h2d page load could land before the K-cache torch.zeros memset queued ahead of it and be wiped (all-zero KV -> constant-token loops). Record an event on the ISSUING thread's current stream at call time (RecordProducerEvent, no null-handle short-circuit) and make the copy stream wait on it inside the task, on all five worker-view copy paths and the three compressed-state paths. GPU regression tests make the producer deliberately slow (offload and load); the static test pins record-before-launch on every site and the absence of the old helper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Order the host-KV worker view's copy streams behind the caller's stream by recording a CUDA event on the issuing thread at call time (
RecordProducerEvent) and waiting on it inside the copy task (WaitForProducerEvent). Applied to all five worker-view copy paths (two h2d page loads, the prefill offload, the per-layer decode append, the batched decode append) and the threecompressed_state_host_manager.hpaths that shared the old helper. Adds a GPU regression test that fails onmainand a static contract test.Motivation
The copy streams come from
at::cuda::getStreamFromPool(cudaStreamNonBlocking). The worker runs the model on PyTorch's default stream, whosecudaStream_thandle is 0, andWaitForProducerStreamreturned early for a null producer handle — so no event ever ordered a copy behind the model's stream:torch.zerosmemset of a re-created K cache and be wiped.Measured on the Kimi-K3 512-request decode contract (2x8 H200): host readback != source for the first sequence of every admission wave on all 8 ranks; a collapsed row's host page == GPU page word-for-word with garbage only in the tail of one page (a partially written buffer). MMLU 64% vs 87% for the same prompts on SGLang. With the fix: 0/512 collapsed on the 512x128 probe (was 21/512) and MMLU 90.23% (462/512) on the frozen contract corpus.
Type of Change
model— add/extend model support (models/**+ registration seam + model kernels only)kernel— add/optimize a compute kernel (batchgen_kernels/**+ in-tree kernel dirs)core— change scheduling/serving/runtime scaffolding (the only type that may)fix— narrow bug fix (+ a regression test)infra— build / CI / packaging / scripts / Dockerdocs— documentation onlyFile changes
core/KV_Storage/host_paged_kv_worker_view.hRecordProducerEventon the issuing thread +WaitForProducerEventin every copy task; old null-handle helper removedcore/KV_Storage/compressed_state_host_manager.htests/integration/paged_kv/test_host_kv_copy_stream_ordering.pymain(1024 of 1024 elements differ)tests/test_host_kv_copy_stream_contract.pyChecklist
PR Merge Policy Contract — pre-merge checklist
git diff --stat origin/mainreviewed; every file traces to the task — no unrelated files (§3.1).model/kernelPR (§2.6).debug_* / check_* / scratch_* / tmp_*scripts added to a production package or repo root (§1.1). (bench_*is allowed.)test_*.pyadded inside the runtime package; tests are undertests/(§1.2, §2.1).BATCHGEN_*env-var debug guard; debug behavior is abatchgen_debugbatch flag (§1.3). A genuinely-needed new env var goes in its own PR.print()or commented-out code in the changed files (§1.4–§1.5). (logging.debug()is fine.)-f(§1.6).MODULE.mdupdated if the public API changed (§2.2). (No public API change.)bash .github/workflows/scripts/check-pr-hygiene.sh origin/main(PR_TYPE=fix): 0 blocking, 0 advisory.🤖 Generated with Claude Code