| safetensors {safetensors} | R Documentation |
Low level control over safetensors files
Description
Low level control over safetensors files
Low level control over safetensors files
Details
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.
Public fields
conthe connection object with the file
metadataan R list containing the metadata header in the file
frameworkthe framework used to return the tensors
devicethe device to where tensors are copied
max_offsetthe largest offset boundary that was visited. Mainly used in torch to find the end of the safetensors file.
Methods
Public methods
Method new()
Opens the connection with the file
Usage
safetensors$new(path, ..., framework = "torch", device = "cpu")
Arguments
pathPath to the file to load
...Unused
frameworkFramework to load the data into. Currently only torch is supported
deviceDevice to copy data once loaded
Method keys()
Get the keys (tensor names) in the file
Usage
safetensors$keys()
Method get_tensor()
Get a tensor from its name
Usage
safetensors$get_tensor(name)
Arguments
nameName of the tensor to load
Method clone()
The objects of this class are cloneable with this method.
Usage
safetensors$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
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")
}