Repository navigation
[Perf] deepseek-v4.1: Replay the breakable prefill CUDA graph at any context length - #43733
Conversation
|
@Oasis-Git This is the companion to #43736 (just merged) — could you add |
|
/tag-and-rerun-ci |
|
Thanks for the work, we also need this to make long context cuda graph work. |
|
cc @Oasis-Git @kpham-sgl this will be needed before we can use BCG |
Oasis-Git
left a comment
There was a problem hiding this comment.
[Codex]: Two issues in the retained captured full-top-k fallback path. These findings are based on source inspection; GPU reproduction has not been performed.
| maybe_copy_inplace(self.indexer_metadata, src=static_metadata.indexer_metadata) | ||
| maybe_copy_inplace( | ||
| self.c1_indexer_metadata, src=static_metadata.c1_indexer_metadata | ||
| ) | ||
| maybe_copy_inplace( | ||
| self.c2_indexer_metadata, src=static_metadata.c2_indexer_metadata | ||
| ) | ||
| # The ratio-1/2 indexer metadata is read by eager breaks (flat scoring) | ||
| # or only through scalars by captured kernels: take the live objects. | ||
| self.c1_indexer_metadata = static_metadata.c1_indexer_metadata | ||
| self.c2_indexer_metadata = static_metadata.c2_indexer_metadata |
There was a problem hiding this comment.
[Codex]: [P1] Preserve in-place metadata refresh for the captured full-top-k fallback. On SM100+ with CP=1, SGLANG_DSV41_TORCH_PREFILL_INDEXER=1 (or candidate_topk_blocks <= 0) makes the factory return DenseBlocksBackend, while low_ratio_prefill_graph can still be true. In that case forward_low_ratio_sources sets captured_index=True and captures FullTopKIndexer._deep_gemm_prefill_captured, which reads the tensor addresses of compressed_seq_lens, page_table, and the DeepGEMM schedule. Replacing the Python metadata objects here does not update those captured buffers, so subsequent requests can compute top-k using capture-time lengths and KV page mappings. Please retain copy_ for that path, or move its indexer out of capture as well. A replay test that changes request lengths and KV page mappings within the same bucket would cover this regression.
| topk_transform_paged_torch( | ||
| # Per row up to its own length: a wide graph then costs only the | ||
| # logits buffer, not a full-static-width top-k. | ||
| topk_transform_paged_from_metadata( |
There was a problem hiding this comment.
[Codex]: [P2] Keep support for top-k sizes above 1024 in this fallback. _low_ratio_prefill_indexer_metadata sets use_topk_v2=False, so this helper dispatches to topk_transform_paged and the v1 CUDA kernel, whose kMaxTopK and runtime check cap k at 1024. For a captured full-top-k configuration with index_topk=2048 and sufficient capture width, warmup/capture now fails with topk must be in (0, 1024]; the previous torch.topk implementation supported that size. This affects the retained full-top-k fallback, not the new sparse-table consumer. Please use an implementation supporting the configured k (for example, a properly configured v2 path or the torch fallback) and add a capture case with k=2048.
…t length The prefill graph is captured at a fixed context width and every batch longer than it ran eager, which on GB300 is host-launch-bound. Run the flat index layers (ratio-2 and the candidate source) as eager_on_graph breaks over the live batch, keep the ratio-1 consumers on the two-level candidate path inside the graph (identity inside the window, correct past it), select with topk_transform_paged_from_metadata per row instead of a static-width torch.topk, and let can_run_prefill_cuda_graph admit any context when the candidate-window indexer is available. The capture width stays at 16k.
…ions for the flat layers The flat index layers project q / head weights once inside their eager break (_index_q_and_weights); the captured source projections now skip the indexer part for those layers and keep it only for the captured candidate consumers (and for the all-captured path when the sparse candidate backend is absent).
… > 1024 Review follow-ups: the replay refresh hands the ratio-1/2 indexer metadata over by reference only under two-level candidates; the captured full-top-k fallback binds the lengths and page tables by address, so it copies in place. The fallback's v1 top-k kernel caps k at 1024; wider selections keep the torch top-k. CPU tests for both.
a233b67 to
0f345f9
Compare
|
@Oasis-Git thanks, both are right.
Also rebased onto main (0f345f9). |
| # for the low-ratio prefill metadata), which caps k; wider selections | ||
| # keep the torch top-k over the captured width. | ||
| topk = min(indexer.index_topk, width) | ||
| torch_topk = not metadata.use_topk_v2 and topk > TOPK_V1_MAX_K |
There was a problem hiding this comment.
[Codex]: [P2] Base the v1 limit check on the output width actually passed to the kernel. There is still a failing width-clipped case: with index_topk=2048, width=512, and use_topk_v2=False, topk is 512 here, so this selects the kernel branch. However, that branch passes the unsliced inputs.out_page_indices / inputs.out_raw_indices to topk_transform_paged_from_metadata. The helper slices rows only, and the v1 kernel derives k from the output column count, which is still 2048, triggering topk must be in (0, 1024]. Please select the torch fallback based on the actual output width (or safely adapt the kernel outputs). The new _topk_calls(2048, 512) test currently expects the kernel branch and mocks away this validation, so it should also be corrected to cover the actual kernel contract. This is based on source inspection, not a GPU reproduction.
…l sees The v1 kernel derives k from the output columns it is handed, which the metadata dispatcher slices by rows only, so a 2048-wide output needs the torch path even when the captured width clips the selection. Test covers that case and the short-width kernel case.
… to its rule; fix the tail_rows docstring
…ll replay with the candidate indexer
… branch; require the refresh mode; declare the graph buffer caches; define the break wrappers at module level
…cate to two-level, tail_rows to gather_rows; keep one name for unbounded replay
…oth metadata builders honor it; test the replay width
…nd; share the prefill sparse logits step; fix the padding-row comment
…refill graph docstrings to one home each
…e the DeepSeek-V4.1 16K default from the hook into the backends so an explicit max_seq_len always applies; test the limits
…drop the per-fix, gate and refresh tests
…de two-level candidates
|
/rerun-test test_dsv41_late_layer_tail_readers.py test_encoder_swa_replay.py test_prefill_cuda_graph_runner.py test_prefill_cuda_graph_runner_helpers.py test_platform_prefill_cp_deprecation.py test_eager_on_graph.py test_deepseek_v41_bcg_multimodal.py test_dsv4_compressed_pools.py test_dsv4_nonpaged_indexer.py test_deepseek_v4.py test_dsv41_prefill_sparse_indexer.py test_deepseek_v41_flash_gb300_tp4ep4.py test_deepseek_v41_flash_gb300_dp4ep4.py test_deepseek_v41_flash_gb300_trtllm_bounded_bcg.py |
|
/tag-and-rerun-ci |
|
🚀 🚀 🚀 🚀 |
Motivation
DeepSeek-V4.1's breakable prefill CUDA graph is captured at a 16,384-token context width, and every batch with a longer context runs eager. The width is the candidate window (2,048 blocks of 8 tokens): inside it the captured full top-k equals the two-level candidate selection, and past it it does not. Eager DeepSeek-V4.1 prefill is bound by host kernel launches, so long-context requests lose the graph where it would help most.
Modifications
The graph replays at any context length when the two-level candidate indexer runs on DeepGEMM (SM100, no prefill context parallelism, no ratio-4/128 layers):
_prefill_graph_full_topk_break). The candidate source also returns its block table (SparseTableBackend.publish_prefill_graph), which is copied into the addresses the consumers were captured with.SparseTableBackend.consume_prefill_captured) and score only the published candidate blocks, so nothing captured depends on the context length.prefill_cuda_graph_default_max_seq_len(None for every other backend) is 16,384 while the captured indexer bounds the context and None when any context replays. The runner uses it only whencuda_graph_config[prefill].max_seq_lenis unset, so an explicit--cuda-graph-max-seq-len-prefillalways applies._prefill_graph_context_width). In two-level mode the ratio-1/2 indexer metadata is reference-assigned on refresh instead of copied in place.Configurations without the candidate indexer (no candidate blocks, the torch prefill path, prefill context parallelism, or pre-SM100 GPUs) keep the 16K limit and today's behavior. The prefill graph stays opt-in for DeepSeek-V4.1 (
--cuda-graph-backend-prefill breakable).Accuracy Tests
GB300 with DeepSeek-V4.1-Flash, TP4 (DSPARK, chunked prefill 8192) and DEP2 with MegaMoE, against main:
--cuda-graph-max-seq-len-prefill 16384, and withSGLANG_DSV41_TORCH_PREFILL_INDEXER=1(no two-level candidates, so the backend default of 16,384 applies), 14 of the same 123 prefill batches replay, as on main's 16K graph; by default 121 do.Speed Tests and Profiling
TTFT, TP4, one request, decoder replay on, median of 3:
Checklist
CI States
Latest PR Test (Base): 🚫 Run #39209663048
Latest PR Test (Extra): ❌ Run #39209662889
Latest PR Test (AMD ROCm 10): ⏳ Run #39209663190