name: Build

on:
  pull_request:
    types: [synchronize, labeled]

jobs:
  detect:
    name: Detect kernel-related file change
    if: |
        github.event.pull_request.draft == false &&
        github.base_ref == 'main' &&
        github.event.pull_request.base.repo.full_name == 'hpcaitech/ColossalAI' &&
        contains( github.event.pull_request.labels.*.name, 'Run Build and Test')
    outputs:
      changedFiles: ${{ steps.find-changed-files.outputs.changedFiles }}
      anyChanged: ${{ steps.find-changed-files.outputs.any_changed }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
          ref: ${{ github.event.pull_request.head.sha }}
      - name: Find the changed files
        id: find-changed-files
        uses: tj-actions/changed-files@v35
        with:
          files: |
            op_builder/**
            colossalai/kernel/**
            setup.py
      - name: List changed files
        run: |
          for file in ${{ steps.find-changed-files.outputs.all_changed_files }}; do
            echo "$file was changed"
          done


  build:
    name: Build and Test Colossal-AI
    needs: detect
    runs-on: [self-hosted, gpu]
    container:
      image: hpcaitech/pytorch-cuda:1.11.0-11.3.0
      options: --gpus all --rm -v /data/scratch/cifar-10:/data/scratch/cifar-10
    timeout-minutes: 40
    steps:
      - uses: actions/checkout@v2
        with:
          repository: hpcaitech/TensorNVMe
          ssh-key: ${{ secrets.SSH_KEY_FOR_CI }}
          path: TensorNVMe

      - name: Install tensornvme
        run: |
          cd TensorNVMe
          conda install cmake
          pip install -r requirements.txt
          pip install -v .

      - uses: actions/checkout@v2
        with:
          ssh-key: ${{ secrets.SSH_KEY_FOR_CI }}

      - name: Restore cache
        if: needs.detect.outputs.anyChanged != 'true'
        run: |
          # -p flag is required to preserve the file timestamp to avoid ninja rebuild
          [ ! -z "$(ls -A /github/home/cuda_ext_cache/)" ] && cp -p -r /github/home/cuda_ext_cache/* /__w/ColossalAI/ColossalAI/

      - name: Install Colossal-AI
        run: |
          CUDA_EXT=1 pip install -v -e .
          pip install -r requirements/requirements-test.txt

      - name: Unit Testing
        run: |
          PYTHONPATH=$PWD pytest --cov=. --cov-report xml tests
        env:
          DATA: /data/scratch/cifar-10
          NCCL_SHM_DISABLE: 1
          LD_LIBRARY_PATH: /github/home/.tensornvme/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64

      - name: Collate artifact
        env:
          PR_NUMBER: ${{ github.event.number }}
        run: |
          mkdir report
          echo $PR_NUMBER > ./report/pr_number
          mv coverage.xml ./report

      - name: Upload test coverage artifact
        uses: actions/upload-artifact@v3
        with:
          name: report
          path: report/

      - name: Store Cache
        run: |
          # -p flag is required to preserve the file timestamp to avoid ninja rebuild
          cp -p -r /__w/ColossalAI/ColossalAI/build /github/home/cuda_ext_cache/
