tch-rs  by LaurentMazare

Rust bindings for PyTorch C++ API (libtorch)

Created 6 years ago
5,013 stars

Top 9.9% on SourcePulse

GitHubView on GitHub
Project Summary

This Rust crate provides low-level bindings to the C++ PyTorch (libtorch) API, enabling Rust developers to leverage PyTorch's deep learning capabilities. It aims to mirror the C++ API closely, serving as a foundation for higher-level Rust abstractions.

How It Works

The tch-rs crate acts as a thin wrapper around libtorch, the C++ backend of PyTorch. It utilizes code generation derived from ocaml-torch to create bindings for the C API of libtorch. This approach ensures a direct mapping to PyTorch's core functionalities, allowing for efficient tensor operations, automatic differentiation, and neural network construction within a Rust environment.

Quick Start & Requirements

  • Installation: Requires libtorch version v2.7.0. Can be linked to a system-wide installation, a Python PyTorch installation (LIBTORCH_USE_PYTORCH=1), or downloaded via the download-libtorch feature. CUDA support requires TORCH_CUDA_VERSION to be set (e.g., cu117).
  • Manual Libtorch: Set LIBTORCH environment variable to the libtorch directory.
  • Static Linking: Use LIBTORCH_STATIC=1 and compile libtorch manually.
  • Windows: Recommended to use MSVC Rust toolchain due to PyTorch compatibility.
  • Docs: docs.rs

Highlighted Details

  • Provides tensor operations, neural network modules (nn), optimizers, and utilities for vision tasks (e.g., MNIST, ResNet).
  • Supports loading pre-trained models, including those exported via safetensors.
  • Includes examples for gradient descent, neural network training, and inference with pre-trained models.

Maintenance & Community

  • Active development with examples covering various deep learning tasks.
  • Community discussions may occur on GitHub issues.

Licensing & Compatibility

  • Dual-licensed under MIT and Apache 2.0. Permissive for commercial use and integration with closed-source projects.

Limitations & Caveats

  • Compilation can be slow due to potential torch-sys rebuilds; environment variables like LIBTORCH need to be correctly configured for IDEs like rust-analyzer.
  • Windows users should be mindful of MSVC vs. MinGW compatibility issues with PyTorch.
  • Error handling for shared library loading requires careful configuration of LD_LIBRARY_PATH or DYLD_LIBRARY_PATH.
Health Check
Last Commit

1 month ago

Responsiveness

1 day

Pull Requests (30d)
0
Issues (30d)
6
Star History
64 stars in the last 30 days

Explore Similar Projects

Starred by Andrej Karpathy Andrej Karpathy(Founder of Eureka Labs; Formerly at Tesla, OpenAI; Author of CS 231n), Edward Sun Edward Sun(Research Scientist at Meta Superintelligence Lab), and
5 more.

attorch by BobMcDear

0.2%
576
PyTorch nn module subset, implemented in Python using Triton
Created 2 years ago
Updated 1 month ago
Starred by Shizhe Diao Shizhe Diao(Author of LMFlow; Research Scientist at NVIDIA), Evan Hubinger Evan Hubinger(Head of Alignment Stress-Testing at Anthropic), and
2 more.

awesome-deeplearning-resources by endymecy

0%
3k
Deep learning research paper and code repository
Created 8 years ago
Updated 1 week ago
Feedback? Help us improve.