-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[core
] Fix safetensors serialization for shared tensors
#1101
[core
] Fix safetensors serialization for shared tensors
#1101
Conversation
The documentation is not available anymore as the PR was closed or merged. |
if isinstance(tensor, torch.Tensor): | ||
ptrs[id_tensor_storage(tensor)].append(name) | ||
else: | ||
# In the non-tensor case, fall back to the pointer of the object itself | ||
ptrs[id(tensor)].append(name) |
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.
Not sure if this is needed, I added that in the transformers code to handle bnb state dict case and should be safe to keep it but happy to remove it as well
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.
Probably it's better to follow the same steps as transformers.
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.
Thanks for investigating this. Just a minor comment about adding a comment, apart from this, LGTM.
@@ -199,6 +203,25 @@ def save_pretrained( | |||
os.makedirs(output_dir, exist_ok=True) | |||
|
|||
if safe_serialization: | |||
# Safetensors does not allow tensor aliasing. |
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 block is basically copied from https://github.com/huggingface/transformers/blob/main/src/transformers/modeling_utils.py#L2111-L2134, right? Let's add a comment.
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.
Ok perfect!
if isinstance(tensor, torch.Tensor): | ||
ptrs[id_tensor_storage(tensor)].append(name) | ||
else: | ||
# In the non-tensor case, fall back to the pointer of the object itself | ||
ptrs[id(tensor)].append(name) |
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.
Probably it's better to follow the same steps as transformers.
What does this PR do?
Fixes #1079
Safetensors do not support tensor aliasing, before saving the model state dict, we need to loop over the state dict and check for aliased tensors and
clone()
them to avoid any errorcc @BenjaminBossan @pacman100