File size: 3,168 Bytes
f8d016c
 
 
 
 
 
11d0b50
5d65665
de9b734
f8d016c
 
 
 
 
 
 
 
 
 
 
 
 
5d65665
 
f8d016c
 
5d65665
f8d016c
 
 
 
 
 
5d65665
f8d016c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de9b734
 
 
 
 
 
 
f8d016c
 
 
 
 
 
 
 
 
5d65665
 
f8d016c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11d0b50
f8d016c
 
 
 
 
 
 
 
 
 
 
11d0b50
f8d016c
 
 
 
 
 
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
import { pipeline, env } from "@huggingface/transformers";
import { performance } from "node:perf_hooks";
import path from "node:path";
import { BenchmarkOptions, BenchmarkResult } from "../core/types.js";
import { BenchmarkRawResult, aggregateMetrics } from "../core/metrics.js";
import { ensureEmptyDir } from "./cache.js";
import { getSystemInfo } from "../core/sysinfo.js";
import { getTaskInput } from "../core/task-inputs.js";
import { logger } from "../core/logger.js";

async function benchOnce(
  modelId: string,
  task: string,
  dtype: string | undefined,
  batchSize: number
): Promise<BenchmarkRawResult> {
  const t0 = performance.now();
  const options: any = {};
  if (dtype) options.dtype = dtype;
  const pipe = await pipeline(task, modelId, options);
  const t1 = performance.now();

  // Get task-appropriate input
  const { inputs, options: taskOptions } = getTaskInput(task, batchSize);

  const t2 = performance.now();
  await pipe(inputs, taskOptions);
  const t3 = performance.now();

  // Run additional inferences to measure subsequent performance
  const subsequentTimes: number[] = [];
  for (let i = 0; i < 3; i++) {
    const t4 = performance.now();
    await pipe(inputs, taskOptions);
    const t5 = performance.now();
    subsequentTimes.push(+(t5 - t4).toFixed(1));
  }

  return {
    load_ms: +(t1 - t0).toFixed(1),
    first_infer_ms: +(t3 - t2).toFixed(1),
    subsequent_infer_ms: subsequentTimes,
  };
}

export async function runNodeBenchmark(options: BenchmarkOptions): Promise<BenchmarkResult> {
  const { modelId, task, mode, repeats, dtype, batchSize } = options;

  const cacheDir = path.resolve(".bench-cache/default");
  env.cacheDir = cacheDir;

  logger.log(`Model     : ${modelId}`);
  logger.log(`Task      : ${task}`);
  logger.log(`Mode      : ${mode}`);
  logger.log(`Repeats   : ${repeats}`);
  logger.log(`DType     : ${dtype || 'auto'}`);
  logger.log(`Batch Size: ${batchSize}`);
  logger.log(`Cache     : ${cacheDir}`);

  const results: BenchmarkRawResult[] = [];

  if (mode === "warm") {
    // Fresh cache dir, prefetch once (not measured), then measure N times
    ensureEmptyDir(cacheDir);
    const warmOptions: any = {};
    if (dtype) warmOptions.dtype = dtype;
    const warm = await pipeline(task, modelId, warmOptions);
    const { inputs: warmupInputs, options: taskOptions } = getTaskInput(task, batchSize);
    await warm(warmupInputs, taskOptions);

    for (let i = 0; i < repeats; i++) {
      const r = await benchOnce(modelId, task, dtype, batchSize);
      results.push(r);
    }
  } else {
    // cold: delete cache dir before each measured run
    for (let i = 0; i < repeats; i++) {
      ensureEmptyDir(cacheDir);
      const r = await benchOnce(modelId, task, dtype, batchSize);
      results.push(r);
    }
  }

  const metrics = aggregateMetrics(results);
  const sysInfo = getSystemInfo();

  const result: BenchmarkResult = {
    platform: "node",
    runtime: `node-${process.versions.node}`,
    model: modelId,
    task,
    mode,
    repeats,
    batchSize,
    cacheDir,
    metrics,
    environment: sysInfo,
  };

  if (dtype) result.dtype = dtype;

  return result;
}