Skip to content

Commit 1b899e1

Browse files
authored
feat(stablediffusion): Enable SYCL (#5144)
* 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 <[email protected]> * fix(ci): Don't set nproc on cmake Signed-off-by: Richard Palethorpe <[email protected]> --------- Signed-off-by: Richard Palethorpe <[email protected]>
1 parent 3bf13f8 commit 1b899e1

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

Makefile

+4-7
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
@@ -260,11 +260,7 @@ backend/go/image/stablediffusion-ggml/libsd.a: sources/stablediffusion-ggml.cpp
260260
$(MAKE) -C backend/go/image/stablediffusion-ggml libsd.a
261261

262262
backend-assets/grpc/stablediffusion-ggml: backend/go/image/stablediffusion-ggml/libsd.a backend-assets/grpc
263-
CGO_LDFLAGS="$(CGO_LDFLAGS)" C_INCLUDE_PATH=$(CURDIR)/backend/go/image/stablediffusion-ggml/ LIBRARY_PATH=$(CURDIR)/backend/go/image/stablediffusion-ggml/ \
264-
$(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o backend-assets/grpc/stablediffusion-ggml ./backend/go/image/stablediffusion-ggml/
265-
ifneq ($(UPX),)
266-
$(UPX) backend-assets/grpc/stablediffusion-ggml
267-
endif
263+
$(MAKE) -C backend/go/image/stablediffusion-ggml stablediffusion-ggml
268264

269265
sources/onnxruntime:
270266
mkdir -p sources/onnxruntime
@@ -809,6 +805,7 @@ docker-aio-all:
809805

810806
docker-image-intel:
811807
docker build \
808+
--progress plain \
812809
--build-arg BASE_IMAGE=intel/oneapi-basekit:2025.1.0-0-devel-ubuntu24.04 \
813810
--build-arg IMAGE_TYPE=$(IMAGE_TYPE) \
814811
--build-arg GO_TAGS="none" \

backend/cpp/llama/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ grpc-server: llama.cpp llama.cpp/examples/grpc-server
8080
@echo "Building grpc-server with $(BUILD_TYPE) build type and $(CMAKE_ARGS)"
8181
ifneq (,$(findstring sycl,$(BUILD_TYPE)))
8282
+bash -c "source $(ONEAPI_VARS); \
83-
cd llama.cpp && mkdir -p build && cd build && cmake .. $(CMAKE_ARGS) && cmake --build . --config Release $(TARGET) -j$(nproc)"
83+
cd llama.cpp && mkdir -p build && cd build && cmake .. $(CMAKE_ARGS) && cmake --build . --config Release $(TARGET)"
8484
else
85-
+cd llama.cpp && mkdir -p build && cd build && cmake .. $(CMAKE_ARGS) && cmake --build . --config Release $(TARGET) -j$(nproc)
85+
+cd llama.cpp && mkdir -p build && cd build && cmake .. $(CMAKE_ARGS) && cmake --build . --config Release $(TARGET)
8686
endif
8787
cp llama.cpp/build/bin/grpc-server .

backend/go/image/stablediffusion-ggml/Makefile

+46-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ ONEAPI_VARS?=/opt/intel/oneapi/setvars.sh
88
# keep standard at C11 and C++11
99
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
1010

11+
GOCMD?=go
12+
CGO_LDFLAGS?=
13+
GO_TAGS?=
14+
LD_FLAGS?=
15+
1116
# Disable Shared libs as we are linking on static gRPC and we can't mix shared and static
1217
CMAKE_ARGS+=-DBUILD_SHARED_LIBS=OFF
1318

@@ -21,7 +26,7 @@ else ifeq ($(BUILD_TYPE),openblas)
2126
# If build type is clblas (openCL) we set -DGGML_CLBLAST=ON -DCLBlast_DIR=/some/path
2227
else ifeq ($(BUILD_TYPE),clblas)
2328
CMAKE_ARGS+=-DGGML_CLBLAST=ON -DCLBlast_DIR=/some/path
24-
# If it's hipblas we do have also to set CC=/opt/rocm/llvm/bin/clang CXX=/opt/rocm/llvm/bin/clang++
29+
# If it's hipblas we do have also to set CC=/opt/rocm/llvm/bin/clang CXX=/opt/rocm/llvm/bin/clang++
2530
else ifeq ($(BUILD_TYPE),hipblas)
2631
CMAKE_ARGS+=-DGGML_HIP=ON
2732
# If it's OSX, DO NOT embed the metal library - -DGGML_METAL_EMBED_LIBRARY=ON requires further investigation
@@ -36,16 +41,35 @@ else ifeq ($(OS),Darwin)
3641
endif
3742
endif
3843

39-
# ifeq ($(BUILD_TYPE),sycl_f16)
40-
# CMAKE_ARGS+=-DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_SYCL_F16=ON -DSD_SYCL=ON -DGGML_SYCL_F16=ON
41-
# endif
44+
ifeq ($(BUILD_TYPE),sycl_f16)
45+
CMAKE_ARGS+=-DGGML_SYCL=ON \
46+
-DCMAKE_C_COMPILER=icx \
47+
-DCMAKE_CXX_COMPILER=icpx \
48+
-DSD_SYCL=ON \
49+
-DGGML_SYCL_F16=ON
50+
CC=icx
51+
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 )
56+
endif
4257

43-
# ifeq ($(BUILD_TYPE),sycl_f32)
44-
# CMAKE_ARGS+=-DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DSD_SYCL=ON
45-
# endif
58+
ifeq ($(BUILD_TYPE),sycl_f32)
59+
CMAKE_ARGS+=-DGGML_SYCL=ON \
60+
-DCMAKE_C_COMPILER=icx \
61+
-DCMAKE_CXX_COMPILER=icpx \
62+
-DSD_SYCL=ON
63+
CC=icx
64+
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 )
69+
endif
4670

4771
# warnings
48-
CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
72+
# CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
4973

5074
# Find all .a archives in ARCHIVE_DIR
5175
# (ggml can have different backends cpu, cuda, etc., each backend generates a .a archive)
@@ -86,11 +110,24 @@ endif
86110
$(MAKE) $(COMBINED_LIB)
87111

88112
gosd.o:
113+
ifneq (,$(findstring sycl,$(BUILD_TYPE)))
114+
+bash -c "source $(ONEAPI_VARS); \
115+
$(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c"
116+
else
89117
$(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c
118+
endif
90119

91120
libsd.a: gosd.o
92121
cp $(INCLUDE_PATH)/build/libstable-diffusion.a ./libsd.a
93122
$(AR) rcs libsd.a gosd.o
94123

124+
stablediffusion-ggml:
125+
CGO_LDFLAGS="$(CGO_LDFLAGS)" C_INCLUDE_PATH="$(INCLUDE_PATH)" LIBRARY_PATH="$(LIBRARY_PATH)" \
126+
CC="$(CC)" CXX="$(CXX)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" \
127+
$(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o ../../../../backend-assets/grpc/stablediffusion-ggml ./
128+
ifneq ($(UPX),)
129+
$(UPX) ../../../../backend-assets/grpc/stablediffusion-ggml
130+
endif
131+
95132
clean:
96-
rm -rf gosd.o libsd.a build $(COMBINED_LIB)
133+
rm -rf gosd.o libsd.a build $(COMBINED_LIB)

0 commit comments

Comments
 (0)