| Crates.io | torsh-python |
| lib.rs | torsh-python |
| version | 0.1.0-alpha.2 |
| created_at | 2025-09-30 03:22:57.572219+00 |
| updated_at | 2025-12-22 05:24:42.946802+00 |
| description | Python bindings for ToRSh - PyTorch-compatible deep learning in Rust |
| homepage | https://github.com/cool-japan/torsh/ |
| repository | https://github.com/cool-japan/torsh/ |
| max_upload_size | |
| id | 1860523 |
| size | 523,392 |
Python bindings for ToRSh - a PyTorch-compatible deep learning framework built in pure Rust.
# Install maturin
pip install maturin
# Clone the repository
git clone https://github.com/cool-japan/torsh.git
cd torsh/crates/torsh-python
# Build and install in development mode
maturin develop
# Or build in release mode for better performance
maturin develop --release
pip install torsh
import torsh
# Device management
cpu = torsh.PyDevice("cpu")
cuda = torsh.PyDevice("cuda:0")
print(f"Device: {cpu.type}, Index: {cpu.index}")
# Data types
float32 = torsh.PyDType("float32")
int64 = torsh.PyDType("int64")
print(f"DType: {float32.name}, Size: {float32.itemsize} bytes")
# Check device availability
print(f"CUDA available: {torsh.cuda_is_available()}")
print(f"MPS available: {torsh.mps_is_available()}")
See examples/basic_usage.py for more examples.
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.
.pyi type stubs for IDE supportSee TODO.md for the full roadmap and progress tracking.
# Create devices
cpu = torsh.PyDevice("cpu")
cuda0 = torsh.PyDevice("cuda") # Default to cuda:0
cuda1 = torsh.PyDevice("cuda:1") # Specific GPU
metal = torsh.PyDevice("metal:0") # Apple Silicon
# Device properties
print(cuda1.type) # "cuda"
print(cuda1.index) # 1
# Device equality
cpu1 = torsh.PyDevice("cpu")
cpu2 = torsh.PyDevice("cpu")
assert cpu1 == cpu2
# Utility functions
torsh.device_count() # Number of devices
torsh.is_available() # General availability
torsh.cuda_is_available() # CUDA availability
torsh.mps_is_available() # Metal Performance Shaders availability
# Create dtypes
float32 = torsh.PyDType("float32") # or "f32"
float64 = torsh.PyDType("float64") # or "f64"
int32 = torsh.PyDType("int32") # or "i32"
int64 = torsh.PyDType("int64") # or "i64"
bool_type = torsh.PyDType("bool")
# DType properties
print(float32.name) # "float32"
print(float32.itemsize) # 4 (bytes)
print(float32.is_floating_point) # True
print(float32.is_signed) # True
# DType constants
torsh.float32 # Predefined dtype
torsh.float64
torsh.int32
torsh.int64
torsh.bool
# PyTorch-style aliases
torsh.float # Same as float32
torsh.double # Same as float64
torsh.long # Same as int64
torsh.int # Same as int32
# Custom errors
error = torsh.TorshError("Custom error message")
print(str(error)) # "Custom error message"
print(repr(error)) # "TorshError('Custom error message')"
# Built-in validation with helpful errors
try:
invalid_device = torsh.PyDevice("invalid")
except ValueError as e:
print(f"Error: {e}") # "Unknown device: invalid"
This crate strictly follows the SciRS2 POLICY:
scirs2-core abstractionsndarray, rand, num-traits, rayon, etc.scirs2_core::ndarray, scirs2_core::random, etc.scirs2_core onlySee SCIRS2_INTEGRATION_POLICY.md for full details.
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
# Run Rust unit tests
cargo test
# Run validation tests
cargo test --lib validation::tests
# Build Python extension for manual testing
maturin develop
python examples/basic_usage.py
# Install development dependencies
pip install -r requirements-dev.txt
# Build in debug mode
maturin develop
# Build in release mode
maturin develop --release
# Build wheel
maturin build --release
# Format Rust code
cargo fmt
# Lint Rust code
cargo clippy
# Format Python code
black examples/
# Type check Python code
mypy examples/
Benchmarks will be added once tensor operations are re-enabled.
See TODO.md for detailed task breakdown.
Contributions are welcome! Please follow these guidelines:
cargo fmt and cargo clippy before submittingLicensed under either of:
at your option.
Status: π§ Active Development | Version: 0.1.0-alpha.2 | Last Updated: 2025-10-24