Skip to content

[Refactor] deepseek-v4: Use explicit stage boundaries and row transport - #43607

Merged
ch-wan merged 1 commit into
mainfrom
cheng/refactor/layer-boundary-stack
Oct 15, 2026
Merged

ch-wan merged 1 commit into
mainfrom
cheng/refactor/layer-boundary-stack

Conversation

@ch-wan

@ch-wan ch-wan commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

This PR was part of the following stack (oldest at bottom; all merged):

Motivation

DeepSeek V4/V4.1 CUDA execution manages mHC reads, residual updates, deferred sums and MoE operand movement in model-specific forward paths. Explicit boundary declarations let the same producer completion reach ordinary consumers, PP transfers, graph exits and draft-model exits while preserving the original residual rows.

Modifications

Use a frozen msgspec.Struct for layer invocation metadata and explicitly declare the terminal mHC reader stateless.

Route CUDA attention and MoE outputs through mHC read/update adapters and compute-row transport. Keep DeepSeek-specific gather, return, shared-expert finalization and TBO events/buffers in models/deepseek_common/moe_transport.py. The generic layer_boundary/row_transport.py defines the source/compute/returned-row contract and completion lifecycle; V4 is currently its only model-family user.

Select explicit CUDA/non-CUDA decoder subclasses. Non-CUDA tuple/HcState and fused execution are grouped in deepseek_v4_platform_stage.py, retaining their existing numerical protocols. Include NextN, DSpark, PP receiving stages, late-layer DP and breakable-graph tail handling. Preserve update_context through both StageBoundary.finish and StageBoundary.exit into the row-transport update binder; bind it once for deferred and immediately applied updates.

Operation CUDA location
Attention output all-reduce TransportExit.finish / complete_owed, or consumer read completion fusion
DP operand gather MoeRowTransport.__init__, bound during stage prepare
MoE all-reduce / shared-expert finalization MoeFinalize.complete
DP scatter / reduce-scatter / variable reduce-scatter MoeRowTransport.return_output
mHC post Consumer read completion, TransportExit._write, or FinalRead
TBO issue / wait Model transport helpers and V4 op strategy
Main method / entry CUDA implementation Non-CUDA implementation
Decoder forward / forward_hc_pre_from_prev DeepseekV4CudaDecoderLayer.forward + HcRead / HcUpdate NonCudaDecoderMixin.forward / forward_hc_pre_from_prev
_run_moe_ffn_dp_sync MoeTransport / MoeRowTransport / MoeFinalize NonCudaDecoderMixin._run_moe_ffn_dp_sync using shared transport
TBO mHC prepare / post operations CUDA decoder ops + boundary adapters Same ops on NonCudaDecoderMixin
TBO gather / combine operations Shared DeepSeek transport helpers Platform ops using shared transport helpers
DSpark stage forward / FFN CUDA DSpark stage + boundary completion NonCudaDSparkMixin
AMD fused model loop Not used Platform fused-boundary functions called by AMD model helpers

Accuracy Tests

  • Before this restack: affected CPU suite 470 passed, 21 skipped, 7,363 subtests; H200 Hopper/cache/dual-stream GPU tests 18 passed, 32 deselected, 2 subtests.
  • GB300 comparisons against fixed main 72730d6360 matched for V4 TP2 decode graph, V4.1 TP4/DP4 late-layer shared experts, V4.1 DSpark serial decode graph and V4 NextN serial decode graph.
  • A fresh main/final TP4/EP4/DP2 BF16 DeepGEMM comparison matched with eight actual TBO executions on each side. DP-padded child rows were even. Without DP and with attention gather disabled, the scheduler did not prepare TBO children, so this is not evidence of an uneven-row TBO execution.
  • All GPU results above used reduced synthetic models with dummy weights and real kernels/collectives. They predate this restack. Model-specific adapter files are unchanged. The latest revision corrects shared reader callback binding and row-transport update-context forwarding, with CPU regressions; the older GPU runs do not validate these new API fixes. Current CPU and hooks results are recorded below.

Limits and Follow-up

  • DSpark attention completion uses the attention-TP group when 1 < attn_tp < tp; main may use the full TP group. Reachability and complete numerical validation of that non-default configuration remain open, so universal numerical equivalence is not claimed.
  • Model, DSpark and NextN still select different platform call signatures/state at some call sites. Non-import _is_cuda lines remain 31 / 7 / 2 respectively. A unified single-layer calling contract and platform naming cleanup remain follow-up work; this PR does not claim the CUDA path's overall complexity is reduced.
  • CUDA transport scope still covers return/update, while the non-CUDA synchronous scope ends after compute. Static sum guards, bound-update TBO splitting and per-call next-boundary construction also remain open.
  • Non-CUDA hardware, full-weight accuracy, PD + CP4 and formal performance acceptance are unverified. CPU orchestration coverage does not substitute for AMD/NPU/XPU hardware validation.

Speed Tests and Profiling

No speedup or formal no-regression claim. Existing synthetic-model timing samples and host profiles are diagnostic only.

Current Stack Validation

All three updated adapter commits passed pre-commit independently; the two K3 commits are unchanged. The final stack CPU suite passed 481 tests and 7,409 subtests, with 21 hardware-specific skips. Independent common-layer and Inkling boundary suites passed 372 tests / 7,315 subtests and 386 tests / 7,329 subtests, respectively.

Four new regressions fail on the prior implementation and pass with the fixes. They cover terminal export releasing state retained by an old output handle (including a failing final norm), terminal reader binding for written/pending/fused output with two contexts, rejection and cleanup when binding changes read capabilities, and rejection of bindable update results on both immediate and deferred exit paths. Existing tests cover the converted containers, short-convolution positions and stateless V4 terminal reads.

The earlier callback-selection, transformed-reader and row-transport context regressions remain covered. No GPU tests or formal performance benchmarks were rerun for these fixes; prior GPU evidence retains its stated limitations.

Landing

Rebased onto main 3a835cf417 for merge. Validation above predates this rebase; no tests or benchmarks were rerun as part of landing.


CI States

Latest PR Test (Base): 🚫 Run #38710372685
Latest PR Test (Extra): 🚫 Run #38710372266
Latest PR Test (AMD ROCm 10): 🚫 Run #38710372377

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Oct 10, 2026
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-connections branch from 307cbc6 to b7a8f57 Compare October 14, 2026 19:27
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-stack branch from 5c0f683 to b3d7a1c Compare October 14, 2026 19:27
@ch-wan ch-wan changed the title [Refactor] Give layer stack assembly its own boundary module [Refactor] deepseek-v4: Use explicit stage boundaries and row transport Oct 14, 2026
@ch-wan
ch-wan marked this pull request as ready for review October 14, 2026 19:47
@ch-wan ch-wan added run-ci-extra CI: also run the extra suite (requires run-ci) bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) parallel-stages CI: stages dispatch together instead of waiting on each other labels Oct 14, 2026
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-connections branch from b7a8f57 to b8e60fb Compare October 14, 2026 21:37
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-stack branch from b3d7a1c to 50adca4 Compare October 14, 2026 21:38
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-connections branch from b8e60fb to b202b82 Compare October 14, 2026 21:55
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-stack branch from 50adca4 to bb1c72b Compare October 14, 2026 21:55
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-connections branch from b202b82 to f2fae0c Compare October 14, 2026 22:08
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-stack branch from bb1c72b to 8c8357b Compare October 14, 2026 22:09
@ch-wan ch-wan added the run-ci CI: run the baseline test suite on this PR label Oct 14, 2026
@ch-wan

ch-wan commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator Author

/run-full-ci

@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-connections branch from f2fae0c to 630a645 Compare October 15, 2026 09:04
Base automatically changed from cheng/refactor/layer-boundary-connections to main October 15, 2026 09:04
Route CUDA attention and MoE outputs through explicit hyper-connection reads,
updates and compute-row transport. Preserve source residual rows while moving
MoE operands, and materialize outstanding work at PP, capture and draft exits.

Select CUDA and non-CUDA decoder subclasses at import time. Non-CUDA mHC and
TBO execution live in one platform module; DeepSeek-specific gather/combine
logic lives beside the model. Preserve late-layer DP metadata changes and
breakable-graph tail entry/exit.

Declare uneven token rows for overlapped expert dispatch. DSpark attention
completion uses the attention-TP group when it is smaller than the full TP
group. Keep transport binders out of neighbour facts.

Cover receiving PP stages, MoEOutput completion, weight refresh, transport
lifetimes and Hopper/dual-stream GPU kernels. Non-CUDA execution is checked by
CPU orchestration tests; hardware validation remains separate.
@ch-wan
ch-wan force-pushed the cheng/refactor/layer-boundary-stack branch from 8c8357b to b9e67e6 Compare October 15, 2026 09:05
@ch-wan
ch-wan merged commit f7ad434 into main Oct 15, 2026
3 of 5 checks passed
@ch-wan
ch-wan deleted the cheng/refactor/layer-boundary-stack branch October 15, 2026 09:05
detain pushed a commit to detain/sglang that referenced this pull request Oct 20, 2026
…P all-reduce

sgl-project#42245 dropped the wide-prefill multistream overlap that sgl-project#39704 added: in
target prefill of 4096 to 65536 rows each sublayer's mix stats ran on the
stats stream beside that sublayer's TP all-reduce. Since then only decode and
verify use the stream, so in prefill the mix GEMM and Sinkhorn run after the
all-reduce on the critical path, twice per layer.

Bring it back on the layer-boundary transports (sgl-project#43607). When a sublayer's
TP all-reduce is still owed and not deferred to a fused post, the attention
and MoE transports fork the stats stream in prepare_output, issue the
triplet on it, and record the stream on the residual state; HcUpdate.post
waits on it before reading the triplet. Gating follows sgl-project#39704: lagged V4.1
layers, Blackwell, extend without speculation, 4096 to 65536 rows,
attention DP 1, no SP, no prefill CP, not batch-invariant, TP4 and no MoE
A2A backend. Decode and verify keep forking before the combine.

(cherry picked from commit a6a64b6a945e704251c16a1face9d06a15ca5c18)
ormandj added a commit to ormandj/sglang that referenced this pull request Oct 29, 2026
…P all-reduce

sgl-project#42245 dropped the wide-prefill multistream overlap that sgl-project#39704 added: in
target prefill of 4096 to 65536 rows each sublayer's mix stats ran on the
stats stream beside that sublayer's TP all-reduce. Since then only decode and
verify use the stream, so in prefill the mix GEMM and Sinkhorn run after the
all-reduce on the critical path, twice per layer.

Bring it back on the layer-boundary transports (sgl-project#43607). When a sublayer's
TP all-reduce is still owed and not deferred to a fused post, the attention
and MoE transports fork the stats stream in prepare_output, issue the
triplet on it, and record the stream on the residual state; HcUpdate.post
waits on it before reading the triplet. Gating follows sgl-project#39704: lagged V4.1
layers, Blackwell, extend without speculation, 4096 to 65536 rows,
attention DP 1, no SP, no prefill CP, not batch-invariant, TP4 and no MoE
A2A backend. Decode and verify keep forking before the combine.
@lmyybh

lmyybh commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

I think there’s excessive encapsulation in the current code, which hurts readability. For example, in the snippet below, it’s hard to tell that state and hidden_states refer to the same object. 😵‍💫

state = mhc.HcState(hidden_states)
if _is_cuda:
    residual_batch.start(forward_batch)
    hidden_states = residual_batch.set_written(state, forward_batch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

amd bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) deepseek documentation Improvements or additions to documentation parallel-stages CI: stages dispatch together instead of waiting on each other run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants