[Spec][DSV4] perf: Fuse offline C128 speculative-draft state cleanup into a single kernel launch - #32090
Open
mattteochen wants to merge 39 commits into
Open
[Spec][DSV4] perf: Fuse offline C128 speculative-draft state cleanup into a single kernel launch#32090mattteochen wants to merge 39 commits into
mattteochen wants to merge 39 commits into
Conversation
Resolve the migration conflict from sgl-project#32045 (jit_kernel.dsv4 -> kernels.ops.attention.dsv4): - Port the fused C128 cleanup (C128DraftCleanup + fused kernel) onto the relocated kernels/ops/attention/dsv4/c128_cleanup.py (rename auto-detected) and export C128DraftCleanup from the new package __init__. - Resolve the deepseek_v4_memory_pool import block to the new path while keeping the fused-cleanup construction and dispatch. - Repoint the new unit test and benchmark to the new import path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
mattteochen
marked this pull request as ready for review
July 22, 2026 15:49
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
mattteochen
requested review from
alphabetc1,
hanming-lu,
hnyls2002,
hzh0425,
ispobock,
xiezhq-hermann and
yizhang2077
as code owners
July 22, 2026 15:49
mattteochen
requested review from
BBuf,
DarkSharpness,
HydraQYH,
celve and
yuan-luo
as code owners
July 23, 2026 08:54
41 tasks
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.
Motivation
The offline C128 speculative-draft state cleanup (
DeepSeekV4TokenToKVPool.clear_unaccepted_c128_draft_states) resets the ring slots written for rejected speculative tokens. It currently issues one Triton kernel launch per C128 layer. For DeepSeek-V4, which has many C128 layers, that is a long series of tiny launches on the decode-step hot path, and the per-launch overhead dominates the negligible amount of work each launch does. This PR fuses the per-layer cleanup into a single kernel launch across all C128 states.Modifications
_clear_c128_draft_stateTriton device function, reused by both the existing single-state kernel and the new fused kernel (no behavior change to the single-state path)._fused_clear_c128_draft_states_kernel, which clears the rejected rows across all C128 layer states in one launch. Because the per-layerkv_scorebuffers are separate allocations, it reads each layer's base pointer from a device-side pointer table;grid.zfolds the layer index with the feature-dimension block index.C128DraftCleanup, a small validated helper that owns the pool-stable C128 state tensors, validates their shared dtype/shape/device/contiguity once, and caches the pointer table and launch geometry at construction.clear()is then a single launch with no per-call recomputation.DeepSeekV4TokenToKVPool: build the fused cleanup once (CUDA + offline C128) in_init_paged_compress_states, and dispatch to it fromclear_unaccepted_c128_draft_states, keeping the existing per-pool loop as the non-CUDA (HIP/NPU) fallback.Accuracy Tests
Speed Tests and Profiling
main:

PR:

Micro-benchmark (
bench_c128_cleanup.py, fused vs. per-layer loop):Benchmarked on NVIDIA B200 (
test/registered/jit/benchmark/bench_c128_cleanup.py), comparing the existing per-layer loop (one kernel launch per C128 layer) against the new single fused launch. Latency in µs; speedup = loop / fused. State width = 1024 (half=512), ring_size=256.Takeaways. The loop cost scales linearly with the C128 layer count (launch overhead); the fused kernel stays ~flat. At the real DSV4 decode geometry (31 C128 layers,
states=31) the fused path is ~23× faster eager and ~25× faster under CUDA graph, growing to ~50–70× at 64 layers. Atstates=1the two are at parity (a single launch either way; the fused pointer-table indirection costs a sub-µs overhead under CUDA graph) — expected, since fusion targets the many-layer case.Checklist
CI States
Latest PR Test (Base): ❌ Run #31469920652
Latest PR Test (Extra): ❌ Run #31469920480