0.0
A long-lived project that still receives updates
Simple, safe way to store and distribute tensors
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 0
 Project Readme

Safetensors Ruby

🙂 Simple, safe way to store and distribute tensors

Supports Torch.rb and Numo

Build Status

Installation

Add this line to your application’s Gemfile:

gem "safetensors"

Getting Started

Save tensors

tensors = {
  "weight1" => Torch.zeros([1024, 1024]),
  "weight2" => Torch.zeros([1024, 1024])
}
Safetensors::Torch.save_file(tensors, "model.safetensors")

Load tensors

tensors = Safetensors::Torch.load_file("model.safetensors")
# or
tensors = {}
Safetensors.safe_open("model.safetensors", framework: "torch", device: "cpu") do |f|
  f.keys.each do |key|
    tensors[key] = f.get_tensor(key)
  end
end

API

This library follows the Safetensors Python API. You can follow Python tutorials and convert the code to Ruby in many cases. Feel free to open an issue if you run into problems.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/safetensors-ruby.git
cd safetensors-ruby
bundle install
bundle exec rake compile
bundle exec rake test