Skip to content

Accept and ignore topk_indices in FlashInfer MLA dense backend - #30994

Closed
AliceChenyy wants to merge 2 commits into
sgl-project:mainfrom
AliceChenyy:fix/mla-backend-topk-indices
Closed

AliceChenyy wants to merge 2 commits into
sgl-project:mainfrom
AliceChenyy:fix/mla-backend-topk-indices

Conversation

@AliceChenyy

@AliceChenyy AliceChenyy commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

DSA-family models (GLM-5.x, DeepSeek V3.2+) unconditionally pass the indexer's topk_indices to the attention backend. The dense FlashInfer MLA backend doesn't accept that keyword, so every code path that calls the backend interface directly crashes:

  • speculative decoding verify (TARGET_VERIFY through the decode graph runner)
  • pipeline-parallel non-overlap decode
TypeError: FlashInferMLAAttnBackend.forward_decode() got an unexpected keyword argument 'topk_indices'

The default overlap decode path happens to work because the tc-piecewise wrapper filters kwargs — which makes this failure surprisingly configuration-dependent: on SM120 (RTX PRO 6000 / RTX 6000D, where DSA sparse kernels are unavailable and dense MLA is the only option) --tp 8 serves GLM-5.2-NVFP4 fine, but adding --speculative-algorithm EAGLE or --pipeline-parallel-size 2 crashes on the first verify/decode. Reproduced on v0.5.15.

Modification

Add an optional topk_indices parameter to forward_extend / forward_decode and ignore it. Dense attention over the full KV is a lossless superset of the sparse top-k selection, so ignoring the hint is sound (sparse is an approximation of dense, not the other way around).

Validated on 8x RTX 6000D, GLM-5.2-NVFP4, v0.5.15-equivalent build: TP8+EAGLE and TP4xPP2 both serve correctly with this shim (GSM8K 20q 0.90, greedy outputs identical to non-spec).

Related: this unblocks the SM120 GLM-5.2 recipe being added to sgl-cookbook.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #29236214796
Latest PR Test (Extra): ❌ Run #29236214430

DSA-family models (GLM-5.x / DeepSeek V3.2+, GlmMoeDsaForCausalLM etc.)
unconditionally pass the indexer's topk_indices to the attention backend.
The dense FlashInfer MLA backend does not take that keyword, so any code
path that calls the backend interface directly -- speculative-decoding
verify (TARGET_VERIFY via the decode graph runner) and pipeline-parallel
non-overlap decode -- crashes with:

    TypeError: FlashInferMLAAttnBackend.forward_decode() got an
    unexpected keyword argument 'topk_indices'

while the default overlap decode path happens to work because the
tc-piecewise wrapper filters kwargs. Reproduced on v0.5.15 with
GLM-5.2-NVFP4 on SM120 (where the DSA sparse kernels are unavailable and
dense MLA is the only option): plain --tp 8 serves fine, adding EAGLE MTP
or PP crashes on the first verify/decode.

Dense attention over the full KV is a lossless superset of the sparse
top-k selection, so accepting and ignoring the hint is sound.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds a topk_indices parameter to the forward_extend and forward_decode methods in FlashInferMLAAttnBackend to support DSA-family models. The reviewer recommends replacing this specific parameter with **kwargs in both methods to adhere to the Liskov Substitution Principle, align with the base class signature, and prevent potential TypeError crashes from unsupported arguments in the future.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +526 to +529
# DSA-family models pass the indexer's top-k selection; dense MLA
# attends the full KV (a lossless superset of the sparse selection),
# so the hint is safely ignored.
topk_indices: Optional[torch.Tensor] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Instead of adding a specific topk_indices parameter, it is highly recommended to add **kwargs to the signature of forward_extend.\n\nThe base class AttentionBackend defines forward_extend with **kwargs to allow backends to gracefully ignore arguments they do not support. By not accepting **kwargs, FlashInferMLAAttnBackend violates the Liskov Substitution Principle and is fragile to any other keyword arguments passed by different model architectures or speculative decoding strategies (which can cause TypeError crashes). Adding **kwargs makes the backend robust and future-proof.

Suggested change
# DSA-family models pass the indexer's top-k selection; dense MLA
# attends the full KV (a lossless superset of the sparse selection),
# so the hint is safely ignored.
topk_indices: Optional[torch.Tensor] = None,
**kwargs,

Comment on lines +609 to +611
# DSA-family models pass the indexer's top-k selection; dense MLA
# attends the full KV (a lossless superset), safely ignored.
topk_indices: Optional[torch.Tensor] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Instead of adding a specific topk_indices parameter, it is highly recommended to add **kwargs to the signature of forward_decode.\n\nThe base class AttentionBackend defines forward_decode with **kwargs to allow backends to gracefully ignore arguments they do not support. By not accepting **kwargs, FlashInferMLAAttnBackend violates the Liskov Substitution Principle and is fragile to any other keyword arguments passed by different model architectures or speculative decoding strategies (which can cause TypeError crashes). Adding **kwargs makes the backend robust and future-proof.

Suggested change
# DSA-family models pass the indexer's top-k selection; dense MLA
# attends the full KV (a lossless superset), safely ignored.
topk_indices: Optional[torch.Tensor] = None,
**kwargs,

…ture

The base class declares forward_extend/forward_decode with **kwargs so
backends tolerate keyword arguments they do not consume; accept that
contract here instead of enumerating topk_indices specifically.

Validated on 8x RTX 6000D with a v0.5.15 build of this patch:
TP8 + EAGLE MTP now serves GLM-5.2-NVFP4 correctly (GSM8K 20q 0.90,
greedy output identical to non-spec; bs=1 median TPOT 11.4ms vs 20.8ms
without MTP on the same host).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@AliceChenyy

Copy link
Copy Markdown
Contributor Author

Addressed the review: switched to **kwargs to match the AttentionBackend base signature (both base methods already declare **kwargs; this backend simply wasn't honoring that contract).

Two notes for maintainers:

  1. Convention survey: 13 of the 20 concrete attention backends do not accept **kwargs today (only wrapper/dispatch backends like hybrid/tbo do), so any of them would hit the same TypeError with a DSA model. The more fundamental fix would be capability negotiation at the call site (only pass topk_indices when the backend supports it) — happy to take that on in a follow-up if you prefer that direction.

  2. Validation update: with this patch on a v0.5.15 build, 8x RTX 6000D serves GLM-5.2-NVFP4 with TP8 + EAGLE MTP correctly — GSM8K 20q 0.90, greedy output identical to non-spec, bs=1 median TPOT 11.4ms vs 20.8ms without MTP (1.8x). TP4xPP2 then surfaces the next independent blocker: AssertionError: PP stage ending at layer 39 must forward DSA topk_indices because the next stage starts on a skip-topk layer — working around it via an index_topk_pattern override; not in scope here.

@AliceChenyy

Copy link
Copy Markdown
Contributor Author

Follow-up: TP4xPP2 also validated end to end with this patch (v0.5.15 build, same 8x RTX 6000D host) — GSM8K 20q 0.90, greedy output identical to TP8, full serving bench at concurrency 1/4/8/16. The skip-topk PP-boundary assertion mentioned above is cleanly avoided with --json-model-override-args '{"index_topk_pattern": "F...x78"}' (every layer computes its own indexer top-k; with the dense backend the indexer output is not consumed, so no accuracy impact). So this one-line patch is the only code change needed for both MTP and PP on SM120.

@AliceChenyy

Copy link
Copy Markdown
Contributor Author

Closing: with #26928 (flashinfer sparse MLA for GLM on SM120), the dense MLA fallback path is no longer on the critical path for our SM120 GLM deployment. The underlying interface gap (dense backend not honoring the base class **kwargs contract) still exists for other scenarios but is lower priority.

@b8zhong b8zhong mentioned this pull request Jul 15, 2026
25 of 68 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant