-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[None][fix] SA spec dec: promote accepted hybrid recurrent states in-worker #16759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brnguyen2
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
brnguyen2:port/sa-worker-hybrid-state-promotion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
tests/unittest/_torch/speculative/test_sa_hybrid_state_promotion.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Regression tests for SAWorker promoting accepted recurrent states on | ||
| hybrid (SSM) models. | ||
|
|
||
| Verification writes per-step recurrent states to the cache manager's | ||
| speculative scratch buffers, never the live pools; the worker must promote | ||
| the accepted step via ``update_mamba_states`` after acceptance (mirroring | ||
| the dflash/eagle3 one-engine workers), or hybrid models silently corrupt | ||
| their recurrent state under standalone SA speculative decoding. | ||
| """ | ||
|
|
||
| from types import SimpleNamespace | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from tensorrt_llm._torch.pyexecutor.mamba_cache_manager import MixedMambaHybridCacheManager | ||
| from tensorrt_llm._torch.speculative.sa_worker import SAWorker | ||
|
|
||
|
|
||
| def _make_worker() -> SAWorker: | ||
| spec_config = SimpleNamespace(max_draft_len=4, max_matching_ngram_size=2) | ||
| worker = SAWorker(spec_config) | ||
| # Stub out everything around the state-promotion call site; the mocked | ||
| # sampler return values flow into update_mamba_states unchanged. | ||
| worker._execute_guided_decoder_if_present = mock.MagicMock() | ||
| worker._sample_and_accept_draft_tokens = mock.MagicMock( | ||
| return_value=(mock.sentinel.accepted_tokens, mock.sentinel.num_accepted_tokens) | ||
| ) | ||
| worker._generate_draft_tokens = mock.MagicMock(return_value=mock.sentinel.next_draft_tokens) | ||
| worker._prepare_next_new_tokens = mock.MagicMock(return_value=mock.sentinel.next_new_tokens) | ||
| return worker | ||
|
|
||
|
|
||
| def _make_metadata(kv_cache_manager, num_seqs: int, num_contexts: int): | ||
| attn_metadata = SimpleNamespace( | ||
| num_seqs=num_seqs, | ||
| num_contexts=num_contexts, | ||
| kv_cache_manager=kv_cache_manager, | ||
| mamba_metadata=SimpleNamespace(state_indices=mock.sentinel.state_indices), | ||
| ) | ||
| spec_metadata = SimpleNamespace( | ||
| runtime_draft_len=4, | ||
| batch_indices_cuda=mock.sentinel.batch_indices_cuda, | ||
| ) | ||
| return attn_metadata, spec_metadata | ||
|
|
||
|
|
||
| def _run_forward(worker, attn_metadata, spec_metadata): | ||
| return worker._forward_impl( | ||
| input_ids=mock.sentinel.input_ids, | ||
| position_ids=mock.sentinel.position_ids, | ||
| hidden_states=mock.sentinel.hidden_states, | ||
| logits=mock.sentinel.logits, | ||
| attn_metadata=attn_metadata, | ||
| spec_metadata=spec_metadata, | ||
| ) | ||
|
|
||
|
|
||
| def test_hybrid_manager_promotes_accepted_states(): | ||
| worker = _make_worker() | ||
| manager = mock.MagicMock(spec=MixedMambaHybridCacheManager) | ||
| attn_metadata, spec_metadata = _make_metadata(manager, num_seqs=2, num_contexts=0) | ||
|
|
||
| result = _run_forward(worker, attn_metadata, spec_metadata) | ||
|
|
||
| manager.update_mamba_states.assert_called_once_with( | ||
| attn_metadata=attn_metadata, | ||
| num_accepted_tokens=mock.sentinel.num_accepted_tokens, | ||
| state_indices=mock.sentinel.state_indices, | ||
| ) | ||
| assert result["new_tokens"] is mock.sentinel.accepted_tokens | ||
|
|
||
|
|
||
| def test_hybrid_manager_context_only_batch_skips_promotion(): | ||
| worker = _make_worker() | ||
| manager = mock.MagicMock(spec=MixedMambaHybridCacheManager) | ||
| attn_metadata, spec_metadata = _make_metadata(manager, num_seqs=2, num_contexts=2) | ||
|
|
||
| _run_forward(worker, attn_metadata, spec_metadata) | ||
|
|
||
| manager.update_mamba_states.assert_not_called() | ||
|
|
||
|
|
||
| def test_pure_attention_manager_skips_promotion(): | ||
| worker = _make_worker() | ||
| manager = mock.MagicMock() # not a MambaHybridCacheManager | ||
| attn_metadata, spec_metadata = _make_metadata(manager, num_seqs=2, num_contexts=0) | ||
|
|
||
| _run_forward(worker, attn_metadata, spec_metadata) | ||
|
|
||
| manager.update_mamba_states.assert_not_called() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| pytest.main([__file__, "-v"]) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.