Skip to content

Commit 01eb543

Browse files
committed
fix(): fix llama compilation for ios
1 parent 4fd0b11 commit 01eb543

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

bin/install.sh

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ if [ "$host" == "Darwin" ]; then
160160
die $? "not ok - missing build tools, try \"$(advice "libtool")\""
161161
quiet command -v curl
162162
die $? "not ok - missing curl, try \"$(advice "curl")\""
163+
if ! brew list | grep libomp >/dev/null 2>&1; then
164+
die $? "not ok - missing libomp, try \"$(advice "libomp")\""
165+
fi
163166
fi
164167

165168
if [ "$host" == "Linux" ]; then
@@ -286,6 +289,8 @@ function _build_cli {
286289
static_libs+=("$BUILD_DIR/$arch-$platform/lib/libuv.a")
287290
static_libs+=("$BUILD_DIR/$arch-$platform/lib/libllama.a")
288291
static_libs+=("$BUILD_DIR/$arch-$platform/lib/libsocket-runtime.a")
292+
elif [[ "$(uname -s)" == "Darwin" ]]; then
293+
cflags+=("-fopenmp")
289294
fi
290295

291296
libs=($(find "$root/build/$arch-$platform/lib$d/*" 2>/dev/null))
@@ -818,7 +823,7 @@ function _compile_llama_metal {
818823
mkdir -p "$STAGING_DIR/build/"
819824
mkdir -p ../lib
820825

821-
xcrun -sdk $sdk metal -O3 -c ggml-metal.metal -o ggml-metal.air
826+
xcrun -sdk $sdk metal -O3 -c ggml/src/ggml-metal/ggml-metal.metal -o ggml-metal.air
822827
xcrun -sdk $sdk metallib ggml-metal.air -o ../lib/default.metallib
823828
rm *.air
824829

@@ -893,15 +898,26 @@ function _compile_llama {
893898

894899
rm -f "$root/build/$(host_arch)-desktop/lib$d"/*.{so,la,dylib}*
895900
return
896-
elif [ "$platform" == "iPhoneOS" ] || [ "$platform" == "iPhoneSimulator" ]; then
901+
#elif [ "$platform" == "iPhoneOS" ] || [ "$platform" == "iPhoneSimulator" ]; then
902+
elif [ "$platform" == "iPhoneOS" ]; then
897903
# https://github.com/ggerganov/llama.cpp/discussions/4508
898904

899905
local ar="$(xcrun -sdk $sdk -find ar)"
900906
local cc="$(xcrun -sdk $sdk -find clang)"
901907
local cxx="$(xcrun -sdk $sdk -find clang++)"
902-
local cflags="--target=$target-apple-ios -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk -m$sdk-version-min=$SDKMINVERSION -DLLAMA_METAL_EMBED_LIBRARY=ON"
908+
local cflags="--target=$target-apple-ios -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk -m$sdk-version-min=$SDKMINVERSION -DLLAMA_METAL_EMBED_LIBRARY=ON -DUSE_NEON_DOTPROD -march=armv8.2-a+dotprod"
909+
910+
export AR="$ar"
911+
export CFLAGS="$cflags"
912+
export CXXFLAGS="$cflags"
913+
export CXX="$cxx"
914+
export CC="$cc"
915+
export SDKROOT="$PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk"
903916

904-
AR="$ar" CFLAGS="$cflags" CXXFLAGS="$cflags" CXX="$cxx" CC="$cc" make libllama.a
917+
quiet cmake -S . -B build -DCMAKE_OSX_ARCHITECTURES="$target" -DCMAKE_OSX_SYSROOT="$SDKROOT" -DCMAKE_C_COMPILER="$cc" -DCMAKE_CXX_COMPILER="$cxx" -DCMAKE_INSTALL_PREFIX="$BUILD_DIR/$target-$platform" -DLLAMA_NATIVE=OFF -DGGML_ARM_DOTPROD=ON ${cmake_args[@]} &&
918+
quiet cmake --build build &&
919+
quiet cmake --build build -- -j"$CPU_CORES" &&
920+
quiet cmake --install build
905921

906922
if [ ! $? = 0 ]; then
907923
die $? "not ok - Unable to compile libllama for '$platform'"
@@ -937,6 +953,7 @@ function _compile_llama {
937953
cd "$BUILD_DIR" || exit 1
938954
rm -f "$root/build/$target-$platform/lib$d"/*.{so,la,dylib}*
939955
echo "ok - built llama for $target-$platform"
956+
return 0
940957
}
941958

942959
function _compile_libuv {
@@ -1114,8 +1131,8 @@ if [[ "$(uname -s)" == "Darwin" ]] && [[ -z "$NO_IOS" ]]; then
11141131
quiet xcode-select -p
11151132
die $? "not ok - xcode needs to be installed from the mac app store: https://apps.apple.com/us/app/xcode/id497799835"
11161133

1117-
SDKMINVERSION="8.0"
1118-
export IPHONEOS_DEPLOYMENT_TARGET="8.0"
1134+
SDKMINVERSION="13.0"
1135+
export IPHONEOS_DEPLOYMENT_TARGET="13.0"
11191136

11201137
LIPO=$(xcrun -sdk iphoneos -find lipo)
11211138
PLATFORMPATH="/Applications/Xcode.app/Contents/Developer/Platforms"
@@ -1126,11 +1143,11 @@ if [[ "$(uname -s)" == "Darwin" ]] && [[ -z "$NO_IOS" ]]; then
11261143
_compile_llama arm64 iPhoneOS & pids+=($!)
11271144

11281145
_compile_libuv x86_64 iPhoneSimulator & pids+=($!)
1129-
_compile_llama x86_64 iPhoneSimulator & pids+=($!)
1146+
#_compile_llama x86_64 iPhoneSimulator & pids+=($!)
11301147

11311148
if [[ "$arch" = "arm64" ]]; then
11321149
_compile_libuv arm64 iPhoneSimulator & pids+=($!)
1133-
_compile_llama arm64 iPhoneSimulator & pids+=($!)
1150+
#_compile_llama arm64 iPhoneSimulator & pids+=($!)
11341151
fi
11351152

11361153
for pid in "${pids[@]}"; do wait "$pid"; done

bin/ldflags.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ if [[ "$host" = "Darwin" ]]; then
114114
ldflags+=("-framework" "UserNotifications")
115115
ldflags+=("-framework" "OSLog")
116116
ldflags+=("-ldl")
117+
ldflags+=("-lggml")
118+
ldflags+=("-lggml-cpu")
119+
ldflags+=("-lggml-base")
120+
ldflags+=("-lggml-blas")
121+
ldflags+=("-lggml-metal")
117122
elif [[ "$host" = "Linux" ]]; then
118123
ldflags+=("-ldl")
119124
ldflags+=($(pkg-config --libs dbus-1 gtk+-3.0 webkit2gtk-4.1))

src/runtime/platform/types.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <array>
55
#include <atomic>
6+
#include <condition_variable>
67
#include <filesystem>
78
#include <fstream>
89
#include <functional>

0 commit comments

Comments
 (0)