Repository navigation
Expand file tree
/
Copy pathcompile.ts
More file actions
358 lines (339 loc) · 10.2 KB
/
Copy pathcompile.ts
File metadata and controls
358 lines (339 loc) · 10.2 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
356
357
358
import {
readdir,
rm,
stat,
} from "node:fs/promises"
import { basename, delimiter, dirname, resolve } from "node:path"
import { fileURLToPath } from "node:url"
import { IcnBinaryIdentity } from "@magnitudedev/icn-protocol"
import { ICN_EXECUTABLE_NAME } from "@magnitudedev/release/executables"
import { Schema } from "effect"
import { getTargetInfo } from "../../scripts/release-target"
const PROJECT_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..")
const CARGO_MANIFEST = resolve(PROJECT_ROOT, "inference/Cargo.toml")
const run = async (
command: readonly string[],
options: {
readonly cwd?: string
readonly env?: Readonly<Record<string, string | undefined>>
} = {},
): Promise<string> => {
const child = Bun.spawn([...command], {
cwd: options.cwd,
env: options.env,
stdin: "ignore",
stdout: "pipe",
stderr: "pipe",
})
const [code, stdout, stderr] = await Promise.all([
child.exited,
new Response(child.stdout).text(),
new Response(child.stderr).text(),
])
if (code !== 0) {
const diagnostics = [stderr, stdout]
.filter((value) => value.trim().length > 0)
.join("\n")
.trim()
throw new Error(
`${command[0]} failed with exit ${code}: ${diagnostics}`,
)
}
return stdout
}
export interface IcnBuild {
readonly binary: string
readonly identity: IcnBinaryIdentity
readonly backendModules: readonly string[]
readonly runtimeLibraries: readonly string[]
}
interface CargoMetadata {
readonly packages: readonly {
readonly id: string
readonly name: string
}[]
}
interface CargoMessage {
readonly reason?: string
readonly package_id?: string
readonly out_dir?: string
readonly executable?: string
readonly target?: { readonly name?: string }
readonly message?: { readonly rendered?: string | null }
}
export const readCargoMessages = async (
stream: ReadableStream<Uint8Array>,
writeDiagnostic: (rendered: string) => void = (rendered) =>
process.stderr.write(rendered),
): Promise<readonly CargoMessage[]> => {
const reader = stream.getReader()
const decoder = new TextDecoder()
const messages: CargoMessage[] = []
let pending = ""
const accept = (line: string): void => {
if (line.trim().length === 0) return
const message = JSON.parse(line) as CargoMessage
if (
message.reason === "compiler-message" &&
typeof message.message?.rendered === "string"
) writeDiagnostic(message.message.rendered)
if (
message.reason === "build-script-executed" ||
message.reason === "compiler-artifact"
) messages.push(message)
}
try {
while (true) {
const next = await reader.read()
if (next.done) break
const lines = `${pending}${decoder.decode(next.value, { stream: true })}`
.split("\n")
pending = lines.pop() ?? ""
for (const line of lines) accept(line)
}
pending += decoder.decode()
accept(pending)
return messages
} finally {
reader.releaseLock()
}
}
const runCargoBuild = async (
command: readonly string[],
options: {
readonly cwd: string
readonly env: Readonly<Record<string, string | undefined>>
readonly diagnostics: "all" | "errors"
},
): Promise<readonly CargoMessage[]> => {
const child = Bun.spawn([...command], {
cwd: options.cwd,
env: options.env,
stdin: "ignore",
stdout: "pipe",
stderr: options.diagnostics === "all" ? "inherit" : "pipe",
})
const renderedDiagnostics: string[] = []
const [code, messages, stderr] = await Promise.all([
child.exited,
readCargoMessages(child.stdout, options.diagnostics === "all"
? (rendered) => process.stderr.write(rendered)
: (rendered) => renderedDiagnostics.push(rendered)),
options.diagnostics === "all"
? Promise.resolve("")
: new Response(child.stderr).text(),
])
if (code !== 0) {
const diagnostics = [stderr, ...renderedDiagnostics]
.filter((value) => value.trim().length > 0)
.join("\n")
.trim()
throw new Error(`${command[0]} failed with exit ${code}${diagnostics.length > 0 ? `: ${diagnostics}` : ""}`)
}
return messages
}
const rustTarget = (target: string): string => {
const { platform, arch } = getTargetInfo(target)
const mapped: Record<string, string> = {
"darwin-arm64": "aarch64-apple-darwin",
"darwin-x64": "x86_64-apple-darwin",
"linux-arm64": "aarch64-unknown-linux-gnu",
"linux-x64": "x86_64-unknown-linux-gnu",
"windows-x64": "x86_64-pc-windows-msvc",
}
const value = mapped[`${platform}-${arch}`]
if (!value) throw new Error(`No ICN Rust target for ${target}`)
return value
}
const nativeRuntimeLinkageEnvironment = (
target: string,
): Readonly<Record<string, string>> => {
const { platform } = getTargetInfo(target)
if (platform === "linux") {
return {
CMAKE_BUILD_RPATH_USE_ORIGIN: "ON",
CMAKE_INSTALL_RPATH: "$ORIGIN;$ORIGIN/../runtime",
}
}
if (platform === "darwin") {
return {
CMAKE_INSTALL_RPATH: "@loader_path;@loader_path/../runtime",
}
}
return {}
}
const filesIn = async (directory: string): Promise<readonly string[]> => {
try {
const entries = await readdir(directory, { withFileTypes: true })
return entries
.filter((entry) => entry.isFile() || entry.isSymbolicLink())
.map((entry) => resolve(directory, entry.name))
.sort()
} catch (cause) {
if (
cause instanceof Error &&
"code" in cause &&
cause.code === "ENOENT"
) return []
throw cause
}
}
const isRuntimeLibrary = (file: string): boolean => {
const name = basename(file)
return name.endsWith(".dylib") ||
name.endsWith(".dll") ||
name.includes(".so")
}
const isBackendModule = (file: string): boolean => {
const name = basename(file).toLowerCase()
return [
"libggml-cpu",
"libggml-metal",
"libggml-cuda",
"libggml-vulkan",
"ggml-cpu",
"ggml-metal",
"ggml-cuda",
"ggml-vulkan",
].some((prefix) => name.startsWith(prefix))
}
const readIdentity = async (
binary: string,
runtimeDirectories: readonly string[],
): Promise<IcnBinaryIdentity> => {
const loader = process.platform === "win32"
? "PATH"
: process.platform === "darwin"
? "DYLD_LIBRARY_PATH"
: "LD_LIBRARY_PATH"
const stdout = await run([binary, "version", "--json"], {
env: {
...process.env,
[loader]: [...runtimeDirectories, process.env[loader]]
.filter(Boolean)
.join(delimiter),
},
})
const value = Schema.decodeUnknownSync(
Schema.parseJson(IcnBinaryIdentity),
)(stdout)
if (value.api_version !== 1) {
throw new Error("ICN identity probe returned an invalid contract")
}
return value
}
export interface BuildIcnInput {
readonly target: string
readonly profile: string
readonly features: readonly string[]
readonly release?: boolean
readonly clean?: boolean
readonly buildEnvironment?: Readonly<Record<string, string>>
/** Print successful compiler diagnostics, or retain them only for a failed build. */
readonly diagnostics?: "all" | "errors"
}
export const buildIcnBinary = async ({
target,
profile,
features,
release = true,
clean = true,
buildEnvironment = {},
diagnostics = "all",
}: BuildIcnInput): Promise<IcnBuild> => {
const cargoTarget = rustTarget(target)
const targetDirectory = resolve(
PROJECT_ROOT,
"inference/target",
`release-${profile}`,
)
if (clean) await rm(targetDirectory, { recursive: true, force: true })
const metadata = JSON.parse(await run([
"cargo",
"metadata",
"--format-version",
"1",
"--manifest-path",
CARGO_MANIFEST,
], { cwd: PROJECT_ROOT })) as CargoMetadata
const nativePackage = metadata.packages.find(
(candidate) => candidate.name === "llama-cpp-sys-2",
)
if (!nativePackage) {
throw new Error("Cargo metadata has no llama-cpp-sys-2 package")
}
const messages = await runCargoBuild([
"cargo",
"build",
...(release ? ["--release"] : []),
"--manifest-path",
CARGO_MANIFEST,
"-p",
"icn-server",
"--target",
cargoTarget,
"--no-default-features",
"--features",
[...new Set(features)].join(","),
"--message-format",
"json-render-diagnostics",
], {
cwd: PROJECT_ROOT,
diagnostics,
env: {
...process.env,
...buildEnvironment,
...nativeRuntimeLinkageEnvironment(target),
CARGO_TARGET_DIR: targetDirectory,
},
})
const outDirectories = messages
.filter((message) =>
message.reason === "build-script-executed" &&
message.package_id === nativePackage.id &&
typeof message.out_dir === "string"
)
.map((message) => message.out_dir!)
if (outDirectories.length !== 1) {
throw new Error(`Cargo reported ${outDirectories.length} native output directories`)
}
const binaryMessages = messages.filter((message) =>
message.reason === "compiler-artifact" &&
message.target?.name === ICN_EXECUTABLE_NAME &&
typeof message.executable === "string"
)
if (binaryMessages.length !== 1) {
throw new Error(`Cargo reported ${binaryMessages.length} ICN executables`)
}
const binary = binaryMessages[0]!.executable!
const nativeOutput = outDirectories[0]!
const backendModules = await filesIn(resolve(nativeOutput, "backends"))
if (backendModules.length === 0) {
throw new Error("ICN build emitted no dynamic backend modules")
}
const installedRuntimeLibraries = (
await filesIn(resolve(nativeOutput, "lib"))
).filter((file) => isRuntimeLibrary(file) && !isBackendModule(file))
const installedNames = new Set(
installedRuntimeLibraries.map((file) => basename(file)),
)
const supplementalRuntimeLibraries = (
await filesIn(resolve(nativeOutput, "build", "bin"))
).filter((file) =>
isRuntimeLibrary(file) &&
!isBackendModule(file) &&
!installedNames.has(basename(file))
)
const runtimeLibraries = [
...installedRuntimeLibraries,
...supplementalRuntimeLibraries,
]
const identity = await readIdentity(
binary,
[...new Set(runtimeLibraries.map(dirname))],
)
for (const file of [binary, ...backendModules, ...runtimeLibraries]) {
if (!(await stat(file)).isFile()) throw new Error(`missing ICN output ${file}`)
}
return { binary, identity, backendModules, runtimeLibraries }
}