-
Notifications
You must be signed in to change notification settings - Fork 1
/
datautils.py
292 lines (243 loc) · 9.76 KB
/
datautils.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
import pdb
import random
import numpy as np
import torch
from transformers import AutoTokenizer
from datasets import load_dataset, load_from_disk
def set_seed(seed):
np.random.seed(seed)
torch.random.manual_seed(seed)
def get_pile(nsamples, seed, seqlen, model):
print("get_pile")
traindata = load_dataset(
"json",
data_files="/cpfs01/user/chenmengzhao/prompt_quantization/val.jsonl.zst",
split="train",
)
tokenizer = AutoTokenizer.from_pretrained(model, use_fast=False)
trainenc = tokenizer("\n\n".join(traindata["text"][:1000]), return_tensors="pt")
random.seed(seed)
trainloader = []
for _ in range(nsamples):
i = random.randint(0, trainenc.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
inp = trainenc.input_ids[:, i:j]
tar = inp.clone()
tar[:, :-1] = -100
trainloader.append((inp, tar))
return trainloader, None
def get_wikitext2(nsamples, seed, seqlen, model):
print("get_wikitext2")
print("load data from network")
traindata = load_dataset("wikitext", "wikitext-2-raw-v1", split="train")
testdata = load_dataset("wikitext", "wikitext-2-raw-v1", split="test")
tokenizer = AutoTokenizer.from_pretrained(model, use_fast=False)
trainenc = tokenizer("\n\n".join(traindata["text"]), return_tensors="pt")
testenc = tokenizer("\n\n".join(testdata["text"]), return_tensors="pt")
random.seed(seed)
trainloader = []
for _ in range(nsamples):
i = random.randint(0, trainenc.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
inp = trainenc.input_ids[:, i:j]
tar = inp.clone()
tar[:, :-1] = -100
trainloader.append((inp, tar))
return trainloader, testenc
def get_ptb(nsamples, seed, seqlen, model):
print("get_ptb")
print("load data from network")
traindata = load_dataset("ptb_text_only", "penn_treebank", split="train")
valdata = load_dataset("ptb_text_only", "penn_treebank", split="validation")
tokenizer = AutoTokenizer.from_pretrained(model, use_fast=False)
trainenc = tokenizer("\n\n".join(traindata["sentence"]), return_tensors="pt")
testenc = tokenizer("\n\n".join(valdata["sentence"]), return_tensors="pt")
random.seed(seed)
trainloader = []
for _ in range(nsamples):
i = random.randint(0, trainenc.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
inp = trainenc.input_ids[:, i:j]
tar = inp.clone()
tar[:, :-1] = -100
trainloader.append((inp, tar))
return trainloader, testenc
def get_c4(nsamples, seed, seqlen, model):
print("get_c4")
print("load data from network")
traindata = load_dataset(
"allenai/c4",
data_files={"train": "en/c4-train.00000-of-01024.json.gz"},
split="train",
)
valdata = load_dataset(
"allenai/c4",
data_files={"validation": "en/c4-validation.00000-of-00008.json.gz"},
split="validation",
)
tokenizer = AutoTokenizer.from_pretrained(model, use_fast=False)
random.seed(seed)
trainloader = []
for _ in range(nsamples):
while True:
i = random.randint(0, len(traindata) - 1)
trainenc = tokenizer(traindata[i]["text"], return_tensors="pt")
if trainenc.input_ids.shape[1] > seqlen:
break
i = random.randint(0, trainenc.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
inp = trainenc.input_ids[:, i:j]
tar = inp.clone()
tar[:, :-1] = -100
trainloader.append((inp, tar))
random.seed(0)
valenc = []
for _ in range(256):
while True:
i = random.randint(0, len(valdata) - 1)
tmp = tokenizer(valdata[i]["text"], return_tensors="pt")
if tmp.input_ids.shape[1] >= seqlen:
break
i = random.randint(0, tmp.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
valenc.append(tmp.input_ids[:, i:j])
valenc = torch.hstack(valenc)
return trainloader, valenc
def get_ptb_new(nsamples, seed, seqlen, model):
print("get_ptb_new")
print("load data from network")
traindata = load_dataset("ptb_text_only", "penn_treebank", split="train")
testdata = load_dataset("ptb_text_only", "penn_treebank", split="test")
tokenizer = AutoTokenizer.from_pretrained(model, use_fast=False)
trainenc = tokenizer(" ".join(traindata["sentence"]), return_tensors="pt")
testenc = tokenizer(" ".join(testdata["sentence"]), return_tensors="pt")
random.seed(seed)
trainloader = []
for _ in range(nsamples):
i = random.randint(0, trainenc.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
inp = trainenc.input_ids[:, i:j]
tar = inp.clone()
tar[:, :-1] = -100
trainloader.append((inp, tar))
return trainloader, testenc
def get_c4_new(nsamples, seed, seqlen, model):
print("get_c4_new")
print("load data from network")
traindata = load_dataset(
"allenai/c4",
"allenai--c4",
data_files={"train": "en/c4-train.00000-of-01024.json.gz"},
split="train",
)
valdata = load_dataset(
"allenai/c4",
"allenai--c4",
data_files={"validation": "en/c4-validation.00000-of-00008.json.gz"},
split="validation",
)
tokenizer = AutoTokenizer.from_pretrained(model, use_fast=False)
random.seed(seed)
trainloader = []
for _ in range(nsamples):
while True:
i = random.randint(0, len(traindata) - 1)
trainenc = tokenizer(traindata[i]["text"], return_tensors="pt")
if trainenc.input_ids.shape[1] >= seqlen:
break
i = random.randint(0, trainenc.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
inp = trainenc.input_ids[:, i:j]
tar = inp.clone()
tar[:, :-1] = -100
trainloader.append((inp, tar))
valenc = tokenizer(" ".join(valdata[:1100]["text"]), return_tensors="pt")
valenc = valenc.input_ids[:, : (256 * seqlen)]
return trainloader, valenc
def get_loaders(name, nsamples=128, seed=0, seqlen=2048, model=""):
if "wikitext2" in name:
return get_wikitext2(nsamples, seed, seqlen, model)
if "pile" in name:
return get_pile(nsamples, seed, seqlen, model)
if "ptb" in name:
if "new" in name:
return get_ptb_new(nsamples, seed, seqlen, model)
return get_ptb(nsamples, seed, seqlen, model)
if "c4" in name:
if "new" in name:
return get_c4_new(nsamples, seed, seqlen, model)
return get_c4(nsamples, seed, seqlen, model)
if "mix" in name:
wiki_train, wiki_val = get_wikitext2(nsamples // 3, seed, seqlen, model)
ptb_train, ptb_val = get_ptb(nsamples // 3, seed, seqlen, model)
c4_train, c4_val = get_c4(
nsamples - nsamples // 3 - nsamples // 3, seed, seqlen, model
)
train = wiki_train + ptb_train + c4_train
val = None
return train, val
class CustomJsonDataset(torch.utils.data.IterableDataset):
def __init__(self, dataset, tokenizer, block_size=1024):
raw_data = dataset
self.tokenizer = tokenizer
self.block_size = block_size
tokenized_datasets = []
for d in raw_data:
tokenized_datasets.append(self.tokenize_function(d))
grouped_dataset = self.group_texts(tokenized_datasets)
self.input_ids = grouped_dataset["input_ids"]
self.labels = grouped_dataset["labels"]
self.data = [
dict(input_ids=self.input_ids[i], labels=self.labels[i])
for i in range(len(self.input_ids))
]
def __len__(self):
return len(self.input_ids)
def __getitem__(self, i):
return dict(input_ids=self.input_ids[i], labels=self.labels[i])
def __iter__(self):
return iter(self.data)
def tokenize_function(self, examples):
return self.tokenizer(examples["text"])
def group_texts(self, examples):
# Concatenate all texts.
# Initialize an empty dictionary
concatenated_examples = {}
# Loop through the list of dictionaries
for d in examples:
# Loop through the keys in each dictionary
for key in d.keys():
# If the key is not already a key in the dict_of_lists, create a new list
if key not in concatenated_examples:
concatenated_examples[key] = []
# Append the value to the list associated with the key in dict_of_lists
concatenated_examples[key].extend(d[key])
total_length = len(concatenated_examples["input_ids"])
# 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 >= self.block_size:
total_length = (total_length // self.block_size) * self.block_size
# Split by chunks of max_len.
result = {
k: [
t[i : i + self.block_size]
for i in range(0, total_length, self.block_size)
]
for k, t in concatenated_examples.items()
}
result["labels"] = result["input_ids"].copy()
return result
class CustomDataset(torch.utils.data.IterableDataset):
def __init__(self, dataset):
self.input_ids = [dataset[i][0].squeeze() for i in range(len(dataset))]
self.labels = [dataset[i][1].squeeze() for i in range(len(dataset))]
self.data = [
dict(input_ids=self.input_ids[i], labels=self.labels[i])
for i in range(len(self.input_ids))
]
def __len__(self):
return len(self.input_ids)
def __getitem__(self, i):
return dict(input_ids=self.input_ids[i], labels=self.labels[i])
def __iter__(self):
return iter(self.data)