🙂 Simple, safe way to store and distribute tensors
Add this line to your application’s Gemfile:
gem "safetensors"
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
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.
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
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