# mit-han-lab/mcunet architecture

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

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

## Overview

##### Project Type & Purpose
- The project is focused on enabling deep learning on microcontrollers under tight memory constraints. It is a research and reference implementation that combines a neural architecture search / network design component (TinyNAS) with a lightweight inference engine (TinyEngine).
- The README makes clear that the framework is about “tiny deep learning” with components to build, evaluate, and deploy both PyTorch and TF-Lite models on low-memory devices.

##### Analyzing the File Structure
- At the top level, you see configuration files (e.g., .github, setup.py, requirements.txt) which support building, continuous integration, and deployment.
- The primary code resides in the “mcunet” directory. Within it, there are two key submodules:
– TinyNAS (under mcunet/tinynas): Contains directories like elastic_nn, elastic_nn/modules, elastic_nn/networks, nn (with its own modules/networks), and a separate TensorFlow codebase. This is where the model design/search and structural modules are implemented.
– TinyEngine (under mcunet/tinyengine): Dedicated for the inference engine which is used during deployment and evaluation of models on microcontrollers.
- The “model_zoo.py” in mcunet points to a registry or factory of available models, and the eval scripts (eval_det.py, eval_tflite.py, eval_torch.py) provide command-line drivers to test and benchmark the models.

##### Key Components
- MCUNet Core: Represent this as the central orchestrator which ties together the model construction, training or architecture search (TinyNAS), and deployment/inference (TinyEngine).
- TinyNAS Module: Show components such as “elastic_nn” and “nn” modules. Detail that these submodules contain dynamic layers, operations, and network architectures (like OFA Proxyless and MobileNet V2 variants) for automatic network design.
- TinyEngine Module: Emphasize its role in model inference – it is an efficient engine designed for microcontrollers that improves speed and reduces memory usage.
- Model Zoo: Include a component that aggregates model definitions and configurations (from “mcunet/model_zoo.py”), and ties into the build_model and download_tflite methods.
- Evaluation / Test Drivers: Highlight the presence of evaluation scripts for both PyTorch and TF-Lite, as well as the demo for detection (eval_det.py). These act as entry-points/scripts to trigger experiments.
- Utility Libraries: Represent the “mcunet/utils” component, which contains common helper functions, configuration utilities, and module definitions that support the functionality of TinyNAS and TinyEngine.

##### Interactions & Data Flow
- From a “User / Experiment” input (or CI/CD pipeline from .github/workflows) to the Evaluation Scripts.
- From Evaluation Scripts → Model Zoo: They call build_model() or download_tflite() to obtain network models.
- From Model Zoo → TinyNAS: For models that require architecture search, dynamic layer configuration, or network construction.
- Additionally, the Utility libraries support the above modules (TinyNAS and TinyEngine) with functions like batch normalization utilities, PyTorch module wrappers, etc.

## Diagram (Mermaid)

```mermaid
flowchart TD
    U["User/CI Pipeline"]:::user

    subgraph "Evaluation Drivers"
        ED["eval_det.py"]:::eval
        ET["eval_tflite.py"]:::eval
        ETW["eval_torch.py"]:::eval
    end

    MZ["Model Zoo"]:::model
    CC["MCUNet Core"]:::core

    subgraph "TinyNAS Module"
        TN["TinyNAS Module"]:::module
        EN["Elastic NN"]:::submod
        NN["NN Modules"]:::submod
        TN --> EN
        TN --> NN
    end

    TE["TinyEngine Module"]:::engine

    UL["Utility Libraries"]:::util

    U --> ED
    U --> ET
    U --> ETW

    ED --> MZ
    ET --> MZ
    ETW --> MZ

    MZ --> CC

    CC --> TN
    CC --> TE

    UL --> TN
    UL --> TE

    %% Click events
    click CC "https://github.com/mit-han-lab/mcunet/tree/master/mcunet"
    click TN "https://github.com/mit-han-lab/mcunet/tree/master/mcunet/tinynas"
    click EN "https://github.com/mit-han-lab/mcunet/tree/master/mcunet/tinynas/elastic_nn"
    click NN "https://github.com/mit-han-lab/mcunet/tree/master/mcunet/tinynas/nn"
    click TE "https://github.com/mit-han-lab/mcunet/tree/master/mcunet/tinyengine"
    click MZ "https://github.com/mit-han-lab/mcunet/blob/master/mcunet/model_zoo.py"
    click ED "https://github.com/mit-han-lab/mcunet/blob/master/eval_det.py"
    click ET "https://github.com/mit-han-lab/mcunet/blob/master/eval_tflite.py"
    click ETW "https://github.com/mit-han-lab/mcunet/blob/master/eval_torch.py"
    click UL "https://github.com/mit-han-lab/mcunet/tree/master/mcunet/utils"

    %% Styles
    classDef user fill:#ffcccc,stroke:#b30000,stroke-width:2px;
    classDef eval fill:#ffd700,stroke:#b38f00,stroke-width:2px;
    classDef model fill:#90ee90,stroke:#2e8b57,stroke-width:2px;
    classDef core fill:#add8e6,stroke:#00008b,stroke-width:2px;
    classDef module fill:#d3ffce,stroke:#2e8b57,stroke-width:2px;
    classDef submod fill:#bdecb6,stroke:#008000,stroke-width:2px;
    classDef engine fill:#e6e6fa,stroke:#6a5acd,stroke-width:2px;
    classDef util fill:#fff2cc,stroke:#daa520,stroke-width:2px;
```
