-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathdata_utils_sft.py
351 lines (311 loc) · 12.7 KB
/
data_utils_sft.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
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import copy
import glob
import os
from dataclasses import dataclass, field
from typing import Optional, Dict, Sequence, List
import pandas as pd
import torch
import transformers
from torch.nn.utils.rnn import pad_sequence
from datasets import load_dataset, Dataset
IGNORE_INDEX = -100
@dataclass
class DataCollatorForCausalLM(object):
left_truncated_tokenizer: transformers.PreTrainedTokenizer
tokenizer: transformers.PreTrainedTokenizer
source_max_len: int
target_max_len: int
train_on_source: bool
predict_with_generate: bool
add_eos_to_target: bool
def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
sources = [example["input"] for example in instances]
if self.add_eos_to_target:
targets = [
f"\n{example['output']}{self.tokenizer.eos_token}"
for example in instances
]
else:
targets = [f"\n{example['output']}" for example in instances]
begin_padding_len = self.tokenizer(
["\n"], return_tensors="pt", add_special_tokens=False
).input_ids.shape[1]
# Tokenize
tokenized_sources_with_prompt = self.left_truncated_tokenizer(
sources,
max_length=self.source_max_len,
truncation=True,
# add_special_tokens=False,
)
tokenized_targets = self.tokenizer(
targets,
max_length=self.target_max_len + begin_padding_len,
truncation=True,
add_special_tokens=False,
)
# Build the input and labels for causal LM
input_ids = []
labels = []
for tokenized_source, tokenized_target in zip(
tokenized_sources_with_prompt["input_ids"], tokenized_targets["input_ids"]
):
tokenized_target = tokenized_target[begin_padding_len:]
if not self.predict_with_generate:
input_ids.append(torch.tensor(tokenized_source + tokenized_target))
if not self.train_on_source:
labels.append(
torch.tensor(
[IGNORE_INDEX for _ in range(len(tokenized_source))]
+ copy.deepcopy(tokenized_target)
)
)
else:
labels.append(
torch.tensor(copy.deepcopy(tokenized_source + tokenized_target))
)
else:
input_ids.append(torch.tensor(tokenized_source))
# Apply padding
input_ids = pad_sequence(
input_ids, batch_first=True, padding_value=self.tokenizer.pad_token_id
)
labels = (
pad_sequence(labels, batch_first=True, padding_value=IGNORE_INDEX)
if not self.predict_with_generate
else None
)
data_dict = {
"input_ids": input_ids,
"attention_mask": input_ids.ne(self.tokenizer.pad_token_id),
}
if labels is not None:
data_dict["labels"] = labels
return data_dict
def extract_unnatural_instructions_data(examples, extract_reformulations=False):
out = {
"input": [],
"output": [],
}
for example_instances in examples["instances"]:
for instance in example_instances:
out["input"].append(instance["instruction_with_input"])
out["output"].append(instance["output"])
if extract_reformulations:
for example_reformulations in examples["reformulations"]:
if example_reformulations is not None:
for instance in example_reformulations:
out["input"].append(instance["instruction_with_input"])
out["output"].append(instance["output"])
return out
ALPACA_PROMPT_DICT = {
"prompt_input": (
"Below is an instruction that describes a task, paired with an input that provides further context. "
"Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response: "
),
"prompt_no_input": (
"Below is an instruction that describes a task. "
"Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{instruction}\n\n### Response: "
),
}
def extract_alpaca_dataset(example):
if example.get("input", "") != "":
prompt_format = ALPACA_PROMPT_DICT["prompt_input"]
else:
prompt_format = ALPACA_PROMPT_DICT["prompt_no_input"]
return {"input": prompt_format.format(**example)}
DROMEDARY_PROMPT_DICT = {
"prompt_input": (
"{meta_prompt}\n" "{instruction}\n\n" "{input}\n\n" "### Dromedary"
),
"prompt_no_input": ("{meta_prompt}\n" "{instruction}\n\n" "### Dromedary"),
}
def extract_dromedary_dataset(example, meta_prompts):
assert "example_id" in example
total_meta_prompt = len(meta_prompts)
meta_prompt = meta_prompts[int(example["example_id"]) % total_meta_prompt]
if example.get("input", "") != "":
prompt_format = DROMEDARY_PROMPT_DICT["prompt_input"]
else:
prompt_format = DROMEDARY_PROMPT_DICT["prompt_no_input"]
return {
"input": prompt_format.format(meta_prompt=meta_prompt, **example),
"output": "\n" + example["output"],
}
def local_dataset(dataset_name):
if dataset_name.endswith(".json"):
full_dataset = load_dataset("json", data_files=dataset_name)
else:
raise ValueError(f"Unsupported dataset format: {dataset_name}")
return full_dataset
def make_meta_prompts(meta_prompt_pattern: str):
meta_prompt_files = glob.glob(meta_prompt_pattern)
print(f"Found {len(meta_prompt_files)} meta prompts: {meta_prompt_files}")
meta_prompts = []
for meta_prompt_file in meta_prompt_files:
with open(meta_prompt_file, "r", encoding="utf-8") as f:
meta_prompt = f.readlines()
meta_prompt = "".join(meta_prompt).strip()
meta_prompts.append(meta_prompt)
return meta_prompts
def make_sft_data_module(
left_truncated_tokenizer: transformers.PreTrainedTokenizer,
tokenizer: transformers.PreTrainedTokenizer,
args,
) -> Dict:
"""
Make dataset and collator for supervised fine-tuning.
Datasets are expected to have the following columns: { `input`, `output` }
Available datasets to be selected with `dataset` argument:
- alpaca, 52002 examples
- alpaca cleaned, 51942 examples
- chip2 (OIG), 210289 examples
- self-instruct, 82612 examples
- hh-rlhf (Anthropic), 160800 examples
- longform, 23.7k examples
- oasst1 (OpenAssistant) primary message tree only, 9,846 examples
Coming soon:
- unnatural instructions core, 66010 examples
- unnatural instructions full, 240670 examples
- alpaca-gpt4, 52002 examples
- unnatural-instructions-gpt4, 9000 examples
- supernatural-instructions, 69624 examples (same as paper with 100 ex/task more can be used)
- flan (FLAN v2), up to 20M examples available
- vicuna
"""
def load_data(dataset_name):
if dataset_name == "alpaca":
return load_dataset("tatsu-lab/alpaca")
elif dataset_name == "alpaca-clean":
return load_dataset("yahma/alpaca-cleaned")
elif dataset_name == "chip2":
return load_dataset("laion/OIG", data_files="unified_chip2.jsonl")
elif dataset_name == "self-instruct":
return load_dataset("yizhongw/self_instruct", name="self_instruct")
elif dataset_name == "hh-rlhf":
return load_dataset("Anthropic/hh-rlhf")
elif dataset_name == "longform":
return load_dataset("akoksal/LongForm")
elif dataset_name == "oasst1":
return load_dataset("timdettmers/openassistant-guanaco")
elif dataset_name == "vicuna":
raise NotImplementedError("Vicuna data was not released.")
else:
if os.path.exists(dataset_name):
try:
args.dataset_format = (
args.dataset_format if args.dataset_format else "alpaca"
)
full_dataset = local_dataset(dataset_name)
return full_dataset
except:
raise ValueError(f"Error loading dataset from {dataset_name}")
else:
raise NotImplementedError(
f"Dataset {dataset_name} not implemented yet."
)
def format_dataset(dataset, dataset_format, meta_prompt_pattern):
if dataset_format == "dromedary":
assert meta_prompt_pattern is not None
meta_prompts = make_meta_prompts(meta_prompt_pattern)
dataset = dataset.map(
lambda ex: extract_dromedary_dataset(ex, meta_prompts=meta_prompts),
remove_columns=["instruction", "example_id"],
)
elif (
dataset_format == "alpaca"
or dataset_format == "alpaca-clean"
or (dataset_format is None and args.dataset in ["alpaca", "alpaca-clean"])
):
dataset = dataset.map(
extract_alpaca_dataset, remove_columns=["instruction"]
)
elif dataset_format == "chip2" or (
dataset_format is None and args.dataset == "chip2"
):
dataset = dataset.map(
lambda x: {
"input": x["text"].split("\n<bot>: ")[0].replace("<human>: ", ""),
"output": x["text"].split("\n<bot>: ")[1],
}
)
elif dataset_format == "self-instruct" or (
dataset_format is None and args.dataset == "self-instruct"
):
for old, new in [["prompt", "input"], ["completion", "output"]]:
dataset = dataset.rename_column(old, new)
elif dataset_format == "hh-rlhf" or (
dataset_format is None and args.dataset == "hh-rlhf"
):
dataset = dataset.map(lambda x: {"input": "", "output": x["chosen"]})
elif dataset_format == "oasst1" or (
dataset_format is None and args.dataset == "oasst1"
):
dataset = dataset.map(
lambda x: {
"input": "",
"output": x["text"],
}
)
# Remove unused columns.
dataset = dataset.remove_columns(
[
col
for col in dataset.column_names["train"]
if col not in ["input", "output"]
]
)
return dataset
# Load dataset.
dataset = load_data(args.dataset)
dataset = format_dataset(dataset, args.dataset_format, args.meta_prompt_pattern)
# Split train/eval, reduce size
if args.do_eval or args.do_predict:
if "eval" in dataset:
eval_dataset = dataset["eval"]
else:
print(
"Splitting train dataset in train and validation according to `eval_dataset_size`"
)
dataset = dataset["train"].train_test_split(
test_size=args.eval_dataset_size, shuffle=True, seed=42
)
eval_dataset = dataset["test"]
if (
args.max_eval_samples is not None
and len(eval_dataset) > args.max_eval_samples
):
eval_dataset = eval_dataset.select(range(args.max_eval_samples))
if args.group_by_length:
eval_dataset = eval_dataset.map(
lambda x: {"length": len(x["input"]) + len(x["output"])}
)
if args.do_train:
train_dataset = dataset["train"]
if (
args.max_train_samples is not None
and len(train_dataset) > args.max_train_samples
):
train_dataset = train_dataset.select(range(args.max_train_samples))
if args.group_by_length:
train_dataset = train_dataset.map(
lambda x: {"length": len(x["input"]) + len(x["output"])}
)
data_collator = DataCollatorForCausalLM(
left_truncated_tokenizer=left_truncated_tokenizer,
tokenizer=tokenizer,
source_max_len=args.source_max_len,
target_max_len=args.target_max_len,
train_on_source=args.train_on_source,
predict_with_generate=args.predict_with_generate,
add_eos_to_target=args.add_eos_to_target,
)
return dict(
train_dataset=train_dataset if args.do_train else None,
eval_dataset=eval_dataset if args.do_eval else None,
predict_dataset=eval_dataset if args.do_predict else None,
data_collator=data_collator,
)