name: Upload torch dynamo performance stats

on:
  workflow_run:
    workflows: [inductor-A100-perf-nightly, inductor-perf-nightly-A10g, inductor-perf-nightly-aarch64, inductor-perf-nightly-x86, perf-nightly-macos, inductor-perf-nightly-rocm, inductor-perf-nightly-h100]
    types:
      - completed

jobs:
  get-conclusion:
    runs-on: ubuntu-latest
    outputs:
      conclusion: ${{ fromJson(steps.get-conclusion.outputs.data).conclusion }}
    steps:
      - name: Get workflow run conclusion
        # TODO (huydhn): Pin this once https://github.com/octokit/request-action/issues/315 is resolved
        uses: octokit/request-action@release/2.7
        id: get-conclusion
        with:
          route: GET /repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/attempts/${{ github.event.workflow_run.run_attempt }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  upload-perf-stats:
    needs: get-conclusion
    if: github.event.workflow_run.conclusion == 'success' || needs.get-conclusion.outputs.conclusion == 'success' ||
        github.event.workflow_run.conclusion == 'failure' || needs.get-conclusion.outputs.conclusion == 'failure'
    runs-on: ubuntu-22.04
    environment: upload-stats
    permissions:
      id-token: write
    name: Upload dynamo performance stats for ${{ github.event.workflow_run.id }}, attempt ${{ github.event.workflow_run.run_attempt }}
    steps:
      - name: Checkout PyTorch
        uses: pytorch/pytorch/.github/actions/checkout-pytorch@release/2.7
        with:
          submodules: false
          fetch-depth: 1

      - name: Configure aws credentials
        uses: aws-actions/configure-aws-credentials@v3
        continue-on-error: true
        with:
          role-to-assume: arn:aws:iam::308535385114:role/gha_workflow_upload-torch-test-stats
          aws-region: us-east-1

      - uses: actions/setup-python@v4
        with:
          python-version: '3.11'
          cache: pip

      - run: |
          pip3 install requests==2.32.2 boto3==1.35.42

      - name: Upload torch dynamo performance stats to S3
        id: upload-s3
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          WORKFLOW_ARTIFACTS_URL: ${{ github.event.workflow_run.artifacts_url }}
          WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }}
          WORKFLOW_RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }}
          REPO_FULLNAME: ${{ github.event.workflow_run.repository.full_name }}
        run: |
          # Upload perf test reports from GHA to S3, which can now be downloaded
          # on HUD
          python3 -m tools.stats.upload_artifacts --workflow-run-id "${WORKFLOW_RUN_ID}" --workflow-run-attempt "${WORKFLOW_RUN_ATTEMPT}" --repo "${REPO_FULLNAME}"

      - name: Upload torch dynamo performance stats to s3
        if: steps.upload-s3.outcome && steps.upload-s3.outcome == 'success'
        env:
          WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }}
          WORKFLOW_RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }}
          REPO_FULLNAME: ${{ github.event.workflow_run.repository.full_name }}
          HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
        run: |
          python3 -m tools.stats.upload_dynamo_perf_stats --workflow-run-id "${WORKFLOW_RUN_ID}" --workflow-run-attempt "${WORKFLOW_RUN_ATTEMPT}" --repo "${REPO_FULLNAME}" --head-branch "${HEAD_BRANCH}" --dynamodb-table torchci-dynamo-perf-stats --match-filename "^inductor_"
