Skip to content

Commit 303f580

Browse files
authored
metal : fix issue with ggml-metal.metal path. Closes #1769 (#1782)
* Fix issue with ggml-metal.metal path * Add ggml-metal.metal as a resource for llama target * Update flake.nix metal kernel substitution
1 parent 059e990 commit 303f580

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Diff for: CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ if (LLAMA_METAL)
218218

219219
# copy ggml-metal.metal to bin directory
220220
configure_file(ggml-metal.metal bin/ggml-metal.metal COPYONLY)
221+
if (LLAMA_METAL)
222+
set_target_properties(llama PROPERTIES RESOURCE "${CMAKE_CURRENT_SOURCE_DIR}/ggml-metal.metal")
223+
endif()
221224

222225
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS}
223226
${FOUNDATION_LIBRARY}
@@ -432,6 +435,9 @@ target_link_libraries(llama PRIVATE
432435
if (BUILD_SHARED_LIBS)
433436
set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON)
434437
target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD)
438+
if (LLAMA_METAL)
439+
set_target_properties(llama PROPERTIES RESOURCE "${CMAKE_CURRENT_SOURCE_DIR}/ggml-metal.metal")
440+
endif()
435441
endif()
436442

437443
if (GGML_SOURCES_CUDA)

Diff for: flake.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
postPatch =
2929
if isM1 then ''
3030
substituteInPlace ./ggml-metal.m \
31-
--replace '[[NSBundle mainBundle] pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/ggml-metal.metal\";"
31+
--replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/ggml-metal.metal\";"
3232
'' else "";
3333
nativeBuildInputs = with pkgs; [ cmake ];
3434
buildInputs = osSpecific;

Diff for: ggml-metal.m

+8-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
// for now it is easier to work in a separate file
7474
static NSString * const msl_library_source = @"see metal.metal";
7575

76+
// Here to assist with NSBundle Path Hack
77+
@interface GGMLMetalClass : NSObject
78+
@end
79+
@implementation GGMLMetalClass
80+
@end
81+
7682
struct ggml_metal_context * ggml_metal_init(void) {
7783
fprintf(stderr, "%s: allocating\n", __func__);
7884

@@ -108,7 +114,8 @@
108114
NSError * error = nil;
109115

110116
//NSString * path = [[NSBundle mainBundle] pathForResource:@"../../examples/metal/metal" ofType:@"metal"];
111-
NSString * path = [[NSBundle mainBundle] pathForResource:@"ggml-metal" ofType:@"metal"];
117+
NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]];
118+
NSString * path = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
112119
fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]);
113120

114121
NSString * src = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

0 commit comments

Comments
 (0)