Skip to content

Commit 6a299c0

Browse files
authored
feat(stablediffusion-ggml): respect build type (#4581)
* feat(stablediffusion-ggml): respect build type Signed-off-by: Ettore Di Giacinto <[email protected]> * combine libraries Signed-off-by: Ettore Di Giacinto <[email protected]> --------- Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 9ce71fe commit 6a299c0

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

Makefile

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,8 @@ sources/stablediffusion-ggml.cpp:
302302
git checkout $(STABLEDIFFUSION_GGML_VERSION) && \
303303
git submodule update --init --recursive --depth 1 --single-branch
304304

305-
sources/stablediffusion-ggml.cpp/build/libstable-diffusion.a: sources/stablediffusion-ggml.cpp
306-
cd sources/stablediffusion-ggml.cpp && \
307-
mkdir -p build && \
308-
cd build && \
309-
cmake $(CMAKE_ARGS) .. && \
310-
cmake --build . --config Release
311-
312-
backend/go/image/stablediffusion-ggml/libsd.a: sources/stablediffusion-ggml.cpp/build/libstable-diffusion.a
305+
backend/go/image/stablediffusion-ggml/libsd.a: sources/stablediffusion-ggml.cpp
306+
$(MAKE) -C backend/go/image/stablediffusion-ggml build/libstable-diffusion.a
313307
$(MAKE) -C backend/go/image/stablediffusion-ggml libsd.a
314308

315309
backend-assets/grpc/stablediffusion-ggml: backend/go/image/stablediffusion-ggml/libsd.a backend-assets/grpc

backend/go/image/stablediffusion-ggml/Makefile

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,85 @@ INCLUDE_PATH := $(abspath ./)
22
LIBRARY_PATH := $(abspath ./)
33

44
AR?=ar
5-
5+
CMAKE_ARGS?=
66
BUILD_TYPE?=
77
# keep standard at C11 and C++11
88
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
99

10+
# Disable Shared libs as we are linking on static gRPC and we can't mix shared and static
11+
CMAKE_ARGS+=-DBUILD_SHARED_LIBS=OFF
12+
13+
# If build type is cublas, then we set -DGGML_CUDA=ON to CMAKE_ARGS automatically
14+
ifeq ($(BUILD_TYPE),cublas)
15+
CMAKE_ARGS+=-DGGML_CUDA=ON
16+
# If build type is openblas then we set -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS
17+
# to CMAKE_ARGS automatically
18+
else ifeq ($(BUILD_TYPE),openblas)
19+
CMAKE_ARGS+=-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS
20+
# If build type is clblas (openCL) we set -DGGML_CLBLAST=ON -DCLBlast_DIR=/some/path
21+
else ifeq ($(BUILD_TYPE),clblas)
22+
CMAKE_ARGS+=-DGGML_CLBLAST=ON -DCLBlast_DIR=/some/path
23+
# If it's hipblas we do have also to set CC=/opt/rocm/llvm/bin/clang CXX=/opt/rocm/llvm/bin/clang++
24+
else ifeq ($(BUILD_TYPE),hipblas)
25+
CMAKE_ARGS+=-DGGML_HIP=ON
26+
# If it's OSX, DO NOT embed the metal library - -DGGML_METAL_EMBED_LIBRARY=ON requires further investigation
27+
# But if it's OSX without metal, disable it here
28+
else ifeq ($(OS),Darwin)
29+
ifneq ($(BUILD_TYPE),metal)
30+
CMAKE_ARGS+=-DGGML_METAL=OFF
31+
else
32+
CMAKE_ARGS+=-DGGML_METAL=ON
33+
CMAKE_ARGS+=-DGGML_METAL_EMBED_LIBRARY=ON
34+
TARGET+=--target ggml-metal
35+
endif
36+
endif
37+
38+
ifeq ($(BUILD_TYPE),sycl_f16)
39+
CMAKE_ARGS+=-DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_SYCL_F16=ON
40+
endif
41+
42+
ifeq ($(BUILD_TYPE),sycl_f32)
43+
CMAKE_ARGS+=-DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx
44+
endif
45+
1046
# warnings
1147
CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
1248

49+
# Find all .a archives in ARCHIVE_DIR
50+
# (ggml can have different backends cpu, cuda, etc., each backend generates a .a archive)
51+
GGML_ARCHIVE_DIR := build/ggml/src/
52+
ALL_ARCHIVES := $(shell find $(GGML_ARCHIVE_DIR) -type f -name '*.a')
53+
54+
# Name of the single merged library
55+
COMBINED_LIB := libggmlall.a
56+
57+
# Rule to merge all the .a files into one
58+
$(COMBINED_LIB): $(ALL_ARCHIVES)
59+
@echo "Merging all .a into $(COMBINED_LIB)"
60+
rm -f $@
61+
mkdir -p merge-tmp
62+
for a in $(ALL_ARCHIVES); do \
63+
( cd merge-tmp && ar x ../$$a ); \
64+
done
65+
( cd merge-tmp && ar rcs ../$@ *.o )
66+
# Ensure we have a proper index
67+
ranlib $@
68+
# Clean up
69+
rm -rf merge-tmp
70+
71+
build/libstable-diffusion.a:
72+
mkdir -p build && \
73+
cd build && \
74+
cmake $(CMAKE_ARGS) ../../../../../sources/stablediffusion-ggml.cpp && \
75+
cmake --build . --config Release
76+
$(MAKE) $(COMBINED_LIB)
77+
1378
gosd.o:
1479
$(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c
1580

1681
libsd.a: gosd.o
17-
cp $(INCLUDE_PATH)/../../../../sources/stablediffusion-ggml.cpp/build/libstable-diffusion.a ./libsd.a
82+
cp $(INCLUDE_PATH)/build/libstable-diffusion.a ./libsd.a
1883
$(AR) rcs libsd.a gosd.o
1984

2085
clean:
21-
rm -f gosd.o libsd.a
86+
rm -rf gosd.o libsd.a build $(COMBINED_LIB)

backend/go/image/stablediffusion-ggml/gosd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
// #cgo CXXFLAGS: -I${SRCDIR}/../../../../sources/stablediffusion-ggml.cpp/thirdparty -I${SRCDIR}/../../../../sources/stablediffusion-ggml.cpp -I${SRCDIR}/../../../../sources/stablediffusion-ggml.cpp/ggml/include
4-
// #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
4+
// #cgo LDFLAGS: -L${SRCDIR}/ -lsd -lstdc++ -lm -lggmlall -lgomp
55
// #include <gosd.h>
66
// #include <stdlib.h>
77
import "C"

0 commit comments

Comments
 (0)