-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdata_utils.py
530 lines (478 loc) · 23.2 KB
/
data_utils.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
"""
Part of the code was adopted from https://github.com/microsoft/Megatron-DeepSpeed/blob/main/megatron/data/dataset_utils.py
"""
import torch
from torch.utils.data import Dataset, Subset, ConcatDataset
from torch.nn.utils.rnn import pad_sequence
import torch.nn.functional as F
from datasets import load_dataset
import numpy as np
import os
import hashlib
from itertools import chain
from dschat.utils.data import raw_datasets
from deepspeed.accelerator import get_accelerator
def get_raw_dataset(dataset_name, output_path, seed, local_rank):
if "Dahoas/rm-static" in dataset_name:
return raw_datasets.DahoasRmstaticDataset(output_path, seed,
local_rank, dataset_name)
elif "Dahoas/full-hh-rlhf" in dataset_name:
return raw_datasets.DahoasFullhhrlhfDataset(output_path, seed,
local_rank, dataset_name)
elif "Dahoas/synthetic-instruct-gptj-pairwise" in dataset_name:
return raw_datasets.DahoasSyntheticinstructgptjpairwiseDataset(
output_path, seed, local_rank, dataset_name)
elif "yitingxie/rlhf-reward-datasets" in dataset_name:
return raw_datasets.YitingxieRlhfrewarddatasetsDataset(
output_path, seed, local_rank, dataset_name)
elif "openai/webgpt_comparisons" in dataset_name:
return raw_datasets.OpenaiWebgptcomparisonsDataset(
output_path, seed, local_rank, dataset_name)
elif "stanfordnlp/SHP" in dataset_name:
return raw_datasets.StanfordnlpSHPDataset(output_path, seed,
local_rank, dataset_name)
elif "pvduy/sharegpt_alpaca_oa_vicuna_format" in dataset_name:
return raw_datasets.PvduySharegptalpacaoavicunaformatDataset(
output_path, seed, local_rank, dataset_name)
elif "wangrui6/Zhihu-KOL" in dataset_name:
return raw_datasets.Wangrui6ZhihuKOLDataset(output_path, seed,
local_rank, dataset_name)
elif "Cohere/miracl-zh-queries-22-12" in dataset_name:
return raw_datasets.CohereMiraclzhqueries2212Dataset(
output_path, seed, local_rank, dataset_name)
elif "Hello-SimpleAI/HC3-Chinese" in dataset_name:
return raw_datasets.HelloSimpleAIHC3ChineseDataset(
output_path, seed, local_rank, dataset_name)
elif "mkqa-Chinese" in dataset_name:
return raw_datasets.MkqaChineseDataset(output_path, seed, local_rank,
"mkqa")
elif "mkqa-Japanese" in dataset_name:
return raw_datasets.MkqaJapaneseDataset(output_path, seed, local_rank,
"mkqa")
elif "Cohere/miracl-ja-queries-22-12" in dataset_name:
return raw_datasets.CohereMiracljaqueries2212Dataset(
output_path, seed, local_rank, dataset_name)
elif "lmqg/qg_jaquad" in dataset_name:
return raw_datasets.LmqgQgjaquadDataset(output_path, seed, local_rank,
dataset_name)
elif "lmqg/qag_jaquad" in dataset_name:
return raw_datasets.LmqgQagjaquadDataset(output_path, seed, local_rank,
dataset_name)
elif "local/jsonfile" in dataset_name:
chat_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.path.pardir,
os.path.pardir, os.path.pardir))
if not (os.path.isfile(chat_path + '/data/train.json')
and os.path.isfile(chat_path + '/data/eval.json')):
raise RuntimeError(
f"Please check both the train.json and eval.json files in your applications/DeepSpeed-Chat/data directory."
)
return raw_datasets.LocalJsonFileDataset(output_path, seed, local_rank,
dataset_name, chat_path)
else:
raise RuntimeError(
f"We do not have configs for dataset {dataset_name}, but you can add it by yourself in raw_datasets.py."
)
def get_shuffle_idx(seed, size):
np_rng = np.random.RandomState(seed=seed)
dtype_ = np.uint32
if size >= (np.iinfo(np.uint32).max - 1):
dtype_ = np.int64
shuffle_idx = np.arange(start=0, stop=size, step=1, dtype=dtype_)
np_rng.shuffle(shuffle_idx)
return shuffle_idx
def get_raw_dataset_split_index(local_rank,
output_path,
dataset_name,
seed,
split_name,
data_split,
split_index,
data_size,
rebuild=False):
index_file_name = f"{output_path}/{dataset_name}_seed{seed}_{split_name}_{data_split}_{split_index}.npy"
# reindex each time when using local jsonfile since it's more likely to get modified
if rebuild or (not os.path.isfile(index_file_name)) or (dataset_name
== 'jsonfile'):
splits = [float(s) for s in data_split.split(',')]
splits_sum = sum(splits)
splits = [split / splits_sum for split in splits]
splits_index = [0]
for index, split in enumerate(splits):
splits_index.append(splits_index[index] +
int(round(split * float(data_size))))
diff = splits_index[-1] - data_size
for index in range(1, len(splits_index)):
splits_index[index] -= diff
assert splits_index[-1] == data_size
shuffle_idx = get_shuffle_idx(seed, data_size)
for split_i in range(len(splits)):
shuffle_idx_split_file_name = f"{output_path}/{dataset_name}_seed{seed}_{split_name}_{data_split}_{split_i}.npy"
shuffle_idx_split = shuffle_idx[
splits_index[split_i]:splits_index[split_i + 1]]
np.save(shuffle_idx_split_file_name,
shuffle_idx_split,
allow_pickle=True)
index = np.load(index_file_name, allow_pickle=True)
return index.tolist()
class PromptDataset(Dataset):
def __init__(self, prompt_dataset, chosen_dataset, reject_dataset,
pad_token_id, train_phase, tokenizer, max_seq_len) -> None:
super().__init__()
self.prompt_dataset = prompt_dataset
self.chosen_dataset = chosen_dataset
self.reject_dataset = reject_dataset
self.pad_token_id = pad_token_id
self.train_phase = train_phase
self.tokenizer = tokenizer
self.max_seq_len = max_seq_len
def __len__(self):
length = len(self.chosen_dataset)
if self.train_phase == 3:
length = len(self.prompt_dataset)
return length
def __getitem__(self, idx):
if self.train_phase == 1:
sentence = self.chosen_dataset[idx]
tokenized_sentence = self.tokenizer(sentence,
max_length=self.max_seq_len,
padding="max_length",
truncation=True,
return_tensors="pt")
tokenized_sentence["input_ids"] = tokenized_sentence["input_ids"].squeeze(0)
tokenized_sentence["attention_mask"] = tokenized_sentence["attention_mask"].squeeze(0)
return {
"input_ids": tokenized_sentence["input_ids"],
"attention_mask": tokenized_sentence["attention_mask"],
"labels": tokenized_sentence["input_ids"]
}
elif self.train_phase == 2:
chosen_sentence = self.chosen_dataset[idx]
reject_sentence = self.reject_dataset[idx]
chosen_token = self.tokenizer(chosen_sentence,
max_length=self.max_seq_len,
padding="max_length",
truncation=True,
return_tensors="pt")
reject_token = self.tokenizer(reject_sentence,
max_length=self.max_seq_len,
padding="max_length",
truncation=True,
return_tensors="pt")
return chosen_token["input_ids"], chosen_token["attention_mask"], \
reject_token["input_ids"], reject_token["attention_mask"]
elif self.train_phase == 3:
prompt_sentence = self.prompt_dataset[idx]
prompt_token = self.tokenizer(prompt_sentence, return_tensors="pt")
for key_word in ["input_ids", "attention_mask"]:
prompt_token[key_word] = prompt_token[
key_word].squeeze(0).flip(0)
return prompt_token["input_ids"], prompt_token["attention_mask"], \
self.pad_token_id
def create_dataset_split(current_dataset, raw_dataset, train_phase, tokenizer,
end_of_conversation_token, max_seq_len):
prompt_dataset = []
chosen_dataset = []
reject_dataset = []
if train_phase == 1:
for i, tmp_data in enumerate(current_dataset):
# tokenize the text
chosen_sentence = raw_dataset.get_prompt_and_chosen(
tmp_data) # the accept response
if chosen_sentence is not None:
chosen_sentence += end_of_conversation_token
chosen_dataset.append(chosen_sentence)
print(
f'Creating dataset {raw_dataset.dataset_name_clean} for {train_phase=} size={len(chosen_dataset)}'
)
elif train_phase == 2:
for i, tmp_data in enumerate(current_dataset):
# tokenize the text
chosen_sentence = raw_dataset.get_prompt_and_chosen(
tmp_data) # the accept response
reject_sentence = raw_dataset.get_prompt_and_rejected(
tmp_data) # the accept response
if chosen_sentence is not None and reject_sentence is not None:
chosen_sentence += end_of_conversation_token # the accept response
reject_sentence += end_of_conversation_token
chosen_dataset.append(chosen_sentence)
reject_dataset.append(reject_sentence)
print(
f'Creating dataset {raw_dataset.dataset_name_clean} for {train_phase=} size={len(chosen_dataset)}'
)
elif train_phase == 3:
filtered = 0
for i, tmp_data in enumerate(current_dataset):
# tokenize the text
prompt = raw_dataset.get_prompt(tmp_data)
if prompt is not None:
prompt_token = tokenizer(prompt, return_tensors="pt")
if prompt_token["input_ids"].size()[-1] <= max_seq_len:
prompt_dataset.append(prompt)
else:
filtered += 1
print(f'Creating dataset {raw_dataset.dataset_name_clean} '
f'for {train_phase=} size={len(prompt_dataset)} {filtered=}')
return PromptDataset(prompt_dataset, chosen_dataset, reject_dataset,
tokenizer.pad_token_id, train_phase, tokenizer, max_seq_len)
def create_dataset(local_rank, dataset_name, data_split, output_path,
train_phase, seed, tokenizer, end_of_conversation_token,
max_seq_len, rebuild):
raw_dataset = get_raw_dataset(dataset_name, output_path, seed, local_rank)
train_dataset = raw_dataset.get_train_data()
train_index = get_raw_dataset_split_index(local_rank, output_path,
raw_dataset.dataset_name_clean,
seed, "train", data_split,
train_phase - 1,
len(train_dataset), rebuild)
train_dataset = Subset(train_dataset, train_index)
train_dataset = create_dataset_split(train_dataset, raw_dataset,
train_phase, tokenizer,
end_of_conversation_token,
max_seq_len)
eval_dataset = raw_dataset.get_eval_data()
eval_index = get_raw_dataset_split_index(local_rank, output_path,
raw_dataset.dataset_name_clean,
seed, "eval",
data_split, train_phase - 1,
len(eval_dataset), rebuild)
eval_dataset = Subset(eval_dataset, eval_index)
eval_dataset = create_dataset_split(eval_dataset, raw_dataset, train_phase,
tokenizer, end_of_conversation_token,
max_seq_len)
return train_dataset, eval_dataset
def create_prompt_dataset(local_rank,
data_path,
data_split,
output_path,
train_phase,
seed,
tokenizer,
max_seq_len,
end_of_conversation_token="<|endoftext|>",
sft_only_data_path=[],
reload=False):
"""
Creates the prompt dataset
"""
os.makedirs(output_path, exist_ok=True)
fname = "_".join(data_path)
sft_cache_key = "_".join(sft_only_data_path)
tokenizer_name = tokenizer.init_kwargs["name_or_path"].replace("/", "_")
fname = f"{fname}_split{data_split}_phase{train_phase}_seed{seed}_tokenizer{tokenizer_name}_seqlen{max_seq_len}_sft{sft_cache_key}"
fname = "_".join(fname.split("/"))
fname = hashlib.sha256(fname.encode()).hexdigest(
) # hash the file name to avoid too long file name
train_fname = f"{output_path}/traindata_{fname}.pt"
eval_fname = f"{output_path}/evaldata_{fname}.pt"
cache_found = os.path.isfile(train_fname) and os.path.isfile(eval_fname)
buf_create_cache = torch.ByteTensor([not cache_found]).to(
get_accelerator().current_device_name())
torch.distributed.all_reduce(buf_create_cache)
if local_rank <= 0 and (buf_create_cache.item() != 0 or reload):
print(f'Creating prompt dataset {data_path}, {reload=}')
if len(data_path) == 1: # Single dataset.
train_dataset, eval_dataset = create_dataset(
local_rank,
data_path[0],
data_split,
output_path,
train_phase,
seed,
tokenizer,
end_of_conversation_token,
max_seq_len,
rebuild=reload)
else: # Blending datasets.
train_datasets = []
eval_datasets = []
train_size = 0
eval_size = 0
for d_path in data_path:
train_dataset, eval_dataset = create_dataset(
local_rank,
d_path,
data_split,
output_path,
train_phase,
seed,
tokenizer,
end_of_conversation_token,
max_seq_len,
rebuild=reload)
train_datasets.append(train_dataset)
eval_datasets.append(eval_dataset)
train_size += len(train_dataset)
eval_size += len(eval_dataset)
train_dataset = ConcatDataset(train_datasets)
shuffle_idx = get_shuffle_idx(seed, train_size)
train_dataset = Subset(train_dataset, shuffle_idx.tolist())
eval_dataset = ConcatDataset(eval_datasets)
shuffle_idx = get_shuffle_idx(seed, eval_size)
eval_dataset = Subset(eval_dataset, shuffle_idx.tolist())
# Append the SFT-only dataset if it exists, and current phase is 1(SFT).
if train_phase == 1 and sft_only_data_path:
sft_train_datasets = []
sft_eval_datasets = []
sft_train_size = 0
sft_eval_size = 0
for sft_path in sft_only_data_path:
sft_train_dataset, sft_eval_dataset = create_dataset(
local_rank,
sft_path,
"10,0,0",
output_path,
train_phase,
seed,
tokenizer,
end_of_conversation_token,
max_seq_len,
rebuild=reload)
sft_train_datasets.append(sft_train_dataset)
sft_eval_datasets.append(sft_eval_dataset)
sft_train_size += len(sft_train_dataset)
sft_eval_size += len(sft_eval_dataset)
if sft_train_datasets: # Check if sft_train_datasets is not empty
sft_train_dataset = ConcatDataset(sft_train_datasets)
train_dataset = ConcatDataset(
[train_dataset, sft_train_dataset])
shuffle_idx = get_shuffle_idx(seed, len(train_dataset))
train_dataset = Subset(train_dataset, shuffle_idx.tolist())
if sft_eval_datasets: # Check if sft_eval_datasets is not empty
sft_eval_dataset = ConcatDataset(sft_eval_datasets)
eval_dataset = ConcatDataset([eval_dataset, sft_eval_dataset])
shuffle_idx = get_shuffle_idx(seed, len(eval_dataset))
eval_dataset = Subset(eval_dataset, shuffle_idx.tolist())
torch.save(train_dataset, train_fname)
torch.save(eval_dataset, eval_fname)
torch.distributed.barrier()
return torch.load(train_fname), torch.load(eval_fname)
class DataCollatorReward:
def __call__(self, data):
batch = {}
batch["input_ids"] = torch.cat([f[0]
for f in data] + [f[2] for f in data],
dim=0)
batch["attention_mask"] = torch.cat([f[1] for f in data] +
[f[3] for f in data],
dim=0)
return batch
class DataCollatorRLHF:
def __init__(self, max_token_len, inference_tp_size):
self.max_token_len = max_token_len
self.inference_tp_size = inference_tp_size
def __call__(self, data):
batch = {}
pad_token_id = data[-1][-1]
prompt = pad_sequence([f[0] for f in data],
padding_value=pad_token_id,
batch_first=True)
prompt_mask = pad_sequence([f[1] for f in data],
padding_value=0,
batch_first=True)
### make sure the final ouput is a seqence of 2**?
length = prompt.size()[-1]
pad_length = self.max_token_len - length
if pad_length > 0:
batch["prompt"] = F.pad(prompt,
pad=(0, pad_length),
mode='constant',
value=pad_token_id)
batch["prompt_att_mask"] = F.pad(prompt_mask,
pad=(0, pad_length),
mode='constant',
value=0)
else:
batch["prompt"] = prompt
batch["prompt_att_mask"] = prompt_mask
batch["prompt"] = batch["prompt"].flip(1)
batch["prompt_att_mask"] = batch["prompt_att_mask"].flip(1)
return batch
def get_unsupervised_data(args, tokenizer):
unsupervised_raw_datasets = load_dataset(
args.unsupervised_dataset_name, args.unsupervised_dataset_config_name)
column_names = unsupervised_raw_datasets["train"].column_names
text_column_name = "text" if "text" in column_names else column_names[0]
def tokenize_function(examples):
return tokenizer(examples[text_column_name])
tokenized_datasets = unsupervised_raw_datasets.map(
tokenize_function,
batched=True,
num_proc=args.preprocessing_num_workers,
remove_columns=column_names,
load_from_cache_file=True,
desc="Running tokenizer on dataset",
)
block_size = args.max_prompt_seq_len + args.max_answer_seq_len
def group_texts(examples):
# Concatenate all texts.
concatenated_examples = {
k: list(chain(*examples[k]))
for k in examples.keys()
}
total_length = len(concatenated_examples[list(examples.keys())[0]])
# We drop the small remainder, we could add padding if the model supported it instead of this drop, you can
# customize this part to your needs.
if total_length >= block_size:
total_length = (total_length // block_size) * block_size
# Split by chunks of max_len.
result = {
k:
[t[i:i + block_size] for i in range(0, total_length, block_size)]
for k, t in concatenated_examples.items()
}
result["labels"] = result["input_ids"].copy()
return result
lm_datasets = tokenized_datasets.map(
group_texts,
batched=True,
num_proc=args.preprocessing_num_workers,
load_from_cache_file=True,
desc=f"Grouping texts in chunks of {block_size}",
)
train_dataset = lm_datasets["train"]
return train_dataset
class MiniDataset:
def __init__(self, max_size, small_batch_size):
self.dataset = []
self.max_size = max_size
self.small_batch_size = small_batch_size
def seperate(self):
small_dataset = []
for large_batch in self.dataset:
if type(large_batch) == list or type(large_batch) == tuple:
large_size = len(large_batch[0])
elif type(large_batch) == dict:
large_size = len(large_batch[list(large_batch.keys())[0]])
else:
large_size = len(large_batch)
for i in range(0, large_size, self.small_batch_size):
if type(large_batch) == list or type(large_batch) == tuple:
small_dataset.append(
[x[i:i + self.small_batch_size] for x in large_batch])
elif type(large_batch) == dict:
small_dataset.append({
k: v[i:i + self.small_batch_size]
for k, v in large_batch.items()
})
else:
small_dataset.append(large_batch[i:i +
self.small_batch_size])
self.free()
return small_dataset
def add(self, data):
if len(self.dataset) < self.max_size:
self.dataset.append(data)
if len(self.dataset) == self.max_size:
return self.seperate()
else:
return None
else:
raise ValueError(
"The dataset is full but we did not stop it. There is a bug in the code."
)
def free(self):
self.dataset = []