-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[None][feat] Save state first pass #7012
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
Merged
mikeiovine
merged 3 commits into
NVIDIA:main
from
IzzyPutterman:iputterman/savestate-config
Oct 1, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import os | ||
from typing import Optional | ||
|
||
import torch | ||
|
||
from tensorrt_llm._utils import local_mpi_rank | ||
|
||
from ..pyexecutor.llm_request import LlmRequest | ||
from ..pyexecutor.resource_manager import ResourceManager | ||
from ..pyexecutor.scheduler import ScheduledRequests | ||
from .drafter import Drafter | ||
|
||
|
||
class SaveHiddenStatesDrafter(Drafter): | ||
|
||
def __init__( | ||
self, | ||
spec_config: "SaveHiddenStatesDecodingConfig", | ||
spec_resource_manager, | ||
): | ||
super().__init__(spec_config.max_concurrency) | ||
self.spec_config = spec_config | ||
self.max_draft_len = spec_config.max_draft_len | ||
self._iter = 1 | ||
self._output_directory = spec_config.output_directory | ||
self._file_prefix = spec_config.file_prefix | ||
self._write_interval = spec_config.write_interval | ||
self._saved_state = [] | ||
self.spec_resource_manager = spec_resource_manager | ||
os.makedirs(self._output_directory, exist_ok=True) | ||
|
||
def _process_request(self, request: LlmRequest, resource_manager) -> None: | ||
out_dict = {} | ||
if local_mpi_rank() == 0: | ||
input_ids = torch.tensor(list(request.get_tokens(0)), | ||
dtype=torch.long, | ||
device='cpu') | ||
hidden_size = resource_manager.hidden_size | ||
num_tokens = input_ids.shape[0] | ||
hidden_states = resource_manager.hidden_states[:num_tokens, | ||
-hidden_size:].cpu( | ||
).clone() | ||
|
||
out_dict = { | ||
"id": self._iter, | ||
"input_ids": input_ids, | ||
"hidden_state": hidden_states, | ||
} | ||
if len(self.spec_config.eagle3_layers_to_capture) > 1: | ||
if self.spec_config._last_hidden_in_save: | ||
out_dict[ | ||
"aux_hidden_states"] = resource_manager.hidden_states[:num_tokens, :].cpu( | ||
).clone() | ||
else: | ||
out_dict[ | ||
"aux_hidden_states"] = resource_manager.hidden_states[: | ||
num_tokens, : | ||
-hidden_size].cpu( | ||
).clone( | ||
) | ||
|
||
self._saved_state.append(out_dict) | ||
|
||
def _write_to_file(self) -> None: | ||
if local_mpi_rank() == 0: | ||
output_path = os.path.join(self._output_directory, | ||
f"{self._file_prefix}_{self._iter}.pt") | ||
torch.save(self._saved_state, output_path) | ||
self._saved_state = [] | ||
|
||
def prepare_draft_tokens( | ||
self, | ||
scheduled_requests: ScheduledRequests, | ||
resource_manager: Optional[ResourceManager] = None, | ||
) -> None: | ||
for request in sorted( | ||
scheduled_requests.context_requests, | ||
mikeiovine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
key=lambda r: | ||
(r.py_batch_idx is None, r.py_batch_idx or r.request_id), | ||
): | ||
request.py_max_new_tokens = 1 | ||
|
||
def run_drafter_post( | ||
self, | ||
scheduled_requests: ScheduledRequests, | ||
resource_manager: Optional[ResourceManager] = None, | ||
is_warmup: bool = False, | ||
) -> None: | ||
if is_warmup: | ||
return | ||
for request in sorted( | ||
scheduled_requests.context_requests, | ||
key=lambda r: | ||
(r.py_batch_idx is None, r.py_batch_idx or r.request_id), | ||
): | ||
self._process_request(request, self.spec_resource_manager) | ||
if self._iter % self._write_interval == 0: | ||
self._write_to_file() | ||
self._iter += 1 |
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
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
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.