# marcelpetrick/esp32-tiny-llm architecture

> Architecture diagram of the GitHub repository marcelpetrick/esp32-tiny-llm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/marcelpetrick/esp32-tiny-llm
- Repository: https://github.com/marcelpetrick/esp32-tiny-llm

## Overview

ESP32-Tiny-LLM trains a greenhouse-focused decoder model, exports it for a portable C inference runtime, then serves conversations through a desktop CLI, web simulator, or ESP32 firmware. The runtime builds prompts, generates replies, retrieves device facts, and parses and validates proposed actions before simulated device state changes. Training also includes rule-based dialogue generation, optional local-LLM paraphrases, tokenizer and model training, export, and evaluation. Some runtime and training internals are unsampled; their links below rely on the README’s documented workflow and capabilities.

Main entry points: [`runtime/src/console.c`](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/console.c) (Prompt and action console), [`training/data/dialogue.py`](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/data/dialogue.py) (Dialogue generation), [`runtime/src/transformer.c`](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/transformer.c) (Transformer inference), [`training/train.py`](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/train.py) (Training loop)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_training["Model training"]
  node_dialogue["Dialogue generation<br/>[dialogue.py]"]
  node_teacher["LLM paraphrase teacher<br/>[teacher.py]"]
  node_dataset["Dataset builder<br/>[dataset.py]"]
  node_tokenizer["Tokenizer training<br/>[bpe.py]"]
  node_model["Transformer training<br/>[model.py]"]
  node_trainer["Training loop<br/>[train.py]"]
  node_export["Model export<br/>[export.py]"]
  node_evaluation["Model evaluation<br/>[eval.py]"]
end

subgraph group_domain["Greenhouse domain"]
  node_world["Device rules<br/>[actions.py]"]
  node_diagnosis["Fault diagnosis<br/>[diagnose.py]"]
  node_facts["Device facts<br/>[facts.py]"]
  node_state["Greenhouse state<br/>[state.py]"]
end

subgraph group_runtime["C inference runtime"]
  node_model_loader["Model loader<br/>[model_loader.c]"]
  node_tokenizer_runtime["Tokenizer runtime<br/>[tokenizer.c]"]
  node_transformer["Transformer inference<br/>[transformer.c]"]
  node_kernels["Inference kernels<br/>[kernels.c]"]
  node_sampler["Token sampler<br/>[sampler.c]"]
  node_console["Prompt and action console<br/>[console.c]"]
  node_device_runtime["Action validation<br/>[device.c]"]
  node_fact_runtime["Fact retrieval<br/>[facts.c]"]
  node_host["Host API<br/>[host.c]"]
end

subgraph group_interfaces["Product interfaces"]
  node_cli["Desktop CLI<br/>[main.c]"]
  node_python_binding["Python runtime binding<br/>[runtime.py]"]
  node_web["Web simulator<br/>[server.py]"]
  node_browser["Browser UI<br/>[app.js]"]
  node_firmware["ESP32 firmware<br/>[app_main.c]"]
end

node_user(("User"))

node_teacher -->|"provides paraphrases"| node_dialogue
node_world -->|"supplies semantics"| node_dialogue
node_diagnosis -->|"supplies diagnoses"| node_dialogue
node_facts -->|"supplies facts"| node_dialogue
node_dialogue -->|"generates samples"| node_dataset
node_dataset -->|"builds vocabulary"| node_tokenizer
node_dataset -->|"provides training data"| node_trainer
node_tokenizer -->|"encodes text"| node_model
node_model -->|"trained by"| node_trainer
node_trainer -->|"exports weights"| node_export
node_export -->|"loads exported model"| node_model_loader
node_trainer -->|"evaluates model"| node_evaluation
node_model_loader -->|"provides model"| node_transformer
node_transformer -->|"runs operations"| node_kernels
node_transformer -->|"uses tokenizer"| node_tokenizer_runtime
node_transformer -->|"produces logits"| node_sampler
node_sampler -->|"returns sampled tokens"| node_console
node_console -->|"validates actions"| node_device_runtime
node_console -->|"retrieves prompt facts"| node_fact_runtime
node_console -->|"applies approved actions"| node_device_runtime
node_cli -->|"opens and submits"| node_host
node_host -->|"serves console API"| node_console
node_python_binding -->|"calls host API"| node_host
node_web -->|"creates runtimes"| node_python_binding
node_browser -.->|"uses web API"| node_web
node_firmware -->|"serves serial console"| node_console
node_user -->|"enters commands"| node_cli
node_user -->|"sends chat"| node_browser
node_user -->|"sends serial input"| node_firmware

click node_dialogue "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/data/dialogue.py"
click node_teacher "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/data/teacher.py"
click node_dataset "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/data/dataset.py"
click node_tokenizer "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/tokenizer/bpe.py"
click node_model "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/model.py"
click node_trainer "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/train.py"
click node_export "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/export.py"
click node_evaluation "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/eval.py"
click node_world "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/world/actions.py"
click node_diagnosis "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/world/diagnose.py"
click node_facts "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/world/facts.py"
click node_state "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/world/state.py"
click node_model_loader "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/model_loader.c"
click node_tokenizer_runtime "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/tokenizer.c"
click node_transformer "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/transformer.c"
click node_kernels "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/kernels.c"
click node_sampler "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/sampler.c"
click node_console "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/console.c"
click node_device_runtime "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/device.c"
click node_fact_runtime "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/facts.c"
click node_host "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/host.c"
click node_cli "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/cli/main.c"
click node_python_binding "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/tools/runtime.py"
click node_web "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/web/server.py"
click node_browser "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/web/static/app.js"
click node_firmware "https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/firmware/main/app_main.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dialogue,node_teacher,node_dataset,node_tokenizer,node_model,node_trainer,node_export,node_evaluation,node_user toneBlue
class node_world,node_diagnosis,node_facts,node_state toneAmber
class node_model_loader,node_tokenizer_runtime,node_transformer,node_kernels,node_sampler,node_console,node_device_runtime,node_fact_runtime,node_host toneMint
class node_cli,node_python_binding,node_web,node_browser,node_firmware toneRose
```

## Components

### Model training

- [Dialogue generation](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/data/dialogue.py): `training/data/dialogue.py`
- [LLM paraphrase teacher](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/data/teacher.py): `training/data/teacher.py`
- [Dataset builder](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/data/dataset.py): `training/data/dataset.py`
- [Tokenizer training](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/tokenizer/bpe.py): `training/tokenizer/bpe.py`
- [Transformer training](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/model.py): `training/model.py`
- [Training loop](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/train.py): `training/train.py`
- [Model export](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/export.py): `training/export.py`
- [Model evaluation](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/eval.py): `training/eval.py`

### Greenhouse domain

- [Device rules](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/world/actions.py): `training/world/actions.py`
- [Fault diagnosis](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/world/diagnose.py): `training/world/diagnose.py`
- [Device facts](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/world/facts.py): `training/world/facts.py`
- [Greenhouse state](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/training/world/state.py): `training/world/state.py`

### C inference runtime

- [Model loader](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/model_loader.c): `runtime/src/model_loader.c`
- [Tokenizer runtime](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/tokenizer.c): `runtime/src/tokenizer.c`
- [Transformer inference](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/transformer.c): `runtime/src/transformer.c`
- [Inference kernels](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/kernels.c): `runtime/src/kernels.c`
- [Token sampler](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/sampler.c): `runtime/src/sampler.c`
- [Prompt and action console](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/console.c): `runtime/src/console.c`
- [Action validation](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/device.c): `runtime/src/device.c`
- [Fact retrieval](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/facts.c): `runtime/src/facts.c`
- [Host API](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/src/host.c): `runtime/src/host.c`

### Product interfaces

- [Desktop CLI](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/runtime/cli/main.c): `runtime/cli/main.c`
- [Python runtime binding](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/tools/runtime.py): `tools/runtime.py`
- [Web simulator](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/web/server.py): `web/server.py`
- [Browser UI](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/web/static/app.js): `web/static/app.js`
- [ESP32 firmware](https://github.com/marcelpetrick/esp32-tiny-llm/blob/main/firmware/main/app_main.c): `firmware/main/app_main.c`

### Other components

- **User**

## Connections

- LLM paraphrase teacher → Dialogue generation: provides paraphrases (evidence: `training/data/dialogue.py`)
- Device rules → Dialogue generation: supplies semantics (evidence: `training/data/dialogue.py`)
- Fault diagnosis → Dialogue generation: supplies diagnoses (evidence: `training/data/dialogue.py`)
- Device facts → Dialogue generation: supplies facts (evidence: `training/data/dialogue.py`)
- Dialogue generation → Dataset builder: generates samples (evidence: `training/data/dataset.py`)
- Dataset builder → Tokenizer training: builds vocabulary (evidence: `training/data/dataset.py`)
- Dataset builder → Training loop: provides training data
- Tokenizer training → Transformer training: encodes text
- Transformer training → Training loop: trained by (evidence: `training/train.py`)
- Training loop → Model export: exports weights
- Model export → Model loader: loads exported model
- Training loop → Model evaluation: evaluates model (evidence: `training/eval.py`)
- Model loader → Transformer inference: provides model
- Transformer inference → Inference kernels: runs operations (evidence: `runtime/src/transformer.c`)
- Transformer inference → Tokenizer runtime: uses tokenizer (evidence: `runtime/src/transformer.c`)
- Transformer inference → Token sampler: produces logits
- Token sampler → Prompt and action console: returns sampled tokens
- Prompt and action console → Action validation: validates actions
- Prompt and action console → Fact retrieval: retrieves prompt facts (evidence: `runtime/src/console.c`)
- Prompt and action console → Action validation: applies approved actions (evidence: `runtime/src/console.c`)
- Desktop CLI → Host API: opens and submits (evidence: `runtime/cli/main.c`)
- Host API → Prompt and action console: serves console API
- Python runtime binding → Host API: calls host API (evidence: `tools/runtime.py`)
- Web simulator → Python runtime binding: creates runtimes (evidence: `web/server.py`)
- Browser UI → Web simulator: uses web API
- ESP32 firmware → Prompt and action console: serves serial console (evidence: `firmware/main/app_main.c`)
- User → Desktop CLI: enters commands
- User → Browser UI: sends chat
- User → ESP32 firmware: sends serial input
