name: Tests against Transformers branch

on:
  workflow_dispatch:
    inputs:
      transformers_ref:
        description: "Transformers git ref (branch, tag, or commit SHA)"
        required: true
        default: "main"

env:
  TQDM_DISABLE: 1
  CI_SLACK_CHANNEL: ${{ secrets.CI_PUSH_MAIN_CHANNEL }}
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
  PYTORCH_CUDA_ALLOC_CONF: "expandable_segments:True"
  PYTORCH_ALLOC_CONF: "expandable_segments:True"

jobs:
  tests_transformers_branch:
    name: Tests with Transformers ${{ inputs.transformers_ref }}
    runs-on:
      group: aws-g4dn-2xlarge
    container:
      image: pytorch/pytorch:2.8.0-cuda12.8-cudnn9-devel
      options: --gpus all
    defaults:
      run:
        shell: bash
    steps:
      - name: Git checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1  # v7.0.1

      - name: Set up Python 3.12
        uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1  # v6.3.0
        with:
          python-version: '3.12'

      - name: Install Make and Git
        run: |
          apt-get update && apt-get install -y make git curl

      - name: Install uv
        uses: astral-sh/setup-uv@11f9893b081a58869d3b5fccaea48c9e9e46f990  # v8.3.2

      - name: Create Python virtual environment
        run: |
          uv venv
          uv pip install --upgrade setuptools wheel

      - name: Install dependencies
        env:
          TRANSFORMERS_REF: ${{ inputs.transformers_ref }}
        run: |
          source .venv/bin/activate
          uv pip install -U \
            ".[dev]" \
            "transformers @ git+https://github.com/huggingface/transformers.git@${TRANSFORMERS_REF}"

      - name: Test with pytest
        run: |
          source .venv/bin/activate
          make test

      - name: Post to Slack
        if: github.ref == 'refs/heads/main' && always()
        uses: huggingface/hf-workflows/.github/actions/post-slack@main
        with:
          slack_channel: ${{ env.CI_SLACK_CHANNEL }}
          title: Results with Transformers ${{ inputs.transformers_ref }}
          status: ${{ job.status }}
          slack_token: ${{ secrets.SLACK_CIFEEDBACK_BOT_TOKEN }}

  distributed_smoke:
    name: Distributed smoke tests with Transformers ${{ inputs.transformers_ref }}
    runs-on:
      group: aws-g5-12xlarge-cache
    container:
      image: pytorch/pytorch:2.8.0-cuda12.8-cudnn9-devel
      options: --gpus all
    defaults:
      run:
        shell: bash
    env:
      CUDA_VISIBLE_DEVICES: "0,1"
    steps:
      - name: Git checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1  # v7.0.1

      - name: Set up Python 3.12
        uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1  # v6.3.0
        with:
          python-version: '3.12'

      - name: Install Make and Git
        run: |
          apt-get update && apt-get install -y make git curl

      - name: Install uv
        uses: astral-sh/setup-uv@11f9893b081a58869d3b5fccaea48c9e9e46f990  # v8.3.2

      - name: Create Python virtual environment
        run: |
          uv venv
          uv pip install --upgrade setuptools wheel

      - name: Install dependencies
        env:
          TRANSFORMERS_REF: ${{ inputs.transformers_ref }}
        run: |
          source .venv/bin/activate
          uv pip install -U \
            ".[dev]" \
            "transformers @ git+https://github.com/huggingface/transformers.git@${TRANSFORMERS_REF}"

      - name: Run distributed smoke tests
        run: |
          source .venv/bin/activate
          pytest -v tests/distributed/test_distributed.py

      - name: Post to Slack
        if: github.ref == 'refs/heads/main' && always()
        uses: huggingface/hf-workflows/.github/actions/post-slack@main
        with:
          slack_channel: ${{ env.CI_SLACK_CHANNEL }}
          title: Results of distributed smoke tests with Transformers ${{ inputs.transformers_ref }}
          status: ${{ job.status }}
          slack_token: ${{ secrets.SLACK_CIFEEDBACK_BOT_TOKEN }}
