# huggingface/trl architecture

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

- Interactive diagram: https://gitdiagram.com/huggingface/trl
- Repository: https://github.com/huggingface/trl

## Overview

TRL is a Transformers-based library for post-training foundation models. Users provide a model and dataset, select an algorithm-specific trainer, and run training; GRPO additionally uses reward functions and can generate through vLLM, with AsyncGRPO overlapping remote rollouts and training. The map includes documented trainer families, CLI entry points, shared data/model utilities, and experimental methods. Edges are limited to README statements and relationships named by the source index; unsampled implementations are represented only where the tree or README supports them.

Main entry points: [`trl/trainer/grpo_trainer.py`](https://github.com/huggingface/trl/blob/main/trl/trainer/grpo_trainer.py) (GRPO trainer), [`trl/trainer/async_grpo_trainer.py`](https://github.com/huggingface/trl/blob/main/trl/trainer/async_grpo_trainer.py) (Async GRPO), [`trl/trainer/sft_trainer.py`](https://github.com/huggingface/trl/blob/main/trl/trainer/sft_trainer.py) (SFT trainer), [`trl/trainer/dpo_trainer.py`](https://github.com/huggingface/trl/blob/main/trl/trainer/dpo_trainer.py) (DPO trainer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_training["Training algorithms"]
  node_sft["SFT trainer<br/>[sft_trainer.py]"]
  node_grpo["GRPO trainer<br/>[grpo_trainer.py]"]
  node_async_grpo["Async GRPO"]
  node_dpo["DPO trainer<br/>[dpo_trainer.py]"]
  node_kto["KTO trainer<br/>[kto_trainer.py]"]
  node_reward_trainer["Reward trainer<br/>[reward_trainer.py]"]
  node_distillation["Distillation trainer"]
  node_experimental["Experimental trainers"]
  node_gold_config["GOLD configuration<br/>[gold_config.py]"]
  node_sdpo_config["SDPO configuration<br/>[sdpo_config.py]"]
end

subgraph group_generation["Generation and rewards"]
  node_reward_funcs["Reward functions"]
  node_vllm_gen["vLLM generation<br/>[vllm_generation.py]"]
  node_vllm_client["vLLM client<br/>[vllm_client.py]"]
  node_async_client["Async vLLM client"]
end

subgraph group_data["Data and model utilities"]
  node_chat_templates["Chat templates"]
  node_data_utils["Dataset utilities<br/>[data_utils.py]"]
  node_model_utils["Model utilities<br/>[utils.py]"]
end

subgraph group_runtime["Training runtime"]
  node_base_trainer["Trainer foundation<br/>[base_trainer.py]"]
  node_distributed["Distributed training<br/>[distributed.py]"]
  node_profiling["Profiling<br/>[profiling.py]"]
  node_chunked_logprob["Chunked log-prob<br/>[chunked_logprob.py]"]
  node_tree_kernel["Gated delta kernel"]
  node_import_utils["Dependency checks<br/>[import_utils.py]"]
end

subgraph group_interfaces["User interfaces"]
  node_cli["TRL CLI<br/>[main.py]"]
  node_cli_training["CLI training commands<br/>[training.py]"]
end

node_user(("Trainer user"))
node_dataset[("Training dataset")]
node_model["Foundation model"]
node_vllm_server["vLLM server"]

node_user -->|"starts training"| node_sft
node_user -->|"starts training"| node_grpo
node_user -->|"starts training"| node_async_grpo
node_user -->|"starts training"| node_dpo
node_user -->|"starts training"| node_kto
node_user -->|"starts training"| node_reward_trainer
node_dataset -->|"training input"| node_sft
node_dataset -->|"training input"| node_grpo
node_dataset -->|"training input"| node_async_grpo
node_dataset -->|"training input"| node_dpo
node_dataset -->|"training input"| node_kto
node_dataset -->|"training input"| node_reward_trainer
node_model -->|"model input"| node_sft
node_model -->|"model input"| node_grpo
node_model -->|"model input"| node_async_grpo
node_model -->|"model input"| node_dpo
node_model -->|"model input"| node_kto
node_model -->|"model input"| node_reward_trainer
node_grpo -->|"scores with"| node_reward_funcs
node_async_grpo -->|"scores with"| node_reward_funcs
node_grpo -->|"generates with"| node_vllm_gen
node_vllm_gen -->|"uses client"| node_vllm_client
node_vllm_client -->|"sends requests"| node_vllm_server
node_async_grpo -->|"streams rollouts"| node_vllm_server
node_async_grpo -->|"uses client"| node_async_client
node_async_client -->|"checks server"| node_vllm_server
node_grpo -->|"inherits support"| node_base_trainer
node_grpo -->|"uses utilities"| node_data_utils
node_grpo -->|"uses templates"| node_chat_templates
node_grpo -->|"uses utilities"| node_model_utils
node_grpo -->|"uses support"| node_distributed
node_grpo -->|"profiles with"| node_profiling
node_distillation -->|"uses kernel"| node_chunked_logprob
node_async_grpo -->|"uses kernel"| node_tree_kernel
node_experimental -->|"includes config"| node_gold_config
node_experimental -->|"includes config"| node_sdpo_config
node_cli -->|"dispatches to"| node_cli_training
node_cli_training -->|"launches method"| node_sft
node_cli_training -->|"launches method"| node_dpo

click node_sft "https://github.com/huggingface/trl/blob/main/trl/trainer/sft_trainer.py"
click node_grpo "https://github.com/huggingface/trl/blob/main/trl/trainer/grpo_trainer.py"
click node_async_grpo "https://github.com/huggingface/trl/blob/main/trl/trainer/async_grpo_trainer.py"
click node_dpo "https://github.com/huggingface/trl/blob/main/trl/trainer/dpo_trainer.py"
click node_kto "https://github.com/huggingface/trl/blob/main/trl/trainer/kto_trainer.py"
click node_reward_trainer "https://github.com/huggingface/trl/blob/main/trl/trainer/reward_trainer.py"
click node_distillation "https://github.com/huggingface/trl/blob/main/trl/trainer/distillation_trainer.py"
click node_experimental "https://github.com/huggingface/trl/tree/main/trl/experimental"
click node_reward_funcs "https://github.com/huggingface/trl/blob/main/trl/rewards/accuracy_rewards.py"
click node_vllm_gen "https://github.com/huggingface/trl/blob/main/trl/generation/vllm_generation.py"
click node_vllm_client "https://github.com/huggingface/trl/blob/main/trl/generation/vllm_client.py"
click node_async_client "https://github.com/huggingface/trl/blob/main/trl/generation/async_vllm_client.py"
click node_chat_templates "https://github.com/huggingface/trl/blob/main/trl/chat_template_utils.py"
click node_data_utils "https://github.com/huggingface/trl/blob/main/trl/data_utils.py"
click node_model_utils "https://github.com/huggingface/trl/blob/main/trl/models/utils.py"
click node_base_trainer "https://github.com/huggingface/trl/blob/main/trl/trainer/base_trainer.py"
click node_distributed "https://github.com/huggingface/trl/blob/main/trl/distributed.py"
click node_profiling "https://github.com/huggingface/trl/blob/main/trl/extras/profiling.py"
click node_chunked_logprob "https://github.com/huggingface/trl/blob/main/trl/kernels/chunked_logprob.py"
click node_tree_kernel "https://github.com/huggingface/trl/blob/main/trl/kernels/tree_gated_delta_rule.py"
click node_gold_config "https://github.com/huggingface/trl/blob/main/trl/experimental/gold/gold_config.py"
click node_sdpo_config "https://github.com/huggingface/trl/blob/main/trl/experimental/sdpo/sdpo_config.py"
click node_cli "https://github.com/huggingface/trl/blob/main/trl/cli/main.py"
click node_cli_training "https://github.com/huggingface/trl/blob/main/trl/cli/commands/training.py"
click node_import_utils "https://github.com/huggingface/trl/blob/main/trl/import_utils.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_sft,node_grpo,node_async_grpo,node_dpo,node_kto,node_reward_trainer,node_distillation,node_experimental,node_gold_config,node_sdpo_config,node_user toneBlue
class node_reward_funcs,node_vllm_gen,node_vllm_client,node_async_client,node_dataset toneAmber
class node_chat_templates,node_data_utils,node_model_utils,node_vllm_server toneMint
class node_base_trainer,node_distributed,node_profiling,node_chunked_logprob,node_tree_kernel,node_import_utils toneRose
class node_cli,node_cli_training,node_model toneIndigo
```

## Components

### Training algorithms

- [SFT trainer](https://github.com/huggingface/trl/blob/main/trl/trainer/sft_trainer.py): `trl/trainer/sft_trainer.py`
- [GRPO trainer](https://github.com/huggingface/trl/blob/main/trl/trainer/grpo_trainer.py): `trl/trainer/grpo_trainer.py`
- [Async GRPO](https://github.com/huggingface/trl/blob/main/trl/trainer/async_grpo_trainer.py): `trl/trainer/async_grpo_trainer.py`
- [DPO trainer](https://github.com/huggingface/trl/blob/main/trl/trainer/dpo_trainer.py): `trl/trainer/dpo_trainer.py`
- [KTO trainer](https://github.com/huggingface/trl/blob/main/trl/trainer/kto_trainer.py): `trl/trainer/kto_trainer.py`
- [Reward trainer](https://github.com/huggingface/trl/blob/main/trl/trainer/reward_trainer.py): `trl/trainer/reward_trainer.py`
- [Distillation trainer](https://github.com/huggingface/trl/blob/main/trl/trainer/distillation_trainer.py): `trl/trainer/distillation_trainer.py`
- [Experimental trainers](https://github.com/huggingface/trl/tree/main/trl/experimental): `trl/experimental`
- [GOLD configuration](https://github.com/huggingface/trl/blob/main/trl/experimental/gold/gold_config.py): `trl/experimental/gold/gold_config.py`
- [SDPO configuration](https://github.com/huggingface/trl/blob/main/trl/experimental/sdpo/sdpo_config.py): `trl/experimental/sdpo/sdpo_config.py`

### Generation and rewards

- [Reward functions](https://github.com/huggingface/trl/blob/main/trl/rewards/accuracy_rewards.py): `trl/rewards/accuracy_rewards.py`
- [vLLM generation](https://github.com/huggingface/trl/blob/main/trl/generation/vllm_generation.py): `trl/generation/vllm_generation.py`
- [vLLM client](https://github.com/huggingface/trl/blob/main/trl/generation/vllm_client.py): `trl/generation/vllm_client.py`
- [Async vLLM client](https://github.com/huggingface/trl/blob/main/trl/generation/async_vllm_client.py): `trl/generation/async_vllm_client.py`

### Data and model utilities

- [Chat templates](https://github.com/huggingface/trl/blob/main/trl/chat_template_utils.py): `trl/chat_template_utils.py`
- [Dataset utilities](https://github.com/huggingface/trl/blob/main/trl/data_utils.py): `trl/data_utils.py`
- [Model utilities](https://github.com/huggingface/trl/blob/main/trl/models/utils.py): `trl/models/utils.py`

### Training runtime

- [Trainer foundation](https://github.com/huggingface/trl/blob/main/trl/trainer/base_trainer.py): `trl/trainer/base_trainer.py`
- [Distributed training](https://github.com/huggingface/trl/blob/main/trl/distributed.py): `trl/distributed.py`
- [Profiling](https://github.com/huggingface/trl/blob/main/trl/extras/profiling.py): `trl/extras/profiling.py`
- [Chunked log-prob](https://github.com/huggingface/trl/blob/main/trl/kernels/chunked_logprob.py): `trl/kernels/chunked_logprob.py`
- [Gated delta kernel](https://github.com/huggingface/trl/blob/main/trl/kernels/tree_gated_delta_rule.py): `trl/kernels/tree_gated_delta_rule.py`
- [Dependency checks](https://github.com/huggingface/trl/blob/main/trl/import_utils.py): `trl/import_utils.py`

### User interfaces

- [TRL CLI](https://github.com/huggingface/trl/blob/main/trl/cli/main.py): `trl/cli/main.py`
- [CLI training commands](https://github.com/huggingface/trl/blob/main/trl/cli/commands/training.py): `trl/cli/commands/training.py`

### Other components

- **Trainer user**
- **Training dataset**
- **Foundation model**
- **vLLM server**

## Connections

- Trainer user → SFT trainer: starts training (evidence: `README.md`)
- Trainer user → GRPO trainer: starts training (evidence: `README.md`)
- Trainer user → Async GRPO: starts training (evidence: `README.md`)
- Trainer user → DPO trainer: starts training (evidence: `README.md`)
- Trainer user → KTO trainer: starts training (evidence: `README.md`)
- Trainer user → Reward trainer: starts training (evidence: `README.md`)
- Training dataset → SFT trainer: training input (evidence: `README.md`)
- Training dataset → GRPO trainer: training input (evidence: `README.md`)
- Training dataset → Async GRPO: training input (evidence: `README.md`)
- Training dataset → DPO trainer: training input (evidence: `README.md`)
- Training dataset → KTO trainer: training input (evidence: `README.md`)
- Training dataset → Reward trainer: training input (evidence: `README.md`)
- Foundation model → SFT trainer: model input (evidence: `README.md`)
- Foundation model → GRPO trainer: model input (evidence: `README.md`)
- Foundation model → Async GRPO: model input (evidence: `README.md`)
- Foundation model → DPO trainer: model input (evidence: `README.md`)
- Foundation model → KTO trainer: model input (evidence: `README.md`)
- Foundation model → Reward trainer: model input (evidence: `README.md`)
- GRPO trainer → Reward functions: scores with (evidence: `README.md`)
- Async GRPO → Reward functions: scores with (evidence: `README.md`)
- GRPO trainer → vLLM generation: generates with (evidence: `trl/trainer/grpo_trainer.py`)
- vLLM generation → vLLM client: uses client (evidence: `trl/generation/vllm_generation.py`)
- vLLM client → vLLM server: sends requests (evidence: `trl/generation/vllm_client.py`)
- Async GRPO → vLLM server: streams rollouts (evidence: `README.md`)
- Async GRPO → Async vLLM client: uses client (evidence: `trl/trainer/async_grpo_trainer.py`)
- Async vLLM client → vLLM server: checks server (evidence: `trl/generation/async_vllm_client.py`)
- GRPO trainer → Trainer foundation: inherits support (evidence: `trl/trainer/grpo_trainer.py`)
- GRPO trainer → Dataset utilities: uses utilities (evidence: `trl/trainer/grpo_trainer.py`)
- GRPO trainer → Chat templates: uses templates (evidence: `trl/trainer/grpo_trainer.py`)
- GRPO trainer → Model utilities: uses utilities (evidence: `trl/trainer/grpo_trainer.py`)
- GRPO trainer → Distributed training: uses support (evidence: `trl/trainer/grpo_trainer.py`)
- GRPO trainer → Profiling: profiles with (evidence: `trl/trainer/grpo_trainer.py`)
- Distillation trainer → Chunked log-prob: uses kernel (evidence: `trl/trainer/distillation_trainer.py`)
- Async GRPO → Gated delta kernel: uses kernel (evidence: `trl/trainer/async_grpo_trainer.py`)
- Experimental trainers → GOLD configuration: includes config (evidence: `trl/experimental/gold/gold_trainer.py`)
- Experimental trainers → SDPO configuration: includes config (evidence: `trl/experimental/sdpo/sdpo_trainer.py`)
- TRL CLI → CLI training commands: dispatches to (evidence: `trl/cli/main.py`)
- CLI training commands → SFT trainer: launches method (evidence: `README.md`)
- CLI training commands → DPO trainer: launches method (evidence: `README.md`)
