Skip to content

[DCP] Drop two per-layer launches from the MLA target-verify path - #34240

Merged
kpham-sgl merged 4 commits into
sgl-project:mainfrom
kpham-sgl:dcp-mla-drop-lse-rebase-and-arange
Aug 11, 2026
Merged

kpham-sgl merged 4 commits into
sgl-project:mainfrom
kpham-sgl:dcp-mla-drop-lse-rebase-and-arange

Conversation

@kpham-sgl

@kpham-sgl kpham-sgl commented Aug 10, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Profiling Kimi-K3 @ 128k on 8xB300 (--dcp-size 8 + DSpark, cutedsl_mla) shows that in every MLA layer of the captured target-verify graph, the window between the attention epilogue and the DCP all-to-all is mostly glue. Two of those kernels do no useful work at all:

kernel µs/layer what it is
vectorized_elementwise<MulFunctor<float>>, grid[1] 1.76 lse * log2(e) — rebase the cute-dsl kernel's natural-log LSE to base-2
elementwise_kernel_with_index<arange>, grid[1] 0.94 build dense_q_indptr for the zero-KV fixup

Both are pure launch overhead: the multiply touches 768 fp32 values, the arange produces bs + 1 int32 values. With 12 MLA layers per verify step that is ~32 µs/step serialized on the main stream, ahead of the a2a.

Modifications

LSE base is declared, not converted. dcp_a2a_lse_reduce / cp_lse_ag_out_rs_mla already take is_lse_base_on_e, and both merge implementations branch on it symmetrically (dcp_lse_combine_triton picks exp/log vs exp2/log2, correct_attn_out likewise). So cutedsl_mla is added to a new _MLA_DCP_BASE_E_LSE_BACKENDS set in forward_mla.py and CuteDslMLABackend._run_decode_kernel returns the kernel's LSE untouched. HybridAttnBackend._select_backend routes a forward mode to the same backend that _set_current_attention_backend names, so the string reliably identifies the backend object that produced the LSE.

Dense verify indptr is materialized once. arange(0, (bs + 1) * draft_token_num, draft_token_num) is constant for a given draft_token_num, so TRTLLMMLABackend.__init__ builds it from the existing q_indptr_decode buffer and _dense_q_indptr(bs, draft_token_num) slices it. The general expression is kept for the case where a batch's draft_token_num differs from the server-args value (it does not on the EAGLE / DFLASH / DSpark verify paths, where speculative_num_draft_tokens is validated to equal the verify window).

Other MLA DCP backends are unaffected: tokenspeed_mla, trtllm_mla and FlashInfer-MLA still report base-2 and still take the same merge path.

Accuracy Tests

Not run. The runs below pin acceptance with SGLANG_SIMULATE_ACC_LEN=5, so they measure kernel structure and speed, not output quality — I have not validated accuracy for this change.

The LSE change is mathematically an identity (softmax output is base-invariant; only the LSE value changes), and it removes a rounding step rather than adding one, since the log2(e) multiply no longer touches the fp32 LSE. The indptr change produces bit-identical indices, pinned by the new unit test. Happy to run GSM8K on the 8xB300 box before merge if a reviewer wants it.

Speed Tests and Profiling

Before

Screenshot 2026-08-10 at 4 13 26 PM

After
Screenshot 2026-08-10 at 4 13 32 PM

Checklist

Tests added:

  • test/registered/kernels/test_dcp_lse_combine.py::test_natural_log_lse_backends — pins which backends declare base-e, including the negative branches.
  • test/registered/dcp/test_dcp_layout_unit.py::test_dense_q_indptr_matches_the_arange_it_replaces — pins both the precomputed buffer and the general path to the arange formula.

Neither was executed locally (no torch in this environment); both are CPU-only and registered in CI.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #31374613530
Latest PR Test (Extra): ❌ Run #31374613569

Between the MLA attention and the DCP all-to-all, each MLA layer of the
captured verify graph launched two kernels that do no useful work:

- `lse * log2(e)` rebasing the cute-dsl kernel's natural-log LSE to base-2
  (~1.8 us/layer). The cross-rank merge already takes an `is_lse_base_on_e`
  flag and both implementations (`dcp_lse_combine_triton` for a2a/fi_a2a,
  `correct_attn_out` for the allgather fallback) branch exp/log vs exp2/log2
  on it, so declare the base instead of converting to it.
- `torch.arange` building the dense verify query indptr (~0.9 us/layer). It
  is constant for a given num_draft_tokens, so materialize it once at init.

On Kimi-K3 @128k, DCP8+DSpark, B300 x8 that is ~2.7 us per MLA layer, 12 MLA
layers per verify step, i.e. ~32 us/step off the critical path. No numerics
change beyond dropping one rounding step from the LSE.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
kpham-sgl and others added 3 commits August 10, 2026 01:08
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…se-and-arange

# Conflicts:
#	test/registered/dcp/test_dcp_layout_unit.py
Pure refactor, no behavior change: the frozenset had a single use, and a
bare set literal in an `in` comparison is constant-folded by CPython, so
it is still built once rather than per call.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/dcp/*

@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/dcp/*:

🚀 ubuntu-latest (1 test): ❌ View workflow run

cd test/ && python3 registered/dcp/test_dcp_layout_unit.py

🚀 8-gpu-h200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/dcp/test_dsv31_dcp8_gsm8k.py
cd test/ && python3 registered/dcp/test_reduce_scatter_along_dim.py

🚀 4-gpu-b200 (4 tests): ✅ View workflow run

cd test/ && python3 registered/dcp/test_kimi_linear_dcp4.py
cd test/ && python3 registered/dcp/test_kimi_linear_dcp_dspark4.py
cd test/ && python3 registered/dcp/test_qwen3p5_triton_dcp.py
cd test/ && python3 registered/dcp/test_tokenspeed_mla_dcp_metadata.py

@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/kernels/test_dcp_lse_combine.py

@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/kernels/test_dcp_lse_combine.py:

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/test_dcp_lse_combine.py

@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

Should be safe to merge. CPU tests are currently broken

@kpham-sgl
kpham-sgl merged commit 0967885 into sgl-project:main Aug 11, 2026
103 of 119 checks passed
vstone-w pushed a commit to ClownBin/sglang that referenced this pull request Aug 12, 2026
qtris123 added a commit to qtris123/sglang that referenced this pull request Aug 12, 2026
…the base at the merge site

Obsoletes 2e5e123 ("[DCP] Fix the decode LSE base for trtllm-gen; resolve it
per backend"), which is superseded by the merge-site LSE convention that landed
upstream in sgl-project#34240.

2e5e123 fixed a real bug: the decode LSE was rebased by log2(e)
unconditionally, which is right for cute-dsl (natural log) but corrupts
trtllm-gen, which already emits base-2. It fixed it inside the backend,
resolving the base from a registry keyed on the flashinfer kernel string
("trtllm-gen" / "cute-dsl").

sgl-project#34240 fixes the same problem from the other end: a backend now returns its
native LSE and the merge site decides, keyed on the sglang attention backend
name instead:

    def is_mla_dcp_lse_base_on_e(attention_backend):
        return attention_backend in {"flashmla", "cutedsl_mla"}

Keeping both would leave two registries for one correction, and the rebase in
_run_decode_kernel would re-enter the very double-rebase 2e5e123 removed as
soon as a cute-dsl call reached the base hook with return_lse=True. So drop the
backend-side mechanism; the merge-site convention already gives the right
answer across the family:

  - trtllm_mla     -> merge assumes base-2; trtllm-gen emits base-2.
  - cutedsl_mla    -> merge assumes base-e; cute-dsl emits natural log, and
                      sgl-project#34240 removed its own rebase.
  - tokenspeed_mla -> merge assumes base-2; unaffected, it never passed a
                      flashinfer backend string so it never rebased.

_run_decode_kernel keeps only the return_lse passthrough that the DCP decode
path needs and otherwise returns the kernel result unchanged, as upstream does.
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
@kpham-sgl
kpham-sgl deleted the dcp-mla-drop-lse-rebase-and-arange branch August 24, 2026 22:25
kpham-sgl pushed a commit to qtris123/sglang that referenced this pull request Aug 26, 2026
…the base at the merge site

Obsoletes 2e5e123 ("[DCP] Fix the decode LSE base for trtllm-gen; resolve it
per backend"), which is superseded by the merge-site LSE convention that landed
upstream in sgl-project#34240.

2e5e123 fixed a real bug: the decode LSE was rebased by log2(e)
unconditionally, which is right for cute-dsl (natural log) but corrupts
trtllm-gen, which already emits base-2. It fixed it inside the backend,
resolving the base from a registry keyed on the flashinfer kernel string
("trtllm-gen" / "cute-dsl").

sgl-project#34240 fixes the same problem from the other end: a backend now returns its
native LSE and the merge site decides, keyed on the sglang attention backend
name instead:

    def is_mla_dcp_lse_base_on_e(attention_backend):
        return attention_backend in {"flashmla", "cutedsl_mla"}

Keeping both would leave two registries for one correction, and the rebase in
_run_decode_kernel would re-enter the very double-rebase 2e5e123 removed as
soon as a cute-dsl call reached the base hook with return_lse=True. So drop the
backend-side mechanism; the merge-site convention already gives the right
answer across the family:

  - trtllm_mla     -> merge assumes base-2; trtllm-gen emits base-2.
  - cutedsl_mla    -> merge assumes base-e; cute-dsl emits natural log, and
                      sgl-project#34240 removed its own rebase.
  - tokenspeed_mla -> merge assumes base-2; unaffected, it never passed a
                      flashinfer backend string so it never rebased.

_run_decode_kernel keeps only the return_lse passthrough that the DCP decode
path needs and otherwise returns the kernel result unchanged, as upstream does.
qtris123 added a commit to qtris123/sglang that referenced this pull request Aug 31, 2026
…the base at the merge site

Obsoletes 2e5e123 ("[DCP] Fix the decode LSE base for trtllm-gen; resolve it
per backend"), which is superseded by the merge-site LSE convention that landed
upstream in sgl-project#34240.

2e5e123 fixed a real bug: the decode LSE was rebased by log2(e)
unconditionally, which is right for cute-dsl (natural log) but corrupts
trtllm-gen, which already emits base-2. It fixed it inside the backend,
resolving the base from a registry keyed on the flashinfer kernel string
("trtllm-gen" / "cute-dsl").

native LSE and the merge site decides, keyed on the sglang attention backend
name instead:

    def is_mla_dcp_lse_base_on_e(attention_backend):
        return attention_backend in {"flashmla", "cutedsl_mla"}

Keeping both would leave two registries for one correction, and the rebase in
_run_decode_kernel would re-enter the very double-rebase 2e5e123 removed as
soon as a cute-dsl call reached the base hook with return_lse=True. So drop the
backend-side mechanism; the merge-site convention already gives the right
answer across the family:

  - trtllm_mla     -> merge assumes base-2; trtllm-gen emits base-2.
  - cutedsl_mla    -> merge assumes base-e; cute-dsl emits natural log, and
                      sgl-project#34240 removed its own rebase.
  - tokenspeed_mla -> merge assumes base-2; unaffected, it never passed a
                      flashinfer backend string so it never rebased.

_run_decode_kernel keeps only the return_lse passthrough that the DCP decode
path needs and otherwise returns the kernel result unchanged, as upstream does.
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blackwell SM100/SM120

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant