A PyTorch-compatible API with Candle backend
Project description
🕯️ Torch-Candle: Vectorized Deep Learning Core with Drop-In PyTorch Compatibility
Torch-Candle is a high-performance deep learning library combining the mathematical simplicity and drop-in interface of PyTorch with the blazing-fast, memory-efficient Candle Rust backend.
Engineered for production reliability, minimal memory footprints, and state-of-the-art academic training innovations.
🚀 Key Architectural Pillars
1. Drop-In PyTorch Compatibility
Replace PyTorch with a single line. Torch-Candle can dynamically register itself in Python's environment registry, translating all standard PyTorch model loads, functions, and operations to high-speed vectorized C++/Rust backends:
import torch_candle as torch
torch.enable_torch_compat()
# Future standard PyTorch imports automatically redirect!
import torch
x = torch.Tensor([1.0, 2.0, 3.0])
2. Self-Healing Autograd (SHA) Engine
Catastrophic gradient explosions (NaN/Inf) caused by numerical instability (like dividing by zero or exponential overflows) permanently corrupt weights in standard frameworks. SHA dynamically intercepts anomalies during the backward pass at an element level and reconstructs stable estimates using a dynamic Exponential Moving Average (EMA) of parameter gradient history:
$$g_{t} = \beta g_{t-1} + (1 - \beta) g_{curr}$$
3. Auto-Device Alignment Discovery
Bypass RuntimeError: Expected all tensors to be on the same device permanently. Arithmetic mutators, logical operators, and matrix multiplications automatically detect cross-device operands (e.g. CPU vs. CUDA) and align them to the primary execution device on-the-fly without crashing.
4. Zero-Allocation In-Place AdamW Optimizer
Eliminate unnecessary memory allocation overhead. Parameters, momentum vectors, and velocity states are mutated directly in-place, offering a significant speedup and minimal memory allocation peaks.
5. Dynamic Graph JIT Compiler (torch.compile)
Optimizes hot execution paths via lightweight tracing. Traces functional subgraphs, compiles vectorized execution pathways, and caches hot execution calls for near-instant subsequent executions.
6. Causal Attention (SDPA) with Contiguous Layouts
Includes highly optimized Multi-Head Attention and Scaled Dot-Product Attention with native hardware-accelerated memory contiguity alignments, perfect for Transformer and Large Language Model (LLM) fine-tuning pipelines.
7. Decoupled Local Analytical Solving (DLLT-AS)
A revolutionary zero-backpropagation training framework. Instead of slow iterative gradient descent (Adam/SGD) over hundreds of epochs, DLLT-AS solves layer weight matrices analytically in a single closed-form pass using Moore-Penrose Pseudo-Inverse (Ridge) projections: $$W_k = (X_k^T X_k + \lambda I)^{-1} X_k^T Y$$ Combined with Swish activation gating and Dense Representation Reuse (DRR), DLLT-AS trains a multi-layer deep network in a single mathematical step (under 22ms), achieving 98.00% accuracy on classification benchmarks with virtually zero computational and energy cost.
🛠️ Installation
Prerequisite: Rust Toolchain
Since Torch-Candle compiles native C++/Rust kernels during installation, ensure the Rust toolchain is installed:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
⚡ Installation using uv (Recommended — Ultra Fast)
Install the package instantly utilizing Astral's high-speed Rust-powered uv package manager:
# Install in active virtual environment
uv pip install torch-candle
# Or add as a dependency in a uv-managed project
uv add torch-candle
🐍 Standard Installation using pip
pip install torch-candle
🛠️ Local Development Build
To compile and install the extension locally for development:
# Build and link editable module using maturin + uv under the hood
maturin develop
# Or build via uv directly
uv pip install -e .
💡 Quickstart Example: LoRA Model Fine-Tuning
import torch_candle as torch
import torch_candle.nn as nn
import torch_candle.optim as optim
import torch_candle.nn.functional as F
# 1. Initialize a model
model = nn.Linear(128, 64)
# 2. Setup training criteria and zero-allocation optimizer
optimizer = optim.AdamW(model.parameters(), lr=1e-3)
# 3. Fine-tuning step with Auto-Device Alignment active
x = torch.Tensor([[1.0] * 128], device="cpu")
target = torch.Tensor([[0.0] * 64], device="cuda" if torch.cuda.is_available() else "cpu")
optimizer.zero_grad()
output = model(x)
loss = F.mse_loss(output, target)
loss.backward()
optimizer.step()
print(f"Fine-tuned Step Loss: {loss.item():.4f}")
Zero-Backpropagation Analytical Learning (DLLT-AS)
import torch_candle as torch
import torch_candle.nn as nn
# 1. Initialize input features and targets
x = torch.Tensor([[1.2, -0.5, 0.8], [0.5, 1.1, -1.2], [-0.3, 0.4, 0.9]])
target = torch.Tensor([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) # One-hot
# 2. Instantiate our zero-backprop DLLT-AS Model
# in_features=3, hidden_dim=16, out_classes=2
model = nn.DLLTASModel(in_features=3, hidden_dim=16, out_classes=2)
# 3. Train all deep decoupled layers analytically in a single mathematical step!
# Completes in under 22ms on standard CPU!
model.fit(x, target)
# 4. Predict instantly with solved weights
predictions = model(x)
print(f"Solved Predictions Output:\n{predictions.numpy()}")
🧪 Visual Verification Suites
Torch-Candle includes two dedicated CLI scripts to verify your hardware configuration and test training resilience:
- Hardware Diagnostics & E2E LoRA SFT Pipeline:
python3 tests/diagnose_hardware.py - Self-Healing Autograd Comparative Test:
python3 tests/test_self_healing_demo.py
🔧 Memory Allocation Tuning (Linux)
To prevent glibc memory arena fragmentation under high concurrency, Torch-Candle automatically sets MALLOC_MMAP_THRESHOLD_=65536 on import, which forces glibc to use mmap instead of heap arenas for allocations above 64KB. This eliminates OOM fragmentation without requiring root privileges.
If launching from a shell script, you can also set this before the process boots:
# Force glibc to use mmap for allocations ≥ 64KB (prevents arena fragmentation)
export MALLOC_MMAP_THRESHOLD_=65536
python train.py
Note: Do not use
sysctlor modify/etc/sysctl.conffor memory tuning — this requires root privileges and targets the wrong kernel parameter.
📄 License
Licensed under the MIT License.
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 Distribution
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 torch_candle-2026.6.4.tar.gz.
File metadata
- Download URL: torch_candle-2026.6.4.tar.gz
- Upload date:
- Size: 127.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa6a879664ce95b8feb6892daaafc13200b1ea12da1d5364bd432401b2c88f4d
|
|
| MD5 |
70cd359591296f71fdbabb671be4e67a
|
|
| BLAKE2b-256 |
f5f6d8da8ccc7ae51a4949cd2052c5742ca1d3f35a5eb190b7b97960693efbb5
|
File details
Details for the file torch_candle-2026.6.4-cp310-cp310-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: torch_candle-2026.6.4-cp310-cp310-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 1.6 MB
- Tags: CPython 3.10, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17182653816b3af3584162dd1affa6a76fdebaf4ecffed0c2d674a94b7484d86
|
|
| MD5 |
3f5d9223e8856f5aad7c64243bebf1d6
|
|
| BLAKE2b-256 |
5c0a654e9ac2fd9861fefaecc0af9a7f50d3eafc46749743bb8b8a2d4beb86af
|