llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Compile bug: iOS version able to build not not able to run

Open Animaxx opened this issue 11 months ago • 9 comments

Git commit

https://github.com/ggerganov/llama.cpp/commit/43ed389a3f102517e6f7d5620d8e451e88afbf27

Operating systems

Mac

GGML backends

Metal

Problem description & steps to reproduce

related to https://github.com/ggerganov/llama.cpp/issues/10747

I have follow with the CI action for macOS

mkdir build
cd build

cmake -G Xcode .. \
    -DGGML_METAL_USE_BF16=ON \
    -DGGML_METAL_EMBED_LIBRARY=ON \
    -DLLAMA_BUILD_EXAMPLES=OFF \
    -DLLAMA_BUILD_TESTS=OFF \
    -DLLAMA_BUILD_SERVER=OFF \
    -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
    -DCMAKE_SYSTEM_NAME=Darwin \
    -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 \
    -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml
    
cmake --build . --config Release -j $(sysctl -n hw.logicalcpu) -- CODE_SIGNING_ALLOWED=NO
sudo cmake --install . --config Release

# build for swift package
cd ..
xcodebuild -scheme llama-Package -destination 'generic/platform=macOS'

or build for iOS

mkdir build
cd build
cmake -G Xcode .. \
    -DGGML_METAL_USE_BF16=ON \
    -DGGML_METAL_EMBED_LIBRARY=ON \
    -DLLAMA_BUILD_EXAMPLES=OFF \
    -DLLAMA_BUILD_TESTS=OFF \
    -DLLAMA_BUILD_SERVER=OFF \
    -DCMAKE_SYSTEM_NAME=iOS \
    -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 \
    -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml

cmake --build . --config Release -j $(sysctl -n hw.logicalcpu) -- CODE_SIGNING_ALLOWED=NO
sudo cmake --install . --config Release

# build for swift package
cd ..
xcodebuild -scheme llama-Package -destination 'generic/platform=iOS'

the build is success but when run it shows error dyld[70862]: Library not loaded: @rpath/libggml.dylib

First Bad Commit

No response

Relevant log output

dyld[70862]: Library not loaded: @rpath/libggml.dylib
  Referenced from: <4A6AE769-FE07-3AAE-84F0-552992A8720C> /Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/LocalLLM.app/Contents/MacOS/LocalLLM.debug.dylib
  Reason: tried: '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/libggml.dylib' (no such file), '/usr/lib/system/introspection/libggml.dylib' (no such file, not in dyld cache), '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/PackageFrameworks/libggml.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/PackageFrameworks/libggml.dylib' (no such file), '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/LocalLLM.app/Contents/MacOS/Frameworks/libggml.dylib' (no such file), '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/LocalLLM.app/Contents/MacOS/libggml.dylib' (no such file), '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/PackageFrameworks/libggml.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/PackageFrameworks/libggml.dylib' (no such file), '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/LocalLLM.app/Contents/MacOS/Frameworks/libggml.dylib' (no such file), '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/LocalLLM.app/Contents/MacOS/libggml.dylib' (no such file), '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/PackageFrameworks/libggml.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/PackageFrameworks/libggml.dylib' (no such file), '/Users/animax/Library/Developer/Xcode/DerivedData/LocalLLM-ctyejtpossqslxdwvdukottbyflj/Build/Products/Debug/LocalLLM.app/Contents/MacOS/Frameworks/libggml.dylib' (no such file)
Message from debugger: Terminated due to signal 6

Animaxx avatar Dec 20 '24 19:12 Animaxx

update: looks like dylib files are not able to copy into the product folder while the app build. I used this script to build for macOS version

mkdir build
cd build
cmake -G Xcode .. \
  -DCMAKE_INSTALL_RPATH="@rpath" \
  -DCMAKE_INSTALL_PREFIX="PRODUCT/macOS" \
  -DGGML_METAL_USE_BF16=ON \
  -DGGML_METAL_EMBED_LIBRARY=ON \
  -DLLAMA_BUILD_EXAMPLES=OFF \
  -DLLAMA_BUILD_TESTS=OFF \
  -DLLAMA_BUILD_SERVER=OFF \
  -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"
cmake --build . --config Release -j $(sysctl -n hw.logicalcpu)
cmake --install . --config Release

cd ..
xcodebuild -scheme llama-Package -destination 'generic/platform=macOS'

and based on @ggerganov suggestion https://github.com/ggerganov/llama.cpp/issues/10747#issuecomment-2538203367 to add the search path the problem still not solve.

also tried add all "PRODUCT/macOS/lib" files in Build Phases -> new copy files phases, still not luck. but manually copy paste these files works.

I am trying to see if there any better way to do it, please let me know if any advice

Animaxx avatar Dec 20 '24 23:12 Animaxx

It is recommended to restore the maintenance of Package.swift, which is very convenient for encapsulating other Swift projects.

edisonzf2020 avatar Dec 21 '24 16:12 edisonzf2020

mkdir build
cd build

echo "=== cmake for iOS"
cmake -G Xcode .. \
  -DCMAKE_INSTALL_RPATH="@rpath" \
  -DGGML_METAL_USE_BF16=ON \
  -DGGML_METAL_EMBED_LIBRARY=ON \
  -DLLAMA_BUILD_EXAMPLES=OFF \
  -DLLAMA_BUILD_TESTS=OFF \
  -DLLAMA_BUILD_SERVER=OFF \
  -DCMAKE_SYSTEM_NAME=iOS \
  -DCMAKE_OSX_DEPLOYMENT_TARGET=16.0 \
  -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml

cmake --build . --config Release -j $(sysctl -n hw.logicalcpu) -- CODE_SIGNING_ALLOWED=NO

cmake --install . --prefix ../../llama_product_ios

cd ..
xcodebuild -scheme llama-Package -destination 'generic/platform=iOS'

with set the search paths and have "copy file phases" for all the "lib" dylib as Frameworks section works for both iOS and MacOS for real device, but it not build for simulator.

Animaxx avatar Dec 22 '24 15:12 Animaxx

Have you solved this problem?

Tongziqi avatar Dec 25 '24 16:12 Tongziqi

Have you solved this problem?

no.. it might work for debug but not for app store release. I am thinking maybe swift package is not compatible for this case

Animaxx avatar Jan 05 '25 04:01 Animaxx

@Animaxx sorry to bother you but where you able to find a solution?

pgorzelany avatar Jan 30 '25 22:01 pgorzelany

No, I don't have a good solution for this one yet. the dylib not friendly to iOS. Maybe we can find a way to integrate the C++ code different iOS project? We need someone who extreme good at C++ CMake and iOS framework here...

Animaxx avatar Jan 31 '25 06:01 Animaxx

mkdir build
cd build

echo "=== cmake for iOS"
cmake -G Xcode .. \
  -DCMAKE_INSTALL_RPATH="@rpath" \
  -DGGML_METAL_USE_BF16=ON \
  -DGGML_METAL_EMBED_LIBRARY=ON \
  -DLLAMA_BUILD_EXAMPLES=OFF \
  -DLLAMA_BUILD_TESTS=OFF \
  -DLLAMA_BUILD_SERVER=OFF \
  -DCMAKE_SYSTEM_NAME=iOS \
  -DCMAKE_OSX_DEPLOYMENT_TARGET=16.0 \
  -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml

cmake --build . --config Release -j $(sysctl -n hw.logicalcpu) -- CODE_SIGNING_ALLOWED=NO

cmake --install . --prefix ../../llama_product_ios

cd ..
xcodebuild -scheme llama-Package -destination 'generic/platform=iOS'

with set the search paths and have "copy file phases" for all the "lib" dylib as Frameworks section works for both iOS and MacOS for real device, but it not build for simulator.

for anyone interested, after cmake -G Xcode .. , open llama.cpp.xcodeproj and make sure that the development team is set for all targets, and then you won't need the -- CODE_SIGNING_ALLOWED=NO for cmake --build . --config Release -j $(sysctl -n hw.logicalcpu) -- CODE_SIGNING_ALLOWED=NO isn't necessary, and ultimately you'll need to have those signed if you want the app to be on the app store.

Thank you @Animaxx for documenting this. It works well for me.

benliong avatar Feb 19 '25 22:02 benliong

For me llama.cpp is compiled by a podspec. The build system is copying the library files to the correct directory and flutter is loading the library correctly but none of the functions seem to do anything and return nullptr.

danemadsen avatar Feb 27 '25 02:02 danemadsen

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Apr 13 '25 01:04 github-actions[bot]