name: PR Test (sgl-kernel)

on:
  push:
    branches: [ main ]
    paths:
      - "sgl-kernel/**"
  pull_request:
    branches: [ main ]
    paths:
      - "sgl-kernel/**"
  workflow_dispatch:

concurrency:
  group: pr-test-sgl-kernel-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Check clang-format
        uses: DoozyX/clang-format-lint-action@v0.18.1
        with:
          source: sgl-kernel
          extensions: h,c,cpp,hpp,cu,cuh,cc
          clangFormatVersion: 16
          style: file

  unit-test:
    if: github.repository == 'sgl-project/sglang' || github.event_name == 'pull_request'
    runs-on: 1-gpu-runner
    steps:
      - uses: actions/checkout@v4

      - name: Install
        run: |
          pip3 install torch==2.5.1 && pip3 install pytest && pip3 install vllm
          pip3 uninstall sgl-kernel -y || true
          find . -name index.lock -delete
          cd sgl-kernel
          git submodule deinit --all --force && git submodule sync --recursive && git submodule update --init --force --recursive
          pip3 install .
          pip3 list | grep sgl-kernel

      - name: Run test
        timeout-minutes: 10
        run: |
          cd sgl-kernel
          find tests -name "test_*.py" | xargs -n 1 python3

      - name: Uninstall dependencies
        run: |
          pip3 uninstall sgl-kernel -y

  finish:
    needs: [unit-test, lint]
    runs-on: ubuntu-latest
    steps:
      - name: Finish
        run: echo "This is an empty step to ensure that all jobs are completed."
