Repository navigation
[2/8] Add the external draft speculative-decoding mode #2636
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
phicksy
merged 4 commits into
NVIDIA:ext-draft/1-dump-path
from
phicksy:ext-draft/2-core-mode
Oct 14, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
35247e3
Update examples/speculative_decoding/collect_hidden_states/compute_hi…
phicksy b2faa52
Update examples/speculative_decoding/collect_hidden_states/compute_hi…
phicksy c00021c
Address review suggestions on the aux-hidden-states dump path
phicksy 657ef87
[2/8] Add the external draft speculative-decoding mode
phicksy 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
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,20 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| """External Draft Optimization Method.""" | ||
|
|
||
| from .conversion import * | ||
| from .external_model import * |
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,72 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """External draft conversion/restore utilities.""" | ||
|
|
||
| from torch import nn | ||
|
|
||
| from modelopt.torch.opt.conversion import ModelLikeModule | ||
| from modelopt.torch.opt.dynamic import _DMRegistryCls | ||
| from modelopt.torch.opt.mode import ConvertReturnType, MetadataDict | ||
|
|
||
| from ..config import ExternalDraftConfig | ||
|
|
||
| ExternalDraftDMRegistry = _DMRegistryCls(prefix="ExternalDraft") # global instance for the registry | ||
|
|
||
| __all__ = [ | ||
| "ExternalDraftDMRegistry", | ||
| "convert_to_external_draft_model", | ||
| "restore_external_draft_model", | ||
| ] | ||
|
|
||
|
|
||
| def convert_to_external_draft_model( | ||
| model: nn.Module, config: ExternalDraftConfig | ||
| ) -> ConvertReturnType: | ||
| """Convert a pretrained causal LM into a trainable external draft as per `config`. | ||
|
|
||
| Note that, unlike the other speculative modes, ``model`` here is the *draft* | ||
| rather than the base model. Nothing is grafted onto a target; the draft keeps | ||
| its own embeddings and lm_head. | ||
| """ | ||
| # initialize the true module if necessary | ||
| model = model.init_modellike() if isinstance(model, ModelLikeModule) else model | ||
|
|
||
| original_cls = type(model) | ||
| if original_cls not in ExternalDraftDMRegistry: | ||
| for cls in ExternalDraftDMRegistry._registry: | ||
| if issubclass(original_cls, cls): | ||
| ExternalDraftDMRegistry.register({original_cls: "draft_model_class"})( | ||
| ExternalDraftDMRegistry[cls] | ||
| ) | ||
| break | ||
|
|
||
| external_draft_model = ExternalDraftDMRegistry.convert(model) | ||
| external_draft_model.modify(config) | ||
|
|
||
| # no metadata, all specified via config. | ||
| metadata = {} | ||
|
|
||
| return external_draft_model, metadata | ||
|
|
||
|
|
||
| def restore_external_draft_model( | ||
| model: nn.Module, config: ExternalDraftConfig, metadata: MetadataDict | ||
| ) -> nn.Module: | ||
| """Function for restoring a previously converted model to an external draft model.""" | ||
| # the metadata should be empty | ||
| assert not metadata, "No metadata expected!" | ||
|
|
||
| return convert_to_external_draft_model(model, config)[0] |
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,45 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """External draft model to support speculative decoding with a standalone draft.""" | ||
|
|
||
| from modelopt.torch.opt.dynamic import DynamicModule | ||
|
|
||
| __all__ = ["ExternalDraftModel"] | ||
|
|
||
|
|
||
| class ExternalDraftModel(DynamicModule): | ||
| """Base external draft model. | ||
|
|
||
| Unlike Eagle/Medusa/DFlash, the converted module *is* the draft: a pretrained | ||
| causal LM that owns its embeddings and lm_head. The base (target) model is | ||
| never grafted onto it; it contributes only cached hidden states, which are | ||
| projected by the target lm_head to obtain teacher logits. | ||
| """ | ||
|
|
||
| def _setup(self): | ||
| self._register_temp_attribute("external_offline", True) | ||
| self._register_temp_attribute("external_loss", "tvd") | ||
| self._register_temp_attribute("external_report_acc", True) | ||
| self._register_temp_attribute("external_top_k", 20) | ||
| self._register_temp_attribute("external_top_p", 0.95) | ||
|
|
||
| def modify(self, config): | ||
| """Base external draft modify function. Child class should implement the details.""" | ||
| self.external_offline = config.external_offline | ||
| self.external_loss = config.external_loss | ||
| self.external_report_acc = config.external_report_acc | ||
| self.external_top_k = config.external_top_k | ||
| self.external_top_p = config.external_top_p |
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.
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.