@toloka-platform/sdk 0.27.0 → 0.28.0
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/README.md +5 -3
- package/dist/cli/toloka.js +179 -190
- package/dist/cli/toloka.js.jsc +0 -0
- package/dist/cli/toloka.js.jsc.sha256 +1 -1
- package/dist/cli/toloka.js.sha256 +1 -1
- package/examples/video-pair-rating/nodes/adjudicate/node.ts +1 -1
- package/examples/video-pair-rating/nodes/adjudicate_op/node.ts +1 -1
- package/examples/video-pair-rating/nodes/overall_preference_pick/node.ts +5 -1
- package/examples/video-pair-rating/nodes/rate_pair/node.ts +5 -1
- package/openapi/gen/api.schemas.ts +51 -758
- package/openapi/gen/api.ts +0 -860
- package/package.json +2 -2
- package/skills/catalog.json +6 -40
- package/skills/toloka-build/references/human-work.md +15 -6
- package/skills/toloka-build/references/local-setup.md +2 -2
- package/skills/toloka-build/references/workflow-and-cli.md +3 -22
- package/skills/toloka-get-started/SKILL.md +9 -13
- package/src/cli/analytics.ts +0 -31
- package/src/cli/auth.ts +15 -7
- package/src/cli/config.ts +67 -3
- package/src/cli/context.ts +3 -124
- package/src/cli/credentials.ts +11 -10
- package/src/cli/init.ts +1 -1
- package/src/cli/program.ts +0 -6
- package/src/cli/project.ts +9 -1
- package/src/client.ts +3 -261
- package/src/importer.ts +12 -2
- package/src/index.ts +0 -2
- package/src/pipeline-graph-v1.contract.json +130 -128
- package/skills/toloka-fine-tuning/SKILL.md +0 -177
- package/skills/toloka-gisting/SKILL.md +0 -186
- package/src/cli/batch-inference.ts +0 -267
- package/src/cli/commands/batch-inference.ts +0 -116
- package/src/cli/commands/models.ts +0 -121
- package/src/cli/commands/training.ts +0 -275
- package/src/cli/models.ts +0 -304
- package/src/cli/trainings.ts +0 -880
package/src/cli/trainings.ts
DELETED
|
@@ -1,880 +0,0 @@
|
|
|
1
|
-
import { basename } from "node:path";
|
|
2
|
-
import { readFile, stat } from "node:fs/promises";
|
|
3
|
-
|
|
4
|
-
import { randomUUID } from "node:crypto";
|
|
5
|
-
import { omit } from "es-toolkit";
|
|
6
|
-
|
|
7
|
-
import type {
|
|
8
|
-
DatasetField,
|
|
9
|
-
DatasetSlice,
|
|
10
|
-
PotatoClient,
|
|
11
|
-
Training,
|
|
12
|
-
TrainingCostEstimate,
|
|
13
|
-
TrainingRun,
|
|
14
|
-
TrainingRunMetricHistory,
|
|
15
|
-
TrainingRunMetrics,
|
|
16
|
-
} from "../client";
|
|
17
|
-
import type { BaseModelId, TrainingConfigurationInput } from "../../openapi/gen/api.schemas";
|
|
18
|
-
import { assertCli, RETRY_OR_REPORT_HINT, TolokaCliError } from "./cli-error";
|
|
19
|
-
import type { CliErrorHint } from "./cli-error";
|
|
20
|
-
import { resolveLiveDataset } from "./data";
|
|
21
|
-
import { formatCount, waitForFilledFields, waitUntilDone } from "./slices";
|
|
22
|
-
import { DEFAULT_SPLIT_COLUMN, splitColumnId, waitForSplitColumn } from "./splits";
|
|
23
|
-
import { uploadedFieldHint } from "./uploaded-fields";
|
|
24
|
-
import { formatTable, type TableIo as TrainingIo, writeOutput } from "./table";
|
|
25
|
-
|
|
26
|
-
const ESTIMATE_FILE_HINT: CliErrorHint = {
|
|
27
|
-
description: "Regenerate it with toloka training cost <training> --json > <file>.",
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
type TrainingMethod = TrainingConfigurationInput["method"];
|
|
31
|
-
type TrainingSource = TrainingConfigurationInput["source"];
|
|
32
|
-
type SftConfigurationInput = Extract<TrainingConfigurationInput, { method: "sft" }>;
|
|
33
|
-
type GistingConfigurationInput = Extract<TrainingConfigurationInput, { method: "gisting" }>;
|
|
34
|
-
type ResolvedSelections = Pick<SftConfigurationInput, "sliceId" | "splitFieldId" | "trainParts">;
|
|
35
|
-
|
|
36
|
-
export type TrainingDraftOptions = {
|
|
37
|
-
method?: TrainingMethod;
|
|
38
|
-
baseModelId?: BaseModelId;
|
|
39
|
-
modelVersionId?: string;
|
|
40
|
-
sourceRoot?: string;
|
|
41
|
-
datasetName?: string;
|
|
42
|
-
sliceId?: string;
|
|
43
|
-
splitColumn?: string;
|
|
44
|
-
trainParts?: string[];
|
|
45
|
-
loraRank?: SftConfigurationInput["loraRank"];
|
|
46
|
-
gistTokens?: GistingConfigurationInput["gistTokens"];
|
|
47
|
-
epochs?: number;
|
|
48
|
-
learningRate?: number;
|
|
49
|
-
batchSize?: SftConfigurationInput["batchSize"];
|
|
50
|
-
maxSequenceLength?: number;
|
|
51
|
-
promptFilePath?: string;
|
|
52
|
-
fetch?: typeof globalThis.fetch;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
type TrainingApi = Pick<PotatoClient, "trainings">;
|
|
56
|
-
type TrainingDraftApi = PotatoClient;
|
|
57
|
-
type TrainingCostApi = Pick<PotatoClient, "trainings" | "waitFor">;
|
|
58
|
-
type TrainingRunApi = Pick<PotatoClient, "trainings" | "waitFor">;
|
|
59
|
-
const GISTING_PROMPT_MAX_BYTES = 256 * 1024;
|
|
60
|
-
const formatTraining = (training: Training): string =>
|
|
61
|
-
formatTable([
|
|
62
|
-
["ID", training.id],
|
|
63
|
-
["Project", training.projectId],
|
|
64
|
-
[
|
|
65
|
-
"Method",
|
|
66
|
-
training.configuration.method === "sft" ? "Supervised fine-tuning (SFT)" : "Prompt gisting",
|
|
67
|
-
],
|
|
68
|
-
["Slice", training.configuration.sliceId ?? "-"],
|
|
69
|
-
["Split column", training.configuration.splitFieldId ?? "-"],
|
|
70
|
-
["Train parts", training.configuration.trainParts?.join(", ") ?? "every train part"],
|
|
71
|
-
training.configuration.method === "sft"
|
|
72
|
-
? ["LoRA rank", String(training.configuration.loraRank)]
|
|
73
|
-
: ["Gist tokens", String(training.configuration.gistTokens)],
|
|
74
|
-
["Epochs", String(training.configuration.epochs)],
|
|
75
|
-
["Learning rate", String(training.configuration.learningRate)],
|
|
76
|
-
["Batch size", String(training.configuration.batchSize)],
|
|
77
|
-
["Max sequence length", String(training.configuration.maxSequenceLength)],
|
|
78
|
-
["Output Model", training.modelId ?? "-"],
|
|
79
|
-
["Archived at", training.archivedAt ?? "-"],
|
|
80
|
-
["Created at", training.createdAt],
|
|
81
|
-
["Updated at", training.updatedAt],
|
|
82
|
-
]);
|
|
83
|
-
|
|
84
|
-
const formatTrainingRun = (run: TrainingRun): string =>
|
|
85
|
-
formatTable([
|
|
86
|
-
["ID", run.id],
|
|
87
|
-
["Training", run.trainingId],
|
|
88
|
-
["Status", run.status],
|
|
89
|
-
["Blocked reason", run.blockedReason ?? "-"],
|
|
90
|
-
["Message", run.message ?? "-"],
|
|
91
|
-
["Error code", run.error?.code ?? "-"],
|
|
92
|
-
["Billing status", run.billingStatus],
|
|
93
|
-
[
|
|
94
|
-
"Charged amount",
|
|
95
|
-
run.chargedAmount === null ? "-" : `$${run.chargedAmount} ${run.quote.currency}`,
|
|
96
|
-
],
|
|
97
|
-
[
|
|
98
|
-
"Progress",
|
|
99
|
-
run.progress
|
|
100
|
-
? `${run.progress.phase}${run.progress.step === null ? "" : ` ${run.progress.step}${run.progress.total === null ? "" : `/${run.progress.total}`}`}`
|
|
101
|
-
: "-",
|
|
102
|
-
],
|
|
103
|
-
["Model Version name", run.outputModelVersionName],
|
|
104
|
-
[
|
|
105
|
-
"Model Version revision",
|
|
106
|
-
run.outputModelVersionRevision === null ? "-" : String(run.outputModelVersionRevision),
|
|
107
|
-
],
|
|
108
|
-
["Future Model Version", run.outputModelVersionId],
|
|
109
|
-
["Maximum charge", `$${run.quote.maximumCharge} ${run.quote.currency}`],
|
|
110
|
-
["Created at", run.createdAt],
|
|
111
|
-
]);
|
|
112
|
-
|
|
113
|
-
type ReadyEstimateFile = {
|
|
114
|
-
status: "ready";
|
|
115
|
-
trainingId: string;
|
|
116
|
-
inputSetId: string;
|
|
117
|
-
fingerprint: string;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const parseReadyEstimateFile = (value: unknown): ReadyEstimateFile => {
|
|
121
|
-
assertCli(
|
|
122
|
-
typeof value === "object" && value !== null && !Array.isArray(value),
|
|
123
|
-
"The estimate file must contain a JSON object.",
|
|
124
|
-
{ hint: ESTIMATE_FILE_HINT },
|
|
125
|
-
);
|
|
126
|
-
assertCli(
|
|
127
|
-
"status" in value && value.status === "ready",
|
|
128
|
-
"The estimate file is not a ready Training Cost Estimate.",
|
|
129
|
-
{ hint: ESTIMATE_FILE_HINT },
|
|
130
|
-
);
|
|
131
|
-
assertCli(
|
|
132
|
-
"trainingId" in value && typeof value.trainingId === "string",
|
|
133
|
-
"The estimate file has no Training ID.",
|
|
134
|
-
{ hint: ESTIMATE_FILE_HINT },
|
|
135
|
-
);
|
|
136
|
-
assertCli(
|
|
137
|
-
"inputSetId" in value && typeof value.inputSetId === "string",
|
|
138
|
-
"The estimate file has no Input Set ID.",
|
|
139
|
-
{ hint: ESTIMATE_FILE_HINT },
|
|
140
|
-
);
|
|
141
|
-
assertCli(
|
|
142
|
-
"fingerprint" in value &&
|
|
143
|
-
typeof value.fingerprint === "string" &&
|
|
144
|
-
/^[0-9a-f]{64}$/.test(value.fingerprint),
|
|
145
|
-
"The estimate file has no valid fingerprint.",
|
|
146
|
-
{ hint: ESTIMATE_FILE_HINT },
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
return {
|
|
150
|
-
status: "ready",
|
|
151
|
-
trainingId: value.trainingId,
|
|
152
|
-
inputSetId: value.inputSetId,
|
|
153
|
-
fingerprint: value.fingerprint,
|
|
154
|
-
};
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const formatCostEstimate = (estimate: TrainingCostEstimate): string => {
|
|
158
|
-
if (estimate.status === "blocked") {
|
|
159
|
-
return [
|
|
160
|
-
"Training cannot run with these inputs:",
|
|
161
|
-
...estimate.issues.map((item) => `- ${item.message}`),
|
|
162
|
-
].join("\n");
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const leftOutAsTest = estimate.inputs.training.heldOutRows ?? 0;
|
|
166
|
-
|
|
167
|
-
return formatTable([
|
|
168
|
-
["Training", estimate.trainingId],
|
|
169
|
-
["Training rows", String(estimate.inputs.training.report?.validCount ?? 0)],
|
|
170
|
-
...(leftOutAsTest === 0
|
|
171
|
-
? []
|
|
172
|
-
: [["Left out as test", `${formatCount(leftOutAsTest)} train rows that a split column now marks as test`]]),
|
|
173
|
-
["Training tokens", String(estimate.inputs.training.report?.tokens.total ?? 0)],
|
|
174
|
-
["Maximum GPU seconds", String(estimate.budget.maximumGpuSeconds)],
|
|
175
|
-
["Maximum charge", `$${estimate.maximumCharge} ${estimate.currency}`],
|
|
176
|
-
["Fingerprint", estimate.fingerprint],
|
|
177
|
-
]);
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
const resolveTraining = async (
|
|
181
|
-
client: TrainingApi,
|
|
182
|
-
projectId: string,
|
|
183
|
-
nameOrId: string,
|
|
184
|
-
): Promise<Training> => {
|
|
185
|
-
if (nameOrId.startsWith("trg.")) {
|
|
186
|
-
const training = await client.trainings.get({ trainingId: nameOrId });
|
|
187
|
-
assertCli(
|
|
188
|
-
training.projectId === projectId,
|
|
189
|
-
`Training ${nameOrId} was not found in project ${projectId}.`,
|
|
190
|
-
{ hint: { description: "Run toloka training list; a Training belongs to one project." } },
|
|
191
|
-
);
|
|
192
|
-
return training;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const matches: Training[] = [];
|
|
196
|
-
for await (const training of client.trainings.listAll({ projectId, limit: 100 })) {
|
|
197
|
-
if (training.name === nameOrId) matches.push(training);
|
|
198
|
-
}
|
|
199
|
-
assertCli(matches.length === 1, `Training ${nameOrId} was not found or is ambiguous.`, {
|
|
200
|
-
hint: { description: "Run toloka training list and pass the training id (trg.…)." },
|
|
201
|
-
});
|
|
202
|
-
return matches[0]!;
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const sourceFromOptions = (
|
|
206
|
-
options: Pick<TrainingDraftOptions, "baseModelId" | "modelVersionId">,
|
|
207
|
-
fallback?: TrainingSource,
|
|
208
|
-
): TrainingSource => {
|
|
209
|
-
assertCli(
|
|
210
|
-
!(options.baseModelId && options.modelVersionId),
|
|
211
|
-
"Choose exactly one of --base-model and --model-version.",
|
|
212
|
-
{ hint: { disabled: true } },
|
|
213
|
-
);
|
|
214
|
-
if (options.baseModelId) return { type: "base_model", baseModelId: options.baseModelId };
|
|
215
|
-
if (options.modelVersionId)
|
|
216
|
-
return { type: "model_version", modelVersionId: options.modelVersionId };
|
|
217
|
-
assertCli(fallback, "Choose exactly one of --base-model and --model-version.", {
|
|
218
|
-
hint: { disabled: true },
|
|
219
|
-
});
|
|
220
|
-
return fallback;
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
const uploadPromptFile = async (
|
|
224
|
-
client: Pick<PotatoClient, "files">,
|
|
225
|
-
projectId: string,
|
|
226
|
-
filePath: string,
|
|
227
|
-
fetchImplementation: typeof globalThis.fetch,
|
|
228
|
-
): Promise<string> => {
|
|
229
|
-
const metadata = await stat(filePath);
|
|
230
|
-
assertCli(metadata.isFile(), "--prompt-file must point to a file.", {
|
|
231
|
-
hint: { description: "Pass the absolute path of the prompt text file." },
|
|
232
|
-
});
|
|
233
|
-
assertCli(metadata.size > 0, "The gisting prompt file is empty.", {
|
|
234
|
-
hint: { description: "Put the full prompt text into the file before creating the Training." },
|
|
235
|
-
});
|
|
236
|
-
assertCli(
|
|
237
|
-
metadata.size <= GISTING_PROMPT_MAX_BYTES,
|
|
238
|
-
"The gisting prompt file exceeds the 256 KiB limit.",
|
|
239
|
-
{ hint: { description: "Shorten the prompt to under 256 KiB." } },
|
|
240
|
-
);
|
|
241
|
-
const payload = await readFile(filePath);
|
|
242
|
-
let prompt: string;
|
|
243
|
-
try {
|
|
244
|
-
prompt = new TextDecoder("utf-8", { fatal: true }).decode(payload);
|
|
245
|
-
} catch {
|
|
246
|
-
throw new TolokaCliError("The gisting prompt file must contain valid UTF-8 text.", {
|
|
247
|
-
hint: { description: "Save the prompt file as UTF-8 text." },
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
assertCli(prompt.trim().length > 0, "The gisting prompt file is empty.", {
|
|
251
|
-
hint: { description: "Put the full prompt text into the file before creating the Training." },
|
|
252
|
-
});
|
|
253
|
-
const started = await client.files.startUpload({
|
|
254
|
-
projectId,
|
|
255
|
-
filename: basename(filePath),
|
|
256
|
-
declaredSizeBytes: payload.byteLength,
|
|
257
|
-
});
|
|
258
|
-
try {
|
|
259
|
-
const response = await fetchImplementation(started.uploadUrl, {
|
|
260
|
-
method: "PUT",
|
|
261
|
-
headers: { "x-ms-blob-type": "BlockBlob", "Content-Type": "text/plain; charset=utf-8" },
|
|
262
|
-
body: payload,
|
|
263
|
-
});
|
|
264
|
-
assertCli(response.ok, `Failed to upload gisting prompt: HTTP ${response.status}.`, {
|
|
265
|
-
hint: RETRY_OR_REPORT_HINT,
|
|
266
|
-
});
|
|
267
|
-
return (await client.files.finalizeUpload({ projectId, fileId: started.file.id })).id;
|
|
268
|
-
} catch (error) {
|
|
269
|
-
await client.files.delete({ projectId, fileId: started.file.id }).catch(() => undefined);
|
|
270
|
-
throw error;
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
const ROWS_HINT: CliErrorHint = {
|
|
275
|
-
description:
|
|
276
|
-
"Pass --dataset <dataset> to split it and freeze a slice for this Training, or --slice <slice-id> for a slice that freezes a split column.",
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const AUTO_WAIT = { timeoutMs: 10 * 60_000 };
|
|
280
|
-
|
|
281
|
-
type ResolvedRows = ResolvedSelections & { readonly notes: readonly string[] };
|
|
282
|
-
|
|
283
|
-
const fillTrainingSplitColumn = async (
|
|
284
|
-
client: TrainingDraftApi,
|
|
285
|
-
{
|
|
286
|
-
datasetId,
|
|
287
|
-
datasetName,
|
|
288
|
-
datasetRef,
|
|
289
|
-
splitColumn,
|
|
290
|
-
}: { datasetId: string; datasetName: string; datasetRef: string; splitColumn: string | undefined },
|
|
291
|
-
): Promise<DatasetField> => {
|
|
292
|
-
const fields = await waitForFilledFields(client, { datasetId, waitFor: AUTO_WAIT });
|
|
293
|
-
const splitColumns = fields.filter((field) => field.split !== null);
|
|
294
|
-
assertCli(
|
|
295
|
-
splitColumn !== undefined || splitColumns.length <= 1,
|
|
296
|
-
`Dataset ${datasetName} has the split columns ${splitColumns.map((field) => field.name).join(", ")}.`,
|
|
297
|
-
{ hint: { description: "Pass --split-column <name> to choose one." } },
|
|
298
|
-
);
|
|
299
|
-
const existing =
|
|
300
|
-
splitColumn === undefined
|
|
301
|
-
? splitColumns[0]
|
|
302
|
-
: fields.find((field) => field.id === splitColumn || field.name === splitColumn);
|
|
303
|
-
|
|
304
|
-
if (existing?.split?.recipe === null) return existing;
|
|
305
|
-
|
|
306
|
-
const column = existing?.name ?? splitColumn ?? DEFAULT_SPLIT_COLUMN;
|
|
307
|
-
if (existing === undefined) {
|
|
308
|
-
const uploaded = await uploadedFieldHint(client, { datasetId, datasetRef, name: column });
|
|
309
|
-
assertCli(
|
|
310
|
-
uploaded === undefined,
|
|
311
|
-
`Dataset ${datasetName} has no field ${column}, but its uploaded rows have one.`,
|
|
312
|
-
{
|
|
313
|
-
hint: {
|
|
314
|
-
description: `Add it as a dataset field with toloka dataset fields add ${datasetRef} ${column} to train on its parts, or pass --split-column <name> to sample a new split column.`,
|
|
315
|
-
},
|
|
316
|
-
},
|
|
317
|
-
);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
return waitForSplitColumn(client, {
|
|
321
|
-
datasetId,
|
|
322
|
-
field: await client.datasets.split({ datasetId, column }),
|
|
323
|
-
waitFor: AUTO_WAIT,
|
|
324
|
-
});
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
const freezeTrainingSlice = async (
|
|
328
|
-
client: TrainingDraftApi,
|
|
329
|
-
{ datasetId, name }: { datasetId: string; name: string },
|
|
330
|
-
): Promise<DatasetSlice> => {
|
|
331
|
-
const created = await client.datasets.createSlice({ datasetId, name });
|
|
332
|
-
const slice = await waitUntilDone(() => client.waitFor({ type: "datasetSlice", sliceId: created.id }, AUTO_WAIT), {
|
|
333
|
-
what: `slice ${created.id}`,
|
|
334
|
-
checkCommand: `dataset slice show ${created.id}`,
|
|
335
|
-
});
|
|
336
|
-
assertCli(slice.status === "ready", `Slice ${slice.id} failed: ${slice.error?.message ?? "no reason given"}`, {
|
|
337
|
-
hint: { description: `Check it with toloka dataset slice show ${slice.id}.` },
|
|
338
|
-
});
|
|
339
|
-
return slice;
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
const splitAndSlice = async (
|
|
343
|
-
client: TrainingDraftApi,
|
|
344
|
-
{
|
|
345
|
-
sourceRoot,
|
|
346
|
-
projectId,
|
|
347
|
-
datasetName,
|
|
348
|
-
splitColumn,
|
|
349
|
-
trainingName,
|
|
350
|
-
}: { sourceRoot: string; projectId: string; datasetName: string; splitColumn: string | undefined; trainingName: string },
|
|
351
|
-
): Promise<ResolvedRows> => {
|
|
352
|
-
const dataset = await resolveLiveDataset(client, { sourceRoot, projectId, datasetName }, false);
|
|
353
|
-
const column = await fillTrainingSplitColumn(client, {
|
|
354
|
-
datasetId: dataset.datasetId,
|
|
355
|
-
datasetName: dataset.name,
|
|
356
|
-
datasetRef: dataset.nameIsUnique ? dataset.name : dataset.datasetId,
|
|
357
|
-
splitColumn,
|
|
358
|
-
});
|
|
359
|
-
const slice = await freezeTrainingSlice(client, { datasetId: dataset.datasetId, name: trainingName });
|
|
360
|
-
const lastFill = column.split?.lastFill;
|
|
361
|
-
|
|
362
|
-
return {
|
|
363
|
-
sliceId: slice.id,
|
|
364
|
-
splitFieldId: column.id,
|
|
365
|
-
notes: [
|
|
366
|
-
...(lastFill == null
|
|
367
|
-
? []
|
|
368
|
-
: [
|
|
369
|
-
`Split ${dataset.name} by column ${column.name}: placed ${formatCount(lastFill.placed)} rows, ${formatCount(lastFill.kept)} kept their part.`,
|
|
370
|
-
]),
|
|
371
|
-
`Froze slice ${slice.name} (${slice.id}) with ${formatCount(slice.rows)} rows.`,
|
|
372
|
-
],
|
|
373
|
-
};
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
const resolveSelections = async ({
|
|
377
|
-
client,
|
|
378
|
-
projectId,
|
|
379
|
-
trainingName,
|
|
380
|
-
current,
|
|
381
|
-
options,
|
|
382
|
-
}: {
|
|
383
|
-
client: TrainingDraftApi;
|
|
384
|
-
projectId: string;
|
|
385
|
-
trainingName: string;
|
|
386
|
-
current: Training["configuration"] | undefined;
|
|
387
|
-
options: TrainingDraftOptions;
|
|
388
|
-
}): Promise<ResolvedRows> => {
|
|
389
|
-
assertCli(options.datasetName === undefined || options.sliceId === undefined, "Pass --dataset or --slice, not both.", {
|
|
390
|
-
hint: ROWS_HINT,
|
|
391
|
-
});
|
|
392
|
-
if (options.datasetName !== undefined) {
|
|
393
|
-
assertCli(options.sourceRoot !== undefined, "The CLI needs the project source root to resolve --dataset.", {
|
|
394
|
-
hint: RETRY_OR_REPORT_HINT,
|
|
395
|
-
});
|
|
396
|
-
return {
|
|
397
|
-
...(await splitAndSlice(client, {
|
|
398
|
-
sourceRoot: options.sourceRoot,
|
|
399
|
-
projectId,
|
|
400
|
-
datasetName: options.datasetName,
|
|
401
|
-
splitColumn: options.splitColumn,
|
|
402
|
-
trainingName,
|
|
403
|
-
})),
|
|
404
|
-
...(options.trainParts === undefined ? {} : { trainParts: options.trainParts }),
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
const inherited = options.sliceId === undefined ? current : undefined;
|
|
409
|
-
const sliceId = options.sliceId ?? inherited?.sliceId;
|
|
410
|
-
assertCli(sliceId !== undefined, "--dataset or --slice is required.", { hint: ROWS_HINT });
|
|
411
|
-
const slice = await client.datasets.getSlice({ sliceId });
|
|
412
|
-
assertCli(slice.status === "ready", `Slice ${slice.id} is ${slice.status}, not ready.`, {
|
|
413
|
-
hint: { description: `Check it with toloka dataset slice show ${slice.id}.` },
|
|
414
|
-
});
|
|
415
|
-
const splitFieldId =
|
|
416
|
-
options.splitColumn === undefined
|
|
417
|
-
? inherited?.splitFieldId
|
|
418
|
-
: await splitColumnId(client, slice.datasetId, options.splitColumn);
|
|
419
|
-
const trainParts = options.trainParts ?? inherited?.trainParts;
|
|
420
|
-
|
|
421
|
-
return {
|
|
422
|
-
sliceId: slice.id,
|
|
423
|
-
...(splitFieldId === undefined ? {} : { splitFieldId }),
|
|
424
|
-
...(trainParts === undefined ? {} : { trainParts }),
|
|
425
|
-
notes: [],
|
|
426
|
-
};
|
|
427
|
-
};
|
|
428
|
-
|
|
429
|
-
const sharedHyperparameters = (options: TrainingDraftOptions) => ({
|
|
430
|
-
...(options.learningRate === undefined ? {} : { learningRate: options.learningRate }),
|
|
431
|
-
...(options.batchSize === undefined ? {} : { batchSize: options.batchSize }),
|
|
432
|
-
...(options.maxSequenceLength === undefined
|
|
433
|
-
? {}
|
|
434
|
-
: { maxSequenceLength: options.maxSequenceLength }),
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
const assertMethodOptions = (method: TrainingMethod, options: TrainingDraftOptions): void => {
|
|
438
|
-
if (method === "gisting") {
|
|
439
|
-
assertCli(
|
|
440
|
-
options.loraRank === undefined,
|
|
441
|
-
"--lora-rank applies to SFT; gisting takes --gist-tokens.",
|
|
442
|
-
{
|
|
443
|
-
hint: {
|
|
444
|
-
description: "Use --gist-tokens for gisting, or omit --method gisting to create SFT.",
|
|
445
|
-
},
|
|
446
|
-
},
|
|
447
|
-
);
|
|
448
|
-
return;
|
|
449
|
-
}
|
|
450
|
-
assertCli(options.promptFilePath === undefined, "--prompt-file applies to gisting only.", {
|
|
451
|
-
hint: {
|
|
452
|
-
description: "Pass --method gisting to use --prompt-file, or omit --prompt-file for SFT.",
|
|
453
|
-
},
|
|
454
|
-
});
|
|
455
|
-
assertCli(
|
|
456
|
-
options.gistTokens === undefined,
|
|
457
|
-
"--gist-tokens applies to gisting; SFT takes --lora-rank.",
|
|
458
|
-
{
|
|
459
|
-
hint: {
|
|
460
|
-
description: "Use --lora-rank for SFT, or pass --method gisting to use --gist-tokens.",
|
|
461
|
-
},
|
|
462
|
-
},
|
|
463
|
-
);
|
|
464
|
-
};
|
|
465
|
-
|
|
466
|
-
const SELECTION_KEYS = ["training", "validation", "sliceId", "splitFieldId", "trainParts"] as const;
|
|
467
|
-
|
|
468
|
-
const buildSftConfiguration = ({
|
|
469
|
-
source,
|
|
470
|
-
selections,
|
|
471
|
-
current,
|
|
472
|
-
options,
|
|
473
|
-
}: {
|
|
474
|
-
source: TrainingSource;
|
|
475
|
-
selections: ResolvedSelections;
|
|
476
|
-
current: Extract<Training["configuration"], { method: "sft" }> | undefined;
|
|
477
|
-
options: TrainingDraftOptions;
|
|
478
|
-
}): SftConfigurationInput => {
|
|
479
|
-
return {
|
|
480
|
-
...(current === undefined ? {} : omit(current, SELECTION_KEYS)),
|
|
481
|
-
method: "sft",
|
|
482
|
-
source,
|
|
483
|
-
...selections,
|
|
484
|
-
...(options.loraRank === undefined ? {} : { loraRank: options.loraRank }),
|
|
485
|
-
...(options.epochs === undefined ? {} : { epochs: options.epochs }),
|
|
486
|
-
...sharedHyperparameters(options),
|
|
487
|
-
};
|
|
488
|
-
};
|
|
489
|
-
|
|
490
|
-
const buildGistingConfiguration = async ({
|
|
491
|
-
client,
|
|
492
|
-
projectId,
|
|
493
|
-
source,
|
|
494
|
-
selections,
|
|
495
|
-
current,
|
|
496
|
-
options,
|
|
497
|
-
}: {
|
|
498
|
-
client: TrainingDraftApi;
|
|
499
|
-
projectId: string;
|
|
500
|
-
source: TrainingSource;
|
|
501
|
-
selections: ResolvedSelections;
|
|
502
|
-
current: Extract<Training["configuration"], { method: "gisting" }> | undefined;
|
|
503
|
-
options: TrainingDraftOptions;
|
|
504
|
-
}): Promise<GistingConfigurationInput> => {
|
|
505
|
-
const promptFileId = options.promptFilePath
|
|
506
|
-
? await uploadPromptFile(
|
|
507
|
-
client,
|
|
508
|
-
projectId,
|
|
509
|
-
options.promptFilePath,
|
|
510
|
-
options.fetch ?? globalThis.fetch,
|
|
511
|
-
)
|
|
512
|
-
: current?.promptFileId;
|
|
513
|
-
assertCli(promptFileId, "--prompt-file is required for gisting.", {
|
|
514
|
-
hint: { description: "Pass --prompt-file <absolute-path> with the prompt text." },
|
|
515
|
-
});
|
|
516
|
-
return {
|
|
517
|
-
...(current === undefined ? {} : omit(current, SELECTION_KEYS)),
|
|
518
|
-
method: "gisting",
|
|
519
|
-
source,
|
|
520
|
-
...selections,
|
|
521
|
-
promptFileId,
|
|
522
|
-
...(options.gistTokens === undefined ? {} : { gistTokens: options.gistTokens }),
|
|
523
|
-
...(options.epochs === undefined ? {} : { epochs: options.epochs }),
|
|
524
|
-
...sharedHyperparameters(options),
|
|
525
|
-
};
|
|
526
|
-
};
|
|
527
|
-
|
|
528
|
-
const buildTrainingConfiguration = async ({
|
|
529
|
-
client,
|
|
530
|
-
projectId,
|
|
531
|
-
trainingName,
|
|
532
|
-
current,
|
|
533
|
-
options,
|
|
534
|
-
}: {
|
|
535
|
-
client: TrainingDraftApi;
|
|
536
|
-
projectId: string;
|
|
537
|
-
trainingName: string;
|
|
538
|
-
current?: Training["configuration"];
|
|
539
|
-
options: TrainingDraftOptions;
|
|
540
|
-
}): Promise<{ configuration: TrainingConfigurationInput; notes: readonly string[] }> => {
|
|
541
|
-
const source = sourceFromOptions(options, current?.source);
|
|
542
|
-
const method = options.method ?? current?.method ?? "sft";
|
|
543
|
-
assertMethodOptions(method, options);
|
|
544
|
-
const { notes, ...selections } = await resolveSelections({ client, projectId, trainingName, current, options });
|
|
545
|
-
const configuration = await (method === "sft"
|
|
546
|
-
? buildSftConfiguration({
|
|
547
|
-
source,
|
|
548
|
-
selections,
|
|
549
|
-
current: current?.method === "sft" ? current : undefined,
|
|
550
|
-
options,
|
|
551
|
-
})
|
|
552
|
-
: buildGistingConfiguration({
|
|
553
|
-
client,
|
|
554
|
-
projectId,
|
|
555
|
-
source,
|
|
556
|
-
selections,
|
|
557
|
-
current: current?.method === "gisting" ? current : undefined,
|
|
558
|
-
options,
|
|
559
|
-
}));
|
|
560
|
-
return { configuration, notes };
|
|
561
|
-
};
|
|
562
|
-
|
|
563
|
-
export const listTrainings = async (
|
|
564
|
-
client: TrainingApi,
|
|
565
|
-
input: { projectId: string; json: boolean },
|
|
566
|
-
io: TrainingIo,
|
|
567
|
-
): Promise<void> => {
|
|
568
|
-
const page = await client.trainings.listPage({
|
|
569
|
-
projectId: input.projectId,
|
|
570
|
-
limit: 100,
|
|
571
|
-
offset: 0,
|
|
572
|
-
});
|
|
573
|
-
writeOutput(
|
|
574
|
-
io,
|
|
575
|
-
input.json,
|
|
576
|
-
page.trainings,
|
|
577
|
-
page.trainings.length === 0
|
|
578
|
-
? "No Trainings found."
|
|
579
|
-
: formatTable([
|
|
580
|
-
["ID", "NAME", "METHOD", "OUTPUT MODEL"],
|
|
581
|
-
...page.trainings.map((training) => [
|
|
582
|
-
training.id,
|
|
583
|
-
training.name,
|
|
584
|
-
training.configuration.method === "sft" ? "SFT" : "GISTING",
|
|
585
|
-
training.modelId ?? "-",
|
|
586
|
-
]),
|
|
587
|
-
]),
|
|
588
|
-
);
|
|
589
|
-
};
|
|
590
|
-
|
|
591
|
-
export const getTraining = async (
|
|
592
|
-
client: TrainingApi,
|
|
593
|
-
input: { projectId: string; nameOrId: string; json: boolean },
|
|
594
|
-
io: TrainingIo,
|
|
595
|
-
): Promise<void> => {
|
|
596
|
-
const training = await resolveTraining(client, input.projectId, input.nameOrId);
|
|
597
|
-
writeOutput(io, input.json, training, formatTraining(training));
|
|
598
|
-
};
|
|
599
|
-
|
|
600
|
-
export const createTraining = async (
|
|
601
|
-
client: TrainingDraftApi,
|
|
602
|
-
input: TrainingDraftOptions & {
|
|
603
|
-
projectId: string;
|
|
604
|
-
name: string;
|
|
605
|
-
json: boolean;
|
|
606
|
-
},
|
|
607
|
-
io: TrainingIo,
|
|
608
|
-
): Promise<void> => {
|
|
609
|
-
const { configuration, notes } = await buildTrainingConfiguration({
|
|
610
|
-
client,
|
|
611
|
-
projectId: input.projectId,
|
|
612
|
-
trainingName: input.name,
|
|
613
|
-
options: input,
|
|
614
|
-
});
|
|
615
|
-
const training = await client.trainings.create({
|
|
616
|
-
projectId: input.projectId,
|
|
617
|
-
name: input.name,
|
|
618
|
-
configuration,
|
|
619
|
-
});
|
|
620
|
-
writeOutput(io, input.json, training, [...notes, `Created training ${training.id}`].join("\n"));
|
|
621
|
-
};
|
|
622
|
-
|
|
623
|
-
export const updateTraining = async (
|
|
624
|
-
client: TrainingDraftApi,
|
|
625
|
-
input: TrainingDraftOptions & {
|
|
626
|
-
projectId: string;
|
|
627
|
-
nameOrId: string;
|
|
628
|
-
json: boolean;
|
|
629
|
-
},
|
|
630
|
-
io: TrainingIo,
|
|
631
|
-
): Promise<void> => {
|
|
632
|
-
const current = await resolveTraining(client, input.projectId, input.nameOrId);
|
|
633
|
-
const { configuration, notes } = await buildTrainingConfiguration({
|
|
634
|
-
client,
|
|
635
|
-
projectId: input.projectId,
|
|
636
|
-
trainingName: current.name,
|
|
637
|
-
current: current.configuration,
|
|
638
|
-
options: input,
|
|
639
|
-
});
|
|
640
|
-
const updated = await client.trainings.replaceConfiguration({ trainingId: current.id, configuration });
|
|
641
|
-
writeOutput(io, input.json, updated, [...notes, `Updated training ${updated.id}`].join("\n"));
|
|
642
|
-
};
|
|
643
|
-
|
|
644
|
-
const prepareTrainingCostEstimate = async (
|
|
645
|
-
client: TrainingCostApi,
|
|
646
|
-
input: { nameOrId: string; projectId: string; idempotencyKey?: string },
|
|
647
|
-
): Promise<{ training: Training; estimate: TrainingCostEstimate }> => {
|
|
648
|
-
const training = await resolveTraining(client, input.projectId, input.nameOrId);
|
|
649
|
-
const inputSet = await client.trainings.prepareCostEstimateInputs({
|
|
650
|
-
trainingId: training.id,
|
|
651
|
-
idempotencyKey: input.idempotencyKey ?? randomUUID(),
|
|
652
|
-
});
|
|
653
|
-
const exportIds = [inputSet.exports.training, inputSet.exports.validation].filter(
|
|
654
|
-
(exportId): exportId is string => exportId !== null,
|
|
655
|
-
);
|
|
656
|
-
|
|
657
|
-
await Promise.all(
|
|
658
|
-
exportIds.map((exportId) => client.waitFor({ type: "datasetExport", exportId })),
|
|
659
|
-
);
|
|
660
|
-
|
|
661
|
-
return {
|
|
662
|
-
training,
|
|
663
|
-
estimate: await client.trainings.estimateCost({
|
|
664
|
-
trainingId: training.id,
|
|
665
|
-
inputSetId: inputSet.id,
|
|
666
|
-
}),
|
|
667
|
-
};
|
|
668
|
-
};
|
|
669
|
-
|
|
670
|
-
export const estimateTrainingCost = async (
|
|
671
|
-
client: TrainingCostApi,
|
|
672
|
-
input: { projectId: string; nameOrId: string; idempotencyKey?: string; json: boolean },
|
|
673
|
-
io: TrainingIo,
|
|
674
|
-
): Promise<void> => {
|
|
675
|
-
const { estimate } = await prepareTrainingCostEstimate(client, input);
|
|
676
|
-
assertCli(estimate.status === "ready", formatCostEstimate(estimate), {
|
|
677
|
-
hint: { description: "Fix the blocked issues above, then run toloka training cost again." },
|
|
678
|
-
});
|
|
679
|
-
writeOutput(io, input.json, estimate, formatCostEstimate(estimate));
|
|
680
|
-
};
|
|
681
|
-
|
|
682
|
-
export const createTrainingRun = async (
|
|
683
|
-
client: TrainingRunApi,
|
|
684
|
-
input: {
|
|
685
|
-
projectId: string;
|
|
686
|
-
nameOrId: string;
|
|
687
|
-
estimateFile?: string;
|
|
688
|
-
versionName?: string;
|
|
689
|
-
idempotencyKey?: string;
|
|
690
|
-
json: boolean;
|
|
691
|
-
},
|
|
692
|
-
io: TrainingIo,
|
|
693
|
-
): Promise<void> => {
|
|
694
|
-
const training = await resolveTraining(client, input.projectId, input.nameOrId);
|
|
695
|
-
const estimate = input.estimateFile
|
|
696
|
-
? parseReadyEstimateFile(JSON.parse(await readFile(input.estimateFile, "utf8")))
|
|
697
|
-
: await (async () => {
|
|
698
|
-
const prepared = await prepareTrainingCostEstimate(client, {
|
|
699
|
-
projectId: input.projectId,
|
|
700
|
-
nameOrId: input.nameOrId,
|
|
701
|
-
idempotencyKey: input.idempotencyKey,
|
|
702
|
-
});
|
|
703
|
-
assertCli(prepared.estimate.status === "ready", formatCostEstimate(prepared.estimate), {
|
|
704
|
-
hint: {
|
|
705
|
-
description: "Fix the blocked issues above, then run toloka training cost again.",
|
|
706
|
-
},
|
|
707
|
-
});
|
|
708
|
-
return parseReadyEstimateFile(prepared.estimate);
|
|
709
|
-
})();
|
|
710
|
-
assertCli(estimate.trainingId === training.id, "The estimate file belongs to another Training.", {
|
|
711
|
-
hint: {
|
|
712
|
-
description:
|
|
713
|
-
"Run toloka training cost <training> --json > <file> for this Training, get its maximumCharge approved, then re-run with that file.",
|
|
714
|
-
},
|
|
715
|
-
});
|
|
716
|
-
const run = await client.trainings.createRun({
|
|
717
|
-
trainingId: training.id,
|
|
718
|
-
inputSetId: estimate.inputSetId,
|
|
719
|
-
estimateFingerprint: estimate.fingerprint,
|
|
720
|
-
versionName: input.versionName ?? training.name,
|
|
721
|
-
idempotencyKey: input.idempotencyKey ?? randomUUID(),
|
|
722
|
-
});
|
|
723
|
-
writeOutput(io, input.json, run, formatTrainingRun(run));
|
|
724
|
-
};
|
|
725
|
-
|
|
726
|
-
export const listTrainingRuns = async (
|
|
727
|
-
client: TrainingApi,
|
|
728
|
-
input: { projectId: string; nameOrId: string; json: boolean },
|
|
729
|
-
io: TrainingIo,
|
|
730
|
-
): Promise<void> => {
|
|
731
|
-
const training = await resolveTraining(client, input.projectId, input.nameOrId);
|
|
732
|
-
const page = await client.trainings.listRunsPage({
|
|
733
|
-
trainingId: training.id,
|
|
734
|
-
limit: 100,
|
|
735
|
-
offset: 0,
|
|
736
|
-
});
|
|
737
|
-
writeOutput(
|
|
738
|
-
io,
|
|
739
|
-
input.json,
|
|
740
|
-
page.trainingRuns,
|
|
741
|
-
page.trainingRuns.length === 0
|
|
742
|
-
? "No Training Runs found."
|
|
743
|
-
: formatTable([
|
|
744
|
-
["ID", "STATUS", "VERSION", "MAXIMUM CHARGE", "OUTPUT MODEL VERSION"],
|
|
745
|
-
...page.trainingRuns.map((run) => [
|
|
746
|
-
run.id,
|
|
747
|
-
run.status,
|
|
748
|
-
run.outputModelVersionName,
|
|
749
|
-
`$${run.quote.maximumCharge}`,
|
|
750
|
-
run.outputModelVersionId,
|
|
751
|
-
]),
|
|
752
|
-
]),
|
|
753
|
-
);
|
|
754
|
-
};
|
|
755
|
-
|
|
756
|
-
export const getTrainingRun = async (
|
|
757
|
-
client: TrainingApi,
|
|
758
|
-
input: { trainingRunId: string; json: boolean },
|
|
759
|
-
io: TrainingIo,
|
|
760
|
-
): Promise<void> => {
|
|
761
|
-
const run = await client.trainings.getRun({ trainingRunId: input.trainingRunId });
|
|
762
|
-
writeOutput(io, input.json, run, formatTrainingRun(run));
|
|
763
|
-
};
|
|
764
|
-
|
|
765
|
-
export const resumeTrainingRun = async (
|
|
766
|
-
client: TrainingApi,
|
|
767
|
-
input: { trainingRunId: string; json: boolean },
|
|
768
|
-
io: TrainingIo,
|
|
769
|
-
): Promise<void> => {
|
|
770
|
-
const run = await client.trainings.resumeRun({ trainingRunId: input.trainingRunId });
|
|
771
|
-
writeOutput(io, input.json, run, formatTrainingRun(run));
|
|
772
|
-
};
|
|
773
|
-
|
|
774
|
-
export const cancelTrainingRun = async (
|
|
775
|
-
client: TrainingApi,
|
|
776
|
-
input: { trainingRunId: string; json: boolean },
|
|
777
|
-
io: TrainingIo,
|
|
778
|
-
): Promise<void> => {
|
|
779
|
-
const run = await client.trainings.cancelRun({ trainingRunId: input.trainingRunId });
|
|
780
|
-
writeOutput(io, input.json, run, formatTrainingRun(run));
|
|
781
|
-
};
|
|
782
|
-
|
|
783
|
-
export const readTrainingRunLogs = async (
|
|
784
|
-
client: TrainingApi,
|
|
785
|
-
input: { trainingRunId: string; follow: boolean; pollIntervalMs?: number },
|
|
786
|
-
io: TrainingIo,
|
|
787
|
-
): Promise<void> => {
|
|
788
|
-
let cursor: string | null = null;
|
|
789
|
-
for (;;) {
|
|
790
|
-
const page = await client.trainings.getRunLogs({
|
|
791
|
-
trainingRunId: input.trainingRunId,
|
|
792
|
-
cursor: cursor ?? undefined,
|
|
793
|
-
limit: 100,
|
|
794
|
-
});
|
|
795
|
-
for (const entry of page.entries) {
|
|
796
|
-
io.stdout(`${entry.timestamp} ${entry.level.toUpperCase()} ${entry.message}`);
|
|
797
|
-
}
|
|
798
|
-
if (!input.follow || page.complete) {
|
|
799
|
-
if (page.truncated) io.stdout("Training logs were truncated by the server.");
|
|
800
|
-
return;
|
|
801
|
-
}
|
|
802
|
-
cursor = page.nextCursor;
|
|
803
|
-
await new Promise<void>((resolve) => {
|
|
804
|
-
setTimeout(resolve, input.pollIntervalMs ?? 1_000);
|
|
805
|
-
});
|
|
806
|
-
}
|
|
807
|
-
};
|
|
808
|
-
|
|
809
|
-
const formatMetricValue = (point: {
|
|
810
|
-
value: number | null;
|
|
811
|
-
nonFinite: "nan" | "positive_infinity" | "negative_infinity" | null;
|
|
812
|
-
}): string => {
|
|
813
|
-
if (point.nonFinite === "nan") return "NaN";
|
|
814
|
-
if (point.nonFinite === "positive_infinity") return "Infinity";
|
|
815
|
-
if (point.nonFinite === "negative_infinity") return "-Infinity";
|
|
816
|
-
return point.value === null ? "-" : String(point.value);
|
|
817
|
-
};
|
|
818
|
-
|
|
819
|
-
const METRICS_UNAVAILABLE: Record<"not_configured" | "unresolved", string> = {
|
|
820
|
-
not_configured:
|
|
821
|
-
"This Training Run has no metrics: it started before metrics were available, or the server records none.",
|
|
822
|
-
unresolved:
|
|
823
|
-
"Metrics are temporarily unreachable. The Training Run itself is unaffected; try again later.",
|
|
824
|
-
};
|
|
825
|
-
|
|
826
|
-
const formatTrainingRunMetrics = (metrics: TrainingRunMetrics): string => {
|
|
827
|
-
if (!metrics.available) {
|
|
828
|
-
return metrics.unavailableReason === null
|
|
829
|
-
? "No metrics are available for this Training Run."
|
|
830
|
-
: METRICS_UNAVAILABLE[metrics.unavailableReason];
|
|
831
|
-
}
|
|
832
|
-
if (metrics.metrics.length === 0) return "No metrics have been reported yet.";
|
|
833
|
-
|
|
834
|
-
const rows = metrics.metrics.map((entry) => [
|
|
835
|
-
entry.key,
|
|
836
|
-
entry.metric ?? "-",
|
|
837
|
-
formatMetricValue(entry.latest),
|
|
838
|
-
String(entry.latest.step),
|
|
839
|
-
]);
|
|
840
|
-
const table = formatTable([["Metric", "Meaning", "Latest", "Step"], ...rows]);
|
|
841
|
-
return metrics.truncated ? `${table}\n\nMore metrics exist than the server returns.` : table;
|
|
842
|
-
};
|
|
843
|
-
|
|
844
|
-
const formatTrainingRunMetricHistory = (history: TrainingRunMetricHistory): string => {
|
|
845
|
-
const header = formatTable([
|
|
846
|
-
["Metric", history.key],
|
|
847
|
-
["Meaning", history.metric ?? "-"],
|
|
848
|
-
["Points", String(history.points.length)],
|
|
849
|
-
]);
|
|
850
|
-
const points = formatTable([
|
|
851
|
-
["Step", "Value"],
|
|
852
|
-
...history.points.map((point) => [String(point.step), formatMetricValue(point)]),
|
|
853
|
-
]);
|
|
854
|
-
const note = history.truncated
|
|
855
|
-
? "\n\nNot every recorded point is shown: the series is thinned to fit --resolution, and a very long run keeps only its most recent points."
|
|
856
|
-
: "";
|
|
857
|
-
return `${header}\n\n${points}${note}`;
|
|
858
|
-
};
|
|
859
|
-
|
|
860
|
-
export const readTrainingRunMetrics = async (
|
|
861
|
-
client: TrainingApi,
|
|
862
|
-
input: { trainingRunId: string; json: boolean },
|
|
863
|
-
io: TrainingIo,
|
|
864
|
-
): Promise<void> => {
|
|
865
|
-
const metrics = await client.trainings.getRunMetrics({ trainingRunId: input.trainingRunId });
|
|
866
|
-
writeOutput(io, input.json, metrics, formatTrainingRunMetrics(metrics));
|
|
867
|
-
};
|
|
868
|
-
|
|
869
|
-
export const readTrainingRunMetricHistory = async (
|
|
870
|
-
client: TrainingApi,
|
|
871
|
-
input: { trainingRunId: string; metricKey: string; resolution?: number; json: boolean },
|
|
872
|
-
io: TrainingIo,
|
|
873
|
-
): Promise<void> => {
|
|
874
|
-
const history = await client.trainings.getRunMetricHistory({
|
|
875
|
-
trainingRunId: input.trainingRunId,
|
|
876
|
-
metricKey: input.metricKey,
|
|
877
|
-
resolution: input.resolution,
|
|
878
|
-
});
|
|
879
|
-
writeOutput(io, input.json, history, formatTrainingRunMetricHistory(history));
|
|
880
|
-
};
|