#
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This file is a part of the vllm-ascend project.
#
FROM quay.io/ascend/cann:9.2.1-910b-ubuntu22.04-py3.12

WORKDIR /workspace

# Install clang-15 (for triton-ascend)
RUN apt-get update -y && \
    apt-get install -y git vim wget curl protobuf-compiler libprotobuf-dev net-tools gcc g++ cmake numactl libnuma-dev libibverbs-dev libhiredis-dev clang-15 && \
    update-alternatives --install /usr/bin/clang clang /usr/bin/clang-15 20 && \
    update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-15 20 && \
    rm -rf /var/cache/apt/* && \
    rm -rf /var/lib/apt/lists/*

# Install modelscope (for fast download) and ray (for multinode)
RUN python3 -m pip install 'modelscope<1.38' 'ray>=2.47.1,<=2.48.0' 'protobuf>3.20.0' && \
    python3 -m pip cache purge

# Install vLLM
ARG VLLM_REPO=https://github.com/vllm-project/vllm.git
ARG VLLM_TAG=v0.30.0
ARG VLLM_COMMIT=""
RUN if [ -n "$VLLM_COMMIT" ]; then \
      git init /vllm-workspace/vllm && \
      git -C /vllm-workspace/vllm fetch --depth 1 $VLLM_REPO "$VLLM_COMMIT" && \
      git -C /vllm-workspace/vllm checkout FETCH_HEAD; \
    else \
      git clone --depth 1 -b $VLLM_TAG $VLLM_REPO /vllm-workspace/vllm; \
    fi

# In x86, triton will be installed by vllm. But in Ascend, triton doesn't work correctly. we need to uninstall it.
ARG PYTORCH_INDEX_URL="https://download.pytorch.org/whl/cpu/"
RUN VLLM_TARGET_DEVICE="empty" python3 -m pip install -e /vllm-workspace/vllm/[audio] --extra-index-url ${PYTORCH_INDEX_URL} && \
    python3 -m pip uninstall -y triton && \
    python3 -m pip cache purge

# Install vllm-ascend
# Dependency versions used by the vllm-ascend build.
ARG SOC_VERSION="ascend910b1"
ENV DEBIAN_FRONTEND=noninteractive
ENV SOC_VERSION=$SOC_VERSION \
    TASK_QUEUE_ENABLE=1 \
    OMP_NUM_THREADS=1
COPY . /vllm-workspace/vllm-ascend/
ARG ASCEND_INDEX_URL="https://mirrors.huaweicloud.com/ascend/repos/pypi"
ARG COMPILE_CUSTOM_KERNELS=1
RUN export PIP_EXTRA_INDEX_URL="${ASCEND_INDEX_URL}" && \
    export VLLM_BATCH_INVARIANT=1 && \
    source /usr/local/Ascend/ascend-toolkit/set_env.sh && \
    source /usr/local/Ascend/nnal/atb/set_env.sh && \
    python3 -m pip install -e /vllm-workspace/vllm-ascend/ --extra-index-url ${PYTORCH_INDEX_URL} && \
    python3 -m pip install concurrent-log-handler && \
    python3 -m pip cache purge

# Install _rust_tool_parser for the Rust frontend.
# The default toolchain is read from vllm's rust-toolchain.toml (falling back
# to stable).
ENV PATH="/root/.cargo/bin:$PATH"
RUN cd /vllm-workspace/vllm && \
    export PROTOC_INCLUDE=/usr/include && \
    python3 -m pip install setuptools-rust && \
    ./build_rust.sh

# Append `libascend_hal.so` path (devlib) to LD_LIBRARY_PATH
RUN echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib" >> ~/.bashrc

# ===== Install pinned dependencies =====
# Update the pinned versions here.
ARG TARGETPLATFORM
ARG MEMCACHE_VERSION="1.3.0"
ARG MEMCACHE_DATE="20261027.3"
ARG MEMFABRIC_VERSION="1.3.0"
ARG MEMFABRIC_DATE="20261027.3"
ARG TORCH_NPU_VERSION="26.2.0"
ARG TORCH_NPU_DATE="20261015.5"
ARG FLA_VERSION="26.9.1"
ARG FLA_RELEASE_TAG="v26.9.1-release"
ARG MOONCAKE_OBS_PREFIX="https://mindie.obs.cn-north-4.myhuaweicloud.com/buildtools/docker_images_tools/Mooncake/v0.3.14.rc1"
ARG HYBRID_OBS_BASE="https://obs-memfabric-hybrid.obs.cn-north-4.myhuaweicloud.com"
ARG TORCH_NPU_OBS_BASE="https://pytorch-package.obs.cn-north-4.myhuaweicloud.com"
ARG TRITON_ASCEND_URL_BASE="https://ascend-triton-open.obs.cn-north-4.myhuaweicloud.com/triton-ascend"
ARG TRITON_ASCEND_VERSION="20261028193430"
ARG TRITON_ASCEND_PACKAGE_VERSION="3.6.0.post2"
RUN case "${TARGETPLATFORM:-$(uname -m)}" in \
        "linux/amd64"|"x86_64") \
            ARCH_SUFFIX="x86_64"; \
            TRITON_ASCEND_URL="${TRITON_ASCEND_URL_BASE}/${TRITON_ASCEND_VERSION}/triton_ascend-${TRITON_ASCEND_PACKAGE_VERSION}-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl"; \
            MEMFABRIC_URL="${HYBRID_OBS_BASE}/mf/release/${MEMFABRIC_VERSION}/${MEMFABRIC_DATE}/memfabric_hybrid-${MEMFABRIC_VERSION}-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl"; \
            MEMCACHE_URL="${HYBRID_OBS_BASE}/memcache/release/${MEMCACHE_VERSION}/${MEMCACHE_DATE}/memcache_hybrid-${MEMCACHE_VERSION}-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl"; \
            MOONCAKE_WHL="mooncake_transfer_engine-0.3.13-cp312-cp312-manylinux_2_35_x86_64.whl"; \
            FLA_WHL="flash_linear_attention_npu_a2-${FLA_VERSION}-py3-none-manylinux_2_34_x86_64.whl"; \
            FLA_SHA256="f9e5b2cac5278eee9b781e758a896f6f2dbae42b43b9f36a3073e4493167918a" ;; \
        "linux/arm64"|"aarch64") \
            ARCH_SUFFIX="aarch64"; \
            TRITON_ASCEND_URL="${TRITON_ASCEND_URL_BASE}/${TRITON_ASCEND_VERSION}/triton_ascend-${TRITON_ASCEND_PACKAGE_VERSION}-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl"; \
            MEMFABRIC_URL="${HYBRID_OBS_BASE}/mf/release/${MEMFABRIC_VERSION}/${MEMFABRIC_DATE}/memfabric_hybrid-${MEMFABRIC_VERSION}-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl"; \
            MEMCACHE_URL="${HYBRID_OBS_BASE}/memcache/release/${MEMCACHE_VERSION}/${MEMCACHE_DATE}/memcache_hybrid-${MEMCACHE_VERSION}-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl"; \
            MOONCAKE_WHL="mooncake_transfer_engine-0.3.13-cp312-cp312-manylinux_2_35_aarch64.whl"; \
            FLA_WHL="flash_linear_attention_npu_a2-${FLA_VERSION}-py3-none-manylinux_2_34_aarch64.whl"; \
            FLA_SHA256="2577eca3f1015ca83897bf638046f41cb54691a0c15c433d9e43946fa32d985b" ;; \
        *) echo "Unsupported platform: ${TARGETPLATFORM:-$(uname -m)}" && exit 1 ;; \
    esac && \
    source /usr/local/Ascend/ascend-toolkit/set_env.sh && \
# Install triton-ascend
    # echo "Install triton-ascend..." && \
    # python3 -m pip install "$TRITON_ASCEND_URL" && \
# Install mooncake-transfer-engine, memfabric_hybrid and memcache_hybrid
    wget -q --no-check-certificate "${MOONCAKE_OBS_PREFIX}/${MOONCAKE_WHL}" -P /tmp && \
    python3 -m pip install /tmp/${MOONCAKE_WHL} --force-reinstall --no-deps && \
# Install memfabric_hybrid kernels (A3/A5)
    case "${SOC_VERSION:-}" in \
        ascend950dt_9582) TORCH_DEVICE_BACKEND_AUTOLOAD=0 mfcli kernel install --soc-version A5 ;; \
        ascend910_9391) TORCH_DEVICE_BACKEND_AUTOLOAD=0 mfcli kernel install --soc-version A3 ;; \
        ascend910b1) TORCH_DEVICE_BACKEND_AUTOLOAD=0 mfcli kernel install --soc-version A2 ;; \
    esac && \
# Install flash-linear-attention-npu
    URL="https://mirrors.huaweicloud.com/ascend/repos/pypi/flash_linear_attention_npu_a2/${FLA_WHL}" && \
    if [[ -n "${GIT_PROXY}" ]]; then \
      wget --quiet --proxy="${GIT_PROXY}" "${URL}" -O "/tmp/${FLA_WHL}"; \
    else \
      wget --quiet "${URL}" -O "/tmp/${FLA_WHL}"; \
    fi && \
    # echo "${FLA_SHA256}  /tmp/${FLA_WHL}" | sha256sum --check && \
    python3 -m pip install --force-reinstall --no-deps "/tmp/${FLA_WHL}"
# Install torch-npu
    # mkdir -p /tmp/torch_npu && \
    # wget -q --retry-connrefused --tries=5 --timeout=30 --waitretry=10 \
    #     -O /tmp/torch_npu/torch_npu.tar.gz \
    #     "${TORCH_NPU_OBS_BASE}/pta/Daily/v2.10.0-${TORCH_NPU_VERSION}/${TORCH_NPU_DATE}/pytorch_v2.10.0-${TORCH_NPU_VERSION}_py312.tar.gz" && \
    # tar -xzf /tmp/torch_npu/torch_npu.tar.gz -C /tmp/torch_npu && \
    # python3 -m pip install /tmp/torch_npu/torch_npu-2.10.0*_"${ARCH_SUFFIX}".whl --extra-index-url https://download.pytorch.org/whl/cpu/ && \
    # rm -rf /tmp/torch_npu /tmp/${MOONCAKE_WHL} /tmp/${FLA_WHL} && \
    # python3 -m pip cache purge

CMD ["/bin/bash"]
