Skip to content

[None][fix] SA spec dec: promote accepted hybrid recurrent states in-worker#16759

Open
brnguyen2 wants to merge 1 commit into
NVIDIA:mainfrom
brnguyen2:port/sa-worker-hybrid-state-promotion
Open

[None][fix] SA spec dec: promote accepted hybrid recurrent states in-worker#16759
brnguyen2 wants to merge 1 commit into
NVIDIA:mainfrom
brnguyen2:port/sa-worker-hybrid-state-promotion

Conversation

@brnguyen2

@brnguyen2 brnguyen2 commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Description

SAWorker (standalone suffix-automaton speculative decoding) never commits accepted verification-step recurrent states for SSM/hybrid models. Verification writes per-step states into the cache manager's SpeculativeState scratch buffers — the live pools are read-only during verify, since a rejected draft must not corrupt them — but nothing promotes the accepted step back into the live pools afterwards. The result is silent recurrent-state corruption on any hybrid (Mamba/GDN/linear-attention) model running standalone SA spec-dec.

Fix: call update_mamba_states right after on-device acceptance, at the same call site the other one-engine workers (dflash, eagle3, mtp_dynamic_tree) already use. The call is gated on isinstance(attn_metadata.kv_cache_manager, MambaHybridCacheManager) and num_gens > 0, so pure-attention models and context-only batches are unaffected.

Test Coverage

New tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py (mock-based, no GPU/model needed, <1s):

  • hybrid cache manager + generation batch → update_mamba_states called exactly once with the sampler's num_accepted_tokens and the metadata's state_indices
  • hybrid cache manager + context-only batch → not called
  • pure-attention cache manager → not called

The promotion test fails against the pre-fix worker and passes with the fix. Added to the H100 L0 pre-merge list (l0_h100.yml) alongside the other speculative unit tests.

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.

Dev Engineer Review

  • Updated tensorrt_llm/_torch/speculative/sa_worker.py to import MambaHybridCacheManager and, in SAWorker._forward_impl, call attn_metadata.kv_cache_manager.update_mamba_states(...) immediately after _sample_and_accept_draft_tokens (i.e., after accepted/verified tokens are determined).
  • Promotion is gated to hybrid generation batches only: num_gens = batch_size - attn_metadata.num_contexts and num_gens > 0, plus isinstance(attn_metadata.kv_cache_manager, MambaHybridCacheManager). Context-only batches (num_contexts > 0) and pure-attention (non-MambaHybridCacheManager) managers skip promotion.
  • The call promotes the accepted verification-step recurrent state using attn_metadata, num_accepted_tokens, and state_indices=attn_metadata.mamba_metadata.state_indices.

QA Engineer Review

  • Added tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py with 3 new tests:
    • test_hybrid_manager_promotes_accepted_states: asserts update_mamba_states is called once with attn_metadata, num_accepted_tokens, and state_indices, and that result["new_tokens"] returns the mocked accepted tokens.
    • test_hybrid_manager_context_only_batch_skips_promotion: asserts update_mamba_states is not called when num_contexts > 0.
    • test_pure_attention_manager_skips_promotion: asserts update_mamba_states is not called when the KV cache manager is not a MambaHybridCacheManager.
  • Added unittest/_torch/speculative/test_sa_hybrid_state_promotion.py to tests/integration/test_lists/test-db/l0_h100.yml under the l0_h100 list.
  • Verdict: sufficient.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 7cfade9f-2207-401f-ab27-b0872c7eb879

📥 Commits

Reviewing files that changed from the base of the PR and between b665066 and b6dbcce.

📒 Files selected for processing (3)
  • tensorrt_llm/_torch/speculative/sa_worker.py
  • tests/integration/test_lists/test-db/l0_h100.yml
  • tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • tests/integration/test_lists/test-db/l0_h100.yml
  • tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py
  • tensorrt_llm/_torch/speculative/sa_worker.py

Walkthrough

SA speculative decoding now conditionally promotes accepted-token recurrent state for Mamba hybrid cache managers. Regression tests cover generation batches, context-only batches, non-hybrid managers, and Hopper pre-merge test-list integration.

Changes

SA Hybrid State Promotion

Layer / File(s) Summary
Hybrid state update and regression coverage
tensorrt_llm/_torch/speculative/sa_worker.py, tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py, tests/integration/test_lists/test-db/l0_h100.yml
SAWorker calls update_mamba_states for eligible hybrid generation batches, while tests verify promotion arguments, returned tokens, skip conditions, and Hopper pre-merge test-list integration.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SAWorker
  participant MambaHybridCacheManager
  participant LiveStatePools
  SAWorker->>MambaHybridCacheManager: update_mamba_states(attn_metadata, num_accepted_tokens, state_indices)
  MambaHybridCacheManager->>LiveStatePools: promote accepted recurrent state
Loading

Suggested reviewers: qijune, yiqingy0, xinhe-nv, yihuilu512

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and clearly describes the main fix with the required [None][fix] format.
Description check ✅ Passed The description covers the issue, solution, test coverage, and checklist sections required by the template.
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.
✨ 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 (1)
tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py (1)

36-58: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Annotate the new test functions and helpers.

_make_metadata, _run_forward, and all three test functions are unannotated. Add precise parameter and return annotations, including -> None for tests.

As per coding guidelines, every function must be annotated.

Also applies to: 61-93

🤖 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/speculative/test_sa_hybrid_state_promotion.py` around
lines 36 - 58, Add complete parameter and return type annotations to
_make_metadata and _run_forward, using appropriate types for metadata, worker,
and returned values. Annotate all three test functions in this file with their
precise parameter types and -> None return types, without changing test
behavior.

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 `@tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py`:
- Around line 61-93: The tests lack coverage for mixed batches and the new test
module is not registered in test lists. Add a test alongside
test_hybrid_manager_promotes_accepted_states and
test_hybrid_manager_context_only_batch_skips_promotion using 0 < num_contexts <
num_seqs, asserting accepted-state promotion occurs appropriately; also register
test_sa_hybrid_state_promotion.py in the relevant
tests/integration/test_lists/test-db or qa list(s).

---

Nitpick comments:
In `@tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py`:
- Around line 36-58: Add complete parameter and return type annotations to
_make_metadata and _run_forward, using appropriate types for metadata, worker,
and returned values. Annotate all three test functions in this file with their
precise parameter types and -> None return types, without changing test
behavior.
🪄 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: 7420fe6f-beda-4871-9ca1-f84d916def05

📥 Commits

Reviewing files that changed from the base of the PR and between b294868 and 09bf8a8.

📒 Files selected for processing (2)
  • tensorrt_llm/_torch/speculative/sa_worker.py
  • tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py

Comment thread tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py
@brnguyen2
brnguyen2 force-pushed the port/sa-worker-hybrid-state-promotion branch from 09bf8a8 to 9b336f8 Compare July 22, 2026 22:34
@brnguyen2
brnguyen2 requested review from a team as code owners July 22, 2026 22:34

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

SA spec-dec accepted-state promotion is confined to the SA path (SAWorker only exists in SA mode, early-returns when drafting is off, gated on generation rows + hybrid manager); normal decode unaffected, well-tested. LGTM.

@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

1 similar comment
@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61289 [ run ] triggered by Bot. Commit: d7f68c7 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61290 [ run ] triggered by Bot. Commit: d7f68c7 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61289 [ run ] completed with state ABORTED. Commit: d7f68c7

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61290 [ run ] completed with state FAILURE. Commit: d7f68c7
/LLM/main/L0_MergeRequest_PR pipeline #49524 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

@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61373 [ run ] triggered by Bot. Commit: d7f68c7 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61373 [ run ] completed with state FAILURE. Commit: d7f68c7
/LLM/main/L0_MergeRequest_PR pipeline #49601 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

@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61459 [ run ] triggered by Bot. Commit: 8ce63a5 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61459 [ run ] completed with state FAILURE. Commit: 8ce63a5
/LLM/main/L0_MergeRequest_PR pipeline #49680 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

@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61498 [ run ] triggered by Bot. Commit: 8ce63a5 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61498 [ run ] completed with state FAILURE. Commit: 8ce63a5
/LLM/main/L0_MergeRequest_PR pipeline #49718 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

@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61581 [ run ] triggered by Bot. Commit: b665066 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61581 [ run ] completed with state FAILURE. Commit: b665066
/LLM/main/L0_MergeRequest_PR pipeline #49792 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

@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61601 [ run ] triggered by Bot. Commit: b665066 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61601 [ run ] completed with state FAILURE. Commit: b665066
/LLM/main/L0_MergeRequest_PR pipeline #49811 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

…worker

SAWorker never committed accepted verification-step states for
SSM/hybrid models: verification writes per-step states to the cache
manager's SpeculativeState scratch buffers (live pools are read-only
during verify), and nothing promoted the accepted step — silent state
corruption on any hybrid model running standalone SA. Add the
update_mamba_states call right after on-device acceptance, mirroring
the other one-engine workers (dflash/eagle3); isinstance-gated so
pure-attention models are unaffected.

Add a mock-based unit test pinning the contract: promotion fires for
hybrid cache managers on generation batches, and is skipped for
context-only batches and pure-attention managers. Verified to fail
against the pre-fix worker. Listed in the H100 L0 pre-merge set.

Signed-off-by: Brian Nguyen <brnguyen@nvidia.com>
@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

@brnguyen2
brnguyen2 force-pushed the port/sa-worker-hybrid-state-promotion branch from b665066 to b6dbcce Compare July 24, 2026 19:02
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61629 [ run ] triggered by Bot. Commit: b6dbcce Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61629 [ run ] completed with state FAILURE. Commit: b6dbcce
/LLM/main/L0_MergeRequest_PR pipeline #49836 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

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.

6 participants