Skip to content

Commit 50efe41

Browse files
committed
Revert "chore(build): Convert stablediffusion-ggml backend to Purego (#5989)"
This reverts commit 94cb20a.
1 parent 576e821 commit 50efe41

File tree

9 files changed

+196
-148
lines changed

9 files changed

+196
-148
lines changed

backend/go/stablediffusion-ggml/CMakeLists.txt

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
INCLUDE_PATH := $(abspath ./)
2+
LIBRARY_PATH := $(abspath ./)
3+
4+
AR?=ar
15
CMAKE_ARGS?=
26
BUILD_TYPE?=
37
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
412

513
GOCMD?=go
14+
CGO_LDFLAGS?=
15+
# Avoid parent make file overwriting CGO_LDFLAGS which is needed for hipblas
16+
CGO_LDFLAGS_SYCL=
617
GO_TAGS?=
18+
LD_FLAGS?=
719

820
# stablediffusion.cpp (ggml)
921
STABLEDIFFUSION_GGML_REPO?=https://github.com/leejet/stable-diffusion.cpp
1022
STABLEDIFFUSION_GGML_VERSION?=5900ef6605c6fbf7934239f795c13c97bc993853
1123

12-
CMAKE_ARGS+=-DGGML_MAX_NAME=128
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
1326

1427
ifeq ($(NATIVE),false)
1528
CMAKE_ARGS+=-DGGML_NATIVE=OFF
@@ -18,6 +31,7 @@ endif
1831
# If build type is cublas, then we set -DGGML_CUDA=ON to CMAKE_ARGS automatically
1932
ifeq ($(BUILD_TYPE),cublas)
2033
CMAKE_ARGS+=-DSD_CUDA=ON -DGGML_CUDA=ON
34+
CGO_LDFLAGS+=-lcublas -lcudart -L$(CUDA_LIBPATH) -L$(CUDA_LIBPATH)/stubs/ -lcuda
2135
# If build type is openblas then we set -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS
2236
# to CMAKE_ARGS automatically
2337
else ifeq ($(BUILD_TYPE),openblas)
@@ -32,12 +46,14 @@ else ifeq ($(BUILD_TYPE),hipblas)
3246
# But if it's OSX without metal, disable it here
3347
else ifeq ($(BUILD_TYPE),vulkan)
3448
CMAKE_ARGS+=-DSD_VULKAN=ON -DGGML_VULKAN=ON
49+
CGO_LDFLAGS+=-lvulkan
3550
else ifeq ($(OS),Darwin)
3651
ifneq ($(BUILD_TYPE),metal)
3752
CMAKE_ARGS+=-DSD_METAL=OFF -DGGML_METAL=OFF
3853
else
3954
CMAKE_ARGS+=-DSD_METAL=ON -DGGML_METAL=ON
4055
CMAKE_ARGS+=-DGGML_METAL_EMBED_LIBRARY=ON
56+
TARGET+=--target ggml-metal
4157
endif
4258
endif
4359

@@ -47,36 +63,91 @@ ifeq ($(BUILD_TYPE),sycl_f16)
4763
-DCMAKE_CXX_COMPILER=icpx \
4864
-DSD_SYCL=ON \
4965
-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 )
5072
endif
5173

5274
ifeq ($(BUILD_TYPE),sycl_f32)
5375
CMAKE_ARGS+=-DGGML_SYCL=ON \
5476
-DCMAKE_C_COMPILER=icx \
5577
-DCMAKE_CXX_COMPILER=icpx \
5678
-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 )
5785
endif
5886

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)
59132
sources/stablediffusion-ggml.cpp:
60133
git clone --recursive $(STABLEDIFFUSION_GGML_REPO) sources/stablediffusion-ggml.cpp && \
61134
cd sources/stablediffusion-ggml.cpp && \
62135
git checkout $(STABLEDIFFUSION_GGML_VERSION) && \
63136
git submodule update --init --recursive --depth 1 --single-branch
64137

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 ./
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
72141

73-
stablediffusion-ggml: main.go gosd.go libgosd.so
74-
CGO_ENABLED=0 $(GOCMD) build -tags "$(GO_TAGS)" -o stablediffusion-ggml ./
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 ./
75146

76147
package:
77148
bash package.sh
78149

79150
build: stablediffusion-ggml package
80151

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

0 commit comments

Comments
 (0)