name: backends

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'version'
        required: true
        type: string
      commit:
        description: 'sd.cpp commit'
        required: true
        type: string

jobs:
  windows-cpu:
    runs-on: windows-latest
    
    strategy:
      matrix:
        include:
          - build: 'noavx'
            defines: '-DGGML_NATIVE=OFF -DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx'
            defines: '-DGGML_NATIVE=OFF -DGGML_AVX2=OFF -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx2'
            defines: '-DGGML_NATIVE=OFF -DGGML_AVX2=ON -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx512'
            defines: '-DGGML_NATIVE=OFF -DGGML_AVX512=ON -DSD_BUILD_SHARED_LIBS=ON'

    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive

      - name: Build
        id: cmake_build
        run: |
          mkdir build
          cd build
          cmake .. ${{ matrix.defines }}
          cmake --build . --config Release
      
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: windows-${{ matrix.build }}
          path: .\build\bin\Release\stable-diffusion.dll

  windows-cuda12:
    runs-on: windows-latest

    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive

      - name:  Install Cuda Toolkit 12.8
        id: cuda-12
        run: |
          mkdir -p "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8"
          choco install unzip -y
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_cudart/windows-x86_64/cuda_cudart-windows-x86_64-12.8.57-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvcc/windows-x86_64/cuda_nvcc-windows-x86_64-12.8.61-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvrtc/windows-x86_64/cuda_nvrtc-windows-x86_64-12.8.61-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/libcublas/windows-x86_64/libcublas-windows-x86_64-12.8.3.14-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvtx/windows-x86_64/cuda_nvtx-windows-x86_64-12.8.55-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_profiler_api/windows-x86_64/cuda_profiler_api-windows-x86_64-12.8.55-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/visual_studio_integration/windows-x86_64/visual_studio_integration-windows-x86_64-12.8.55-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvprof/windows-x86_64/cuda_nvprof-windows-x86_64-12.8.57-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_cccl/windows-x86_64/cuda_cccl-windows-x86_64-12.8.55-archive.zip"
          unzip '*.zip' -d "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8"
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\cuda_cudart-windows-x86_64-12.8.57-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\cuda_nvcc-windows-x86_64-12.8.61-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\cuda_nvrtc-windows-x86_64-12.8.61-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\libcublas-windows-x86_64-12.8.3.14-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\cuda_nvtx-windows-x86_64-12.8.55-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\cuda_profiler_api-windows-x86_64-12.8.55-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\visual_studio_integration-windows-x86_64-12.8.55-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\cuda_nvprof-windows-x86_64-12.8.57-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\cuda_cccl-windows-x86_64-12.8.55-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\visual_studio_integration\MSBuildExtensions\*" "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Microsoft\VC\v170\BuildCustomizations" /E /I /H /Y
          echo "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
          echo "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\libnvvp" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
          echo "CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
          echo "CUDA_PATH_V12_8=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8

      - name: Build
        id: cmake_build
        shell: cmd
        run: |
          mkdir build
          cd build
          call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
          cmake .. -DSD_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="61;75;86;89;100" -DSD_BUILD_SHARED_LIBS=ON
          cmake --build . --config Release
      
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: windows-cuda12
          path: .\build\bin\Release\stable-diffusion.dll

  windows-cuda11:
    runs-on: windows-2019

    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive

      - name:  Install Cuda Toolkit 11.8
        id: cuda-11
        run: |
          mkdir -p "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8"
          choco install unzip -y
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_cudart/windows-x86_64/cuda_cudart-windows-x86_64-11.8.89-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvcc/windows-x86_64/cuda_nvcc-windows-x86_64-11.8.89-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvrtc/windows-x86_64/cuda_nvrtc-windows-x86_64-11.8.89-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/libcublas/windows-x86_64/libcublas-windows-x86_64-11.8.1.74-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvtx/windows-x86_64/cuda_nvtx-windows-x86_64-11.8.86-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_profiler_api/windows-x86_64/cuda_profiler_api-windows-x86_64-11.8.86-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/visual_studio_integration/windows-x86_64/visual_studio_integration-windows-x86_64-11.8.86-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvprof/windows-x86_64/cuda_nvprof-windows-x86_64-11.8.87-archive.zip"
          curl -O "https://developer.download.nvidia.com/compute/cuda/redist/cuda_cccl/windows-x86_64/cuda_cccl-windows-x86_64-11.8.89-archive.zip"
          unzip '*.zip' -d "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8"
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\cuda_cudart-windows-x86_64-11.8.89-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\cuda_nvcc-windows-x86_64-11.8.89-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\cuda_nvrtc-windows-x86_64-11.8.89-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\libcublas-windows-x86_64-11.8.1.74-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\cuda_nvtx-windows-x86_64-11.8.86-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\cuda_profiler_api-windows-x86_64-11.8.86-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\visual_studio_integration-windows-x86_64-11.8.86-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\cuda_nvprof-windows-x86_64-11.8.87-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\cuda_cccl-windows-x86_64-11.8.89-archive\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" /E /I /H /Y
          xcopy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\visual_studio_integration\MSBuildExtensions\*" "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VC\v160\BuildCustomizations" /E /I /H /Y
          echo "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
          echo "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\libnvvp" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
          echo "CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
          echo "CUDA_PATH_V11_8=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8

      - name: Build
        id: cmake_build
        shell: cmd
        run: |
          mkdir build
          cd build
          call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
          cmake .. -DSD_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="61;75;86;89" -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_FLAGS="-allow-unsupported-compiler"
          cmake --build . --config Release
      
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: windows-cuda11
          path: .\build\bin\Release\stable-diffusion.dll

  windows-sycl:
    runs-on: windows-latest
    
    defaults:
      run:
        shell: bash

    env:
      WINDOWS_BASEKIT_URL: https://registrationcenter-download.intel.com/akdlm/IRC_NAS/b380d914-366b-4b77-a74a-05e3c38b3514/intel-oneapi-base-toolkit-2025.0.0.882_offline.exe
      WINDOWS_DPCPP_MKL: intel.oneapi.win.cpp-dpcpp-common:intel.oneapi.win.mkl.devel:intel.oneapi.win.dnnl:intel.oneapi.win.tbb.devel
      ONEAPI_ROOT: "C:/Program Files (x86)/Intel/oneAPI"

    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive            
          
      - name: Checkout Install Script
        id: checkoutInstallScript
        uses: actions/checkout@v4.2.2
        with:
          path: scripts
          sparse-checkout: |
            Backends/install-oneapi.bat
            Backends/win-build-sycl.bat
          sparse-checkout-cone-mode: false
        
      - name: Install Sycl tools
        id: installSyclCompiler
        run:  scripts/Backends/install-oneapi.bat $WINDOWS_BASEKIT_URL $WINDOWS_DPCPP_MKL
          
      - name: Build
        id: cmake_build
        run: scripts/Backends/win-build-sycl.bat
              
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: windows-sycl
          path: .\build\bin\stable-diffusion.dll

  windows-hip:
    runs-on: windows-latest

    steps:
      - name: Install
        id: depends
        run: |
          $ErrorActionPreference = "Stop"
          write-host "Downloading AMD HIP SDK Installer"
          Invoke-WebRequest -Uri "https://download.amd.com/developer/eula/rocm-hub/AMD-Software-PRO-Edition-24.Q4-WinSvr2022-For-HIP.exe" -OutFile "${env:RUNNER_TEMP}\rocm-install.exe"
          write-host "Installing AMD HIP SDK"
          Start-Process "${env:RUNNER_TEMP}\rocm-install.exe" -ArgumentList '-install' -NoNewWindow -Wait
          write-host "Completed AMD HIP SDK installation"
      - name: Verify ROCm
        id: verify
        run: |
          & 'C:\Program Files\AMD\ROCm\*\bin\clang.exe' --version
          
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive  

      - name: Build
        id: cmake_build
        run: |
          $env:HIP_PATH=$(Resolve-Path 'C:\Program Files\AMD\ROCm\*\bin\clang.exe' | split-path | split-path)
          $env:CMAKE_PREFIX_PATH="${env:HIP_PATH}"
          mkdir build
          cd build
          cmake -G "Unix Makefiles" .. -DCMAKE_C_COMPILER="${env:HIP_PATH}\bin\clang.exe" -DCMAKE_CXX_COMPILER="${env:HIP_PATH}\bin\clang++.exe" -DGGML_HIP=ON -DCMAKE_BUILD_TYPE=Release -DAMDGPU_TARGETS="gfx1100;gfx1101;gfx1030" -DGGML_RPC=ON -DSD_BUILD_SHARED_LIBS=ON
          cmake --build . --config Release
              
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: windows-rocm6
          path: .\build\bin\stable-diffusion.dll

  windows-vulkan:
    runs-on: windows-latest
    
    env:
      VULKAN_VERSION: 1.3.261.1
    
    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive

      - name: Install Vulkan SDK
        id: get_vulkan
        run: |
          curl.exe -o $env:RUNNER_TEMP/VulkanSDK-Installer.exe -L "https://sdk.lunarg.com/sdk/download/${env:VULKAN_VERSION}/windows/VulkanSDK-${env:VULKAN_VERSION}-Installer.exe"
          & "$env:RUNNER_TEMP\VulkanSDK-Installer.exe" --accept-licenses --default-answer --confirm-command install
          Add-Content $env:GITHUB_ENV "VULKAN_SDK=C:\VulkanSDK\${env:VULKAN_VERSION}"
          Add-Content $env:GITHUB_PATH "C:\VulkanSDK\${env:VULKAN_VERSION}\bin"

      - name: Build
        id: cmake_build
        run: |
          mkdir build
          cd build
          cmake .. -DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON
          cmake --build . --config Release
      
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: windows-vulkan
          path: .\build\bin\Release\stable-diffusion.dll
          
  linux-cpu:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        include:
          - build: 'noavx'
            defines: '-DGGML_NATIVE=OFF -DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx'
            defines: '-DGGML_NATIVE=OFF -DGGML_AVX2=OFF -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx2'
            defines: '-DGGML_NATIVE=OFF -DGGML_AVX2=ON -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx512'
            defines: '-DGGML_NATIVE=OFF -DGGML_AVX512=ON -DSD_BUILD_SHARED_LIBS=ON'
            
    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive

      - name: Build
        id: cmake_build
        run: |
          mkdir build
          cd build
          cmake .. ${{ matrix.defines }}
          cmake --build . --config Release
                  
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: linux-${{ matrix.build }}
          path: ./build/bin/libstable-diffusion.so
          
  linux-cuda12:
    runs-on: ubuntu-latest
    container: nvidia/cuda:12.8.0-devel-ubuntu24.04
    
    steps:
      - name: Install dependencies
        env:
          DEBIAN_FRONTEND: noninteractive
        run: |
          apt update
          apt install -y cmake build-essential ninja-build libgomp1 git
    
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive

      - name: Build
        id: cmake_build
        run: |
          mkdir build
          cd build
          cmake .. -DSD_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="61;75;86;89;100" -DSD_BUILD_SHARED_LIBS=ON
          cmake --build . --config Release
     
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: linux-cuda12
          path: ./build/bin/libstable-diffusion.so
          
  linux-cuda11:
    runs-on: ubuntu-latest
    container: nvidia/cuda:11.8.0-devel-ubuntu22.04
    
    steps:
      - name: Install dependencies
        env:
          DEBIAN_FRONTEND: noninteractive
        run: |
          apt update
          apt install -y cmake build-essential ninja-build libgomp1 git
    
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive

      - name: Build
        id: cmake_build
        run: |
          mkdir build
          cd build
          cmake .. -DSD_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="61;75;86;89" -DSD_BUILD_SHARED_LIBS=ON
          cmake --build . --config Release
     
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: linux-cuda11
          path: ./build/bin/libstable-diffusion.so
          
  linux-sycl:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive

      - name: Install Sycl tools
        id: installSyclCompiler
        shell: bash
        run: |        
          cd /tmp
          wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
          sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
          rm GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
          sudo add-apt-repository "deb https://apt.repos.intel.com/oneapi all main"
          sudo apt update
          sudo apt install intel-oneapi-compiler-dpcpp-cpp intel-oneapi-mkl-devel
   
      - name: Build
        id: cmake_build
        run: |
          source /opt/intel/oneapi/setvars.sh
          mkdir build
          cd build
          cmake .. -DSD_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DSD_BUILD_SHARED_LIBS=ON
          cmake --build . --config Release
     
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: linux-sycl
          path: ./build/bin/libstable-diffusion.so
          
  linux-hip:
    runs-on: ubuntu-22.04
    container: rocm/dev-ubuntu-22.04:6.0.2
   
    steps:
      - name: Dependencies
        id: depends
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential git cmake rocblas-dev hipblas-dev
          hipconfig
          
      - name: Clone
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive
                    
      - name: Build
        id: cmake_build
        run: |
          mkdir build
          cd build
          cmake .. -DCMAKE_HIP_COMPILER="$(hipconfig -l)/clang" -DGGML_HIP=ON -DSD_BUILD_SHARED_LIBS=ON
          cmake --build . --config Release
               
      - name: Upload artifact Rocm
        id: upload_artifact_rocm
        uses: actions/upload-artifact@v4.6.1
        with:
          name: linux-rocm6
          path: ./build/bin/libstable-diffusion.so

  osx-cpu:
    runs-on: macos-latest
          
    strategy:
      matrix:
        include:
          - build: 'noavx'
            defines: '-DGGML_NATIVE=OFF -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx'
            defines: '-DGGML_NATIVE=OFF -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DGGML_AVX2=OFF -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx2'
            defines: '-DGGML_NATIVE=OFF -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DGGML_AVX2=ON -DSD_BUILD_SHARED_LIBS=ON'
          - build: 'avx512'
            defines: '-DGGML_NATIVE=OFF -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -DGGML_AVX512=ON -DSD_BUILD_SHARED_LIBS=ON'
   
    steps:          
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
        with:
          repository: 'leejet/stable-diffusion.cpp'
          ref: '${{ github.event.inputs.commit }}'
          submodules: recursive
   
      - name: Build
        id: cmake_build
        run: |
          mkdir build
          cd build
          cmake .. ${{ matrix.defines }}
          cmake --build . --config Release
                  
      - name: Upload artifact
        id: upload_artifact
        uses: actions/upload-artifact@v4.6.1
        with:
          name: osx-${{ matrix.build }}
          path: ./build/bin/libstable-diffusion.dylib
          
  release:
    runs-on: windows-latest

    needs:
      - windows-cpu
      - windows-cuda12
      - windows-cuda11
#     - windows-sycl
      - windows-hip
      - windows-vulkan
      - linux-cpu
      - linux-cuda12
      - linux-cuda11
      - linux-sycl
      - linux-hip
      - osx-cpu

    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4.2.2
      
      - name: Download artifacts
        id: download_artifacts
        uses: actions/download-artifact@v4.1.9
        with:
            path: Backends
      
      - name: Display structure of downloaded files
        run: ls -R

      - name: Setup nuget
        id: setup_nuget
        uses: NuGet/setup-nuget@v2.0.1

      - name: Pack
        id: pack
        run: |
          nuget pack ./Backends/StableDiffusion.NET.Backend.Cpu.nuspec -version ${{ github.event.inputs.version }}
          nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda11.Windows.nuspec -version ${{ github.event.inputs.version }}
          nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda12.Windows.nuspec -version ${{ github.event.inputs.version }}
          nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda11.Linux.nuspec -version ${{ github.event.inputs.version }}
          nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda12.Linux.nuspec -version ${{ github.event.inputs.version }}
          nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda.nuspec -version ${{ github.event.inputs.version }}
          nuget pack ./Backends/StableDiffusion.NET.Backend.Rocm.nuspec -version ${{ github.event.inputs.version }}
          nuget pack ./Backends/StableDiffusion.NET.Backend.Sycl.nuspec -version ${{ github.event.inputs.version }}
          nuget pack ./Backends/StableDiffusion.NET.Backend.Vulkan.nuspec -version ${{ github.event.inputs.version }}
          
      - name: Upload artifacts
        id: upload_artifacts
        uses: actions/upload-artifact@v4.6.1
        with:
          name: StableDiffusion.NET.Backend-Nugets
          path: ./*.nupkg
          
      - name: Nuget Push
        id: nuget_push
        run: dotnet nuget push **\*.nupkg --skip-duplicate --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json
