Skip to content

Commit 94cb20a

Browse files
authored
chore(build): Convert stablediffusion-ggml backend to Purego (#5989)
* Try converting SD to purego * chore(build): Use Purego with stablediffusion backend Signed-off-by: Richard Palethorpe <[email protected]> --------- Signed-off-by: Richard Palethorpe <[email protected]>
1 parent 47c20f9 commit 94cb20a

File tree

9 files changed

+148
-196
lines changed

9 files changed

+148
-196
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(gosd LANGUAGES C CXX)
3+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
4+
5+
add_subdirectory(./sources/stablediffusion-ggml.cpp)
6+
7+
add_library(gosd MODULE gosd.cpp)
8+
target_link_libraries(gosd PRIVATE stable-diffusion ggml stdc++fs)
9+
10+
target_include_directories(gosd PUBLIC
11+
stable-diffusion.cpp
12+
stable-diffusion.cpp/thirdparty
13+
)
14+
15+
set_property(TARGET gosd PROPERTY CXX_STANDARD 17)
16+
set_target_properties(gosd PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
Lines changed: 11 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
1-
INCLUDE_PATH := $(abspath ./)
2-
LIBRARY_PATH := $(abspath ./)
3-
4-
AR?=ar
51
CMAKE_ARGS?=
62
BUILD_TYPE?=
73
NATIVE?=false
8-
CUDA_LIBPATH?=/usr/local/cuda/lib64/
9-
ONEAPI_VARS?=/opt/intel/oneapi/setvars.sh
10-
# keep standard at C11 and C++11
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
124

135
GOCMD?=go
14-
CGO_LDFLAGS?=
15-
# Avoid parent make file overwriting CGO_LDFLAGS which is needed for hipblas
16-
CGO_LDFLAGS_SYCL=
176
GO_TAGS?=
18-
LD_FLAGS?=
197

208
# stablediffusion.cpp (ggml)
219
STABLEDIFFUSION_GGML_REPO?=https://github.com/leejet/stable-diffusion.cpp
2210
STABLEDIFFUSION_GGML_VERSION?=5900ef6605c6fbf7934239f795c13c97bc993853
2311

24-
# Disable Shared libs as we are linking on static gRPC and we can't mix shared and static
25-
CMAKE_ARGS+=-DBUILD_SHARED_LIBS=OFF -DGGML_MAX_NAME=128 -DSD_USE_SYSTEM_GGML=OFF
12+
CMAKE_ARGS+=-DGGML_MAX_NAME=128
2613

2714
ifeq ($(NATIVE),false)
2815
CMAKE_ARGS+=-DGGML_NATIVE=OFF
@@ -31,7 +18,6 @@ endif
3118
# If build type is cublas, then we set -DGGML_CUDA=ON to CMAKE_ARGS automatically
3219
ifeq ($(BUILD_TYPE),cublas)
3320
CMAKE_ARGS+=-DSD_CUDA=ON -DGGML_CUDA=ON
34-
CGO_LDFLAGS+=-lcublas -lcudart -L$(CUDA_LIBPATH) -L$(CUDA_LIBPATH)/stubs/ -lcuda
3521
# If build type is openblas then we set -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS
3622
# to CMAKE_ARGS automatically
3723
else ifeq ($(BUILD_TYPE),openblas)
@@ -46,14 +32,12 @@ else ifeq ($(BUILD_TYPE),hipblas)
4632
# But if it's OSX without metal, disable it here
4733
else ifeq ($(BUILD_TYPE),vulkan)
4834
CMAKE_ARGS+=-DSD_VULKAN=ON -DGGML_VULKAN=ON
49-
CGO_LDFLAGS+=-lvulkan
5035
else ifeq ($(OS),Darwin)
5136
ifneq ($(BUILD_TYPE),metal)
5237
CMAKE_ARGS+=-DSD_METAL=OFF -DGGML_METAL=OFF
5338
else
5439
CMAKE_ARGS+=-DSD_METAL=ON -DGGML_METAL=ON
5540
CMAKE_ARGS+=-DGGML_METAL_EMBED_LIBRARY=ON
56-
TARGET+=--target ggml-metal
5741
endif
5842
endif
5943

@@ -63,91 +47,36 @@ ifeq ($(BUILD_TYPE),sycl_f16)
6347
-DCMAKE_CXX_COMPILER=icpx \
6448
-DSD_SYCL=ON \
6549
-DGGML_SYCL_F16=ON
66-
export CC=icx
67-
export CXX=icpx
68-
CGO_LDFLAGS_SYCL += -fsycl -L${DNNLROOT}/lib -ldnnl ${MKLROOT}/lib/intel64/libmkl_sycl.a -fiopenmp -fopenmp-targets=spir64 -lOpenCL
69-
CGO_LDFLAGS_SYCL += $(shell pkg-config --libs mkl-static-lp64-gomp)
70-
CGO_CXXFLAGS += -fiopenmp -fopenmp-targets=spir64
71-
CGO_CXXFLAGS += $(shell pkg-config --cflags mkl-static-lp64-gomp )
7250
endif
7351

7452
ifeq ($(BUILD_TYPE),sycl_f32)
7553
CMAKE_ARGS+=-DGGML_SYCL=ON \
7654
-DCMAKE_C_COMPILER=icx \
7755
-DCMAKE_CXX_COMPILER=icpx \
7856
-DSD_SYCL=ON
79-
export CC=icx
80-
export CXX=icpx
81-
CGO_LDFLAGS_SYCL += -fsycl -L${DNNLROOT}/lib -ldnnl ${MKLROOT}/lib/intel64/libmkl_sycl.a -fiopenmp -fopenmp-targets=spir64 -lOpenCL
82-
CGO_LDFLAGS_SYCL += $(shell pkg-config --libs mkl-static-lp64-gomp)
83-
CGO_CXXFLAGS += -fiopenmp -fopenmp-targets=spir64
84-
CGO_CXXFLAGS += $(shell pkg-config --cflags mkl-static-lp64-gomp )
8557
endif
8658

87-
# warnings
88-
# CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
89-
90-
# Find all .a archives in ARCHIVE_DIR
91-
# (ggml can have different backends cpu, cuda, etc., each backend generates a .a archive)
92-
GGML_ARCHIVE_DIR := build/ggml/src/
93-
ALL_ARCHIVES := $(shell find $(GGML_ARCHIVE_DIR) -type f -name '*.a')
94-
ALL_OBJS := $(shell find $(GGML_ARCHIVE_DIR) -type f -name '*.o')
95-
96-
# Name of the single merged library
97-
COMBINED_LIB := libggmlall.a
98-
99-
# Instead of using the archives generated by GGML, use the object files directly to avoid overwriting objects with the same base name
100-
$(COMBINED_LIB): $(ALL_ARCHIVES)
101-
@echo "Merging all .o into $(COMBINED_LIB): $(ALL_OBJS)"
102-
rm -f $@
103-
ar -qc $@ $(ALL_OBJS)
104-
# Ensure we have a proper index
105-
ranlib $@
106-
107-
build/libstable-diffusion.a:
108-
@echo "Building SD with $(BUILD_TYPE) build type and $(CMAKE_ARGS)"
109-
ifneq (,$(findstring sycl,$(BUILD_TYPE)))
110-
+bash -c "source $(ONEAPI_VARS); \
111-
mkdir -p build && \
112-
cd build && \
113-
cmake $(CMAKE_ARGS) ../sources/stablediffusion-ggml.cpp && \
114-
cmake --build . --config Release"
115-
else
116-
mkdir -p build && \
117-
cd build && \
118-
cmake $(CMAKE_ARGS) ../sources/stablediffusion-ggml.cpp && \
119-
cmake --build . --config Release
120-
endif
121-
$(MAKE) $(COMBINED_LIB)
122-
123-
gosd.o:
124-
ifneq (,$(findstring sycl,$(BUILD_TYPE)))
125-
+bash -c "source $(ONEAPI_VARS); \
126-
$(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c"
127-
else
128-
$(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c
129-
endif
130-
131-
## stablediffusion (ggml)
13259
sources/stablediffusion-ggml.cpp:
13360
git clone --recursive $(STABLEDIFFUSION_GGML_REPO) sources/stablediffusion-ggml.cpp && \
13461
cd sources/stablediffusion-ggml.cpp && \
13562
git checkout $(STABLEDIFFUSION_GGML_VERSION) && \
13663
git submodule update --init --recursive --depth 1 --single-branch
13764

138-
libsd.a: sources/stablediffusion-ggml.cpp build/libstable-diffusion.a gosd.o
139-
cp $(INCLUDE_PATH)/build/libstable-diffusion.a ./libsd.a
140-
$(AR) rcs libsd.a gosd.o
65+
libgosd.so: sources/stablediffusion-ggml.cpp CMakeLists.txt gosd.cpp gosd.h
66+
mkdir -p build && \
67+
cd build && \
68+
cmake .. $(CMAKE_ARGS) && \
69+
cmake --build . --config Release -j$(JOBS) && \
70+
cd .. && \
71+
mv build/libgosd.so ./
14172

142-
stablediffusion-ggml: libsd.a
143-
CGO_LDFLAGS="$(CGO_LDFLAGS) $(CGO_LDFLAGS_SYCL)" C_INCLUDE_PATH="$(INCLUDE_PATH)" LIBRARY_PATH="$(LIBRARY_PATH)" \
144-
CC="$(CC)" CXX="$(CXX)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" \
145-
$(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o stablediffusion-ggml ./
73+
stablediffusion-ggml: main.go gosd.go libgosd.so
74+
CGO_ENABLED=0 $(GOCMD) build -tags "$(GO_TAGS)" -o stablediffusion-ggml ./
14675

14776
package:
14877
bash package.sh
14978

15079
build: stablediffusion-ggml package
15180

15281
clean:
153-
rm -rf gosd.o libsd.a build $(COMBINED_LIB)
82+
rm -rf libgosd.o build stablediffusion-ggml

0 commit comments

Comments
 (0)