A deep learning framework built from scratch — reverse-mode autodiff, n-dimensional tensors, and a neural-network stack, with NumPy, Numba-parallel CPU, and CUDA backends.
MiniTorch is a minimal, PyTorch-style autodiff engine implemented end-to-end in
Python — no torch under the hood. It rebuilds the machinery a modern deep
learning library hides behind the abstractions: a computation graph that records
operations, reverse-mode automatic differentiation that walks it backward, and
tensor ops that run on three interchangeable backends. The same model code
trains on a pure-Python tensor, a Numba-JIT parallel CPU kernel, or a CUDA GPU
kernel by swapping a single backend object.
- Reverse-mode autodiff — a
Variable/Functiongraph withforward/backwardfor every op, topological-sort backprop, and gradient accumulation. Verified against numerical central-difference gradients. - N-dimensional tensors — strided storage, broadcasting, views, perm/ reshape, and zero-copy indexing, all differentiable.
- Three execution backends — the same operators compiled as:
pure Python, Numba
@njit(parallel=True)CPU kernels, and CUDA kernels with shared-memory tiled matrix multiply. - Neural-network stack — 1D/2D convolution, max/avg pooling, softmax & log-softmax (log-sum-exp stable), ReLU/sigmoid, dropout, and an SGD optimizer.
- Tested like a library — 200+ property-based tests (Hypothesis) covering operators, gradients, broadcasting, and every layer.
The codebase is layered, each module builds strictly on the one below it.
| Layer | Module | What it provides |
|---|---|---|
| Math core | operators.py |
Elementary scalar functions + functional map/zip/reduce |
| Scalar autodiff | scalar.py, autodiff.py |
Single-value variables, the backprop engine, chain rule |
| Tensors | tensor_data.py, tensor.py, tensor_ops.py |
Strided n-d arrays, broadcasting, differentiable tensor ops |
| Speed | fast_ops.py, fast_conv.py, cuda_ops.py |
Numba-parallel CPU and CUDA backends + fast matmul |
| Networks | nn.py, module.py, optim.py |
Conv, pooling, softmax, dropout, Module, SGD |
input → Tensor (records op on the graph) → forward → loss
│
loss.backward() ← topological sort ← chain rule ← stored contexts
Requires Python 3.12. Recommended using uv:
uv venv --python 3.12 && source .venv/bin/activate
uv pip install -r requirements.txtRun the test suite:
pytest tests/ # everything
pytest tests/ -m task2_3 # one task group (markers task0_1 … task4_4)CUDA kernels compile lazily; the GPU test groups (
task3_3,task3_4) are skipped automatically when no CUDA device is present.
The CUDA backend needs an NVIDIA GPU, so task3_3/task3_4 are skipped locally
and in CI. To exercise them, open a Colab GPU runtime (Runtime → Change
runtime type → T4 GPU) and run:
# install numba's CUDA backend, then confirm the GPU is visible
!pip install -q numba-cuda
from numba import cuda # note: `from numba import cuda`, not `numba.cuda`
print(cuda.is_available()) # -> True on a GPU runtime
# get the code + test deps
!git clone https://github.com/dantp-ai/minitorch.git
!pip install -q numpy hypothesis pytest pytest-env
# run ONLY the CUDA task groups
!cd minitorch && python -m pytest tests -m "task3_3 or task3_4" -vOn a T4 this runs the full CUDA suite — map/zip/reduce, the sum/matmul
practice kernels, and the shared-memory tiled matrix multiply (58 tests, ~3 min).
The project/ directory contains runnable demos that train models built on the
engine:
| Script | Task |
|---|---|
project/run_tensor.py |
Binary classification on toy datasets (xor, circle, spiral) |
project/run_fast_tensor.py |
Same, on the Numba-parallel backend |
project/run_mnist_multiclass.py |
MNIST digit classification with a small ConvNet |
project/run_sentiment.py |
Sentence sentiment with embeddings + 1D conv |
python project/run_tensor.pyBuilt on the excellent MiniTorch teaching framework by Sasha Rush (Cornell Tech). MiniTorch provides the scaffolding, tests, and project structure; the engine internals in this repository — autodiff, tensor ops, the Numba/CUDA kernels, convolutions, and the NN layers — are my own implementation.