#!/usr/bin/env bash # Assemble the Windows package that has both GPU backends in one folder, from # the three zips that .github/workflows/build.yml builds for every release: # # sd---bin-win-cuda12-x64.zip programs + CUDA module (ggml-cuda.dll) # cudart-sd-bin-win-cu12-x64.zip CUDA runtime DLLs (cudart, cublas, cublasLt) # sd---bin-win-vulkan-x64.zip programs + Vulkan module (ggml-vulkan.dll) # # The package is the CUDA zip, plus the runtime DLLs, plus ggml-vulkan.dll. The # rest of the Vulkan zip is not copied: it holds the same file names built # without the CUDA code (stable-diffusion.dll differs), so unpacking one zip over # the other would leave a mixture of two builds. # # Usage: scripts/package_windows_cuda_vulkan.sh [output_dir] # Output: /sd---bin-win-cuda12-vulkan-x64.zip # (output_dir defaults to the input directory) set -euo pipefail usage() { echo "usage: $0 [output_dir]" >&2 exit 1 } fail() { echo "error: $*" >&2 exit 1 } warn() { if [[ "${GITHUB_ACTIONS:-}" == true ]]; then echo "::warning::$*" else echo "warning: $*" >&2 fi } [[ $# -ge 1 && $# -le 2 ]] || usage for tool in zip unzip; do command -v "$tool" >/dev/null 2>&1 || fail "$tool is required" done [[ -d "$1" ]] || fail "$1 is not a directory" input_dir="$(cd "$1" && pwd)" output_dir="${2:-$input_dir}" mkdir -p "$output_dir" output_dir="$(cd "$output_dir" && pwd)" shopt -s nullglob cuda_zips=("$input_dir"/sd-*-bin-win-cuda12-x64.zip) vulkan_zips=("$input_dir"/sd-*-bin-win-vulkan-x64.zip) shopt -u nullglob cudart_zip="$input_dir/cudart-sd-bin-win-cu12-x64.zip" [[ ${#cuda_zips[@]} -eq 1 ]] || fail "expected one sd-*-bin-win-cuda12-x64.zip in $input_dir, found ${#cuda_zips[@]}" [[ ${#vulkan_zips[@]} -eq 1 ]] || fail "expected one sd-*-bin-win-vulkan-x64.zip in $input_dir, found ${#vulkan_zips[@]}" [[ -f "$cudart_zip" ]] || fail "expected cudart-sd-bin-win-cu12-x64.zip in $input_dir" cuda_zip="${cuda_zips[0]}" vulkan_zip="${vulkan_zips[0]}" # sd--: the two programs zips must come from the same build. prefix="$(basename "$cuda_zip")" prefix="${prefix%-bin-win-cuda12-x64.zip}" vulkan_prefix="$(basename "$vulkan_zip")" vulkan_prefix="${vulkan_prefix%-bin-win-vulkan-x64.zip}" [[ "$prefix" == "$vulkan_prefix" ]] || fail "the CUDA and Vulkan zips come from different builds: $prefix and $vulkan_prefix" commit="${prefix##*-}" name="$prefix-bin-win-cuda12-vulkan-x64" output_zip="$output_dir/$name.zip" entries() { unzip -Z1 "$1" } overlap="$(comm -12 <(entries "$cuda_zip" | LC_ALL=C sort) <(entries "$cudart_zip" | LC_ALL=C sort))" [[ -z "$overlap" ]] || fail "the CUDA zip and the CUDA runtime zip both contain: ${overlap//$'\n'/ }" work="$(mktemp -d)" trap 'rm -rf "$work"' EXIT root="$work/$name" mkdir -p "$root" # -n: never overwrite, and never ask. unzip -q -n "$cuda_zip" -d "$root" unzip -q -n "$cudart_zip" -d "$root" unzip -q -n -j "$vulkan_zip" ggml-vulkan.dll -d "$root" || fail "ggml-vulkan.dll is not in $(basename "$vulkan_zip")" missing=() for file in sd-cli.exe sd-server.exe stable-diffusion.dll ggml.dll ggml-base.dll ggml-cuda.dll ggml-vulkan.dll; do [[ -f "$root/$file" ]] || missing+=("$file") done for pattern in 'ggml-cpu-*.dll' 'cudart64_*.dll' 'cublas64_*.dll' 'cublasLt64_*.dll'; do compgen -G "$root/$pattern" >/dev/null || missing+=("$pattern") done if ((${#missing[@]})); then fail "the package would be incomplete; missing: ${missing[*]}" fi # ggml-vulkan.dll was built next to the ggml-base.dll and ggml.dll of the Vulkan # zip and now runs with the ones of the CUDA zip. Same commit and same options # give the same size; a different size means a different ggml build. for shared in ggml-base.dll ggml.dll; do vulkan_size="$(unzip -p "$vulkan_zip" "$shared" | wc -c | tr -d ' ')" package_size="$(wc -c <"$root/$shared" | tr -d ' ')" if [[ "$vulkan_size" != "$package_size" ]]; then warn "$shared is $package_size bytes in the CUDA zip and $vulkan_size bytes in the Vulkan zip: the two builds may not share the same ggml" fi done [[ ! -e "$root/LEGGIMI.txt" ]] || fail "the zips already contain a LEGGIMI.txt, which would be overwritten" cat >"$root/LEGGIMI.txt" <