-
Notifications
You must be signed in to change notification settings - Fork 52
/
train.py
313 lines (283 loc) · 10.4 KB
/
train.py
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
import argparse
import asyncio
import os
import shutil
import subprocess
from zipfile import ZipFile
import psutil
import torch
from cog import BaseModel, Input, Path
from config import (
LOCAL_TRAINING_WEIGHTS_PATH,
REMOTE_TRAINING_WEIGHTS_PATH,
REMOTE_TRAINING_FILES_TO_DOWNLOAD,
MODEL_NAME,
)
from src.utils import maybe_download_with_pget, download_file_with_pget
MODEL_OUT = "/src/tuned_weights.tensors"
CHECKPOINT_DIR = "checkpoints"
SAVE_STRATEGY = "epoch"
OUTPUT_DIR = "training_output"
class TrainingOutput(BaseModel):
weights: Path
def train(
fake_output: str = Input(description="fake training", default=None),
train_data: Path = Input(
description="path to data file to use for fine-tuning your model"
),
num_train_epochs: int = Input(
description="number of training epochs",
ge=1,
default=1,
),
train_batch_size: int = Input(
description="Global batch size. This specifies the batch size that will be used to calculate gradients.",
default=4,
ge=1,
),
gradient_accumulation_steps: int = Input(
description="Number of training steps (each of train_batch_size) to update gradients for before performing a backward pass.",
default=1,
ge=1,
),
num_validation_samples: int = Input(
description=(
"Number of samples to use for validation."
"If `run_validation` is `True` and `validation_data` is not specified, this number of samples"
"will be selected from the tail of the training data. If `validation_data` is specified, this"
"number of samples will be selected from the head of the validation data, up to the size of the validation data."
),
default=50,
ge=1,
),
validation_data: Path = Input(
description="path to optional evaluation data file to use for model eval",
default=None,
),
validation_batch_size: int = Input(
description="Batch size for evaluation", default=1, ge=1
),
run_validation: bool = Input(
description="Whether to run validation during training.", default=True
),
validation_prompt: str = Input(
description="Prompt to use for generation during validation. If provided, a response to this prompt will be sampled and logged during validation.",
default=None,
),
learning_rate: float = Input(
description="learning rate, for learning!", default=1e-4, ge=0
),
pack_sequences: bool = Input(
description="If 'True', sequences will be packed into a single sequences up to a given length. This improves computational efficiency.",
default=False,
),
wrap_packed_sequences: bool = Input(
description="If 'pack_sequences' is 'True', this will wrap packed sequences across examples, ensuring a constant sequence length but breaking prompt formatting.",
default=False,
),
chunk_size: int = Input(
description="If 'pack_sequences' is 'True', this will chunk sequences into chunks of this size.",
default=2048,
ge=1,
),
peft_method: str = Input(
description="Training method to use. Currently, 'lora' and 'qlora'.",
default="lora",
choices=["lora", "qlora"],
),
seed: int = Input(description="random seed to use for training", default=42),
local_model_path: str = Input(
description="Path to local model to use for training. If not specified, will download a model based on `REMOTE_TRAINING_WEIGHTS_PATH`.",
default=None,
),
# weights: Path = Input(
# description="location of weights that are going to be fine-tuned", default=None
# ),
#
# warmup_ratio: float = Input(
# description="pct of steps for a linear learning rate warmup",
# ge=0,
# le=0.5,
# default=0.03,
# ),
# max_steps: int = Input(
# description="number of steps to run training for, supersedes num_train_epochs",
# default=-1,
# ),
# logging_steps: int = Input(
# description="number of steps between logging epoch & loss", default=1
# ),
lora_rank: int = Input(description="Rank of the lora matrices", default=8, ge=1),
lora_alpha: int = Input(
description="Alpha parameter for scaling lora weights; weights are scaled by alpha/rank",
default=16,
ge=1,
),
lora_dropout: float = Input(
description="Dropout for lora training", default=0.05, ge=0.0, le=1.0
),
# lora_target_modules: str = Input(description="Comma-separated list of lora modules to target, i.e. 'q_proj,v_proj'. Leave blank for default.", default="q_proj,v_proj")
) -> TrainingOutput:
if fake_output:
out_path = f"/tmp/{os.path.basename(fake_output)}"
asyncio.run(download_file_with_pget(fake_output, out_path))
return TrainingOutput(weights=Path(out_path))
# Hardcode QLoRA for 70B models for now
if "70" in MODEL_NAME and peft_method != "qlora":
print("Using 70B model, setting peft_method to qlora")
peft_method = "qlora"
if not local_model_path:
weights = REMOTE_TRAINING_WEIGHTS_PATH
if "http" in weights:
print(f"Downloading weights to {LOCAL_TRAINING_WEIGHTS_PATH}...")
model_path = maybe_download_with_pget(
LOCAL_TRAINING_WEIGHTS_PATH,
weights,
REMOTE_TRAINING_FILES_TO_DOWNLOAD,
)
else:
model_path = local_model_path
root_path = os.getcwd()
output_dir = OUTPUT_DIR
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
os.makedirs(output_dir)
num_gpus = torch.cuda.device_count()
print(f"Local Output Dir: {output_dir}")
print(f"Number of GPUs: {num_gpus}")
os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["HF_DATASETS_CACHE"] = "/src/.hf-cache"
args = []
if peft_method != "qlora":
args.extend(
[
"python3",
"-m",
"torch.distributed.run",
"--nnodes=1",
f"--nproc_per_node={num_gpus}",
]
)
else:
args.append("python")
args.append(
"llama_recipes/llama_finetuning.py",
)
if peft_method != "qlora":
args.append(
"--enable_fsdp",
)
args.extend(
[
# Hard coded for now
"--use_peft",
f"--model_name={model_path}",
"--pure_bf16",
f"--output_dir={output_dir}",
# User specified arguments -----
# Preprocessing arguments
f"--pack_sequences={pack_sequences}",
f"--wrap_packed_sequences={wrap_packed_sequences}",
f"--chunk_size={chunk_size}",
# Train arguments
f"--data_path={train_data}",
f"--num_epochs={num_train_epochs}",
f"--batch_size_training={train_batch_size}",
f"--gradient_accumulation_steps={gradient_accumulation_steps}",
f"--lr={learning_rate}",
f"--lora_rank={lora_rank}",
f"--lora_alpha={lora_alpha}",
f"--lora_dropout={lora_dropout}",
f"--peft_method={peft_method}",
# Validation arguments
f"--run_validation={'False' if not run_validation else 'True'}",
f"--num_validation_samples={num_validation_samples}",
f"--validation_data_path={validation_data}",
f"--val_batch_size={validation_batch_size}",
f"--validation_prompt={validation_prompt}",
# Other arguments
f"--seed={seed}",
]
)
print(f"Train.py Arguments: \n{args}")
p = None
try:
p = subprocess.Popen(args, close_fds=False)
p.wait()
return_code = p.poll()
if return_code != 0:
raise Exception(
f"Training failed with exit code {return_code}! Check logs for details"
)
out_path = "training_output.zip"
directory = Path(output_dir)
with ZipFile(out_path, "w") as zip:
for file_path in directory.rglob("*"):
print(file_path)
zip.write(file_path, arcname=file_path.relative_to(directory))
return TrainingOutput(weights=Path(out_path))
finally:
if p and p.poll() is None:
top = psutil.Process(p.pid)
children = top.children(recursive=True)
for process in children + [top]:
process.terminate()
_, alive = psutil.wait_procs(children + [top], timeout=5)
if alive:
for process in alive:
print(f"process {process.pid} survived termination")
else:
print("terminated all processes successfully")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Fine-tune a language model on a text dataset"
)
parser.add_argument(
"--train_data", type=Path, required=True, help="Path to the json dataset"
)
parser.add_argument(
"--eval_data",
type=Path,
required=False,
help="Path to the json dataset",
default=None,
)
parser.add_argument(
"--weights",
type=str,
default=None,
help="The model class to fine-tune on HF or as a local path (e.g. 'google/flan-t5-xxl'",
)
parser.add_argument(
"--num_train_epochs", type=int, required=True, help="Number of training epochs"
)
parser.add_argument(
"--learning_rate",
type=float,
default=2e-5,
help="Learning rate for the optimizer",
)
parser.add_argument(
"--train_batch_size", type=int, default=4, help="Batch size for training"
)
parser.add_argument(
"--warmup_ratio",
type=float,
default=0.03,
help="Number of warmup steps for the learning rate scheduler",
)
parser.add_argument(
"--max_steps",
type=int,
default=0,
help="Number of training steps to run, overrides num_train_epochs, useful for testing",
)
parser.add_argument(
"--gradient_accumulation_steps",
type=int,
default=8,
help="Number of training steps to run, overrides num_train_epochs, useful for testing",
)
parser.add_argument("--logging_steps", type=int, default=1)
some_args = parser.parse_args()
train(**vars(some_args))