Safetensors Ruby

:slightly_smiling_face: 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