Safetensors documentation

Flax API

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.3.2).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Flax API

safetensors.flax.load_file

< >

( filename: Union ) β†’ Dict[str, Array]

Parameters

  • filename (str, or os.PathLike)) — The name of the file which contains the tensors

Returns

Dict[str, Array]

dictionary that contains name as key, value as Array

Loads a safetensors file into flax format.

Example:

from safetensors.flax import load_file

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

safetensors.flax.load

< >

( data: bytes ) β†’ Dict[str, Array]

Parameters

  • data (bytes) — The content of a safetensors file

Returns

Dict[str, Array]

dictionary that contains name as key, value as Array on cpu

Loads a safetensors file into flax format from pure bytes.

Example:

from safetensors.flax import load

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

loaded = load(data)

safetensors.flax.save_file

< >

( tensors: Dict filename: Union metadata: Optional = None ) β†’ None

Parameters

  • tensors (Dict[str, Array]) — 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.flax import save_file
from jax import numpy as jnp

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

safetensors.flax.save

< >

( tensors: Dict metadata: Optional = None ) β†’ bytes

Parameters

  • tensors (Dict[str, Array]) — 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.flax import save
from jax import numpy as jnp

tensors = {"embedding": jnp.zeros((512, 1024)), "attention": jnp.zeros((256, 256))}
byte_data = save(tensors)
< > Update on GitHub