-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfinetune.py
More file actions
495 lines (441 loc) · 15.9 KB
/
finetune.py
File metadata and controls
495 lines (441 loc) · 15.9 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
from __future__ import annotations
import json
from datetime import datetime, timezone
from textwrap import wrap
from typing import Any, Literal
import re
import click
from click.core import ParameterSource # type: ignore[attr-defined]
from rich import print as rprint
from tabulate import tabulate
from together import Together
from together.cli.api.utils import BOOL_WITH_AUTO, INT_WITH_MAX
from together.utils import (
finetune_price_to_dollars,
log_warn,
log_warn_once,
parse_timestamp,
format_timestamp,
)
from together.types.finetune import (
DownloadCheckpointType,
FinetuneTrainingLimits,
FinetuneEventType,
)
_CONFIRMATION_MESSAGE = (
"You are about to create a fine-tuning job. "
"The cost of your job will be determined by the model size, the number of tokens "
"in the training file, the number of tokens in the validation file, the number of epochs, and "
"the number of evaluations. Visit https://www.together.ai/pricing to get a price estimate.\n"
"You can pass `-y` or `--confirm` to your command to skip this message.\n\n"
"Do you want to proceed?"
)
class DownloadCheckpointTypeChoice(click.Choice):
def __init__(self) -> None:
super().__init__([ct.value for ct in DownloadCheckpointType])
def convert(
self, value: str, param: click.Parameter | None, ctx: click.Context | None
) -> DownloadCheckpointType:
value = super().convert(value, param, ctx)
return DownloadCheckpointType(value)
@click.group(name="fine-tuning")
@click.pass_context
def fine_tuning(ctx: click.Context) -> None:
"""Fine-tunes API commands"""
pass
@fine_tuning.command()
@click.pass_context
@click.option(
"--training-file",
"-t",
type=str,
required=True,
help="Training file ID from Files API",
)
@click.option("--model", "-m", type=str, help="Base model name")
@click.option(
"--n-epochs", "-ne", type=int, default=1, help="Number of epochs to train for"
)
@click.option(
"--validation-file", type=str, default="", help="Validation file ID from Files API"
)
@click.option("--n-evals", type=int, default=0, help="Number of evaluation loops")
@click.option(
"--n-checkpoints", "-c", type=int, default=1, help="Number of checkpoints to save"
)
@click.option(
"--batch-size", "-b", type=INT_WITH_MAX, default="max", help="Train batch size"
)
@click.option("--learning-rate", "-lr", type=float, default=1e-5, help="Learning rate")
@click.option(
"--lr-scheduler-type",
type=click.Choice(["linear", "cosine"]),
default="linear",
help="Learning rate scheduler type",
)
@click.option(
"--min-lr-ratio",
type=float,
default=0.0,
help="The ratio of the final learning rate to the peak learning rate",
)
@click.option(
"--scheduler-num-cycles",
type=float,
default=0.5,
help="Number or fraction of cycles for the cosine learning rate scheduler.",
)
@click.option(
"--warmup-ratio",
type=float,
default=0.0,
help="Warmup ratio for the learning rate scheduler.",
)
@click.option(
"--max-grad-norm",
type=float,
default=1.0,
help="Max gradient norm to be used for gradient clipping. Set to 0 to disable.",
)
@click.option(
"--weight-decay",
type=float,
default=0.0,
help="Weight decay",
)
@click.option(
"--lora/--no-lora",
type=bool,
default=True,
help="Whether to use LoRA adapters for fine-tuning",
)
@click.option("--lora-r", type=int, default=8, help="LoRA adapters' rank")
@click.option("--lora-dropout", type=float, default=0, help="LoRA adapters' dropout")
@click.option("--lora-alpha", type=float, default=8, help="LoRA adapters' alpha")
@click.option(
"--lora-trainable-modules",
type=str,
default="all-linear",
help="Trainable modules for LoRA adapters. For example, 'all-linear', 'q_proj,v_proj'",
)
@click.option(
"--training-method",
type=click.Choice(["sft", "dpo"]),
default="sft",
help="Training method to use. Options: sft (supervised fine-tuning), dpo (Direct Preference Optimization)",
)
@click.option(
"--dpo-beta",
type=float,
default=0.1,
help="Beta parameter for DPO training (only used when '--training-method' is 'dpo')",
)
@click.option(
"--suffix",
"-s",
type=str,
default=None,
help="Suffix for the fine-tuned model name",
)
@click.option("--wandb-api-key", type=str, default=None, help="Wandb API key")
@click.option("--wandb-base-url", type=str, default=None, help="Wandb base URL")
@click.option("--wandb-project-name", type=str, default=None, help="Wandb project name")
@click.option("--wandb-name", type=str, default=None, help="Wandb run name")
@click.option(
"--confirm",
"-y",
type=bool,
is_flag=True,
default=False,
help="Whether to skip the launch confirmation message",
)
@click.option(
"--train-on-inputs",
type=BOOL_WITH_AUTO,
default="auto",
help="Whether to mask the user messages in conversational data or prompts in instruction data. "
"`auto` will automatically determine whether to mask the inputs based on the data format.",
)
@click.option(
"--from-checkpoint",
type=str,
default=None,
help="The checkpoint identifier to continue training from a previous fine-tuning job. "
"The format: {$JOB_ID/$OUTPUT_MODEL_NAME}:{$STEP}. "
"The step value is optional, without it the final checkpoint will be used.",
)
def create(
ctx: click.Context,
training_file: str,
validation_file: str,
model: str,
n_epochs: int,
n_evals: int,
n_checkpoints: int,
batch_size: int | Literal["max"],
learning_rate: float,
lr_scheduler_type: Literal["linear", "cosine"],
min_lr_ratio: float,
scheduler_num_cycles: float,
warmup_ratio: float,
max_grad_norm: float,
weight_decay: float,
lora: bool,
lora_r: int,
lora_dropout: float,
lora_alpha: float,
lora_trainable_modules: str,
suffix: str,
wandb_api_key: str,
wandb_base_url: str,
wandb_project_name: str,
wandb_name: str,
confirm: bool,
train_on_inputs: bool | Literal["auto"],
training_method: str,
dpo_beta: float,
from_checkpoint: str,
) -> None:
"""Start fine-tuning"""
client: Together = ctx.obj
training_args: dict[str, Any] = dict(
training_file=training_file,
model=model,
n_epochs=n_epochs,
validation_file=validation_file,
n_evals=n_evals,
n_checkpoints=n_checkpoints,
batch_size=batch_size,
learning_rate=learning_rate,
lr_scheduler_type=lr_scheduler_type,
min_lr_ratio=min_lr_ratio,
scheduler_num_cycles=scheduler_num_cycles,
warmup_ratio=warmup_ratio,
max_grad_norm=max_grad_norm,
weight_decay=weight_decay,
lora=lora,
lora_r=lora_r,
lora_dropout=lora_dropout,
lora_alpha=lora_alpha,
lora_trainable_modules=lora_trainable_modules,
suffix=suffix,
wandb_api_key=wandb_api_key,
wandb_base_url=wandb_base_url,
wandb_project_name=wandb_project_name,
wandb_name=wandb_name,
train_on_inputs=train_on_inputs,
training_method=training_method,
dpo_beta=dpo_beta,
from_checkpoint=from_checkpoint,
)
if model is None and from_checkpoint is None:
raise click.BadParameter("You must specify either a model or a checkpoint")
model_name = model
if from_checkpoint is not None:
model_name = from_checkpoint.split(":")[0]
model_limits: FinetuneTrainingLimits = client.fine_tuning.get_model_limits(
model=model_name
)
if lora:
if model_limits.lora_training is None:
raise click.BadParameter(
f"LoRA fine-tuning is not supported for the model `{model}`"
)
if training_method == "dpo":
default_batch_size = model_limits.lora_training.max_batch_size_dpo
else:
default_batch_size = model_limits.lora_training.max_batch_size
default_values = {
"lora_r": model_limits.lora_training.max_rank,
"batch_size": default_batch_size,
"learning_rate": 1e-3,
}
for arg in default_values:
arg_source = ctx.get_parameter_source("arg") # type: ignore[attr-defined]
if arg_source == ParameterSource.DEFAULT:
training_args[arg] = default_values[arg_source]
if ctx.get_parameter_source("lora_alpha") == ParameterSource.DEFAULT: # type: ignore[attr-defined]
training_args["lora_alpha"] = training_args["lora_r"] * 2
else:
if model_limits.full_training is None:
raise click.BadParameter(
f"Full fine-tuning is not supported for the model `{model}`"
)
for param in ["lora_r", "lora_dropout", "lora_alpha", "lora_trainable_modules"]:
param_source = ctx.get_parameter_source(param) # type: ignore[attr-defined]
if param_source != ParameterSource.DEFAULT:
raise click.BadParameter(
f"You set LoRA parameter `{param}` for a full fine-tuning job. "
f"Please change the job type with --lora or remove `{param}` from the arguments"
)
batch_size_source = ctx.get_parameter_source("batch_size") # type: ignore[attr-defined]
if batch_size_source == ParameterSource.DEFAULT:
if training_method == "dpo":
training_args["batch_size"] = (
model_limits.full_training.max_batch_size_dpo
)
else:
training_args["batch_size"] = model_limits.full_training.max_batch_size
if n_evals <= 0 and validation_file:
log_warn(
"Warning: You have specified a validation file but the number of evaluation loops is set to 0. No evaluations will be performed."
)
elif n_evals > 0 and not validation_file:
raise click.BadParameter(
"You have specified a number of evaluation loops but no validation file."
)
if confirm or click.confirm(_CONFIRMATION_MESSAGE, default=True, show_default=True):
response = client.fine_tuning.create(
**training_args,
verbose=True,
)
report_string = f"Successfully submitted a fine-tuning job {response.id}"
if response.created_at is not None:
created_time = datetime.strptime(
response.created_at, "%Y-%m-%dT%H:%M:%S.%f%z"
)
# created_at reports UTC time, we use .astimezone() to convert to local time
formatted_time = created_time.astimezone().strftime("%m/%d/%Y, %H:%M:%S")
report_string += f" at {formatted_time}"
rprint(report_string)
else:
click.echo("No confirmation received, stopping job launch")
@fine_tuning.command()
@click.pass_context
def list(ctx: click.Context) -> None:
"""List fine-tuning jobs"""
client: Together = ctx.obj
response = client.fine_tuning.list()
response.data = response.data or []
# Use a default datetime for None values to make sure the key function always returns a comparable value
epoch_start = datetime.fromtimestamp(0, tz=timezone.utc)
response.data.sort(key=lambda x: parse_timestamp(x.created_at or "") or epoch_start)
display_list = []
for i in response.data:
display_list.append(
{
"Fine-tune ID": i.id,
"Model Output Name": "\n".join(wrap(i.output_name or "", width=30)),
"Status": i.status,
"Created At": i.created_at,
"Price": f"""${finetune_price_to_dollars(
float(str(i.total_price))
)}""", # convert to string for mypy typing
}
)
table = tabulate(display_list, headers="keys", tablefmt="grid", showindex=True)
click.echo(table)
@fine_tuning.command()
@click.pass_context
@click.argument("fine_tune_id", type=str, required=True)
def retrieve(ctx: click.Context, fine_tune_id: str) -> None:
"""Retrieve fine-tuning job details"""
client: Together = ctx.obj
response = client.fine_tuning.retrieve(fine_tune_id)
# remove events from response for cleaner output
response.events = None
click.echo(json.dumps(response.model_dump(exclude_none=True), indent=4))
@fine_tuning.command()
@click.pass_context
@click.argument("fine_tune_id", type=str, required=True)
@click.option(
"--quiet", is_flag=True, help="Do not prompt for confirmation before cancelling job"
)
def cancel(ctx: click.Context, fine_tune_id: str, quiet: bool = False) -> None:
"""Cancel fine-tuning job"""
client: Together = ctx.obj
if not quiet:
confirm_response = input(
"You will be billed for any completed training steps upon cancellation. "
f"Do you want to cancel job {fine_tune_id}? [y/N]"
)
if "y" not in confirm_response.lower():
click.echo({"status": "Cancel not submitted"})
return
response = client.fine_tuning.cancel(fine_tune_id)
click.echo(json.dumps(response.model_dump(exclude_none=True), indent=4))
@fine_tuning.command()
@click.pass_context
@click.argument("fine_tune_id", type=str, required=True)
def list_events(ctx: click.Context, fine_tune_id: str) -> None:
"""List fine-tuning events"""
client: Together = ctx.obj
response = client.fine_tuning.list_events(fine_tune_id)
response.data = response.data or []
display_list = []
for i in response.data:
display_list.append(
{
"Message": "\n".join(wrap(i.message or "", width=50)),
"Type": i.type,
"Created At": parse_timestamp(i.created_at or ""),
"Hash": i.hash,
}
)
table = tabulate(display_list, headers="keys", tablefmt="grid", showindex=True)
click.echo(table)
@fine_tuning.command()
@click.pass_context
@click.argument("fine_tune_id", type=str, required=True)
def list_checkpoints(ctx: click.Context, fine_tune_id: str) -> None:
"""List available checkpoints for a fine-tuning job"""
client: Together = ctx.obj
checkpoints = client.fine_tuning.list_checkpoints(fine_tune_id)
display_list = []
for checkpoint in checkpoints:
display_list.append(
{
"Type": checkpoint.type,
"Timestamp": format_timestamp(checkpoint.timestamp),
"Name": checkpoint.name,
}
)
if display_list:
click.echo(f"Job {fine_tune_id} contains the following checkpoints:")
table = tabulate(display_list, headers="keys", tablefmt="grid")
click.echo(table)
click.echo("\nTo download a checkpoint, use `together fine-tuning download`")
else:
click.echo(f"No checkpoints found for job {fine_tune_id}")
@fine_tuning.command()
@click.pass_context
@click.argument("fine_tune_id", type=str, required=True)
@click.option(
"--output_dir",
"-o",
type=click.Path(exists=True, file_okay=False, resolve_path=True),
required=False,
default=None,
help="Output directory",
)
@click.option(
"--checkpoint-step",
"-s",
type=int,
required=False,
default=None,
help="Download fine-tuning checkpoint. Defaults to latest.",
)
@click.option(
"--checkpoint-type",
type=DownloadCheckpointTypeChoice(),
required=False,
default=DownloadCheckpointType.DEFAULT.value,
help="Specifies checkpoint type. 'merged' and 'adapter' options work only for LoRA jobs.",
)
def download(
ctx: click.Context,
fine_tune_id: str,
output_dir: str,
checkpoint_step: int | None,
checkpoint_type: DownloadCheckpointType,
) -> None:
"""Download fine-tuning checkpoint"""
client: Together = ctx.obj
response = client.fine_tuning.download(
fine_tune_id,
output=output_dir,
checkpoint_step=checkpoint_step,
checkpoint_type=checkpoint_type,
)
click.echo(json.dumps(response.model_dump(exclude_none=True), indent=4))