-
Notifications
You must be signed in to change notification settings - Fork 2k
[None][feat] Skip prefetching consolidated safetensors when appropriate #7225
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
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
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
80 changes: 80 additions & 0 deletions
80
tests/unittest/_torch/models/checkpoints/hf/test_weight_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,80 @@ | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from tensorrt_llm._torch.models.checkpoints import HfWeightLoader | ||
2ez4bz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class MyError(Exception): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "dir_name, safetensor_filenames, expected_safetensor_filenames", | ||
| [ | ||
| ( | ||
| "foo", | ||
| [ | ||
| "model-00001-of-00002.safetensors", | ||
| "model-000002-of-00002.safetensors", | ||
| "consolidated.safetensors", | ||
| ], | ||
| ["model-00001-of-00002.safetensors", "model-000002-of-00002.safetensors"], | ||
| ), | ||
| ( | ||
| "foo", | ||
| [ | ||
| *(f"model-0000{i}-of-00010.safetensors" for i in range(1, 11)), | ||
| "foo-consolidated.safetensors", | ||
| ], | ||
| [f"model-0000{i}-of-00010.safetensors" for i in range(1, 11)], | ||
| ), | ||
| # If there is only a consolidated safetensor, that one should still be used. | ||
| ( | ||
| "foo", | ||
| ["consolidated.safetensors"], | ||
| ["consolidated.safetensors"], | ||
| ), | ||
| # If the directory contains "consolidated" in its name, but its contents are sharded tensors. | ||
| ( | ||
| "consolidated-model", | ||
| [ | ||
| "model-00001-of-00002.safetensors", | ||
| "model-000002-of-00002.safetensors", | ||
| "consolidated.safetensors", | ||
| ], | ||
| ["model-00001-of-00002.safetensors", "model-000002-of-00002.safetensors"], | ||
| ), | ||
| ], | ||
| ) | ||
| def test_load_weights_ignores_consolidated_ckpt_when_sharded_ckpt_exists( | ||
| tmp_path, | ||
| dir_name: str, | ||
| safetensor_filenames: list[str], | ||
| expected_safetensor_filenames: list[str], | ||
| ): | ||
| checkpoint_dir = tmp_path / dir_name | ||
| checkpoint_dir.mkdir() | ||
| for filename in safetensor_filenames: | ||
| (checkpoint_dir / filename).touch() | ||
| expected_safetensor_filenames = set( | ||
| str(checkpoint_dir / filename) for filename in expected_safetensor_filenames | ||
| ) | ||
|
|
||
| loader = HfWeightLoader() | ||
| with ( | ||
| mock.patch.object( | ||
| loader, "_load_weights_in_parallel", side_effect=MyError | ||
| ) as load_weights_in_parallel, | ||
| mock.patch.object(loader, "prefetch_files") as prefetch_files, | ||
| pytest.raises(MyError), | ||
| ): | ||
| loader.load_weights(checkpoint_dir=str(checkpoint_dir)) | ||
|
|
||
| prefetch_files.assert_called_once() | ||
| prefetched_files = prefetch_files.call_args[0][0] | ||
| assert set(prefetched_files) == expected_safetensor_filenames | ||
|
|
||
| load_weights_in_parallel.assert_called_once() | ||
| loaded_weight_files = load_weights_in_parallel.call_args[0][0] | ||
| assert set(loaded_weight_files) == expected_safetensor_filenames | ||
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.