name: Server (self-hosted)

on:
  workflow_dispatch: # allows manual triggering
    inputs:
      sha:
        description: 'Commit SHA1 to build'
        required: false
        type: string
      slow_tests:
        description: 'Run slow tests'
        required: true
        type: boolean
  push:
    branches:
      - master
    paths: [
      '.github/workflows/server-self-hosted.yml',
      '**/CMakeLists.txt',
      '**/Makefile',
      '**/*.h',
      '**/*.hpp',
      '**/*.c',
      '**/*.cpp',
      '**/*.cu',
      '**/*.swift',
      '**/*.m',
      'tools/server/**.*'
    ]

cache-mode: none
permissions:
  contents: read

env:
  # note: this is dud token to avoid rate limiting (https://github.com/ggml-org/llama.cpp/pull/25706#issuecomment-4979941302)
  HF_TOKEN: ${{ secrets.HF_TOKEN_CI }}
  LLAMA_ARG_LOG_COLORS: 1
  LLAMA_ARG_LOG_PREFIX: 1
  LLAMA_ARG_LOG_TIMESTAMPS: 1
  LLAMA_ARG_LOG_VERBOSITY: 10

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  server-metal:
    runs-on: [self-hosted, llama-server, macOS, ARM64]

    steps:
      - name: Clone
        id: checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          ref: ${{ github.event.inputs.sha || github.event.pull_request.head.sha || github.sha || github.head_ref || github.ref_name }}

      - name: Build
        id: cmake_build
        run: |
          cmake -B build -DGGML_SCHED_NO_REALLOC=ON
          cmake --build build --config Release -j $(sysctl -n hw.logicalcpu) --target llama-server

      - name: Python setup
        id: setup_python
        run: |
          cd tools/server/tests
          python3 -m venv venv
          source venv/bin/activate
          pip install -r requirements.txt

      - name: Tests (GPUx1)
        id: server_integration_tests
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          PYTEST_WORKERS=1 ./tests.sh

      - name: Tests (GPUx1, backend-sampling)
        id: server_integration_tests_backend_sampling
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          export LLAMA_ARG_BACKEND_SAMPLING=1
          PYTEST_WORKERS=1 ./tests.sh

      - name: Tests (GPUx2)
        id: server_integration_tests_gpu2
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          export GGML_METAL_DEVICES=2
          PYTEST_WORKERS=1 ./tests.sh

      - name: Tests (GPUx2, backend-sampling)
        id: server_integration_tests_gpu2_backend_sampling
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          export GGML_METAL_DEVICES=2 LLAMA_ARG_BACKEND_SAMPLING=1
          PYTEST_WORKERS=1 ./tests.sh

  server-cuda:
    runs-on: "hf-jobs-t4-medium:cuda13"

    steps:
      - name: Clone
        id: checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          ref: ${{ github.event.inputs.sha || github.event.pull_request.head.sha || github.sha || github.head_ref || github.ref_name }}

      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install -y cmake libssl-dev python3 python3-venv python3-pip

      - name: ccache
        uses: ggml-org/ccache-action@v1.2.24
        with:
          restore: false
          save: false

      - name: ccache-buckets-restore
        uses: ./.github/actions/ccache-buckets
        with:
          key: self-hosted-server-cuda
          folder: llama.cpp
          hf_bucket: ggml-org/cache

      - name: Build
        id: cmake_build
        run: |
          cmake -B build -DGGML_CUDA=ON -DGGML_SCHED_NO_REALLOC=ON -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc
          cmake --build build --config Release -j $(nproc) --target llama-server

      - name: ccache-buckets-save
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
        uses: ./.github/actions/ccache-buckets
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN_CACHE_OUTPUT }}
        with:
          key: self-hosted-server-cuda
          folder: llama.cpp
          evict-old-files: 1d
          hf_bucket: ggml-org/cache
          save: true

      - name: Python setup
        id: setup_python
        run: |
          cd tools/server/tests
          python3 -m venv venv
          source venv/bin/activate
          pip install -r requirements.txt

      - name: Tests (GPUx1)
        id: server_integration_tests
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          PYTEST_WORKERS=1 ./tests.sh

      - name: Tests (GPUx1, backend-sampling)
        id: server_integration_tests_backend_sampling
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          export LLAMA_ARG_BACKEND_SAMPLING=1
          PYTEST_WORKERS=1 ./tests.sh

      - name: Tests (GPUx2)
        id: server_integration_tests_gpu2
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          export GGML_CUDA_DEVICES=2
          PYTEST_WORKERS=1 ./tests.sh

      - name: Tests (GPUx2, backend-sampling)
        id: server_integration_tests_gpu2_backend_sampling
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          export GGML_CUDA_DEVICES=2 LLAMA_ARG_BACKEND_SAMPLING=1
          PYTEST_WORKERS=1 ./tests.sh

  server-kleidiai:
    runs-on: ah-ubuntu_24_04-c8g_8x

    steps:
      - name: Clone
        id: checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          ref: ${{ github.event.inputs.sha || github.event.pull_request.head.sha || github.sha || github.head_ref || github.ref_name }}

      - name: Dependencies
        id: depends
        run: |
          set -euxo pipefail
          sudo apt-get update
          sudo DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a \
          apt-get install -y \
           build-essential \
           libssl-dev \
           python3-venv \
           gpg \
           wget \
           time \
           git-lfs

          git lfs install

          # install the latest cmake
          sudo install -d /usr/share/keyrings
          wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc \
           | gpg --dearmor \
           | sudo tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null
          echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ jammy main' \
           | sudo tee /etc/apt/sources.list.d/kitware.list
          sudo apt-get update
          sudo apt-get install -y cmake

      - name: Build
        id: cmake_build
        run: |
          cmake -B build -DGGML_SCHED_NO_REALLOC=ON -DGGML_CPU_KLEIDIAI=ON -DLLAMA_FATAL_WARNINGS=ON
          cmake --build build --config Release -j $(nproc) --target llama-server

      - name: Python setup
        id: setup_python
        run: |
          cd tools/server/tests
          python3 -m venv venv
          source venv/bin/activate
          pip install -r requirements.txt

      - name: Tests
        id: server_integration_tests
        if: ${{ !github.event.pull_request }}
        run: |
          cd tools/server/tests
          source venv/bin/activate
          ./tests.sh
