Skip to content

[PD] Wait for prefill completion before Mooncake early KV transfer - #41395

Merged
ShangmingCai merged 5 commits into
sgl-project:mainfrom
salexspb:codex/mooncake-early-send
Oct 6, 2026
Merged

ShangmingCai merged 5 commits into
sgl-project:mainfrom
salexspb:codex/mooncake-early-send

Conversation

@salexspb

@salexspb salexspb commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

PD stack

  1. [PD] Wait for prefill completion before Mooncake early KV transfer (this PR)
  2. [PD][Spec] Give a PD decode's first EAGLE draft step a proposal distribution: #41783

Motivation

Under overlap scheduling with chunked prefill, the Mooncake sender can hand a chunk's KV to the transfer worker before that chunk's forward has finished writing it. This is a race in the sender and applies to any GPU or NIC. maybe_send_cached_prefix_chunk (disaggregation/prefill.py) runs while the next chunk is being launched, and it records _early_send_wait_event on the forward stream for exactly this case. However, MooncakeKVSender.send never forwards that event, and the transfer worker never waits on it. The NIC can therefore read KV that is still unwritten, or stale from an earlier request. The decode then receives zero or stale KV for most of a long prompt, and every transfer still reports success. The Mori path already waits on this event.

Modifications

  • MooncakeKVSender.send passes the event through the existing TransferKVChunk field.
  • transfer_worker synchronizes on the event before reading source KV, clears it, and re-checks the request status, so an abort during the wait still cancels the transfer.
  • A room that has already failed skips the wait.
  • The scheduler never blocks on the event, and chunks without an event keep their existing path.

Tests

test/registered/unit/disaggregation/test_mooncake_transfer_batching.py::TestMooncakeEarlySend (CPU, 6 tests) covers:

  • event propagation for final and non-final sends;
  • one-time consumption of the event;
  • queue preservation;
  • wait-before-read ordering;
  • abort during the wait;
  • the failed-room skip;
  • the no-event path.

Without the fix, the event and wait tests fail and the no-event test passes.

python3 test/registered/unit/disaggregation/test_mooncake_transfer_batching.py TestMooncakeEarlySend

Validation

Measured on MI355X with GLM-5.2 MXFP4 with FP8 KV, PD prefill TP4 → decode TP4 over Mooncake RDMA, using 491K-token cold prompts in 16K chunks:

Without this PR With this PR
Chunks the transfer worker read before their forward finished 25–29 of 30 waited out, 0 read early
Decode host KV rows received as zero (first cold request) 31.7M–34.2M of 38.3M, on 4 of 4 fresh servers 0
Long-context needle prompts (59K–101K tokens), HiSparse decode 0/6 correct 6/6 correct

Steady-state throughput on a multi-turn workload is unchanged (111.7 vs 111.8 output tok/s).


CI States

Latest PR Test (Base): ⏳ Run #37384567537
Latest PR Test (Extra): ⚠️ Not enabled -- add run-ci-extra label to opt in.
Latest PR Test (AMD ROCm 10): ❌ Run #37384567697

@salexspb
salexspb force-pushed the codex/mooncake-early-send branch from dad7422 to 69d3a60 Compare October 2, 2026 23:16

@ShangmingCai ShangmingCai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ShangmingCai

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Oct 5, 2026
TestMooncakeEarlySend drives the unbound MooncakeKVManager.transfer_worker
against a hand-rolled SimpleNamespace. That fake was complete when written,
but two changes have since landed on main that made transfer_worker read
attributes it does not carry:

  - sgl-project#42051 added self.state_layout_rejections, read under session_lock for
    every non-dummy req.
  - sgl-project#37077 added self.poison_deferred_ack_room(), called from the worker's
    except path.

Merging main into the branch picked up the new conn.py without touching the
fake, and the merge was clean because the two sides touch different files.
test_worker_waits_before_reading_kv and test_worker_preserves_no_event_path
then died with AttributeError on state_layout_rejections, which the except
handler turned into a second AttributeError on poison_deferred_ack_room.

Only the fixture was stale: the real manager initializes
state_layout_rejections on the prefill path in __init__ and inherits
poison_deferred_ack_room from CommonKVManager, so no shipping path was
affected.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@ShangmingCai ShangmingCai added the highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed label Oct 5, 2026
@ShangmingCai
ShangmingCai merged commit c683190 into sgl-project:main Oct 6, 2026
183 of 214 checks passed
@nvpohanh

nvpohanh commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

cc @weireweire @YAMY1234

ZitingLiu added a commit to pagoda-inference/sglang that referenced this pull request Oct 10, 2026
Align DP speculative prefill coordination and Mooncake early-transfer cleanup. The Mooncake cleanup semantics follow upstream sgl-project#41395. Validated five full 100k benchmark rounds for both GLM models through batch 128.
LinyuanLi0046 added a commit to LinyuanLi0046/sglang that referenced this pull request Oct 19, 2026
Port existing community lifecycle code onto b85c644.
Fixed upstream source: 0dc6de2.

Keep generation-tagged ABORT/ACK state across sender cleanup, count dequeued
and in-flight transfers, drain running transfer futures, poison uncertain
native failures, and retain decode KV plus metadata until ACK or the
community device-destination timeout. Resolve held releases before decode
scheduling gates and account for them in scheduler idle checks. Complete
the existing cached-prefix event handoff through the worker. Use shared
terminal-status helpers and socket locks for failure fanout and to prevent
Success after cancellation.

Migrated upstream implementations and provenance:
- 97dedd1 (sgl-project#35049): device deferred
  release, ACK tracking, future draining and worker drain integration.
- adca19c (sgl-project#35360): common ACK helpers,
  unique prefill rank and dequeue-before-status accounting.
- 8ef6d31 (sgl-project#40645): preserve pending
  drain ACK targets across sender cleanup.
- cd11037 (sgl-project#41023): default-on setting
  and explicit backend capability gate.
- 14ab74e (sgl-project#41402): ACK endpoint fanout.
- 2a1c477 (sgl-project#41404): defer all eligible
  failure paths and force-arm partial metadata publication.
- 8293f9a (sgl-project#37077): shared ABORT/ACK
  types, generation filtering and common target/poison state.
- 4195980 (sgl-project#43719): suppress drained
  ACK after uncertain native KV/state/aux failure.
- f9df334 (sgl-project#44386): pending decode
  releases keep the scheduler non-idle.
- c683190 (sgl-project#41395): cached-prefix
  ready-event forwarding and worker-side synchronization.
- 2a46cf2 (sgl-project#36612): terminal/cleared
  room handling, first failure cause and shared failure/status fanout.
- abddb1c (sgl-project#32541): state-send return
  propagation and per-cached-socket send locking.
- 05c7ebf (sgl-project#30545): staging_counted
  and outstanding accounting across worker requeue/completion.
- 8ad7641 (sgl-project#36160): common wait_event
  job field used by the early-send handoff.
- 5b5d721 (sgl-project#40703): worker cleanup
  precedes outstanding-entry removal.
- 348689d (sgl-project#38959): sender poll wrapper
  holds Success while counted transfers remain.

- 1a0250c (sgl-project#41607): check Failed
  after state transfer before continuing to aux.
- 8bf780d (sgl-project#41128): retain deferred
  release start_time/deadline tuple shape without its new metrics API.
- 4dc9cda (sgl-project#37454): decode-side
  backend capability check before deferring a failed request.

Baseline compatibility adaptations:
- Insert selected snippets into existing server_args/configuration and
  worker/listener layout; retain local request timing and transfer batching.
- Omit absent deferred-allocation/bootstrap ownership, role-switch teardown,
  host-receive and PP-consensus branches; import no placeholder subsystems.
- Keep baseline 30-second socket timeout, optional sender-table guards and
  three-frame Mooncake status wire format; serialize existing manager sends
  with the imported endpoint locks.
- Use release_kv_cache(req, tree_cache, is_insert=False), the old failed
  request release API, and the existing scheduler invariant-check entrypoint.
- Omit unavailable new deferred-release metrics without importing the full
  metrics refactor; retain existing local metrics and diagnostics.

No WELM/NPU layerwise additions are included. Device timeout behavior and
backend capability defaults match the pinned community source.

Validation: 48 external CPU protocol/worker/decode cases; source AST
comparison for unadapted helpers; Python syntax, isort and configured Ruff
checks; git diff --check. CPU fixtures remain outside this repository.
No NPU/RDMA hardware execution was available for this commit.
luke396 pushed a commit to Infrawaves/sglang that referenced this pull request Oct 31, 2026
…gl-project#41395)

Co-authored-by: Shangming Cai <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
(cherry picked from commit c683190)
luke396 added a commit to Infrawaves/sglang that referenced this pull request Oct 31, 2026
The sgl-project#41395 worker tests built their manager stub around the upstream
_staging_outstanding counter. spec-bp tracks queued chunks with
TransferLifetimeTracker leases instead, so the stub now opens and acquires
one lease per chunk and asserts the room drains.
LinyuanLi0046 added a commit to LinyuanLi0046/sglang that referenced this pull request Nov 12, 2026
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().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants