From 1b899e1a68e0dae9f931129a1295bb4cfdf71235 Mon Sep 17 00:00:00 2001 From: Richard Palethorpe Date: Thu, 10 Apr 2025 14:20:53 +0100 Subject: [PATCH] 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 * fix(ci): Don't set nproc on cmake Signed-off-by: Richard Palethorpe --------- Signed-off-by: Richard Palethorpe --- Makefile | 11 ++-- backend/cpp/llama/Makefile | 4 +- .../go/image/stablediffusion-ggml/Makefile | 55 ++++++++++++++++--- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index dad20fe2..98bfa0d5 100644 --- a/Makefile +++ b/Makefile @@ -21,8 +21,8 @@ BARKCPP_REPO?=https://github.com/PABannier/bark.cpp.git BARKCPP_VERSION?=v1.0.0 # stablediffusion.cpp (ggml) -STABLEDIFFUSION_GGML_REPO?=https://github.com/leejet/stable-diffusion.cpp -STABLEDIFFUSION_GGML_VERSION?=19d876ee300a055629926ff836489901f734f2b7 +STABLEDIFFUSION_GGML_REPO?=https://github.com/richiejp/stable-diffusion.cpp +STABLEDIFFUSION_GGML_VERSION?=6c848a2e0053c86984023570e706aa212933d50d ONNX_VERSION?=1.20.0 ONNX_ARCH?=x64 @@ -260,11 +260,7 @@ backend/go/image/stablediffusion-ggml/libsd.a: sources/stablediffusion-ggml.cpp $(MAKE) -C backend/go/image/stablediffusion-ggml libsd.a backend-assets/grpc/stablediffusion-ggml: backend/go/image/stablediffusion-ggml/libsd.a backend-assets/grpc - CGO_LDFLAGS="$(CGO_LDFLAGS)" C_INCLUDE_PATH=$(CURDIR)/backend/go/image/stablediffusion-ggml/ LIBRARY_PATH=$(CURDIR)/backend/go/image/stablediffusion-ggml/ \ - $(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o backend-assets/grpc/stablediffusion-ggml ./backend/go/image/stablediffusion-ggml/ -ifneq ($(UPX),) - $(UPX) backend-assets/grpc/stablediffusion-ggml -endif + $(MAKE) -C backend/go/image/stablediffusion-ggml stablediffusion-ggml sources/onnxruntime: mkdir -p sources/onnxruntime @@ -809,6 +805,7 @@ docker-aio-all: docker-image-intel: docker build \ + --progress plain \ --build-arg BASE_IMAGE=intel/oneapi-basekit:2025.1.0-0-devel-ubuntu24.04 \ --build-arg IMAGE_TYPE=$(IMAGE_TYPE) \ --build-arg GO_TAGS="none" \ diff --git a/backend/cpp/llama/Makefile b/backend/cpp/llama/Makefile index 24db9356..21aea285 100644 --- a/backend/cpp/llama/Makefile +++ b/backend/cpp/llama/Makefile @@ -80,8 +80,8 @@ grpc-server: llama.cpp llama.cpp/examples/grpc-server @echo "Building grpc-server with $(BUILD_TYPE) build type and $(CMAKE_ARGS)" ifneq (,$(findstring sycl,$(BUILD_TYPE))) +bash -c "source $(ONEAPI_VARS); \ - cd llama.cpp && mkdir -p build && cd build && cmake .. $(CMAKE_ARGS) && cmake --build . --config Release $(TARGET) -j$(nproc)" + cd llama.cpp && mkdir -p build && cd build && cmake .. $(CMAKE_ARGS) && cmake --build . --config Release $(TARGET)" else - +cd llama.cpp && mkdir -p build && cd build && cmake .. $(CMAKE_ARGS) && cmake --build . --config Release $(TARGET) -j$(nproc) + +cd llama.cpp && mkdir -p build && cd build && cmake .. $(CMAKE_ARGS) && cmake --build . --config Release $(TARGET) endif cp llama.cpp/build/bin/grpc-server . diff --git a/backend/go/image/stablediffusion-ggml/Makefile b/backend/go/image/stablediffusion-ggml/Makefile index f92c3a77..e76710ee 100644 --- a/backend/go/image/stablediffusion-ggml/Makefile +++ b/backend/go/image/stablediffusion-ggml/Makefile @@ -8,6 +8,11 @@ ONEAPI_VARS?=/opt/intel/oneapi/setvars.sh # keep standard at C11 and C++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 +GOCMD?=go +CGO_LDFLAGS?= +GO_TAGS?= +LD_FLAGS?= + # Disable Shared libs as we are linking on static gRPC and we can't mix shared and static CMAKE_ARGS+=-DBUILD_SHARED_LIBS=OFF @@ -21,7 +26,7 @@ else ifeq ($(BUILD_TYPE),openblas) # If build type is clblas (openCL) we set -DGGML_CLBLAST=ON -DCLBlast_DIR=/some/path else ifeq ($(BUILD_TYPE),clblas) CMAKE_ARGS+=-DGGML_CLBLAST=ON -DCLBlast_DIR=/some/path -# If it's hipblas we do have also to set CC=/opt/rocm/llvm/bin/clang CXX=/opt/rocm/llvm/bin/clang++ +# If it's hipblas we do have also to set CC=/opt/rocm/llvm/bin/clang CXX=/opt/rocm/llvm/bin/clang++ else ifeq ($(BUILD_TYPE),hipblas) CMAKE_ARGS+=-DGGML_HIP=ON # 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) endif endif -# ifeq ($(BUILD_TYPE),sycl_f16) -# CMAKE_ARGS+=-DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_SYCL_F16=ON -DSD_SYCL=ON -DGGML_SYCL_F16=ON -# endif +ifeq ($(BUILD_TYPE),sycl_f16) + CMAKE_ARGS+=-DGGML_SYCL=ON \ + -DCMAKE_C_COMPILER=icx \ + -DCMAKE_CXX_COMPILER=icpx \ + -DSD_SYCL=ON \ + -DGGML_SYCL_F16=ON + CC=icx + CXX=icpx + CGO_LDFLAGS += -fsycl -L${DNNLROOT}/lib -ldnnl ${MKLROOT}/lib/intel64/libmkl_sycl.a -fiopenmp -fopenmp-targets=spir64 -lOpenCL + CGO_LDFLAGS += $(shell pkg-config --libs mkl-static-lp64-gomp) + CGO_CXXFLAGS += -fiopenmp -fopenmp-targets=spir64 + CGO_CXXFLAGS += $(shell pkg-config --cflags mkl-static-lp64-gomp ) +endif -# ifeq ($(BUILD_TYPE),sycl_f32) -# CMAKE_ARGS+=-DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DSD_SYCL=ON -# endif +ifeq ($(BUILD_TYPE),sycl_f32) + CMAKE_ARGS+=-DGGML_SYCL=ON \ + -DCMAKE_C_COMPILER=icx \ + -DCMAKE_CXX_COMPILER=icpx \ + -DSD_SYCL=ON + CC=icx + CXX=icpx + CGO_LDFLAGS += -fsycl -L${DNNLROOT}/lib -ldnnl ${MKLROOT}/lib/intel64/libmkl_sycl.a -fiopenmp -fopenmp-targets=spir64 -lOpenCL + CGO_LDFLAGS += $(shell pkg-config --libs mkl-static-lp64-gomp) + CGO_CXXFLAGS += -fiopenmp -fopenmp-targets=spir64 + CGO_CXXFLAGS += $(shell pkg-config --cflags mkl-static-lp64-gomp ) +endif # warnings -CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function +# CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function # Find all .a archives in ARCHIVE_DIR # (ggml can have different backends cpu, cuda, etc., each backend generates a .a archive) @@ -86,11 +110,24 @@ endif $(MAKE) $(COMBINED_LIB) gosd.o: +ifneq (,$(findstring sycl,$(BUILD_TYPE))) + +bash -c "source $(ONEAPI_VARS); \ + $(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c" +else $(CXX) $(CXXFLAGS) gosd.cpp -o gosd.o -c +endif libsd.a: gosd.o cp $(INCLUDE_PATH)/build/libstable-diffusion.a ./libsd.a $(AR) rcs libsd.a gosd.o +stablediffusion-ggml: + CGO_LDFLAGS="$(CGO_LDFLAGS)" C_INCLUDE_PATH="$(INCLUDE_PATH)" LIBRARY_PATH="$(LIBRARY_PATH)" \ + CC="$(CC)" CXX="$(CXX)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" \ + $(GOCMD) build -ldflags "$(LD_FLAGS)" -tags "$(GO_TAGS)" -o ../../../../backend-assets/grpc/stablediffusion-ggml ./ +ifneq ($(UPX),) + $(UPX) ../../../../backend-assets/grpc/stablediffusion-ggml +endif + clean: - rm -rf gosd.o libsd.a build $(COMBINED_LIB) \ No newline at end of file + rm -rf gosd.o libsd.a build $(COMBINED_LIB)