-
Notifications
You must be signed in to change notification settings - Fork 31.2k
Clean load keys #24505
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
Clean load keys #24505
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1d1040f
Preliminary work on some models
sgugger bcddced
Fix test load missing and make sure nonpersistent buffers are tested
sgugger 5e8754f
Always ignore nonpersistent buffers if in state_dict
sgugger 15efbc0
Treat models
sgugger b8ca0cb
More models
sgugger 99fd175
Treat remaining models
sgugger a787b65
Fix quality
sgugger 7fbf9c8
Fix tests
sgugger 5d4ffa2
Remove draft
sgugger 7aa7e80
This test is not needed anymore
sgugger c30366f
Fix copies
sgugger dc52803
Fix last test
sgugger 9f4511e
Newly added models
sgugger 002c1b8
Fix last tests
sgugger 0ccd672
Address review comments
sgugger 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -320,8 +320,9 @@ def shard_checkpoint( | |
|
|
||
| weight_size = weight.numel() * dtype_byte_size(weight.dtype) | ||
|
|
||
| # If this weight is going to tip up over the maximal size, we split. | ||
| if last_block_size + weight_size > max_shard_size: | ||
| # If this weight is going to tip up over the maximal size, we split, but only if we have put at least one | ||
| # weight in the current shard. | ||
| if last_block_size + weight_size > max_shard_size and len(sharded_state_dicts[-1]) > 0: | ||
| sharded_state_dicts.append({}) | ||
| last_block_size = 0 | ||
|
|
||
|
|
@@ -3044,15 +3045,30 @@ def _fix_key(key): | |
| expected_keys = [".".join([prefix, s]) for s in expected_keys] | ||
|
|
||
| missing_keys = list(set(expected_keys) - set(loaded_keys)) | ||
| unexpected_keys = list(set(loaded_keys) - set(expected_keys)) | ||
| unexpected_keys = set(loaded_keys) - set(expected_keys) | ||
| # Remove nonpersistent buffers from unexpected keys: they are not in the state dict but will be in the model | ||
| # buffers | ||
| model_buffers = {n for n, _ in model.named_buffers()} | ||
| if remove_prefix_from_model: | ||
| model_buffers = {key[len(_prefix) :] if key.startswith(_prefix) else key for key in model_buffers} | ||
| elif add_prefix_to_model: | ||
| model_buffers = {".".join([prefix, key]) for key in model_buffers} | ||
| unexpected_keys = list(unexpected_keys - model_buffers) | ||
|
|
||
| if is_accelerate_available(): | ||
| model.tie_weights() | ||
| tied_params = find_tied_parameters(model) | ||
| else: | ||
| tied_params = [] | ||
| model.tie_weights() | ||
| ptrs = collections.defaultdict(list) | ||
| for name, tensor in model.state_dict().items(): | ||
| id_tensor = id_tensor_storage(tensor) if tensor.device != torch.device("meta") else id(tensor) | ||
|
Collaborator
Author
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. Accelerate detects tied weights on IDs but it doesn't work for all models (deformable_detr for instance). So we use the same test as elsewhere except when the tensor is on the meta device (in which case it fails) where we default to id. |
||
| ptrs[id_tensor].append(name) | ||
|
|
||
| # These are all the pointers of shared tensors. | ||
| tied_params = [names for _, names in ptrs.items() if len(names) > 1] | ||
|
|
||
| for group in tied_params: | ||
| if remove_prefix_from_model: | ||
| group = [key[len(_prefix) :] if key.startswith(_prefix) else key for key in group] | ||
| elif add_prefix_to_model: | ||
| group = [".".join([prefix, key]) for key in group] | ||
| missing_in_group = [k for k in missing_keys if k in group] | ||
| if len(missing_in_group) > 0 and len(missing_in_group) < len(group): | ||
| missing_keys = [k for k in missing_keys if k not in missing_in_group] | ||
|
|
||
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
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.
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.
This change is necessary to make sure we save something in the first shard. With the removal of
position_idsfrom the tensors saved, some tests of checkpoint sharding with BERT started failing.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.
I think it's worth adding a comment mentioning why we're doing this!