Skip to content
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

Conversation

younesbelkada
Copy link
Contributor

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 error

cc @BenjaminBossan @pacman100

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Nov 9, 2023

The documentation is not available anymore as the PR was closed or merged.

Comment on lines +212 to +216
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)
Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Member

@BenjaminBossan BenjaminBossan left a 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok perfect!

Comment on lines +212 to +216
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)
Copy link
Member

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.

@younesbelkada younesbelkada merged commit b5641cc into huggingface:main Nov 9, 2023
11 checks passed
@younesbelkada younesbelkada deleted the fix-safe-serialization-shared-tensors branch November 9, 2023 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Token classification LoRA example broken
3 participants