neurarch 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +507 -0
- package/README.md +662 -152
- package/attest.bundle.mjs +343 -0
- package/bin/attest.mjs +188 -0
- package/bin/neurarch.mjs +109 -22
- package/bin/pinCheck.mjs +138 -0
- package/bin/policy.mjs +320 -0
- package/bin/scan.mjs +255 -0
- package/bin/templateCheck.mjs +99 -0
- package/bin/weightsDiff.mjs +35 -0
- package/engine.bundle.mjs +9481 -1364
- package/examples/.neurarch.yml +44 -0
- package/examples/github-attest.yml +69 -0
- package/examples/github-precheck.yml +40 -0
- package/package.json +5 -2
- package/policy.bundle.mjs +8392 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `neurarch template-check <hf-id>`: does the prompt an engine sends match the
|
|
3
|
+
* lab's reference, and did an upstream template change move it?
|
|
4
|
+
*
|
|
5
|
+
* Reads chat_template.jinja (or tokenizer_config.json's chat_template) at the
|
|
6
|
+
* given revisions from the Hugging Face Hub, then renders a fixed conversation
|
|
7
|
+
* set with src/utils/templateConformance.ts (through engine.bundle.mjs). No
|
|
8
|
+
* weights, no GPU. Exit 1 when a prompt differs, so it can gate CI.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const tty = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
12
|
+
const c = (code, s) => (tty ? `\x1b[${code}m${s}\x1b[0m` : String(s));
|
|
13
|
+
|
|
14
|
+
const HELP = `neurarch template-check: the prompt an engine sends, against the reference
|
|
15
|
+
|
|
16
|
+
npx neurarch template-check <hf-id> [--at <rev>] [--tool-parts] [--json]
|
|
17
|
+
npx neurarch template-check <hf-id> --from <rev> [--to <rev>] [--tool-parts] [--json]
|
|
18
|
+
|
|
19
|
+
With one revision (--at, default main): renders the model's chat template the
|
|
20
|
+
way transformers' apply_chat_template is usually called (content as a string)
|
|
21
|
+
and the way vLLM calls it with --chat-template-content-format auto (system,
|
|
22
|
+
user and assistant content as text parts when the template reads parts; tool
|
|
23
|
+
results as strings, as measured on vLLM 0.33.0), on a system+user turn, a
|
|
24
|
+
multi-turn chat and a tool call. A difference is a prompt vLLM sends that the
|
|
25
|
+
reference does not. --tool-parts also hands tool results as parts, as older
|
|
26
|
+
stacks did and as a client that sends parts gets.
|
|
27
|
+
|
|
28
|
+
With --from: renders the template at both revisions in the engine's form. A
|
|
29
|
+
difference is an upstream change that moves what a deployment sends.
|
|
30
|
+
|
|
31
|
+
Set HF_TOKEN for gated repos. Exits 1 when any prompt differs, 2 on a read error.
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
function templateFrom(tok) {
|
|
35
|
+
const t = tok?.chat_template;
|
|
36
|
+
if (typeof t === 'string') return t;
|
|
37
|
+
if (Array.isArray(t)) {
|
|
38
|
+
const d = t.find((x) => x && x.name === 'default') ?? t[0];
|
|
39
|
+
return typeof d?.template === 'string' ? d.template : null;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const tokenText = (v) => (v && typeof v === 'object' && typeof v.content === 'string' ? v.content : typeof v === 'string' ? v : null);
|
|
44
|
+
|
|
45
|
+
async function readAt(id, rev) {
|
|
46
|
+
const headers = process.env.HF_TOKEN ? { Authorization: `Bearer ${process.env.HF_TOKEN}` } : {};
|
|
47
|
+
const get = async (file) => {
|
|
48
|
+
const r = await fetch(`https://huggingface.co/${id}/resolve/${encodeURIComponent(rev)}/${file}`, { headers, signal: AbortSignal.timeout(20_000) });
|
|
49
|
+
if (r.status === 404) return null;
|
|
50
|
+
if (!r.ok) throw new Error(`${file} at ${rev}: HTTP ${r.status}${r.status === 401 || r.status === 403 ? ' (gated: set HF_TOKEN)' : ''}`);
|
|
51
|
+
return r.text();
|
|
52
|
+
};
|
|
53
|
+
const [jinja, tokRaw] = await Promise.all([get('chat_template.jinja'), get('tokenizer_config.json')]);
|
|
54
|
+
let tok = null;
|
|
55
|
+
try { tok = tokRaw ? JSON.parse(tokRaw) : null; } catch { tok = null; }
|
|
56
|
+
const text = jinja ?? templateFrom(tok);
|
|
57
|
+
if (text == null) throw new Error(`no chat template at ${rev} (neither chat_template.jinja nor tokenizer_config.json carries one)`);
|
|
58
|
+
return { text, specials: { bos_token: tokenText(tok?.bos_token), eos_token: tokenText(tok?.eos_token) } };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function templateCheckCommand(argv, lib) {
|
|
62
|
+
const o = { engine: 'vllm', json: false };
|
|
63
|
+
const pos = [];
|
|
64
|
+
for (let i = 0; i < argv.length; i++) {
|
|
65
|
+
const a = argv[i];
|
|
66
|
+
if (a === '-h' || a === '--help') { process.stdout.write(HELP); return 0; }
|
|
67
|
+
if (a === '--json') { o.json = true; continue; }
|
|
68
|
+
if (a === '--tool-parts') { o.toolParts = true; continue; }
|
|
69
|
+
if (['--engine', '--at', '--from', '--to'].includes(a)) { o[a.slice(2)] = argv[++i]; continue; }
|
|
70
|
+
if (a.startsWith('--')) { process.stderr.write(`unknown flag ${a}\n${HELP}`); return 2; }
|
|
71
|
+
pos.push(a);
|
|
72
|
+
}
|
|
73
|
+
const id = pos[0];
|
|
74
|
+
if (!id) { process.stderr.write(HELP); return 2; }
|
|
75
|
+
if (!['vllm', 'sglang'].includes(o.engine)) { process.stderr.write(`--engine must be vllm or sglang\n`); return 2; }
|
|
76
|
+
try {
|
|
77
|
+
let result, labels;
|
|
78
|
+
if (o.from) {
|
|
79
|
+
const to = o.to ?? 'main';
|
|
80
|
+
const [a, b] = await Promise.all([readAt(id, o.from), readAt(id, to)]);
|
|
81
|
+
result = lib.checkRevision(a.text, b.text, o.engine, a.specials, b.specials, { toolParts: !!o.toolParts });
|
|
82
|
+
labels = { left: o.from.slice(0, 12), right: to.slice(0, 12) };
|
|
83
|
+
} else {
|
|
84
|
+
const at = o.at ?? 'main';
|
|
85
|
+
const s = await readAt(id, at);
|
|
86
|
+
result = lib.checkParity(s.text, o.engine, s.specials, { toolParts: !!o.toolParts });
|
|
87
|
+
labels = { left: 'reference', right: o.engine };
|
|
88
|
+
}
|
|
89
|
+
if (o.json) process.stdout.write(JSON.stringify({ model: id, ...result }, null, 2) + '\n');
|
|
90
|
+
else {
|
|
91
|
+
const text = lib.renderConformanceText(result, labels);
|
|
92
|
+
process.stdout.write((result.verdict === 'different' ? c('31', text.split('\n')[0]) + '\n' + text.split('\n').slice(1).join('\n') : text) + '\n');
|
|
93
|
+
}
|
|
94
|
+
return result.verdict === 'different' ? 1 : 0;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
process.stderr.write(`${c('31', 'cannot read the template')}: ${e.message}\n`);
|
|
97
|
+
return 2;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `neurarch weights-diff <a> <b>`: which tensors differ between two local
|
|
3
|
+
* safetensors checkpoints, and the shape of each edit. The reading is
|
|
4
|
+
* scripts/weightsDiff.ts through the engine bundle; this file only parses
|
|
5
|
+
* arguments and prints. Nothing leaves the machine.
|
|
6
|
+
*/
|
|
7
|
+
const USAGE = `usage: npx neurarch weights-diff <a> <b> [--json] [--max-tensors N]
|
|
8
|
+
|
|
9
|
+
<a>, <b> a .safetensors file, or a directory of them
|
|
10
|
+
--max-tensors N read the shape of at most N changed tensors (default 32); the rest are counted
|
|
11
|
+
--json the full result
|
|
12
|
+
|
|
13
|
+
Exit code 0 when the weights match, 1 when they differ, 2 on a usage or read error.
|
|
14
|
+
`;
|
|
15
|
+
|
|
16
|
+
export function weightsDiffCommand(args, weightsDiff, render) {
|
|
17
|
+
const pos = [];
|
|
18
|
+
const opts = {};
|
|
19
|
+
let json = false;
|
|
20
|
+
for (let i = 0; i < args.length; i++) {
|
|
21
|
+
const a = args[i];
|
|
22
|
+
if (a === '--json') json = true;
|
|
23
|
+
else if (a === '--max-tensors') opts.maxTensors = Number(args[++i]);
|
|
24
|
+
else if (a === '-h' || a === '--help') { process.stdout.write(USAGE); return 0; }
|
|
25
|
+
else pos.push(a);
|
|
26
|
+
}
|
|
27
|
+
if (pos.length !== 2 || (opts.maxTensors != null && !(opts.maxTensors >= 0))) { process.stderr.write(USAGE); return 2; }
|
|
28
|
+
let r;
|
|
29
|
+
try { r = weightsDiff(pos[0], pos[1], opts); } catch (e) {
|
|
30
|
+
process.stderr.write(`neurarch weights-diff: ${e instanceof Error ? e.message : e}\n`);
|
|
31
|
+
return 2;
|
|
32
|
+
}
|
|
33
|
+
process.stdout.write(json ? `${JSON.stringify(r, null, 2)}\n` : render(r));
|
|
34
|
+
return r.a.digest === r.b.digest ? 0 : 1;
|
|
35
|
+
}
|