-
Notifications
You must be signed in to change notification settings - Fork 2k
[TRTLLM-7136][feat] Update load_weights method to include mapping parameter in checkpoint loaders #9583
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
Merged
[TRTLLM-7136][feat] Update load_weights method to include mapping parameter in checkpoint loaders #9583
Changes from all commits
Commits
Show all changes
2 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
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
111 changes: 111 additions & 0 deletions
111
tests/unittest/_torch/models/checkpoints/hf/test_checkpoint_loader.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,111 @@ | ||
| import pathlib as _pl | ||
| from typing import Any, Optional | ||
|
|
||
| import pytest | ||
| import torch | ||
| from transformers.configuration_utils import PretrainedConfig | ||
|
|
||
| from tensorrt_llm import LLM, Mapping | ||
| from tensorrt_llm._torch.model_config import ModelConfig | ||
| from tensorrt_llm._torch.models.checkpoints import HfCheckpointLoader | ||
| from tensorrt_llm._torch.models.checkpoints.base_config_loader import BaseConfigLoader | ||
| from tensorrt_llm._torch.models.checkpoints.base_weight_loader import BaseWeightLoader | ||
| from tensorrt_llm._torch.models.checkpoints.base_weight_mapper import BaseWeightMapper | ||
| from tensorrt_llm._torch.models.modeling_utils import register_auto_model | ||
|
|
||
|
|
||
| class DummyConfig(PretrainedConfig): | ||
| def __init__(self): | ||
| self.architectures: list[str] = ["DummyModel"] | ||
| self.dtype: torch.dtype = torch.float16 | ||
| self.num_attention_heads: int = 16 | ||
| self.hidden_size: int = 256 | ||
| self.vocab_size: int = 1000 | ||
| self.num_hidden_layers: int = 1 | ||
|
|
||
|
|
||
| @register_auto_model("DummyModel") | ||
| class DummyModel(torch.nn.Module): | ||
| def __init__(self, model_config: ModelConfig): | ||
| super().__init__() | ||
| self.model_config = model_config | ||
|
|
||
| def infer_max_seq_len(self): | ||
| return 2048 | ||
|
|
||
| @property | ||
| def config(self): | ||
| return self.model_config.pretrained_config | ||
|
|
||
| def forward(self, *args, input_ids: torch.Tensor, **kwargs) -> torch.Tensor: | ||
| num_batch_tokens = input_ids.size(0) | ||
| vocab_size = self.config.vocab_size | ||
|
|
||
| # Logits: dummy values for testing | ||
| logits = torch.ones((num_batch_tokens, vocab_size), device="cuda") * 0.1 | ||
|
|
||
| return { | ||
| "logits": logits, | ||
| } | ||
|
|
||
| def load_weights( | ||
| self, | ||
| weights: dict, | ||
| weight_mapper: Optional[BaseWeightMapper] = None, | ||
| skip_modules: list[str] = [], | ||
| ): | ||
| pass | ||
|
|
||
|
|
||
| class DummyWeightLoader(BaseWeightLoader): | ||
| def load_weights(self, checkpoint_dir: str, mapping: Mapping, **kwargs) -> dict[str, Any]: | ||
| """Load weights from your dummy format. | ||
| Args: | ||
| checkpoint_dir: Directory containing checkpoint files | ||
| mapping: Mapping object containing the distributed configuration | ||
| **kwargs: Additional loading parameters | ||
| Returns: | ||
| Dictionary mapping parameter names to tensors | ||
| """ | ||
|
|
||
| assert mapping is not None | ||
| assert isinstance(mapping, Mapping) | ||
| assert mapping.world_size == 1 | ||
| assert mapping.rank == 0 | ||
|
|
||
| weights = {} | ||
|
|
||
| return weights | ||
|
|
||
|
|
||
| class DummyConfigLoader(BaseConfigLoader): | ||
| def load(self, checkpoint_dir: str, **kwargs) -> ModelConfig: | ||
| """Load and parse configuration from your dummy format. | ||
| Args: | ||
| checkpoint_dir: Directory containing configuration files | ||
| **kwargs: Additional loading parameters | ||
| Returns: | ||
| ModelConfig object containing parsed configuration | ||
| """ | ||
| return ModelConfig(pretrained_config=DummyConfig()) | ||
|
|
||
|
|
||
| def test_weight_loader_mapping(): | ||
| """Test that the mapping in weight loader is correct.""" | ||
|
|
||
| # Create LLM with the provided model | ||
| with LLM( | ||
| model=_pl.Path("dummy_path"), | ||
| backend="pytorch", | ||
| cuda_graph_config=None, | ||
| checkpoint_loader=HfCheckpointLoader( | ||
| weight_loader=DummyWeightLoader(), config_loader=DummyConfigLoader() | ||
| ), | ||
| ): | ||
| pass | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
Funatiq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pytest.main([__file__]) | ||
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
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.