# buddy-compiler/buddy-mlir architecture

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

- Interactive diagram: https://gitdiagram.com/buddy-compiler/buddy-mlir
- Repository: https://github.com/buddy-compiler/buddy-mlir
- Video tour (about a minute): https://gitdiagram.com/buddy-compiler/buddy-mlir/video

## Overview

BUDDY MLIR combines a DSL-to-DSA compiler with packaged model inference. The documented workflow builds a model from a spec (optionally importing local HuggingFace weights), then runs its .rax artifact through buddy-cli. The compiler graph preserves Python frontend/lowering and MLIR dialect/conversion stages; the runtime graph covers manifest-based model loading, model-specific inference, and text, audio, and vision-language use cases. Some subsystem links are documented only at a high level; unsampled implementation details are not inferred as specific calls.

Main entry points: [`tools/buddy-cli/buddy-cli.cpp`](https://github.com/buddy-compiler/buddy-mlir/blob/main/tools/buddy-cli/buddy-cli.cpp) (Inference CLI), [`tools/buddy-codegen/build_model.py`](https://github.com/buddy-compiler/buddy-mlir/blob/main/tools/buddy-codegen/build_model.py) (Model codegen), [`models/llama31_tt/Llama31TTRunner.cpp`](https://github.com/buddy-compiler/buddy-mlir/blob/main/models/llama31_tt/Llama31TTRunner.cpp) (Llama runner), [`frontend/Python/frontend.py`](https://github.com/buddy-compiler/buddy-mlir/blob/main/frontend/Python/frontend.py) (Python frontend)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_compiler["Compiler frontend"]
  node_python_frontend["Python frontend<br/>[frontend.py]"]
  node_frontendgen["Frontend generator<br/>[Parser.cpp]"]
  node_python_ops["TTIR operations<br/>[ttir_llm.py]"]
  node_graph_ir["Graph transforms"]
end

subgraph group_ir["MLIR optimization"]
  node_dialects["MLIR dialects"]
  node_conversion["Conversion passes"]
  node_llvm_export["LLVM export"]
end

subgraph group_packaging["Model packaging"]
  node_codegen["Model codegen<br/>[build_model.py]"]
  node_model_import["Model import<br/>[import_model.py]"]
  node_artifact["RAX model artifact"]
end

subgraph group_runtime["Inference runtime"]
  node_cli["Inference CLI<br/>[buddy-cli.cpp]"]
  node_manifest["Manifest resolver<br/>[ModelManifest.h]"]
  node_runner["Inference runner<br/>[InferenceRunner.h]"]
  node_llm_generation["Text generation<br/>[TextGeneration.cpp]"]
  node_sampler["Token sampler<br/>[Sampler.h]"]
end

subgraph group_models["Model capabilities"]
  node_llama_runner["Llama runner"]
  node_embedding["BGE-M3 embeddings"]
  node_whisper["Whisper transcription<br/>[WhisperRuntime.cpp]"]
  node_qwen_vl["Qwen3-VL vision<br/>[Qwen3VLRuntime.cpp]"]
end

node_developer(("Model developer"))
node_result["Inference result"]

node_developer -->|"builds model"| node_codegen
node_codegen -.->|"imports weights"| node_model_import
node_codegen -.->|"lowers model"| node_python_frontend
node_python_frontend -.->|"transforms graph"| node_graph_ir
node_python_ops -.->|"provides operations"| node_graph_ir
node_frontendgen -.->|"generates definitions"| node_dialects
node_dialects -.->|"supports conversion"| node_conversion
node_conversion -.->|"lowers to LLVM"| node_llvm_export
node_codegen -->|"produces RAX"| node_artifact
node_developer -->|"runs model"| node_cli
node_cli -->|"loads artifact"| node_artifact
node_cli -->|"resolves manifest"| node_manifest
node_cli -.->|"invokes runner"| node_runner
node_runner -.->|"generates text"| node_llm_generation
node_llama_runner -->|"uses sampler"| node_sampler
node_llama_runner -->|"reads manifest"| node_manifest
node_llm_generation -.->|"returns text"| node_result
node_cli -->|"supports audio"| node_whisper
node_cli -->|"supports images"| node_qwen_vl
node_embedding -.->|"returns embeddings"| node_result

click node_codegen "https://github.com/buddy-compiler/buddy-mlir/blob/main/tools/buddy-codegen/build_model.py"
click node_model_import "https://github.com/buddy-compiler/buddy-mlir/blob/main/tools/buddy-codegen/import_model.py"
click node_python_frontend "https://github.com/buddy-compiler/buddy-mlir/blob/main/frontend/Python/frontend.py"
click node_frontendgen "https://github.com/buddy-compiler/buddy-mlir/blob/main/frontend/FrontendGen/lib/Parser.cpp"
click node_python_ops "https://github.com/buddy-compiler/buddy-mlir/blob/main/frontend/Python/ops/ttir_llm.py"
click node_graph_ir "https://github.com/buddy-compiler/buddy-mlir/tree/main/frontend/Python/graph/transform"
click node_dialects "https://github.com/buddy-compiler/buddy-mlir/tree/main/midend/lib/Dialect"
click node_conversion "https://github.com/buddy-compiler/buddy-mlir/tree/main/midend/lib/Conversion"
click node_llvm_export "https://github.com/buddy-compiler/buddy-mlir/blob/main/midend/lib/Target/LLVMIR/ConvertBuddyToLLVMIR.cpp"
click node_cli "https://github.com/buddy-compiler/buddy-mlir/blob/main/tools/buddy-cli/buddy-cli.cpp"
click node_manifest "https://github.com/buddy-compiler/buddy-mlir/blob/main/runtime/include/buddy/runtime/core/ModelManifest.h"
click node_runner "https://github.com/buddy-compiler/buddy-mlir/blob/main/runtime/include/buddy/runtime/core/InferenceRunner.h"
click node_llm_generation "https://github.com/buddy-compiler/buddy-mlir/blob/main/runtime/llm/TextGeneration.cpp"
click node_sampler "https://github.com/buddy-compiler/buddy-mlir/blob/main/runtime/include/buddy/runtime/llm/Sampler.h"
click node_llama_runner "https://github.com/buddy-compiler/buddy-mlir/blob/main/models/llama31_tt/Llama31TTRunner.cpp"
click node_embedding "https://github.com/buddy-compiler/buddy-mlir/blob/main/models/bge_m3/BgeM3EmbeddingModel.cpp"
click node_whisper "https://github.com/buddy-compiler/buddy-mlir/blob/main/models/whisper/WhisperRuntime.cpp"
click node_qwen_vl "https://github.com/buddy-compiler/buddy-mlir/blob/main/models/qwen3_vl/Qwen3VLRuntime.cpp"

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_python_frontend,node_frontendgen,node_python_ops,node_graph_ir toneBlue
class node_dialects,node_conversion,node_llvm_export toneAmber
class node_codegen,node_model_import,node_artifact toneMint
class node_cli,node_manifest,node_runner,node_llm_generation,node_sampler toneRose
class node_llama_runner,node_embedding,node_whisper,node_qwen_vl,node_developer,node_result toneIndigo
```

## Components

### Compiler frontend

- [Python frontend](https://github.com/buddy-compiler/buddy-mlir/blob/main/frontend/Python/frontend.py): `frontend/Python/frontend.py`
- [Frontend generator](https://github.com/buddy-compiler/buddy-mlir/blob/main/frontend/FrontendGen/lib/Parser.cpp): `frontend/FrontendGen/lib/Parser.cpp`
- [TTIR operations](https://github.com/buddy-compiler/buddy-mlir/blob/main/frontend/Python/ops/ttir_llm.py): `frontend/Python/ops/ttir_llm.py`
- [Graph transforms](https://github.com/buddy-compiler/buddy-mlir/tree/main/frontend/Python/graph/transform): `frontend/Python/graph/transform`

### MLIR optimization

- [MLIR dialects](https://github.com/buddy-compiler/buddy-mlir/tree/main/midend/lib/Dialect): `midend/lib/Dialect`
- [Conversion passes](https://github.com/buddy-compiler/buddy-mlir/tree/main/midend/lib/Conversion): `midend/lib/Conversion`
- [LLVM export](https://github.com/buddy-compiler/buddy-mlir/blob/main/midend/lib/Target/LLVMIR/ConvertBuddyToLLVMIR.cpp): `midend/lib/Target/LLVMIR/ConvertBuddyToLLVMIR.cpp`

### Model packaging

- [Model codegen](https://github.com/buddy-compiler/buddy-mlir/blob/main/tools/buddy-codegen/build_model.py): `tools/buddy-codegen/build_model.py`
- [Model import](https://github.com/buddy-compiler/buddy-mlir/blob/main/tools/buddy-codegen/import_model.py): `tools/buddy-codegen/import_model.py`
- **RAX model artifact**

### Inference runtime

- [Inference CLI](https://github.com/buddy-compiler/buddy-mlir/blob/main/tools/buddy-cli/buddy-cli.cpp): `tools/buddy-cli/buddy-cli.cpp`
- [Manifest resolver](https://github.com/buddy-compiler/buddy-mlir/blob/main/runtime/include/buddy/runtime/core/ModelManifest.h): `runtime/include/buddy/runtime/core/ModelManifest.h`
- [Inference runner](https://github.com/buddy-compiler/buddy-mlir/blob/main/runtime/include/buddy/runtime/core/InferenceRunner.h): `runtime/include/buddy/runtime/core/InferenceRunner.h`
- [Text generation](https://github.com/buddy-compiler/buddy-mlir/blob/main/runtime/llm/TextGeneration.cpp): `runtime/llm/TextGeneration.cpp`
- [Token sampler](https://github.com/buddy-compiler/buddy-mlir/blob/main/runtime/include/buddy/runtime/llm/Sampler.h): `runtime/include/buddy/runtime/llm/Sampler.h`

### Model capabilities

- [Llama runner](https://github.com/buddy-compiler/buddy-mlir/blob/main/models/llama31_tt/Llama31TTRunner.cpp): `models/llama31_tt/Llama31TTRunner.cpp`
- [BGE-M3 embeddings](https://github.com/buddy-compiler/buddy-mlir/blob/main/models/bge_m3/BgeM3EmbeddingModel.cpp): `models/bge_m3/BgeM3EmbeddingModel.cpp`
- [Whisper transcription](https://github.com/buddy-compiler/buddy-mlir/blob/main/models/whisper/WhisperRuntime.cpp): `models/whisper/WhisperRuntime.cpp`
- [Qwen3-VL vision](https://github.com/buddy-compiler/buddy-mlir/blob/main/models/qwen3_vl/Qwen3VLRuntime.cpp): `models/qwen3_vl/Qwen3VLRuntime.cpp`

### Other components

- **Model developer**
- **Inference result**

## Connections

- Model developer → Model codegen: builds model (evidence: `README.md`)
- Model codegen → Model import: imports weights
- Model codegen → Python frontend: lowers model
- Python frontend → Graph transforms: transforms graph
- TTIR operations → Graph transforms: provides operations
- Frontend generator → MLIR dialects: generates definitions
- MLIR dialects → Conversion passes: supports conversion
- Conversion passes → LLVM export: lowers to LLVM
- Model codegen → RAX model artifact: produces RAX (evidence: `README.md`)
- Model developer → Inference CLI: runs model (evidence: `README.md`)
- Inference CLI → RAX model artifact: loads artifact (evidence: `tools/buddy-cli/buddy-cli.cpp`)
- Inference CLI → Manifest resolver: resolves manifest (evidence: `tools/buddy-cli/buddy-cli.cpp`)
- Inference CLI → Inference runner: invokes runner (evidence: `tools/buddy-cli/buddy-cli.cpp`)
- Inference runner → Text generation: generates text
- Llama runner → Token sampler: uses sampler (evidence: `models/llama31_tt/Llama31TTRunner.cpp`)
- Llama runner → Manifest resolver: reads manifest (evidence: `models/llama31_tt/Llama31TTRunner.cpp`)
- Text generation → Inference result: returns text
- Inference CLI → Whisper transcription: supports audio (evidence: `README.md`)
- Inference CLI → Qwen3-VL vision: supports images (evidence: `README.md`)
- BGE-M3 embeddings → Inference result: returns embeddings
