Skip to content

[TRTLLM-14345][feat] Support GDN MTP Replay#16464

Merged
JadoTu merged 4 commits into
NVIDIA:mainfrom
JadoTu:add_gdn_replay_function
Jul 20, 2026
Merged

[TRTLLM-14345][feat] Support GDN MTP Replay#16464
JadoTu merged 4 commits into
NVIDIA:mainfrom
JadoTu:add_gdn_replay_function

Conversation

@JadoTu

@JadoTu JadoTu commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added an optimized cached replay path for GDN/Mamba speculative decoding verification.
    • Supports efficient replay state updates and synchronized history commits across layers.
    • Added optional fused gating and improved support for packed QKV inputs.
  • Performance

    • Enables faster GPU execution for compatible hardware and workloads.
    • Adds optimized handling for larger decode batches and CUDA graph execution.
  • Bug Fixes

    • Improved handling of padded or invalid replay slots without modifying cached state.
  • Tests

    • Added coverage validating numerical correctness, state updates, data types, and CUDA graph behavior.

-->

Description

This change adds an opt-in cached replay path for Gated DeltaNet state updates during one-model MTP verification in the PyTorch backend.

Instead of materializing and retaining a full recurrent state for every draft position, cached replay stores a compact update history and reconstructs the accepted checkpoint state only when needed. This reduces MTP state-cache memory and avoids redundant replay computation while preserving the existing state precision.

The implementation includes:

  • A fused replay-and-commit path optimized for small batches.
  • A partitioned replay path with one fused all-layer commit launch for larger batches.
  • Direct consumption of the packed mixed-QKV buffer to avoid additional input-copy operations.
  • CUDA graph and eager-mode support.
  • Integration with the shared Qwen3-Next runtime used by Qwen3-Next and Qwen3.5 text models.
  • Recurrent-reference and CUDA-graph correctness coverage.

Cached replay is disabled by default because its end-to-end benefit is workload-dependent. Set TRTLLM_USE_GDN_REPLAY=1 to enable it.

The replay function can dramatically saving the buffer of SSM used.

Estimated GDN MTP cache HBM saving

Qwen3.5-397B-A17B-NVFP4 | B200 | TEP4 | T=3 | HIST=16 | BF16 state

Batch size No replay (GiB/GPU) Cached replay (GiB/GPU) Saved (GiB/GPU) Reduction
4 0.352 0.056 0.295 84.0%
16 1.406 0.141 1.265 90.0%
64 5.625 0.479 5.146 91.5%
128 11.250 0.929 10.321 91.7%
256 22.500 1.830 20.670 91.9%

end-to-end perf comparisons.

image image image

kernel microbenchmarks

image

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.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

Signed-off-by: jiant <107457950+JadoTu@users.noreply.github.com>
@JadoTu
JadoTu requested review from a team as code owners July 16, 2026 02:17
@JadoTu JadoTu changed the title [None][feat] Support GDN MTP replay [None][feat] Support GDN MTP Replay Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds Triton cached-replay kernels for GDN verification and history commits, wires them into Qwen3-Next speculative decoding, adds runtime gating and cache-manager coordination, and validates numerical correctness, state updates, invalid slots, and CUDA-graph execution.

Changes

GDN cached replay

Layer / File(s) Summary
Cached replay kernels and dispatch
tensorrt_llm/_torch/modules/fla/cached_replay.py
Adds replay and layered history-commit Triton kernels with PyTorch wrappers, tensor validation, tiling selection, optional gating, packed-QKV handling, state commits, and PDL support.
Replay enablement and state commits
tensorrt_llm/_torch/pyexecutor/_util.py, tensorrt_llm/_torch/pyexecutor/mamba_cache_manager.py
Gates replay by speculative-decoding configuration and hardware/cache constraints, exposes all-layer commit state, and invokes layered history commits during Mamba state updates.
Speculative verification integration
tensorrt_llm/_torch/modules/mamba/gdn_mixer.py
Routes decode and extend target verification through cached replay, passes replay metadata and buffers, and coordinates dependent convolution launches.
Replay correctness and dispatch tests
tests/unittest/_torch/modules/mamba/test_gdn_replay_recurrent.py
Tests reference equivalence, legacy-path equivalence, fused gating, invalid slots, all-layer commits, state bookkeeping, and eager versus CUDA-graph execution.

Estimated code review effort: 5 (Critical) | ~120 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ForwardCore
  participant GDNMixer
  participant ReplayUpdate
  participant MambaCacheManager
  participant LayerCommit
  ForwardCore->>GDNMixer: pass replay metadata and cached state
  GDNMixer->>ReplayUpdate: run cached recurrent verification
  ReplayUpdate-->>GDNMixer: return verification output
  MambaCacheManager->>LayerCommit: commit replay history layers
  LayerCommit->>MambaCacheManager: update SSM state buffers
Loading

Suggested reviewers: vallis-neria

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise, follows the required ticket/type pattern, and clearly matches the cached replay feature change.
Description check ✅ Passed The PR description covers the change, rationale, and checklist; test coverage is mentioned in prose but not in a dedicated section.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
tensorrt_llm/_torch/modules/fla/cached_replay.py (2)

745-745: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Write replay results directly into the provided output buffer.

The current hot path always allocates a full output tensor and then performs a second full-size device copy.

  • tensorrt_llm/_torch/modules/fla/cached_replay.py#L745-L745: accept an optional output tensor and launch directly into it.
  • tensorrt_llm/_torch/modules/mamba/gdn_mixer.py#L518-L520: pass output_d through and remove the post-kernel copy.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/_torch/modules/fla/cached_replay.py` at line 745, Update
tensorrt_llm/_torch/modules/fla/cached_replay.py lines 745-745 to accept an
optional output tensor and use it as the replay destination, allocating a new
tensor only when none is provided. Update
tensorrt_llm/_torch/modules/mamba/gdn_mixer.py lines 518-520 to pass output_d
into the replay call and remove the subsequent full-size device copy.

494-508: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the public replay interfaces and tensor contracts.

These exported functions omit argument shapes, dtypes, mutation behavior, and return semantics despite relying on strict CUDA layouts.

As per coding guidelines, “document public function arguments, and include tensor dimensions and constrained dtypes where applicable.”

Also applies to: 619-650

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/_torch/modules/fla/cached_replay.py` around lines 494 - 508,
Document the public replay interfaces, including
commit_gdn_cached_replay_history_layers and the functions around its related
replay entry points, with each argument’s tensor shape, required dtype, mutation
behavior, and return semantics. Specify constrained layouts and
scalar/configuration types for parameters such as replay_work_items, n_writes,
history_size, and commit tuning options, while preserving the existing API
behavior.

Source: Coding guidelines

tests/unittest/_torch/modules/mamba/test_gdn_replay_recurrent.py (1)

379-513: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

Extend coverage to every enabled dtype and the cache-manager dispatch.

Kernel-level bfloat16 coverage is sufficient, but end-to-end coverage is insufficient:

  • Parametrize this file’s all-layer commit and CUDA-graph tests for float16, bfloat16, and float32, matching _util.py.
  • Add tests/unittest/_torch/pyexecutor/test_mamba_cache_manager.py cases around 15/16 decodes to verify commit ordering, PNAT/buffer flips, and disabled replay.

As per path instructions, test feedback must identify concrete files and state whether coverage is sufficient.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unittest/_torch/modules/mamba/test_gdn_replay_recurrent.py` around
lines 379 - 513, Extend the relevant all-layer commit and CUDA-graph tests,
including test_gdn_cached_replay_dispatch_cuda_graph_matches_eager, with a dtype
parameter covering float16, bfloat16, and float32 as defined in _util.py, and
create cache-manager tests around the 15/16 decode boundary in
test_mamba_cache_manager.py. Verify commit ordering, PNAT and buffer-index
flips, and behavior when replay is disabled; report concrete test files and
whether the resulting coverage is sufficient.

Source: Path instructions

tensorrt_llm/_torch/modules/mamba/gdn_mixer.py (1)

457-475: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Annotate the replay verification contract.

Add precise tensor, metadata, optional-buffer, integer, boolean, and return types. This method crosses kernel, cache-manager, and mixer boundaries, so untyped parameters obscure its strict contract.

As per coding guidelines, “Annotate every function” and avoid imprecise types.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/_torch/modules/mamba/gdn_mixer.py` around lines 457 - 475,
Annotate the complete signature of _replay_verify_recurrent with precise types
for all tensor inputs, replay metadata, cache/work-item objects, integer and
boolean arguments, optional buffers, and the return value. Reuse established
tensor, metadata, cache-manager, and mixer type aliases from the surrounding
module or project, and express optional parameters explicitly rather than using
imprecise types.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tensorrt_llm/_torch/pyexecutor/_util.py`:
- Around line 1996-2000: Require TRTLLM_USE_GDN_REPLAY to equal exactly "1"
before enabling replay in the _util.py replay configuration, treating all other
values as disabled. Apply the same exact-value parsing and consistent disabled
logging in gdn_mixer.py so both sites share identical opt-in behavior.

---

Nitpick comments:
In `@tensorrt_llm/_torch/modules/fla/cached_replay.py`:
- Line 745: Update tensorrt_llm/_torch/modules/fla/cached_replay.py lines
745-745 to accept an optional output tensor and use it as the replay
destination, allocating a new tensor only when none is provided. Update
tensorrt_llm/_torch/modules/mamba/gdn_mixer.py lines 518-520 to pass output_d
into the replay call and remove the subsequent full-size device copy.
- Around line 494-508: Document the public replay interfaces, including
commit_gdn_cached_replay_history_layers and the functions around its related
replay entry points, with each argument’s tensor shape, required dtype, mutation
behavior, and return semantics. Specify constrained layouts and
scalar/configuration types for parameters such as replay_work_items, n_writes,
history_size, and commit tuning options, while preserving the existing API
behavior.

In `@tensorrt_llm/_torch/modules/mamba/gdn_mixer.py`:
- Around line 457-475: Annotate the complete signature of
_replay_verify_recurrent with precise types for all tensor inputs, replay
metadata, cache/work-item objects, integer and boolean arguments, optional
buffers, and the return value. Reuse established tensor, metadata,
cache-manager, and mixer type aliases from the surrounding module or project,
and express optional parameters explicitly rather than using imprecise types.

In `@tests/unittest/_torch/modules/mamba/test_gdn_replay_recurrent.py`:
- Around line 379-513: Extend the relevant all-layer commit and CUDA-graph
tests, including test_gdn_cached_replay_dispatch_cuda_graph_matches_eager, with
a dtype parameter covering float16, bfloat16, and float32 as defined in
_util.py, and create cache-manager tests around the 15/16 decode boundary in
test_mamba_cache_manager.py. Verify commit ordering, PNAT and buffer-index
flips, and behavior when replay is disabled; report concrete test files and
whether the resulting coverage is sufficient.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 9296ea1d-a7b3-4ca5-a1ef-28911b44bcfa

📥 Commits

Reviewing files that changed from the base of the PR and between ef90708 and 4c4c2ce.

📒 Files selected for processing (5)
  • tensorrt_llm/_torch/modules/fla/cached_replay.py
  • tensorrt_llm/_torch/modules/mamba/gdn_mixer.py
  • tensorrt_llm/_torch/pyexecutor/_util.py
  • tensorrt_llm/_torch/pyexecutor/mamba_cache_manager.py
  • tests/unittest/_torch/modules/mamba/test_gdn_replay_recurrent.py

Comment thread tensorrt_llm/_torch/pyexecutor/_util.py
@nv-guomingz nv-guomingz changed the title [None][feat] Support GDN MTP Replay [TRTLLM-14345][feat] Support GDN MTP Replay Jul 16, 2026
Comment thread tensorrt_llm/_torch/modules/mamba/gdn_mixer.py Outdated
JadoTu added 2 commits July 17, 2026 08:54
Signed-off-by: jiant <107457950+JadoTu@users.noreply.github.com>
Signed-off-by: jiant <107457950+JadoTu@users.noreply.github.com>
Signed-off-by: Jian Tu <107457950+JadoTu@users.noreply.github.com>
@JadoTu

JadoTu commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59976 [ run ] triggered by Bot. Commit: ac0e149 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59976 [ run ] completed with state FAILURE. Commit: ac0e149
/LLM/main/L0_MergeRequest_PR pipeline #48370 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@JadoTu

JadoTu commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60102 [ run ] triggered by Bot. Commit: ac0e149 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60102 [ run ] completed with state FAILURE. Commit: ac0e149
/LLM/main/L0_MergeRequest_PR pipeline #48489 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@JadoTu

JadoTu commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60138 [ run ] triggered by Bot. Commit: ac0e149 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60138 [ run ] completed with state SUCCESS. Commit: ac0e149
/LLM/main/L0_MergeRequest_PR pipeline #48518 completed with status: 'SUCCESS'

CI Report

Link to invocation

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

Reviewed the GDN MTP cached replay change. The feature is opt-in via TRTLLM_USE_GDN_REPLAY=1 and remains disabled by default; enablement is additionally gated by the speculative/cache/hardware constraints, so the default execution path is not broadened. The earlier env-var ambiguity was fixed by centralizing is_gdn_replay_enabled() as an exact "1" check, with a parameterized test.\n\nThe patch includes meaningful coverage: replay-vs-reference/legacy numerical checks, invalid-slot behavior, all-layer commit reference checks, CUDA-graph-vs-eager checks, and a B200 e2e accuracy entry. Final Blossom CI on the head commit is green and there are already current member approvals. I also ran an independent Claude cross-check before approving; it agreed this is safe to approve.\n\nNon-blocking follow-ups: document the public replay tensor contracts more explicitly, and consider extending dtype/cache-manager boundary coverage as CodeRabbit suggested.

@JadoTu
JadoTu enabled auto-merge (squash) July 20, 2026 03:25

@VALLIS-NERIA VALLIS-NERIA 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.

LGTM for tensorrt_llm/_torch/pyexecutor/mamba_cache_manager.py

@zhaoyangwang-nvidia zhaoyangwang-nvidia 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.

LGTM

@JadoTu
JadoTu merged commit ee241d2 into NVIDIA:main Jul 20, 2026
8 checks passed
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.

8 participants