From 6a299c04a7e4a4e23188504bcb0e89488819ee1f Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sun, 12 Jan 2025 18:33:51 +0100 Subject: [PATCH] feat(stablediffusion-ggml): respect build type (#4581) * feat(stablediffusion-ggml): respect build type Signed-off-by: Ettore Di Giacinto * combine libraries Signed-off-by: Ettore Di Giacinto --------- Signed-off-by: Ettore Di Giacinto --- Makefile | 10 +-- .../go/image/stablediffusion-ggml/Makefile | 71 ++++++++++++++++++- backend/go/image/stablediffusion-ggml/gosd.go | 2 +- 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 261f2833bd76..0ec85bc35513 100644 --- a/Makefile +++ b/Makefile @@ -302,14 +302,8 @@ sources/stablediffusion-ggml.cpp: git checkout $(STABLEDIFFUSION_GGML_VERSION) && \ git submodule update --init --recursive --depth 1 --single-branch -sources/stablediffusion-ggml.cpp/build/libstable-diffusion.a: sources/stablediffusion-ggml.cpp - cd sources/stablediffusion-ggml.cpp && \ - mkdir -p build && \ - cd build && \ - cmake $(CMAKE_ARGS) .. && \ - cmake --build . --config Release - -backend/go/image/stablediffusion-ggml/libsd.a: sources/stablediffusion-ggml.cpp/build/libstable-diffusion.a +backend/go/image/stablediffusion-ggml/libsd.a: sources/stablediffusion-ggml.cpp + $(MAKE) -C backend/go/image/stablediffusion-ggml build/libstable-diffusion.a $(MAKE) -C backend/go/image/stablediffusion-ggml libsd.a backend-assets/grpc/stablediffusion-ggml: backend/go/image/stablediffusion-ggml/libsd.a backend-assets/grpc diff --git a/backend/go/image/stablediffusion-ggml/Makefile b/backend/go/image/stablediffusion-ggml/Makefile index cca9bf6e89dd..7c6d9a176bf4 100644 --- a/backend/go/image/stablediffusion-ggml/Makefile +++ b/backend/go/image/stablediffusion-ggml/Makefile @@ -2,20 +2,85 @@ INCLUDE_PATH := $(abspath ./) LIBRARY_PATH := $(abspath ./) AR?=ar - +CMAKE_ARGS?= BUILD_TYPE?= # keep standard at C11 and C++11 CXXFLAGS = -I. -I$(INCLUDE_PATH)/../../../../sources/stablediffusion-ggml.cpp/thirdparty -I$(INCLUDE_PATH)/../../../../sources/stablediffusion-ggml.cpp/ggml/include -I$(INCLUDE_PATH)/../../../../sources/stablediffusion-ggml.cpp -O3 -DNDEBUG -std=c++17 -fPIC +# Disable Shared libs as we are linking on static gRPC and we can't mix shared and static +CMAKE_ARGS+=-DBUILD_SHARED_LIBS=OFF + +# If build type is cublas, then we set -DGGML_CUDA=ON to CMAKE_ARGS automatically +ifeq ($(BUILD_TYPE),cublas) + CMAKE_ARGS+=-DGGML_CUDA=ON +# If build type is openblas then we set -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS +# to CMAKE_ARGS automatically +else ifeq ($(BUILD_TYPE),openblas) + CMAKE_ARGS+=-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS +# If build type is clblas (openCL) we set -DGGML_CLBLAST=ON -DCLBlast_DIR=/some/path +else ifeq ($(BUILD_TYPE),clblas) + CMAKE_ARGS+=-DGGML_CLBLAST=ON -DCLBlast_DIR=/some/path +# If it's hipblas we do have also to set CC=/opt/rocm/llvm/bin/clang CXX=/opt/rocm/llvm/bin/clang++ +else ifeq ($(BUILD_TYPE),hipblas) + CMAKE_ARGS+=-DGGML_HIP=ON +# If it's OSX, DO NOT embed the metal library - -DGGML_METAL_EMBED_LIBRARY=ON requires further investigation +# But if it's OSX without metal, disable it here +else ifeq ($(OS),Darwin) + ifneq ($(BUILD_TYPE),metal) + CMAKE_ARGS+=-DGGML_METAL=OFF + else + CMAKE_ARGS+=-DGGML_METAL=ON + CMAKE_ARGS+=-DGGML_METAL_EMBED_LIBRARY=ON + TARGET+=--target ggml-metal + endif +endif + +ifeq ($(BUILD_TYPE),sycl_f16) + CMAKE_ARGS+=-DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_SYCL_F16=ON +endif + +ifeq ($(BUILD_TYPE),sycl_f32) + CMAKE_ARGS+=-DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx +endif + # warnings CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function +# Find all .a archives in ARCHIVE_DIR +# (ggml can have different backends cpu, cuda, etc., each backend generates a .a archive) +GGML_ARCHIVE_DIR := build/ggml/src/ +ALL_ARCHIVES := $(shell find $(GGML_ARCHIVE_DIR) -type f -name '*.a') + +# Name of the single merged library +COMBINED_LIB := libggmlall.a + +# Rule to merge all the .a files into one +$(COMBINED_LIB): $(ALL_ARCHIVES) + @echo "Merging all .a into $(COMBINED_LIB)" + rm -f $@ + mkdir -p merge-tmp + for a in $(ALL_ARCHIVES); do \ + ( cd merge-tmp && ar x ../$$a ); \ + done + ( cd merge-tmp && ar rcs ../$@ *.o ) + # Ensure we have a proper index + ranlib $@ + # Clean up + rm -rf merge-tmp + +build/libstable-diffusion.a: + mkdir -p build && \ + cd build && \ + cmake $(CMAKE_ARGS) ../../../../../sources/stablediffusion-ggml.cpp && \ + cmake --build . --config Release + $(MAKE) $(COMBINED_LIB) + gosd.o: $(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c libsd.a: gosd.o - cp $(INCLUDE_PATH)/../../../../sources/stablediffusion-ggml.cpp/build/libstable-diffusion.a ./libsd.a + cp $(INCLUDE_PATH)/build/libstable-diffusion.a ./libsd.a $(AR) rcs libsd.a gosd.o clean: - rm -f gosd.o libsd.a \ No newline at end of file + rm -rf gosd.o libsd.a build $(COMBINED_LIB) \ No newline at end of file diff --git a/backend/go/image/stablediffusion-ggml/gosd.go b/backend/go/image/stablediffusion-ggml/gosd.go index 29d0033de96c..8c3bdb906aba 100644 --- a/backend/go/image/stablediffusion-ggml/gosd.go +++ b/backend/go/image/stablediffusion-ggml/gosd.go @@ -1,7 +1,7 @@ package main // #cgo CXXFLAGS: -I${SRCDIR}/../../../../sources/stablediffusion-ggml.cpp/thirdparty -I${SRCDIR}/../../../../sources/stablediffusion-ggml.cpp -I${SRCDIR}/../../../../sources/stablediffusion-ggml.cpp/ggml/include -// #cgo LDFLAGS: -L${SRCDIR}/ -L${SRCDIR}/../../../../sources/stablediffusion-ggml.cpp/build/ggml/src/ggml-cpu -L${SRCDIR}/../../../../sources/stablediffusion-ggml.cpp/build/ggml/src -lsd -lstdc++ -lm -lggml -lggml-base -lggml-cpu -lgomp +// #cgo LDFLAGS: -L${SRCDIR}/ -lsd -lstdc++ -lm -lggmlall -lgomp // #include // #include import "C"