# mit-han-lab/tinyengine architecture

> Architecture diagram of the GitHub repository mit-han-lab/tinyengine, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mit-han-lab/tinyengine
- Repository: https://github.com/mit-han-lab/tinyengine

## Overview

##### Code Generator Module (Python)
- Location: The “code_generator” directory.
- Purpose: This module takes TFLite or other neural network graphs as input and converts them into optimized low‐level C/C++ source code that is tailored for the TinyEngine runtime.
- Subcomponents: It includes converters, operator templates (found in codetemplate), allocators, and parser utilities.

##### TinyEngine Inference Library Module (C/C++ Runtime)
- Location: “TinyEngine” directory.
- Purpose: This is the runtime core that contains all the operator implementations (organized in subdirectories like kernels, both fp_ and int_ operators) and integrates third-party components (e.g., CMSIS code) to deploy the network on microcontrollers.
- Interaction: Show that the generated code from the Code Generator is integrated/linked with TinyEngine’s runtime kernels. Also indicate that TinyEngine implements the key inference optimizations to reduce peak memory, improve MAC/s, and handle patch-based scheduling.

##### Deployment/Examples and Tutorial Module
- Location: “examples” and “tutorial” directories.
- Purpose: These modules demonstrate how to use TinyEngine (and the generated C code) within an application. The tutorials include both inference demos and training demos. They also interface with hardware-specific SDKs (e.g., STM32CubeIDE) for microcontroller deployment.

##### Model Assets and External Resources
- Location: “assets” directory, along with references to online repositories and demo videos in the README.
- Purpose: This contains pre-trained models in TFLite format, configuration files, and other media (e.g., figures for demo purposes).

##### Platform & Toolchain

- Detail the internal modularity of TinyEngine by drawing sub-modules (like kernels that implement various forward and backward ops) and note that these implement specific optimizations (in-place depthwise, patch-based inference, etc.).
- “code_generator” part separately to highlight the co-design approach between algorithm development (via model conversion) and system-level memory/compute optimizations in TinyEngine.
- Since the project also supports training (as seen in the “tutorial/training” directory), you might optionally add a separate block labeled “On-Device Training (Tiny Training Engine)” that shares the same low-level optimizations; connect that with the runtime and demo examples.

1. Model Assets (TFLite/Pre-trained models)
2. Code Generator Module (Python library – code_generator) with its subcomponents (converters, allocators, template engine)
3. TinyEngine Runtime Library (C/C++ modules – kernels, operator implementations, memory scheduling optimizations) with submodules as needed
4. Example Applications and Tutorial Demos (deployment demos, training demos)
5. Microcontroller Deployment Platform (hardware toolchain, STM32CubeIDE integration)

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Model Assets"
        A["Model Assets (TFLite/Pre-trained models)"]
    end

    subgraph "Code Generator Module (Python)"
        B["Code Generator Module"]
        B1["Converters"]
        B2["Codetemplate"]
        B3["Allocators"]
        B4["Operators"]
    end

    C["Generated Optimized C/C++ Code"]

    subgraph "TinyEngine Runtime Library (C/C++)"
        D["TinyEngine Runtime Library"]
        D1["Operator Kernels"]
        D2["Memory Scheduling & Optimization"]
        D3["Third Party Integration (CMSIS)"]
        D4["Runtime Include Files"]
    end

    subgraph "Deployment & Tutorial Demos"
        E1["Example Applications"]
        E2["Tutorial Demos (Inference&Training)"]
    end

    subgraph "Microcontroller Deployment Platform"
        F1["Firmware Build (Inference)"]
        F2["Firmware Build (Training)"]
    end

    %% Connections
    A -->|"input"| B
    B -->|"generates"| C
    C -->|"integratedWith"| D
    D -->|"usedIn"| E1
    D -->|"usedIn"| E2
    E1 -->|"deployedOn"| F1
    E2 -->|"deployedOn"| F2

    %% Internal Links in Code Generator Module
    B --> B1
    B --> B2
    B --> B3
    B --> B4

    %% Internal Links in TinyEngine Runtime Library
    D --> D1
    D --> D2
    D --> D3
    D --> D4

    %% Click Events for Code Generator Module
    click B "https://github.com/mit-han-lab/tinyengine/tree/main/code_generator/"
    click B1 "https://github.com/mit-han-lab/tinyengine/tree/main/code_generator/converters"
    click B2 "https://github.com/mit-han-lab/tinyengine/tree/main/code_generator/codetemplate"
    click B3 "https://github.com/mit-han-lab/tinyengine/tree/main/code_generator/allocator"
    click B4 "https://github.com/mit-han-lab/tinyengine/tree/main/code_generator/operators"

    %% Click Events for TinyEngine Runtime Library
    click D "https://github.com/mit-han-lab/tinyengine/tree/main/TinyEngine/"
    click D1 "https://github.com/mit-han-lab/tinyengine/tree/main/TinyEngine/src"
    click D3 "https://github.com/mit-han-lab/tinyengine/tree/main/TinyEngine/third_party/CMSIS"
    click D4 "https://github.com/mit-han-lab/tinyengine/tree/main/TinyEngine/include"

    %% Click Events for Deployment & Tutorial Demos
    click E1 "https://github.com/mit-han-lab/tinyengine/tree/main/examples/"
    click E2 "https://github.com/mit-han-lab/tinyengine/tree/main/tutorial/"

    %% Click Events for Microcontroller Deployment Platform
    click F1 "https://github.com/mit-han-lab/tinyengine/tree/main/tutorial/inference/"
    click F2 "https://github.com/mit-han-lab/tinyengine/tree/main/tutorial/training/"

    %% Class Definitions for Colors
    classDef asset fill:#FFDD88,stroke:#000,stroke-width:1px;
    classDef generator fill:#AEDFF7,stroke:#000,stroke-width:1px;
    classDef code fill:#EDE7B1,stroke:#000,stroke-width:1px;
    classDef runtime fill:#C2F0C2,stroke:#000,stroke-width:1px;
    classDef deployment fill:#F7C2C2,stroke:#000,stroke-width:1px;
    classDef platform fill:#D8BFD8,stroke:#000,stroke-width:1px;

    class A asset;
    class B,B1,B2,B3,B4 generator;
    class C code;
    class D,D1,D2,D3,D4 runtime;
    class E1,E2 deployment;
    class F1,F2 platform;
```
