From c6dd99a0237fb09edd93607b3bd3f2c051c3f4b8 Mon Sep 17 00:00:00 2001 From: Andrei Date: Fri, 9 Jun 2023 12:15:59 -0400 Subject: [PATCH 1/3] Fix issue with ggml-metal.metal path --- ggml-metal.m | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ggml-metal.m b/ggml-metal.m index 5c9ecd76e78c0..9e06dd9dee63a 100644 --- a/ggml-metal.m +++ b/ggml-metal.m @@ -71,6 +71,12 @@ // for now it is easier to work in a separate file static NSString * const msl_library_source = @"see metal.metal"; +// Here to assist with NSBundle Path Hack +@interface GGMLMetalClass : NSObject +@end +@implementation GGMLMetalClass +@end + struct ggml_metal_context * ggml_metal_init(void) { fprintf(stderr, "%s: allocating\n", __func__); @@ -106,7 +112,8 @@ NSError * error = nil; //NSString * path = [[NSBundle mainBundle] pathForResource:@"../../examples/metal/metal" ofType:@"metal"]; - NSString * path = [[NSBundle mainBundle] pathForResource:@"ggml-metal" ofType:@"metal"]; + NSBundle * bundle = [NSBundle bundleForClass:[GGMLMetalClass class]]; + NSString * path = [bundle pathForResource:@"ggml-metal" ofType:@"metal"]; fprintf(stderr, "%s: loading '%s'\n", __func__, [path UTF8String]); NSString * src = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; From 43fe774573921c3dcce8bcf04b4e0345f1f65134 Mon Sep 17 00:00:00 2001 From: Andrei Date: Fri, 9 Jun 2023 12:28:58 -0400 Subject: [PATCH 2/3] Add ggml-metal.metal as a resource for llama target --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 41f5bb7378393..84e2a88cbdc97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -218,6 +218,9 @@ if (LLAMA_METAL) # copy ggml-metal.metal to bin directory configure_file(ggml-metal.metal bin/ggml-metal.metal COPYONLY) + if (LLAMA_METAL) + set_target_properties(llama PROPERTIES RESOURCE "${CMAKE_CURRENT_SOURCE_DIR}/ggml-metal.metal") + endif() set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${FOUNDATION_LIBRARY} @@ -432,6 +435,9 @@ target_link_libraries(llama PRIVATE if (BUILD_SHARED_LIBS) set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON) target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD) + if (LLAMA_METAL) + set_target_properties(llama PROPERTIES RESOURCE "${CMAKE_CURRENT_SOURCE_DIR}/ggml-metal.metal") + endif() endif() if (GGML_SOURCES_CUDA) From 921a8e483bc0ae6e6fd798d872aebc0cc45e6476 Mon Sep 17 00:00:00 2001 From: Andrei Date: Fri, 9 Jun 2023 12:58:33 -0400 Subject: [PATCH 3/3] Update flake.nix metal kernel substitution --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 6191004496291..f3180c841bf0b 100644 --- a/flake.nix +++ b/flake.nix @@ -28,7 +28,7 @@ postPatch = if isM1 then '' substituteInPlace ./ggml-metal.m \ - --replace '[[NSBundle mainBundle] pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/ggml-metal.metal\";" + --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/ggml-metal.metal\";" '' else ""; nativeBuildInputs = with pkgs; [ cmake ]; buildInputs = osSpecific;