FROM nvcr.io/nvidia/pytorch:22.04-py3

WORKDIR /openmmlab
ARG ONNXRUNTIME_VERSION=1.8.1
ENV DEBIAN_FRONTEND=noninteractive \
    APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn \
    FORCE_CUDA="1"

RUN apt-key del 7fa2af80 \
    && apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/3bf863cc.pub \
    && apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/7fa2af80.pub

# (Optional)
# RUN sed -i 's/http:\/\/archive.ubuntu.com\/ubuntu\//http:\/\/mirrors.aliyun.com\/ubuntu\//g' /etc/apt/sources.list \
# && pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

RUN apt-get update \
    && apt-get install -y ffmpeg git libgl1-mesa-glx libopencv-dev \
    libsm6 libspdlog-dev libssl-dev ninja-build libxext6 libxrender-dev \
    libglib2.0-0 vim wget --no-install-recommends \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# get onnxruntime
RUN wget -q https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION}/onnxruntime-linux-x64-${ONNXRUNTIME_VERSION}.tgz \
    && tar -zxvf onnxruntime-linux-x64-${ONNXRUNTIME_VERSION}.tgz \
    && pip install --no-cache-dir onnxruntime-gpu==${ONNXRUNTIME_VERSION} \
    && pip install pycuda


# Install OPENMIM MMENGINE MMDET
RUN pip install --no-cache-dir openmim \
    && mim install --no-cache-dir "mmengine>=0.6.0" "mmdet>=3.0.0,<4.0.0" \
    && mim install --no-cache-dir opencv-python==4.5.5.64 opencv-python-headless==4.5.5.64

RUN git clone https://github.com/open-mmlab/mmcv.git -b 2.x mmcv \
    && cd mmcv \
    && mim install --no-cache-dir -r requirements/optional.txt \
    && MMCV_WITH_OPS=1 mim install --no-cache-dir -e . -v \
    && cd ..

# Install MMYOLO
RUN git clone https://github.com/open-mmlab/mmyolo.git -b dev mmyolo \
    && cd mmyolo \
    && mim install --no-cache-dir -e . \
    && cd ..

# Install MMDEPLOY
ENV ONNXRUNTIME_DIR=/openmmlab/onnxruntime-linux-x64-${ONNXRUNTIME_VERSION} \
    TENSORRT_DIR=/usr/lib/x86_64-linux-gnu \
    CUDNN_DIR=/usr/lib/x86_64-linux-gnu

RUN git clone https://github.com/open-mmlab/mmdeploy -b dev-1.x mmdeploy \
    && cd mmdeploy \
    && git submodule update --init --recursive \
    && mkdir -p build \
    && cd build \
    && cmake -DMMDEPLOY_TARGET_BACKENDS="ort;trt" -DONNXRUNTIME_DIR=${ONNXRUNTIME_DIR} -DTENSORRT_DIR=${TENSORRT_DIR} -DCUDNN_DIR=${CUDNN_DIR} .. \
    && make -j$(nproc) \
    && make install \
    && cd .. \
    && mim install --no-cache-dir -e .

# Fix undefined symbol bug
    RUN echo -e "\nexport LD_LIBRARY_PATH=${ONNXRUNTIME_DIR}/lib:${TENSORRT_DIR}/lib:${CUDNN_DIR}/lib64:${LD_LIBRARY_PATH}\nldconfig" >> /root/.bashrc
