Skip to content

sudonitj/nn-Scratch

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 nn-from-Scratch

PyTorch-from-Scratch is a minimal deep learning framework built from the ground up to demystify the internals of modern machine learning libraries like PyTorch and TensorFlow.
It serves as a learning tool, a research sandbox, and a hackable base for experiments.


πŸš€ Project Vision

  • Educational First: Learn how tensor operations, autograd, and neural networks work under the hood
  • Hackable and Modular: Easily extend core functionality in Python or C++
  • Lightweight by Design: Minimal dependencies, designed to run on CPU with optional CUDA support

🧱 Core Components

1. Tensor System

Multi-dimensional array library with core tensor operations and shape handling.

  • Memory management with support for CPU (GPU support via CUDA planned)
  • Math ops: add, mul, matmul, etc.
  • Broadcasting and shape inference

2. Autograd Engine

Dynamic computation graph with automatic differentiation.

  • Forward graph construction on-the-fly
  • Reverse-mode autodiff (.backward())
  • Custom backward functions supported

3. Python API

Python-first design that mimics PyTorch's style for user familiarity.

  • Operator overloading (e.g., a + b)
  • Interoperable with NumPy
  • Clean class-based architecture

4. Neural Network Module

Composable building blocks for model construction.

  • Layers like Linear, Conv2D, ReLU
  • Parameter tracking and weight initialization
  • Module nesting and hooks

5. Optimization System

Standard training optimizers for parameter updates.

  • Support for SGD, Adam, etc.
  • Learning rate scheduling
  • Momentum and weight decay

πŸ”§ Architecture Flow

x = Tensor(...)  
y = model(x)  
loss = y.sum()  
loss.backward()  

Under the hood:

  • Python objects map to efficient C++ Tensor structures
  • Operations build a computation graph dynamically
  • Gradients flow back automatically through the graph
  • Optimizer steps update weights

βš™οΈ Technical Architecture

Project Layers

[ Python API ]
     ↓
[ pybind11 Bindings ]
     ↓
[ C++ Core Library ]

Build Flow

  • Core C++: libpytorch_core.so (tensor ops, autograd)
  • Python Bindings: via pybind11
  • Python Package: high-level modules (nn, optim, etc.)

πŸ“¦ Project Structure

nn-from-scratch/
β”œβ”€β”€ cmake/                     # CMake configuration files
β”œβ”€β”€ include/                   # Public C++ headers
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ tensor/           # Tensor core implementation
β”‚   β”‚   β”‚   β”œβ”€β”€ tensor.h
β”‚   β”‚   β”‚   β”œβ”€β”€ tensor_impl.h
β”‚   β”‚   β”‚   └── tensor_ops.h
β”‚   β”‚   β”œβ”€β”€ autograd/         # Autograd components
β”‚   β”‚   β”‚   β”œβ”€β”€ function.h
β”‚   β”‚   β”‚   β”œβ”€β”€ engine.h
β”‚   β”‚   β”‚   └── variable.h
β”‚   β”‚   β”œβ”€β”€ nn/               # Neural network components
β”‚   β”‚   β”‚   β”œβ”€β”€ modules.h
β”‚   β”‚   β”‚   └── functional.h
β”‚   β”‚   └── utils/            # Utilities
β”‚   β”‚       β”œβ”€β”€ logging.h
β”‚   β”‚       └── serializer.h
β”œβ”€β”€ src/                       # C++ implementation
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ tensor/
β”‚   β”‚   β”‚   β”œβ”€β”€ tensor.cpp
β”‚   β”‚   β”‚   └── tensor_ops.cpp
β”‚   β”‚   β”œβ”€β”€ autograd/
β”‚   β”‚   β”‚   β”œβ”€β”€ engine.cpp
β”‚   β”‚   β”‚   └── function.cpp
β”‚   β”‚   └── nn/
β”‚   β”‚       └── modules.cpp
β”‚   └── python/               # Python binding sources
β”‚       β”œβ”€β”€ module.cpp
β”‚       └── tensor.cpp
β”œβ”€β”€ python/                    # Python package
β”‚   β”œβ”€β”€ torchscratch/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ tensor.py         # Python tensor class
β”‚   β”‚   β”œβ”€β”€ nn/               # Neural network modules
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   β”œβ”€β”€ linear.py
β”‚   β”‚   β”‚   └── functional.py
β”‚   β”‚   β”œβ”€β”€ optim/            # Optimizers
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   └── sgd.py
β”‚   β”‚   β”œβ”€β”€ utils/            # Python utilities
β”‚   β”‚   β”‚   β”œβ”€β”€ data.py
β”‚   β”‚   β”‚   └── logger.py
β”‚   β”‚   └── csrc/             # Compiled extensions
β”œβ”€β”€ test/                      # Comprehensive tests
β”‚   β”œβ”€β”€ cpp/                  # C++ tests
β”‚   β”‚   β”œβ”€β”€ tensor/
β”‚   β”‚   β”‚   └── test_tensor.cpp
β”‚   β”‚   └── autograd/
β”‚   β”‚       └── test_autograd.cpp
β”‚   └── python/               # Python tests
β”‚       β”œβ”€β”€ test_tensor.py
β”‚       └── test_nn.py
β”œβ”€β”€ third_party/              # External dependencies
β”‚   β”œβ”€β”€ pybind11/             # Python binding library
β”‚   └── googletest/           # Google Test framework
β”œβ”€β”€ examples/                 # Example projects
β”‚   β”œβ”€β”€ mnist/
β”‚   └── cifar10/
β”œβ”€β”€ docs/                     # Documentation
β”‚   β”œβ”€β”€ source/
β”‚   β”‚   β”œβ”€β”€ getting_started.rst
β”‚   β”‚   └── api_reference.rst
β”‚   └── Makefile
β”œβ”€β”€ scripts/                  # Maintenance scripts
β”‚   β”œβ”€β”€ build.sh
β”‚   β”œβ”€β”€ format_code.sh
β”‚   └── run_tests.sh
β”œβ”€β”€ .github/
β”‚   └── workflows/           # CI/CD pipelines
β”‚       β”œβ”€β”€ build.yml
β”‚       └── test.yml
β”œβ”€β”€ CMakeLists.txt           # Root CMake configuration
β”œβ”€β”€ setup.py                 # Python package installation
β”œβ”€β”€ pyproject.toml          # Modern Python packaging config
β”œβ”€β”€ LICENSE
β”œβ”€β”€ CONTRIBUTING.md
β”œβ”€β”€ README.md
└── .gitignore

🚰 Key Challenges & Goals

Area Challenge Solution Goal
Memory Tensor allocation & reuse Smart allocator & GPU memory hooks (CUDA)
Performance Python-C++ overhead Minimize bindings + vectorized kernels
Autograd In-place ops, non-diff ops Robust backward graph & error handling
API Design PyTorch-like UX vs complexity Intuitive syntax + clean abstraction layers

🌐 Future Plans

  • βœ… CPU-first stable backend
  • πŸ› οΈ CUDA integration (GPU acceleration)
  • πŸ”„ ONNX export & model serialization
  • 🌍 Distributed training (multi-GPU, autograd support)
  • πŸ“¦ Plugin system for custom ops/layers/optimizers

πŸ“š Why This Project Matters

Role Value
Learners Understand how PyTorch works under the hood
Researchers Safely prototype new models, backprop ideas, and layers
Developers Contribute to a clean, modular, and understandable framework

βœ… What Success Looks Like

Users can:

  • Define custom neural networks
  • Train with .backward() + optimizers
  • Extend components in both C++ and Python

The codebase stays:

  • πŸ“¦ Lightweight – minimal setup
  • 🧺 Testable – isolated units + integration tests
  • πŸ’‘ Hackable – core logic easy to follow and modify

🀝 Contributing

Pull requests are welcome! We aim for clean code, thoughtful abstractions, and a welcoming space for learning and experimentation.


πŸ“„ License

MIT License – Free to use, learn from, and build on.

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C++ 68.7%
  • Python 14.5%
  • Shell 10.8%
  • CMake 6.0%