Skip to content

Commit 897314e

Browse files
committed
feat(sycl): Enable SYCL for stable diffusion
This is a pain because we compile with CGO, but SD is compiled with CMake. I don't think we can easily use CMake to set the linker flags necessary. Also I could not find pkg-config calls that would fully set the flags, so some of them are set manually. See https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl-link-line-advisor.html for reference. I also resorted to searching the shared object files in MKLROOT/lib for the symbols. Signed-off-by: Richard Palethorpe <io@richiejp.com>
1 parent c68d0c4 commit 897314e

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ BARKCPP_REPO?=https://github.com/PABannier/bark.cpp.git
2121
BARKCPP_VERSION?=v1.0.0
2222

2323
# stablediffusion.cpp (ggml)
24-
STABLEDIFFUSION_GGML_REPO?=https://github.com/leejet/stable-diffusion.cpp
25-
STABLEDIFFUSION_GGML_VERSION?=19d876ee300a055629926ff836489901f734f2b7
24+
STABLEDIFFUSION_GGML_REPO?=https://github.com/richiejp/stable-diffusion.cpp
25+
STABLEDIFFUSION_GGML_VERSION?=6c848a2e0053c86984023570e706aa212933d50d
2626

2727
ONNX_VERSION?=1.20.0
2828
ONNX_ARCH?=x64
@@ -805,6 +805,7 @@ docker-aio-all:
805805

806806
docker-image-intel:
807807
docker build \
808+
--progress plain \
808809
--build-arg BASE_IMAGE=intel/oneapi-basekit:2025.1.0-0-devel-ubuntu24.04 \
809810
--build-arg IMAGE_TYPE=$(IMAGE_TYPE) \
810811
--build-arg GO_TAGS="none" \

backend/go/image/stablediffusion-ggml/Makefile

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,30 @@ ifeq ($(BUILD_TYPE),sycl_f16)
4646
-DCMAKE_C_COMPILER=icx \
4747
-DCMAKE_CXX_COMPILER=icpx \
4848
-DSD_SYCL=ON \
49-
-DGGML_SYCL_F16=ON \
50-
-DCMAKE_CXX_FLAGS="-fsycl"
49+
-DGGML_SYCL_F16=ON
5150
CC=icx
5251
CXX=icpx
52+
CGO_LDFLAGS += -fsycl -L${DNNLROOT}/lib -ldnnl ${MKLROOT}/lib/intel64/libmkl_sycl.a -fiopenmp -fopenmp-targets=spir64 -lOpenCL
53+
CGO_LDFLAGS += $(shell pkg-config --libs mkl-static-lp64-gomp)
54+
CGO_CXXFLAGS += -fiopenmp -fopenmp-targets=spir64
55+
CGO_CXXFLAGS += $(shell pkg-config --cflags mkl-static-lp64-gomp )
5356
endif
5457

5558
ifeq ($(BUILD_TYPE),sycl_f32)
5659
CMAKE_ARGS+=-DGGML_SYCL=ON \
5760
-DCMAKE_C_COMPILER=icx \
5861
-DCMAKE_CXX_COMPILER=icpx \
59-
-DSD_SYCL=ON \
60-
-DCMAKE_CXX_FLAGS="-fsycl"
62+
-DSD_SYCL=ON
6163
CC=icx
6264
CXX=icpx
65+
CGO_LDFLAGS += -fsycl -L${DNNLROOT}/lib -ldnnl ${MKLROOT}/lib/intel64/libmkl_sycl.a -fiopenmp -fopenmp-targets=spir64 -lOpenCL
66+
CGO_LDFLAGS += $(shell pkg-config --libs mkl-static-lp64-gomp)
67+
CGO_CXXFLAGS += -fiopenmp -fopenmp-targets=spir64
68+
CGO_CXXFLAGS += $(shell pkg-config --cflags mkl-static-lp64-gomp )
6369
endif
6470

6571
# warnings
66-
CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
72+
# CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
6773

6874
# Find all .a archives in ARCHIVE_DIR
6975
# (ggml can have different backends cpu, cuda, etc., each backend generates a .a archive)
@@ -94,34 +100,33 @@ ifneq (,$(findstring sycl,$(BUILD_TYPE)))
94100
mkdir -p build && \
95101
cd build && \
96102
cmake $(CMAKE_ARGS) ../../../../../sources/stablediffusion-ggml.cpp && \
97-
cmake --build . --config Release -j$(nproc)"
103+
cmake --build . --config Release"
98104
else
99105
mkdir -p build && \
100106
cd build && \
101107
cmake $(CMAKE_ARGS) ../../../../../sources/stablediffusion-ggml.cpp && \
102-
cmake --build . --config Release -j$(nproc)
108+
cmake --build . --config Release
103109
endif
104110
$(MAKE) $(COMBINED_LIB)
105111

106112
gosd.o:
107113
ifneq (,$(findstring sycl,$(BUILD_TYPE)))
108-
+bash -c "source $(ONEAPI_VARS)
109-
endif
114+
+bash -c "source $(ONEAPI_VARS); \
115+
$(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c"
116+
else
110117
$(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c
118+
endif
111119

112120
libsd.a: gosd.o
113121
cp $(INCLUDE_PATH)/build/libstable-diffusion.a ./libsd.a
114122
$(AR) rcs libsd.a gosd.o
115123

116124
stablediffusion-ggml:
117-
ifneq (,$(findstring sycl,$(BUILD_TYPE)))
118-
+bash -c "source $(ONEAPI_VARS)
119-
endif
120125
CGO_LDFLAGS="$(CGO_LDFLAGS)" C_INCLUDE_PATH="$(INCLUDE_PATH)" LIBRARY_PATH="$(LIBRARY_PATH)" \
121-
CC=$(CC) CXX=$(CXX) \
122-
$(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o backend-assets/grpc/stablediffusion-ggml ./backend/go/image/stablediffusion-ggml/
126+
CC="$(CC)" CXX="$(CXX)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" \
127+
$(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o ../../../../backend-assets/grpc/stablediffusion-ggml ./
123128
ifneq ($(UPX),)
124-
$(UPX) backend-assets/grpc/stablediffusion-ggml
129+
$(UPX) ../../../../backend-assets/grpc/stablediffusion-ggml
125130
endif
126131

127132
clean:

0 commit comments

Comments
 (0)