Skip to content

[TRTLLM-16622][perf] DeepSeek-V4.1: derive the DSpark ring window in the attention kernel and load whole ring tiles - #22365

Closed
ziyixiong-nv wants to merge 1 commit into
NVIDIA:mainfrom
ziyixiong-nv:dsv41-mb/pr19-dspark-ring-attention
Closed

ziyixiong-nv wants to merge 1 commit into
NVIDIA:mainfrom
ziyixiong-nv:dsv41-mb/pr19-dspark-ring-attention

Conversation

@ziyixiong-nv

@ziyixiong-nv ziyixiong-nv commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

@coderabbitai summary

Description

Summary

The embedded DeepSeek-V4/V4.1 DSpark draft (DSv4DSparkDraftModel) attends each request's rolling 128-row
captured-context window. The draft worker keeps that window in one page of a ring it owns. #21067 made the DSpark
window attention paged. To read these windows through it, the draft built per-forward metadata with a separate
Triton launch: a two-column page table aliasing the request's page, shifted positions, valid lengths and capacities.
With a partial window (valid_len < 128) the paged kernel also split each 64-row tile into 16-, 4- and 1-row TMA
copies issued by one thread, plus a one-row copy per unwritten row, so a partial window cost about twice a full one.

Partial windows are the common case on a disaggregated generation server. The draft's windows are not transferred
from the context server, so every request starts there with an empty window and fills it with its accepted tokens,
about 3.5 per step at the measured point. For the first ~36 decode steps of every request, all three draft stages
paid the split loads.

This PR adds trtllm::fused_dsv4_dspark_ring_attention, which takes each request's slot, start position and valid
length. A ring specialization of the paged kernel derives the window end and capacity from the start position, stages
only the slot and loads whole 64-row tiles; the softmax mask already drops the rows past valid_len. Partial windows
now cost what full ones do, and the metadata launch is gone.

Changes

  • Kernel (attention/kernels/blackwell/dspark/attention.py, attention_kernel.py): a compile-time ring
    specialization.
    • window_end maps the start position into the ring's second alias (start % 128 + 129), and window_capacity is
      the constant 2 * 128.
    • The page-table warp stages the slot for every row without reading the position or the valid length.
    • Each 64-row tile is one TMA copy (history_tile_sizes = (64,)): row r of the window is row r of the page.
    • A ring needs a 128-row page, and its unwritten rows must hold finite values because the mask, not the load, drops
      them.
  • Op (custom_ops/dspark_attention_custom_op.py):
    • trtllm::fused_dsv4_dspark_ring_attention(q, draft_block, kv_pages, slots, start_pos, valid_len, attn_sink, inverse_rope_freqs, softmax_scale), with contiguous INT64 [G] slots, start positions and valid lengths, its
      own kernel cache and a fake.
    • The input checks both ops share move into _check_dspark_attention_inputs. fused_dsv4_dspark_attention keeps its
      signature and compiles the same kernel.
    • The warmup compiles the ring op.
  • Draft (models/modeling_dspark.py):
    • dspark_attention_forward_batched and dspark_attention_forward_tp call the ring op.
    • _dspark_ring_attention_metadata and the ring_attention_metadata arguments of both paths, _forward_stage and
      forward_batched are removed.
  • speculative/dspark_ring_metadata.py (the Triton metadata launch) is removed.

Performance

All numbers are on GB300.

Kernel time per launch at the TEP8 decode shapes (8 and 16 heads):

valid_len split loads (before) whole-tile ring loads (this PR)
< 128 (partial window) 13.7-16.8 us 7.5-7.8 us
128 (full window) 7.4-7.7 us 7.5-7.8 us
  • The non-paged kernel used before [None][feat] Rubin merge back: MLA, DSpark, DWDP, Helix #21067 took 7.2-7.7 us on the same shapes for every valid_len.
  • On the low-latency TEP8 generation step, with the rest of the DS-V4.1 decode stack, deriving the window in the
    kernel cut each of the three draft attention launches from 11.1 to 9.9 us and removed the 1.4 us metadata launch:
    4.2-4.8 us per step off the attention's critical path.

Disaggregated low-latency TEP8 serving (attention TP8 generation, batch 1, DSpark k=5), 30-minute windows. This was
measured with the rest of the DS-V4.1 decode stack (the other PRs of this set), not on main alone:

draft window attention tokens/s per user GEN step p50
main's paged kernel with the Triton metadata launch 1,484.2 2.220 ms
window derived in the kernel 1,488.0 2.215 ms
plus whole-tile ring loads (this PR) 1,504.8 2.206 ms
  • The acceptance length is 3.517 in all three runs.
  • The non-paged kernel used before [None][feat] Rubin merge back: MLA, DSpark, DWDP, Helix #21067 measured 1,506.5 tokens/s per user (GEN step p50 2.209 ms).
  • Grouped by request age, the first 40 decode steps of a request now take 2 us less per step than with the non-paged
    kernel. With split loads they took 26 us more.

Compatibility

  • Scope: only the window attention of the embedded DeepSeek-V4/V4.1 DSpark draft. Standalone DSpark drafters
    (including Kimi-K3-DSpark) use the DFlash paged-KV decode and do not call these ops. There is no LLM API or
    configuration change.
  • Gating: the ring op runs wherever the fused DSpark attention ran (SM100, SM103 and SM107 with CuTe DSL, on the inputs
    the existing support checks accept). Other GPUs and shapes keep the PyTorch reference path.
  • fused_dsv4_dspark_attention keeps its signature and kernel; the ring branches are compile-time specializations.
  • Numerics: given finite unwritten rows, the ring op equals the generic op over the aliased page table bit for bit.
    The draft worker allocates and clears its windows with zeros, so draft outputs do not change.
  • Memory: unchanged, minus the four small metadata tensors per forward.

Dependencies

None. One commit on main, independent of the other PRs of this set.

#22371 (DSpark draft) is stacked on this PR: its attention-TP path passes pdl=True to the ring op, and its commit
sits on this one.

Test Coverage

  • New: test_dspark_cute_dsl_attention.py::test_ring_attention_matches_aliased_page_table (SM100/SM103/SM107). The
    ring op equals the generic op over the equivalent aliased page table bit for bit, with wrapped start positions and
    empty, partial and full windows, at 64 and 128 heads.
  • Adapted to the ring op:
    • test_dspark_cute_dsl_attention.py: the warmup, production-ring and batched scalar-reference tests.
      test_tp_attention_shards_sum_to_replicated now runs through the ring op unchanged.
    • hw_agnostic/test_dspark_attention.py: the batched-preparation test patches the ring op. The two tests of the
      removed metadata helper are dropped.

GPU run on this branch (GB300): the DSpark unit tests, including the new one, and the hw_agnostic tests pass; the
only failures are 6 test_spec_sampler_step_fold.py cases that fail on main as well. GSM8K with DSpark passes at
attention TP4 (NVFP4, the attention-TP path) and DP4 (NVFP4, the batched path). With the rest of the DS-V4.1 decode
stack, the same tests and GSM8K with DSpark MXFP4 at TP4 pass too.

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

…the attention kernel and load whole ring tiles

The embedded DeepSeek-V4/V4.1 DSpark draft keeps each request's rolling
128-row captured-context window in one page of a worker-owned ring. The
paged DSpark attention read it through per-forward metadata that a Triton
launch built: a two-column page table aliasing the page, shifted
positions, valid lengths and capacities. With a partial window
(valid_len < 128) the kernel also split each 64-row tile into 16-, 4- and
1-row TMA copies issued by one thread. A disaggregated generation server
starts every request with an empty window that fills with about 3.5
accepted tokens per step, so the first ~36 decode steps of each request
paid the split loads in all three draft stages.

- trtllm::fused_dsv4_dspark_ring_attention takes each request's slot,
  start position and valid length. A ring specialization of the paged
  kernel derives the window end and capacity from the start position,
  stages only the slot and loads whole 64-row tiles. The softmax mask
  drops the rows past valid_len, so unwritten rows must hold finite
  values; the worker zero-initializes its windows.
- The batched and attention-TP draft paths call the ring op. The Triton
  metadata launch (speculative/dspark_ring_metadata.py), its helper and
  its plumbing through the draft stages are removed.
- fused_dsv4_dspark_attention keeps its signature and kernel; the input
  checks both ops share move into one helper.

GB300, TEP8 decode shapes (8 and 16 heads), kernel time per launch:
7.5-7.8 us for every valid_len, was 13.7-16.8 us below 128 and 7.4-7.7 us
at 128; the non-paged kernel used before NVIDIA#21067 took 7.2-7.7 us. On the
low-latency TEP8 GEN step with the rest of the DS-V4.1 decode stack,
deriving the window in the kernel cut each draft attention launch from
11.1 to 9.9 us and removed a 1.4 us launch. Disaggregated low-latency TEP8
serving (batch 1, DSpark k=5, same stack): 1,484.2 -> 1,504.8 tokens/s
per user, GEN step p50 2.220 -> 2.206 ms, acceptance length 3.517 in both.

Test: test_ring_attention_matches_aliased_page_table checks the ring op
bit for bit against the generic op over the equivalent aliased page table
(wrapped starts, partial and empty windows). The DSpark attention tests
that patched the generic op or built the metadata now use the ring op.

Signed-off-by: ziyixiong-nv <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.

Next included review available in 57 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 12 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3a426e2 and 03d3da0.

Files selected for processing: 7

  • tensorrt_llm/_torch/attention/kernels/blackwell/dspark/attention.py
  • tensorrt_llm/_torch/attention/kernels/blackwell/dspark/attention_kernel.py
  • tensorrt_llm/_torch/custom_ops/dspark_attention_custom_op.py
  • tensorrt_llm/_torch/models/modeling_dspark.py
  • tensorrt_llm/_torch/speculative/dspark_ring_metadata.py
  • tests/unittest/_torch/speculative/hw_agnostic/test_dspark_attention.py
  • tests/unittest/_torch/speculative/test_dspark_cute_dsl_attention.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@yuxianq

yuxianq commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

@kris1025, could you help to review this PR as trt-llm-torch-attention-devs? Thanks~

Assignment explanation
Componentskernels/cute-dsl-mla (85%); runtime/metadata-workspaces (15%)
Tagsalgorithm:dense (100%); representation:mla (100%); phase:decode (100%); features:paged-kv (60%); features:speculative-decoding (40%)
PR effort8 points — The optimization changes the metadata/kernel contract and memory-loading behavior. Review must establish wraparound and empty-window equivalence, masked-tail numerical safety, and preservation of the generic specialization.
ImportanceImportant — Ring mode replaces boundary-sensitive loads with whole tiles and derives window_end as start % window + window + 1. Correctness now depends on ring coordinates, slot validity and safe values in loaded but masked rows.
Affinity (0–1)0.817 component/tag match; 0.613 after PIC preference
Outstanding points at selection63 → 71 (+8)
Points already reserved on this PR0
Effective weight1
Completed review points (preceding 168 hours)3
Combined points after assignment74
Demand-derived target63.00 points (soft share)
Additional fair share before assignment0.00 points
Share explanationWhole PR / qualification or coverage constraint exceeds a soft share; assignment is allowed.
Selection ruleaffinity with 0.3-share guard
Assignment scope (kris1025)all

Affinity and workload are snapshots at selection, not live counters. Existing reservations on this PR are counted once.

@yuxianq
yuxianq requested review from kris1025 and removed request for farazkh80 and pengbowang-nv November 16, 2026 16:05
@ziyixiong-nv

Copy link
Copy Markdown
Collaborator Author

Closing as we're going to enable ctx DSpark in disagg mode.

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.

2 participants