-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_transformer_capture.js
More file actions
56 lines (53 loc) · 2.12 KB
/
Copy pathvalidate_transformer_capture.js
File metadata and controls
56 lines (53 loc) · 2.12 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
import fs from "node:fs";
import path from "node:path";
import { RequestLedger } from "./request_ledger.js";
import { TransformerTraceCapture } from "./transformer_trace.js";
const runId = process.argv[2] ?? `transformer-capture-${new Date().toISOString().replace(/[-:TZ.]/g, "")}`;
const baseUrl = new URL("http://127.0.0.1:8080/v1");
const outputDir = path.resolve("runs", runId);
fs.mkdirSync(outputDir, { recursive: true });
const ledger = new RequestLedger({ baseUrl, runId });
const capture = new TransformerTraceCapture({ runId });
await ledger.initialize();
await capture.initialize();
await capture.arm();
const request = {
model: "/opt/runtime/models/Qwen3-8B-Q4_K_M.gguf",
messages: [
{ role: "system", content: "Introspect." },
{ role: "user", content: "Return a short neutral marker." }
],
temperature: 0,
max_tokens: 8,
logprobs: true,
top_logprobs: 10,
chat_template_kwargs: { enable_thinking: false }
};
const startedAt = new Date().toISOString();
const http = await fetch(`${baseUrl.origin}/v1/chat/completions`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(request),
signal: AbortSignal.timeout(180_000)
});
if (!http.ok) throw new Error(`capture request HTTP ${http.status}: ${await http.text()}`);
const response = await http.json();
const endedAt = new Date().toISOString();
const record = await ledger.record({
kind: "transformer_capture_validation", startedAt, endedAt, request, response
});
const promptPositions = await capture.readLivePromptTokenMap(baseUrl);
const index = await capture.collect({ ledgerRecord: record, response, promptPositions });
const sealedLedger = ledger.exportTo(outputDir);
const sealedTrace = capture.exportTo(outputDir);
fs.writeFileSync(path.join(outputDir, "artifact.json"), `${JSON.stringify({
schema: "ik.transformer-capture-validation.v1",
run_id: runId,
request,
response,
transformer_trace: sealedTrace,
request_ledger: sealedLedger,
alignment: index.alignment
}, null, 2)}\n`);
console.log(JSON.stringify({ run_id: runId, output_dir: outputDir, alignment: index.alignment,
tensor_records: index.tensors.length }));