# rt-detrs/rt-detrv4 architecture

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

- Interactive diagram: https://gitdiagram.com/rt-detrs/rt-detrv4
- Repository: https://github.com/rt-detrs/rt-detrv4

## Overview

#### Purpose and entry points
RT-DETRv4 is a PyTorch real-time object-detection training and deployment repository. Its primary path is `train.py`, driven by hierarchical YAML configurations such as `configs/rtv4/rtv4_hgnetv2_s_coco.yml`. The same framework can reproduce RT-DETRv2, D-FINE, and DEIM model families through their config sets.

#### Configuration and object assembly
The configuration/workspace layer resolves YAML inheritance, runtime settings, and named component construction. This separates experiment definitions from Python implementations and permits model, dataset, optimizer, and solver substitution.
- `engine/core/yaml_config.py`
- `engine/core/workspace.py`
- `configs/runtime.yml`

#### Data ingestion and evaluation boundary
Detection data enters through configurable PyTorch dataloaders, COCO/VOC dataset adapters, augmentations, and batch collation. COCO-format custom datasets are the main supported custom-data boundary; evaluators calculate detection metrics from model predictions.
- `engine/data/dataloader.py`
- `engine/data/dataset/coco_dataset.py`
- `engine/data/transforms/`

#### Detector architecture
The RT-DETRv4 path composes a visual backbone, hybrid encoder, transformer-style decoder, matching/criterion loss, and prediction postprocessing. Backbones include HGNetv2 and ResNet/CSP variants; selected configuration determines the concrete graph.
- `engine/backbone/hgnetv2.py`
- `engine/rtv4/rtv4.py`
- `engine/rtv4/hybrid_encoder.py`

#### Foundation-model distillation
A central RT-DETRv4-specific training stage loads an external DINOv2 or DINOv3 vision foundation model as teacher, producing representations used by the student detector’s loss path. Teacher source and checkpoint locations are supplied by config; DINOv3 code/weights are external prerequisites rather than vendored infrastructure.
- `engine/rtv4/dinov3_teacher.py`
- `engine/rtv4/dinov2_teacher.py`
- `configs/rtv4/rtv4_hgnetv2_l_coco.yml`

#### Training, distributed execution, and optimization
`train.py` delegates task lifecycle to detection solver/engine classes: distributed initialization, train/eval loops, checkpoint restore, AMP, EMA, schedulers, and optimizer updates. Runtime assumes CUDA-capable PyTorch; documented execution uses `torchrun` across GPUs.
- `train.py`
- `engine/solver/det_solver.py`
- `engine/optim/amp.py`

#### Inference and deployment outputs
Trained checkpoints can run through native PyTorch inference or be exported to ONNX, then consumed by ONNX Runtime, OpenVINO, or TensorRT. TensorRT engines are also benchmarked for latency; these are downstream execution targets, not required for core training.
- `tools/deployment/export_onnx.py`
- `tools/inference/torch_inf.py`
- `tools/inference/trt_inf.py`

Main entry points: [`engine/solver/det_solver.py`](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/solver/det_solver.py) (Detection solver), [`engine/core/workspace.py`](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/core/workspace.py) (Component workspace), [`engine/core/yaml_config.py`](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/core/yaml_config.py) (YAML resolver), [`engine/rtv4/rtv4.py`](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/rtv4/rtv4.py) (RT-DETRv4 detector)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_config["Configuration"]
  node_yaml_config["YAML resolver<br/>configuration<br/>[yaml_config.py]"]
  node_workspace["Component workspace<br/>object factory<br/>[workspace.py]"]
  node_runtime_config["Runtime defaults<br/>YAML config<br/>[runtime.yml]"]
  node_rtv4_config["RT-DETRv4 experiment<br/>YAML config"]
end

subgraph group_data["Data &amp; Evaluation"]
  node_dataloader["Detection dataloader<br/>PyTorch input<br/>[dataloader.py]"]
  node_coco_dataset["COCO/VOC adapters<br/>dataset boundary<br/>[coco_dataset.py]"]
  node_transforms["Augmentations<br/>data transforms"]
end

subgraph group_model["Detector &amp; Distillation"]
  node_detector["RT-DETRv4 detector<br/>student detector<br/>[rtv4.py]"]
  node_backbone["Visual backbone<br/>feature extractor<br/>[hgnetv2.py]"]
  node_hybrid_encoder["Hybrid encoder<br/>feature encoder<br/>[hybrid_encoder.py]"]
  node_teacher["DINO foundation teacher<br/>external teacher model<br/>[dinov3_teacher.py]"]
end

subgraph group_training["Training Runtime"]
  node_train["Training entry point<br/>PyTorch CLI<br/>[train.py]"]
  node_solver["Detection solver<br/>training engine<br/>[det_solver.py]"]
  node_amp["AMP execution<br/>mixed precision<br/>[amp.py]"]
  node_checkpoint["Trained checkpoint<br/>model artifact"]
end

subgraph group_deployment["Inference &amp; Deployment"]
  node_torch_inference["PyTorch inference<br/>native inference<br/>[torch_inf.py]"]
  node_onnx_export["ONNX export<br/>model export<br/>[export_onnx.py]"]
  node_onnx_targets["ONNX deployment targets<br/>deployment runtime"]
  node_trt_inference["TensorRT inference<br/>benchmark runtime<br/>[trt_inf.py]"]
end

node_runtime_config -->|"default settings"| node_yaml_config
node_rtv4_config -->|"experiment recipe"| node_yaml_config
node_yaml_config -->|"resolved definitions"| node_workspace
node_train -->|"loads config"| node_yaml_config
node_workspace -->|"constructs"| node_dataloader
node_workspace -->|"constructs"| node_detector
node_workspace -->|"constructs"| node_teacher
node_workspace -->|"constructs"| node_solver
node_coco_dataset -->|"samples"| node_transforms
node_transforms -->|"batches"| node_dataloader
node_train -->|"runs"| node_solver
node_dataloader -->|"training/eval batches"| node_solver
node_backbone -->|"visual features"| node_hybrid_encoder
node_hybrid_encoder -->|"encoded features"| node_detector
node_teacher -->|"distillation representations"| node_detector
node_detector -->|"predictions and losses"| node_solver
node_amp -->|"mixed precision"| node_solver
node_solver -->|"saves/restores"| node_checkpoint
node_checkpoint -->|"loads"| node_torch_inference
node_checkpoint -->|"exports"| node_onnx_export
node_onnx_export -->|"ONNX model"| node_onnx_targets
node_onnx_targets -->|"TensorRT engine"| node_trt_inference

click node_train "https://github.com/rt-detrs/rt-detrv4/blob/main/train.py"
click node_yaml_config "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/core/yaml_config.py"
click node_workspace "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/core/workspace.py"
click node_runtime_config "https://github.com/rt-detrs/rt-detrv4/blob/main/configs/runtime.yml"
click node_rtv4_config "https://github.com/rt-detrs/rt-detrv4/blob/main/configs/rtv4/rtv4_hgnetv2_l_coco.yml"
click node_dataloader "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/data/dataloader.py"
click node_coco_dataset "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/data/dataset/coco_dataset.py"
click node_detector "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/rtv4/rtv4.py"
click node_backbone "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/backbone/hgnetv2.py"
click node_hybrid_encoder "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/rtv4/hybrid_encoder.py"
click node_teacher "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/rtv4/dinov3_teacher.py"
click node_solver "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/solver/det_solver.py"
click node_amp "https://github.com/rt-detrs/rt-detrv4/blob/main/engine/optim/amp.py"
click node_torch_inference "https://github.com/rt-detrs/rt-detrv4/blob/main/tools/inference/torch_inf.py"
click node_onnx_export "https://github.com/rt-detrs/rt-detrv4/blob/main/tools/deployment/export_onnx.py"
click node_trt_inference "https://github.com/rt-detrs/rt-detrv4/blob/main/tools/inference/trt_inf.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_yaml_config,node_workspace,node_runtime_config,node_rtv4_config toneBlue
class node_dataloader,node_coco_dataset,node_transforms toneAmber
class node_detector,node_backbone,node_hybrid_encoder,node_teacher toneMint
class node_train,node_solver,node_amp,node_checkpoint toneRose
class node_torch_inference,node_onnx_export,node_onnx_targets,node_trt_inference toneIndigo
```

## Components

### Configuration

- [YAML resolver](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/core/yaml_config.py): configuration · Resolves inherited experiment settings. · `engine/core/yaml_config.py`
- [Component workspace](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/core/workspace.py): object factory · Builds named model, data, and solver components. · `engine/core/workspace.py`
- [Runtime defaults](https://github.com/rt-detrs/rt-detrv4/blob/main/configs/runtime.yml): YAML config · `configs/runtime.yml`
- [RT-DETRv4 experiment](https://github.com/rt-detrs/rt-detrv4/blob/main/configs/rtv4/rtv4_hgnetv2_l_coco.yml): YAML config · Selects the detector, teacher, and training recipe. · `configs/rtv4/rtv4_hgnetv2_l_coco.yml`

### Data & Evaluation

- [Detection dataloader](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/data/dataloader.py): PyTorch input · `engine/data/dataloader.py`
- [COCO/VOC adapters](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/data/dataset/coco_dataset.py): dataset boundary · COCO format is the primary custom-data interface. · `engine/data/dataset/coco_dataset.py`
- **Augmentations**: data transforms

### Detector & Distillation

- [RT-DETRv4 detector](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/rtv4/rtv4.py): student detector · Composes backbone, encoder, decoder, loss, and postprocessing. · `engine/rtv4/rtv4.py`
- [Visual backbone](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/backbone/hgnetv2.py): feature extractor · HGNetv2 and other configured backbone variants. · `engine/backbone/hgnetv2.py`
- [Hybrid encoder](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/rtv4/hybrid_encoder.py): feature encoder · `engine/rtv4/hybrid_encoder.py`
- [DINO foundation teacher](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/rtv4/dinov3_teacher.py): external teacher model · Configured DINOv2/DINOv3 checkpoints provide distillation representations. · `engine/rtv4/dinov3_teacher.py`

### Training Runtime

- [Training entry point](https://github.com/rt-detrs/rt-detrv4/blob/main/train.py): PyTorch CLI · `train.py`
- [Detection solver](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/solver/det_solver.py): training engine · Owns distributed train/eval lifecycle and checkpointing. · `engine/solver/det_solver.py`
- [AMP execution](https://github.com/rt-detrs/rt-detrv4/blob/main/engine/optim/amp.py): mixed precision · `engine/optim/amp.py`
- **Trained checkpoint**: model artifact

### Inference & Deployment

- [PyTorch inference](https://github.com/rt-detrs/rt-detrv4/blob/main/tools/inference/torch_inf.py): native inference · `tools/inference/torch_inf.py`
- [ONNX export](https://github.com/rt-detrs/rt-detrv4/blob/main/tools/deployment/export_onnx.py): model export · `tools/deployment/export_onnx.py`
- **ONNX deployment targets**: deployment runtime · ONNX Runtime, OpenVINO, or TensorRT consume exported models.
- [TensorRT inference](https://github.com/rt-detrs/rt-detrv4/blob/main/tools/inference/trt_inf.py): benchmark runtime · Runs and benchmarks TensorRT engines. · `tools/inference/trt_inf.py`

## Connections

- Runtime defaults → YAML resolver: default settings
- RT-DETRv4 experiment → YAML resolver: experiment recipe
- YAML resolver → Component workspace: resolved definitions
- Training entry point → YAML resolver: loads config
- Component workspace → Detection dataloader: constructs
- Component workspace → RT-DETRv4 detector: constructs
- Component workspace → DINO foundation teacher: constructs
- Component workspace → Detection solver: constructs
- COCO/VOC adapters → Augmentations: samples
- Augmentations → Detection dataloader: batches
- Training entry point → Detection solver: runs
- Detection dataloader → Detection solver: training/eval batches
- Visual backbone → Hybrid encoder: visual features
- Hybrid encoder → RT-DETRv4 detector: encoded features
- DINO foundation teacher → RT-DETRv4 detector: distillation representations
- RT-DETRv4 detector → Detection solver: predictions and losses
- AMP execution → Detection solver: mixed precision
- Detection solver → Trained checkpoint: saves/restores
- Trained checkpoint → PyTorch inference: loads
- Trained checkpoint → ONNX export: exports
- ONNX export → ONNX deployment targets: ONNX model
- ONNX deployment targets → TensorRT inference: TensorRT engine
