-
Notifications
You must be signed in to change notification settings - Fork 312
[Feat]FakeBaseModel for offline eagle; Kimi-K2.5 fixes; #1052
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
h-guo18
wants to merge
9
commits into
main
Choose a base branch
from
haoguo/fakebasemodel
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
551a46c
Add FakeBaseModel for offline speculative decoding and Kimi-K2.5 fixes
h-guo18 dcbfcf4
refactor
h-guo18 d28279f
refactor
h-guo18 45a5aa9
polish
h-guo18 9221267
remove kimi patches
h-guo18 0df75e2
add tests
h-guo18 d9b25e1
refactor
h-guo18 a023e6e
ddp for offline
h-guo18 99946d5
new unit tests
h-guo18 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
194 changes: 194 additions & 0 deletions
194
modelopt/torch/speculative/plugins/modeling_fakebase.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,194 @@ | ||
| # 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. | ||
|
|
||
| """Lightweight fake base model for offline speculative decoding training.""" | ||
|
|
||
| import json | ||
| import os | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
| import transformers | ||
| from huggingface_hub import hf_hub_download | ||
| from huggingface_hub.errors import EntryNotFoundError | ||
| from safetensors.torch import load_file as safetensors_load_file | ||
| from transformers import PretrainedConfig, PreTrainedModel | ||
|
|
||
| # Candidate module paths searched in order — shared with HFEagleModel._find_base_model_parts | ||
| _EMBED_TOKENS_PATHS = [ | ||
| "embed_tokens", | ||
| "language_model.model.embed_tokens", | ||
| "model.embed_tokens", | ||
| "backbone.embeddings", | ||
| "language_model.backbone.embeddings", | ||
| "model.language_model.embed_tokens", | ||
| ] | ||
| _LM_HEAD_PATHS = ["lm_head", "language_model.lm_head"] | ||
| _BASE_MODEL_PATHS = [ | ||
| "language_model.model", | ||
| "model.language_model", | ||
| "model", | ||
| "backbone", | ||
| "language_model.backbone", | ||
| ] | ||
| _VLM_CONFIG_ATTRS = ["text_config", "llm_config"] | ||
| _SAFETENSORS_INDEX_FILENAME = "model.safetensors.index.json" | ||
|
|
||
|
|
||
| class FakeBaseConfig(PretrainedConfig): | ||
| """Minimal config for FakeBaseModel that supports offline speculative decoding training.""" | ||
|
|
||
| model_type = "fake_base_model" | ||
|
|
||
| def __init__( | ||
| self, | ||
| num_hidden_layers=None, | ||
| hidden_size=None, | ||
| vocab_size=None, | ||
| max_position_embeddings=None, | ||
| dtype=torch.bfloat16, | ||
| tie_word_embeddings=False, | ||
| **kwargs, | ||
| ): | ||
| """Initialize FakeBaseConfig with minimal model configuration parameters.""" | ||
| super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs) | ||
| self.num_hidden_layers = num_hidden_layers | ||
| self.hidden_size = hidden_size | ||
| self.vocab_size = vocab_size | ||
| self.max_position_embeddings = max_position_embeddings | ||
| self.dtype = dtype | ||
|
|
||
|
|
||
| class FakeBaseModel(PreTrainedModel): | ||
| """Minimal base model for offline speculative decoding. | ||
|
|
||
| Contains only ``lm_head``, ``embed_tokens``, and the minimal config needed by the EAGLE | ||
| training loop. The full model weights are never loaded, keeping memory usage low. | ||
|
|
||
| Weights are loaded from a local HuggingFace checkpoint directory. Weight key names and | ||
| VLM config nesting are auto-detected from the shared path constants. | ||
| """ | ||
|
|
||
| config_class = FakeBaseConfig | ||
|
|
||
| def __init__(self, source: str, trust_remote_code: bool = False): | ||
| """Load lm_head and embed_tokens from a local directory or HuggingFace Hub repo. | ||
|
|
||
| Args: | ||
| source: Path to a local HuggingFace checkpoint directory, or a HuggingFace Hub | ||
| repo ID (e.g. ``"meta-llama/Llama-3.1-8B"``). The source type is detected | ||
| automatically: if ``source`` is an existing local directory it is treated as a | ||
| local checkpoint; otherwise it is treated as a Hub repo ID and the required | ||
| files are downloaded via ``huggingface_hub``. | ||
| """ | ||
| orig_config = transformers.AutoConfig.from_pretrained( | ||
| source, trust_remote_code=trust_remote_code | ||
| ) | ||
| # For vlms, detect language model config based on _VLM_CONFIG_ATTRS | ||
| base_cfg = next( | ||
| ( | ||
| getattr(orig_config, attr) | ||
| for attr in _VLM_CONFIG_ATTRS | ||
| if getattr(orig_config, attr, None) is not None | ||
| ), | ||
| orig_config, | ||
| ) | ||
| # Extract necessary info for spec training from base config | ||
| config = FakeBaseConfig( | ||
| num_hidden_layers=getattr(base_cfg, "num_hidden_layers", None), | ||
| hidden_size=getattr(base_cfg, "hidden_size", None), | ||
| vocab_size=getattr(base_cfg, "vocab_size", None), | ||
| max_position_embeddings=getattr(base_cfg, "max_position_embeddings", None), | ||
| dtype=getattr(base_cfg, "dtype", torch.bfloat16), | ||
| tie_word_embeddings=getattr(base_cfg, "tie_word_embeddings", False), | ||
h-guo18 marked this conversation as resolved.
Show resolved
Hide resolved
h-guo18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| super().__init__(config) | ||
| # Initialize dummy module and attributes for compatibility with HFEagleModel | ||
| self.model = nn.Module() | ||
| self.model.layers = nn.ModuleList() | ||
| self.model.dtype = config.dtype | ||
| self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size) | ||
| self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False) | ||
|
|
||
| # Load lm_head and embed_tokens only from checkpoint | ||
| lm_head_w, embed_tokens_w = self._load_weights(source) | ||
| assert lm_head_w.shape == (config.vocab_size, config.hidden_size) | ||
| assert embed_tokens_w.shape == (config.vocab_size, config.hidden_size) | ||
| self.lm_head.weight.data.copy_(lm_head_w) | ||
| self.embed_tokens.weight.data.copy_(embed_tokens_w) | ||
|
|
||
h-guo18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @staticmethod | ||
| def _find_weight_key(weight_map: dict, paths: list[str], label: str) -> str: | ||
| """Return the first ``path + '.weight'`` found in ``weight_map``.""" | ||
| for path in paths: | ||
| key = path + ".weight" | ||
| if key in weight_map: | ||
| return key | ||
| tried = [p + ".weight" for p in paths] | ||
| raise RuntimeError(f"Cannot find {label} in checkpoint; tried: {tried}") | ||
|
|
||
h-guo18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @staticmethod | ||
| def _load_index(source: str) -> dict: | ||
| """Load weight_map from model.safetensors.index.json (local directory or Hub repo).""" | ||
| if os.path.isdir(source): | ||
| index_path = os.path.join(source, _SAFETENSORS_INDEX_FILENAME) | ||
| if not os.path.isfile(index_path): | ||
| raise FileNotFoundError( | ||
| f"No {_SAFETENSORS_INDEX_FILENAME} found in {source!r}. " | ||
| "FakeBaseModel only supports safetensors checkpoints. " | ||
| "Checkpoints using pytorch_model.bin or single-file formats are not supported." | ||
| ) | ||
| else: | ||
| try: | ||
| index_path = hf_hub_download(repo_id=source, filename=_SAFETENSORS_INDEX_FILENAME) | ||
| except EntryNotFoundError: | ||
| raise ValueError( | ||
| f"Repository {source!r} does not contain {_SAFETENSORS_INDEX_FILENAME}. " | ||
| "FakeBaseModel only supports safetensors checkpoints. " | ||
| "Checkpoints using pytorch_model.bin or single-file formats are not supported." | ||
| ) from None | ||
| with open(index_path) as f: | ||
| return json.load(f).get("weight_map", {}) | ||
|
|
||
| @staticmethod | ||
| def _resolve_shard_paths(source: str, shard_filenames: list[str]) -> list[str]: | ||
| """Return local filesystem paths for each shard filename. | ||
|
|
||
| For a local directory the paths are joined directly; for a HuggingFace Hub repo ID the | ||
| shards are downloaded via ``hf_hub_download`` (cached on subsequent calls). | ||
| """ | ||
| if os.path.isdir(source): | ||
| return [os.path.join(source, name) for name in shard_filenames] | ||
| return [hf_hub_download(repo_id=source, filename=name) for name in shard_filenames] | ||
|
|
||
| def _load_weights(self, source: str): | ||
| """Load lm_head and embed_tokens weights from a local directory or HuggingFace Hub repo.""" | ||
| weight_map = self._load_index(source) | ||
|
|
||
| lm_head_key = self._find_weight_key(weight_map, _LM_HEAD_PATHS, "lm_head") | ||
| embed_tokens_key = self._find_weight_key(weight_map, _EMBED_TOKENS_PATHS, "embed_tokens") | ||
|
|
||
| lm_head_path, embed_tokens_path = self._resolve_shard_paths( | ||
| source, [weight_map[lm_head_key], weight_map[embed_tokens_key]] | ||
| ) | ||
|
|
||
| lm_head_state = safetensors_load_file(lm_head_path, device="cpu") | ||
| embed_tokens_state = safetensors_load_file(embed_tokens_path, device="cpu") | ||
|
|
||
| return lm_head_state[lm_head_key], embed_tokens_state[embed_tokens_key] | ||
|
|
||
| def forward(self, *args, **kwargs): | ||
| """Not implemented: FakeBaseModel omits full model weights and cannot run inference.""" | ||
| raise NotImplementedError("FakeBaseModel forward is not implemented.") | ||
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.