# a1henu/tinytorch architecture

> Architecture diagram of the GitHub repository a1henu/tinytorch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/a1henu/tinytorch
- Repository: https://github.com/a1henu/tinytorch

## Overview

##### Project type and purpose
- This is a tiny deep-learning framework implemented in C++ with Python bindings (“tinytorch”).
- It provides core tensor computations (CPU/GPU), layers (conv, pooling, FC, loss), and automatic differentiation, exposed to Python via pybind11.
- It includes examples for CIFAR-10, MNIST, ImageNet; unit tests in C++ (GoogleTest) and Python (pytest); build and CI scripts.

##### File structure
- Top-level directories
- csrc/ – C++ source tree
· core/ (device management, memory, low-level kernels)
· tensor/ (tensor class, operators)
· layers/ (layer implementations, bindings)
· error/ (error definitions)
· CMakeLists.txt, macros.h
- tinytorch/ – Python package
· autodiff.py, tensorbase.py, tensor.py
· data/ (datasets, data loaders)
· funcs/ (functional API, auto-diff)
· nn/ (Module classes, activations, loss, optimizers)
· node.py, operators.py, optim/
- examples/ – sample training scripts
- tests/ – pytest suites
- scripts/ – build/test/clean helpers
- third_party/ – googletest, pybind11
- CI configs (.github/workflows)
- Patterns
- Layered design: core C++ engine → pybind11 binding → Python API
- CPU/GPU abstraction: in core/kernels and core/device
- Modular structure: separate folders per concern
- Build/test orchestration via CMake + shell scripts + GitHub Actions

##### README for additional insights
- Confirms use of OpenBLAS, optional CUDA
- Installation steps for C++ build (CMake), Python packaging (pip install)
- Testing workflows (GoogleTest, pytest)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% C++ Core Library
    subgraph "C++ Core Library"
        direction TB
        CMgr["Device Manager"]:::cpp
        MMgr["Memory Manager"]:::cpp
        Kernels["CPU/GPU Kernels"]:::cpp
        TensorImpl["Tensor Implementation"]:::cpp
        TensorOps["Tensor Operators"]:::cpp
        LayersCPP["Layer Implementations"]:::cpp
        ErrorDef["Error Definitions"]:::cpp
    end

    %% Pybind11 Binding Layer
    subgraph "Pybind11 Binding Layer"
        direction TB
        TBinds["Tensor Bindings"]:::cpp
        FBinds["Functional/Layers Bindings"]:::cpp
    end

    %% Python Package
    subgraph "Python Package (tinytorch)"
        direction TB
        API["API Core\n(autodiff.py, tensorbase.py, tensor.py)"]:::py
        Data["Data Loaders & Datasets"]:::py
        FuncAPI["Functional API & Autograd"]:::py
        NNAPI["Neural Network API"]:::py
        Optim["Optimization Algorithms"]:::py
    end

    %% Examples
    subgraph "Examples & Client Code"
        ExMap["Examples Scripts"]:::py
    end

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        OBLAS["OpenBLAS"]:::ext
        CUDA["CUDA/cuBLAS (optional)"]:::ext
        PYB["pybind11"]:::ext
        GTEST["GoogleTest"]:::ext
        PYTEST["pytest"]:::ext
    end

    %% Build & CI System
    subgraph "Build & CI System"
        direction TB
        BuildSys["CMake Build Rules"]:::ci
        Scripts["Shell Scripts"]:::ci
        CIWF["GitHub Actions Workflows"]:::ci
    end

    %% Relationships
    API --> TBinds
    API --> FBinds
    TBinds --> CMgr
    TBinds --> TensorImpl
    FBinds --> LayersCPP
    CMgr --> Kernels
    CMgr --> MMgr
    TensorImpl --> TensorOps
    LayersCPP --> TensorOps
    Kernels --> OBLAS
    Kernels --> CUDA
    TBinds --> PYB
    ExMap --> API
    BuildSys --> CMgr
    BuildSys --> API
    Scripts --> BuildSys
    CIWF --> GTEST
    CIWF --> PYTEST

    %% Click Events
    click CMgr "https://github.com/a1henu/tinytorch/blob/main/csrc/core/device/device.cpp"
    click MMgr "https://github.com/a1henu/tinytorch/blob/main/csrc/core/memory/memory.cpp"
    click Kernels "https://github.com/a1henu/tinytorch/tree/main/csrc/core/kernels"
    click TensorImpl "https://github.com/a1henu/tinytorch/blob/main/csrc/tensor/tensor.cpp"
    click TensorOps "https://github.com/a1henu/tinytorch/blob/main/csrc/tensor/operators/tensor_activation.cpp"
    click LayersCPP "https://github.com/a1henu/tinytorch/blob/main/csrc/layers/conv2d_layer.cpp"
    click ErrorDef "https://github.com/a1henu/tinytorch/blob/main/csrc/error/error.h"
    click TBinds "https://github.com/a1henu/tinytorch/blob/main/csrc/tensor/tensor_binding.cpp"
    click FBinds "https://github.com/a1henu/tinytorch/blob/main/csrc/layers/funcs_binding.cpp"
    click API "https://github.com/a1henu/tinytorch/tree/main/tinytorch/"
    click Data "https://github.com/a1henu/tinytorch/tree/main/tinytorch/data"
    click FuncAPI "https://github.com/a1henu/tinytorch/tree/main/tinytorch/funcs"
    click NNAPI "https://github.com/a1henu/tinytorch/tree/main/tinytorch/nn"
    click Optim "https://github.com/a1henu/tinytorch/tree/main/tinytorch/optim"
    click ExMap "https://github.com/a1henu/tinytorch/tree/main/examples/"
    click BuildSys "https://github.com/a1henu/tinytorch/blob/main/CMakeLists.txt"
    click Scripts "https://github.com/a1henu/tinytorch/tree/main/scripts/"
    click CIWF "https://github.com/a1henu/tinytorch/tree/main/.github/workflows/"

    %% Styles
    classDef cpp fill:#cfe2ff,stroke:#000,stroke-width:1px
    classDef py fill:#d1e7dd,stroke:#000,stroke-width:1px
    classDef ext fill:#e2e3e5,stroke:#000,stroke-width:1px
    classDef ci fill:#ffe5b4,stroke:#000,stroke-width:1px
```
