-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathllmUtils.ts
More file actions
340 lines (301 loc) · 10.2 KB
/
Copy pathllmUtils.ts
File metadata and controls
340 lines (301 loc) · 10.2 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import dayjs from "dayjs";
import { geomean } from "lib/benchmark/compilerUtils";
import { fetcher } from "lib/GeneralUtils";
import { BranchAndCommit } from "lib/types";
import useSWR from "swr";
import {
BranchAndCommitPerfData,
DEFAULT_ARCH_NAME,
DEFAULT_BACKEND_NAME,
DEFAULT_DEVICE_NAME,
DEFAULT_DTYPE_NAME,
DEFAULT_MODE_NAME,
DEFAULT_MODEL_NAME,
EXCLUDED_METRICS,
LLMsBenchmarkData,
REPO_TO_BENCHMARKS,
} from "../common";
import { LLMsBenchmarkProps } from "../types/dashboardProps";
import { TORCHAO_BASELINE } from "./aoUtils";
export function useBenchmark(
queryParams: { [key: string]: any },
branchAndCommit: BranchAndCommit
) {
const queryName: string = "oss_ci_benchmark_llms";
const queryParamsWithBranchAndCommit: { [key: string]: any } = queryParams;
(queryParamsWithBranchAndCommit as { [key: string]: any })["branches"] =
branchAndCommit.branch ? [branchAndCommit.branch] : [];
(queryParamsWithBranchAndCommit as { [key: string]: any })["commits"] =
branchAndCommit.commit ? [branchAndCommit.commit] : [];
const url = `/api/clickhouse/${queryName}?parameters=${encodeURIComponent(
JSON.stringify(queryParamsWithBranchAndCommit)
)}`;
return useSWR(url, fetcher, {
refreshInterval: 60 * 60 * 1000, // refresh every hour
});
}
/**
* generate query params for benchmark page.
* @param props LLMsBenchmarkProps
*/
export function getLLMsBenchmarkPropsQueryParameter(props: LLMsBenchmarkProps) {
const queryParams = {
arch: props.archName === DEFAULT_ARCH_NAME ? "" : props.archName,
device: props.deviceName === DEFAULT_DEVICE_NAME ? "" : props.deviceName,
mode: props.modeName === DEFAULT_MODE_NAME ? "" : props.modeName,
dtypes:
props.dtypeName === DEFAULT_DTYPE_NAME
? []
: props.repoName !== "pytorch/ao" // TODO(elainewy): add config to handle repos-specific logics
? [props.dtypeName]
: [props.dtypeName, TORCHAO_BASELINE],
excludedMetrics: EXCLUDED_METRICS,
benchmarks: props.benchmarkName
? [props.benchmarkName]
: REPO_TO_BENCHMARKS[props.repoName],
granularity: props.granularity,
models: props.modelName === DEFAULT_MODEL_NAME ? [] : [props.modelName],
backends:
props.backendName === DEFAULT_BACKEND_NAME ? [] : [props.backendName],
repo: props.repoName,
startTime: dayjs(props.startTime).utc().format("YYYY-MM-DDTHH:mm:ss.SSS"),
stopTime: dayjs(props.stopTime).utc().format("YYYY-MM-DDTHH:mm:ss.SSS"),
};
return queryParams;
}
export const useBenchmarkPropsData = (queryParams: any) => {
const queryName = "oss_ci_benchmark_names";
const url = `/api/clickhouse/${queryName}?parameters=${encodeURIComponent(
JSON.stringify(queryParams)
)}`;
return useSWR(url, fetcher, {
refreshInterval: 60 * 60 * 1000, // refresh every
});
};
export function combineLeftAndRight(
repoName: string,
benchmarkName: string,
lPerfData: BranchAndCommitPerfData,
rPerfData: BranchAndCommitPerfData
): { [k: string]: any }[] {
// The left (base commit)
const lBranch = lPerfData.branch;
const lCommit = lPerfData.commit;
const lData = lPerfData.data;
// and the right (new commit)
const rBranch = rPerfData.branch;
const rCommit = rPerfData.commit;
const rData = rPerfData.data;
const dataGroupedByModel: { [k: string]: any } = {};
rData.forEach((record: LLMsBenchmarkData) => {
const model = record.model;
const backend = record.backend;
const mode = record.mode;
const dtype = record.dtype;
const device = record.device;
const arch = record.arch;
const extra = JSON.stringify(record.extra);
const metric = record.metric;
const key = `${model};${backend};${mode};${dtype};${device};${arch};${extra}`;
if (!(key in dataGroupedByModel)) {
dataGroupedByModel[key] = {};
}
if (!(metric in dataGroupedByModel[key])) {
dataGroupedByModel[key][metric] = {};
}
dataGroupedByModel[key][metric] = {
r: record,
};
});
// Combine with left (base) data
if (lCommit !== rCommit && lData !== undefined) {
lData.forEach((record: LLMsBenchmarkData) => {
const model = record.model;
const backend = record.backend;
const mode = record.mode;
const dtype = record.dtype;
const device = record.device;
const arch = record.arch;
const extra = JSON.stringify(record.extra);
const metric = record.metric;
const key = `${model};${backend};${mode};${dtype};${device};${arch};${extra}`;
if (!(key in dataGroupedByModel)) {
dataGroupedByModel[key] = {};
}
if (!(metric in dataGroupedByModel[key])) {
dataGroupedByModel[key][metric] = {};
}
dataGroupedByModel[key][metric]["l"] = record;
});
}
// NB: This is a hack to keep track of valid devices. The problem is that the records
// in the benchmark database alone don't have the information to differentiate between
// benchmarks that are failed to run and benchmarks that are not run. Both show up as
// 0 on the dashboard. Note that we can do a join with workflow_job table to get this
// information, but it's a rather slow and expensive route
const validDevices = new Set<string>();
const validBackends = new Set<string>();
// First round to get all the valid devices
Object.keys(dataGroupedByModel).forEach((key: string) => {
const [model, backend, mode, dtype, device, arch, extra] = key.split(";");
const row: { [k: string]: any } = {
// Keep the name as as the row ID as DataGrid requires it
name: `${model} ${backend} (${mode} / ${dtype} / ${device} / ${arch})`,
};
for (const metric in dataGroupedByModel[key]) {
const record = dataGroupedByModel[key][metric];
const hasL = "l" in record;
const hasR = "r" in record;
if (hasL && hasR) {
validDevices.add(device);
validBackends.add(`${model} ${backend}`);
}
}
});
// Transform the data into a displayable format
const data: { [k: string]: any }[] = [];
Object.keys(dataGroupedByModel).forEach((key: string) => {
const [model, backend, mode, dtype, device, arch, extra] = key.split(";");
const row: { [k: string]: any } = {
// Keep the name as as the row ID as DataGrid requires it
name: `${model} ${backend} (${mode} / ${dtype} / ${device} / ${arch} / ${extra})`,
};
for (const metric in dataGroupedByModel[key]) {
const record = dataGroupedByModel[key][metric];
const hasL = "l" in record;
const hasR = "r" in record;
// Skip devices and models that weren't run in this commit
if (
(validDevices.size !== 0 && !validDevices.has(device)) ||
(validBackends.size !== 0 && !validBackends.has(`${model} ${backend}`))
) {
continue;
}
// No overlapping between left and right commits, just show what it's on the
// right commit instead of showing a blank page
if (!hasR) {
continue;
}
if (!("metadata" in row)) {
row["metadata"] = {
model: model,
origins: record["r"].origins,
backend: backend,
mode: mode,
dtype: dtype,
device: device,
arch: arch,
l: hasL ? record["l"]["job_id"] : undefined,
r: hasR ? record["r"]["job_id"] : undefined,
};
} else {
row["metadata"]["l"] =
row["metadata"]["l"] ?? (hasL ? record["l"]["job_id"] : undefined);
row["metadata"]["r"] =
row["metadata"]["r"] ?? (hasR ? record["r"]["job_id"] : undefined);
}
if (mode !== "") {
row["mode"] = mode;
}
if (dtype !== "") {
row["dtype"] = dtype;
}
if (backend !== "") {
row["backend"] = backend;
}
row["device_arch"] = {
device: device,
arch: arch,
};
if (repoName === "vllm-project/vllm") {
// These fields are only available on vLLM benchmark
const extraInfo = JSON.parse(extra);
row["extra"] = extraInfo;
row["tensor_parallel_size"] = extraInfo["tensor_parallel_size"];
row["request_rate"] = extraInfo["request_rate"];
}
if (
repoName === "pytorch/pytorch" &&
benchmarkName === "TorchCache Benchmark"
) {
const extraInfo = JSON.parse(extra);
row["is_dynamic"] = extraInfo["is_dynamic"];
}
row[metric] = {
l: hasL
? {
actual: record["l"].actual,
target: record["l"].target,
}
: {
actual: 0,
target: 0,
},
r: hasR
? {
actual: record["r"].actual,
target: record["r"].target,
}
: {
actual: 0,
target: 0,
},
highlight:
validDevices.size !== 0 &&
validBackends.has(`${model} ${backend}`) &&
hasL &&
hasR,
};
}
if ("metadata" in row) {
data.push(row);
}
});
return data;
}
export function computeGeomean(data: LLMsBenchmarkData[], metricName: string) {
const metricValues: { [key: string]: number[] } = {};
const returnedGeomean: LLMsBenchmarkData[] = [];
data.forEach((r: LLMsBenchmarkData) => {
if (r.metric !== metricName) {
return;
}
const origins = r.origins.join(",");
const k = `${r.granularity_bucket}+${r.workflow_id}+${r.job_id}+${r.backend}+${r.dtype}+${origins}+${r.device}+${r.arch}+${r.metric}`;
if (!(k in metricValues)) {
metricValues[k] = [];
}
if (r.actual !== 0) {
metricValues[k].push(r.actual);
}
});
Object.keys(metricValues).forEach((k: string) => {
const gm = geomean(metricValues[k]);
const [
bucket,
workflowId,
jobId,
backend,
dtype,
origins,
device,
arch,
metric,
] = k.split("+");
returnedGeomean.push({
granularity_bucket: bucket,
model: "",
backend: backend,
origins: origins.split(","),
workflow_id: Number(workflowId),
job_id: Number(jobId),
metric: `${metric} (geomean)`,
actual: Number(gm),
target: 0,
dtype: dtype,
device: device,
arch: arch,
});
});
return returnedGeomean;
}