Skip to content

[Refactor] sparse-mla: Expose arithmetic and DSA metadata hooks - #44503

Merged
ispobock merged 21 commits into
sgl-project:mainfrom
sshleifer:c/glm53-mla-extension-hooks
Nov 4, 2026
Merged

ispobock merged 21 commits into
sgl-project:mainfrom
sshleifer:c/glm53-mla-extension-hooks

Conversation

@sshleifer

@sshleifer sshleifer commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Add four extension points for custom MLA arithmetic and DSA metadata while retaining the SRT forward lifecycle: run_mla_indexer, prepare_mla_qkv, project_mla_values, and init_kernel_metadata.

Defaults preserve existing operations and fast paths. Query-transforming overrides set needs_unfused_mla_qkv = True to receive materialized queries. Custom indexers run during CP prefill; custom metadata refreshes on replay and opts out of glue capture. LoRA correction, output gating/projection, shared-index reuse, and seed handoff remain in the existing lifecycle.

Scope: ROCm dense MHA preparation dispatches the indexer hook. The separate MLA_ROCM absorbed path bypasses the arithmetic hooks and requires its own overrides.

Validation

  • 41 CPU tests and 10 subtests passed after merging main at af98fcd545 (merge commit 8baffc66f3), across metadata replay/glue and shared-index coverage. Removed the mocked arithmetic-hook matrix; retained compact regressions for live custom metadata and allocation-free default replay.
  • Real DSA backend tests on GB300: 4 tests and 18 subtests passed, four unsupported variants skipped. Local runtime used a version override. The targeted H100 SM90 seed-handoff regression subsequently passed on 6cef97f7c3 (one test executed, not skipped).
  • Earlier B300 CI passed GLM-5.2 GSM8K at 0.94072 against 0.935 on retry.
  • The eight-GPU CP failure matches the separate upstream diagnostic: maximum VERIFY/prefill logprob gap 0.8945 on CP8, with plain TP8 also exceeding the 0.02 bound (0.4381). Merged main includes the CI disablement from #47894; #47970 proposes measured replacement bounds. This PR adds no tolerance change. Fresh CI is pending; no dedicated throughput benchmark.
python -m pytest -q \
  test/registered/unit/layers/attention/test_dsa_kernel_metadata_hook.py \
  test/registered/unit/layers/attention/test_index_topk_share.py \
  test/registered/unit/model_executor/runner/test_dsv41_draft_metadata_glue.py \
  test/registered/unit/model_executor/runner/test_dsv41_target_verify_metadata_glue.py

CI States

Latest PR Test (Base): ✅ Run #41575243362
Latest PR Test (Extra): ⏳ Run #41575243331
Latest PR Test (AMD ROCm 10): ❌ Run #41575243299

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T18:24:34.633146Z 4d46f70 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8a6b5e95e5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@sshleifer sshleifer changed the title [Refactor] MLA: Expose arithmetic and DSA metadata hooks Sparse MLA: Expose arithmetic and DSA metadata hooks Oct 15, 2026
@ispobock ispobock added run-ci CI: run the baseline test suite on this PR 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) labels Oct 16, 2026
@ispobock

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/unit/models/test_mla_extension_hooks.py test/registered/unit/layers/attention/test_dsa_kernel_metadata_hook.py test/registered/unit/models/test_deepseek_mla_dispatch.py test/registered/unit/layers/attention/test_dsa_backend_dp_padding.py

@ispobock

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (4 tests): ✅ View workflow run

cd test/ && python3 registered/unit/models/test_mla_extension_hooks.py
cd test/ && python3 registered/unit/layers/attention/test_dsa_kernel_metadata_hook.py
cd test/ && python3 registered/unit/models/test_deepseek_mla_dispatch.py
cd test/ && python3 registered/unit/layers/attention/test_dsa_backend_dp_padding.py

@github-actions

This comment has been minimized.

@ispobock

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

Sam Shleifer and others added 2 commits October 18, 2026 13:03
main replaced the tc_piecewise context with is_in_prefill_graph_scope /
is_in_full_prefill_graph and dropped the piecewise BMM branch.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@sshleifer sshleifer changed the title Sparse MLA: Expose arithmetic and DSA metadata hooks [Refactor] sparse-mla: Expose arithmetic and DSA metadata hooks Oct 18, 2026
@github-actions

This comment has been minimized.

One class flag gates both deferred BMM fusion and born-FP8 query packing.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@github-actions github-actions Bot added the run-ci-nv-extra CI: NVIDIA/shared Extra suite (requires run-ci) label Oct 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⏳ PR #44503 - [Refactor] sparse-mla: Expose arithmetic and DSA metadata hooks is already in_progress for this commit; left it alone.
🚀 Re-running Lint.
🚀 Re-running PR Test (sgl-router).
🚀 Re-running PR Test (sglang-processor).
🚀 Re-running PR Test Extra.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d46f70b47

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions github-actions Bot added the amd label Oct 30, 2026
@peymanr

peymanr commented Oct 31, 2026

Copy link
Copy Markdown

PR Pundit review

(Testing PR Pundit Code Review agent. Not all comments may be relevant. This is a one-time automated pass: please address what is useful, no need to request a re-review. A quick reply per point (useful or not, and why) helps us calibrate the agent. Reach out to @peymanr for suggestions and feedback.)

# Finding Where
1 🔴 DSA MTP seed-handoff MHA subtest fails with TypeError: this PR removed layer_id but left kit caller passing it python/sglang/srt/models/deepseek_common/attention_forward_methods/forward_mha.py:96
2 🟠 New hook/glue unit tests use mock stand-ins, breaking on refactors and only indirectly covering the default capture route test/registered/unit/layers/attention/test_dsa_kernel_metadata_hook.py:230-258
3 🟠 Multistep draft-decode replay adds ~3 µs host overhead by building inner_fb and looping hooks unconditionally python/sglang/srt/layers/attention/dsa_backend.py:4359

1. 🔴 Blocking: DSA MTP seed-handoff MHA subtest fails with TypeError: this PR removed layer_id but left kit caller passing it

python/sglang/srt/models/deepseek_common/attention_forward_methods/forward_mha.py:96

forward_dsa_indexer_for_mha dropped the layer_id keyword (and changed its first param to a run_indexer callable), but the test kit python/sglang/test/kits/attention_unittest/attention_methods/dsa_attention.py:1516 (run_dsa_mtp_seed_handoff_case) still passes layer_id=0. Registered test test/registered/attention/unittests/dsa/test_dsa.py:435 reaches this and fails with TypeError: forward_dsa_indexer_for_mha() got an unexpected keyword argument 'layer_id'. Production callers and test_index_topk_share.py were updated but the kit was not. Drop layer_id=0 from the kit call; optionally type-hint run_indexer.

How it is reached: pr-test.yml base-b-test-1-gpu-large (runs_on 1-gpu-h100, SM90) -> test/registered/attention/unittests/dsa/test_dsa.py::test_mtp_seed_handoff_cases (seed_path='mha') -> run_dsa_mtp_seed_handoff_case -> dsa_attention.py:1516 forward_dsa_indexer_for_mha(..., layer_id=0) -> TypeError against the new signature at forward_mha.py:96

Who is affected: Every PR CI run on the 1-gpu-large (H100, SM90) runner fails the registered DSA MTP seed-handoff test; production model forward is unaffected because the PR updated both runtime callers.

Checked against the code by an independent pass.

2. 🟠 Should fix: New hook/glue unit tests use mock stand-ins, breaking on refactors and only indirectly covering the default capture route

test/registered/unit/layers/attention/test_dsa_kernel_metadata_hook.py:230-258

The new tests in test_dsa_kernel_metadata_hook.py build backends from SimpleNamespace/__new__ stand-ins wired to private methods (_apply_cuda_graph_metadata, _precompute_replay_metadata, _copy_replay_metadata_from_sibling). They will break on internal renames rather than on behaviour changes. test_custom_metadata_is_never_frozen_in_glue_graph does check that the eager short-circuit is taken: without it, the third run would reach torch.cuda.Stream() on CPU. Only its final not glue.is_captured((2,)) assertion follows trivially from glue.disabled. The default direction is covered only in pieces: the test asserts not DSA.__new__(DSA).requires_eager_metadata, and TestGlueCaptureFailure routes a requires_eager_metadata=False mock to capture. An end-to-end check with a real default DSA backend would be a cheap addition.

Who is affected: Maintainers refactoring DSA metadata internals; no runtime user impact.

Checked against the code by an independent pass.

3. 🟠 Should fix: Multistep draft-decode replay adds ~3 µs host overhead by building inner_fb and looping hooks unconditionally

python/sglang/srt/layers/attention/dsa_backend.py:4359

DeepseekSparseAttnMultiStepBackend.init_forward_metadata_out_graph now calls build_inner_fb_view (a fresh SimpleNamespace) on every replay, not only under in_capture. On the stock backend it then calls the no-op init_kernel_metadata on each of the speculative_num_steps - 1 children. The added host cost is a few microseconds per draft-decode replay, small next to _init_forward_metadata_replay. Consider building the view and running the loop only when some child's requires_eager_metadata is true.

Who is affected: DSA models (DeepSeek-V3.2/GLM-5) running multi-step speculative draft decode with CUDA graphs.

Checked against the code by an independent pass.

Related past reviews: #8616 on python/sglang/srt/models/deepseek_v2.py, #25001 on python/sglang/srt/models/deepseek_common/attention_forward_methods/forward_mla.py, #40782 on python/sglang/srt/layers/attention/dsa_backend.py

Shared from a pilot of PR Pundit, an agentic code reviewer that builds and tests the change and reads the project's past reviews. Reviewed at 0b8cdcab; findings checked and posted by @peymanr; points already raised on this PR are left out.

Copy link
Copy Markdown
Contributor Author

Fixed the stale MHA seed-handoff kit caller in a764c80 by removing the obsolete layer_id keyword. The hook, metadata-glue, and seed-export CPU suites pass 112 tests and 10 subtests. The registered seed-handoff cases skip on local GB300 because they require SM90; H100 CI must validate them. Updated the description.

The current B300 failure is test_cp_decode_attn_tp_server.py, VERIFY versus teacher-forced CP prefill logprob tolerance. I am checking it against upstream evidence before attributing it to these hooks. The two advisory test-structure/host-overhead suggestions remain open; this commit only fixes the confirmed caller mismatch.

-Robot

Copy link
Copy Markdown
Contributor Author

Addressed the default-replay overhead follow-up in 3d4611c: the multi-step backend now builds the DECODE view only during capture or when a child overrides the kernel-metadata hook, and skips default no-op dispatch during replay. A regression fails before the change and passes after it; the focused CPU suites pass 113 tests plus 10 subtests.

Also ran the existing real DSA backend GPU tests on GB300: dense MHA fallback, sparse top-k attention, EAGLE draft CUDA-graph runner, and supported sparse decode graph variants pass (4 tests plus 18 subtests; 4 unsupported variants skipped). These use actual backend/kernel execution and reference comparisons, supplementing the mocked hook tests. Local environment: PyTorch 2.11/CUDA 13.2, FlashInfer 0.7.2, CuTeDSL 4.8, sgl-kernel 0.4.6.post1 with version-check override; this does not substitute for the pinned CI environment or the SM90-only seed-handoff test.

The separate eight-GPU CP VERIFY/prefill accuracy failure is still unclassified; I have not relaxed its tolerance.

@peymanr

peymanr commented Oct 31, 2026

Copy link
Copy Markdown

PR Pundit review

(Testing PR Pundit Code Review agent. Not all comments may be relevant. This is a one-time automated pass: please address what is useful, no need to request a re-review. A quick reply per point (useful or not, and why) helps us calibrate the agent. Reach out to @peymanr for suggestions and feedback.)

# Finding Where
1 🟠 MLA hook overrides silently skipped on ROCm absorbed path: this PR wired hooks only into CUDA/MHA paths python/sglang/srt/models/deepseek_common/attention_forward_methods/forward_mla_rocm.py:663

1. 🟠 Should fix: MLA hook overrides silently skipped on ROCm absorbed path: this PR wired hooks only into CUDA/MHA paths

python/sglang/srt/models/deepseek_common/attention_forward_methods/forward_mla_rocm.py:663

The new MLA extension hooks are only wired into the CUDA absorbed path and the MHA paths. DeepseekMLARocmForwardMixin.forward_absorb_rocm_prepare still calls self.indexer(...) directly (forward_mla_rocm.py:663, :690) and self.rotary_emb (:816). forward_absorb_rocm_core still uses rocm_absorb_v_bmm (:1122), and the ROCm path never checks needs_unfused_mla_qkv. On HIP, DSA/MLA dispatch is remapped MLA -> MLA_ROCM, so a subclass overriding run_mla_indexer, prepare_mla_qkv or project_mla_values would be silently ignored there. No in-tree class overrides these hooks today; either route the ROCm mixin through them or document that they are CUDA-path only.

Who is affected: Only a future or out-of-tree DeepseekV2AttentionMLA subclass that overrides the new hooks and runs on ROCm/HIP with the absorbed MLA path.

Checked against the code by an independent pass.

Shared from a pilot of PR Pundit, an agentic code reviewer that builds and tests the change and reads the project's past reviews. Reviewed at 3d4611c5; findings checked and posted by @peymanr; points already raised on this PR are left out.

@sshleifer

Copy link
Copy Markdown
Contributor Author

/rerun-test registered/unit/layers/attention/test_dsa_kernel_metadata_hook.py registered/attention/unittests/dsa/test_dsa.py::TestDSAAttentionBackendCorrectness.test_mtp_seed_handoff_cases

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/attention/unittests/dsa/test_dsa.py TestDSAAttentionBackendCorrectness.test_mtp_seed_handoff_cases

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

cd test/ && python3 registered/attention/unittests/dsa/test_dsa.py TestDSAAttentionBackendCorrectness.test_mtp_seed_handoff_cases

⛔ registered/unit/layers/attention/test_dsa_kernel_metadata_hook.py: File not found: test/registered/unit/layers/attention/test_dsa_kernel_metadata_hook.py

@Fridge003

Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Contributor Author

@Fridge003 Investigated the failure and merged current main in 4f101d3. The CP test's 0.89451 maximum logprob gap matches the separate upstream diagnostic in #47980 (comment). Main already disables that test via #47894; #47970 proposes measured replacement bounds.

The branch now includes that upstream disablement. All 41 focused CPU tests and 10 subtests passed after the merge, and fresh CI has started. Updated the PR description with the findings.

@ispobock

ispobock commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

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) run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) run-ci-nv-extra CI: NVIDIA/shared Extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants