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

llama : add xcframework build script #11996

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,11 @@ jobs:
-DLLAMA_BUILD_SERVER=OFF \
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"
cmake --build build --config Release -j $(sysctl -n hw.logicalcpu)
sudo cmake --install build --config Release
- name: xcodebuild for swift package
id: xcodebuild
run: |
xcodebuild -scheme llama-Package -destination "${{ matrix.destination }}"
./build-xcframework.sh
windows-msys2:
runs-on: windows-latest
Expand Down Expand Up @@ -1328,15 +1327,14 @@ jobs:
-DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 \
-DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml
cmake --build build --config Release -j $(sysctl -n hw.logicalcpu) -- CODE_SIGNING_ALLOWED=NO
sudo cmake --install build --config Release
- name: xcodebuild for swift package
id: xcodebuild
run: |
xcodebuild -scheme llama-Package -destination 'generic/platform=iOS'
./build-xcframework.sh
- name: Build Xcode project
run: xcodebuild -project examples/llama.swiftui/llama.swiftui.xcodeproj -scheme llama.swiftui -sdk iphoneos CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= -destination 'generic/platform=iOS' build
run: xcodebuild -project examples/llama.swiftui/llama.swiftui.xcodeproj -scheme llama.swiftui -sdk iphoneos CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= -destination 'generic/platform=iOS' FRAMEWORK_FOLDER_PATH=./build-ios build

android-build:
runs-on: ubuntu-latest
Expand Down
19 changes: 0 additions & 19 deletions Package.swift

This file was deleted.

4 changes: 0 additions & 4 deletions Sources/llama/llama.h

This file was deleted.

5 changes: 0 additions & 5 deletions Sources/llama/module.modulemap

This file was deleted.

142 changes: 142 additions & 0 deletions build-xcframework.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/bin/bash
#
# Options
DEPLOYMENT_TARGET=14.0
BUILD_SHARED_LIBS=ON
LLAMA_STATIC=OFF
LLAMA_BUILD_EXAMPLES=OFF
LLAMA_BUILD_TESTS=OFF
LLAMA_BUILD_SERVER=OFF
GGML_METAL_EMBED_LIBRARY=ON
GGML_METAL_USE_BF16=ON

set -xe

rm -rf build-ios
rm -rf build-ios-sim
rm -rf build-ios-device

# Function to setup framework structure for a given architecture
setup_framework_structure() {
local build_dir=$1
local framework_name="llama"

# Create framework directory structure
mkdir -p ${build_dir}/framework/${framework_name}.framework
mkdir -p ${build_dir}/framework/${framework_name}.framework/Headers
mkdir -p ${build_dir}/framework/${framework_name}.framework/Modules

# Copy all required headers.
cp include/llama.h ${build_dir}/framework/${framework_name}.framework/Headers/
cp ggml/include/ggml.h ${build_dir}/framework/${framework_name}.framework/Headers/
cp ggml/include/ggml-alloc.h ${build_dir}/framework/${framework_name}.framework/Headers/
cp ggml/include/ggml-backend.h ${build_dir}/framework/${framework_name}.framework/Headers/
cp ggml/include/ggml-metal.h ${build_dir}/framework/${framework_name}.framework/Headers/
cp ggml/include/ggml-cpu.h ${build_dir}/framework/${framework_name}.framework/Headers/
cp ggml/include/ggml-blas.h ${build_dir}/framework/${framework_name}.framework/Headers/

# Create module map that describes how which headers are part of the llama module.
# This enables swift code to import 'llama' and get access to all public interfaces
# the headers with the usage of export *. For example, LibLlama.swift imports the
# 'llama' module.
cat > ${build_dir}/framework/${framework_name}.framework/Modules/module.modulemap << EOF
framework module llama {
header "llama.h"
header "ggml.h"
header "ggml-alloc.h"
header "ggml-backend.h"
header "ggml-metal.h"
header "ggml-cpu.h"
header "ggml-blas.h"

export *
}
EOF

# Create Info.plist (Information Property List) file that describes the app.
# This will be a Framework bundle (FMWK).
cat > ${build_dir}/framework/${framework_name}.framework/Info.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>llama</string>
<key>CFBundleIdentifier</key>
<string>ggml-org.llama.framework</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>llama</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>MinimumOSVersion</key>
<string>14.0</string>
</dict>
</plist>
EOF
}

# Common options for all builds.
COMMON_CMAKE_ARGS=(
-DIOS=ON
-DCMAKE_SYSTEM_NAME=iOS
-DCMAKE_OSX_DEPLOYMENT_TARGET=${DEPLOYMENT_TARGET}
-DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=NO
-DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY=""
-DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO
-DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
-DLLAMA_STATIC=${LLAMA_STATIC}
-DLLAMA_BUILD_EXAMPLES=${LLAMA_BUILD_EXAMPLES}
-DLLAMA_BUILD_TESTS=${LLAMA_BUILD_TESTS}
-DLLAMA_BUILD_SERVER=${LLAMA_BUILD_SERVER}
-DGGML_METAL_EMBED_LIBRARY=${GGML_METAL_EMBED_LIBRARY}
-DGGML_METAL_USE_BF16=${GGML_METAL_USE_BF16}
-DCMAKE_INSTALL_NAME_DIR="@rpath/llama.framework/llama"
)

# Build for iOS simulator.
cmake -B build-ios-sim -G Xcode \
"${COMMON_CMAKE_ARGS[@]}" \
-DCMAKE_OSX_SYSROOT=iphonesimulator \
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphonesimulator \
-S .
cmake --build build-ios-sim --config Release

# Build for iOS devices.
cmake -B build-ios-device -G Xcode \
"${COMMON_CMAKE_ARGS[@]}" \
-DCMAKE_OSX_SYSROOT=iphoneos \
-DCMAKE_OSX_ARCHITECTURES="arm64" \
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphoneos \
-S .
cmake --build build-ios-device --config Release

# Setup frameworks and copy binaries and headers.
setup_framework_structure "build-ios-sim"
setup_framework_structure "build-ios-device"

# Copy and rename the binaries.
cp build-ios-sim/bin/Release/libllama.dylib build-ios-sim/framework/llama.framework/llama
cp build-ios-device/bin/Release/libllama.dylib build-ios-device/framework/llama.framework/llama

# Fix the install name in the binaries
install_name_tool -id "@rpath/llama.framework/llama" build-ios-sim/framework/llama.framework/llama
install_name_tool -id "@rpath/llama.framework/llama" build-ios-device/framework/llama.framework/llama

# Create XCFramework with the two builds which will contain both simulator
# and device versions in the same framework.
xcodebuild -create-xcframework \
-framework $(pwd)/build-ios-sim/framework/llama.framework \
-framework $(pwd)/build-ios-device/framework/llama.framework \
-output $(pwd)/build-ios/llama.xcframework

# The generated framework can be found in build-ios/llama.xcframework and
# can be added to a projects "Frameworks, Libraries, and Embedded Content".
15 changes: 15 additions & 0 deletions examples/llama.swiftui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ point for more advanced projects.

For usage instructions and performance stats, check the following discussion: https://github.com/ggml-org/llama.cpp/discussions/4508


### Building
First llama.cpp need to be built and a XCFramework needs to be created. This can be done by running
the following script from the llama.cpp project root:
```console
$ ./build-xcframework.sh
```
Open `llama.swiftui.xcodeproj` project in Xcode and you should be able to build and run the app on
a simulator or a real device.

To use the framework with a different project, the XCFramework can be added to the project by
adding `build-ios/llama.xcframework` by dragging and dropping it into the project navigator, or
by manually selecting the framework in the "Frameworks, Libraries, and Embedded Content" section
of the project settings.

![image](https://github.com/ggml-org/llama.cpp/assets/1991296/2b40284f-8421-47a2-b634-74eece09a299)

Video demonstration:
Expand Down
32 changes: 20 additions & 12 deletions examples/llama.swiftui/llama.swiftui.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
1809696D2D05A39F00400EE8 /* llama in Frameworks */ = {isa = PBXBuildFile; productRef = 1809696C2D05A39F00400EE8 /* llama */; };
549479CB2AC9E16000E0F78B /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 549479CA2AC9E16000E0F78B /* Metal.framework */; };
79E1D9CD2B4CD16E005F8E46 /* InputButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79E1D9CC2B4CD16E005F8E46 /* InputButton.swift */; };
7FA3D2B32B2EA2F600543F92 /* DownloadButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FA3D2B22B2EA2F600543F92 /* DownloadButton.swift */; };
Expand All @@ -18,9 +17,25 @@
8A3F84242AC4C891005E2EE8 /* models in Resources */ = {isa = PBXBuildFile; fileRef = 8A3F84232AC4C891005E2EE8 /* models */; };
8A907F332AC7138A006146EA /* LibLlama.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A907F322AC7134E006146EA /* LibLlama.swift */; };
8A9F7C4D2AC332EE008AE1EA /* LlamaState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A9F7C4C2AC332EE008AE1EA /* LlamaState.swift */; };
DD0078922D6756A5003C187C /* llama.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD0078912D6756A5003C187C /* llama.xcframework */; };
DD0078932D6756A5003C187C /* llama.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DD0078912D6756A5003C187C /* llama.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
F1FE20E22B465ECA00B45541 /* LoadCustomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1FE20E12B465EC900B45541 /* LoadCustomButton.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
DD0078942D6756A5003C187C /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
DD0078932D6756A5003C187C /* llama.xcframework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
549479CA2AC9E16000E0F78B /* Metal.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Metal.framework; path = System/Library/Frameworks/Metal.framework; sourceTree = SDKROOT; };
79E1D9CC2B4CD16E005F8E46 /* InputButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputButton.swift; sourceTree = "<group>"; };
Expand All @@ -33,7 +48,7 @@
8A3F84232AC4C891005E2EE8 /* models */ = {isa = PBXFileReference; lastKnownFileType = folder; name = models; path = llama.swiftui/Resources/models; sourceTree = "<group>"; };
8A907F322AC7134E006146EA /* LibLlama.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibLlama.swift; sourceTree = "<group>"; };
8A9F7C4C2AC332EE008AE1EA /* LlamaState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LlamaState.swift; sourceTree = "<group>"; };
DF2D2FE72B4A59BE00FCB72D /* llama.cpp */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = llama.cpp; path = ../..; sourceTree = "<group>"; };
DD0078912D6756A5003C187C /* llama.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = llama.xcframework; path = "../../build-ios/llama.xcframework"; sourceTree = "<group>"; };
F1FE20E12B465EC900B45541 /* LoadCustomButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadCustomButton.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand All @@ -42,9 +57,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
1809696D2D05A39F00400EE8 /* llama in Frameworks */,
549479CB2AC9E16000E0F78B /* Metal.framework in Frameworks */,
8A39BE0A2AC7601100BFEB40 /* Accelerate.framework in Frameworks */,
DD0078922D6756A5003C187C /* llama.xcframework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -54,7 +69,6 @@
8A1C836A2AC328BD0096AF73 = {
isa = PBXGroup;
children = (
DF2D2FE72B4A59BE00FCB72D /* llama.cpp */,
8A907F312AC7134E006146EA /* llama.cpp.swift */,
8A3F84232AC4C891005E2EE8 /* models */,
8A1C83752AC328BD0096AF73 /* llama.swiftui */,
Expand Down Expand Up @@ -86,6 +100,7 @@
8A39BE082AC7601000BFEB40 /* Frameworks */ = {
isa = PBXGroup;
children = (
DD0078912D6756A5003C187C /* llama.xcframework */,
549479CA2AC9E16000E0F78B /* Metal.framework */,
8A39BE092AC7601000BFEB40 /* Accelerate.framework */,
);
Expand Down Expand Up @@ -144,14 +159,14 @@
8A1C836F2AC328BD0096AF73 /* Sources */,
8A1C83702AC328BD0096AF73 /* Frameworks */,
8A1C83712AC328BD0096AF73 /* Resources */,
DD0078942D6756A5003C187C /* Embed Frameworks */,
);
buildRules = (
);
dependencies = (
);
name = llama.swiftui;
packageProductDependencies = (
1809696C2D05A39F00400EE8 /* llama */,
);
productName = llama.swiftui;
productReference = 8A1C83732AC328BD0096AF73 /* llama.swiftui.app */;
Expand Down Expand Up @@ -427,13 +442,6 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCSwiftPackageProductDependency section */
1809696C2D05A39F00400EE8 /* llama */ = {
isa = XCSwiftPackageProductDependency;
productName = llama;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 8A1C836B2AC328BD0096AF73 /* Project object */;
}
1 change: 0 additions & 1 deletion spm-headers/ggml-alloc.h

This file was deleted.

1 change: 0 additions & 1 deletion spm-headers/ggml-backend.h

This file was deleted.

1 change: 0 additions & 1 deletion spm-headers/ggml-cpp.h

This file was deleted.

1 change: 0 additions & 1 deletion spm-headers/ggml-cpu.h

This file was deleted.

1 change: 0 additions & 1 deletion spm-headers/ggml-metal.h

This file was deleted.

1 change: 0 additions & 1 deletion spm-headers/ggml.h

This file was deleted.

1 change: 0 additions & 1 deletion spm-headers/llama.h

This file was deleted.

Loading