You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/en/package_reference/serialization.md
+19-3
Original file line number
Diff line number
Diff line change
@@ -4,19 +4,22 @@ rendered properly in your Markdown viewer.
4
4
5
5
# Serialization
6
6
7
-
`huggingface_hub`contains helpers to help ML libraries serialize models weights in a standardized way. This part of the lib is still under development and will be improved in future releases. The goal is to harmonize how weights are serialized on the Hub, both to remove code duplication across libraries and to foster conventions on the Hub.
7
+
`huggingface_hub`provides helpers to save and load ML model weights in a standardized way. This part of the library is still under development and will be improved in future releases. The goal is to harmonize how weights are saved and loaded across the Hub, both to remove code duplication across libraries and to establish consistent conventions.
8
8
9
-
## Save torch state dict
9
+
## Saving
10
10
11
11
The main helper of the `serialization` module takes a torch `nn.Module` as input and saves it to disk. It handles the logic to save shared tensors (see [safetensors explanation](https://huggingface.co/docs/safetensors/torch_shared_tensors)) as well as logic to split the state dictionary into shards, using [`split_torch_state_dict_into_shards`] under the hood. At the moment, only `torch` framework is supported.
12
12
13
13
If you want to save a state dictionary (e.g. a mapping between layer names and related tensors) instead of a `nn.Module`, you can use [`save_torch_state_dict`] which provides the same features. This is useful for example if you want to apply custom logic to the state dict before saving it.
14
14
15
+
### save_torch_model
16
+
15
17
[[autodoc]] huggingface_hub.save_torch_model
16
18
19
+
### save_torch_state_dict
20
+
17
21
[[autodoc]] huggingface_hub.save_torch_state_dict
18
22
19
-
## Split state dict into shards
20
23
21
24
The `serialization` module also contains low-level helpers to split a state dictionary into several shards, while creating a proper index in the process. These helpers are available for `torch` and `tensorflow` tensors and are designed to be easily extended to any other ML frameworks.
22
25
@@ -34,6 +37,19 @@ This is the underlying factory from which each framework-specific helper is deri
The loading helpers support both single-file and sharded checkpoints in either safetensors or pickle format. [`load_torch_model`] takes a `nn.Module` and a checkpoint path (either a single file or a directory) as input and load the weights into the model.
0 commit comments