ToRSh Python Bindings
Python bindings for ToRSh - a PyTorch-compatible deep learning framework built in pure Rust.
π Quick Start
Installation
From Source (Development)
# Install maturin
# Clone the repository
# Build and install in development mode
# Or build in release mode for better performance
From PyPI (Coming Soon)
Basic Usage
# Device management
=
=
# Data types
=
=
# Check device availability
See examples/basic_usage.py for more examples.
π Documentation
Current Status
Version: 0.1.0-alpha.2
Note: This crate is in active development. Many features are currently disabled due to dependency conflicts with scirs2-autograd and are being re-enabled incrementally.
β Available Features
- Device Management: CPU, CUDA, Metal device support with PyTorch-compatible API
- Data Type Handling: Complete dtype system with float32, int64, bool, etc.
- Error Handling: Comprehensive error types with helpful messages
- Validation Utilities: 25+ validation functions for input checking
- Type Stubs: Full
.pyitype stubs for IDE support - Documentation: Comprehensive documentation for all public APIs
β Currently Disabled (Coming Soon)
- Tensor operations and creation functions
- Neural network layers (torsh.nn)
- Optimization algorithms (torsh.optim)
- Automatic differentiation (torsh.autograd)
- Distributed training (torsh.distributed)
- Functional operations (torsh.F)
See TODO.md for the full roadmap and progress tracking.
API Reference
Device Management
# Create devices
=
= # Default to cuda:0
= # Specific GPU
= # Apple Silicon
# Device properties
# "cuda"
# 1
# Device equality
=
=
assert ==
# Utility functions
# Number of devices
# General availability
# CUDA availability
# Metal Performance Shaders availability
Data Types
# Create dtypes
= # or "f32"
= # or "f64"
= # or "i32"
= # or "i64"
=
# DType properties
# "float32"
# 4 (bytes)
# True
# True
# DType constants
# Predefined dtype
# PyTorch-style aliases
# Same as float32
# Same as float64
# Same as int64
# Same as int32
Error Handling
# Custom errors
=
# "Custom error message"
# "TorshError('Custom error message')"
# Built-in validation with helpful errors
=
# "Unknown device: invalid"
ποΈ Architecture
Design Principles
- PyTorch Compatibility: API designed to match PyTorch's interface for easy migration
- SciRS2 Integration: Built on top of the SciRS2 scientific computing ecosystem
- Type Safety: Leverages Rust's type system with Python type hints
- Performance: Zero-cost abstractions and efficient memory management
- Modularity: Clean separation between core functionality and Python bindings
SciRS2 POLICY Compliance
This crate strictly follows the SciRS2 POLICY:
- β
REQUIRED: All external dependencies accessed through
scirs2-coreabstractions - β
REQUIRED: No direct imports of
ndarray,rand,num-traits,rayon, etc. - β
REQUIRED: Unified access through
scirs2_core::ndarray,scirs2_core::random, etc. - β
MANDATORY: SIMD/parallel operations through
scirs2_coreonly
See SCIRS2_INTEGRATION_POLICY.md for full details.
Project Structure
torsh-python/
βββ src/
β βββ lib.rs # Main module registration
β βββ device.rs # Device management
β βββ dtype.rs # Data type handling
β βββ error.rs # Error handling
β βββ tensor/ # Tensor operations (disabled)
β βββ nn/ # Neural network layers (disabled)
β βββ optim/ # Optimizers (disabled)
β βββ utils/ # Validation and utilities
βββ python/
β βββ torsh/
β βββ __init__.pyi # Type stubs
β βββ py.typed # PEP 561 marker
βββ tests/ # Integration tests
βββ examples/ # Usage examples
βββ pyproject.toml # Python package metadata
βββ Cargo.toml # Rust package metadata
βββ README.md # This file
π§ͺ Testing
Running Tests
# Run Rust unit tests
# Run validation tests
# Build Python extension for manual testing
Test Coverage
- β Device Module: 30+ comprehensive tests covering all functionality
- β DType Module: 40+ tests for all data types and properties
- β Error Module: Error creation and conversion tests
- β Validation Module: 70+ tests for all validation functions
π οΈ Development
Prerequisites
- Rust 1.70+ (for GAT support)
- Python 3.8+
- Maturin 1.0+
Building from Source
# Install development dependencies
# Build in debug mode
# Build in release mode
# Build wheel
Code Quality
# Format Rust code
# Lint Rust code
# Format Python code
# Type check Python code
π Benchmarks
Benchmarks will be added once tensor operations are re-enabled.
πΊοΈ Roadmap
v0.1.0-alpha.3 (Next Release)
- Re-enable tensor operations
- Re-enable basic neural network layers
- Add tensor creation functions (zeros, ones, randn)
- Add basic tensor operations (add, mul, matmul)
v0.1.0-alpha.4
- Re-enable autograd support
- Re-enable optimizer implementations
- Add data loading utilities
- Performance benchmarks
v0.1.0-beta.1
- Distributed training support
- Complete PyTorch API compatibility
- Comprehensive documentation
- Full test coverage
See TODO.md for detailed task breakdown.
π€ Contributing
Contributions are welcome! Please follow these guidelines:
- SciRS2 POLICY: All code must follow the SciRS2 POLICY strictly
- Tests: Add comprehensive tests for all new functionality
- Documentation: Document all public APIs with examples
- Type Hints: Include Python type stubs (.pyi files)
- Code Quality: Run
cargo fmtandcargo clippybefore submitting
π License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
π Acknowledgments
- PyTorch: For the excellent API design that we strive to emulate
- SciRS2: For providing the scientific computing foundation
- PyO3: For excellent Rust-Python bindings
π Contact
- Repository: https://github.com/cool-japan/torsh
- Issues: https://github.com/cool-japan/torsh/issues
π Related Projects
- ToRSh - Main ToRSh framework
- SciRS2 - Scientific computing in Rust
- NumRS2 - Numerical computing library
- PyO3 - Rust-Python bindings
Status: π§ Active Development | Version: 0.1.0-alpha.2 | Last Updated: 2025-10-24