-
Notifications
You must be signed in to change notification settings - Fork 590
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
[Serialization] Add is_main_process
argument to save_torch_state_dict()
#2648
Merged
hanouticelina
merged 4 commits into
main
from
add-condition-to-cleaning-previous-saved-state-dict
Nov 5, 2024
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 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 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 |
---|---|---|
|
@@ -264,6 +264,7 @@ def test_save_torch_model(mocker: MockerFixture, tmp_path: Path) -> None: | |
max_shard_size="3GB", | ||
metadata={"foo": "bar"}, | ||
safe_serialization=True, | ||
is_main_process=True, | ||
) | ||
safe_state_dict_mock.assert_called_once_with( | ||
state_dict=model_mock.state_dict.return_value, | ||
|
@@ -273,6 +274,7 @@ def test_save_torch_model(mocker: MockerFixture, tmp_path: Path) -> None: | |
max_shard_size="3GB", | ||
metadata={"foo": "bar"}, | ||
safe_serialization=True, | ||
is_main_process=True, | ||
) | ||
|
||
|
||
|
@@ -472,3 +474,27 @@ def test_save_torch_state_dict_delete_existing_files( | |
assert (tmp_path / "pytorch_model-00001-of-00003.bin").is_file() | ||
assert (tmp_path / "pytorch_model-00002-of-00003.bin").is_file() | ||
assert (tmp_path / "pytorch_model-00003-of-00003.bin").is_file() | ||
|
||
|
||
def test_save_torch_state_dict_not_main_process( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice test! |
||
tmp_path: Path, | ||
torch_state_dict: Dict[str, "torch.Tensor"], | ||
) -> None: | ||
""" | ||
Test that previous files in the directory are not deleted when is_main_process=False. | ||
When is_main_process=True, previous files should be deleted, | ||
this is already tested in `test_save_torch_state_dict_delete_existing_files`. | ||
""" | ||
# Create some .safetensors files before saving a new state dict. | ||
(tmp_path / "model.safetensors").touch() | ||
(tmp_path / "model-00001-of-00002.safetensors").touch() | ||
(tmp_path / "model-00002-of-00002.safetensors").touch() | ||
(tmp_path / "model.safetensors.index.json").touch() | ||
# Save with is_main_process=False | ||
save_torch_state_dict(torch_state_dict, tmp_path, is_main_process=False) | ||
|
||
# Previous files should still exist (not deleted) | ||
assert (tmp_path / "model.safetensors").is_file() | ||
assert (tmp_path / "model-00001-of-00002.safetensors").is_file() | ||
assert (tmp_path / "model-00002-of-00002.safetensors").is_file() | ||
assert (tmp_path / "model.safetensors.index.json").is_file() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍