Safetensors documentation

Torch API

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Torch API

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:

from safetensors.torch import load_file

file_path = "./my_folder/bert.safetensors"
loaded = load_file(file_path)

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:

from safetensors.torch import load

file_path = "./my_folder/bert.safetensors"
with open(file_path, "rb") as f:
    data = f.read()

loaded = load(data)

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:

from safetensors.torch import save_file
import torch

tensors = {"embedding": torch.zeros((512, 1024)), "attention": torch.zeros((256, 256))}
save_file(tensors, "model.safetensors")

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:

from safetensors.torch import save
import torch

tensors = {"embedding": torch.zeros((512, 1024)), "attention": torch.zeros((256, 256))}
byte_data = save(tensors)