Skip to content

Commit 43ed389

Browse files
slarenggerganov
andauthored
llama : use cmake for swift build (#10525)
* llama : use cmake for swift build * swift : <> -> "" * ci : remove make * ci : disable ios build * Revert "swift : <> -> """ This reverts commit d39ffd9. * ci : try fix ios build * ci : cont * ci : cont --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent ecc93d0 commit 43ed389

File tree

7 files changed

+81
-131
lines changed

7 files changed

+81
-131
lines changed

.github/workflows/build.yml

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -552,35 +552,44 @@ jobs:
552552
-DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml
553553
cmake --build . --config Release -j $(sysctl -n hw.logicalcpu) -- CODE_SIGNING_ALLOWED=NO
554554
555-
# TODO: tmp disabled. see for possible re-enable:
556-
# https://github.com/ggerganov/llama.cpp/pull/10525
557-
# macOS-latest-swift:
558-
# runs-on: macos-latest
559-
#
560-
# strategy:
561-
# matrix:
562-
# destination: ['generic/platform=macOS', 'generic/platform=iOS', 'generic/platform=tvOS']
563-
#
564-
# steps:
565-
# - name: Clone
566-
# id: checkout
567-
# uses: actions/checkout@v4
568-
#
569-
# - name: Dependencies
570-
# id: depends
571-
# continue-on-error: true
572-
# run: |
573-
# brew update
574-
#
575-
# - name: xcodebuild for swift package
576-
# id: xcodebuild
577-
# run: |
578-
# xcodebuild -scheme llama -destination "${{ matrix.destination }}"
579-
#
580-
# - name: Build Swift Example
581-
# id: make_build_swift_example
582-
# run: |
583-
# make swift
555+
macOS-latest-swift:
556+
runs-on: macos-latest
557+
558+
strategy:
559+
matrix:
560+
destination: ['generic/platform=macOS', 'generic/platform=iOS', 'generic/platform=tvOS']
561+
562+
steps:
563+
- name: Clone
564+
id: checkout
565+
uses: actions/checkout@v4
566+
567+
- name: Dependencies
568+
id: depends
569+
continue-on-error: true
570+
run: |
571+
brew update
572+
573+
- name: Build llama.cpp with CMake
574+
id: cmake_build
575+
run: |
576+
sysctl -a
577+
mkdir build
578+
cd build
579+
cmake -G Xcode .. \
580+
-DGGML_METAL_USE_BF16=ON \
581+
-DGGML_METAL_EMBED_LIBRARY=ON \
582+
-DLLAMA_BUILD_EXAMPLES=OFF \
583+
-DLLAMA_BUILD_TESTS=OFF \
584+
-DLLAMA_BUILD_SERVER=OFF \
585+
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"
586+
cmake --build . --config Release -j $(sysctl -n hw.logicalcpu)
587+
sudo cmake --install . --config Release
588+
589+
- name: xcodebuild for swift package
590+
id: xcodebuild
591+
run: |
592+
xcodebuild -scheme llama-Package -destination "${{ matrix.destination }}"
584593
585594
windows-msys2:
586595
runs-on: windows-latest
@@ -1104,6 +1113,29 @@ jobs:
11041113
- name: Checkout code
11051114
uses: actions/checkout@v4
11061115

1116+
- name: Build
1117+
id: cmake_build
1118+
run: |
1119+
sysctl -a
1120+
mkdir build
1121+
cd build
1122+
cmake -G Xcode .. \
1123+
-DGGML_METAL_USE_BF16=ON \
1124+
-DGGML_METAL_EMBED_LIBRARY=ON \
1125+
-DLLAMA_BUILD_EXAMPLES=OFF \
1126+
-DLLAMA_BUILD_TESTS=OFF \
1127+
-DLLAMA_BUILD_SERVER=OFF \
1128+
-DCMAKE_SYSTEM_NAME=iOS \
1129+
-DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 \
1130+
-DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml
1131+
cmake --build . --config Release -j $(sysctl -n hw.logicalcpu) -- CODE_SIGNING_ALLOWED=NO
1132+
sudo cmake --install . --config Release
1133+
1134+
- name: xcodebuild for swift package
1135+
id: xcodebuild
1136+
run: |
1137+
xcodebuild -scheme llama-Package -destination 'generic/platform=iOS'
1138+
11071139
- name: Build Xcode project
11081140
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
11091141

@@ -1131,23 +1163,6 @@ jobs:
11311163
11321164
./gradlew build --no-daemon
11331165
1134-
# freeBSD-latest:
1135-
# runs-on: macos-12
1136-
# steps:
1137-
# - name: Clone
1138-
# uses: actions/checkout@v4
1139-
#
1140-
# - name: Build
1141-
# uses: cross-platform-actions/[email protected]
1142-
# with:
1143-
# operating_system: freebsd
1144-
# version: '13.2'
1145-
# hypervisor: 'qemu'
1146-
# run: |
1147-
# sudo pkg update
1148-
# sudo pkg install -y gmake automake autoconf pkgconf llvm15 openblas
1149-
# gmake CC=/usr/local/bin/clang15 CXX=/usr/local/bin/clang++15 -j `sysctl -n hw.ncpu`
1150-
11511166
release:
11521167
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
11531168

Package.swift

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,6 @@
22

33
import PackageDescription
44

5-
var sources = [
6-
"src/llama.cpp",
7-
"src/llama-vocab.cpp",
8-
"src/llama-grammar.cpp",
9-
"src/llama-sampling.cpp",
10-
"src/unicode.cpp",
11-
"src/unicode-data.cpp",
12-
"ggml/src/ggml.c",
13-
"ggml/src/ggml-alloc.c",
14-
"ggml/src/ggml-backend.cpp",
15-
"ggml/src/ggml-backend-reg.cpp",
16-
"ggml/src/ggml-cpu/ggml-cpu.c",
17-
"ggml/src/ggml-cpu/ggml-cpu.cpp",
18-
"ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp",
19-
"ggml/src/ggml-cpu/ggml-cpu-hbm.cpp",
20-
"ggml/src/ggml-cpu/ggml-cpu-quants.c",
21-
"ggml/src/ggml-cpu/ggml-cpu-traits.cpp",
22-
"ggml/src/ggml-threading.cpp",
23-
"ggml/src/ggml-quants.c",
24-
]
25-
26-
var resources: [Resource] = []
27-
var linkerSettings: [LinkerSetting] = []
28-
var cSettings: [CSetting] = [
29-
.unsafeFlags(["-Wno-shorten-64-to-32", "-O3", "-DNDEBUG"]),
30-
.unsafeFlags(["-fno-objc-arc"]),
31-
.headerSearchPath("ggml/src"),
32-
.headerSearchPath("ggml/src/ggml-cpu"),
33-
// NOTE: NEW_LAPACK will required iOS version 16.4+
34-
// We should consider add this in the future when we drop support for iOS 14
35-
// (ref: ref: https://developer.apple.com/documentation/accelerate/1513264-cblas_sgemm?language=objc)
36-
// .define("ACCELERATE_NEW_LAPACK"),
37-
// .define("ACCELERATE_LAPACK_ILP64")
38-
.define("GGML_USE_CPU"),
39-
]
40-
41-
42-
#if canImport(Darwin)
43-
sources.append("ggml/src/ggml-common.h")
44-
sources.append("ggml/src/ggml-metal/ggml-metal.m")
45-
resources.append(.process("ggml/src/ggml-metal/ggml-metal.metal"))
46-
linkerSettings.append(.linkedFramework("Accelerate"))
47-
cSettings.append(
48-
contentsOf: [
49-
.define("GGML_USE_ACCELERATE"),
50-
.define("GGML_USE_METAL"),
51-
]
52-
)
53-
#endif
54-
55-
#if os(Linux)
56-
cSettings.append(.define("_GNU_SOURCE"))
57-
#endif
58-
595
let package = Package(
606
name: "llama",
617
platforms: [
@@ -68,26 +14,6 @@ let package = Package(
6814
.library(name: "llama", targets: ["llama"]),
6915
],
7016
targets: [
71-
.target(
72-
name: "llama",
73-
path: ".",
74-
exclude: [
75-
"build",
76-
"cmake",
77-
"examples",
78-
"scripts",
79-
"models",
80-
"tests",
81-
"CMakeLists.txt",
82-
"Makefile",
83-
"ggml/src/ggml-metal-embed.metal"
84-
],
85-
sources: sources,
86-
resources: resources,
87-
publicHeadersPath: "spm-headers",
88-
cSettings: cSettings,
89-
linkerSettings: linkerSettings
90-
)
91-
],
92-
cxxLanguageStandard: .cxx17
17+
.systemLibrary(name: "llama", pkgConfig: "llama"),
18+
]
9319
)

Sources/llama/llama.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
#include <llama.h>
4+

Sources/llama/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module llama [system] {
2+
header "llama.h"
3+
link "llama"
4+
export *
5+
}

cmake/llama.pc.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ includedir=${prefix}/include
66
Name: llama
77
Description: Port of Facebook's LLaMA model in C/C++
88
Version: @PROJECT_VERSION@
9-
Libs: -L${libdir} -lllama
9+
Libs: -L${libdir} -lggml -lggml-base -lllama
1010
Cflags: -I${includedir}

examples/llama.swiftui/llama.cpp.swift/LibLlama.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,20 @@ actor LlamaContext {
210210

211211
llama_kv_cache_clear(context)
212212

213-
let t_pp_start = ggml_time_us()
213+
let t_pp_start = DispatchTime.now().uptimeNanoseconds / 1000;
214214

215215
if llama_decode(context, batch) != 0 {
216216
print("llama_decode() failed during prompt")
217217
}
218218
llama_synchronize(context)
219219

220-
let t_pp_end = ggml_time_us()
220+
let t_pp_end = DispatchTime.now().uptimeNanoseconds / 1000;
221221

222222
// bench text generation
223223

224224
llama_kv_cache_clear(context)
225225

226-
let t_tg_start = ggml_time_us()
226+
let t_tg_start = DispatchTime.now().uptimeNanoseconds / 1000;
227227

228228
for i in 0..<tg {
229229
llama_batch_clear(&batch)
@@ -238,7 +238,7 @@ actor LlamaContext {
238238
llama_synchronize(context)
239239
}
240240

241-
let t_tg_end = ggml_time_us()
241+
let t_tg_end = DispatchTime.now().uptimeNanoseconds / 1000;
242242

243243
llama_kv_cache_clear(context)
244244

examples/llama.swiftui/llama.swiftui.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
1809696D2D05A39F00400EE8 /* llama in Frameworks */ = {isa = PBXBuildFile; productRef = 1809696C2D05A39F00400EE8 /* llama */; };
1011
549479CB2AC9E16000E0F78B /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 549479CA2AC9E16000E0F78B /* Metal.framework */; };
1112
79E1D9CD2B4CD16E005F8E46 /* InputButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79E1D9CC2B4CD16E005F8E46 /* InputButton.swift */; };
1213
7FA3D2B32B2EA2F600543F92 /* DownloadButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FA3D2B22B2EA2F600543F92 /* DownloadButton.swift */; };
@@ -17,7 +18,6 @@
1718
8A3F84242AC4C891005E2EE8 /* models in Resources */ = {isa = PBXBuildFile; fileRef = 8A3F84232AC4C891005E2EE8 /* models */; };
1819
8A907F332AC7138A006146EA /* LibLlama.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A907F322AC7134E006146EA /* LibLlama.swift */; };
1920
8A9F7C4D2AC332EE008AE1EA /* LlamaState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A9F7C4C2AC332EE008AE1EA /* LlamaState.swift */; };
20-
DF810E132B4A5BA200301144 /* llama in Frameworks */ = {isa = PBXBuildFile; productRef = DF810E122B4A5BA200301144 /* llama */; };
2121
F1FE20E22B465ECA00B45541 /* LoadCustomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1FE20E12B465EC900B45541 /* LoadCustomButton.swift */; };
2222
/* End PBXBuildFile section */
2323

@@ -42,7 +42,7 @@
4242
isa = PBXFrameworksBuildPhase;
4343
buildActionMask = 2147483647;
4444
files = (
45-
DF810E132B4A5BA200301144 /* llama in Frameworks */,
45+
1809696D2D05A39F00400EE8 /* llama in Frameworks */,
4646
549479CB2AC9E16000E0F78B /* Metal.framework in Frameworks */,
4747
8A39BE0A2AC7601100BFEB40 /* Accelerate.framework in Frameworks */,
4848
);
@@ -151,7 +151,7 @@
151151
);
152152
name = llama.swiftui;
153153
packageProductDependencies = (
154-
DF810E122B4A5BA200301144 /* llama */,
154+
1809696C2D05A39F00400EE8 /* llama */,
155155
);
156156
productName = llama.swiftui;
157157
productReference = 8A1C83732AC328BD0096AF73 /* llama.swiftui.app */;
@@ -429,7 +429,7 @@
429429
/* End XCConfigurationList section */
430430

431431
/* Begin XCSwiftPackageProductDependency section */
432-
DF810E122B4A5BA200301144 /* llama */ = {
432+
1809696C2D05A39F00400EE8 /* llama */ = {
433433
isa = XCSwiftPackageProductDependency;
434434
productName = llama;
435435
};

0 commit comments

Comments
 (0)