-
Notifications
You must be signed in to change notification settings - Fork 9
/
finetune_quality.py
208 lines (173 loc) · 7.43 KB
/
finetune_quality.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
import copy
import os
from typing import Dict, Optional
import torch
import transformers
from data import make_lloco_data_module
from model import DataArguments, ModelArguments, TrainingArguments, init_model
from torch.utils.data import Dataset
from transformers import Trainer
from utils import load_jsonl
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
IGNORE_INDEX = -100
truncation_seperator = "... [The rest of the story is omitted]\n\n"
local_rank = None
def rank0_print(*args):
if local_rank == 0:
print(*args)
quality_prompt = "You are provided a story from above. We will now give you a multiple-choice question with 4 possible answers (marked by A, B, C, D). Choose the best answer by writing its corresponding letter (either A, B, C, or D).\nExample question:\nWhere is the capital of France?\n\nChoices:\nA. Berlin\nB. Paris\nC. London\nD. Tokyo\nChoose the best answer by writing its corresponding letter (either A, B, C, or D).\n\nAnswer:\nB. Paris\n\n"
class LazyQualitySFTDataset(Dataset):
"""Dataset for supervised fine-tuning."""
def __init__(
self,
quality_path: str,
tokenizer: transformers.PreTrainedTokenizer,
embedding_path: Optional[str] = None,
split: str = "train",
mode: str = "baseline",
):
super(LazyQualitySFTDataset, self).__init__()
rank0_print("Loading data...")
self.quality_data = load_jsonl(quality_path)
self.context_data_dict = self.__build_context_dict(self.quality_data)
self.sft_data_dict = self.__preprocess(self.quality_data)
if embedding_path is not None:
rank0_print("Loading context embeddings...")
self.context_embeddings_map = torch.load(embedding_path)
self.is_preprocessed = True
else:
rank0_print(
"No context embeddings provided, will use context data instead."
)
self.context_embeddings_map = None
self.is_preprocessed = False
assert self.is_preprocessed
self.tokenizer = tokenizer
self.is_eval = True if split == "validation" else False
self.eval_mode = mode
def __build_context_dict(self, quality_data):
ret = {}
for entry in quality_data:
article_id = entry["article_id"]
article = entry["article"]
ret[article_id] = article
return ret
def __preprocess(self, quality_data):
ret = []
choice_prefix = ["A. ", "B. ", "C. ", "D. "]
for entry in quality_data:
for q in entry["questions"]:
res = {}
gold_label = q["gold_label"]
gt = gold_label - 1
question_prompt = "Question:\n" + q["question"] + "\n\n"
choice_prompt = "Choices:\n"
choices = q["options"]
for i in range(4):
c = choices[i]
choice_prompt += choice_prefix[i] + c + "\n"
choice_prompt += (
"Choose the best answer by writing its corresponding letter (either A, B, C, or D).\n\nAnswer:"
+ "\n"
)
answer_prompt = choice_prefix[gt] + choices[gt]
res = {
"article_id": entry["article_id"],
"id": q["question_unique_id"],
"question": question_prompt + choice_prompt,
"answer": answer_prompt,
}
ret.append(res)
return ret
def __len__(self):
return len(self.sft_data_dict)
def __getitem__(self, i) -> Dict[str, torch.Tensor]:
instruction_pair = self.sft_data_dict[i]
article_id = instruction_pair["article_id"]
question = instruction_pair["question"]
answer = instruction_pair["answer"]
q_prompt = quality_prompt + question
q_input_ids = self.tokenizer(
q_prompt,
add_special_tokens=False,
).input_ids
a_input_ids = self.tokenizer(answer, add_special_tokens=False).input_ids
a_input_ids += [self.tokenizer.eos_token_id]
context_embeddings = self.context_embeddings_map[article_id]
if self.is_eval:
if self.eval_mode == "baseline":
context = self.context_data_dict[article_id]
context = "Article: " + context + truncation_seperator
c_input_ids = self.tokenizer(
context,
padding="longest",
truncation=True,
add_special_tokens=False,
max_length=4000 - len(q_input_ids),
).input_ids
decoder_input_ids = copy.deepcopy(c_input_ids + q_input_ids)
return {
"decoder_input_ids": torch.as_tensor([decoder_input_ids]).to(device)
}
elif self.eval_mode == "baseline_nocontext":
decoder_input_ids = copy.deepcopy(q_input_ids)
return {
"decoder_input_ids": torch.as_tensor([decoder_input_ids]).to(device)
}
else:
decoder_input_ids = copy.deepcopy(q_input_ids)
ret = dict(
decoder_input_ids=torch.as_tensor(decoder_input_ids).to(device),
context_embeddings=context_embeddings.to(device),
)
return ret
else:
decoder_input_ids = copy.deepcopy(q_input_ids)
decoder_input_ids += a_input_ids
decoder_input_ids = torch.as_tensor(decoder_input_ids)
labels = copy.deepcopy(decoder_input_ids)
labels[: len(q_input_ids)] = IGNORE_INDEX
ret = dict(
input_ids=decoder_input_ids,
labels=labels,
inputs_embeds=context_embeddings,
)
return ret
def get_ground_truth(self, i):
instruction_pair = self.sft_data_dict[i]
answer = instruction_pair["answer"]
return answer
def get_example_id(self, i):
return self.sft_data_dict[i]["id"]
def train():
global local_rank
parser = transformers.HfArgumentParser(
(ModelArguments, DataArguments, TrainingArguments)
)
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
local_rank = training_args.local_rank
if not os.path.exists(data_args.embedding_path):
rank0_print("Embedding file does not exist...")
exit()
else:
rank0_print("Embedding file exists, skipping preprocessing...")
model = init_model(model_args, data_args, training_args)
tokenizer = transformers.AutoTokenizer.from_pretrained(model_args.model_name_or_path)
tokenizer.pad_token = '[PAD]'
model.config.use_cache = False # required for gradient checkpointing
model.base_model.enable_input_require_grads() # required for gradient checkpointing
model.base_model.gradient_checkpointing_enable() # enable gradient
data_module = make_lloco_data_module(model=model,
tokenizer=tokenizer,
dataset_cls=LazyQualitySFTDataset,
data_args=data_args,
quality_path=data_args.data_path,
)
trainer = Trainer(
model=model, tokenizer=tokenizer, args=training_args, **data_module
)
trainer.train()
trainer.save_state()
trainer.save_model(output_dir=training_args.output_dir)
if __name__ == "__main__":
train()