# fundamentalvision/deformable-detr architecture

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

- Interactive diagram: https://gitdiagram.com/fundamentalvision/deformable-detr
- Repository: https://github.com/fundamentalvision/deformable-detr

## Overview

##### Project type and purpose
- This is a research codebase for “Deformable DETR,” an end‑to‑end object detection system built on PyTorch. It provides training, evaluation, custom CUDA operators, and tools for distributed and slurm‐based launches.

##### File structure
- Top‐level scripts:
- main.py / benchmark.py – entry points for training/evaluation/benchmarking
- engine.py – orchestrates the training/evaluation loop
- configs/ – bash scripts encoding hyperparameters and model variants
- datasets/ – data loading, transforms, samplers, COCO‐specific IO and evaluation
- models/ – network definitions (ResNet backbone, Deformable Transformer, heads)
- models/ops/ – custom CUDA/C++ kernels for deformable attention
- tools/ – scripts to launch distributed jobs (single‑node, multi‑node, slurm)
- figs/ and docs/ – illustrations and changelog for reference
- requirements.txt – Python dependencies

##### README for additional insights
- It describes the high‑level architecture (backbone + deformable transformer encoder/decoder + detection head), usage patterns (single vs. multi‑node training, evaluation), and custom operator compilation.

##### Components
a. Main components:
1. Configuration & CLI layer (configs/ + tools/launch scripts)
2. Data layer (datasets/ + transforms + data prefetcher)
3. Model layer (models/):
– Backbone (ResNet)
– Deformable Transformer (encoder + decoder)
– Position encoding
– Matcher (Hungarian matching)
4. Custom ops module (models/ops/): CPU/CUDA kernels for deformable attention
5. Training & evaluation engine (engine.py + main.py)
6. Utilities (logging, checkpointing, plotting)
7. External dependencies: COCO dataset storage, PyTorch, CUDA toolkits

b. Relationships & interactions:
- Config/CLI → invokes main.py → parses config → calls engine
- Engine:
- pulls batches from Data layer → applies transforms → feeds to Model
- Model → uses custom ops for efficient attention → produces predictions
- Engine computes loss (with matcher), backprop via PyTorch → optimizer update
- Checkpointing & logging utilities record metrics
- During inference: same flow minus backward pass; evaluation uses datasets/coco_eval
- Custom ops: compiled at start, loaded by Model layer for forward/backward

c. Architectural patterns & principles:
- Modular separation: data / model / training engine / tools
- Extensibility: config scripts allow many model variants
- Performance optimization: custom low‑level operators; data prefetcher
- Distributed training support via launch scripts

d. Key technologies & libraries:
- PyTorch & torchvision
- CUDA/C++ for custom kernels
- Shell scripts for orchestration
- COCO dataset format

##### Detailed breakdown
- Config & Launch layer: run_dist_launch.sh, run_dist_slurm.sh → feeds to main.py
- Data layer:
- datasets/coco.py, transforms.py, data_prefetcher.py → PyTorch DataLoader
- Model layer:
- models/backbone.py → features → models/deformable_transformer.py → queries → models/deformable_detr.py heads
- models/position_encoding.py, matcher.py, segmentation.py
- Custom ops block:
- models/ops/modules/ms_deform_attn.py (Python binding)
- C++/CUDA src under models/ops/src → compiled via make.sh
- Engine & main:
- engine.py loops epochs, step, validation (calls coco_eval)
- benchmark.py for speed tests
- Utilities:
- util/box_ops.py, misc.py, plot_utils.py
- External:
- COCO dataset directory → annotations + images
- PyTorch & CUDA runtime

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Config & CLI Layer
    subgraph "Config & CLI Layer"
        direction TB
        ConfigScripts["configs/*.sh"]:::tools
        RunLaunch["tools/run_dist_launch.sh"]:::tools
        RunSlurm["tools/run_dist_slurm.sh"]:::tools
        LaunchPy["tools/launch.py"]:::tools
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        COCOIO["datasets/coco.py"]:::data
        COCOEval["datasets/coco_eval.py"]:::data
        PanopticEval["datasets/panoptic_eval.py"]:::data
        Transforms["datasets/transforms.py"]:::data
        Samplers["datasets/samplers.py"]:::data
        Prefetcher["datasets/data_prefetcher.py"]:::data
        Torchvision["datasets/torchvision_datasets/coco.py"]:::data
    end

    %% Model Layer
    subgraph "Model Layer"
        direction TB
        Backbone["models/backbone.py"]:::model
        DeformTransformer["models/deformable_transformer.py"]:::model
        DeformDETR["models/deformable_detr.py"]:::model
        Matcher["models/matcher.py"]:::model
        PositionEnc["models/position_encoding.py"]:::model
        Segmentation["models/segmentation.py"]:::model
    end

    %% Custom Ops Module
    subgraph "Custom Ops Module"
        direction TB
        OpsModule["models/ops/modules/ms_deform_attn.py"]:::ops
        OpsSetup["models/ops/setup.py"]:::ops
        OpsFunc["models/ops/functions/ms_deform_attn_func.py"]:::ops
        OpsBuild["models/ops/make.sh"]:::ops
        CPUCpp["models/ops/src/cpu/ms_deform_attn_cpu.cpp"]:::ops
        CUDACpp["models/ops/src/cuda/ms_deform_attn_cuda.cu"]:::ops
        CUDAH["models/ops/src/cuda/ms_deform_im2col_cuda.cuh"]:::ops
        AttnH["models/ops/src/ms_deform_attn.h"]:::ops
        VisionCPP["models/ops/src/vision.cpp"]:::ops
    end

    %% Engine & Entry Points
    subgraph "Engine & Entry Points"
        direction TB
        MainPy["main.py"]:::tools
        EnginePy["engine.py"]:::tools
        BenchmarkPy["benchmark.py"]:::tools
    end

    %% Utilities
    subgraph "Utilities"
        direction TB
        BoxOps["util/box_ops.py"]:::util
        MiscUtil["util/misc.py"]:::util
        PlotUtil["util/plot_utils.py"]:::util
    end

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        COCOData["COCO Dataset"]:::external
        PyTorch["PyTorch & Torchvision"]:::external
        CUDA["CUDA Toolkit"]:::external
        Checkpoints["Checkpoint & Logs"]:::external
    end

    %% Connections
    ConfigScripts -->|invokes| MainPy
    RunLaunch -->|invokes| MainPy
    RunSlurm -->|invokes| MainPy
    LaunchPy -->|invokes| MainPy

    MainPy -->|calls| EnginePy
    EnginePy -->|loads| ConfigScripts

    EnginePy -->|fetches| COCOIO
    EnginePy -->|applies| Transforms
    Transforms --> Prefetcher
    Prefetcher -->|yields| DeformDETR

    DeformDETR --> DeformTransformer
    DeformTransformer --> Backbone
    DeformDETR --> Matcher
    DeformDETR --> PositionEnc
    DeformDETR --> Segmentation

    DeformTransformer -->|uses| OpsModule
    OpsModule --> OpsFunc
    OpsFunc --> CPUCpp
    OpsFunc --> CUDACpp
    CUDACpp --> CUDAH
    CUDACpp --> AttnH
    OpsSetup --> OpsModule
    OpsBuild --> CPUCpp

    EnginePy -->|logs to| Checkpoints
    EnginePy -->|uses| BoxOps
    EnginePy -->|uses| MiscUtil
    EnginePy -->|uses| PlotUtil
    EnginePy -->|evaluates| COCOEval
    EnginePy -->|evaluates| PanopticEval

    BenchmarkPy -->|runs| DeformDETR

    COCOData --> COCOIO

    MainPy -->|requires| PyTorch
    DeformDETR -->|on GPU via| CUDA

    %% Click Events
    click ConfigScripts "https://github.com/fundamentalvision/deformable-detr/blob/main/configs/r50_deformable_detr*.sh"
    click RunLaunch "https://github.com/fundamentalvision/deformable-detr/blob/main/tools/run_dist_launch.sh"
    click RunSlurm "https://github.com/fundamentalvision/deformable-detr/blob/main/tools/run_dist_slurm.sh"
    click LaunchPy "https://github.com/fundamentalvision/deformable-detr/blob/main/tools/launch.py"
    click COCOIO "https://github.com/fundamentalvision/deformable-detr/blob/main/datasets/coco.py"
    click COCOEval "https://github.com/fundamentalvision/deformable-detr/blob/main/datasets/coco_eval.py"
    click PanopticEval "https://github.com/fundamentalvision/deformable-detr/blob/main/datasets/panoptic_eval.py"
    click Transforms "https://github.com/fundamentalvision/deformable-detr/blob/main/datasets/transforms.py"
    click Samplers "https://github.com/fundamentalvision/deformable-detr/blob/main/datasets/samplers.py"
    click Prefetcher "https://github.com/fundamentalvision/deformable-detr/blob/main/datasets/data_prefetcher.py"
    click Torchvision "https://github.com/fundamentalvision/deformable-detr/blob/main/datasets/torchvision_datasets/coco.py"
    click Backbone "https://github.com/fundamentalvision/deformable-detr/blob/main/models/backbone.py"
    click DeformTransformer "https://github.com/fundamentalvision/deformable-detr/blob/main/models/deformable_transformer.py"
    click DeformDETR "https://github.com/fundamentalvision/deformable-detr/blob/main/models/deformable_detr.py"
    click Matcher "https://github.com/fundamentalvision/deformable-detr/blob/main/models/matcher.py"
    click PositionEnc "https://github.com/fundamentalvision/deformable-detr/blob/main/models/position_encoding.py"
    click Segmentation "https://github.com/fundamentalvision/deformable-detr/blob/main/models/segmentation.py"
    click OpsModule "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/modules/ms_deform_attn.py"
    click OpsSetup "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/setup.py"
    click OpsFunc "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/functions/ms_deform_attn_func.py"
    click OpsBuild "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/make.sh"
    click CPUCpp "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/src/cpu/ms_deform_attn_cpu.cpp"
    click CUDACpp "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/src/cuda/ms_deform_attn_cuda.cu"
    click CUDAH "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/src/cuda/ms_deform_im2col_cuda.cuh"
    click AttnH "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/src/ms_deform_attn.h"
    click VisionCPP "https://github.com/fundamentalvision/deformable-detr/blob/main/models/ops/src/vision.cpp"
    click MainPy "https://github.com/fundamentalvision/deformable-detr/blob/main/main.py"
    click EnginePy "https://github.com/fundamentalvision/deformable-detr/blob/main/engine.py"
    click BenchmarkPy "https://github.com/fundamentalvision/deformable-detr/blob/main/benchmark.py"
    click BoxOps "https://github.com/fundamentalvision/deformable-detr/blob/main/util/box_ops.py"
    click MiscUtil "https://github.com/fundamentalvision/deformable-detr/blob/main/util/misc.py"
    click PlotUtil "https://github.com/fundamentalvision/deformable-detr/blob/main/util/plot_utils.py"

    %% Styles
    classDef data fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef model fill:#90EE90,stroke:#333,stroke-width:1px
    classDef ops fill:#FFA500,stroke:#333,stroke-width:1px
    classDef tools fill:#DDA0DD,stroke:#333,stroke-width:1px
    classDef util fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef external fill:#CCCCCC,stroke:#333,stroke-width:1px
```
