-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_clm.py
406 lines (334 loc) · 14.1 KB
/
run_clm.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
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
# Copyright 2023 Databricks, Inc.
import json
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# adapted from https://github.com/databrickslabs/dolly/blob/master/training/consts.py
# and https://github.com/databrickslabs/dolly/blob/master/training/trainer.py
import logging
from functools import partial
from pathlib import Path
from typing import Any, Dict, List, Tuple, Union
import os
import click
import numpy as np
from datasets import Dataset, load_dataset
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
DataCollatorForLanguageModeling,
PreTrainedTokenizer,
Trainer,
TrainingArguments,
set_seed,
)
import datasets
os.environ["RANK"] = "0"
os.environ["LOCAL_RANK"] = "0"
os.environ["WORLD_SIZE"] = "1"
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '0'
DEFAULT_INPUT_MODEL = "EleutherAI/pythia-70m"
DEFAULT_SEED = 42
logger = logging.getLogger(__name__)
ROOT_PATH = Path(__file__).parent.parent
DATABRICKS_DOLLY_15K_PATH = ROOT_PATH / "data" / "databricks-dolly-15k.jsonl"
INTRO_BLURB = (
"Below is an instruction that describes a task. Write a response that appropriately completes the request."
)
INSTRUCTION_KEY = "### Instruction:"
INPUT_KEY = "Input:"
RESPONSE_KEY = "### Response:"
END_KEY = "### End"
RESPONSE_KEY_NL = f"{RESPONSE_KEY}\n"
DEFAULT_SEED = 42
# This is a training prompt that does not contain an input string. The instruction by itself has enough information
# to respond. For example, the instruction might ask for the year a historic figure was born.
PROMPT_NO_INPUT_FORMAT = """{intro}
{instruction_key}
{instruction}
{response_key}
{response}
{end_key}""".format(
intro=INTRO_BLURB,
instruction_key=INSTRUCTION_KEY,
instruction="{instruction}",
response_key=RESPONSE_KEY,
response="{response}",
end_key=END_KEY,
)
# This is a training prompt that contains an input string that serves as context for the instruction. For example,
# the input might be a passage from Wikipedia and the intruction is to extract some information from it.
PROMPT_WITH_INPUT_FORMAT = """{intro}
{instruction_key}
{instruction}
{input_key}
{input}
{response_key}
{response}
{end_key}""".format(
intro=INTRO_BLURB,
instruction_key=INSTRUCTION_KEY,
instruction="{instruction}",
input_key=INPUT_KEY,
input="{input}",
response_key=RESPONSE_KEY,
response="{response}",
end_key=END_KEY,
)
# This is the prompt that is used for generating responses using an already trained model. It ends with the response
# key, where the job of the model is to provide the completion that follows it (i.e. the response itself).
PROMPT_FOR_GENERATION_FORMAT = """{intro}
{instruction_key}
{instruction}
{response_key}
""".format(
intro=INTRO_BLURB,
instruction_key=INSTRUCTION_KEY,
instruction="{instruction}",
response_key=RESPONSE_KEY,
)
INSTRUCTION_KEY = "### Instruction:"
INPUT_KEY = "Input:"
RESPONSE_KEY = "### Response:"
END_KEY = "### End"
class DataCollatorForCompletionOnlyLM(DataCollatorForLanguageModeling):
def torch_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]:
batch = super().torch_call(examples)
# The prompt ends with the response key plus a newline. We encode this and then try to find it in the
# sequence of tokens. This should just be a single token.
response_token_ids = self.tokenizer.encode(RESPONSE_KEY_NL)
labels = batch["labels"].clone()
for i in range(len(examples)):
response_token_ids_start_idx = None
for idx in np.where(batch["labels"][i] == response_token_ids[0])[0]:
response_token_ids_start_idx = idx
break
if response_token_ids_start_idx is None:
raise RuntimeError(
f'Could not find response key {response_token_ids} in token IDs {batch["labels"][i]}'
)
response_token_ids_end_idx = response_token_ids_start_idx + 1
# Make pytorch loss function ignore all tokens up through the end of the response key
labels[i, :response_token_ids_end_idx] = -100
batch["labels"] = labels
return batch
def preprocess_batch(batch: Dict[str, List], tokenizer: AutoTokenizer, max_length: int) -> dict:
return tokenizer(
batch["text"],
max_length=max_length,
truncation=True,
)
def load_training_dataset(name: str, small=False) -> Dataset:
logger.info(f"Loading dataset from {path}")
dataset = datasets.load_dataset(name)
if small:
dataset = dataset.select(range(100))
logger.info("Found %d rows", dataset.num_rows)
def _add_text(rec):
instruction = rec.get("instruction") or rec.get("input")
response = rec.get("response") or rec.get("target")
context = rec.get("context")
if not instruction:
raise ValueError(f"Expected an instruction in: {rec}")
if response is None:
raise ValueError(f"Expected a response in: {rec}")
# For some instructions there is an input that goes along with the instruction, providing context for the
# instruction. For example, the input might be a passage from Wikipedia and the instruction says to extract
# some piece of information from it. The response is that information to extract. In other cases there is
# no input. For example, the instruction might be open QA such as asking what year some historic figure was
# born.
if context:
rec["text"] = PROMPT_WITH_INPUT_FORMAT.format(instruction=instruction, response=response, input=context)
else:
rec["text"] = PROMPT_NO_INPUT_FORMAT.format(instruction=instruction, response=response)
return rec
dataset = dataset.map(_add_text)
return dataset
def load_tokenizer(pretrained_model_name_or_path: str = DEFAULT_INPUT_MODEL) -> PreTrainedTokenizer:
logger.info(f"Loading tokenizer for {pretrained_model_name_or_path}")
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.add_special_tokens({"additional_special_tokens": [END_KEY, INSTRUCTION_KEY, RESPONSE_KEY_NL]})
return tokenizer
def load_model(
pretrained_model_name_or_path: str = DEFAULT_INPUT_MODEL, *, gradient_checkpointing: bool = False
) -> AutoModelForCausalLM:
logger.info(f"Loading model for {pretrained_model_name_or_path}")
model = AutoModelForCausalLM.from_pretrained(
pretrained_model_name_or_path, trust_remote_code=True, use_cache=False if gradient_checkpointing else True
)
return model
def get_model_tokenizer(
pretrained_model_name_or_path: str = DEFAULT_INPUT_MODEL, *, gradient_checkpointing: bool = False
) -> Tuple[AutoModelForCausalLM, PreTrainedTokenizer]:
tokenizer = load_tokenizer(pretrained_model_name_or_path)
model = load_model(pretrained_model_name_or_path, gradient_checkpointing=gradient_checkpointing)
model.resize_token_embeddings(len(tokenizer))
return model, tokenizer
def preprocess_dataset(name, tokenizer: AutoTokenizer, max_length: int, seed=DEFAULT_SEED) -> Dataset:
"""Loads the training dataset and tokenizes it so it is ready for training.
Args:
tokenizer (AutoTokenizer): Tokenizer tied to the model.
max_length (int): Maximum number of tokens to emit from tokenizer.
Returns:
Dataset: HuggingFace dataset
"""
dataset = load_training_dataset(name)
logger.info("Preprocessing dataset")
_preprocessing_function = partial(preprocess_batch, max_length=max_length, tokenizer=tokenizer)
cols_to_remove = dataset.column_names
dataset = dataset.map(
_preprocessing_function,
batched=True,
remove_columns=cols_to_remove,
)
# Make sure we don't have any truncated records, as this would mean the end keyword is missing.
logger.info("Processed dataset has %d rows", dataset.num_rows)
dataset = dataset.filter(lambda rec: len(rec["input_ids"]) < max_length)
logger.info("Processed dataset has %d rows after filtering for truncated records", dataset.num_rows)
logger.info("Shuffling dataset")
dataset = dataset.shuffle(seed=seed)
logger.info("Done preprocessing")
return dataset
def train(
*,
input_model: str,
name: str,
output_dir: str,
epochs: int,
per_device_train_batch_size: int,
per_device_eval_batch_size: int,
lr: float,
seed: int,
deepspeed: str,
gradient_checkpointing: bool,
local_rank: str,
bf16: bool,
logging_steps: int,
test_size: Union[float, int],
save_total_limit: int,
warmup_steps: int,
train_on_input: bool,
gradient_accumulation: int,
):
set_seed(seed)
model, tokenizer = get_model_tokenizer(
pretrained_model_name_or_path=input_model, gradient_checkpointing=gradient_checkpointing
)
conf = model.config
max_length = 512
# for length_setting in ["n_positions", "max_position_embeddings", "seq_length"]:
# max_length = getattr(model.config, length_setting, None)
# if max_length:
# logger.info(f"Found max lenth: {max_length}")
# break
# if not max_length:
# max_length = 1024
# logger.info(f"Using default max length: {max_length}")
processed_dataset = preprocess_dataset(name=name, tokenizer=tokenizer, max_length=max_length, seed=seed)
if test_size > 0:
split_dataset = processed_dataset.train_test_split(test_size=test_size, seed=seed)
else:
split_dataset = {"train": processed_dataset}
logger.info("Train data size: %d", split_dataset["train"].num_rows)
if test_size > 0:
logger.info("Test data size: %d", split_dataset["test"].num_rows)
else:
logger.info("Test data size: 0")
if train_on_input:
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer, mlm=False, return_tensors="pt", pad_to_multiple_of=8
)
else:
data_collator = DataCollatorForCompletionOnlyLM(
tokenizer=tokenizer, mlm=False, return_tensors="pt", pad_to_multiple_of=8
)
training_args = TrainingArguments(
output_dir=output_dir,
per_device_train_batch_size=per_device_train_batch_size,
per_device_eval_batch_size=per_device_eval_batch_size,
fp16=False,
bf16=bf16,
learning_rate=lr,
num_train_epochs=epochs,
deepspeed=deepspeed,
gradient_checkpointing=gradient_checkpointing,
logging_dir=f"{output_dir}/runs",
logging_strategy="steps",
logging_steps=logging_steps,
evaluation_strategy="epoch" if test_size > 0 else "no",
eval_steps=1,
save_strategy="epoch",
save_steps=1,
save_total_limit=save_total_limit,
load_best_model_at_end=False,
report_to="wandb",
disable_tqdm=False,
remove_unused_columns=False,
local_rank=local_rank,
warmup_steps=warmup_steps,
gradient_accumulation_steps=gradient_accumulation
)
logger.info("Instantiating Trainer")
trainer = Trainer(
model=model,
tokenizer=tokenizer,
args=training_args,
train_dataset=split_dataset["train"],
eval_dataset=split_dataset.get("test"),
data_collator=data_collator,
)
logger.info("Training")
trainer.train()
logger.info(f"Saving Model to {output_dir}")
trainer.save_model(output_dir=output_dir)
logger.info("Done.")
@click.command()
@click.option("--train-file", type=str, help="Path to training data", required=True)
@click.option("--input-model", type=str, help="Input model to fine tune", default=DEFAULT_INPUT_MODEL)
@click.option("--output-dir", type=str, help="Write directly to this local path", required=True)
@click.option("--epochs", type=int, default=10, help="Number of epochs to train for.")
@click.option("--per-device-train-batch-size", type=int, default=64, help="Batch size to use for training.")
@click.option("--per-device-eval-batch-size", type=int, default=8, help="Batch size to use for evaluation.")
@click.option(
"--test-size", type=int, default=0, help="Number of test records for evaluation, or ratio of test records."
)
@click.option("--warmup-steps", type=int, default=0, help="Number of steps to warm up to learning rate")
@click.option("--logging-steps", type=int, default=10, help="How often to log")
@click.option("--save-total-limit", type=int, default=10, help="Maximum number of checkpoints to keep on disk")
@click.option("--lr", type=float, default=1e-5, help="Learning rate to use for training.")
@click.option("--seed", type=int, default=DEFAULT_SEED, help="Seed to use for training.")
@click.option("--deepspeed", type=str, default=None, help="Path to deepspeed config file.")
@click.option("--train-on-input", is_flag=True, help="Train the model also on the input (and not only the response).")
@click.option("--gradient-accumulation", type=int, default=1, help="Number of steps to accumulate gradients over.")
@click.option(
"--gradient-checkpointing/--no-gradient-checkpointing",
is_flag=True,
default=False,
help="Use gradient checkpointing?",
)
@click.option(
"--local_rank",
type=str,
default=True,
help="Provided by deepspeed to identify which instance this process is when performing multi-GPU training.",
)
@click.option("--bf16", type=bool, default=True, help="Whether to use bf16 (preferred on A100's).")
def main(**kwargs):
train(**kwargs)
if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s %(levelname)s [%(name)s] %(message)s", level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S"
)
try:
main()
except Exception:
logger.exception("main failed")
raise