Mini Deep Learning framework similar to PyTorch
Project description
Tiny-PyTorch 🧠
Unravel the magic of modern deep learning by building a PyTorch-like framework from the ground up.
Tiny-PyTorch is an educational deep learning framework built entirely in Python. It demystifies the core machinery of libraries like PyTorch by providing a clean, focused, and from-scratch implementation of the essential components.
Philosophy: Understanding by Building
The best way to truly understand how complex systems work is to build them yourself. Tiny-PyTorch is born from this philosophy. While production frameworks like PyTorch and TensorFlow provide powerful, high-level abstractions, their internal complexity can be a barrier to learning.
This project strips away those abstractions, allowing you to:
- See the Core Logic: Grasp the fundamental algorithms and data structures that power deep learning, from the
Tensorobject to the backpropagation process. - Connect Theory to Code: Bridge the gap between the mathematical concepts of deep learning and their concrete implementation.
- Become a Better Practitioner: Use high-level frameworks more effectively by understanding their internal mechanics, performance trade-offs, and potential pitfalls.
✨ Core Features
- Dynamic Computation Graph: Tensors track their history, allowing for flexible model architectures.
- Reverse-Mode Automatic Differentiation: An efficient gradient calculation engine (
autograd) built from scratch. - Extensible
nn.ModuleSystem: A familiar API for building complex neural network layers and models. - Standard Optimizers: Implementations of
SGDandAdamto handle parameter updates. - Hardware Acceleration: A pluggable backend system supporting
NumPy, customCPU(C++), andCUDA(GPU) operations. - Data Loading Utilities:
DatasetandDataLoaderclasses for efficient data pipelines.
🏗️ Project Architecture
The framework is built in a bottom-up fashion, where each layer of abstraction relies on the one below it. This mirrors the logical structure of major deep learning libraries.
graph TD
subgraph "High-Level API"
C[nn.Module] --> D[Optimizers]
B[Tensors & Autograd] --> C
end
subgraph "Low-Level Engine"
A[NDArray] --> B
subgraph "Backends"
direction LR
np[NumPy]
cpu[CPU]
gpu[CUDA]
end
Backend --> A
end
style C fill:#f9f,stroke:#333,stroke-width:2px
style B fill:#ccf,stroke:#333,stroke-width:2px
style A fill:#cfc,stroke:#333,stroke-width:2px
- Backends (NumPy, CPU, CUDA): Perform the actual mathematical computations on flat arrays of data.
- NDArray: A generic, strided N-dimensional array class that provides a unified interface over different backends.
- Tensor & Autograd: The heart of the framework. A
Tensorwraps anNDArrayand builds a dynamic computation graph. Theautogradengine traverses this graph to perform reverse-mode automatic differentiation. - High-Level API (
nn,optimizer): Provides the familiar modules, layers, and optimization algorithms for building and training neural networks.
🚀 Quick Start
First, clone the repository and install it in editable mode.
git clone https://github.com/your-username/tiny-pytorch.git
cd tiny-pytorch
pip install -e .
Here's a simple example of defining a model and running a forward/backward pass.
import tiny_pytorch as tp
import tiny_pytorch.nn as nn
# 1. Define a simple model
class SimpleNet(nn.Module):
def __init__(self, in_features, out_features):
self.fc1 = nn.Linear(in_features, 64)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(64, out_features)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
return self.fc2(x)
# 2. Initialize model, optimizer, and loss function
model = SimpleNet(in_features=10, out_features=1)
optimizer = tp.optim.Adam(model.parameters(), lr=0.001)
loss_fn = nn.MSELoss()
# 3. Create dummy data
x_train = tp.randn(32, 10, requires_grad=True)
y_true = tp.randn(32, 1)
# 4. Perform a single training step
optimizer.zero_grad() # Reset gradients
y_pred = model(x_train) # Forward pass
loss = loss_fn(y_pred, y_true) # Compute loss
loss.backward() # Backward pass (autograd)
optimizer.step() # Update weights
print(f"Loss: {loss.item():.4f}")
🗺️ Roadmap
The project is developed in two main phases. Our current progress is tracked below.
- Phase I: Core Framework (NumPy Backend)
-
Tensor: The main multi-dimensional array with autograd support. -
Op: The base class for all tensor operations. -
Automatic Differentiation: Reverse-mode autograd engine. -
init: Parameter initialization functions (kaiming,xavier, etc.). -
nn: Core neural network layers (Linear,ReLU,BatchNorm,Conv2d). -
optimizer:SGDandAdamoptimizers. -
data:DatasetandDataLoaderfor data handling.
-
- Phase II: Hardware Acceleration & Advanced Models
-
NDArray: Generic, strided N-dimensional array. - NumPy Backend
- CPU Backend (C++)
- CUDA Backend (GPU)
- Advanced CNN operations (e.g.,
padding,dilation). - ResNet implementation.
- RNN and LSTM layers.
- A simple Language Model.
-
📚 Documentation
The official documentation, including detailed API references and tutorials, is hosted at: https://imaddabbura.github.io/tiny-pytorch/
⚠️ Limitations
As an educational project, Tiny-PyTorch has some intentional simplifications:
- Explicit Broadcasting: Broadcasting for element-wise operations must be done manually if tensor shapes do not match.
- Single Data Type:
NDArrayonly supports thefloat32dtype. - Contiguous Memory: Operations on the underlying 1D array require a call to
compact()to ensure the data is in a contiguous memory block. - Limited Reductions: Reduction operations (e.g.,
sum,max) can only be performed on a single axis or all axes at once.
License
Tiny-PyTorch is licensed under the Apache License 2.0. See the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
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 tiny_pytorch-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: tiny_pytorch-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 2.4 MB
- Tags: CPython 3.13, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a7f488452f8fedb869f0dea7fd94bc4d4107e611ab85c604037921a45630f1b
|
|
| MD5 |
3096c08c990fa5ba27bef8c60ed2d351
|
|
| BLAKE2b-256 |
3ab9f8e9c542a27dd46cabe1f2de6161a32daa66f56ac6ac722d4abf6769d258
|
Provenance
The following attestation bundles were made for tiny_pytorch-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl:
Publisher:
publish-pypi.yml on ImadDabbura/tiny-pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tiny_pytorch-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl -
Subject digest:
2a7f488452f8fedb869f0dea7fd94bc4d4107e611ab85c604037921a45630f1b - Sigstore transparency entry: 326365220
- Sigstore integration time:
-
Permalink:
ImadDabbura/tiny-pytorch@56f73407cd85cc7019ff7684e1f0059414517145 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ImadDabbura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@56f73407cd85cc7019ff7684e1f0059414517145 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tiny_pytorch-0.1.0-cp313-cp313-musllinux_1_2_i686.whl.
File metadata
- Download URL: tiny_pytorch-0.1.0-cp313-cp313-musllinux_1_2_i686.whl
- Upload date:
- Size: 2.4 MB
- Tags: CPython 3.13, musllinux: musl 1.2+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e35b242d4bccf06285b816d6b354891db8832c8af5589fe9a49bb874d03663f
|
|
| MD5 |
fb5bcaa4726be56e26595a2737d38da6
|
|
| BLAKE2b-256 |
b801597ca770990c1b53294f056691e26b0b9fb8f8049b9620ed7194007f013f
|
Provenance
The following attestation bundles were made for tiny_pytorch-0.1.0-cp313-cp313-musllinux_1_2_i686.whl:
Publisher:
publish-pypi.yml on ImadDabbura/tiny-pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tiny_pytorch-0.1.0-cp313-cp313-musllinux_1_2_i686.whl -
Subject digest:
9e35b242d4bccf06285b816d6b354891db8832c8af5589fe9a49bb874d03663f - Sigstore transparency entry: 326365091
- Sigstore integration time:
-
Permalink:
ImadDabbura/tiny-pytorch@56f73407cd85cc7019ff7684e1f0059414517145 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ImadDabbura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@56f73407cd85cc7019ff7684e1f0059414517145 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tiny_pytorch-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: tiny_pytorch-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45fc95c185e06e2e4feae7dbc3e070b582183ce0835e0b4285d0b925d1e426be
|
|
| MD5 |
020abd6528890b719d4727067d8666a3
|
|
| BLAKE2b-256 |
9c23557f1af4321e7af88342ecc53853af057c93a5c93e0128d653ac23c32d2f
|
Provenance
The following attestation bundles were made for tiny_pytorch-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
publish-pypi.yml on ImadDabbura/tiny-pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tiny_pytorch-0.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl -
Subject digest:
45fc95c185e06e2e4feae7dbc3e070b582183ce0835e0b4285d0b925d1e426be - Sigstore transparency entry: 326365051
- Sigstore integration time:
-
Permalink:
ImadDabbura/tiny-pytorch@56f73407cd85cc7019ff7684e1f0059414517145 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ImadDabbura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@56f73407cd85cc7019ff7684e1f0059414517145 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tiny_pytorch-0.1.0-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: tiny_pytorch-0.1.0-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.13, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce0258bd5b4d669e62a57c3c032c6c23cfec5b764781deb70c2f901947570c94
|
|
| MD5 |
b998993fe89aacc55c4f665c596dc67d
|
|
| BLAKE2b-256 |
a0dd7cd64d8c971c45adef911c9c12953a5d4340493956adaa726a11539da2c2
|
Provenance
The following attestation bundles were made for tiny_pytorch-0.1.0-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl:
Publisher:
publish-pypi.yml on ImadDabbura/tiny-pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tiny_pytorch-0.1.0-cp313-cp313-manylinux2014_i686.manylinux_2_17_i686.whl -
Subject digest:
ce0258bd5b4d669e62a57c3c032c6c23cfec5b764781deb70c2f901947570c94 - Sigstore transparency entry: 326365184
- Sigstore integration time:
-
Permalink:
ImadDabbura/tiny-pytorch@56f73407cd85cc7019ff7684e1f0059414517145 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ImadDabbura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@56f73407cd85cc7019ff7684e1f0059414517145 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tiny_pytorch-0.1.0-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: tiny_pytorch-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 176.9 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
487e7c474547d857518a394e7b6e04774be66c045095f0cefa29688f30f709c3
|
|
| MD5 |
57ca7959b5ebe4aab4fd59965c19a057
|
|
| BLAKE2b-256 |
67592c7be5e7dc68f5a631f29bb9c1e3ee1fa396a40944a2ded2217c68c68bca
|
Provenance
The following attestation bundles were made for tiny_pytorch-0.1.0-cp313-cp313-macosx_11_0_arm64.whl:
Publisher:
publish-pypi.yml on ImadDabbura/tiny-pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tiny_pytorch-0.1.0-cp313-cp313-macosx_11_0_arm64.whl -
Subject digest:
487e7c474547d857518a394e7b6e04774be66c045095f0cefa29688f30f709c3 - Sigstore transparency entry: 326365138
- Sigstore integration time:
-
Permalink:
ImadDabbura/tiny-pytorch@56f73407cd85cc7019ff7684e1f0059414517145 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ImadDabbura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@56f73407cd85cc7019ff7684e1f0059414517145 -
Trigger Event:
push
-
Statement type: