Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed Metal library source into compiled binary #1842

Merged
merged 4 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if (APPLE)
option(WHISPER_METAL_NDEBUG "whisper: disable Metal debugging" OFF)
option(WHISPER_COREML "whisper: enable Core ML framework" OFF)
option(WHISPER_COREML_ALLOW_FALLBACK "whisper: allow non-CoreML fallback" OFF)
option(WHISPER_EMBED_METAL_LIBRARY "whisper: embed Metal library" OFF)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to WHISPER_METAL_EMBED_LIBRARY

Copy link
Contributor Author

@didzis didzis Feb 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated also the preprocessor directive name

else()
option(WHISPER_BLAS "whisper: use BLAS libraries" OFF)
option(WHISPER_BLAS_VENDOR "whisper: BLAS library vendor" Generic)
Expand Down Expand Up @@ -147,6 +148,12 @@ if (APPLE)

# copy ggml-metal.metal to bin directory
configure_file(ggml-metal.metal bin/ggml-metal.metal COPYONLY)

if (WHISPER_EMBED_METAL_LIBRARY)
enable_language(ASM)
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_EMBED_METAL_LIBRARY)
set(GGML_SOURCES_METAL ${GGML_SOURCES_METAL} ggml-metal-embed.s)
endif()
endif()

if (WHISPER_COREML)
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ ggml-metal.o: ggml-metal.m ggml-metal.h
$(CC) $(CFLAGS) -c $< -o $@

WHISPER_OBJ += ggml-metal.o

ifdef WHISPER_EMBED_METAL_LIBRARY
CFLAGS += -DGGML_EMBED_METAL_LIBRARY

ggml-metal-embed.o: ggml-metal-embed.s
$(AS) $< -o $@

WHISPER_OBJ += ggml-metal-embed.o
endif
endif

libwhisper.a: $(WHISPER_OBJ)
Expand Down
7 changes: 7 additions & 0 deletions ggml-metal-embed.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.section __DATA, __ggml_metallib
.globl _ggml_metallib_start
_ggml_metallib_start:
.incbin "ggml-metal.metal"
.globl _ggml_metallib_end
_ggml_metallib_end:

9 changes: 9 additions & 0 deletions ggml-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
return NULL;
}
} else {
#if GGML_EMBED_METAL_LIBRARY
GGML_METAL_LOG_INFO("%s: using embedded metal library\n", __func__);

extern const char ggml_metallib_start[];
extern const char ggml_metallib_end[];

NSString * src = [[NSString alloc] initWithBytes:ggml_metallib_start length:(ggml_metallib_end-ggml_metallib_start) encoding:NSUTF8StringEncoding];
#else
GGML_METAL_LOG_INFO("%s: default.metallib not found, loading from source\n", __func__);

NSString * sourcePath;
Expand All @@ -293,6 +301,7 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
return NULL;
}
#endif

@autoreleasepool {
// dictionary of preprocessor macros
Expand Down
Loading