Skip to content

Commit

Permalink
fix(ios): build metallib
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Mar 20, 2024
1 parent 0acc180 commit 35bb24a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ android/keystores/debug.keystore
lib/

docs/API/.nojekyll

*.metallib
6 changes: 3 additions & 3 deletions cpp/ggml-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ static void lm_ggml_metal_log(enum lm_ggml_log_level level, const char * format,
const bool try_metallib = true;
#endif

NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
NSString * path_lib = [bundle pathForResource:@"ggml-llama" ofType:@"metallib"];
if (try_metallib && path_lib != nil) {
// pre-compiled library found
NSURL * libURL = [NSURL fileURLWithPath:path_lib];
Expand Down Expand Up @@ -329,9 +329,9 @@ static void lm_ggml_metal_log(enum lm_ggml_log_level level, const char * format,
LM_GGML_METAL_LOG_INFO("%s: LM_GGML_METAL_PATH_RESOURCES = %s\n", __func__, path_resource ? [path_resource UTF8String] : "nil");

if (path_resource) {
path_source = [path_resource stringByAppendingPathComponent:@"ggml-metal-llama.metal"];
path_source = [path_resource stringByAppendingPathComponent:@"ggml-metal.metal"];
} else {
path_source = [bundle pathForResource:@"ggml-metal-llama" ofType:@"metal"];
path_source = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
}

if (path_source == nil) {
Expand Down
1 change: 1 addition & 0 deletions example/ios/.xcode.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export NODE_BINARY="/Users/jhen/.nvm/versions/node/v20.10.0/bin/node"
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ SPEC CHECKSUMS:
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
hermes-engine: 10fbd3f62405c41ea07e71973ea61e1878d07322
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
llama-rn: f4d2ce3b00ccb7b70760b2a07f7751bc366bc462
llama-rn: 1672eecf8df2623f19c4b7493c4642433213bc50
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: a2faf4bad4e438ca37b2040cb8f7799baa065c18
RCTTypeSafety: cb09f3e4747b6d18331a15eb05271de7441ca0b3
Expand Down
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default function App() {
initLlama({
model: file.uri,
use_mlock: true,
n_gpu_layers: Platform.OS === 'ios' ? 0 : 0, // > 0: enable GPU
n_gpu_layers: Platform.OS === 'ios' ? 1 : 0, // > 0: enable GPU
// embedding: true,
})
.then((ctx) => {
Expand Down
2 changes: 1 addition & 1 deletion llama-rn.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/mybigday/llama.rn.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm}", "cpp/**/*.{h,cpp,hpp,c,m,mm}"
s.resources = "cpp/**/*.{metal}"
s.resources = "cpp/**/*.{metallib}"

s.dependency "React-Core"

Expand Down
10 changes: 9 additions & 1 deletion scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ cp ./llama.cpp/ggml.h ./cpp/ggml.h
cp ./llama.cpp/ggml.c ./cpp/ggml.c
cp ./llama.cpp/ggml-metal.h ./cpp/ggml-metal.h
cp ./llama.cpp/ggml-metal.m ./cpp/ggml-metal.m
cp ./llama.cpp/ggml-metal.metal ./cpp/ggml-metal-llama.metal
cp ./llama.cpp/ggml-alloc.h ./cpp/ggml-alloc.h
cp ./llama.cpp/ggml-alloc.c ./cpp/ggml-alloc.c
cp ./llama.cpp/ggml-backend.h ./cpp/ggml-backend.h
Expand Down Expand Up @@ -79,3 +78,12 @@ patch -p0 -d ./cpp < ./scripts/common.cpp.patch
patch -p0 -d ./cpp < ./scripts/log.h.patch
patch -p0 -d ./cpp < ./scripts/llama.cpp.patch
patch -p0 -d ./cpp < ./scripts/ggml-metal.m.patch

# Build metallib (~1.4MB)
if [ "$OS" = "Darwin" ]; then
cd llama.cpp
xcrun --sdk iphoneos metal -c ggml-metal.metal -o ggml-metal.air
xcrun --sdk iphoneos metallib ggml-metal.air -o ggml-llama.metallib
rm ggml-metal.air
cp ./ggml-llama.metallib ../cpp/ggml-llama.metallib
fi
23 changes: 10 additions & 13 deletions scripts/ggml-metal.m.patch
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
--- ggml-metal.m.orig 2024-03-20 12:34:08
+++ ggml-metal.m 2024-03-20 12:35:42
@@ -329,9 +329,9 @@
LM_GGML_METAL_LOG_INFO("%s: LM_GGML_METAL_PATH_RESOURCES = %s\n", __func__, path_resource ? [path_resource UTF8String] : "nil");
--- ggml-metal.m.orig 2024-03-20 13:48:14
+++ ggml-metal.m 2024-03-20 13:46:42
@@ -301,7 +301,7 @@
const bool try_metallib = true;
#endif

if (path_resource) {
- path_source = [path_resource stringByAppendingPathComponent:@"ggml-metal.metal"];
+ path_source = [path_resource stringByAppendingPathComponent:@"ggml-metal-llama.metal"];
} else {
- path_source = [bundle pathForResource:@"ggml-metal" ofType:@"metal"];
+ path_source = [bundle pathForResource:@"ggml-metal-llama" ofType:@"metal"];
}

if (path_source == nil) {
- NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
+ NSString * path_lib = [bundle pathForResource:@"ggml-llama" ofType:@"metallib"];
if (try_metallib && path_lib != nil) {
// pre-compiled library found
NSURL * libURL = [NSURL fileURLWithPath:path_lib];

0 comments on commit 35bb24a

Please sign in to comment.