From 08d3eef97dbe252bc889bdfc5cbc2adf9274d541 Mon Sep 17 00:00:00 2001 From: Didzis Gosko Date: Mon, 15 Apr 2024 20:23:05 +0300 Subject: [PATCH] build : fix embedded Metal library generation (#2045) --- CMakeLists.txt | 13 +++++++++++-- Makefile | 8 +++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2561dc6b..00925b1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -174,19 +174,28 @@ if (APPLE) set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_METAL_EMBED_LIBRARY) set(METALLIB_SOURCE "${CMAKE_SOURCE_DIR}/ggml-metal.metal") + set(COMMON_HEADER "${CMAKE_SOURCE_DIR}/ggml-common.h") file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/autogenerated") set(EMBED_METALLIB_ASSEMBLY "${CMAKE_BINARY_DIR}/autogenerated/ggml-embed-metallib.s") + set(EMBED_METALLIB_SOURCE "${CMAKE_BINARY_DIR}/autogenerated/ggml-metal-combined.metal") + + add_custom_command( + OUTPUT ${EMBED_METALLIB_SOURCE} + COMMAND sed -e "/^#include \\\"ggml-common.h\\\"/r ${COMMON_HEADER}" -e "/^#include \\\"ggml-common.h\\\"/d" ${METALLIB_SOURCE} > ${EMBED_METALLIB_SOURCE} + DEPENDS ${METALLIB_SOURCE} ${COMMON_HEADER} + COMMENT "Generating combined Metal library for embedding" + ) add_custom_command( OUTPUT ${EMBED_METALLIB_ASSEMBLY} COMMAND echo ".section __DATA,__ggml_metallib" > ${EMBED_METALLIB_ASSEMBLY} COMMAND echo ".globl _ggml_metallib_start" >> ${EMBED_METALLIB_ASSEMBLY} COMMAND echo "_ggml_metallib_start:" >> ${EMBED_METALLIB_ASSEMBLY} - COMMAND echo ".incbin \\\"${METALLIB_SOURCE}\\\"" >> ${EMBED_METALLIB_ASSEMBLY} + COMMAND echo ".incbin \\\"${EMBED_METALLIB_SOURCE}\\\"" >> ${EMBED_METALLIB_ASSEMBLY} COMMAND echo ".globl _ggml_metallib_end" >> ${EMBED_METALLIB_ASSEMBLY} COMMAND echo "_ggml_metallib_end:" >> ${EMBED_METALLIB_ASSEMBLY} - DEPENDS ${METALLIB_SOURCE} + DEPENDS ${EMBED_METALLIB_SOURCE} COMMENT "Generate assembly for embedded Metal library" ) diff --git a/Makefile b/Makefile index b69628e0..3dd4a630 100644 --- a/Makefile +++ b/Makefile @@ -408,17 +408,19 @@ WHISPER_OBJ += ggml-metal.o ifdef WHISPER_METAL_EMBED_LIBRARY CFLAGS += -DGGML_METAL_EMBED_LIBRARY -ggml-metal-embed.o: ggml-metal.metal +ggml-metal-embed.o: ggml-metal.metal ggml-common.h @echo "Embedding Metal library" $(eval TEMP_ASSEMBLY=$(shell mktemp)) + $(eval TEMP_METALLIB=$(shell mktemp)) + @sed "/^#include \"ggml-common.h\"/{r ggml-common.h"$$'\n'"d;}" ggml-metal.metal > $(TEMP_METALLIB) @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY) @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY) @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY) - @echo ".incbin \"$<\"" >> $(TEMP_ASSEMBLY) + @echo ".incbin \"$(TEMP_METALLIB)\"" >> $(TEMP_ASSEMBLY) @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY) @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY) @$(AS) $(TEMP_ASSEMBLY) -o $@ - @rm -f ${TEMP_ASSEMBLY} + @rm -f $(TEMP_ASSEMBLY) $(TEMP_METALLIB) WHISPER_OBJ += ggml-metal-embed.o endif