This file provides detailed technical context for AI coding agents working with RF-DETR.
Canonical Sources:
- Contribution Guidelines: CONTRIBUTING.md - The authoritative source for all contribution practices
- Human Documentation: README.md - Project overview and usage
- Copilot Instructions: .github/copilot-instructions.md - GitHub Copilot-specific guidance
This document supplements the contribution guidelines with detailed technical information for automated tooling.
As an AI agent contributing to RF-DETR, you are responsible for:
-
Following test-driven development practices
- Write failing tests first for bug fixes
- Write comprehensive tests for new features
- Ensure final PR commit has all tests passing
-
Adhering to code quality standards
- Run
pre-commit run --all-filesbefore every commit - Follow type hint and docstring requirements
- Prefer direct project imports; conventional third-party aliases are allowed
- Run
-
Maintaining agentic documentation
- Update
AGENTS.mdwhen architecture patterns or technical conventions change - Update
.github/copilot-instructions.mdwhen high-level guidance changes - Update
.github/CONTRIBUTING.mdwhen human workflow is affected - Apply updates after receiving major feedback in PR reviews
- Update
-
Consulting maintainers before major changes
- Open an issue before adding new models or significant features
- Wait for approval on approach before implementing
-
Writing secure, minimal code
- Avoid over-engineering and unnecessary abstractions
- Write secure code (prevent injection vulnerabilities)
- Follow existing patterns in the codebase
Note
Keeping documentation current ensures consistency across agent contributions and reduces repeated feedback on the same issues.
Note
Canonical Reference: See Development Environment Setup in CONTRIBUTING.md for complete setup instructions.
# Install uv (if not already installed)
pip install uv
# Full development environment (always use this)
uv sync --all-groupsPrerequisites: Python >=3.10 (tested on 3.10-3.13)
See pyproject.toml for complete dependency specifications:
- Core: PyTorch, torchvision, transformers, supervision, pydantic, pyDeprecate
- Optional:
[data](WebDataset streaming reader),[train](minimal training loop dependencies, including both COCO evaluation backends selectable viaTrainConfig.eval_backend)),[augment](custom Albumentations CPU augmentations and Kornia GPU augmentations),[lora](LoRA fine-tuning),[plus](Plus models),[onnx](ONNX export),[loggers](tensorboard, wandb, mlflow, clearml) - Development:
tests,docs,buildgroups
Important version constraints:
- PyTorch: >=2.2.0, <3.0.0
- Transformers: >=5.0.0, <6.0.0
Note
Canonical Reference: See Test-Driven Development in CONTRIBUTING.md for complete guidelines.
CI Workflows (Source of Truth): See .github/workflows/ci-tests-cpu.yml and .github/workflows/ci-tests-gpu.yml for exact test commands used in CI.
# CPU tests (default for local development; mirrors CI)
uv run --no-sync pytest src/ tests/ -n 1 -m "not gpu" --ignore=tests/run_smoke_all_models.py --ignore=tests/legacy/test_checkpoint_compat.py --cov=rfdetr --cov-report=xml --timeout=240 --durations=50
# GPU tests (requires GPU; mirrors CI)
uv run --no-sync pytest tests/ -m gpu --ignore=tests/legacy/test_checkpoint_compat.py -n 2 --reruns 1 --only-rerun "OutOfMemoryError" --cov=rfdetr --cov-report=xml --timeout=600 --durations=20
# Pre-commit checks (ALWAYS run before committing)
pre-commit run --all-filesImportant
Testing Requirements:
⚠️ During development: Tests may fail as you work through TDD cycle- ✅ Before opening PR: Final commit MUST have all tests passing
- ✅ Before each commit: Run
pre-commit run --all-files
Test-Driven Development:
- Bug fixes: Write failing test → Fix code → Verify all tests pass
- New features: Write comprehensive tests → Implement feature → Refactor
Test Organization:
- Group related tests in classes
- Use
pytest.param(..., id="name")only for a function, object, compound setup passed as one case, a per-case mark, or when the raw value would produce an unclear/empty ID (e.g.,""); use bare string/number/boolean/Nonevalues otherwise, and do not maintain a parallelidslist - Mark GPU/heavy tests with
@pytest.mark.gpu - Avoid multiple validation cases in a single test - see CONTRIBUTING.md for details
- Fixtures return ready-to-use concrete state or a cohesive tuple of related state. Do not return a callable factory unless fixture-managed lifecycle is required; use an ordinary helper function for configurable construction.
- Keep fixture dependencies minimal, unpack only the values a test needs, and avoid aliases or wrappers that merely rename or forward an object without adding meaning.
CI Information: See CI Testing in CONTRIBUTING.md for details on OS/Python version matrix and workflow configurations.
Note
Canonical Reference: See Code Quality and Linting in CONTRIBUTING.md for setup and details.
# Always run full pre-commit (not individual tools)
pre-commit run --all-filesTip
Pre-commit hooks will auto-format many issues. Review changes and re-stage files.
Configuration Files:
.pre-commit-config.yaml- Pre-commit hooks (ruff, mdformat, prettier, codespell, license headers)pyproject.toml- Ruff linting rules ([tool.ruff]section)
Abstraction Discipline:
- Introduce an abstraction only when it reduces cognitive load and the number of concepts a reader must follow. Extract stable repeated behavior or irrelevant construction mechanics while keeping behavior-defining inputs and outcomes explicit at call sites.
- Design an extracted helper for the complete related behavior already present, including relevant edge cases, and place it in the narrowest scope shared by its consumers. Prefer small visible duplication over a helper, wrapper, alias, or layer that adds indirection without semantic value.
License Header (required for all Python files):
# ------------------------------------------------------------------------
# RF-DETR
# Copyright (c) 2025 Roboflow. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
# ------------------------------------------------------------------------# Full install (matches CI — required for XLarge/2XLarge model pages)
uv pip install -e ".[plus]" --group docs
# Serve locally (live reload)
uv run mkdocs serve
# Build static site
uv run mkdocs buildDocumentation Structure:
- Source:
docs/directory (Markdown) - Config:
mkdocs.yaml(uses custom YAML tags:!!python/name) - Deployment: GitHub Actions publishes to GitHub Pages
Note: mkdocs.yaml is checked by the check-yaml pre-commit hook with --unsafe so custom YAML tags such as !!python/name are accepted.
# Install build dependencies
uv sync --group build
# Build distributions
uv build
# Validate build
uv run twine check --strict dist/*Build outputs:
- Source distribution:
dist/rfdetr-*.tar.gz - Wheel:
dist/rfdetr-*.whl
Note
Canonical Reference: See Project Structure in CONTRIBUTING.md for complete project organization, directory descriptions, and configuration files.
Quick summary: src/rfdetr/ (source code), tests/ (test suite), docs/ (documentation), .github/ (CI/CD), pyproject.toml (dependencies and config).
Internal package organization within src/rfdetr/ is subject to change as this is an active research and development project.
Augmentations:
- Training uses torchvision-native transforms unless Albumentations is installed —
augmentation_backend="cpu"(the default) then auto-selects Albumentations and injects the defaultAUG_CONFIG, even whenaug_config=None. Identical training code therefore resolves differently across environments; passaugmentation_backend="torchvision"to pin the torchvision pipeline regardless of what is installed. This backend selection only reaches the dataset builders:_route_transformschooses Albumentations only forimage_set == "train", so validation always stays on torchvision, and prediction (src/rfdetr/detr.py) and export (src/rfdetr/export/main.py) call torchvision preprocessing directly — do not change inference/export behavior based on the training backend. - Custom non-empty
aug_configvalues on the CPU path use Albumentations and requirerfdetr[augment]. augmentation_backend="auto"resolves to Kornia when CUDA and Kornia are available, falling back to CPU otherwise;augmentation_backend="gpu"pins Kornia and requiresrfdetr[augment].
Model Architecture:
- RFDETR wrappers:
self.modelis the model context returned byget_model() - Underlying PyTorch module:
self.model.model - Segmentation models return
pred_masksastorch.Tensoror dict with keys['spatial_features', 'query_features', 'bias']
Model Selection (examples, docs, tests, defaults):
- Default to
RFDETRSmall/"rfdetr-small". Use it wherever an example needs a concrete detection model. - Never use base models (
RFDETRBase/"rfdetr-base") in new examples, docs, or tests — treat as deprecated; substitutesmall. - Released detection sizes —
nano,small,medium,large(plusxlarge/2xlargePlus models). Always pick one of these for plain object detection; never a-previewvariant. - Released segmentation sizes —
RFDETRSegNano/Small/Medium/Large/"rfdetr-seg-{nano,small,medium,large}"(plusxlarge/2xlarge). Use a sized seg model for segmentation;RFDETRSegPreview/"rfdetr-seg-preview"is now superseded — do not use it in new examples, docs, or tests. -previewvariants are for capabilities with no released sized version yet. Only keypoints remain preview-only:RFDETRKeypointPreview/"rfdetr-keypoint-preview". Use a preview variant only for that task — never as a stand-in for detection or segmentation.
Imports:
- Keep imports at module scope by default. Use a local import only for a verified circular-import boundary, optional dependency boundary, import-behavior test, or material startup/side-effect constraint; the reason must be evident from the surrounding code or documented where it is not obvious.
# Prefer direct project imports. Standard aliases such as `numpy as np`,
# `torch.nn.functional as F`, and lazy module aliases are allowed when conventional.
from rfdetr.utilities.distributed import get_rank, get_world_size, is_main_process, save_on_master
from rfdetr.utilities.logger import get_logger
# Logger usage
logger = get_logger() # Default name: "rf-detr", reads LOG_LEVEL env var
# TQDM (environment compatibility)
from tqdm.auto import tqdm # NOT: from tqdm import tqdmPlus Models (XLarge, 2XLarge):
- Requires separate
rfdetr_pluspackage (PML 1.0 license) - Import handled lazily via
__getattr__insrc/rfdetr/platform/models.py - Raises
ImportErrorif package not installed
Subprocess Usage:
import subprocess
result = subprocess.run(
["command", "arg1", "arg2"],
check=True, # Raise CalledProcessError on failure
text=True, # Return stdout/stderr as strings
capture_output=True,
)
# Note: stderr is already a string, don't decodeLogging:
- Use
logger.debug()for detailed tensor/shape information (notlogger.info()) - Use
logger.info()for high-level progress/status
Checkpoint Handling:
- Always check file existence before operations
- Prevents errors when training is interrupted
Important
Canonical Reference: See Google-Style Docstrings and Mandatory Type Hints in CONTRIBUTING.md for complete requirements and examples.
Requirements:
- MANDATORY type hints for all function parameters and return types
- MANDATORY Google-style docstrings for all functions and classes
- Do not duplicate types in docstrings - types are in the function signature
- Target Python version: 3.10+
- Helper functions in
tests/need a doctest too: any non-test_*function used by tests (fixture builders, assertion helpers, reference implementations) needs a docstring with anExamplesdoctest that exercises it directly —pyproject.tomlruns--doctest-plusacrosstests/on purpose. Skip the live doctest (# doctest: +SKIP+ one-line reason) only when the helper can't run standalone (e.g. a@pytest.fixture, or needs real GPU/XLA/network hardware).
- Setup:
uv sync --all-groups - Before changes: Run tests to establish baseline
- Development:
- Make minimal, focused changes
- Follow existing patterns and conventions
- Add type hints and docstrings
- Testing:
- Bug fixes: Write test first, then fix
- Features: Test all major use cases
- Run:
uv run --no-sync pytest src/ tests/ -n 2 -m "not gpu" --ignore=tests/run_smoke_all_models.py --ignore=tests/legacy/test_checkpoint_compat.py --timeout=240 --durations=50
- Quality checks:
pre-commit run --all-files - Build (if needed):
uv build - Commit: Pre-commit hooks run automatically
Important
Canonical Reference: See Adding a New Model in CONTRIBUTING.md for detailed guidance.
Always consult maintainers before implementing new models.
- Write secure code: Avoid injection vulnerabilities (XSS, SQL injection, command injection)
- Validate inputs: Especially for file paths, URLs, and user-provided data
- No credentials: Never commit API keys, tokens, or credentials
- Follow OWASP best practices
GitHub Actions workflows in .github/workflows/:
- ci-tests-cpu.yml: CPU tests across OS/Python versions
- ci-tests-gpu.yml: GPU-dependent tests
- ci-github-tests.yml: Tests and doctests for the helper scripts under
.github/scripts/, which the CPU/GPU suites never collect; their tests live in.github/_tests/ - ci-legacy-checkpoints.yml: Backward-compatibility checkpoint-loading tests across historical rfdetr releases (advisory only — not a required check; a compat break does not block merge)
- ci-deps-resolution.yml: Dependency resolution (
uv lock) plus an install-plan check (uv sync --dry-run) for every extra on every Python interpreter allowed by requires-python (3.10-3.14). Resolution alone does not prove a pinned version ships a wheel for the interpreter in use. Thelist-extrasjob derives the checked set from every[project.optional-dependencies]extra, so a new extra is covered automatically - build-package.yml: Build and validate distributions
- ci-build-docs.yml: Documentation builds
- publish-docs.yml: Deploy docs to GitHub Pages
Concurrency: PRs cancel in-progress runs on new pushes
- Documentation: https://rfdetr.roboflow.com
- Repository: https://github.com/roboflow/rf-detr
- Issues: https://github.com/roboflow/rf-detr/issues
- Discord: https://discord.gg/GbfgXGJ8Bk
- Contributing:
.github/CONTRIBUTING.md - Copilot Instructions:
.github/copilot-instructions.md
Note: This file is designed for AI coding agents. For human-readable project information, see README.md. For contribution guidelines, see CONTRIBUTING.md.