ankane/torch.rb
Deep learning for Ruby, powered by LibTorch
Torch.rb – Deep Learning for Ruby
What it is
- A Ruby gem that wraps Facebook’s LibTorch (the C++ core of PyTorch) so you can write and run PyTorch‑style deep‑learning code in Ruby.
- Mirrors the PyTorch Python API closely, with Ruby‑idiomatic tweaks (e.g.,
add!for in‑place ops,tensor?for boolean checks, andNumoarrays instead of NumPy).
Key components
- Tensor operations – creation (
Torch.rand,Torch.zeros, etc.), arithmetic, indexing, conversion to/fromNumo::NArray. - Autograd – automatic differentiation with
requires_grad,backward, and gradient access via.grad. - Neural‑network module – define models by subclassing
Torch::NN::Module, use layers likeConv2d,Linear, and functional helpers underTorch::NN::F. - Optimizers – e.g.,
Torch::Optim::SGDwith typicalzero_grad,stepworkflow. - Saving/loading –
Torch.save/Torch.loadfor model state dictionaries (compatible with PyTorch files, with a small Python‑side conversion note). - Device support – CPU, CUDA GPUs (
Torch::CUDA.available?,net.cuda), Apple‑silicon Metal (Torch::Backends::MPS).
Installation
- Download a matching LibTorch build for your platform (CPU‑only or CUDA‑enabled). Example for macOS arm64:
curl -L https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-2.14.0.zip > libtorch.zip unzip -q libtorch.zip - Tell the gem where LibTorch lives and add it to your
Gemfile:bundle config set build.torch-rb --with-torch-dir=/path/to/libtorchgem "torch-rb" - Run
bundle install. Compilation takes ~5‑10 minutes; Windows is not supported.
Getting started
- Follow the 60‑minute blitz tutorial in
tutorials/blitz/README.md. - Additional tutorials cover transfer learning, sequence models, and word embeddings.
- Example projects include MNIST image classification, MovieLens collaborative filtering, and GANs.
Typical workflow (Ruby code)
# Define a simple CNN
class MyNet < Torch::NN::Module
def initialize
super()
@conv1 = Torch::NN::Conv2d.new(1, 6, 3)
@conv2 = Torch::NN::Conv2d.new(6, 16, 3)
@fc1 = Torch::NN::Linear.new(16*6*6, 120)
@fc2 = Torch::NN::Linear.new(120, 84)
@fc3 = Torch::NN::Linear.new(84, 10)
end
def forward(x)
x = Torch::NN::F.max_pool2d(Torch::NN::F.relu(@conv1.call(x)), [2,2])
x = Torch::NN::F.max_pool2d(Torch::NN::F.relu(@conv2.call(x)), 2)
x = Torch.flatten(x, 1)
x = Torch::NN::F.relu(@fc1.call(x))
x = Torch::NN::F.relu(@fc2.call(x))
@fc3.call(x)
end
end
net = MyNet.new
input = Torch.randn(1,1,32,32)
output = net.call(input)
criterion = Torch::NN::MSELoss.new
target = Torch.randn(10).view(1,-1)
loss = criterion.call(output, target)
optimizer = Torch::Optim::SGD.new(net.parameters, lr: 0.01)
optimizer.zero_grad
loss.backward
optimizer.step
Device handling
if Torch::CUDA.available?
net.cuda # move model to GPU
input = input.cuda
end
Or on Apple silicon:
if Torch::Backends::MPS.available?
device = Torch.device('mps')
net.to(device)
end
Ecosystem
- Companion gems for specific domains:
torchvision-ruby,torchtext-ruby,torchaudio-ruby,torchcodec-ruby,torchrec-ruby,torchdata-ruby. - Higher‑level tools:
transformers-ruby(transformer models) andsafetensors-ruby(efficient tensor storage).
Why it matters
- Enables Ruby developers to experiment with modern deep‑learning models without switching languages.
- Leverages the same performant C++ backend used by PyTorch, so models run at native speed on CPU or GPU.
- Keeps the Ruby community aligned with the rapidly evolving PyTorch ecosystem through a familiar API.
Resources
- Build status badge shows CI testing on multiple platforms.
- Detailed changelog, contribution guide, and scripts for GPU testing (AWS Deep Learning AMI) are provided.
Torch.rb brings the power of PyTorch to Ruby, offering a full‑featured, GPU‑accelerated deep‑learning stack that feels native to Ruby programmers.
Related
- Project
- Project
- Project
- Project