name: pr_test_blackwell

on:
  workflow_dispatch:
    inputs:
      test_target:
        description: 'What to test: "pr" or "branch"'
        required: true
        type: choice
        options:
          - pr
          - branch

      pr_number:
        description: 'Pull Request Number (only if test_target == "pr")'
        required: false

      branch_name:
        description: 'Branch name (only if test_target == "branch")'
        default: 'main'
        required: false
#  push:
#    branches: [ main ]
#    paths:
#      - "nunchaku/**"
#      - "src/**"
#      - "tests/**"
#      - "examples/**"
#  pull_request:
#    types: [ opened, synchronize, reopened, edited ]
#    paths:
#      - "nunchaku/**"
#      - "src/**"
#      - "tests/**"
#      - "examples/**"
#  issue_comment:
#    types: [ created ]


concurrency:
  group: ${{ github.repository }}-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  check-comment:
    if: ${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'issue_comment' && github.event.issue.pull_request && !github.event.pull_request.draft) }}
    runs-on: [ self-hosted, blackwell ]
    outputs:
      should_run: ${{ steps.check.outputs.should_run }}
    steps:
      - id: check
        run: |
          body="${{ github.event.comment.body }}"
          body_lower=$(echo "$body" | tr '[:upper:]' '[:lower:]')
          if [[ "$body_lower" == "run tests" || "$body_lower" == "run test" ]]; then
            echo "should_run=true" >> $GITHUB_OUTPUT
          else
            echo "should_run=false" >> $GITHUB_OUTPUT
          fi

  set-up-build-env:
    runs-on: [ self-hosted, blackwell ]
    needs: [ check-comment ]
    if: ${{ github.event_name != 'issue_comment' || needs.check-comment.outputs.should_run == 'true' }}

    steps:
      - name: Determine ref
        id: set-ref
        run: |
          if [[ "${{ github.event.inputs.test_target }}" == "pr" ]]; then
            echo "ref=refs/pull/${{ github.event.inputs.pr_number }}/merge" >> $GITHUB_OUTPUT
          else
            echo "ref=refs/heads/${{ github.event.inputs.branch_name }}" >> $GITHUB_OUTPUT
          fi
      - name: Checkout
        uses: actions/checkout@v4
        with:
          #          ref: ${{ github.event.pull_request.head.sha || github.sha }}
          ref: ${{ steps.set-ref.outputs.ref }}
          submodules: true

      - name: Show current commit
        run: git log -1 --oneline

      - name: Set up Python
        run: |
          which python
          echo "Setting up Python with Conda"
          conda create -n test_env python=3.11 -y

      - name: Install dependencies
        run: |
          source $(conda info --base)/etc/profile.d/conda.sh
          conda activate test_env || { echo "Failed to activate conda env"; exit 1; }
          which python
          conda install -c conda-forge gxx=11 gcc=11
          echo "Installing dependencies"
          pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu128
          pip install ninja wheel diffusers transformers accelerate sentencepiece protobuf huggingface_hub
  

  build:
    needs: set-up-build-env
    runs-on: [ self-hosted, blackwell ]
    timeout-minutes: 30
    if: ${{ github.event_name != 'issue_comment' || needs.check-comment.outputs.should_run == 'true' }}

    steps:
      - name: Run build tests
        run: |
          source $(conda info --base)/etc/profile.d/conda.sh
          conda activate test_env || { echo "Failed to activate conda env"; exit 1; }
          which python
          NUNCHAKU_INSTALL_MODE=ALL python setup.py develop
          pip install -r tests/requirements.txt


  test-flux-memory:
    needs: build
    runs-on: [ self-hosted, blackwell ]
    timeout-minutes: 30
    if: ${{ github.event_name != 'issue_comment' || needs.check-comment.outputs.should_run == 'true' }}

    steps:
      - name: Run FLUX memory test
        run: |
          which python
          source $(conda info --base)/etc/profile.d/conda.sh
          conda activate test_env || { echo "Failed to activate conda env"; exit 1; }
          which python
          NUNCHAKU_TEST_CACHE_ROOT=${{ secrets.NUNCHAKU_TEST_CACHE_ROOT_BLACKWELL }} HF_TOKEN=${{ secrets.HF_TOKEN }} pytest -v tests/flux/test_flux_memory.py

  test-flux-other:
    needs: build
    runs-on: [ self-hosted, blackwell ]
    timeout-minutes: 150
    if: ${{ github.event_name != 'issue_comment' || needs.check-comment.outputs.should_run == 'true' }}

    steps:
      - name: Run other FLUX tests
        run: |
          which python
          source $(conda info --base)/etc/profile.d/conda.sh
          conda activate test_env || { echo "Failed to activate conda env"; exit 1; }
          which python
          NUNCHAKU_TEST_CACHE_ROOT=${{ secrets.NUNCHAKU_TEST_CACHE_ROOT_BLACKWELL }} HF_TOKEN=${{ secrets.HF_TOKEN }} pytest -v tests/flux --ignore=tests/flux/test_flux_memory.py

  test-sana:
    needs: build
    runs-on: [ self-hosted, blackwell ]
    timeout-minutes: 60
    if: ${{ github.event_name != 'issue_comment' || needs.check-comment.outputs.should_run == 'true' }}

    steps:
      - name: Run SANA tests
        run: |
          which python
          source $(conda info --base)/etc/profile.d/conda.sh
          conda activate test_env || { echo "Failed to activate conda env"; exit 1; }
          which python
          NUNCHAKU_TEST_CACHE_ROOT=${{ secrets.NUNCHAKU_TEST_CACHE_ROOT_BLACKWELL }} HF_TOKEN=${{ secrets.HF_TOKEN }} pytest -v tests/sana

  clean-up:
    if: always() && (github.event_name != 'issue_comment' || needs.check-comment.outputs.should_run == 'true')
    needs: [ set-up-build-env, test-flux-memory, test-flux-other, test-sana ]
    runs-on: [ self-hosted, blackwell ]

    steps:
      - name: Clean up
        run: |
          cd ..
          rm -rf *nunchaku*
