Repository navigation
[Fix] disaggregation: Avoid extra GPU contexts with PD - #45678
Conversation
…device Prefill KV-transfer worker threads never selected a device, so their first device call ran on device 0. In Mooncake's transfer_worker that call is TransferKVChunk.wait_event.synchronize() (the early cached-prefix send waits on the in-flight forward), which creates device 0's primary context in the process: every prefill rank whose GPU is not device 0 opens a second, ~1 GiB context on another rank's GPU once it starts sending KV. Seen on a DeepSeek-V4.1 DEP2 prefill worker: nvidia-smi listed the DP1 scheduler on both GPUs, with the second context created at its first PD transfer. Bind the threads to kv_args.gpu_id through the platform layer: - common/utils: bind_thread_to_device() (current_platform.set_device; no-op where the platform does not implement it) and run_on_device(), a thread target that binds and then runs the worker. - mooncake: the transfer_worker threads start through run_on_device, and the transfer thread pools bind each thread via the executor initializer. - mori: the _transfer_worker threads (same wait_event.synchronize()) start through run_on_device. The worker functions are unchanged, so tests that drive them directly are unaffected. Adds a CPU unit test for the helpers. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This comment has been minimized.
This comment has been minimized.
- Rewrite the bind_thread_to_device docstring to state what it does and why, without the bug history. - Remove test_transfer_worker_device.py: it only mocks the platform on CPU and never exercises a real device. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
/tag-and-rerun-ci |
|
[by Claude Code] I've started babysitting the NVIDIA CI of this PR. I'll check it every 30 minutes until all NV pipelines pass. If a relevant job fails, I may rerun it, merge the latest Anyone can stop me at any time: just post a comment asking me to stop (for example, |
This comment has been minimized.
This comment has been minimized.
|
[by Claude Code] @trevor-m, the |
|
/rerun-failed-ci |
|
[by Claude Code] The relevant CI failure (https://github.com/sgl-project/sglang/actions/runs/39742586359/job/119914902681) is not caused by this PR. It is a known breakage on |
|
@ShangmingCai could you review this small change? Thanks! |
Picks up sgl-project#45855, which fixes the unrelated CI failure https://github.com/sgl-project/sglang/actions/runs/39742586359/job/119914902681 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
[by Claude Code] I've started babysitting the NVIDIA CI of this PR. I'll check it every 30 minutes until all NV pipelines pass. If a relevant job fails, I may rerun it, merge the latest Anyone can stop me at any time: just post a comment asking me to stop (for example, |
|
/rerun-failed-ci |
Picks up sgl-project#45461, which fixes the unrelated CI failure https://github.com/sgl-project/sglang/actions/runs/39963987853/job/120661771575 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
/rerun-failed-ci |
|
[by Claude Code] All NV pipelines have passed. |
|
@ShangmingCai could you review this? Thanks! |
…5678) Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Po-Han Huang <[email protected]> (cherry picked from commit 7fe5b12)
Backport the early-send wait from sgl-project#41395 (c683190). Pass the forward-stream event with its cached-prefix chunk and wait for it before reading source KV. Recheck the room after the wait to skip transfers aborted in the meantime. The scheduler can launch the suffix forward as soon as it enqueues the prefix. Adapt the worker device binding from sgl-project#45678 (7fe5b12). Bind each transfer worker to its rank's device on its first event wait, preserving the Ascend worker's CPU affinity. Ascend uses the existing transfer_to_npu mapping for torch.cuda.Event().
Summary
With PD disagg, an extra GPU context is accidently created on GPU0 by the other GPU workers which wastes about 1GB of gpu memory.
[by Claude Code]
Motivation
A PD prefill rank whose GPU is not CUDA device 0 opens a second CUDA context on device 0 the first time it sends KV
for a request with a cached prefix. That rank then holds about 0.6–1 GiB on another rank's GPU (656 MiB in the repro
below, about 1 GiB with DeepSeek-V4.1 attention-DP2 prefill). The memory is taken after that GPU's KV pool was sized
against its free memory, so it eats into the headroom left for activations and graphs.
Cause.
maybe_send_cached_prefix_chunk(disaggregation/prefill.py) records atorch.cuda.Eventon the forward stream for the early cached-prefix send.
transfer_workerthread callswait_event.synchronize()on that event before it reads the KV.context in the process.
_transfer_workerdoes the samewait_event.synchronize().Modifications
disaggregation/common/utils.pyadds two helpers:bind_thread_to_device(gpu_id)sets the calling thread's device throughcurrent_platform. It is a no-op onplatforms that do not implement
set_device.run_on_device(gpu_id, fn, *args)is a thread target that binds the thread, then runsfn(*args).transfer_workerthreads start throughrun_on_device(kv_args.gpu_id, ...).initializer=bind_thread_to_device._transfer_workerthreads start throughrun_on_device.The worker functions themselves are unchanged, so existing unit tests that drive them directly need no changes.
Repro (dummy weights, one 4-GPU node)
Prefill runs TP2 on GPUs 0,1, so rank 1 lives on GPU 1. Decode runs TP1 on GPU 2. The same ~2k-token prompt is
sent three times, so requests 2 and 3 hit the radix cache and take the early cached-prefix send.
On our nodes, Mooncake moved the KV over NVLink (
MC_FORCE_MNNVL=1 SGLANG_MOONCAKE_CUSTOM_MEM_POOL=True). The bugdoes not depend on the transport.
Before (main): requests 2 and 3 report
cached_tokens1984 of 2000, and the TP1 prefill scheduler now alsoholds a context on GPU 0.
The GPU 0 row for pid 684 is absent in
nvidia-smiright after startup; it appears with the first cached-prefixsend.
After (this PR), same commands and the same
cached_tokens: one context per process.Both runs had 0 KV-transfer failures, and every request completed.
Tests
test/registered/unit/disaggregation/test_transfer_worker_device.py(CPU, 3 tests). It checks thatbind_thread_to_devicecallscurrent_platform.set_device, ignoresNotImplementedError, and thatrun_on_devicebinds on the worker's own thread before the worker runs.
test_mooncake_transfer_batching.py(16),test_mooncake_deferred_kv_release.py(9),test_mori_deferred_kv_release.py(11),test_disaggregation_wire.py(79).current main.
Checklist
repro completed before and after (0 failures).
🤖 Generated with Claude Code
CI States
Latest PR Test (Base): ✅ Run #40033598784
Latest PR Test (Extra): ❌ Run #40033598750
Latest PR Test (AMD ROCm 10): ❌ Run #40033598646