name: "Windows Release cu118 dependencies"

on:
  workflow_dispatch:
#  push:
#    branches:
#      - master

jobs:
  build_dependencies:
    env:
        # you need at least cuda 5.0 for some of the stuff compiled here.
        TORCH_CUDA_ARCH_LIST: "5.0+PTX 6.0 6.1 7.0 7.5 8.0 8.6 8.9"
        FORCE_CUDA: 1
        MAX_JOBS: 1 # will crash otherwise
        DISTUTILS_USE_SDK: 1 # otherwise distutils will complain on windows about multiple versions of msvc
        XFORMERS_BUILD_TYPE: "Release"
    runs-on: windows-latest
    steps:
        - name: Cache Built Dependencies
          uses: actions/cache@v3
          id: cache-cu118_python_stuff
          with:
            path: cu118_python_deps.tar
            key: ${{ runner.os }}-build-cu118

        - if: steps.cache-cu118_python_stuff.outputs.cache-hit != 'true'
          uses: actions/checkout@v3

        - if: steps.cache-cu118_python_stuff.outputs.cache-hit != 'true'
          uses: actions/setup-python@v4
          with:
            python-version: '3.10.9'

        - if: steps.cache-cu118_python_stuff.outputs.cache-hit != 'true'
          uses: comfyanonymous/cuda-toolkit@test
          id: cuda-toolkit
          with:
            cuda: '11.8.0'
        # copied from xformers github
        - name: Setup MSVC
          uses: ilammy/msvc-dev-cmd@v1
        - name: Configure Pagefile
          # windows runners will OOM with many CUDA architectures
          # we cheat here with a page file
          uses: al-cheb/configure-pagefile-action@v1.3
          with:
            minimum-size: 2GB
        # really unfortunate: https://github.com/ilammy/msvc-dev-cmd#name-conflicts-with-shell-bash
        - name: Remove link.exe
          shell: bash
          run: rm /usr/bin/link

        - if: steps.cache-cu118_python_stuff.outputs.cache-hit != 'true'
          shell: bash
          run: |
            python -m pip wheel --no-cache-dir torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118 -r requirements.txt pygit2 -w ./temp_wheel_dir
            python -m pip install --no-cache-dir ./temp_wheel_dir/*
            echo installed basic
            git clone --recurse-submodules https://github.com/facebookresearch/xformers.git
            cd xformers
            python -m pip install --no-cache-dir wheel setuptools twine
            echo building xformers
            python setup.py bdist_wheel -d ../temp_wheel_dir/
            cd ..
            rm -rf xformers
            ls -lah temp_wheel_dir
            mv temp_wheel_dir cu118_python_deps
            tar cf cu118_python_deps.tar cu118_python_deps


