# Build-time arguments
FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.3.rc1-910b-ubuntu22.04-py3.11
ARG PYTHON_SITE_PACKAGES=/usr/local/miniconda/lib/python3.11/site-packages
USER root
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
ENV LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/common:/usr/local/Ascend/driver/lib64/driver:$LD_LIBRARY_PATH
ENV ASCEND_RUNTIME=/usr/local/Ascend/ascend-toolkit/latest
ENV PATH=$ASCEND_RUNTIME/bin:$PATH
ENV ASCEND_CUSTOM_OPP_PATH=${PYTHON_SITE_PACKAGES}/mindiesd/ops/vendors/customize:${PYTHON_SITE_PACKAGES}/mindiesd/ops/vendors/aie_ascendc:${ASCEND_CUSTOM_OPP_PATH}

# License
RUN echo "torch-onnx-inference 1.0" > /agreement.txt && \
    echo "Container image Copyright (c) 2026, Huawei Technologies Co., Ltd. All rights reserved." >> /agreement.txt && \
    echo "" >> /agreement.txt && \
    echo "This container image and its contents are governed by the Huawei Container License Agreement (\"License\")." >> /agreement.txt && \
    echo "By pulling and using the container, you accept the terms and conditions of this License." >> /agreement.txt && \
    echo "A copy of this License is made available in this container at: https://www.hiascend.com/en/legal/ascendhub-download" >> /agreement.txt && \
    echo "Note: You agree and undertake that when using Huawei or third-party software in this image, you will comply" >> /agreement.txt && \
    echo "with the license agreement of the corresponding Huawei or third-party software." >> /agreement.txt

RUN sed -i 's|http://ports.ubuntu.com/ubuntu-ports/|https://mirrors.huaweicloud.com/ubuntu-ports/|g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
    ca-certificates wget curl vim git build-essential libnuma-dev \
    libgl1-mesa-glx libglib2.0-0 \
    && rm -rf /var/lib/apt/lists/*

# Install Conda
ENV PATH=/usr/local/miniconda/bin:$PATH
RUN wget -q https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh -O /tmp/miniconda.sh && \
    bash /tmp/miniconda.sh -b -p /usr/local/miniconda && \
    rm -f /tmp/miniconda.sh && \
    conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main && \
    conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r && \
    conda install -y python=3.11 numpy=1.26.4 && \
    conda clean -ya && \
    conda config --set auto_activate_base false

RUN mkdir -p /root/.pip && \
    echo "[global]" > /root/.pip/pip.conf && \
    echo "index-url=https://repo.huaweicloud.com/repository/pypi/simple" >> /root/.pip/pip.conf && \
    echo "trusted-host=repo.huaweicloud.com" >> /root/.pip/pip.conf && \
    echo "timeout=120" >> /root/.pip/pip.conf

# Install PyTorch and torch_npu
RUN mkdir -p /opt/package && \
    wget -q https://download-r2.pytorch.org/whl/cpu/torch-2.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl -P /opt/package && \
    pip3 install /opt/package/torch-2.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl --no-cache-dir && \
    wget -q https://gitcode.com/Ascend/pytorch/releases/download/v7.2.0-pytorch2.1.0/torch_npu-2.1.0.post17-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl -P /opt/package && \
    pip3 install /opt/package/torch_npu-2.1.0.post17-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl --no-cache-dir && \
    rm -rf /opt/package/*

# install mindiesd
RUN pip3 install --no-cache-dir \
    --trusted-host ascend.devcloud.huaweicloud.com \
    -i https://ascend.devcloud.huaweicloud.com/pypi/simple/ \
    mindiesd

# Install ONNX
RUN pip3 install --no-cache-dir --upgrade pip wheel && \
    pip3 install --no-cache-dir setuptools==69.5.1 && \
    pip3 install --no-cache-dir --no-warn-conflicts \
    attrs cloudpickle decorator psutil scipy tornado pandas absl-py ml-dtypes pyyaml && \
    pip3 install --no-cache-dir --no-warn-conflicts \
    onnx==1.16.1 onnxruntime==1.15.1 onnxslim==0.1.93 opencv-python-headless==4.11.0.86

# Install ais_bench & aclruntime
RUN mkdir -p /opt/package && \
    wget -q https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/ait/aclruntime-0.0.2-cp311-cp311-linux_aarch64.whl -P /opt/package && \
    wget -q https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/ait/ais_bench-0.0.2-py3-none-any.whl -P /opt/package && \
    pip3 install /opt/package/*.whl --no-cache-dir --no-warn-conflicts && \
    rm -rf /opt/package/*

# Install msit
RUN pip3 install --no-cache-dir --no-warn-conflicts msit && \
    msit install surgeon

RUN conda init bash
RUN echo "source /usr/local/miniconda/etc/profile.d/conda.sh && conda activate base" >> /etc/bash.bashrc

CMD ["sh", "-c", "cat /agreement.txt && exec bash"]