name: CI (ibm)

on:
  workflow_dispatch: # allows manual triggering
  push:
    branches:
      - master
    paths: [
      '.github/workflows/build-ibm.yml',
      '**/CMakeLists.txt',
      '**/.cmake',
      '**/*.h',
      '**/*.hpp',
      '**/*.c',
      '**/*.cpp'
    ]

  pull_request:
    types: [opened, synchronize, reopened]
    paths: [
      '.github/workflows/build-ibm.yml',
      'ggml/src/ggml-cpu/**',
      'ggml/src/ggml-zdnn/**'
    ]

cache-mode: none
permissions:
  contents: read

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

env:
  GGML_NLOOP: 3
  GGML_N_THREADS: 1
  LLAMA_ARG_LOG_COLORS: 1
  LLAMA_ARG_LOG_PREFIX: 1
  LLAMA_ARG_LOG_TIMESTAMPS: 1

jobs:
  ubuntu-24-s390x:
    name: ubuntu-24-s390x (VXE ${{ matrix.vxe }})
    runs-on: ubuntu-24.04-s390x

    strategy:
      fail-fast: false
      matrix:
        vxe: ["ON", "OFF"]  # `-DGGML_VXE=ON/OFF`

    steps:
      - name: Clone
        id: checkout
        uses: actions/checkout@v6

      - name: Build Dependencies
        id: build_depends
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
            python3 python3-pip python3-dev python3-wheel \
            libjpeg-dev build-essential libssl-dev \
            git-lfs

      - name: Toolchain workaround (GCC 14)
        run: |
          sudo apt-get install -y gcc-14 g++-14
          echo "CC=gcc-14" >> "$GITHUB_ENV"
          echo "CXX=g++-14" >> "$GITHUB_ENV"

      - name: Python Dependencies
        id: python_depends
        run: |
          export PIP_BREAK_SYSTEM_PACKAGES="1"
          python3 -m pip install --upgrade pip setuptools
          pip3 install ./gguf-py

      - name: Swap Endianness
        id: endianness
        run: |
          for f in models/*.gguf; do
            echo YES | python3 gguf-py/gguf/scripts/gguf_convert_endian.py $f big
          done

      - name: Build
        id: cmake_build
        run: |
          cmake -B build \
            -DLLAMA_FATAL_WARNINGS=ON \
            -DGGML_RPC=ON \
            -DGGML_VXE=${{ matrix.vxe }}
          time cmake --build build --config Release -j $(nproc)

      - name: Test
        id: cmake_test
        run: |
          cd build
          ctest -L main --verbose --timeout 900

      - name: Test llama2c (s390x)
        id: llama2c_test_s390x
        run: |
          cd build
          echo "Fetch llama2c big-endian model"
          wget https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories260K-be.gguf
          ./bin/llama-completion -m stories260K-be.gguf -p "One day, Lily met a Shoggoth" -n 500 -c 256

  ubuntu-26-zdnn-s390x:
    name: ubuntu-26-zdnn-s390x
    runs-on: ubuntu-24.04-s390x
    container: ubuntu:26.04  # required to get GCC 15.1 and binutils 2.44
    defaults:
      run:
        shell: bash

    steps:
      - name: Build Dependencies
        id: build_depends
        run: |
          apt-get update
          apt-get install -y --no-install-recommends \
            build-essential cmake git ca-certificates \
            libssl-dev libzdnn-dev

      - name: Clone
        id: checkout
        uses: actions/checkout@v6

      - name: Toolchain workaround (GCC 15)
        run: |
          apt-get install -y gcc-15 g++-15
          echo "CC=gcc-15" >> "$GITHUB_ENV"
          echo "CXX=g++-15" >> "$GITHUB_ENV"

      - name: Build with zDNN Backend
        id: cmake_build
        run: |
          cmake -B build \
            -DLLAMA_FATAL_WARNINGS=ON \
            -DGGML_NATIVE=OFF \
            -DGGML_VXE=ON \
            -DGGML_ZDNN=ON \
            -DGGML_RPC=ON \
            -DCMAKE_C_FLAGS="-march=arch15" \
            -DCMAKE_CXX_FLAGS="-march=arch15"
          time cmake --build build --config Release -j $(nproc)

  ubuntu-24-ppc64le:
    runs-on: ubuntu-24.04-ppc64le

    steps:
      - name: Clone
        id: checkout
        uses: actions/checkout@v6

      - name: Build Dependencies
        id: build_depends
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
            python3 python3-pip python3-dev python3-wheel \
            libjpeg-dev build-essential libssl-dev \
            git-lfs

      - name: Toolchain workaround (GCC 14)
        run: |
          sudo apt-get install -y gcc-14 g++-14
          echo "CC=gcc-14" >> "$GITHUB_ENV"
          echo "CXX=g++-14" >> "$GITHUB_ENV"

      - name: Python Dependencies
        id: python_depends
        run: |
          export PIP_BREAK_SYSTEM_PACKAGES="1"
          python3 -m pip install --upgrade pip setuptools
          pip3 install ./gguf-py

      - name: Build
        id: cmake_build
        run: |
          cmake -B build \
            -DLLAMA_FATAL_WARNINGS=ON \
            -DGGML_RPC=ON
          time cmake --build build --config Release -j $(nproc)

      - name: Test
        id: cmake_test
        run: |
          cd build
          ctest -L main --verbose --timeout 900

      - name: Test llama2c conversion
        id: llama2c_test
        run: |
          cd build
          echo "Fetch tokenizer"
          wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories260K/tok512.bin
          echo "Fetch llama2c model"
          wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories260K/stories260K.bin
          ./bin/llama-convert-llama2c-to-ggml --copy-vocab-from-model ./tok512.bin --llama2c-model stories260K.bin --llama2c-output-model stories260K.gguf
          ./bin/llama-completion -m stories260K.gguf -p "One day, Lily met a Shoggoth" -n 500 -c 256
