| Title: | Safetensors File Format |
|---|---|
| Description: | A file format for storing tensors that is secure (doesn't allow for code execution), fast and simple to implement. 'safetensors' also enables cross language and cross frameworks compatibility making it an ideal format for storing machine learning model weights. |
| Authors: | Tomasz Kalinowski [ctb, cre], Daniel Falbel [aut], Sebastian Fischer [ctb], Posit [cph] |
| Maintainer: | Tomasz Kalinowski <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.1 |
| Built: | 2026-05-27 06:20:14 UTC |
| Source: | https://github.com/mlverse/safetensors |
Loads an safetensors file from disk.
safe_load_file(path, ..., framework)safe_load_file(path, ..., framework)
path |
Path to the file to load |
... |
Additional framework dependent arguments to pass to the tensor creation function. |
framework |
Framework to load the data into. Currently supports "torch" and "pjrt" |
A list with tensors in the file. The metadata attribute can be used
to find metadata the metadata header in the file.
if (rlang::is_installed("torch") && torch::torch_is_installed()) { tensors <- list(x = torch::torch_randn(10, 10)) temp <- tempfile() safe_save_file(tensors, temp) safe_load_file(temp, framework = "torch") }if (rlang::is_installed("torch") && torch::torch_is_installed()) { tensors <- list(x = torch::torch_randn(10, 10)) temp <- tempfile() safe_save_file(tensors, temp) safe_load_file(temp, framework = "torch") }
Writes a list of tensors to the safetensors format
safe_save_file(tensors, path, ..., metadata = NULL) safe_serialize(tensors, ..., metadata = NULL)safe_save_file(tensors, path, ..., metadata = NULL) safe_serialize(tensors, ..., metadata = NULL)
tensors |
A named list of tensors. Currently only torch tensors are supported. |
path |
The path to save the tensors to. It can also be a binary connection, as eg,
created with |
... |
Currently unused. |
metadata |
An optional string that is added to the file header. Possibly adding additional description to the weights. |
The path invisibly or a raw vector.
safe_serialize(): Serializes the tensors and returns a raw vector.
if (rlang::is_installed("torch") && torch::torch_is_installed()) { tensors <- list(x = torch::torch_randn(10, 10)) temp <- tempfile() safe_save_file(tensors, temp) safe_load_file(temp, framework = "torch") ser <- safe_serialize(tensors) }if (rlang::is_installed("torch") && torch::torch_is_installed()) { tensors <- list(x = torch::torch_randn(10, 10)) temp <- tempfile() safe_save_file(tensors, temp) safe_load_file(temp, framework = "torch") ser <- safe_serialize(tensors) }
Convert a tensor object to a raw buffer in the formated expected by safetensors.
safe_tensor_buffer(x)safe_tensor_buffer(x)
x |
(any) |
(raw)
Get the metadata from a tensor.
safe_tensor_meta(x)safe_tensor_meta(x)
x |
(any) |
(list)
Low level control over safetensors files
Low level control over safetensors files
Allows opening a connection to a safetensors file and query the tensor names, metadata, etc. Opening a connection only reads the file metadata into memory. This allows for more fined grained control over reading.
conthe connection object with the file
metadataan R list containing the metadata header in the file
frameworkthe framework used to return the tensors
argsadditional arguments for tensor creation
max_offsetthe largest offset boundary that was visited. Mainly used in torch to find the end of the safetensors file.
new()
Opens the connection with the file
safetensors$new(path, ..., framework)
pathPath to the file to load
...(any)
Additional, framework dependent, arguments to pass to use when creating the tensor.
For torch, this is the device, for pjrt the client.
frameworkFramework to load the data into. Currently supports "torch" and "pjrt"
keys()
Get the keys (tensor names) in the file
safetensors$keys()
get_tensor()
Get a tensor from its name
safetensors$get_tensor(name)
nameName of the tensor to load
clone()
The objects of this class are cloneable with this method.
safetensors$clone(deep = FALSE)
deepWhether to make a deep clone.
if (rlang::is_installed("torch") && torch::torch_is_installed()) { tensors <- list(x = torch::torch_randn(10, 10)) temp <- tempfile() safe_save_file(tensors, temp) f <- safetensors$new(temp, framework = "torch") f$get_tensor("x") }if (rlang::is_installed("torch") && torch::torch_is_installed()) { tensors <- list(x = torch::torch_randn(10, 10)) temp <- tempfile() safe_save_file(tensors, temp) f <- safetensors$new(temp, framework = "torch") f$get_tensor("x") }
A reflection of supported frameworks.
safetensors_frameworkssafetensors_frameworks
An object of class environment of length 1.