Tensorflow API
Tensorflow API
safetensors.tensorflow.load_file
( filename: typing.Union[str, os.PathLike] ) β Dict[str, tf.Tensor]
Parameters
filename (
str
, oros.PathLike
)) β The name of the file which contains the tensorsdevice (
Dict[str, any]
, optional, defaults tocpu
) β The device where the tensors need to be located after load. available options are all regular tensorflow device locations
Returns
Dict[str, tf.Tensor]
dictionary that contains name as key, value as tf.Tensor
Loads a safetensors file into tensorflow format.
Example:
Copied
from safetensors.tensorflow import load_file
file_path = "./my_folder/bert.safetensors"
loaded = load_file(file_path)
safetensors.tensorflow.load
( data: bytes ) β Dict[str, tf.Tensor]
Parameters
data (
bytes
) β The content of a safetensors file
Returns
Dict[str, tf.Tensor]
dictionary that contains name as key, value as tf.Tensor
on cpu
Loads a safetensors file into tensorflow format from pure bytes.
Example:
Copied
from safetensors.tensorflow import load
file_path = "./my_folder/bert.safetensors"
with open(file_path, "rb") as f:
data = f.read()
loaded = load(data)
safetensors.tensorflow.save_file
( tensors: typing.Dict[str, tensorflow.python.framework.ops.Tensor]filename: typing.Union[str, os.PathLike]metadata: typing.Union[typing.Dict[str, str], NoneType] = None ) β None
Parameters
tensors (
Dict[str, tf.Tensor]
) β The incoming tensors. Tensors need to be contiguous and dense.filename (
str
, oros.PathLike
)) β The filename weβre saving into.metadata (
Dict[str, str]
, optional, defaults toNone
) β 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 dictionnary of tensors into raw bytes in safetensors format.
Example:
Copied
from safetensors.tensorflow import save_file
import tensorflow as tf
tensors = {"embedding": tf.zeros((512, 1024)), "attention": tf.zeros((256, 256))}
save(tensors, "model.safetensors")
safetensors.tensorflow.save
( tensors: typing.Dict[str, tensorflow.python.framework.ops.Tensor]metadata: typing.Union[typing.Dict[str, str], NoneType] = None ) β bytes
Parameters
tensors (
Dict[str, tf.Tensor]
) β The incoming tensors. Tensors need to be contiguous and dense.metadata (
Dict[str, str]
, optional, defaults toNone
) β 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 dictionnary of tensors into raw bytes in safetensors format.
Example:
Copied
from safetensors.tensorflow import save
import tensorflow as tf
tensors = {"embedding": tf.zeros((512, 1024)), "attention": tf.zeros((256, 256))}
byte_data = save(tensors)
Last updated