ARG VERSION=mmcv1.6_dtk26.04_v2.0_Hygon
FROM aicompute.tencentcloudcr.com/hyperdrive/hygon:${VERSION}

ARG COS_BASE_URL="https://haihub-model-1251001002.cos.ap-shanghai.myqcloud.com/HyperDrive/haiguang/hygon-bench/train"
ARG PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"
ARG COS_WHL_BASE="https://hygon-thpcbench-1251001002.cos.ap-shanghai.myqcloud.com/hygon/whl"
ARG MMCV_WHL_URL="${COS_WHL_BASE}/mmcv-2.2.0+das.opt1.dtk2604.20260204.gb9d4e636-cp310-cp310-manylinux_2_28_x86_64.whl"
ARG TORCH_WHL_URL="${COS_WHL_BASE}/torch-2.7.1+das.opt1.dtk2604-cp310-cp310-manylinux_2_28_x86_64.whl"
ARG TRITON_WHL_URL="${COS_WHL_BASE}/triton-3.1.0+das.opt1.dtk2604.torch271-cp310-cp310-manylinux_2_28_x86_64.whl"

SHELL ["/bin/bash", "-c"]

# Download Hyhal for compile if the base image does not already provide it.
RUN if [ -e /opt/hyhal/lib ]; then \
        echo "Use existing /opt/hyhal"; \
    else \
        aria2c --enable-color=false --show-console-readout=false -x 8 -s 8 -k 1M -d /tmp -o hyhal.tar.gz ${COS_BASE_URL}/common/hyhal.tar.gz && \
        mkdir -p /opt/hyhal && \
        tar -zxf /tmp/hyhal.tar.gz --strip-components=2 -C /opt/hyhal && \
        rm -rf /tmp/hyhal.tar.gz; \
    fi

COPY PointPillars/mmdetection3d /workspace/PointPillars

# Replace mmcv==1.6 with mmcv==2.2
RUN pip uninstall -y mmcv-full torch triton && \
    pip install "${TORCH_WHL_URL}" && \
    pip install "${TRITON_WHL_URL}" && \
    pip install "${MMCV_WHL_URL}" && \
    pip install -i "${PIP_INDEX_URL}" mmengine==0.10.7 mmdet==3.3.0 numpy==1.25.0 \
        lyft-dataset-sdk==0.0.8 nuscenes-devkit==1.1.11 trimesh==2.35.39 future tensorboard && \
    sed 's/2.2.0/2.2.1/' -i /usr/local/lib/python3.10/dist-packages/mmdet/__init__.py

RUN mv /usr/local/lib/python3.10/dist-packages/mmengine/runner/runner.py /usr/local/lib/python3.10/dist-packages/mmengine/runner/runner.py.bak && \
    wget https://taco-public-1251001002.cos.ap-shanghai.myqcloud.com/external/haiguang/whl/runner.py -P /usr/local/lib/python3.10/dist-packages/mmengine/runner/
    # bash install_deps.sh && \
RUN cd /workspace/PointPillars && \
    source /root/.bashrc && \
    pip install --ignore-installed blinker && \
    pip install -r requirements/runtime.txt && \
    source /root/.bashrc && \
    source /usr/local/bin/fastpt -C && \
    cd /workspace/PointPillars && \
    pip uninstall -y mmdet3d && \
    FORCE_CUDA=1 python setup.py develop --no-deps && \
    (source /usr/local/bin/fastpt -E || true)

# 兼容 THPC 老模板
COPY build.sh /opt/build_pointpillars.sh

COPY one_step_data.sh /workspace/PointPillars/HyperDrive/tools/one_step_data.sh
COPY start_mmdet3d.sh /workspace/PointPillars/tools/start_mmdet3d.sh
COPY pointpillars_hv_fpn_sbn-all_8xb4-2x_nus-3d_perf_test.py /workspace/PointPillars/configs/pointpillars/
COPY schedule-2x_perf_test.py /workspace/PointPillars/configs/_base_/schedules/

RUN rm -f /root/.bash_history

# Remove hyhal for runtime
RUN rm -rf /opt/hyhal

WORKDIR /workspace/PointPillars
