PyTorch-compatible deep learning framework in Rust - Python bindings
Project description
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
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
From PyPI (Coming Soon)
pip install RsTorch
Basic Usage
import rstorch
# Device management
cpu = rstorch.PyDevice("cpu")
cuda = rstorch.PyDevice("cuda:0")
print(f"Device: {cpu.type}, Index: {cpu.index}")
# Data types
float32 = rstorch.PyDType("float32")
int64 = rstorch.PyDType("int64")
print(f"DType: {float32.name}, Size: {float32.itemsize} bytes")
# Check device availability
print(f"CUDA available: {rstorch.cuda_is_available()}")
print(f"MPS available: {rstorch.mps_is_available()}")
See examples/basic_usage.py for more examples.
๐ Documentation
Current Status
Version: 0.1.0
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 (rstorch.nn)
- Optimization algorithms (rstorch.optim)
- Automatic differentiation (rstorch.autograd)
- Distributed training (rstorch.distributed)
- Functional operations (rstorch.F)
See TODO.md for the full roadmap and progress tracking.
API Reference
Device Management
# Create devices
cpu = rstorch.PyDevice("cpu")
cuda0 = rstorch.PyDevice("cuda") # Default to cuda:0
cuda1 = rstorch.PyDevice("cuda:1") # Specific GPU
metal = rstorch.PyDevice("metal:0") # Apple Silicon
# Device properties
print(cuda1.type) # "cuda"
print(cuda1.index) # 1
# Device equality
cpu1 = rstorch.PyDevice("cpu")
cpu2 = rstorch.PyDevice("cpu")
assert cpu1 == cpu2
# Utility functions
rstorch.device_count() # Number of devices
rstorch.is_available() # General availability
rstorch.cuda_is_available() # CUDA availability
rstorch.mps_is_available() # Metal Performance Shaders availability
Data Types
# Create dtypes
float32 = rstorch.PyDType("float32") # or "f32"
float64 = rstorch.PyDType("float64") # or "f64"
int32 = rstorch.PyDType("int32") # or "i32"
int64 = rstorch.PyDType("int64") # or "i64"
bool_type = rstorch.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
rstorch.float32 # Predefined dtype
rstorch.float64
rstorch.int32
rstorch.int64
rstorch.bool
# PyTorch-style aliases
rstorch.float # Same as float32
rstorch.double # Same as float64
rstorch.long # Same as int64
rstorch.int # Same as int32
Error Handling
# Custom errors
error = rstorch.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 = rstorch.PyDevice("invalid")
except ValueError as e:
print(f"Error: {e}") # "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
cargo test
# Run validation tests
cargo test --lib validation::tests
# Build Python extension for manual testing
maturin develop
python examples/basic_usage.py
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
pip install -r requirements-dev.txt
# Build in debug mode
maturin develop
# Build in release mode
maturin develop --release
# Build wheel
maturin build --release
Code Quality
# Format Rust code
cargo fmt
# Lint Rust code
cargo clippy
# Format Python code
black examples/
# Type check Python code
mypy examples/
๐ Benchmarks
Benchmarks will be added once tensor operations are re-enabled.
๐บ๏ธ Roadmap
v0.1.0 (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
- Re-enable autograd support
- Re-enable optimizer implementations
- Add data loading utilities
- Performance benchmarks
v0.1.0
- 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 the Apache License, Version 2.0. See LICENSE for details.
๐ 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 | Last Updated: 2025-10-24
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rstorch-0.1.0a2.tar.gz.
File metadata
- Download URL: rstorch-0.1.0a2.tar.gz
- Upload date:
- Size: 3.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
694a22ba2d41e9f66fb731e78d108153afe200d22a4db5d70776834982a38cc3
|
|
| MD5 |
176c895dbdc5735b4e51771fcc6cf332
|
|
| BLAKE2b-256 |
21b9deb855f7c80160928d7f98f47d4e5793837735e03fa873a438cd476a964e
|
Provenance
The following attestation bundles were made for rstorch-0.1.0a2.tar.gz:
Publisher:
pypi-publish.yml on cool-japan/torsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rstorch-0.1.0a2.tar.gz -
Subject digest:
694a22ba2d41e9f66fb731e78d108153afe200d22a4db5d70776834982a38cc3 - Sigstore transparency entry: 976562924
- Sigstore integration time:
-
Permalink:
cool-japan/torsh@077a056a4737b480add9223356dd9b63988ea96c -
Branch / Tag:
refs/heads/0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@077a056a4737b480add9223356dd9b63988ea96c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file rstorch-0.1.0a2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: rstorch-0.1.0a2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 861.6 kB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
052f7e3b249ac723fe4167302fe73b1958fa6ba32960aa575dbbff219d794581
|
|
| MD5 |
f4fc6e9e89ebca8a8f62e72d0ce71241
|
|
| BLAKE2b-256 |
ed7863ec0efc1ade9d3f942963ad9f71b139350d89e426c7336a079b65c98d05
|
Provenance
The following attestation bundles were made for rstorch-0.1.0a2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
pypi-publish.yml on cool-japan/torsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rstorch-0.1.0a2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
052f7e3b249ac723fe4167302fe73b1958fa6ba32960aa575dbbff219d794581 - Sigstore transparency entry: 976562946
- Sigstore integration time:
-
Permalink:
cool-japan/torsh@077a056a4737b480add9223356dd9b63988ea96c -
Branch / Tag:
refs/heads/0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@077a056a4737b480add9223356dd9b63988ea96c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file rstorch-0.1.0a2-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: rstorch-0.1.0a2-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 847.1 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3789fb286b579a26876d2cda6fc9561a969be50553ec6477d464f6f76e2005a
|
|
| MD5 |
17b1110cb2faf816ee45cd7644cf4fa4
|
|
| BLAKE2b-256 |
1fd21bc64ff969be6872c9cf2cd7de83ff834cdc5c550dc9b077f798675e8159
|
Provenance
The following attestation bundles were made for rstorch-0.1.0a2-cp38-abi3-win_amd64.whl:
Publisher:
pypi-publish.yml on cool-japan/torsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rstorch-0.1.0a2-cp38-abi3-win_amd64.whl -
Subject digest:
e3789fb286b579a26876d2cda6fc9561a969be50553ec6477d464f6f76e2005a - Sigstore transparency entry: 976562931
- Sigstore integration time:
-
Permalink:
cool-japan/torsh@077a056a4737b480add9223356dd9b63988ea96c -
Branch / Tag:
refs/heads/0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@077a056a4737b480add9223356dd9b63988ea96c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file rstorch-0.1.0a2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: rstorch-0.1.0a2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 862.3 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1b858c1c942c3b7e2dcbf612c350142561c850aaffddb1e1c0d53f37221c093
|
|
| MD5 |
0bcead0cc7305b3fc534df91f1367f4b
|
|
| BLAKE2b-256 |
665e781df1ae54ed2d63808567b74aeb62e5a6ba5ebc94089a110e92c53905a2
|
Provenance
The following attestation bundles were made for rstorch-0.1.0a2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
pypi-publish.yml on cool-japan/torsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rstorch-0.1.0a2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
c1b858c1c942c3b7e2dcbf612c350142561c850aaffddb1e1c0d53f37221c093 - Sigstore transparency entry: 976562939
- Sigstore integration time:
-
Permalink:
cool-japan/torsh@077a056a4737b480add9223356dd9b63988ea96c -
Branch / Tag:
refs/heads/0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@077a056a4737b480add9223356dd9b63988ea96c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file rstorch-0.1.0a2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: rstorch-0.1.0a2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 799.3 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e62892abe366e06326debe1dc03a0f16aed1f8a729687489edb76431a97a5f4f
|
|
| MD5 |
4e759399b22dcd2b66e0d94a75694a30
|
|
| BLAKE2b-256 |
301adc6f18f93ec237a0eb8d51631e32d5dce230b2c4b0674e321216a6ad93a9
|
Provenance
The following attestation bundles were made for rstorch-0.1.0a2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:
Publisher:
pypi-publish.yml on cool-japan/torsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rstorch-0.1.0a2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl -
Subject digest:
e62892abe366e06326debe1dc03a0f16aed1f8a729687489edb76431a97a5f4f - Sigstore transparency entry: 976562941
- Sigstore integration time:
-
Permalink:
cool-japan/torsh@077a056a4737b480add9223356dd9b63988ea96c -
Branch / Tag:
refs/heads/0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@077a056a4737b480add9223356dd9b63988ea96c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file rstorch-0.1.0a2-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: rstorch-0.1.0a2-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 809.9 kB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e62599ffc741d9cae4c4fe246c28f2a588b06444d48fe78bd5ef2e39f91296a4
|
|
| MD5 |
683e32afd7987fd1a60e44c841bfcf0e
|
|
| BLAKE2b-256 |
e5b392309dbbebb938eba6fb36664aa4e0943aeae3180108f0b6d1d4f62c5727
|
Provenance
The following attestation bundles were made for rstorch-0.1.0a2-cp38-abi3-macosx_11_0_arm64.whl:
Publisher:
pypi-publish.yml on cool-japan/torsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rstorch-0.1.0a2-cp38-abi3-macosx_11_0_arm64.whl -
Subject digest:
e62599ffc741d9cae4c4fe246c28f2a588b06444d48fe78bd5ef2e39f91296a4 - Sigstore transparency entry: 976562944
- Sigstore integration time:
-
Permalink:
cool-japan/torsh@077a056a4737b480add9223356dd9b63988ea96c -
Branch / Tag:
refs/heads/0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@077a056a4737b480add9223356dd9b63988ea96c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file rstorch-0.1.0a2-cp38-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: rstorch-0.1.0a2-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 870.3 kB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f977cccb30cfddf654537d13ad1ed16bc661c5395b70d62b89c788cd14d54ed
|
|
| MD5 |
41f4002552f3390d59112e59e86dbaae
|
|
| BLAKE2b-256 |
10aa9b0789482cb8b78f3351add2315fbcc47e5807d258f11d57230e4458202e
|
Provenance
The following attestation bundles were made for rstorch-0.1.0a2-cp38-abi3-macosx_10_12_x86_64.whl:
Publisher:
pypi-publish.yml on cool-japan/torsh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rstorch-0.1.0a2-cp38-abi3-macosx_10_12_x86_64.whl -
Subject digest:
1f977cccb30cfddf654537d13ad1ed16bc661c5395b70d62b89c788cd14d54ed - Sigstore transparency entry: 976562938
- Sigstore integration time:
-
Permalink:
cool-japan/torsh@077a056a4737b480add9223356dd9b63988ea96c -
Branch / Tag:
refs/heads/0.1.1 - Owner: https://github.com/cool-japan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@077a056a4737b480add9223356dd9b63988ea96c -
Trigger Event:
workflow_dispatch
-
Statement type: