Skip to contents

Loads an safetensors file from disk.

Usage

safe_load_file(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

Value

A list with tensors in the file. The metadata attribute can be used to find metadata the metadata header in the file.

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)
  safe_load_file(temp)
}
#> $x
#> torch_tensor
#> -1.0264 -0.1272  2.4600 -0.6395 -0.5108  0.4219  0.5495  0.4981 -0.6282 -1.0604
#>  0.0341 -0.7117  0.0621 -0.9795 -2.0807  0.1708  0.6251 -1.3893 -0.6157  2.1258
#>  0.4742  0.8293  0.4114  0.5539  1.1749 -0.9905 -2.2213 -0.4449 -0.2060  0.9405
#>  1.7901  0.5764 -0.3047 -0.4118 -1.0820 -1.7611 -0.1740  1.2009  0.6635 -1.6621
#> -0.9257 -0.2052  0.4933  0.9529 -0.0232  0.4427  0.0287  0.5507 -0.1719  0.7154
#>  0.0491  0.2569 -0.4986  1.3212 -1.0887  1.8834 -0.5332 -0.7718 -0.7900  0.0959
#> -0.3781 -0.5297  0.1070 -0.8688  0.2756 -0.4094 -0.8079 -2.0410  0.6121  0.1556
#>  0.3145 -0.0528 -0.4711 -0.0395 -0.7409  0.3515  1.4764  1.2742 -0.1894  0.2932
#> -0.8532 -0.0502  0.4013  0.1974  0.5432  0.1106  0.8722 -0.4405  1.4817 -0.1325
#>  1.3554 -0.7546  0.1736 -0.7532 -2.1116 -0.6069 -0.1033  0.8853 -0.8112 -1.5441
#> [ CPUFloatType{10,10} ]
#> 
#> attr(,"metadata")
#> attr(,"metadata")$x
#> attr(,"metadata")$x$shape
#> [1] 10 10
#> 
#> attr(,"metadata")$x$dtype
#> [1] "F32"
#> 
#> attr(,"metadata")$x$data_offsets
#> [1]   0 400
#> 
#> 
#> attr(,"max_offset")
#> [1] 468