trigger:
  tags:
    include: ["*"]
  branches:
    include:
      - "main"
      - "release/*"
      - "refs/tags/*"

pr:
  branches:
    include: ["*"]

jobs:
  - job: jupyter
    strategy:
      matrix:
        "notebooks w/ torch 2.8":
          docker-image: "ubuntu24.04-cuda12.8.1-cudnn-fe1.15.0-py3.12-pt_2.8.0-dev"
        "notebooks w/ torch-nightly":
          docker-image: "ubuntu24.04-cuda12.8.1-cudnn-fe1.15.0-py3.12-pt_main-dev"
    # how long to run the job before automatically cancelling
    timeoutInMinutes: "45"
    # how much time to give 'run always even if cancelled tasks' before stopping them
    cancelTimeoutInMinutes: "2"
    pool: "lit-rtx-3090"
    variables:
      DEVICES: $( python -c 'name = "$(Agent.Name)" ; gpus = name.split("_")[-1] if "_" in name else "0"; print(gpus)' )
      TORCH_HOME: "/var/tmp/torch"
      PIP_CACHE_DIR: "/var/tmp/pip"
    container:
      image: "pytorchlightning/lightning-thunder:$(docker-image)"
      options: "--gpus=all --shm-size=16g -v /var/tmp:/var/tmp"
    workspace:
      clean: all
    steps:
      - bash: |
          echo $(DEVICES)
          lspci | egrep 'VGA|3D'
          whereis nvidia
          nvidia-smi
          which python && which pip
          python --version
          pip --version
          pip list
          echo "##vso[task.setvariable variable=CUDA_VISIBLE_DEVICES]$(DEVICES)"
        displayName: "Image info & NVIDIA"

      - bash: |
          set -ex
          # drop pt from requirements so not to interfere with the existing one
          bash scripts/remove-torch-lines.sh requirements/base.txt
          cat requirements/base.txt
          pip install -U -r requirements/notebooks.txt
          # install this package
          pip install -e .
          # double check on test requirements
          echo "Install special requirements for notebooks"
        displayName: "Install package & ..."

      - bash: |
          set -ex
          pip list
          bash scripts/sanity-check.sh
        displayName: "Sanity check / details"

      - bash: |
          set -ex
          # list all notebooks in this folder
          find . -name "*.ipynb" > all.txt
          # drop all "./" from beginning of each line
          sed -i 's/^\.\///' all.txt
          # filter out the ones that are listed in .ignore.ci
          grep -Fxv -f .ignore.ci all.txt > ci.txt
          # iterate over all listed notebooks and execute them with jupyter
          while read -r line; do
              echo "Processing $line"
              jupyter execute $line --timeout=300
          done <<< $(cat ci.txt)
        workingDirectory: "notebooks/"
        displayName: "Execute notebooks"
