# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license

# Builds ultralytics/ultralytics:latest-amd image on DockerHub https://hub.docker.com/r/ultralytics/ultralytics
# Image is ROCm-optimized for YOLO single/multi-GPU training and inference on AMD GPUs

# Start FROM ROCm PyTorch image https://hub.docker.com/r/rocm/pytorch
# See https://hub.docker.com/r/rocm/pytorch/tags for available tags
FROM rocm/pytorch:rocm10.0_ubuntu24.04_py3.12_pytorch_release_2.13.0

# Set environment variables
# Suppress TensorFlow and PyTorch warnings
ENV PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_BREAK_SYSTEM_PACKAGES=1 \
    OMP_NUM_THREADS=1 \
    TF_CPP_MIN_LOG_LEVEL=3 \
    TORCH_CPP_LOG_LEVEL=ERROR

# Downloads to user config dir
ADD https://github.com/ultralytics/assets/releases/download/v0.0.0/Arial.ttf \
    https://github.com/ultralytics/assets/releases/download/v0.0.0/Arial.Unicode.ttf \
    /root/.config/Ultralytics/

# Install linux packages
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    gcc git zip unzip wget curl htop && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Create working directory
WORKDIR /ultralytics

# Copy contents and configure dependencies
COPY . .
RUN sed -i'' -e 's/"opencv-python/"opencv-python-headless/' pyproject.toml
ADD https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo26n.pt .

# Install pip packages into the base image's /opt/venv, which already holds ROCm PyTorch (no --system)
# migraphx-libs provides the MIGraphX EP plugin's C library; libamdhip64.so.7 comes from the base image
RUN python -m pip install --no-cache-dir uv && \
    uv pip install --no-cache -e "." albumentations faster-coco-eval && \
    uv pip install --no-cache "onnxruntime-ep-migraphx==1.0.0+rocm10.0.0" "migraphx-libs==2.17.0+rocm10.0.0" \
      --extra-index-url https://stable.repo.amd.com/rocm/onnxruntime/whl-next/ \
      --extra-index-url https://stable.repo.amd.com/rocm/migraphx/whl-next/ --index-strategy unsafe-best-match && \
    uv pip install --no-cache "onnx>=1.12.0,<2.0.0" "onnxslim>=0.1.82" && \
    rm -rf tmp /root/.config/Ultralytics/persistent_cache.json

# Usage --------------------------------------------------------------------------------------------------------------

# Production builds: https://github.com/ultralytics/ultralytics/blob/main/.github/workflows/docker.yml
# Example (build): t=ultralytics/ultralytics:latest-amd && docker build -f docker/Dockerfile-amd -t $t .
# Example (push): docker push $t
# Example (pull): t=ultralytics/ultralytics:latest-amd && docker pull $t
# Example (run-gpu): docker run -it --ipc=host --device=/dev/kfd --device=/dev/dri --group-add video $t
# Example (run-gpu-subset): docker run -it --ipc=host --device=/dev/kfd --device=/dev/dri --group-add video -e ROCR_VISIBLE_DEVICES=2,3 $t
# Note: ROCR_VISIBLE_DEVICES=2,3 maps to device 0,1 inside the container.
# Example (run-with-volume): docker run -it --ipc=host --device=/dev/kfd --device=/dev/dri --group-add video -v "$PWD/shared/datasets:/datasets" $t
