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

feat(ios): still enable metal backend on simulator & build metallib for simulator #120

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 0 additions & 9 deletions cpp/ggml-backend-reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@
#endif

#ifdef LM_GGML_USE_METAL
#include <TargetConditionals.h>

#if !TARGET_OS_SIMULATOR
#include "ggml-metal.h"
#endif

#endif

#ifdef LM_GGML_USE_SYCL
#include "ggml-sycl.h"
#endif
Expand Down Expand Up @@ -162,12 +157,8 @@ struct lm_ggml_backend_registry {
register_backend(lm_ggml_backend_cuda_reg());
#endif
#ifdef LM_GGML_USE_METAL

#if !TARGET_OS_SIMULATOR
register_backend(lm_ggml_backend_metal_reg());
#endif

#endif
#ifdef LM_GGML_USE_SYCL
register_backend(lm_ggml_backend_sycl_reg());
#endif
Expand Down
4 changes: 4 additions & 0 deletions cpp/ggml-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,11 @@ @implementation LMGGMLMetalClass
const bool try_metallib = true;
#endif

#if TARGET_OS_SIMULATOR
NSString * path_lib = [bundle pathForResource:@"ggml-llama-sim" ofType:@"metallib"];
#else
NSString * path_lib = [bundle pathForResource:@"ggml-llama" ofType:@"metallib"];
#endif
if (path_lib == nil) {
// Try to find the resource in the directory where the current binary located.
NSString * current_binary = [[NSProcessInfo processInfo] arguments][0];
Expand Down
6 changes: 6 additions & 0 deletions ios/RNLlamaContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ + (instancetype)initWithParams:(NSDictionary *)params onProgress:(void (^)(unsig
if (pipeline == nil) {
reasonNoMetal = [error localizedDescription];
} else {
#if TARGET_OS_SIMULATOR
defaultParams.n_gpu_layers = 0;
isMetalEnabled = false;
reasonNoMetal = @"Metal is not supported on simulator";
#else
defaultParams.n_gpu_layers = [params[@"n_gpu_layers"] intValue];
isMetalEnabled = true;
#endif
}
}
device = nil;
Expand Down
6 changes: 5 additions & 1 deletion scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ patch -p0 -d ./cpp < ./scripts/patches/common.h.patch
patch -p0 -d ./cpp < ./scripts/patches/common.cpp.patch
patch -p0 -d ./cpp < ./scripts/patches/log.cpp.patch
patch -p0 -d ./cpp < ./scripts/patches/ggml-metal.m.patch
patch -p0 -d ./cpp < ./scripts/patches/ggml-backend-reg.cpp.patch
patch -p0 -d ./cpp < ./scripts/patches/ggml.c.patch
patch -p0 -d ./cpp < ./scripts/patches/ggml-quants.c.patch
patch -p0 -d ./cpp < ./scripts/patches/llama-mmap.cpp.patch
Expand All @@ -246,6 +245,11 @@ if [ "$OS" = "Darwin" ]; then
rm ggml-metal.air
mv ./ggml-llama.metallib ../../../../cpp/ggml-llama.metallib

xcrun --sdk iphonesimulator metal -c ggml-metal.metal -o ggml-metal.air -DGGML_METAL_USE_BF16=1
xcrun --sdk iphonesimulator metallib ggml-metal.air -o ggml-llama.metallib
rm ggml-metal.air
mv ./ggml-llama.metallib ../../../../cpp/ggml-llama-sim.metallib

cd -

# Generate .xcode.env.local in iOS example
Expand Down
6 changes: 5 additions & 1 deletion scripts/build-ios.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ function build_framework() {
# Copy headers and metallib
cp_headers $4
# TODO: May need to re-build metallib for tvOS
cp ../cpp/ggml-llama.metallib ../ios/rnllama.xcframework/$4/rnllama.framework/ggml-llama.metallib
if [ "$4" == "ios-arm64_x86_64-simulator" ] || [ "$4" == "tvos-arm64_x86_64-simulator" ]; then
cp ../cpp/ggml-llama-sim.metallib ../ios/rnllama.xcframework/$4/rnllama.framework/ggml-llama-sim.metallib
else
cp ../cpp/ggml-llama.metallib ../ios/rnllama.xcframework/$4/rnllama.framework/ggml-llama.metallib
fi

rm -rf ./*
cd ..
Expand Down
30 changes: 0 additions & 30 deletions scripts/patches/ggml-backend-reg.cpp.patch

This file was deleted.

12 changes: 8 additions & 4 deletions scripts/patches/ggml-metal.m.patch
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
--- ggml-metal.m.orig 2024-12-20 13:36:22
+++ ggml-metal.m 2024-12-20 13:37:17
@@ -509,7 +509,7 @@
--- ggml-metal.m.orig 2025-02-06 12:19:16
+++ ggml-metal.m 2025-02-06 12:18:32
@@ -530,7 +530,11 @@
const bool try_metallib = true;
#endif

- NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
+#if TARGET_OS_SIMULATOR
+ NSString * path_lib = [bundle pathForResource:@"ggml-llama-sim" ofType:@"metallib"];
+#else
+ NSString * path_lib = [bundle pathForResource:@"ggml-llama" ofType:@"metallib"];
+#endif
if (path_lib == nil) {
// Try to find the resource in the directory where the current binary located.
NSString * current_binary = [[NSProcessInfo processInfo] arguments][0];
Loading