#
# 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.
#

name: E2E-upstream

on:
  schedule:
    - cron: '0 8 * * 0' # every Sunday at 8:00
# Bash shells do not use ~/.profile or ~/.bashrc so these shells need to be explicitly
# declared as "shell: bash -el {0}" on steps that need to be properly activated.
# It's used to activate ascend-toolkit environment variables.
defaults:
  run:
    shell: bash -el {0}

# only cancel in-progress runs of the same workflow
# and ignore the lint / 1 card / 4 cards test type
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  UV_INDEX_URL: http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple
  UV_EXTRA_INDEX_URL: https://repo.huaweicloud.com/ascend/repos/pypi
  UV_INDEX_STRATEGY: unsafe-best-match
  UV_NO_CACHE: 1
  UV_SYSTEM_PYTHON: 1

jobs:
    e2e-upstream_singlecard:
        runs-on: linux-aarch64-a2b3-1
        strategy:
          fail-fast: false
          matrix:
            part: [0, 1, 2, 3]
            vllm: [v0.20.2]
        container:
          image: swr.cn-southwest-2.myhuaweicloud.com/base_image/ascend-ci/cann:9.0.0-910b-ubuntu22.04-py3.11
          env:
            VLLM_LOGGING_LEVEL: ERROR
            HF_HUB_OFFLINE: 1
        steps:
          - name: Checkout vllm-project/vllm-ascend repo
            uses: actions/checkout@v6

          - name: Check npu and CANN info
            run: |
              npu-smi info
              cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info

          - name: Config mirrors
            run: |
              sed -Ei 's@(ports|archive).ubuntu.com@cache-service.nginx-pypi-cache.svc.cluster.local:8081@g' /etc/apt/sources.list
              pip config set global.index-url http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple
              pip config set global.trusted-host cache-service.nginx-pypi-cache.svc.cluster.local
              apt-get update -y
              apt install git -y

          - name: Install system dependencies
            run: |
              apt-get -y install `cat packages.txt`
              apt-get -y install gcc g++ cmake libnuma-dev clang-15
              git config --global --add safe.directory /__w/vllm-ascend/vllm-ascend
              update-alternatives --install /usr/bin/clang clang /usr/bin/clang-15 20
              update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-15 20

          - name: Checkout vllm-project/vllm repo
            uses: actions/checkout@v6
            with:
              repository: vllm-project/vllm
              ref: ${{ matrix.vllm }}
              path: ./vllm-empty
              fetch-depth: 1

          - name: Install vllm-project/vllm from source
            working-directory: ./vllm-empty
            run: |
              VLLM_TARGET_DEVICE=empty pip install -e .
              pip install -e tests/plugins/bge_m3_sparse_plugin
              pip install -e tests/plugins/prithvi_io_processor_plugin
              sed -i '5i\import vllm_ascend.patch.platform\nimport vllm_ascend.patch.worker' tests/conftest.py

          - name: Install vllm-project/vllm-ascend
            env:
              PIP_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi https://triton-ascend.osinfra.cn/pypi/simple https://download.pytorch.org/whl/cpu/"
            run: |
              pip install uc-manager
              pip install -r requirements-dev.txt
              pip install -e .
              pip install tblib 
              pip install runai_model_streamer pqdm timm schemathesis==3.39.15 "vllm[helion]" "runai-model-streamer[s3,gcs]"
              pip install "mteb[bm25s]" open_clip_torch terratorch==1.2.2 imagehash
              pip install "lm-eval[api]" num2words "vllm[grpc]" "vllm[audio]"
              pip uninstall -y triton
              pip uninstall -y triton-ascend
              pip install triton-ascend==3.2.1 --extra-index-url https://triton-ascend.osinfra.cn/pypi/simple
          - name: Run vllm-project/vllm test
            working-directory: ./vllm-empty
            env:
              PYTORCH_NPU_ALLOC_CONF: max_split_size_mb:256
              VLLM_WORKER_MULTIPROC_METHOD: spawn
            run: |
              mkdir -p tiktoken_encodings
              curl -L -o tiktoken_encodings/o200k_base.tiktoken "https://openaipublic.blob.core.windows.net/encodings/o200k_base.tiktoken"
              curl -L -o tiktoken_encodings/cl100k_base.tiktoken "https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken"
              export TIKTOKEN_ENCODINGS_BASE=${PWD}/tiktoken_encodings
              python3 ../.github/workflows/scripts/run_suite.py \
                --suite e2e-upstream_singlecard \
                --auto-partition-id "${{ matrix.part }}" \
                --auto-partition-size 4 \
                --continue-on-error
              
    e2e-upstream_a2_2:
        runs-on: linux-aarch64-a2b3-2
        strategy:
          fail-fast: false
          matrix:
            part: [0]
            vllm: [v0.20.2]
        container:
          image: swr.cn-southwest-2.myhuaweicloud.com/base_image/ascend-ci/cann:9.0.0-910b-ubuntu22.04-py3.11
          env:
            VLLM_LOGGING_LEVEL: ERROR
            HF_HUB_OFFLINE: 1
        steps:
          - name: Checkout vllm-project/vllm-ascend repo
            uses: actions/checkout@v6
          - name: Check npu and CANN info
            run: |
              npu-smi info
              cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info

          - name: Config mirrors
            run: |
              sed -Ei 's@(ports|archive).ubuntu.com@cache-service.nginx-pypi-cache.svc.cluster.local:8081@g' /etc/apt/sources.list
              pip config set global.index-url http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple
              pip config set global.trusted-host cache-service.nginx-pypi-cache.svc.cluster.local
              apt-get update -y
              apt install git -y

          - name: Install system dependencies
            run: |
              apt-get -y install `cat packages.txt`
              apt-get -y install gcc g++ cmake libnuma-dev clang-15
              git config --global --add safe.directory /__w/vllm-ascend/vllm-ascend
              update-alternatives --install /usr/bin/clang clang /usr/bin/clang-15 20
              update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-15 20

          - name: Checkout vllm-project/vllm repo
            uses: actions/checkout@v6
            with:
              repository: vllm-project/vllm
              ref: ${{ matrix.vllm }}
              path: ./vllm-empty
              fetch-depth: 1

          - name: Install vllm-project/vllm from source
            working-directory: ./vllm-empty
            run: |
              VLLM_TARGET_DEVICE=empty pip install -e .
              sed -i '5i\import vllm_ascend.patch.platform\nimport vllm_ascend.patch.worker' tests/conftest.py

          - name: Install vllm-project/vllm-ascend
            env:
              PIP_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi https://triton-ascend.osinfra.cn/pypi/simple https://download.pytorch.org/whl/cpu/"
            run: |
              pip install uc-manager
              pip install -r requirements-dev.txt
              pip install -e .
              pip install tblib
              pip uninstall -y triton-ascend
              pip uninstall -y triton
              pip install triton-ascend==3.2.1 --extra-index-url https://triton-ascend.osinfra.cn/pypi/simple
          - name: Run vllm-project/vllm test
            working-directory: ./vllm-empty
            env:
              PYTORCH_NPU_ALLOC_CONF: max_split_size_mb:256
              VLLM_WORKER_MULTIPROC_METHOD: spawn
            run: |
              python3 ../.github/workflows/scripts/run_suite.py \
                --suite e2e-upstream_a2_2 \
                --auto-partition-id "${{ matrix.part }}" \
                --auto-partition-size 1 \
                --continue-on-error

    e2e-upstream_a2_4:
        runs-on: linux-aarch64-a2b3-4
        strategy:
          fail-fast: false
          matrix:
            part: [0]
            vllm: [v0.20.2]
        container:
          image: swr.cn-southwest-2.myhuaweicloud.com/base_image/ascend-ci/cann:9.0.0-910b-ubuntu22.04-py3.11
          env:
            VLLM_LOGGING_LEVEL: ERROR
            HF_HUB_OFFLINE: 1
        steps:
          - name: Checkout vllm-project/vllm-ascend repo
            uses: actions/checkout@v6
          - name: Check npu and CANN info
            run: |
              npu-smi info
              cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info

          - name: Config mirrors
            run: |
              sed -Ei 's@(ports|archive).ubuntu.com@cache-service.nginx-pypi-cache.svc.cluster.local:8081@g' /etc/apt/sources.list
              pip config set global.index-url http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple
              pip config set global.trusted-host cache-service.nginx-pypi-cache.svc.cluster.local
              apt-get update -y
              apt install git -y

          - name: Install system dependencies
            run: |
              apt-get -y install `cat packages.txt`
              apt-get -y install gcc g++ cmake libnuma-dev clang-15
              git config --global --add safe.directory /__w/vllm-ascend/vllm-ascend
              update-alternatives --install /usr/bin/clang clang /usr/bin/clang-15 20
              update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-15 20

          - name: Checkout vllm-project/vllm repo
            uses: actions/checkout@v6
            with:
              repository: vllm-project/vllm
              ref: ${{ matrix.vllm }}
              path: ./vllm-empty
              fetch-depth: 1

          - name: Install vllm-project/vllm from source
            working-directory: ./vllm-empty
            run: |
              VLLM_TARGET_DEVICE=empty pip install -e .
              sed -i '5i\import vllm_ascend.patch.platform\nimport vllm_ascend.patch.worker' tests/conftest.py

          - name: Install vllm-project/vllm-ascend
            env:
              PIP_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi https://triton-ascend.osinfra.cn/pypi/simple https://download.pytorch.org/whl/cpu/"
            run: |
              pip install uc-manager
              pip install -r requirements-dev.txt
              pip install -e .
              pip install tblib
              pip uninstall -y triton-ascend
              pip uninstall -y triton
              pip install triton-ascend==3.2.1 --extra-index-url https://triton-ascend.osinfra.cn/pypi/simple
          - name: Run vllm-project/vllm test
            working-directory: ./vllm-empty
            env:
              PYTORCH_NPU_ALLOC_CONF: max_split_size_mb:256
              VLLM_WORKER_MULTIPROC_METHOD: spawn
            run: |
              python3 ../.github/workflows/scripts/run_suite.py \
                --suite e2e-upstream_a2_4 \
                --auto-partition-id "${{ matrix.part }}" \
                --auto-partition-size 1 \
                --continue-on-error