Skip to contents

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

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.

Methods


Method new()

Opens the connection with the file

Usage

safetensors$new(path, ..., framework = "torch", device = "cpu")

Arguments

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


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

name

Name of the tensor to load


Method clone()

The objects of this class are cloneable with this method.

Usage

safetensors$clone(deep = FALSE)

Arguments

deep

Whether 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")
}
#> torch_tensor
#> -1.0069  1.3835 -0.4267  0.9520 -1.1701 -0.1416  1.8651 -0.5533  0.8212  0.5618
#> -0.0693  0.6516 -1.3801  1.1040  0.6986  0.4746 -2.4160 -0.6960 -0.5122  0.0510
#> -0.5953 -1.7474 -0.0464 -2.9962  1.7087  0.0263 -1.6959 -0.5752  1.0939 -0.6676
#> -0.9730  0.0677  2.3533 -0.7038 -0.3961 -0.8484 -0.0824  1.8545 -0.0144  2.4188
#> -2.1679 -0.0730 -0.6888  1.4964 -0.3781 -0.5696  0.2817 -1.5948 -0.5050 -0.6034
#>  0.9729 -0.2263  1.1841 -1.1103 -0.4616  0.2582 -1.1055 -0.8074  0.1076  0.2405
#> -0.3871  0.6849  0.8837 -0.3960 -0.0671 -0.5247 -1.0028 -0.1707  0.8143 -0.5429
#>  0.7049 -0.8462 -0.3540 -0.0198 -0.9670  1.0882 -0.0433 -0.3659  1.1965  1.0966
#> -2.9764 -0.0494 -1.5416 -0.0517  0.0758 -0.3580  0.2868  0.6684  0.9559  0.1280
#>  0.7369  1.7290 -1.2192 -1.7569  0.9882  0.5809 -2.3600  0.3209 -0.6417  0.6386
#> [ CPUFloatType{10,10} ]