name: Backend Tests

on:
  workflow_dispatch:
  push:
    branches: [main]

permissions: read-all

jobs:
  Runner-Preparation:
    runs-on: ubuntu-latest
    outputs:
      matrix-optional: ${{ steps.set-matrix.outputs.matrix-optional }}
    steps:
      - name: Prepare runner matrix
        id: set-matrix
        run: |
          if [ x"${{ github.repository }}" == x"triton-lang/triton" ]; then
            echo '::set-output name=matrix-optional::[["self-hosted", "gfx90a"], ["self-hosted", "arc770"]]'
          else
            echo '::set-output name=matrix-optional::["ubuntu-latest"]'
          fi

  Integration-Tests-Intel:
    needs: Runner-Preparation
    timeout-minutes: 30
    if: false && ((github.event_name == 'workflow_dispatch') || (github.event_name == 'push' && github.ref == 'refs/heads/main'))

    runs-on: ${{ matrix.runner }}

    strategy:
      matrix:
        runner: ${{fromJson(needs.Runner-Preparation.outputs.matrix-optional)}}

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set XPU ENV
        if: ${{(matrix.runner[0] == 'self-hosted') && (matrix.runner[1] == 'arc770')}}
        run: |
          echo "BACKEND=XPU" >> "${GITHUB_ENV}"

      - name: Clear cache
        run: |
          rm -rf ~/.triton

      - name: Update PATH
        run: |
          echo "PATH=${HOME}/.local/bin:${PATH}" >> "${GITHUB_ENV}"

      - name: Check pre-commit arc770
        if: ${{ matrix.runner != 'macos-10.15' && (matrix.runner[1] == 'arc770') }}
        run: |
          source ${HOME}/triton_vars.sh
          source ${HOME}/miniconda3/bin/activate
          conda activate triton-xpu-ci
          python3 -m pip install --upgrade pre-commit
          python3 -m pre_commit run --all-files

      - name: Install Triton on XPU
        if: ${{ env.BACKEND == 'XPU'}}
        run: |
          source ${HOME}/triton_vars.sh
          source ${HOME}/miniconda3/bin/activate
          conda activate triton-xpu-ci
          git submodule update --init --recursive
          cd python
          python3 -m pip install --upgrade pip
          python3 -m pip install cmake==3.24
          export TRITON_CODEGEN_INTEL_XPU_BACKEND=1
          python3 -m pip uninstall -y triton
          python3 setup.py build
          python3 -m pip install --no-build-isolation -vvv '.[tests]'

      - name: Run python tests on XPU
        if: ${{ env.BACKEND == 'XPU'}}
        run: |
          source ${HOME}/triton_vars.sh
          source ${HOME}/miniconda3/bin/activate
          conda activate triton-xpu-ci
          cd python/test/backend/third_party_backends
          python3 -m pytest --capture=tee-sys -rfs --verbose --backend xpu
