Safetensors File Format

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.


safetensors

R-CMD-check

safetensors is a pure R implementation of the safetensors file format for both reading and writing. It currently supports the {torch} and {pjrt} frameworks.

Installation

safetensors can be installed from CRAN with:

install.packages("safetensors")

The development version of safetensors from GitHub with:

# install.packages("devtools")
devtools::install_github("mlverse/safetensors")

Example

Here’s an example of writing and reading safetensors files in combination with torch:

library(torch)
library(safetensors)

tensors <- list(
  x = torch_randn(10, 10),
  y = torch_ones(10, 10)
)

str(tensors)
#> List of 2
#>  $ x:Float [1:10, 1:10]
#>  $ y:Float [1:10, 1:10]

tmp <- tempfile()
safe_save_file(tensors, tmp)

tensors <- safe_load_file(tmp, framework = "torch")
str(tensors)
#> List of 2
#>  $ x:Float [1:10, 1:10]
#>  $ y:Float [1:10, 1:10]
#>  - attr(*, "metadata")=List of 2
#>   ..$ x:List of 3
#>   .. ..$ shape       : int [1:2] 10 10
#>   .. ..$ dtype       : chr "F32"
#>   .. ..$ data_offsets: int [1:2] 0 400
#>   ..$ y:List of 3
#>   .. ..$ shape       : int [1:2] 10 10
#>   .. ..$ dtype       : chr "F32"
#>   .. ..$ data_offsets: int [1:2] 400 800
#>  - attr(*, "max_offset")= num 929

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("safetensors")

0.3.0 by Tomasz Kalinowski, 2 months ago


https://github.com/mlverse/safetensors, https://mlverse.github.io/safetensors/


Report a bug at https://github.com/mlverse/safetensors/issues


Browse source code at https://github.com/cran/safetensors


Authors: Tomasz Kalinowski [ctb, cre] , Daniel Falbel [aut] , Sebastian Fischer [ctb] , Posit [cph]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cli, jsonlite, R6, rlang

Suggests testthat, torch


Imported by Rhobots, brulee, chatterbox, torch, whisper.

Suggested by diffuseR.


See at CRAN