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: | Daniel Falbel [aut, cre], Posit [cph] |
Maintainer: | Daniel Falbel <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.2.9000 |
Built: | 2024-11-16 05:50:59 UTC |
Source: | https://github.com/mlverse/safetensors |
Loads an safetensors file from disk.
safe_load_file(path, ..., framework = "torch", device = "cpu")
safe_load_file(path, ..., framework = "torch", device = "cpu")
path |
Path to the file to load |
... |
Unused |
framework |
Framework to load the data into. Currently only torch is supported |
device |
Device to copy data once loaded |
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) }
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) }
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) 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) ser <- safe_serialize(tensors) }
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.
con
the connection object with the file
metadata
an R list containing the metadata header in the file
framework
the framework used to return the tensors
device
the device to where tensors are copied
max_offset
the 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 = "torch", device = "cpu")
path
Path to the file to load
...
Unused
framework
Framework to load the data into. Currently only torch is supported
device
Device to copy data once loaded
keys()
Get the keys (tensor names) in the file
safetensors$keys()
get_tensor()
Get a tensor from its name
safetensors$get_tensor(name)
name
Name of the tensor to load
clone()
The objects of this class are cloneable with this method.
safetensors$clone(deep = FALSE)
deep
Whether 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) 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) f$get_tensor("x") }