forked from NomosArtificial/static-eval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
153 lines (112 loc) · 6.12 KB
/
data.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
import torch
from datasets import load_dataset
from torch.utils.data import DataLoader
from transformers import DataCollatorForLanguageModeling
from tokenization import build_dataloaders
def load_data(config, tokenizer, split="train[:1%]", streaming=True):
dataset = load_dataset(build_dataloaders(config), cache_dir= config["cache_dir"], split=split, streaming=streaming)
test_size = 0.02
if streaming:
train_dataset = dataset.filter(lambda _, idx: idx % 98 != 0 and idx % 99 != 0, with_indices=True)
val_dataset = dataset.filter(lambda _, idx: idx % 98 == 0 or idx % 99 == 0, with_indices=True)
train_dataset = train_dataset.shuffle(buffer_size=10_000, seed=config["train_args"]["seed"])
val_dataset = val_dataset.shuffle(buffer_size=10_000, seed=config["train_args"]["seed"])
else:
split_dataset = dataset.train_test_split(test_size=test_size, shuffle=True, seed=config["train_args"]["seed"])
train_dataset, val_dataset = split_dataset["train"], split_dataset["test"]
max_length = config["max_length"]
if streaming:
ds_kwargs = {}
else:
ds_kwargs = {"num_proc": config["train_args"]["num_proc"]}
print(train_dataset[2]["text"])
train_dataset = train_dataset.map(lambda ele: tokenizer(ele["text"],
truncation=True,
padding="max_length",
max_length=max_length),
batched=True,
**ds_kwargs)
val_dataset = val_dataset.map(lambda ele: tokenizer(ele["text"],
truncation=True,
padding="max_length",
max_length=max_length),
batched=True,
**ds_kwargs)
train_dataset = train_dataset.with_format("torch")
train_dl = DataLoader(
train_dataset,
collate_fn=DataCollatorForLanguageModeling(
tokenizer, mlm=False,
),
batch_size=config["train_args"]["per_device_train_batch_size"],
)
val_dataset = val_dataset.with_format("torch")
val_dl = DataLoader(
val_dataset,
collate_fn=DataCollatorForLanguageModeling(
tokenizer, mlm=False,
),
batch_size=config["train_args"]["per_device_eval_batch_size"],
)
return train_dl, val_dl
def tokenize_pairwise_rewards(examples, tokenizer, max_length):
tokenized = {"chosen_input_ids": [], "chosen_attention_mask": [], "rejected_input_ids": [], "rejected_attention_mask": []}
for prompt, chosen, rejected in zip(examples["prompt"], examples["positive"], examples["negative"]):
chosen_tokens = tokenizer(prompt + " " + chosen + tokenizer.eos_token,
truncation=True,
padding="max_length",
max_length=max_length,
return_tensors="pt")
rejected_tokens = tokenizer(prompt + " " + rejected + tokenizer.eos_token,
truncation=True,
padding="max_length",
max_length=max_length,
return_tensors="pt")
tokenized["chosen_input_ids"].append(chosen_tokens["input_ids"])
tokenized["chosen_attention_mask"].append(chosen_tokens["attention_mask"])
tokenized["rejected_input_ids"].append(rejected_tokens["input_ids"])
tokenized["rejected_attention_mask"].append(rejected_tokens["attention_mask"])
return tokenized
def pairwise_collator(data):
return {
"chosen_input_ids": torch.cat([t["chosen_input_ids"] for t in data]),
"chosen_attention_mask": torch.cat([t["chosen_attention_mask"] for t in data]),
"rejected_input_ids": torch.cat([t["rejected_input_ids"] for t in data]),
"rejected_attention_mask": torch.cat([t["rejected_attention_mask"] for t in data])
}
def load_pairwise_reward_data(config, tokenizer, split="train", streaming=True):
dataset = load_dataset(config["data_path"], split=split, streaming=streaming)
max_length = config["max_length"]
if streaming:
ds_kwargs = {}
else:
ds_kwargs = {"num_proc": config["train_args"]["num_proc"]}
split_dataset = dataset.train_test_split(test_size=0.2, shuffle=True, seed=config["train_args"]["seed"])
train_dataset, val_dataset = split_dataset["train"], split_dataset["test"]
train_dataset = train_dataset.map(lambda ele: tokenize_pairwise_rewards(ele, tokenizer, max_length),
batched=True,
remove_columns=["prompt", "positive", "negative", "timestep", "key"],
**ds_kwargs
)
val_dataset = val_dataset.map(lambda ele: tokenize_pairwise_rewards(ele, tokenizer, max_length),
batched=True,
remove_columns=["prompt", "positive", "negative", "timestep", "key"],
**ds_kwargs
)
train_dataset = train_dataset.with_format("torch")
train_dl = DataLoader(
train_dataset,
collate_fn=pairwise_collator,
batch_size=config["train_args"]["per_device_train_batch_size"],
# have to drop last as we assume same batch size!
drop_last=True
)
val_dataset = val_dataset.with_format("torch")
val_dl = DataLoader(
val_dataset,
collate_fn=pairwise_collator,
batch_size=config["train_args"]["per_device_eval_batch_size"],
# have to drop last as we assume same batch size!
drop_last=True
)
return train_dl, val_dl