#!/usr/bin/env bash # Build stable-diffusion.cpp with the sasori TQ{K}P ternary kernel. # # The GGUFs in this repo use a CUSTOM ggml quantization type (TQ3P, ggml type id 43) that upstream # stable-diffusion.cpp does NOT know: an unpatched binary will refuse to load them. These two # patches add the type end to end (block layout, CPU vec_dot, CUDA dispatch), so this script is # not optional packaging -- it is how you get a runtime that can read the weights. # # Verified 2026-07-29 against sd.cpp master-802-e92e86f / ggml eced84c8 (25 hunks, 0 rejects). set -euo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SDCPP_REF="${SDCPP_REF:-master}" # pin to a tag/sha for a byte-reproducible build CUDA_ARCH="${CUDA_ARCH:-}" # e.g. 80 (A100) 86 (3090) 89 (4090) 90 (H100); empty = native BUILD_CUDA="${BUILD_CUDA:-ON}" echo "== clone sd.cpp ($SDCPP_REF) ==" [ -d stable-diffusion.cpp ] || git clone --recursive https://github.com/leejet/stable-diffusion.cpp cd stable-diffusion.cpp git fetch --all --tags -q git checkout -q "$SDCPP_REF" git submodule update --init --recursive -q echo "== apply the TQ{K}P kernel (into the ggml submodule) ==" cd ggml # ggml type ids 42..60 must be FREE for the sasori types; if upstream ever claims them the check # below fails loudly instead of silently producing a binary that misreads every tensor. grep -q "GGML_TYPE_COUNT = 42," include/ggml.h || { echo "!! this ggml revision does not have COUNT=42: the TQ{K}P type ids would collide." echo "!! pin SDCPP_REF to a revision where they are free, or re-port the enum block by hand." exit 1; } for p in cpu cuda; do [ "$p" = cuda ] && [ "$BUILD_CUDA" != ON ] && continue git apply --check "$HERE/sdcpp-$p.patch" || { echo "!! sdcpp-$p.patch does not apply to $SDCPP_REF"; exit 1; } git apply "$HERE/sdcpp-$p.patch" echo " applied sdcpp-$p.patch" done cd .. echo "== build ==" CMAKE_ARGS=(-DCMAKE_BUILD_TYPE=Release -DSD_CUDA="$BUILD_CUDA") [ -n "$CUDA_ARCH" ] && CMAKE_ARGS+=(-DCMAKE_CUDA_ARCHITECTURES="$CUDA_ARCH") cmake -B build "${CMAKE_ARGS[@]}" cmake --build build -j"$(nproc)" echo echo "OK -> $(pwd)/build/bin/sd-cli" echo "Sanity check (the type must be known to the binary):" echo " strings build/bin/sd-cli | grep TQ3P"