@milabench/bujang 0.1.12 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +23 -5
- package/dist/constants.d.ts +6 -0
- package/dist/index.js +6231 -5068
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +113 -12
- package/dist/views/JobDataOverview.d.ts +4 -3
- package/dist/views/OverviewPanels.d.ts +37 -0
- package/dist/views/PipelineEditor.d.ts +5 -0
- package/dist/views/PipelinesView.d.ts +1 -1
- package/dist/views/ScheduleDialog.d.ts +14 -0
- package/dist/views/SweepDialog.d.ts +51 -0
- package/dist/views/dataCharts.d.ts +56 -0
- package/package.json +1 -1
- package/src/api.ts +52 -4
- package/src/constants.ts +3 -0
- package/src/types.ts +113 -13
- package/src/views/JanitorView.tsx +101 -32
- package/src/views/JobDataOverview.tsx +126 -213
- package/src/views/JobLogs.tsx +13 -6
- package/src/views/JobSubmit.tsx +377 -318
- package/src/views/JobsView.tsx +34 -21
- package/src/views/LogDisplay.tsx +2 -2
- package/src/views/OverviewPanels.tsx +341 -0
- package/src/views/PipelineEditor.tsx +3 -3
- package/src/views/PipelinesView.tsx +387 -164
- package/src/views/ScheduleDialog.tsx +98 -0
- package/src/views/SettingsView.tsx +131 -1
- package/src/views/SweepDialog.tsx +520 -0
- package/src/views/dataCharts.tsx +261 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/** Charts of the JSONL data of a job: the chart builder, its Vega-Lite spec, and the chart itself. */
|
|
2
|
+
import React, { useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { Alert, Box } from '@chakra-ui/react';
|
|
4
|
+
import type { JobData, JobDataField, JobDataRole, OverviewPanel } from '../types';
|
|
5
|
+
import { useColorMode } from '../hooks/useColorMode';
|
|
6
|
+
|
|
7
|
+
/** Rows fetched for a chart (the server caps at 500k) */
|
|
8
|
+
export const ROW_LIMIT = 50_000;
|
|
9
|
+
/** Fields every row has: the file it comes from */
|
|
10
|
+
export const FILE_FIELDS: JobDataField[] = [
|
|
11
|
+
{ name: '_name', type: 'string', count: 0 },
|
|
12
|
+
{ name: '_file', type: 'string', count: 0 },
|
|
13
|
+
];
|
|
14
|
+
export const MARKS = ['point', 'line', 'bar', 'tick', 'boxplot', 'area', 'rect'] as const;
|
|
15
|
+
export const AGGREGATES = ['', 'mean', 'median', 'sum', 'min', 'max', 'count'] as const;
|
|
16
|
+
/** Events listed first: the plotted ones (the server gives each event its role) */
|
|
17
|
+
export const ROLE_ORDER: Record<JobDataRole, number> = { metric: 0, other: 1, log: 2 };
|
|
18
|
+
/** The keys of a `bujang.logs` line around its data: never a category of a chart */
|
|
19
|
+
export const ENVELOPE_FIELDS = new Set(['type', 'event', 'level', 'logger', 'message', 'exception']);
|
|
20
|
+
|
|
21
|
+
export interface Builder {
|
|
22
|
+
mark: (typeof MARKS)[number];
|
|
23
|
+
x: string;
|
|
24
|
+
y: string;
|
|
25
|
+
yAggregate: (typeof AGGREGATES)[number];
|
|
26
|
+
color: string;
|
|
27
|
+
column: string;
|
|
28
|
+
row: string;
|
|
29
|
+
logX: boolean;
|
|
30
|
+
logY: boolean;
|
|
31
|
+
/** the number scales start at 0 (always for bar / area, whose length means something) */
|
|
32
|
+
zero: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const EMPTY_BUILDER: Builder = {
|
|
36
|
+
mark: 'point', x: '', y: '', yAggregate: '', color: '', column: '', row: '', logX: false, logY: false, zero: false,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const vlType = (field?: JobDataField) => (field?.type === 'number' ? 'quantitative' : 'nominal');
|
|
40
|
+
|
|
41
|
+
/** A number field holding unix seconds (time, timestamp): shown as dates */
|
|
42
|
+
export const isUnixSeconds = (field?: JobDataField) =>
|
|
43
|
+
!!field && field.type === 'number' && /time|stamp|date/i.test(field.name) &&
|
|
44
|
+
(field.min ?? 0) > 1e9 && (field.max ?? 0) < 1e11;
|
|
45
|
+
|
|
46
|
+
/** datum["a"]["b"] of a dotted field, for a Vega expression */
|
|
47
|
+
export const datumOf = (name: string) => `datum${name.split('.').map((p) => `[${JSON.stringify(p)}]`).join('')}`;
|
|
48
|
+
|
|
49
|
+
/** Vega-Lite spec of the builder; the rows are the named dataset "rows" */
|
|
50
|
+
export const buildSpec = (b: Builder, fields: Map<string, JobDataField>): Record<string, unknown> => {
|
|
51
|
+
const encoding: Record<string, unknown> = {};
|
|
52
|
+
const transform: Record<string, unknown>[] = [];
|
|
53
|
+
const keepsZero = b.zero || b.mark === 'bar' || b.mark === 'area';
|
|
54
|
+
const channel = (name: string, extra: Record<string, unknown> = {}) => {
|
|
55
|
+
const field = fields.get(name);
|
|
56
|
+
if (isUnixSeconds(field)) {
|
|
57
|
+
// seconds -> a date (Vega times are milliseconds)
|
|
58
|
+
const as = `${name.replace(/\./g, '_')}_date`;
|
|
59
|
+
if (!transform.some((t) => t.as === as)) transform.push({ calculate: `${datumOf(name)} * 1000`, as });
|
|
60
|
+
return { field: as, type: 'temporal', title: name, ...extra };
|
|
61
|
+
}
|
|
62
|
+
const type = vlType(field);
|
|
63
|
+
const scale = { ...(type === 'quantitative' && !keepsZero ? { zero: false } : {}), ...((extra.scale as object) || {}) };
|
|
64
|
+
return { field: name, type, ...extra, ...(Object.keys(scale).length ? { scale } : {}) };
|
|
65
|
+
};
|
|
66
|
+
if (b.x) encoding.x = channel(b.x, b.logX ? { scale: { type: 'log' } } : {});
|
|
67
|
+
if (b.y || b.yAggregate === 'count') {
|
|
68
|
+
encoding.y = b.yAggregate === 'count'
|
|
69
|
+
? { aggregate: 'count', type: 'quantitative' }
|
|
70
|
+
: channel(b.y, {
|
|
71
|
+
...(b.yAggregate ? { aggregate: b.yAggregate } : {}),
|
|
72
|
+
...(b.logY ? { scale: { type: 'log' } } : {}),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (b.color) encoding.color = channel(b.color, { legend: { labelLimit: 400 } });
|
|
76
|
+
if (b.column) encoding.column = channel(b.column);
|
|
77
|
+
if (b.row) encoding.row = channel(b.row);
|
|
78
|
+
encoding.tooltip = [b.x, b.y, b.color, b.column, b.row, '_name']
|
|
79
|
+
.filter((f, i, all) => f && all.indexOf(f) === i)
|
|
80
|
+
.map((f) => ({ field: f, type: vlType(fields.get(f)) }));
|
|
81
|
+
return {
|
|
82
|
+
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
|
|
83
|
+
data: { name: 'rows' },
|
|
84
|
+
...(transform.length ? { transform } : {}),
|
|
85
|
+
// a composite mark (boxplot) takes no `tooltip` (Altair validates the spec)
|
|
86
|
+
mark: { type: b.mark, ...(b.mark === 'boxplot' ? {} : { tooltip: true }), ...(b.mark === 'line' ? { point: true } : {}) },
|
|
87
|
+
encoding,
|
|
88
|
+
width: b.column ? 260 : 'container',
|
|
89
|
+
height: 320,
|
|
90
|
+
// the legend and the axes inside the container width, not past it
|
|
91
|
+
...(b.column ? {} : { autosize: { type: 'fit-x', contains: 'padding' } }),
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** Counters: on x, never on y (the finest first) */
|
|
96
|
+
const COUNTERS = ['step', 'global_step', 'iteration', 'iter', 'batch', 'epoch'];
|
|
97
|
+
const isCounter = (f: JobDataField) => COUNTERS.includes(f.name.toLowerCase()) || /^(rank|seed|pid|gpu|device)$/i.test(f.name);
|
|
98
|
+
|
|
99
|
+
/** Defaults for a set of fields: a category (else a step / epoch, else time) on x, a measure on y, the file as color */
|
|
100
|
+
export const guessBuilder = (fields: JobDataField[], multipleFiles: boolean): Builder => {
|
|
101
|
+
const numbers = fields.filter((f) => f.type === 'number' && f.name !== 'time');
|
|
102
|
+
// the categories with the most values first (test names before task: detail / train)
|
|
103
|
+
const categories = fields
|
|
104
|
+
.filter((f) => f.type === 'string' && !ENVELOPE_FIELDS.has(f.name) && f.values && f.values.length > 1 && f.values.length <= 40)
|
|
105
|
+
.sort((a, b) => (b.values?.length || 0) - (a.values?.length || 0));
|
|
106
|
+
const measures = numbers.filter((f) => !isCounter(f));
|
|
107
|
+
const y =
|
|
108
|
+
measures.find((f) => /rate|throughput|loss|acc|value|per_s|score|error/i.test(f.name)) ||
|
|
109
|
+
measures[0] ||
|
|
110
|
+
numbers[0];
|
|
111
|
+
const counter = COUNTERS.map((name) => numbers.find((f) => f !== y && f.name.toLowerCase() === name)).find(Boolean);
|
|
112
|
+
const x =
|
|
113
|
+
categories[0] ||
|
|
114
|
+
counter ||
|
|
115
|
+
fields.find((f) => f.name === 'time') ||
|
|
116
|
+
numbers.find((f) => f !== y);
|
|
117
|
+
return {
|
|
118
|
+
...EMPTY_BUILDER,
|
|
119
|
+
// a category: its distribution; a series over steps or time: a line
|
|
120
|
+
mark: x?.type === 'string' ? 'boxplot' : x === counter || x?.name === 'time' ? 'line' : 'point',
|
|
121
|
+
x: x?.name || '',
|
|
122
|
+
y: y?.name || '',
|
|
123
|
+
color: multipleFiles ? '_name' : categories[1]?.name || '',
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/** The fields a spec reads (to fetch only those): every `field` of it */
|
|
128
|
+
export const specFields = (spec: unknown, out = new Set<string>()): Set<string> => {
|
|
129
|
+
if (Array.isArray(spec)) spec.forEach((item) => specFields(item, out));
|
|
130
|
+
else if (spec && typeof spec === 'object') {
|
|
131
|
+
for (const [key, value] of Object.entries(spec)) {
|
|
132
|
+
if (key === 'field' && typeof value === 'string') out.add(value);
|
|
133
|
+
else specFields(value, out);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export const isDarkPage = () =>
|
|
140
|
+
typeof document !== 'undefined' &&
|
|
141
|
+
(document.documentElement.classList.contains('dark') || document.documentElement.dataset.theme === 'dark');
|
|
142
|
+
|
|
143
|
+
/** A Vega-Lite spec rendered with vega-embed (loaded on first use), `rows` as the dataset "rows" */
|
|
144
|
+
export const VegaChart: React.FC<{ spec: Record<string, unknown>; rows: Record<string, unknown>[] }> = ({ spec, rows }) => {
|
|
145
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
146
|
+
const [error, setError] = useState<string | null>(null);
|
|
147
|
+
const { colorMode } = useColorMode();
|
|
148
|
+
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
let cancelled = false;
|
|
151
|
+
let finalize: (() => void) | undefined;
|
|
152
|
+
setError(null);
|
|
153
|
+
import('vega-embed')
|
|
154
|
+
.then(async ({ default: embed }) => {
|
|
155
|
+
if (cancelled || !ref.current) return;
|
|
156
|
+
const full = {
|
|
157
|
+
...spec,
|
|
158
|
+
datasets: { ...((spec.datasets as object) || {}), rows },
|
|
159
|
+
background: 'transparent',
|
|
160
|
+
};
|
|
161
|
+
const result = await embed(ref.current, full as never, {
|
|
162
|
+
actions: { export: true, source: false, compiled: false, editor: false },
|
|
163
|
+
theme: colorMode === 'dark' || isDarkPage() ? 'dark' : undefined,
|
|
164
|
+
// canvas: the CSS reset of the page (font: inherit) would restyle the SVG texts Vega measured
|
|
165
|
+
renderer: 'canvas',
|
|
166
|
+
});
|
|
167
|
+
if (cancelled) result.finalize();
|
|
168
|
+
else finalize = result.finalize;
|
|
169
|
+
})
|
|
170
|
+
.catch((err: unknown) => !cancelled && setError(err instanceof Error ? err.message : String(err)));
|
|
171
|
+
return () => {
|
|
172
|
+
cancelled = true;
|
|
173
|
+
finalize?.();
|
|
174
|
+
};
|
|
175
|
+
}, [spec, rows, colorMode]);
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<Box w="100%" minH="340px">
|
|
179
|
+
{error && (
|
|
180
|
+
<Alert.Root status="error" mb={2}>
|
|
181
|
+
<Alert.Indicator />
|
|
182
|
+
<Alert.Content>
|
|
183
|
+
<Alert.Title>Chart error</Alert.Title>
|
|
184
|
+
<Alert.Description fontFamily="mono" fontSize="xs">{error}</Alert.Description>
|
|
185
|
+
</Alert.Content>
|
|
186
|
+
</Alert.Root>
|
|
187
|
+
)}
|
|
188
|
+
<Box ref={ref} w="100%" />
|
|
189
|
+
</Box>
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/** The files that have rows of `event` */
|
|
194
|
+
export const filesWithEvent = (files: JobData['files'], event: string): JobData['files'] =>
|
|
195
|
+
files.filter((f) => f.events?.some((e) => e.event === event));
|
|
196
|
+
|
|
197
|
+
/** The fields of `event` in `files`, merged (counts added, ranges widened, values joined) */
|
|
198
|
+
export const mergeFields = (files: JobData['files'], event: string): JobDataField[] => {
|
|
199
|
+
const merged = new Map<string, JobDataField>();
|
|
200
|
+
files.forEach((f) =>
|
|
201
|
+
f.events?.filter((e) => e.event === event).forEach((e) =>
|
|
202
|
+
e.fields.forEach((field) => {
|
|
203
|
+
const prev = merged.get(field.name);
|
|
204
|
+
if (!prev) {
|
|
205
|
+
merged.set(field.name, { ...field, values: field.values ? [...field.values] : field.values });
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
prev.count += field.count;
|
|
209
|
+
if (prev.type !== field.type) prev.type = 'mixed';
|
|
210
|
+
if (field.min !== undefined) prev.min = Math.min(prev.min ?? field.min, field.min);
|
|
211
|
+
if (field.max !== undefined) prev.max = Math.max(prev.max ?? field.max, field.max);
|
|
212
|
+
if (prev.values && field.values) prev.values = [...new Set([...prev.values, ...field.values])].sort();
|
|
213
|
+
else if (field.values === null) prev.values = null;
|
|
214
|
+
}),
|
|
215
|
+
),
|
|
216
|
+
);
|
|
217
|
+
return [...merged.values()];
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/** The files a panel reads (`files`: comma separated paths or globs, all when empty) */
|
|
221
|
+
export const panelFiles = (files: JobData['files'], patterns?: string): JobData['files'] => {
|
|
222
|
+
const wanted = (patterns || '').split(',').map((p) => p.trim()).filter(Boolean);
|
|
223
|
+
if (!wanted.length) return files;
|
|
224
|
+
const glob = (p: string) => new RegExp(`^${p.replace(/[.+^${}()|\\]/g, '\\$&').replace(/\*/g, '[^/]*').replace(/\?/g, '.')}$`);
|
|
225
|
+
return files.filter((f) => wanted.some((p) => f.path === p || glob(p).test(f.path)));
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/** A chart panel of a layout from the builder (only what differs from the defaults) */
|
|
229
|
+
export const panelOfBuilder = (b: Builder, event: string, title?: string): OverviewPanel => ({
|
|
230
|
+
kind: 'chart',
|
|
231
|
+
title: title || event || 'records',
|
|
232
|
+
event,
|
|
233
|
+
mark: b.mark,
|
|
234
|
+
...(b.x ? { x: b.x } : {}),
|
|
235
|
+
...(b.y ? { y: b.y } : {}),
|
|
236
|
+
...(b.yAggregate ? { aggregate: b.yAggregate } : {}),
|
|
237
|
+
...(b.color ? { color: b.color } : {}),
|
|
238
|
+
...(b.column ? { column: b.column } : {}),
|
|
239
|
+
...(b.row ? { row: b.row } : {}),
|
|
240
|
+
...(b.logX ? { log_x: true } : {}),
|
|
241
|
+
...(b.logY ? { log_y: true } : {}),
|
|
242
|
+
...(b.zero ? { zero: true } : {}),
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
/** The builder of a chart panel; null when the panel gives no field (the page guesses them) */
|
|
246
|
+
export const builderOfPanel = (p: Extract<OverviewPanel, { kind: 'chart' }>): Builder | null => {
|
|
247
|
+
if (!p.x && !p.y && p.aggregate !== 'count') return null;
|
|
248
|
+
return {
|
|
249
|
+
...EMPTY_BUILDER,
|
|
250
|
+
mark: (MARKS as readonly string[]).includes(p.mark || '') ? (p.mark as Builder['mark']) : 'point',
|
|
251
|
+
x: p.x || '',
|
|
252
|
+
y: p.y || '',
|
|
253
|
+
yAggregate: (AGGREGATES as readonly string[]).includes(p.aggregate || '') ? (p.aggregate as Builder['yAggregate']) : '',
|
|
254
|
+
color: p.color || '',
|
|
255
|
+
column: p.column || '',
|
|
256
|
+
row: p.row || '',
|
|
257
|
+
logX: !!p.log_x,
|
|
258
|
+
logY: !!p.log_y,
|
|
259
|
+
zero: !!p.zero,
|
|
260
|
+
};
|
|
261
|
+
};
|