# hiyouga/llamafactory architecture

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

- Interactive diagram: https://gitdiagram.com/hiyouga/llamafactory
- Repository: https://github.com/hiyouga/llamafactory

## Overview

LLaMA-Factory is a toolkit for fine-tuning and serving large language models. The main workflow takes user configuration, model and dataset inputs, prepares training examples and a model, runs a selected training method, and produces a tuned model or checkpoint. Separate paths support evaluation, interactive chat, and OpenAI-style API inference, using Hugging Face, vLLM, or SGLang engines. The README also documents multimodal tasks, parameter-efficient tuning, and a Gradio Web UI. The graph uses documented capabilities and the supplied source excerpts; several subsystem connections are necessarily high-level because their orchestration code was not sampled.

Main entry points: [`src/llamafactory/train/tuner.py`](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/tuner.py) (Training dispatch), [`src/llamafactory/chat/chat_model.py`](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/chat_model.py) (Chat model), [`src/llamafactory/cli.py`](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/cli.py) (CLI), [`src/llamafactory/webui/interface.py`](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/webui/interface.py) (Gradio UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["User interfaces"]
  node_cli["CLI<br/>[cli.py]"]
  node_webui["Gradio UI<br/>[interface.py]"]
  node_api["OpenAI API<br/>[app.py]"]
  node_webui_engine["UI coordinator<br/>[engine.py]"]
end

subgraph group_preparation["Data and models"]
  node_data_engine["Data engine<br/>[data_engine.py]"]
  node_data_pipeline["Data processing<br/>[loader.py]"]
  node_model_engine["Model engine<br/>[model_engine.py]"]
  node_model_loader["Model loading<br/>[loader.py]"]
end

subgraph group_training["Training and evaluation"]
  node_training_dispatch["Training dispatch<br/>[tuner.py]"]
  node_pretraining["Pretraining<br/>[workflow.py]"]
  node_supervised["Supervised tuning<br/>[workflow.py]"]
  node_preference["Preference tuning<br/>[workflow.py]"]
  node_reward["Reward modeling<br/>[workflow.py]"]
  node_policy["Policy optimization<br/>[workflow.py]"]
  node_evaluation["Evaluation<br/>[evaluator.py]"]
end

subgraph group_serving["Inference and serving"]
  node_chat_model["Chat model<br/>[chat_model.py]"]
  node_hf_engine["Hugging Face engine<br/>[hf_engine.py]"]
  node_vllm_engine["vLLM engine<br/>[vllm_engine.py]"]
  node_sglang_engine["SGLang engine<br/>[sglang_engine.py]"]
  node_generation["Generation engines"]
end

subgraph group_runtime["Runtime components"]
end

node_user(("User"))
node_dataset[("Dataset sources")]
node_checkpoint[("Model checkpoints")]

node_user -->|"configures"| node_cli
node_user -->|"operates"| node_webui
node_user -->|"requests"| node_api
node_cli -->|"dispatches"| node_training_dispatch
node_webui -->|"controls"| node_webui_engine
node_webui_engine -->|"starts training"| node_training_dispatch
node_dataset -->|"loads"| node_data_engine
node_dataset -->|"loads"| node_data_pipeline
node_data_engine -->|"provides samples"| node_training_dispatch
node_data_pipeline -->|"prepares samples"| node_training_dispatch
node_training_dispatch -->|"loads model"| node_model_loader
node_training_dispatch -->|"selects stage"| node_pretraining
node_training_dispatch -->|"selects stage"| node_supervised
node_training_dispatch -->|"selects stage"| node_preference
node_training_dispatch -->|"selects stage"| node_reward
node_training_dispatch -->|"selects stage"| node_policy
node_training_dispatch -->|"evaluates"| node_evaluation
node_pretraining -->|"writes"| node_checkpoint
node_supervised -->|"writes"| node_checkpoint
node_preference -->|"writes"| node_checkpoint
node_reward -->|"writes"| node_checkpoint
node_policy -->|"writes"| node_checkpoint
node_evaluation -->|"evaluates"| node_checkpoint
node_api -->|"routes requests"| node_chat_model
node_chat_model -->|"uses engine"| node_hf_engine
node_chat_model -.->|"uses engine"| node_vllm_engine
node_chat_model -.->|"uses engine"| node_sglang_engine
node_hf_engine -->|"loads model"| node_checkpoint
node_vllm_engine -.->|"loads model"| node_checkpoint
node_sglang_engine -.->|"loads model"| node_checkpoint
node_generation -->|"uses model"| node_model_engine
node_model_engine -->|"loads model"| node_checkpoint

click node_cli "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/cli.py"
click node_webui "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/webui/interface.py"
click node_api "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/api/app.py"
click node_data_engine "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/v1/core/data_engine.py"
click node_data_pipeline "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/data/loader.py"
click node_model_engine "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/v1/core/model_engine.py"
click node_model_loader "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/model/loader.py"
click node_training_dispatch "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/tuner.py"
click node_pretraining "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/pt/workflow.py"
click node_supervised "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/sft/workflow.py"
click node_preference "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/dpo/workflow.py"
click node_reward "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/rm/workflow.py"
click node_policy "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/ppo/workflow.py"
click node_evaluation "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/eval/evaluator.py"
click node_chat_model "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/chat_model.py"
click node_hf_engine "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/hf_engine.py"
click node_vllm_engine "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/vllm_engine.py"
click node_sglang_engine "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/sglang_engine.py"
click node_generation "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/v1/core/utils/inference_engine.py"
click node_webui_engine "https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/webui/engine.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_cli,node_webui,node_api,node_webui_engine,node_user toneBlue
class node_data_engine,node_data_pipeline,node_model_engine,node_model_loader,node_dataset,node_checkpoint toneAmber
class node_training_dispatch,node_pretraining,node_supervised,node_preference,node_reward,node_policy,node_evaluation toneMint
class node_chat_model,node_hf_engine,node_vllm_engine,node_sglang_engine,node_generation toneRose
```

## Components

### User interfaces

- [CLI](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/cli.py): `src/llamafactory/cli.py`
- [Gradio UI](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/webui/interface.py): `src/llamafactory/webui/interface.py`
- [OpenAI API](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/api/app.py): `src/llamafactory/api/app.py`
- [UI coordinator](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/webui/engine.py): `src/llamafactory/webui/engine.py`

### Data and models

- [Data engine](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/v1/core/data_engine.py): `src/llamafactory/v1/core/data_engine.py`
- [Data processing](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/data/loader.py): `src/llamafactory/data/loader.py`
- [Model engine](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/v1/core/model_engine.py): `src/llamafactory/v1/core/model_engine.py`
- [Model loading](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/model/loader.py): `src/llamafactory/model/loader.py`

### Training and evaluation

- [Training dispatch](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/tuner.py): `src/llamafactory/train/tuner.py`
- [Pretraining](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/pt/workflow.py): `src/llamafactory/train/pt/workflow.py`
- [Supervised tuning](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/sft/workflow.py): `src/llamafactory/train/sft/workflow.py`
- [Preference tuning](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/dpo/workflow.py): `src/llamafactory/train/dpo/workflow.py`
- [Reward modeling](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/rm/workflow.py): `src/llamafactory/train/rm/workflow.py`
- [Policy optimization](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/train/ppo/workflow.py): `src/llamafactory/train/ppo/workflow.py`
- [Evaluation](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/eval/evaluator.py): `src/llamafactory/eval/evaluator.py`

### Inference and serving

- [Chat model](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/chat_model.py): `src/llamafactory/chat/chat_model.py`
- [Hugging Face engine](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/hf_engine.py): `src/llamafactory/chat/hf_engine.py`
- [vLLM engine](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/vllm_engine.py): `src/llamafactory/chat/vllm_engine.py`
- [SGLang engine](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/chat/sglang_engine.py): `src/llamafactory/chat/sglang_engine.py`
- [Generation engines](https://github.com/hiyouga/llamafactory/blob/main/src/llamafactory/v1/core/utils/inference_engine.py): `src/llamafactory/v1/core/utils/inference_engine.py`

### Other components

- **User**
- **Dataset sources**
- **Model checkpoints**

## Connections

- User → CLI: configures
- User → Gradio UI: operates
- User → OpenAI API: requests
- CLI → Training dispatch: dispatches
- Gradio UI → UI coordinator: controls
- UI coordinator → Training dispatch: starts training
- Dataset sources → Data engine: loads
- Dataset sources → Data processing: loads
- Data engine → Training dispatch: provides samples
- Data processing → Training dispatch: prepares samples
- Training dispatch → Model loading: loads model
- Training dispatch → Pretraining: selects stage
- Training dispatch → Supervised tuning: selects stage
- Training dispatch → Preference tuning: selects stage
- Training dispatch → Reward modeling: selects stage
- Training dispatch → Policy optimization: selects stage
- Training dispatch → Evaluation: evaluates
- Pretraining → Model checkpoints: writes
- Supervised tuning → Model checkpoints: writes
- Preference tuning → Model checkpoints: writes
- Reward modeling → Model checkpoints: writes
- Policy optimization → Model checkpoints: writes
- Evaluation → Model checkpoints: evaluates
- OpenAI API → Chat model: routes requests
- Chat model → Hugging Face engine: uses engine
- Chat model → vLLM engine: uses engine
- Chat model → SGLang engine: uses engine
- Hugging Face engine → Model checkpoints: loads model
- vLLM engine → Model checkpoints: loads model
- SGLang engine → Model checkpoints: loads model
- Generation engines → Model engine: uses model
- Model engine → Model checkpoints: loads model
