From acc4bbddc183ae7ff7fe6049e80225e031c14a6e Mon Sep 17 00:00:00 2001 From: Steven Liu <59462357+stevhliu@users.noreply.github.com> Date: Thu, 24 Aug 2023 17:08:59 -0700 Subject: [PATCH 1/3] Create convert-weights.md --- docs/source/convert-weights.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 docs/source/convert-weights.md diff --git a/docs/source/convert-weights.md b/docs/source/convert-weights.md new file mode 100644 index 00000000..a3594c6e --- /dev/null +++ b/docs/source/convert-weights.md @@ -0,0 +1,23 @@ +# Convert weights to safetensors + +PyTorch model weights are commonly saved and stored as `.bin` files with Python's [`pickle`](https://docs.python.org/3/library/pickle.html) utility. To save and store your model weights in the more secure `safetensor` format, we recommend converting your weights to `.safetensors`. + +The easiest way to convert your model weights is to use the [Convert Space](https://huggingface.co/spaces/diffusers/convert), given your model weights are already stored on the Hub. The Convert Space downloads the pickled weights, converts them, and opens a Pull Request to upload the newly converted `.safetensors` file to your repository. Merge the Pull Request to upload the weights, but if you can't wait to try it out, you can also use the `.safetensors` immediately by specifying the reference to the Pull Request in the revision parameter: + +```py +from transformers import AutoModel + +model = AutoModel.from_pretrained( + "my-safe-model", revision="refs/pr/1", use_safetensors=True +) +``` + +Another way to convert your `.bin` files is to use the [`~safetensors.torch.save_model`] function: + +```py +from transformers import AutoModel +from safetensors.torch import save_model + +unsafe_model = AutoModel.from_pretrained("my-unsafe-model") +save_model(unsafe_model, "model.safetensors") +``` From d7fc3040be03b487c88697c61a3b01a829b27887 Mon Sep 17 00:00:00 2001 From: Steven Liu <59462357+stevhliu@users.noreply.github.com> Date: Thu, 24 Aug 2023 17:12:52 -0700 Subject: [PATCH 2/3] Update _toctree.yml --- docs/source/_toctree.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/_toctree.yml b/docs/source/_toctree.yml index c41db63b..ce40108c 100644 --- a/docs/source/_toctree.yml +++ b/docs/source/_toctree.yml @@ -7,6 +7,8 @@ title: Tensor Sharing in Pytorch - local: metadata_parsing title: Metadata Parsing + - local: convert-weights + title: Convert weights to safetensors title: Getting started - sections: - local: api/torch From 9f15cfb10cc8c3317f27b1e102e97d0f98a79bce Mon Sep 17 00:00:00 2001 From: Steven Liu <59462357+stevhliu@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:49:44 -0700 Subject: [PATCH 3/3] convert space option only --- docs/source/convert-weights.md | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/docs/source/convert-weights.md b/docs/source/convert-weights.md index a3594c6e..65c42f0d 100644 --- a/docs/source/convert-weights.md +++ b/docs/source/convert-weights.md @@ -2,22 +2,12 @@ PyTorch model weights are commonly saved and stored as `.bin` files with Python's [`pickle`](https://docs.python.org/3/library/pickle.html) utility. To save and store your model weights in the more secure `safetensor` format, we recommend converting your weights to `.safetensors`. -The easiest way to convert your model weights is to use the [Convert Space](https://huggingface.co/spaces/diffusers/convert), given your model weights are already stored on the Hub. The Convert Space downloads the pickled weights, converts them, and opens a Pull Request to upload the newly converted `.safetensors` file to your repository. Merge the Pull Request to upload the weights, but if you can't wait to try it out, you can also use the `.safetensors` immediately by specifying the reference to the Pull Request in the revision parameter: +The easiest way to convert your model weights is to use the [Convert Space](https://huggingface.co/spaces/diffusers/convert), given your model weights are already stored on the Hub. The Convert Space downloads the pickled weights, converts them, and opens a Pull Request to upload the newly converted `.safetensors` file to your repository. -```py -from transformers import AutoModel + -model = AutoModel.from_pretrained( - "my-safe-model", revision="refs/pr/1", use_safetensors=True -) -``` +For larger models, the Space may be a bit slower because its resources are tied up in converting other models. You can also try running the [convert.py](https://github.com/huggingface/safetensors/blob/main/bindings/python/convert.py) script (this is what the Space is running) locally to convert your weights. -Another way to convert your `.bin` files is to use the [`~safetensors.torch.save_model`] function: +Feel free to ping [@Narsil](https://huggingface.co/Narsil) for any issues with the Space. -```py -from transformers import AutoModel -from safetensors.torch import save_model - -unsafe_model = AutoModel.from_pretrained("my-unsafe-model") -save_model(unsafe_model, "model.safetensors") -``` +