File size: 1,818 Bytes
1cc8d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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<string, string>();
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<Record<string, unknown>>;
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));