Repository navigation
Expand file tree
/
Copy pathtest-upstream.ts
More file actions
360 lines (344 loc) · 11.1 KB
/
Copy pathtest-upstream.ts
File metadata and controls
360 lines (344 loc) · 11.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
356
357
358
359
360
import { Effect } from "effect";
import { createHash, randomBytes } from "node:crypto";
import { mkdir, readFile, readdir, unlink, writeFile } from "node:fs/promises";
import { isAbsolute, relative, resolve, sep } from "node:path";
import {
loadTargetManifest,
selectReferenceTargets,
uniqueStrings,
} from "./build-parity-config";
import {
prepareOfflineUpstreamFixtures,
verifyOfflineUpstreamFixtures,
} from "./upstream-model-fixture";
import { verifyNativePin } from "./verify-native-pin";
import {
captureCTestInventory,
ctestSelectionArguments,
verifyCTestJunit,
} from "./ctest-evidence";
import {
controlledEnvironment,
controlledEnvironmentEvidence,
} from "./controlled-environment";
type Backend = "cpu" | "metal";
interface Arguments {
readonly backend: Backend;
readonly targets: readonly string[];
readonly outputDirectory?: string;
readonly modelDirectory?: string;
readonly parallel: number;
readonly help: boolean;
}
const inferenceRoot = resolve(import.meta.dirname, "..");
const usage = `Build and run unchanged pinned upstream CTests.
Usage:
bun run scripts/test-upstream.ts [options]
Options:
--backend <cpu|metal> Backend to test (default: metal on macOS, cpu elsewhere)
--target <id> Target-manifest entry with CTests (default: focused-tests; repeatable)
--output-dir <path> Empty evidence directory (default: unique results/parity/upstream/<run>)
--model-dir <path> Registry artifact root (default: ICN_PARITY_MODEL_DIR)
--parallel <count> Parallel native build jobs (default: 8)
--help Show this help
`;
const requireNext = (args: readonly string[], index: number, option: string) => {
const value = args[index + 1];
if (value === undefined) throw new Error(`${option} requires a value`);
return value;
};
const parseArguments = (): Arguments => {
let backend: Backend = process.platform === "darwin" ? "metal" : "cpu";
const targets: string[] = [];
let outputDirectory: string | undefined;
let modelDirectory: string | undefined;
let parallel = 8;
let help = false;
const args = Bun.argv.slice(2);
for (let index = 0; index < args.length; index += 1) {
const argument = args[index];
if (argument === "--backend") {
const value = requireNext(args, index, argument);
if (value !== "cpu" && value !== "metal") {
throw new Error(`Unsupported backend: ${value}`);
}
backend = value;
index += 1;
} else if (argument === "--target") {
targets.push(requireNext(args, index, argument));
index += 1;
} else if (argument === "--output-dir") {
outputDirectory = requireNext(args, index, argument);
index += 1;
} else if (argument === "--model-dir") {
modelDirectory = requireNext(args, index, argument);
index += 1;
} else if (argument === "--parallel") {
const raw = requireNext(args, index, argument);
parallel = Number(raw);
if (!Number.isSafeInteger(parallel) || parallel < 1) {
throw new Error(`Invalid parallel job count: ${raw}`);
}
index += 1;
} else if (argument === "--help") {
help = true;
} else {
throw new Error(`Unsupported argument: ${argument}`);
}
}
return { backend, targets, outputDirectory, modelDirectory, parallel, help };
};
const run = async (
command: readonly string[],
cwd: string,
environment: Readonly<Record<string, string>>
) => {
const child = Bun.spawn([...command], {
cwd,
env: environment,
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
});
return child.exited;
};
const referenceBuildDirectory = async (manifestPath: string) => {
const value: unknown = JSON.parse(
new TextDecoder().decode(await readFile(manifestPath))
);
if (value === null || typeof value !== "object" || Array.isArray(value)) {
throw new Error("Reference builder produced an invalid manifest");
}
const manifest = value as Record<string, unknown>;
if (manifest.schemaVersion !== 3 || typeof manifest.buildDirectory !== "string") {
throw new Error("Reference builder did not record a schema-v3 build directory");
}
const base = resolve(inferenceRoot, "target/reference/llama.cpp");
const build = resolve(inferenceRoot, manifest.buildDirectory);
const child = relative(base, build);
if (
child.length === 0 ||
child === ".." ||
child.startsWith(`..${sep}`) ||
isAbsolute(child)
) {
throw new Error("Reference build directory escapes its dedicated target root");
}
return build;
};
const cleanupGeneratedTestFiles = async (source: string) => {
await Promise.all(
["test-grammar-output.tmp", "test-json-schema-input.tmp"].map((name) =>
unlink(resolve(source, name)).catch((error: NodeJS.ErrnoException) => {
if (error.code !== "ENOENT") throw error;
})
)
);
};
const outputDirectoryFor = (configured?: string) => {
if (configured !== undefined) {
return isAbsolute(configured) ? configured : resolve(process.cwd(), configured);
}
const timestamp = new Date().toISOString().replaceAll(/[-:.TZ]/g, "");
const suffix = randomBytes(4).toString("hex");
return resolve(
inferenceRoot,
"results/parity/upstream",
`${timestamp}-${suffix}`
);
};
const prepareOutputDirectory = async (path: string, explicit: boolean) => {
if (explicit) {
try {
const entries = await readdir(path);
if (entries.length > 0) {
throw new Error(`Evidence output directory is not empty: ${path}`);
}
return;
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (code !== "ENOENT") throw error;
}
}
await mkdir(path, { recursive: true });
};
const main = async () => {
const args = parseArguments();
if (args.help) {
console.log(usage);
return;
}
const manifest = await loadTargetManifest(inferenceRoot);
const selected = selectReferenceTargets(
manifest,
args.targets.length === 0 ? ["focused-tests"] : args.targets
);
const testNames = uniqueStrings(
selected.flatMap((target) => [...target.ctest_names])
);
const setupNames = uniqueStrings(
selected.flatMap((target) => [...target.ctest_setup_names])
);
const modelFixtures = selected.flatMap((target) => [
...target.ctest_model_fixtures,
]);
if (testNames.length === 0) {
throw new Error("Selected reference targets do not declare any CTests");
}
const outputDirectory = outputDirectoryFor(args.outputDirectory);
const referenceOutput = resolve(outputDirectory, "reference");
const environment = controlledEnvironment({
CMAKE_BUILD_PARALLEL_LEVEL: String(args.parallel),
});
await prepareOutputDirectory(
outputDirectory,
args.outputDirectory !== undefined
);
const buildCommand = [
"bun",
"run",
"scripts/build-reference.ts",
"--backend",
args.backend,
"--lane",
"cargo-equivalent",
"--parallel",
String(args.parallel),
"--output-dir",
referenceOutput,
...selected.flatMap((target) => ["--target", target.id]),
];
const buildExitCode = await run(buildCommand, inferenceRoot, environment);
if (buildExitCode !== 0) {
throw new Error(`Reference build exited with status ${buildExitCode}`);
}
const pin = await Effect.runPromise(verifyNativePin());
const source = resolve(inferenceRoot, pin.llama_cpp.checkout_path);
const build = await referenceBuildDirectory(
resolve(
referenceOutput,
`reference-build-${args.backend}-cargo-equivalent.json`
)
);
const fixturePreparation = await prepareOfflineUpstreamFixtures({
registryPath: resolve(inferenceRoot, "parity/models/registry.toml"),
fixtures: modelFixtures,
buildDirectory: build,
configuredModelDirectory: args.modelDirectory,
cwd: process.cwd(),
});
const selectionArguments = ctestSelectionArguments({
buildDirectory: build,
testNames,
setupNames,
});
const junitPath = resolve(outputDirectory, `upstream-ctest-${args.backend}.xml`);
const inventoryPath = resolve(
outputDirectory,
`upstream-ctest-inventory-${args.backend}.json`
);
const testCommand = [
"ctest",
...selectionArguments,
"--output-on-failure",
"--no-tests=error",
"--output-junit",
junitPath,
];
const ctestInventory = await captureCTestInventory({
cwd: inferenceRoot,
buildDirectory: build,
testNames,
setupNames,
environment,
});
await writeFile(inventoryPath, `${JSON.stringify(ctestInventory, null, 2)}\n`, {
flag: "wx",
});
const startedAt = new Date().toISOString();
let testExitCode: number;
try {
testExitCode = await run(testCommand, inferenceRoot, environment);
} finally {
await Promise.all([
cleanupGeneratedTestFiles(source),
verifyOfflineUpstreamFixtures(fixturePreparation),
]);
}
const completedAt = new Date().toISOString();
const junitSummary = await verifyCTestJunit(junitPath, testNames);
const evidenceArtifact = async (path: string) => {
const file = Bun.file(path);
const bytes = new Uint8Array(await file.arrayBuffer());
return {
path,
bytes: bytes.byteLength,
sha256: createHash("sha256").update(bytes).digest("hex"),
};
};
const result = {
schemaVersion: 3,
startedAt,
completedAt,
backend: args.backend,
lane: "cargo-equivalent",
buildDirectory: build,
environment: controlledEnvironmentEvidence(environment),
selectedTargets: selected.map((target) => target.id),
llamaCppRevision: pin.llama_cpp.revision,
bindingsRevision: pin.llama_cpp_rs.revision,
tests: testNames,
setupTests: setupNames,
suppressedSetupTests: setupNames,
expectedExecutedTests: testNames.length,
executedTests: junitSummary.tests,
ctestInventory: await evidenceArtifact(inventoryPath),
ctestJunit: await evidenceArtifact(junitPath),
junitSummary,
offline: true,
fixturePreparation,
artifacts: fixturePreparation.artifacts.map((artifact) => ({
kind: "model",
id: artifact.registryId,
role: artifact.role,
bytes: artifact.bytes,
sha256: artifact.sha256,
sourceCachePath: artifact.sourceCachePath,
stagedPath: artifact.stagedPath,
stagingMethod: artifact.stagingMethod,
})),
ctestArguments: testCommand.slice(1),
status:
testExitCode === 0 &&
junitSummary.failures === 0 &&
junitSummary.disabled === 0 &&
junitSummary.skipped === 0
? "passed"
: "failed",
exitCode: testExitCode,
};
const resultPath = resolve(
outputDirectory,
`upstream-tests-${args.backend}.json`
);
await writeFile(resultPath, `${JSON.stringify(result, null, 2)}\n`, {
flag: "wx",
});
console.log(`Upstream test evidence: ${resultPath}`);
if (testExitCode !== 0) {
throw new Error(`CTest exited with status ${testExitCode}`);
}
if (
junitSummary.failures !== 0 ||
junitSummary.disabled !== 0 ||
junitSummary.skipped !== 0
) {
throw new Error(
"CTest did not execute every declared test successfully; see the preserved JUnit evidence"
);
}
};
main().catch((error) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});