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
args
additional arguments for tensor creation
max_offset
the largest offset boundary that was visited. Mainly used in torch to find the end of the safetensors file.
Methods
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, framework = "torch")
f$get_tensor("x")
}
#> torch_tensor
#> 1.1603 -2.8112 -1.0078 -0.5570 -2.0469 -0.7907 -1.0431 0.6998 2.2470 0.2334
#> 0.3276 0.7526 -0.7526 0.2288 0.5493 -1.6710 1.5828 -1.4253 0.5906 -0.0762
#> -0.6963 -1.6003 -1.0440 -2.2177 1.5408 0.2955 0.6314 -1.4826 -1.9343 -0.3990
#> -0.5320 -1.4545 0.3000 -0.5193 -0.4956 1.2259 -1.5380 -0.8141 -0.1457 1.9022
#> -0.4099 -1.0397 -1.5297 -0.7222 -1.5332 -0.2155 0.9406 1.3421 0.8129 -0.1776
#> 0.0434 0.1414 -0.9594 0.3958 0.2655 0.6186 -0.5971 -0.7222 -0.5064 -0.0778
#> -1.5162 -0.7071 -1.0614 -0.9679 -0.4573 -1.6129 -0.7667 -1.3759 -0.0680 -1.5809
#> -1.6287 -1.6180 1.6495 2.1286 -0.0473 -2.4814 -0.3256 0.0930 -1.0490 -1.2216
#> 1.5972 -0.2822 -0.9054 0.4230 1.1840 0.9509 -0.0317 -0.1420 -0.7612 -0.8332
#> 0.5695 -1.7658 -0.4732 -0.9734 0.8526 0.1465 -0.9473 -2.2815 0.7963 -0.3349
#> [ CPUFloatType{10,10} ]