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

FEAT: Make safe serialization the default one #1088

Merged
merged 5 commits into from
Nov 15, 2023

Conversation

younesbelkada
Copy link
Contributor

What does this PR do?

As per title, to be in line with the current efforts in the OSS ecosystem let's also make in PEFT safe serialization the default behaviour

Adapted the tests and added some regression tests!

cc @BenjaminBossan @pacman100


# check if `adapter_config.json` is present
self.assertTrue(os.path.exists(os.path.join(tmp_dirname, "adapter_config.json")))

# check if `pytorch_model.bin` is not present
self.assertFalse(os.path.exists(os.path.join(tmp_dirname, "pytorch_model.bin")))
# check if `model.safetensors` is not present
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change in necessary as now in transformers we don't save anymore pytorch-model.bin but the safetensors model

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Nov 7, 2023

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

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.

No full review yet, but some general questions:

  1. Do we want to do a deprecation/future warning cycle before making safetensors the default? How was it done in transformers?
  2. Do we want to run _test_save_pretrained twice, once with safetensors, once with pytorch?

@younesbelkada
Copy link
Contributor Author

Thanks @BenjaminBossan !

  1. not sure how they did it in transformers, let me double check that
  1. Do we want to run _test_save_pretrained twice, once with safetensors, once with pytorch?

Yes this is what I did in the PR, I kept test_save_pretrained as is and replaced the expected file names with safetensors file names and added a new test to check if one calls save_pretrained with safe_serialization=False it leads to the same behavior as we had before this PR

@BenjaminBossan
Copy link
Member

I kept test_save_pretrained as is and replaced the expected file names with safetensors file names and added a new test to check if one calls save_pretrained with safe_serialization=False it leads to the same behavior as we had before this PR

Oh I see. I wonder if _test_save_pretrained could not be changed slightly so that it takes an argument use_safetensors or something like that which can be parameterized. Then the same test can be re-used without duplicating it whole.

I think the new test is not quite a regression test, as regression testing would require the model to be persisted with one PEFT version and loaded with another PEFT version. I think we should really try to advance #995, which adds regression tests which would also help cover this case.

@@ -287,7 +287,10 @@ def _test_save_pretrained(self, model_id, config_cls, config_kwargs):
model = model.to(self.torch_device)

with tempfile.TemporaryDirectory() as tmp_dirname:
model.save_pretrained(tmp_dirname)
if safe_serialization:
Copy link
Contributor Author

@younesbelkada younesbelkada Nov 7, 2023

Choose a reason for hiding this comment

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

I prefer to not pass safe_serialization=safe_serialization in save_pretrained to test the native behaviour, let me know if this makes sense

Copy link
Member

Choose a reason for hiding this comment

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

You mean the default behavior? I think it's okay either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes sorry I meant the default behaviour

@younesbelkada
Copy link
Contributor Author

younesbelkada commented Nov 9, 2023

We are post the patch release now and merged this branch with main, this PR is ready for another review!

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Nov 9, 2023

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

Copy link
Contributor

@pacman100 pacman100 left a comment

Choose a reason for hiding this comment

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

Thank you @younesbelkada for making safetensors the default format for saving the adapter weights 🔐! This makes it safe by avoiding the risk of random code execution cases when using pickled format.

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 making the switch and adjusting the tests. I think we can merge now, although I would imagine some users will be surprised that their code suddenly produces safetensors. We should put a big note about this on the release notes once we publish the next version.

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.

4 participants