import { execFileSync } from 'node:child_process'; import { existsSync, writeFileSync } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '../../..'); const args = new Map(); for (let i = 2; i < process.argv.length; i += 2) { const key = process.argv[i]; const value = process.argv[i + 1]; if (key?.startsWith('--') && value) args.set(key.slice(2), value); } const profile = args.get('profile') ?? 'q8_0'; const model = resolve( root, profile === 'q4_k_m' ? 'model-src/gguf/botpsy-0.8b-q4_k_m-imat.gguf' : 'model-src/gguf/botpsy-0.8b-q8_0.gguf', ); const bench = resolve( root, args.get('bench') ?? process.env.LLAMA_BENCH ?? 'reference/llama.cpp/build/bin/llama-bench', ); if (!existsSync(bench)) throw new Error(`native llama-bench is unavailable at ${bench}`); if (!existsSync(model)) throw new Error(`missing GGUF ${model}`); const promptTokens = Number(args.get('prompt') ?? 128); const genTokens = Number(args.get('gen') ?? 8); const repetitions = Number(args.get('repetitions') ?? 1); const output = execFileSync( bench, ['-m', model, '-p', String(promptTokens), '-n', String(genTokens), '-r', String(repetitions), '-o', 'json'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'inherit'] }, ); const records = JSON.parse(output) as Array>; const report = { schemaVersion: 1, engine: 'native llama.cpp llama-bench', enginePath: bench, profile, model, records, }; const target = resolve(root, args.get('out') ?? `artifacts/native-${profile}-bench.json`); writeFileSync(target, `${JSON.stringify(report, null, 2)}\n`); console.log(JSON.stringify({ ok: true, output: target, records: records.length }, null, 2));