File size: 4,735 Bytes
f8d016c
 
 
 
11d0b50
f8d016c
 
 
 
 
 
 
 
 
 
 
11d0b50
 
 
 
 
 
 
 
 
 
 
 
f8d016c
11d0b50
f8d016c
11d0b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8d016c
 
 
 
 
 
 
 
 
 
 
 
 
11d0b50
 
f8d016c
 
11d0b50
 
 
 
f8d016c
 
 
11d0b50
f8d016c
 
 
 
 
 
 
 
 
 
11d0b50
f8d016c
 
11d0b50
 
 
 
 
 
 
 
f8d016c
 
 
 
 
 
 
 
 
 
 
 
11d0b50
 
f8d016c
11d0b50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8d016c
 
11d0b50
 
 
 
 
 
 
 
 
 
f8d016c
 
11d0b50
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { pipeline } from "@huggingface/transformers";
import { BenchmarkRawResult, aggregateMetrics } from "../core/metrics.js";
import { BenchmarkResult } from "../core/types.js";
import { clearCaches } from "./cache.js";
import { getBrowserEnvInfo } from "./envinfo.js";

function now() {
  return performance.now();
}

async function benchOnce(
  modelId: string,
  task: string,
  device: string,
  dtype: string | undefined,
  batchSize: number
): Promise<BenchmarkRawResult | { error: { type: string; message: string; stage: "load" | "inference" } }> {
  try {
    const t0 = now();
    const options: any = { device };
    if (dtype) options.dtype = dtype;
    const pipe = await pipeline(task, modelId, options);
    const t1 = now();

    // Prepare batch input
    const inputs = Array(batchSize).fill("The quick brown fox jumps over the lazy dog.");

    const t2 = now();
    await pipe(inputs);
    const t3 = now();

    // Run additional inferences to measure subsequent performance
    const subsequentTimes: number[] = [];
    for (let i = 0; i < 3; i++) {
      const t4 = now();
      await pipe(inputs);
      const t5 = 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,
    };
  } catch (error: any) {
    // Determine error type and stage
    const errorMessage = error?.message || String(error);
    let errorType = "runtime_error";
    let stage: "load" | "inference" = "load";

    if (errorMessage.includes("Aborted") || errorMessage.includes("out of memory")) {
      errorType = "memory_error";
    } else if (errorMessage.includes("Failed to fetch") || errorMessage.includes("network")) {
      errorType = "network_error";
    }

    return {
      error: {
        type: errorType,
        message: errorMessage,
        stage,
      },
    };
  }
}

export async function runWebBenchmarkCold(
  modelId: string,
  task: string,
  repeats: number,
  device: string,
  dtype?: string,
  batchSize: number = 1
): Promise<BenchmarkResult> {
  await clearCaches();

  const results: BenchmarkRawResult[] = [];
  let error: { type: string; message: string; stage: "load" | "inference" } | undefined;

  for (let i = 0; i < repeats; i++) {
    const r = await benchOnce(modelId, task, device, dtype, batchSize);
    if ('error' in r) {
      error = r.error;
      break;
    }
    results.push(r);
  }

  const envInfo = await getBrowserEnvInfo();

  const result: BenchmarkResult = {
    platform: "browser",
    runtime: navigator.userAgent,
    mode: "cold",
    repeats,
    batchSize,
    model: modelId,
    task,
    device,
    environment: envInfo,
    notes: "Only the 1st iteration is strictly cold in a single page session.",
  };

  if (error) {
    result.error = error;
  } else {
    const metrics = aggregateMetrics(results);
    result.metrics = metrics;
  }

  if (dtype) result.dtype = dtype;
  return result;
}

export async function runWebBenchmarkWarm(
  modelId: string,
  task: string,
  repeats: number,
  device: string,
  dtype?: string,
  batchSize: number = 1
): Promise<BenchmarkResult> {
  let error: { type: string; message: string; stage: "load" | "inference" } | undefined;

  // Prefetch/warmup
  try {
    const options: any = { device };
    if (dtype) options.dtype = dtype;
    const p = await pipeline(task, modelId, options);
    const warmupInputs = Array(batchSize).fill("warmup");
    await p(warmupInputs);
  } catch (err: any) {
    const errorMessage = err?.message || String(err);
    let errorType = "runtime_error";
    if (errorMessage.includes("Aborted") || errorMessage.includes("out of memory")) {
      errorType = "memory_error";
    } else if (errorMessage.includes("Failed to fetch") || errorMessage.includes("network")) {
      errorType = "network_error";
    }
    error = {
      type: errorType,
      message: errorMessage,
      stage: "load",
    };
  }

  const results: BenchmarkRawResult[] = [];

  if (!error) {
    for (let i = 0; i < repeats; i++) {
      const r = await benchOnce(modelId, task, device, dtype, batchSize);
      if ('error' in r) {
        error = r.error;
        break;
      }
      results.push(r);
    }
  }

  const envInfo = await getBrowserEnvInfo();

  const result: BenchmarkResult = {
    platform: "browser",
    runtime: navigator.userAgent,
    mode: "warm",
    repeats,
    batchSize,
    model: modelId,
    task,
    device,
    environment: envInfo,
  };

  if (error) {
    result.error = error;
  } else {
    const metrics = aggregateMetrics(results);
    result.metrics = metrics;
  }

  if (dtype) result.dtype = dtype;
  return result;
}