# dimitricl/micro-llm-2bit architecture

> Architecture diagram of the GitHub repository dimitricl/micro-llm-2bit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dimitricl/micro-llm-2bit
- Repository: https://github.com/dimitricl/micro-llm-2bit

## Overview

This repository builds and evaluates a compact ternary-weight language model for low-memory CPU inference. The main workflow prepares a reduced vocabulary and batches, distills a Hugging Face teacher into a student, exports a packed model, then generates text through a memory-mapped inference engine and C kernels. Additional branches prepare reasoning-trace data and evaluate exported models with fixed prompts or held-out perplexity. The graph includes documented capabilities whose implementation was not sampled; their links use README evidence, and uncertain internal wiring is omitted.

Main entry points: [`main.py`](https://github.com/dimitricl/micro-llm-2bit/blob/main/main.py) (Command dispatcher), [`engine.py`](https://github.com/dimitricl/micro-llm-2bit/blob/main/engine.py) (CPU inference engine), [`distill.py`](https://github.com/dimitricl/micro-llm-2bit/blob/main/distill.py) (Distillation trainer), [`kernels/ternary.h`](https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/ternary.h) (Kernel interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data preparation"]
  node_fetch_wiki["Fetch Wikipedia<br/>[fetch_wiki.py]"]
  node_think_traces["Generate traces<br/>[gen_think.py]"]
  node_clean_think["Clean traces<br/>[clean_think.py]"]
  node_make_slices["Build slices<br/>[make_slices.py]"]
  node_clean_monde["Prepare country data<br/>[make_monde_fr.py]"]
  node_vocab_batches["Vocabulary and batches<br/>[data.py]"]
end

subgraph group_train["Training and export"]
  node_cli["Command dispatcher<br/>[main.py]"]
  node_teacher_cache[("Teacher logit cache<br/>[distill.py]")]
  node_distillation["Distillation trainer<br/>[distill.py]"]
  node_student_model["Ternary Transformer<br/>[model.py]"]
  node_quantization["Quantization and packing<br/>[quant.py]"]
  node_checkpoint["Training checkpoint"]
end

subgraph group_runtime["Inference runtime"]
  node_packed_model["Packed model file"]
  node_engine["CPU inference engine<br/>[engine.py]"]
  node_kernel_api["Kernel interface<br/>[ternary.h]"]
  node_neon["NEON kernel<br/>[neon.c]"]
  node_avx2["AVX2 kernel<br/>[avx2.c]"]
  node_portable["Portable kernel<br/>[portable.c]"]
  node_generated_text["Generated text"]
end

subgraph group_quality["Evaluation and tracking"]
  node_eval_fixed["Fixed-prompt evaluation<br/>[eval_fixed.py]"]
  node_eval_ppl["Held-out PPL evaluation<br/>[eval_ppl.py]"]
  node_run_plots["Run plots<br/>[plot_run.py]"]
  node_run_table["Run summary<br/>[runs_table.py]"]
  node_train_benchmark["Training benchmark<br/>[bench_train.py]"]
end

node_user(("CLI user"))
node_datasets["Text corpora"]
node_think_service(("Ollama model"))
node_wiki_source(("Wikipedia source"))

node_user -->|"runs commands"| node_cli
node_wiki_source -.->|"provides source"| node_fetch_wiki
node_fetch_wiki -.->|"prepares slices"| node_make_slices
node_think_service -->|"generates responses"| node_think_traces
node_think_traces -.->|"cleans traces"| node_clean_think
node_datasets -->|"loads corpus"| node_vocab_batches
node_clean_monde -.->|"produces text"| node_datasets
node_make_slices -.->|"produces slices"| node_datasets
node_clean_think -.->|"produces traces"| node_datasets
node_cli -->|"prepares batches"| node_vocab_batches
node_cli -->|"starts training"| node_distillation
node_cli -->|"builds cache"| node_teacher_cache
node_cli -->|"exports model"| node_packed_model
node_cli -->|"runs inference"| node_engine
node_distillation -->|"trains student"| node_student_model
node_student_model -->|"quantizes weights"| node_quantization
node_distillation -->|"reads logits"| node_teacher_cache
node_distillation -.->|"saves weights"| node_checkpoint
node_packed_model -->|"loads model"| node_engine
node_engine -->|"calls matvec"| node_kernel_api
node_kernel_api -->|"defines interface"| node_neon
node_kernel_api -->|"defines interface"| node_avx2
node_kernel_api -->|"defines interface"| node_portable
node_engine -->|"generates tokens"| node_generated_text
node_eval_fixed -->|"runs prompts"| node_engine
node_eval_ppl -->|"scores perplexity"| node_engine
node_eval_fixed -->|"encodes prompts"| node_vocab_batches
node_eval_ppl -->|"loads evaluation data"| node_vocab_batches
node_run_table -->|"references plots"| node_run_plots
node_user -->|"requests evaluation"| node_eval_fixed
node_user -->|"requests evaluation"| node_eval_ppl
node_user -->|"benchmarks training"| node_train_benchmark

click node_fetch_wiki "https://github.com/dimitricl/micro-llm-2bit/blob/main/data_fetch/fetch_wiki.py"
click node_think_traces "https://github.com/dimitricl/micro-llm-2bit/blob/main/data_fetch/gen_think.py"
click node_clean_think "https://github.com/dimitricl/micro-llm-2bit/blob/main/data_fetch/clean_think.py"
click node_make_slices "https://github.com/dimitricl/micro-llm-2bit/blob/main/data_fetch/make_slices.py"
click node_clean_monde "https://github.com/dimitricl/micro-llm-2bit/blob/main/data_clean/make_monde_fr.py"
click node_vocab_batches "https://github.com/dimitricl/micro-llm-2bit/blob/main/data.py"
click node_cli "https://github.com/dimitricl/micro-llm-2bit/blob/main/main.py"
click node_teacher_cache "https://github.com/dimitricl/micro-llm-2bit/blob/main/distill.py"
click node_distillation "https://github.com/dimitricl/micro-llm-2bit/blob/main/distill.py"
click node_student_model "https://github.com/dimitricl/micro-llm-2bit/blob/main/model.py"
click node_quantization "https://github.com/dimitricl/micro-llm-2bit/blob/main/quant.py"
click node_engine "https://github.com/dimitricl/micro-llm-2bit/blob/main/engine.py"
click node_kernel_api "https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/ternary.h"
click node_neon "https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/neon.c"
click node_avx2 "https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/avx2.c"
click node_portable "https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/portable.c"
click node_eval_fixed "https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/eval_fixed.py"
click node_eval_ppl "https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/eval_ppl.py"
click node_run_plots "https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/plot_run.py"
click node_run_table "https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/runs_table.py"
click node_train_benchmark "https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/bench_train.py"

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_fetch_wiki,node_think_traces,node_clean_think,node_make_slices,node_clean_monde,node_vocab_batches,node_user toneBlue
class node_cli,node_teacher_cache,node_distillation,node_student_model,node_quantization,node_checkpoint toneAmber
class node_packed_model,node_engine,node_kernel_api,node_neon,node_avx2,node_portable,node_generated_text toneMint
class node_eval_fixed,node_eval_ppl,node_run_plots,node_run_table,node_train_benchmark toneRose
class node_datasets,node_think_service,node_wiki_source toneIndigo
```

## Components

### Data preparation

- [Fetch Wikipedia](https://github.com/dimitricl/micro-llm-2bit/blob/main/data_fetch/fetch_wiki.py): `data_fetch/fetch_wiki.py`
- [Generate traces](https://github.com/dimitricl/micro-llm-2bit/blob/main/data_fetch/gen_think.py): `data_fetch/gen_think.py`
- [Clean traces](https://github.com/dimitricl/micro-llm-2bit/blob/main/data_fetch/clean_think.py): `data_fetch/clean_think.py`
- [Build slices](https://github.com/dimitricl/micro-llm-2bit/blob/main/data_fetch/make_slices.py): `data_fetch/make_slices.py`
- [Prepare country data](https://github.com/dimitricl/micro-llm-2bit/blob/main/data_clean/make_monde_fr.py): `data_clean/make_monde_fr.py`
- [Vocabulary and batches](https://github.com/dimitricl/micro-llm-2bit/blob/main/data.py): `data.py`

### Training and export

- [Command dispatcher](https://github.com/dimitricl/micro-llm-2bit/blob/main/main.py): `main.py`
- [Teacher logit cache](https://github.com/dimitricl/micro-llm-2bit/blob/main/distill.py): `distill.py`
- [Distillation trainer](https://github.com/dimitricl/micro-llm-2bit/blob/main/distill.py): `distill.py`
- [Ternary Transformer](https://github.com/dimitricl/micro-llm-2bit/blob/main/model.py): `model.py`
- [Quantization and packing](https://github.com/dimitricl/micro-llm-2bit/blob/main/quant.py): `quant.py`
- **Training checkpoint**

### Inference runtime

- **Packed model file**
- [CPU inference engine](https://github.com/dimitricl/micro-llm-2bit/blob/main/engine.py): `engine.py`
- [Kernel interface](https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/ternary.h): `kernels/ternary.h`
- [NEON kernel](https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/neon.c): `kernels/neon.c`
- [AVX2 kernel](https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/avx2.c): `kernels/avx2.c`
- [Portable kernel](https://github.com/dimitricl/micro-llm-2bit/blob/main/kernels/portable.c): `kernels/portable.c`
- **Generated text**

### Evaluation and tracking

- [Fixed-prompt evaluation](https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/eval_fixed.py): `tools/eval_fixed.py`
- [Held-out PPL evaluation](https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/eval_ppl.py): `tools/eval_ppl.py`
- [Run plots](https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/plot_run.py): `tools/plot_run.py`
- [Run summary](https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/runs_table.py): `tools/runs_table.py`
- [Training benchmark](https://github.com/dimitricl/micro-llm-2bit/blob/main/tools/bench_train.py): `tools/bench_train.py`

### Other components

- **CLI user**
- **Text corpora**
- **Ollama model**
- **Wikipedia source**

## Connections

- CLI user → Command dispatcher: runs commands (evidence: `README.md`)
- Wikipedia source → Fetch Wikipedia: provides source
- Fetch Wikipedia → Build slices: prepares slices
- Ollama model → Generate traces: generates responses (evidence: `data_fetch/gen_think.py`)
- Generate traces → Clean traces: cleans traces (evidence: `data_fetch/clean_think.py`)
- Text corpora → Vocabulary and batches: loads corpus (evidence: `data.py`)
- Prepare country data → Text corpora: produces text
- Build slices → Text corpora: produces slices
- Clean traces → Text corpora: produces traces
- Command dispatcher → Vocabulary and batches: prepares batches (evidence: `main.py`)
- Command dispatcher → Distillation trainer: starts training (evidence: `README.md`)
- Command dispatcher → Teacher logit cache: builds cache (evidence: `README.md`)
- Command dispatcher → Packed model file: exports model (evidence: `README.md`)
- Command dispatcher → CPU inference engine: runs inference (evidence: `README.md`)
- Distillation trainer → Ternary Transformer: trains student (evidence: `distill.py`)
- Ternary Transformer → Quantization and packing: quantizes weights (evidence: `model.py`)
- Distillation trainer → Teacher logit cache: reads logits (evidence: `README.md`)
- Distillation trainer → Training checkpoint: saves weights
- Packed model file → CPU inference engine: loads model (evidence: `engine.py`)
- CPU inference engine → Kernel interface: calls matvec (evidence: `README.md`)
- Kernel interface → NEON kernel: defines interface (evidence: `kernels/ternary.h`)
- Kernel interface → AVX2 kernel: defines interface (evidence: `kernels/ternary.h`)
- Kernel interface → Portable kernel: defines interface (evidence: `kernels/ternary.h`)
- CPU inference engine → Generated text: generates tokens (evidence: `README.md`)
- Fixed-prompt evaluation → CPU inference engine: runs prompts (evidence: `tools/eval_fixed.py`)
- Held-out PPL evaluation → CPU inference engine: scores perplexity (evidence: `tools/eval_ppl.py`)
- Fixed-prompt evaluation → Vocabulary and batches: encodes prompts (evidence: `tools/eval_fixed.py`)
- Held-out PPL evaluation → Vocabulary and batches: loads evaluation data (evidence: `tools/eval_ppl.py`)
- Run summary → Run plots: references plots (evidence: `tools/runs_table.py`)
- CLI user → Fixed-prompt evaluation: requests evaluation (evidence: `README.md`)
- CLI user → Held-out PPL evaluation: requests evaluation (evidence: `README.md`)
- CLI user → Training benchmark: benchmarks training (evidence: `README.md`)
