-
Notifications
You must be signed in to change notification settings - Fork 2
/
finetune.py
167 lines (135 loc) · 5.38 KB
/
finetune.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
import logging
import sys
import datasets
import determined as det
import evaluate
import torch
import transformers
from determined.transformers import DetCallback
from peft import AutoPeftModelForCausalLM, LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
from trl import DataCollatorForCompletionOnlyLM
from chat_format import get_chat_format, get_response_template_ids, set_special_tokens
from dataset_utils import load_or_create_dataset
logger = logging.getLogger(__name__)
def get_tokenizer(model_name):
tokenizer = AutoTokenizer.from_pretrained(
model_name,
padding_side="right",
truncation_side="right",
)
set_special_tokens(tokenizer, model_name)
return tokenizer
def get_model_and_tokenizer(model_name, use_lora, inference=False, device_map="auto"):
if inference:
if use_lora:
model = AutoPeftModelForCausalLM.from_pretrained(
model_name, torch_dtype=torch.bfloat16, device_map=device_map
)
else:
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
device_map=device_map,
)
else:
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.bfloat16,
)
if use_lora:
peft_config = LoraConfig(
task_type="CAUSAL_LM",
inference_mode=False,
r=8,
lora_alpha=32,
lora_dropout=0.1,
)
model = get_peft_model(model, peft_config)
tokenizer = get_tokenizer(model_name)
return model, tokenizer
def get_tokenize_fn(tokenizer):
def fn(formatted):
return tokenizer(formatted, padding=True, truncation=True, max_length=2048)
return fn
def preprocess_logits_for_metrics(logits, labels):
if isinstance(logits, tuple):
# Depending on the model and config, logits may contain extra tensors,
# like past_key_values, but logits always come first
logits = logits[0]
return logits.argmax(dim=-1)
def main(training_args, det_callback, hparams):
model_name = hparams["model"]
model, tokenizer = get_model_and_tokenizer(model_name, hparams["lora"])
tokenize_fn = get_tokenize_fn(tokenizer)
def tokenize(element):
formatted = tokenizer.apply_chat_template(
get_chat_format(element, model_name), tokenize=False
)
outputs = tokenize_fn(formatted)
return {
"input_ids": outputs["input_ids"],
"attention_mask": outputs["attention_mask"],
}
dataset = load_or_create_dataset(hparams["dataset_subset"])
column_names = list(dataset["train"].features)
for k in dataset.keys():
dataset[k] = dataset[k].map(tokenize, remove_columns=column_names)
response_template_ids = get_response_template_ids(tokenizer, model_name)
collator = DataCollatorForCompletionOnlyLM(
response_template_ids, tokenizer=tokenizer
)
bleu = evaluate.load("bleu")
acc = evaluate.load("accuracy")
def compute_metrics(eval_preds):
preds, labels = eval_preds
# preds have the same shape as the labels, after the argmax(-1) has been calculated
# by preprocess_logits_for_metrics but we need to shift the labels
labels = labels[:, 1:]
preds = preds[:, :-1]
# -100 is a default value for ignore_index used by DataCollatorForCompletionOnlyLM
mask = labels == -100
labels[mask] = tokenizer.pad_token_id
preds[mask] = tokenizer.pad_token_id
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
bleu_score = bleu.compute(predictions=decoded_preds, references=decoded_labels)
accuracy = acc.compute(predictions=preds[~mask], references=labels[~mask])
return {**bleu_score, **accuracy}
trainer = Trainer(
args=training_args,
model=model,
tokenizer=tokenizer,
data_collator=collator,
train_dataset=dataset["train"],
eval_dataset=dataset["valid"],
preprocess_logits_for_metrics=preprocess_logits_for_metrics,
compute_metrics=compute_metrics,
)
trainer.add_callback(det_callback)
trainer.train()
if __name__ == "__main__":
# Setup logging
logging.basicConfig(
format=det.LOG_FORMAT, handlers=[logging.StreamHandler(sys.stdout)]
)
log_level = logging.INFO
transformers.utils.logging.set_verbosity_info()
logger.setLevel(log_level)
datasets.utils.logging.set_verbosity(log_level)
transformers.utils.logging.set_verbosity(log_level)
transformers.utils.logging.enable_default_handler()
transformers.utils.logging.enable_explicit_format()
info = det.get_cluster_info()
hparams = info.trial.hparams
training_args = TrainingArguments(**hparams["training_args"])
if training_args.deepspeed:
distributed = det.core.DistributedContext.from_deepspeed()
else:
distributed = det.core.DistributedContext.from_torch_distributed()
with det.core.init(distributed=distributed) as core_context:
det_callback = DetCallback(
core_context,
training_args,
)
main(training_args, det_callback, hparams)