Skip to content

[Feat] [NVIDIA] attention: Support DSA dense prefill on SM120 - #51473

Merged
BBuf merged 5 commits into
sgl-project:mainfrom
lingxling:feat/dsa-dense-one-shot-sm120-fa4
Nov 30, 2026
Merged

BBuf merged 5 commits into
sgl-project:mainfrom
lingxling:feat/dsa-dense-one-shot-sm120-fa4

Conversation

@lingxling

@lingxling lingxling commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes #51472.

SGLANG_DSA_PREFILL_DENSE_ATTN_KV_LEN_THRESHOLD controls when an eligible DSA prefill batch uses dense one-shot attention instead of sparse attention. SM120 is currently excluded from that selection, so setting the threshold on an RTX 5090 has no effect.

The existing CUDA dispatch cannot be enabled by changing only the architecture gate: _forward_standard_mha() sends every CUDA architecture with major version 10 or newer to flashinfer.prefill.trtllm_ragged_attention_deepseek, whose TllmGenFmhaRunner rejects SM120 with Unsupported architecture.

SGLang already ships an architecture-owned SM120 FA4 varlen implementation. Fixed-random-input validation on an RTX 5090 shows that it supports the packed ragged BF16 attention contract consumed by DSA dense one-shot prefill, including causal attention and asymmetric Q/K/V dimensions. This change wires that existing implementation into DSA dense one-shot prefill without changing the SM100-family or SM90 kernel paths.

Modifications

  • Enable DSA MHA_ONE_SHOT selection on CUDA SM120 when the existing length, dtype, chunk-capacity, CP, and HiSparse conditions are satisfied.
  • Route the enabled SM120 dense one-shot path to sglang.kernels.ops.attention.flash_attention_v4_sm120.flash_attn_varlen_func before the existing TRT-LLM branch.
  • Keep the existing FlashInfer TRT-LLM ragged-attention path for the SM100 family and the existing FlashAttention path for SM90.
  • Keep SM120 prefill on the sparse path when --enable-return-indexer-topk is active, because dense MHA does not run the indexer and therefore cannot populate its capture buffer.
  • Keep SM120 prefill on the sparse path when SGLANG_SKIP_SOFTMAX_PREFILL_THRESHOLD_SCALE_FACTOR is set, because the dense FA4 path does not consume that sparse FlashInfer option.
  • Preserve deterministic zeroing of planned padded extend rows through a helper shared by the SM120 and SM100-family dense paths.
  • Add CPU policy tests for supported CUDA architectures, the non-CUDA major-12 case, and the two sparse-semantics runtime options.
  • Add an SM120 registered integration test that uses a tiny randomly initialized projection module, requires MHA_ONE_SHOT selection instead of silently skipping, exercises ragged requests with non-empty prefixes, and compares the result with an independent PyTorch attention reference.

The existing SM120 FA4 kernel tests provide additional coverage for packed ragged BF16 attention and asymmetric Q/K/V dimensions against a PyTorch reference.

Accuracy Tests

python3 test/registered/unit/layers/attention/test_dsa_dense_one_shot_support.py -v
CUDA_VISIBLE_DEVICES=0 python3 \
  test/registered/attention/unittests/dsa/test_dsa_sm120_dense.py -v
CUDA_VISIBLE_DEVICES=0 python3 -m pytest -q \
  'test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py::test_sm120_varlen_feature_matrix_matches_reference[bf16-gqa-hd192-hdv128-causal-local-splitkv]'
CPU policy tests: 3 passed
DSA SM120 integration test: 1 passed
SM120 FA4 asymmetric Q/K/V reference test: 1 passed

The CPU tests cover the architecture gate and the two sparse-only runtime options. The integration test forces MHA_ONE_SHOT selection and compares the result with an independent PyTorch reference; the existing FA4 test additionally covers asymmetric Q/K/V dimensions.

Speed Tests and Profiling

The layer-level comparison was run twice on one RTX 5090 at the public GLM-5 DSA dimensions (64 heads, QK 256, V 256, KV LoRA rank 512, top-k 2048). Each result is the median of 100 iterations after 10 warmups for a batch-one, prefix-free causal prefill. The measured kernel sources match PR head ff3117691d byte-for-byte.

KV pool Input tokens Base sparse median (ms) PR dense FA4 median (ms) Speedup (base / PR)
BF16 512 0.3417 0.0687 4.97x
BF16 1024 1.1665 0.2242 5.20x
BF16 2048 4.2328 0.7800 5.43x
FP8 E4M3 512 0.1781 0.0686 2.60x
FP8 E4M3 1024 0.3928 0.2249 1.75x
FP8 E4M3 2048 1.3129 0.7809 1.68x

The FP8 baseline is the current GLM/SM120 flashinfer_sparse_mla route. Because FlashMLA BF16 sparse prefill rejects SM120, the runnable triton_sparse_mla backend is used for the BF16 baseline. Prefix-free dense one-shot consumes fresh BF16 K/V in both KV-pool modes. Cache packing is outside the timed attention region. A second run was consistent with the table, and every output had the expected shape with finite values.

Environment: RTX 5090 (SM120), driver 595.71.05, CUDA 13.0, PyTorch 2.14.1+cu130, FlashInfer 0.7.4, Triton 3.8.0, and nvidia-cutlass-dsl 4.8.0.

The SM90, SM100-family, ROCm, and normal sparse-prefill paths are unchanged.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ✅ Run #46248876197
Latest PR Test (Extra): ⊘ Skipped -- add the run-ci-nv-extra label to opt in.
Latest PR Test (AMD ROCm 10): ⚠️ Run #46248879353

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

@claude claude 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.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@lingxling

Copy link
Copy Markdown
Contributor Author

@Fridge003 could you take a look at this PR when you have a chance?

This adds SM120 support for DSA dense one-shot prefill by routing the SM12x path to the existing FA4 varlen implementation, while leaving the existing SM100 and SM90 paths unchanged. I validated the path on an RTX 5090 with the capability test, DSA integration test, and the SM120 FA4 kernel contract test.

Could you help trigger CI and review it? Thanks!

@BBuf

BBuf commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] Thanks for the PR. Before it goes into review, could you add these to the description?

  1. Prefill numbers on an SM120 GPU with a real DSA checkpoint, main vs this PR: TTFT or prefill throughput for prompts under the dense threshold (for example 512, 1024 and 2048 tokens), plus one prompt above it. With the default threshold (2048), short prefills on SM120 switch from sparse attention to the dense FA4 path, so the numbers should show that switch is not slower.
  2. An end-to-end accuracy check on the same model, main vs this PR (GSM8K is fine). The current tests use a tiny random module.
  3. One run with an FP8 KV cache in 1 or 2. The dense path is also selected when the KV pool is float8_e4m3fn, and the tests only cover BF16.

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[BBuf Review Agent] A few things to fix before this can go in.

@lingxling

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. I have seen all three comments and am working through them.

I will update the implementation to handle the return-indexer capture path correctly and to keep prefill on the sparse path when SGLANG_SKIP_SOFTMAX_PREFILL_THRESHOLD_SCALE_FACTOR is enabled.

I am also preparing an SM120 setup with a public real DSA checkpoint. I will add base/head prefill results at 512, 1024, and 2048 tokens, plus a prompt above the dense threshold, and run the requested end-to-end accuracy and FP8 KV-cache validation on the same checkpoint.

This may take a little time because of the model size and GPU requirements, but I am actively working on it and will update the PR with both the fixes and the results. Thanks!

@lingxling
lingxling force-pushed the feat/dsa-dense-one-shot-sm120-fa4 branch from 94bb88e to 589ea59 Compare November 23, 2026 15:23
@BBuf

BBuf commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] The only numbers I need are base/head prefill times at 512, 1024 and 2048 tokens on SM120 with BF16 and FP8 KV, since every DSA prefill up to the 2048 threshold now switches to dense FA4. Layer-level attention timings at the real DSA shapes are fine if a full checkpoint doesn't fit; longer prompts don't change path, so skip them.

@lingxling

Copy link
Copy Markdown
Contributor Author

@BBuf Thanks for the clarification. I have completed the requested layer-level base/head measurements on an RTX 5090 (SM120) at the public GLM-5 DSA shapes, covering 512, 1024, and 2048 tokens with both BF16 and FP8 KV pools. I ran the benchmark twice and obtained consistent results; dense FA4 was faster than the runnable sparse baseline in every case. The methodology, environment, results, and BF16 backend caveat are now included in the PR description. The requested code-path fixes are already present in the current PR head.

@BBuf

BBuf commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @lingxling The 5090 table covers the BF16 and FP8 KV comparisons I asked for, and the numbers look good. I also checked both sparse-path guards at ff3117691d.

@lingxling

Copy link
Copy Markdown
Contributor Author

[BBuf Review Agent] @lingxling The 5090 table covers the BF16 and FP8 KV comparisons I asked for, and the numbers look good. I also checked both sparse-path guards at ff3117691d.

Thanks for confirming the measurements and both sparse-path guards. All review threads are now addressed and resolved. Could you please help move the PR forward and trigger CI when convenient? @BBuf

@BBuf

BBuf commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @lingxling The 5090 test test_ragged_prefix_matches_reference still skips if the DSA constructor raises an assertion or import error at dsa_attention.py:657, before it checks require_mha. Could you pass require_mha into build_dsa_attention_fixture and re-raise those errors when it’s set?

@lingxling

Copy link
Copy Markdown
Contributor Author

Fixed in c01ce38baa. run_dsa_attention_case now passes require_mha into build_dsa_attention_fixture. When MHA is required, backend construction errors are re-raised instead of being converted into a skip, so the SM120 test can no longer pass by skipping before the use_mha check. The earlier RTX 5090 run completed this test with ok, rather than skipped. @BBuf

@BBuf

BBuf commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @lingxling Checked c01ce38baa. The 5090 test’s require_mha=True reaches the fixture, and dsa_attention.py:660 re-raises constructor assertion and import errors before skipTest.

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[BBuf Review Agent] LGTM. Let's wait for CI.

@sglang-bot

Copy link
Copy Markdown
Member

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Nov 27, 2026
@sglang-bot

Copy link
Copy Markdown
Member

[sglang-bot Review Agent] Could you merge the latest main into this branch? CI's maintenance gate won't run the tests until it does.

@lingxling

Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@github-actions

Copy link
Copy Markdown
Contributor

No commit was approved for CI: lingxling has neither write access nor can_tag_run_ci_label in .github/CI_PERMISSIONS.json.

@github-actions

Copy link
Copy Markdown
Contributor

⛔ CI skipped its tests, so nothing was re-run. To run them, have a maintainer approve this commit (/approve-ci), then comment /rerun-failed-ci.

@lingxling

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@github-actions

Copy link
Copy Markdown
Contributor

⛔ CI skipped its tests, so nothing was re-run. To run them, have a maintainer approve this commit (/approve-ci), then comment /rerun-failed-ci.

@lingxling

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@github-actions

Copy link
Copy Markdown
Contributor

⛔ CI skipped its tests, so nothing was re-run. To run them, have a maintainer approve this commit (/approve-ci), then comment /rerun-failed-ci.

@lingxling

Copy link
Copy Markdown
Contributor Author

Hi @sglang-bot, thanks for reviewing the latest commit. I tried /rerun-failed-ci, but GitHub Actions says a maintainer needs to run /approve-ci first.

Could you please run /approve-ci for PR #51473? I'll retrigger CI afterward. Thanks!

#51473

@sglang-bot

Copy link
Copy Markdown
Member

[sglang-bot Review Agent] @lingxling I’m good with running CI on d99a7dc. The approval triggers the rerun automatically, so you can skip the manual retry.

@sglang-bot sglang-bot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[sglang-bot Review Agent] LGTM. Let's wait for CI.

@sglang-bot

Copy link
Copy Markdown
Member

/tag-and-rerun-ci

@sglang-bot

Copy link
Copy Markdown
Member

[sglang-bot Review Agent] Could you rebase this onto the latest main? It conflicts with main now.

@lingxling
lingxling force-pushed the feat/dsa-dense-one-shot-sm120-fa4 branch from d99a7dc to 6b58a14 Compare November 28, 2026 03:01
@lingxling

Copy link
Copy Markdown
Contributor Author

Rebased onto the latest main (098c51af2e) and resolved the conflict. The PR head is now 6b58a14ab5 @sglang-bot

@sglang-bot

Copy link
Copy Markdown
Member

[sglang-bot Review Agent] @lingxling Confirmed 6b58a14ab5 is based on 098c51af2e. That addresses the rebase request.

@lingxling

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@github-actions

Copy link
Copy Markdown
Contributor

⛔ CI skipped its tests, so nothing was re-run. To run them, have a maintainer approve this commit (/approve-ci), then comment /rerun-failed-ci.

@sglang-bot sglang-bot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[sglang-bot Review Agent] LGTM. Let's wait for CI.

@sglang-bot

Copy link
Copy Markdown
Member

/tag-and-rerun-ci

@sglang-bot

Copy link
Copy Markdown
Member

/rerun-failed-ci

@sglang-bot

Copy link
Copy Markdown
Member

[sglang-bot Review Agent] Could you merge the latest main into this branch? test/registered/core/test_rust_server_startup.py changed on main after this branch (e6e6915 [Fix] rust-server: Fix test_rust_server_startup after #53454 by raising its cont), and a CI rerun replays the old code.

@lingxling

Copy link
Copy Markdown
Contributor Author

@sglang-bot I’ve merged the latest main into this branch as requested. The new head is ddc570060e, and test/registered/core/test_rust_server_startup.py now matches main.

Could you please approve the new head for CI and trigger /tag-and-rerun-ci as soon as possible? Since main is moving quickly, a delay may leave the branch out of date and require another merge. Thanks!

@sglang-bot

Copy link
Copy Markdown
Member

/tag-and-rerun-ci

@sglang-bot

Copy link
Copy Markdown
Member

/rerun-failed-ci

@sglang-bot

Copy link
Copy Markdown
Member

[sglang-bot Review Agent] @lingxling these still fail after a rerun, and I can't tell from the logs whether it's this PR. Could you take a look?

  • base-b-test-1-gpu-small (6): ✗ FAILED: /actions-runner/_work/sglang/sglang/test/registered/unit/spec/test_eagle_post_verify_graphs.py returned exit code 1

@sglang-bot

Copy link
Copy Markdown
Member

/tag-and-rerun-ci

@lingxling

Copy link
Copy Markdown
Contributor Author

Hi @BBuf , base CI for #51473 is now green on head 462fcd9a29, including the previously failing EAGLE test. The PR is approved and ready to merge. Could you please merge it when convenient? Thanks!

@BBuf
BBuf merged commit aaacf50 into sgl-project:main Nov 30, 2026
115 of 116 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [NVIDIA] DSA dense-prefill threshold is ignored on SM120 despite native FA4 support

3 participants