-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathPackage.swift
More file actions
355 lines (353 loc) · 15.1 KB
/
Copy pathPackage.swift
File metadata and controls
355 lines (353 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// swift-tools-version: 6.1
import PackageDescription
import Foundation
// Strip absolute build paths from __FILE__ macros in C++ warnings (privacy: don't leak dev machine paths)
let packageDir = URL(fileURLWithPath: #filePath).deletingLastPathComponent().path
let vendoredMLXSwiftLMPath = "\(packageDir)/vendor/mlx-swift-lm"
let mlxSwiftLMDependency: Package.Dependency = FileManager.default.fileExists(
atPath: "\(vendoredMLXSwiftLMPath)/Package.swift"
) ? .package(path: vendoredMLXSwiftLMPath) : .package(
url: "https://github.com/scouzi1966/mlx-swift-lm.git",
revision: "6bab4f5ac55e81903dd74090244c25feb3233338"
)
let package = Package(
name: "MacLocalAPI",
platforms: [
.macOS("26.0")
],
products: [
// Dependency-free provider contracts for apps, CLIs, and provider packages.
.library(
name: "AFMKitCore",
targets: ["AFMKitCore"]
),
.library(
name: "AFMOpenAICompat",
targets: ["AFMOpenAICompat"]
),
.library(
name: "AFMKitMLX",
targets: ["AFMKitMLX"]
),
.library(
name: "AFMKitDwarfStar",
targets: ["AFMKitDwarfStar"]
),
.library(
name: "AFMKitFoundationModels",
targets: ["AFMKitFoundationModels"]
),
.library(
name: "AFMKitFoundationModels27",
targets: ["AFMKitFoundationModels27"]
),
.library(
name: "AFMKitFoundationModels27DwarfStar",
targets: ["AFMKitFoundationModels27DwarfStar"]
),
.library(
name: "AFMKitServices",
targets: ["AFMKitServices"]
),
// Headless, SPM-importable library: model loading + inference + OpenAI-compatible
// services + the HTTP server. `import AFMKit` from another package/app.
.library(
name: "AFMKit",
targets: ["AFMKit"]
),
// Vapor HTTP layer (OpenAI-compatible server). Separate product so consumers that
// only want headless inference can depend on AFMKit alone (no Vapor/NIO in their graph).
.library(
name: "AFMServer",
targets: ["AFMServer"]
),
// The `afm` CLI executable (thin wrapper over AFMKit + AFMServer).
.executable(
name: "afm",
targets: ["AFMCLI"]
)
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.99.3"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
// Development checkouts compile the patched vendor directly so local source edits
// cannot be mistaken for successful stale builds. A plain downstream clone without
// initialized submodules falls back to the pre-patched URL fork and remains portable.
mlxSwiftLMDependency,
.package(url: "https://github.com/huggingface/swift-transformers", from: "1.3.0"),
.package(
url: "https://github.com/huggingface/swift-huggingface.git",
from: "0.8.1",
traits: ["Xet"]
),
// AFMKitDwarfStar uses the public byte-range API directly so very large
// GGUF downloads can resume without discarding completed Xet ranges.
.package(url: "https://github.com/huggingface/swift-xet.git", exact: "0.2.3"),
// Share the official XGrammar product with host applications such as Vesta.
// Compiling the vendored implementation here as well as in coreai-models
// produces duplicate native symbols when both libraries are linked.
.package(
url: "https://github.com/mlc-ai/xgrammar",
revision: "c1570cdb4f8c867a4dbd07b7ff90581f4a2a432b"
),
// DeepSeek V4 uses both MXFP4 and MXFP8 weights. Native MXFP8 kernels
// require mlx-swift 0.31.x; older releases treated the floating-point
// quantized path as four-bit-only and forced a BF16 expansion fallback.
.package(url: "https://github.com/ml-explore/mlx-swift", exact: "0.31.6"),
// Jinja (transitive via swift-transformers) — exposed for test target.
// 2.4.0 broke swift-transformers ≤1.3.3 (ObjectKey change in Hub/Config.swift);
// 2.4.1 restored source compatibility upstream, so no cap is needed.
.package(url: "https://github.com/huggingface/swift-jinja.git", from: "2.0.0")
],
targets: [
.target(
name: "AFMKitCore",
dependencies: []
),
.target(
name: "AFMOpenAICompat",
dependencies: []
),
.target(
name: "AFMKitFoundationModels",
dependencies: [
"AFMOpenAICompat"
]
),
.target(
name: "AFMKitFoundationModels27",
dependencies: [
"AFMKit"
]
),
.target(
name: "AFMKitFoundationModels27DwarfStar",
dependencies: [
"AFMKit",
"AFMKitDwarfStar",
"AFMKitFoundationModels27"
]
),
.target(
name: "AFMKitServices",
dependencies: [
"AFMKitCore"
]
),
.target(
name: "CDwarfStar",
path: "Sources/CDwarfStar",
sources: [
"AFMDwarfStarBridge.c",
"CDwarfStarKVStore.c",
"CDwarfStarEngine.c",
"CDwarfStarDistributed.c",
"CDwarfStarTensorParallel.c",
"CDwarfStarSSD.c",
"CDwarfStarMetal.m",
"CDwarfStarLayerPack.c"
],
publicHeadersPath: "include",
cSettings: [
// Canonical DS4 uses -O3 for every configuration. Besides
// performance, this removes compile-time-impossible CUDA/TP
// branches before a macOS Metal link.
// Keep release artifacts portable across supported Apple Silicon hosts.
// DwarfStar's performance-critical work runs in Metal kernels.
.unsafeFlags(["-O3", "-ffast-math"])
],
linkerSettings: [
.linkedFramework("Foundation"),
.linkedFramework("Metal")
]
),
.target(
name: "AFMKitDwarfStar",
dependencies: [
"AFMKitCore",
"CDwarfStar",
.product(name: "HuggingFace", package: "swift-huggingface"),
.product(name: "Xet", package: "swift-xet")
],
resources: [
// DS4 compiles these include-style fragments at runtime. Keep the
// directory opaque so SwiftPM does not compile each file alone.
.copy("../../vendor/ds4/metal")
],
swiftSettings: [
.unsafeFlags(["-O"], .when(configuration: .release)),
.unsafeFlags(
["-file-prefix-map", "\(packageDir)/="],
.when(configuration: .release)
)
]
),
.target(
name: "AFMKitMLX",
dependencies: [
"AFMKitCore",
"AFMOpenAICompat",
"AFMXGrammar",
.product(name: "MLX", package: "mlx-swift"),
.product(name: "MLXLLM", package: "mlx-swift-lm"),
.product(name: "MLXVLM", package: "mlx-swift-lm"),
.product(name: "MLXLMCommon", package: "mlx-swift-lm"),
.product(name: "Tokenizers", package: "swift-transformers"),
.product(name: "Hub", package: "swift-transformers"),
.product(name: "HuggingFace", package: "swift-huggingface")
],
resources: [
.copy("Resources/default.metallib")
],
swiftSettings: [
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)),
.unsafeFlags(["-O"], .when(configuration: .release)),
.unsafeFlags(["-file-prefix-map", "\(packageDir)/="], .when(configuration: .release))
],
linkerSettings: [
.linkedFramework("Security"),
.linkedFramework("IOKit"),
.linkedLibrary("IOReport"),
.linkedLibrary("sqlite3")
]
),
.target(
name: "AFMXGrammar",
dependencies: [
.product(name: "XGrammar", package: "xgrammar")
],
path: "Sources/CXGrammar",
exclude: [
// Retained temporarily for standalone source compatibility, but the
// implementation is supplied by the shared XGrammar package product.
"xgrammar"
],
cxxSettings: [
// XGrammar's public matcher header imports DLPack, but its package
// does not propagate that private include path to bridge targets.
.headerSearchPath("xgrammar/3rdparty/dlpack/include")
]
),
// Core library — all reusable inference/service/server code. Importable via SPM.
.target(
name: "AFMKit",
dependencies: [
"AFMKitCore",
"AFMOpenAICompat",
"AFMKitMLX",
"AFMKitFoundationModels",
"AFMKitServices"
],
swiftSettings: [
// Enable optimizations for release builds
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)),
.unsafeFlags(["-O"], .when(configuration: .release)),
// Strip build machine prefix so errors show Sources/... not /Volumes/.../Sources/...
.unsafeFlags(["-file-prefix-map", "\(packageDir)/="], .when(configuration: .release))
],
linkerSettings: []
),
// Vapor HTTP layer — the OpenAI-compatible server, controllers, backend
// discovery/proxy, and Telegram bridge. Depends on AFMKit + Vapor.
.target(
name: "AFMServer",
dependencies: [
"AFMKit",
.product(name: "Vapor", package: "vapor"),
.product(name: "MLXLLM", package: "mlx-swift-lm"),
.product(name: "MLXVLM", package: "mlx-swift-lm"),
.product(name: "MLXLMCommon", package: "mlx-swift-lm")
],
swiftSettings: [
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)),
.unsafeFlags(["-O"], .when(configuration: .release)),
.unsafeFlags(["-file-prefix-map", "\(packageDir)/="], .when(configuration: .release))
]
),
// Thin CLI executable over AFMKit + AFMServer.
.executableTarget(
name: "AFMCLI",
dependencies: [
"AFMKit",
"AFMKitDwarfStar",
"AFMKitMLX",
"AFMServer",
.product(name: "Vapor", package: "vapor"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "MLXLLM", package: "mlx-swift-lm"),
.product(name: "MLXVLM", package: "mlx-swift-lm"),
.product(name: "MLXLMCommon", package: "mlx-swift-lm")
],
exclude: [
// Embedded into the binary's __TEXT,__info_plist section via linker flags below.
"Info.plist"
],
swiftSettings: [
// Xcode 27 Beta 3 reports a false circular reference when this
// two-file CLI target is compiled with whole-module/Cross-
// module optimization. Runtime libraries retain their Release
// optimization; only the thin command parser is isolated.
.unsafeFlags(["-no-whole-module-optimization"], .when(configuration: .release)),
.unsafeFlags(["-Onone"], .when(configuration: .release)),
.unsafeFlags(["-file-prefix-map", "\(packageDir)/="], .when(configuration: .release))
],
linkerSettings: [
// Embed Info.plist with NSSpeechRecognitionUsageDescription into the binary's
// __TEXT,__info_plist section. macOS 26 SIGABRTs any process that requests
// privacy-sensitive APIs (Speech Recognition, microphone, camera, etc.) without
// a matching *UsageDescription key in its Info.plist. Required for PR #107's
// Apple Speech feature; harmless for non-Speech code paths.
.unsafeFlags([
"-Xlinker", "-sectcreate",
"-Xlinker", "__TEXT",
"-Xlinker", "__info_plist",
"-Xlinker", "\(packageDir)/Sources/AFMCLI/Info.plist"
]),
// Create a more portable executable
.unsafeFlags(["-Xlinker", "-rpath", "-Xlinker", "@executable_path"], .when(configuration: .release)),
.unsafeFlags(["-Xlinker", "-rpath", "-Xlinker", "/usr/lib/swift"], .when(configuration: .release)),
.unsafeFlags(["-Xlinker", "-dead_strip"], .when(configuration: .release))
]
),
.testTarget(
name: "MacLocalAPITests",
dependencies: [
"AFMKit",
"AFMKitDwarfStar",
"AFMKitMLX",
"AFMKitFoundationModels",
"AFMKitFoundationModels27",
"AFMKitFoundationModels27DwarfStar",
"AFMKitServices",
"AFMServer",
.product(name: "Jinja", package: "swift-jinja"),
.product(name: "XCTVapor", package: "vapor"),
.product(name: "VaporTesting", package: "vapor"),
// MTP P0 validation needs the patched Qwen3.6 VLM model (Qwen3_5MTPHead).
.product(name: "MLXVLM", package: "mlx-swift-lm"),
// EAGLE3 P0 validation needs the Gemma4 drafter (MLXLLM module).
.product(name: "MLXLLM", package: "mlx-swift-lm")
],
swiftSettings: [
// Xcode 27 Beta 3 reports a false circular reference while
// optimizing the combined release test module. Product targets
// remain fully optimized.
.unsafeFlags(["-no-whole-module-optimization"], .when(configuration: .release)),
.unsafeFlags(["-Onone"], .when(configuration: .release))
]
),
.testTarget(
name: "AFMKitDwarfStarTests",
dependencies: [
"AFMKitCore",
"AFMKitDwarfStar",
],
swiftSettings: [
// Match the Xcode 27 Beta 3 workaround used by the main test target.
.unsafeFlags(["-no-whole-module-optimization"], .when(configuration: .release)),
.unsafeFlags(["-Onone"], .when(configuration: .release))
]
)
],
cxxLanguageStandard: .gnucxx17
)