Torch API
Last updated
Last updated
safetensors.torch.load_file
( filename: typing.Union[str, os.PathLike]device = 'cpu' ) β Dict[str, torch.Tensor]
Parameters
filename (str
, or os.PathLike
) β The name of the file which contains the tensors
device (Dict[str, any]
, optional, defaults to cpu
) β The device where the tensors need to be located after load. available options are all regular torch device locations
Returns
Dict[str, torch.Tensor]
dictionary that contains name as key, value as torch.Tensor
Loads a safetensors file into torch format.
Example:
Copied
safetensors.torch.load
( data: bytes ) β Dict[str, torch.Tensor]
Parameters
data (bytes
) β The content of a safetensors file
Returns
Dict[str, torch.Tensor]
dictionary that contains name as key, value as torch.Tensor
on cpu
Loads a safetensors file into torch format from pure bytes.
Example:
Copied
safetensors.torch.save_file
( tensors: typing.Dict[str, torch.Tensor]filename: typing.Union[str, os.PathLike]metadata: typing.Union[typing.Dict[str, str], NoneType] = None ) β None
Parameters
tensors (Dict[str, torch.Tensor]
) β The incoming tensors. Tensors need to be contiguous and dense.
filename (str
, or os.PathLike
)) β The filename weβre saving into.
metadata (Dict[str, str]
, optional, defaults to None
) β Optional text only metadata you might want to save in your header. For instance it can be useful to specify more about the underlying tensors. This is purely informative and does not affect tensor loading.
Returns
None
Saves a dictionary of tensors into raw bytes in safetensors format.
Example:
Copied
safetensors.torch.save
( tensors: typing.Dict[str, torch.Tensor]metadata: typing.Union[typing.Dict[str, str], NoneType] = None ) β bytes
Parameters
tensors (Dict[str, torch.Tensor]
) β The incoming tensors. Tensors need to be contiguous and dense.
metadata (Dict[str, str]
, optional, defaults to None
) β Optional text only metadata you might want to save in your header. For instance it can be useful to specify more about the underlying tensors. This is purely informative and does not affect tensor loading.
Returns
bytes
The raw bytes representing the format
Saves a dictionary of tensors into raw bytes in safetensors format.
Example:
Copied
safetensors.torch.load_model
( model: Modulefilename: typing.Union[str, os.PathLike]strict = True ) β `(missing, unexpected)
Parameters
model (torch.nn.Module
) β The model to load onto.
filename (str
, or os.PathLike
) β The filename location to load the file from.
strict (bool
, optional, defaults to True) β Wether to fail if youβre missing keys or having unexpected ones When false, the function simply returns missing and unexpected names.
Returns
`(missing, unexpected)
(List[str], List[str])
missingare names in the model which were not modified during loading
unexpected` are names that are on the file, but werenβt used during the load.
safetensors.torch.save_model
( model: Modulefilename: strmetadata: typing.Union[typing.Dict[str, str], NoneType] = Noneforce_contiguous: bool = True )
Parameters
model (torch.nn.Module
) β The model to save on disk.
filename (str
) β The filename location to save the file
metadata (Dict[str, str]
, optional) β Extra information to save along with the file. Some metadata will be added for each dropped tensors. This information will not be enough to recover the entire shared structure but might help understanding things
force_contiguous (boolean
, optional, defaults to True) β Forcing the state_dict to be saved as contiguous tensors. This has no effect on the correctness of the model, but it could potentially change performance if the layout of the tensor was chosen specifically for that reason.
Loads a given filename onto a torch model. This method exists specifically to avoid tensor sharing issues which are not allowed in safetensors
.
Saves a given torch model to specified filename. This method exists specifically to avoid tensor sharing issues which are not allowed in safetensors
.