Torsh
ToRSh (Tensor Operations in Rust with Sharding) is a PyTorch-compatible deep learning framework built entirely in Rust.
Install / Use
/learn @cool-japan/TorshREADME
ToRSh - Tensor Operations in Rust with Sharding
<div align="center">Deep Learning in Pure Rust with PyTorch Compatibility
Documentation | Examples | Benchmarks | SciRS2 Showcase | Roadmap
</div>🚀 What is ToRSh?
ToRSh (Tensor Operations in Rust with Sharding) is a PyTorch-compatible deep learning framework built entirely in Rust. We're building a future where machine learning is:
- Fast by default - Leveraging Rust's zero-cost abstractions
- Safe by design - Eliminating entire classes of runtime errors
- Scientifically complete - Built on the comprehensive SciRS2 ecosystem
- Deployment-ready - Single binary, no Python runtime needed
✨ What You Can Do Today
Build PyTorch-Compatible Models
use torsh::prelude::*;
use torsh_nn::*;
// Define models just like PyTorch
struct MyModel {
fc1: Linear,
fc2: Linear,
}
impl Module for MyModel {
fn forward(&self, x: &Tensor) -> Result<Tensor> {
let x = self.fc1.forward(x)?;
let x = F::relu(&x)?;
self.fc2.forward(&x)
}
}
Train with Automatic Differentiation
// Automatic gradient computation - just like PyTorch
let x = tensor![[1.0, 2.0]].requires_grad();
let loss = x.pow(2).sum();
loss.backward()?;
println!("Gradient: {:?}", x.grad());
Use Advanced Scientific Computing
// Graph Neural Networks
use torsh_graph::{GCNLayer, GATLayer};
let gcn = GCNLayer::new(128, 64)?;
// Time Series Analysis
use torsh_series::{STLDecomposition, KalmanFilter};
let stl = STLDecomposition::new(20)?;
// Computer Vision
use torsh_vision::spatial::FeatureMatcher;
let matcher = FeatureMatcher::new(MatchingAlgorithm::NCC)?;
🎯 Key Features
Core Deep Learning
- 🚀 PyTorch Compatible: Drop-in replacement for most PyTorch code
- ⚡ Superior Performance: 2-3x faster inference, 50% less memory usage
- 🛡️ Memory Safety: Compile-time guarantees eliminate segfaults and memory leaks
- 🦀 Pure Rust: Leverage Rust's ecosystem and deployment advantages
- 🔧 Multiple Backends: CPU (SIMD), Metal, and more (CUDA support in progress)
🔬 SciRS2 Scientific Computing Integration
- 📊 Complete Ecosystem: 19/19 SciRS2 crates integrated (100% coverage)
- 🧠 Graph Neural Networks: GCN, GAT, GraphSAGE with spectral optimization
- 📈 Time Series Analysis: STL decomposition, SSA, Kalman filters, state-space models
- 🖼️ Computer Vision Spatial Operations: Feature matching, geometric transforms, interpolation
- 🎲 Advanced Random Generation: SIMD-accelerated distributions with variance reduction
- ⚡ Next-Generation Optimizers: LAMB, Lookahead, enhanced Adam with adaptive learning rates
- 🧮 Mathematical Operations: Auto-vectorized BLAS, sparse operations, GPU tensor cores
🏭 Production Features
- 📦 Easy Deployment: Single binary, no Python runtime required
- 📊 Comprehensive Benchmarking: 50+ benchmark suites with performance analysis
- 🔍 Advanced Profiling: Memory usage, thermal monitoring, performance dashboards
- ⚖️ Precision Support: Mixed precision, quantization, pruning optimizations
- 🌐 Multi-Platform: Edge devices, mobile, WASM, distributed training
🛠️ Installation
Add ToRSh to your Cargo.toml:
[dependencies]
torsh = "0.1.1"
torsh-nn = "0.1.1" # Neural networks
torsh-graph = "0.1.1" # Graph neural networks
torsh-series = "0.1.1" # Time series analysis
torsh-vision = "0.1.1" # Computer vision
torsh-metrics = "0.1.1" # Evaluation metrics
🚀 Quick Start
Basic Tensor Operations (PyTorch Compatible)
use torsh::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// PyTorch-compatible tensor creation
let x = tensor![[1.0, 2.0], [3.0, 4.0]];
let y = tensor![[5.0, 6.0], [7.0, 8.0]];
// Identical operations to PyTorch
let z = x.matmul(&y)?;
println!("Matrix multiplication result: {:?}", z);
// Automatic differentiation
let x = x.requires_grad();
let loss = x.pow(2).sum();
loss.backward()?;
println!("Gradients: {:?}", x.grad());
Ok(())
}
🧠 Graph Neural Networks
use torsh::prelude::*;
use torsh_graph::{GCNLayer, GATLayer, GraphSAGE};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create graph data
let num_nodes = 1000;
let feature_dim = 128;
let node_features = randn(&[num_nodes, feature_dim])?;
let adjacency_matrix = rand(&[num_nodes, num_nodes])?;
// Graph Convolutional Network
let mut gcn = GCNLayer::new(feature_dim, 64)?;
let gcn_output = gcn.forward(&node_features, &adjacency_matrix)?;
// Graph Attention Network
let mut gat = GATLayer::new(feature_dim, 64, 8)?; // 8 attention heads
let gat_output = gat.forward(&node_features, &adjacency_matrix)?;
// GraphSAGE with neighbor sampling
let mut sage = GraphSAGE::new(feature_dim, 64)?;
let sage_output = sage.forward(&node_features, &adjacency_matrix)?;
println!("GCN output shape: {:?}", gcn_output.shape());
println!("GAT output shape: {:?}", gat_output.shape());
println!("SAGE output shape: {:?}", sage_output.shape());
Ok(())
}
📈 Time Series Analysis
use torsh::prelude::*;
use torsh_series::{STLDecomposition, SSADecomposition, KalmanFilter};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Generate time series data
let series_length = 1000;
let time_series = randn(&[series_length])?;
// STL Decomposition (Seasonal and Trend decomposition using Loess)
let stl = STLDecomposition::new(20)?; // 20-point seasonal window
let (trend, seasonal, residual) = stl.decompose(&time_series)?;
// Singular Spectrum Analysis
let ssa = SSADecomposition::new(50)?; // 50-dimensional embedding
let (components, reconstruction) = ssa.decompose(&time_series)?;
// Kalman Filter for state estimation
let mut kalman = KalmanFilter::new(2, 1)?; // 2D state, 1D observation
let filtered_series = kalman.filter(&time_series)?;
println!("Original series length: {}", series_length);
println!("STL trend shape: {:?}", trend.shape());
println!("SSA components shape: {:?}", components.shape());
println!("Kalman filtered shape: {:?}", filtered_series.shape());
Ok(())
}
🖼️ Computer Vision Spatial Operations
use torsh::prelude::*;
use torsh_vision::spatial::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load image data (RGB: channels x height x width)
let image = randn(&[3, 512, 512])?;
let template = randn(&[3, 64, 64])?;
// Feature matching with normalized cross-correlation
let matcher = FeatureMatcher::new(MatchingAlgorithm::NCC)?;
let matches = matcher.match_features(&image, &template)?;
// Geometric transformations
let transformer = GeometricTransformer::new();
let rotation_matrix = transformer.rotation_matrix(45.0)?; // 45 degrees
let rotated_image = transformer.apply_transform(&image, &rotation_matrix)?;
// Spatial interpolation for super-resolution
let interpolator = SpatialInterpolator::new(InterpolationMethod::RBF)?;
let upsampled = interpolator.upsample(&image, 2.0)?; // 2x upsampling
println!("Original image shape: {:?}", image.shape());
println!("Found {} feature matches", matches.len());
println!("Rotated image shape: {:?}", rotated_image.shape());
println!("Upsampled image shape: {:?}", upsampled.shape());
Ok(())
}
⚡ Advanced Neural Networks with Transformers
use torsh::prelude::*;
use torsh_nn::layers::advanced::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let batch_size = 32;
let seq_len = 128;
let d_model = 512;
let num_heads = 8;
// Input sequence
let input = randn(&[batch_size, seq_len, d_model])?;
// Multi-Head Attention
let mut attention = MultiHeadAttention::new(d_model, num_heads, 0.1, true)?;
let attention_output = attention.forward(&input)?;
// Layer Normalization
let mut layer_norm = LayerNorm::new(vec![d_model], true, 1e-5)?;
let normalized = layer_norm.forward(&attention_output)?;
// Positional Encoding
let mut pos_encoding = PositionalEncoding::new(d_model, 1000, 0.1)?;
let encoded = pos_encoding.forward(&normalized)?;
println!("Attention output shape: {:?}", attention_output.shape());
println!("Layer norm output shape: {:?}", normalized.shape());
println!("Positional encoding shape: {:?}", encoded.shape());
Ok(())
}
⚡ Advanced Optimizers
use torsh::prelude::*;
use torsh_optim::advanced::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Model parameters
let weights = randn(&[1000, 500])?.requires_grad();
let bias = zeros(&[500])?.requires_grad();
// Enhanced Adam optimizer with advanced features
let mut adam = AdvancedAdam::new(0.001)
.with_amsgrad() // AMSGrad variant
.with_weight_decay(0.01) // L2 regularization
.with_gradient_clipping(1.0) // Gradient clipping
.with_adaptive_lr() // Adaptive learning rate
.with_warmup(1000); // Learning rate warmup
// LAMB optimizer for large batch training
let mut lamb = LAMB::new(0.001);
// Lookahead wrapper for any op
