-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdatautils_block.py
276 lines (245 loc) · 10.3 KB
/
datautils_block.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
from transformers import AutoTokenizer
from datasets import load_dataset
import numpy as np
import torch
import random
from tqdm import tqdm
import torch.nn as nn
import torch
from torch.utils.data import Dataset
import os
def get_wikitext2(tokenizer, train_size, val_size, seed, seqlen, test_only):
print("get_wikitext2")
traindata = load_dataset('wikitext', 'wikitext-2-raw-v1', split='train')
testdata = load_dataset('wikitext', 'wikitext-2-raw-v1', split='test')
testenc = tokenizer("\n\n".join(testdata['text']), return_tensors='pt')
if test_only:
return testenc
trainenc = tokenizer("\n\n".join(traindata['text']), return_tensors='pt')
random.seed(seed)
trainloader = []
val_sample_ratio = 0.9 # sample train from [0:0.9] and val from [0.9:1.0] to avoid overlap
for _ in range(train_size):
i = random.randint(0, int(trainenc.input_ids.shape[1]*val_sample_ratio) - seqlen - 1)
j = i + seqlen
inp = trainenc.input_ids[:, i:j]
tar = inp.clone()
tar[:, :-1] = -100
trainloader.append((inp, tar))
valloader = []
for _ in range(val_size):
i = random.randint(int(trainenc.input_ids.shape[1]*val_sample_ratio) - seqlen - 1, trainenc.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
inp = trainenc.input_ids[:, i:j]
tar = inp.clone()
tar[:, :-1] = -100
valloader.append((inp, tar))
return trainloader, valloader
def get_c4(tokenizer, train_size, val_size, seed, seqlen, test_only):
print("get_c4")
try:
# set local path for faster loading
traindata = load_dataset("arrow",
data_files={
"train": "/cpfs01/user/chenmengzhao/huggingface/datasets/allenai___json/allenai--c4-6fbe877195f42de5/0.0.0/0f7e3662623656454fcd2b650f34e886a7db4b9104504885bd462096cc7a9f51/json-train-00000-of-00002.arrow",
"validation": "/cpfs01/user/chenmengzhao/huggingface/datasets/allenai___json/allenai--c4-efc3d4f4606f44bd/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/json-validation.arrow",
},split='train'
)
valdata = load_dataset("arrow",
data_files={
"validation": "/cpfs01/user/chenmengzhao/huggingface/datasets/allenai___json/allenai--c4-efc3d4f4606f44bd/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/json-validation.arrow",
},split='validation'
)
except:
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'
)
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)
if test_only:
return valenc
random.seed(seed)
trainloader = []
val_sample_ratio = 0.9 # sample train from [0:0.9] and val from [0.9:1.0] to avoid overlap
for _ in range(train_size):
while True:
i = random.randint(0, int(len(traindata)*val_sample_ratio) - 1)
trainenc = tokenizer(traindata[i]['text'], return_tensors='pt')
if trainenc.input_ids.shape[1] >= seqlen+1:
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))
valloader = []
for _ in range(val_size):
while True:
i = random.randint(int(len(traindata)*val_sample_ratio),len(traindata)-1)
trainenc = tokenizer(traindata[i]['text'], return_tensors='pt')
if trainenc.input_ids.shape[1] >= seqlen+1:
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
valloader.append((inp, tar))
return trainloader, valloader
def get_redpajama(tokenizer, train_size, val_size, seed, seqlen):
print("get_redpajama")
try:
loacal_dataset = "/cpfs01/user/chenmengzhao/huggingface/datasets/togethercomputer___red_pajama-data-1_t-sample"
traindata = load_dataset(loacal_dataset,split='train')
except:
traindata = load_dataset("togethercomputer/RedPajama-Data-1T-Sample",split='train')
random.seed(seed)
traindata = traindata.shuffle(seed=seed)
trainloader = []
val_sample_ratio = 0.9
for _ in range(train_size):
while True:
i = random.randint(0, int(len(traindata)*val_sample_ratio) - 1)
trainenc = tokenizer(traindata[i]['text'], return_tensors='pt')
if trainenc.input_ids.shape[1] >= seqlen+1:
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))
valloader = []
for _ in range(val_size):
while True:
i = random.randint(int(len(traindata)*val_sample_ratio),len(traindata)-1)
trainenc = tokenizer(traindata[i]['text'], return_tensors='pt')
if trainenc.input_ids.shape[1] >= seqlen+1:
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
valloader.append((inp, tar))
return trainloader, valloader
def get_loaders(
name, tokenizer, train_size=128, val_size=64,seed=0, seqlen=2048, test_only=False
):
if 'wikitext2' in name:
return get_wikitext2(tokenizer,train_size,val_size,seed,seqlen,test_only)
elif 'c4' in name:
return get_c4(tokenizer,train_size,val_size,seed,seqlen,test_only)
elif 'redpajama' in name:
return get_redpajama(tokenizer,train_size,val_size,seed,seqlen)
else:
raise NotImplementedError
@torch.no_grad()
def test_ppl(model, tokenizer, datasets=['wikitext2'],ppl_seqlen=2048):
results = {}
for dataset in datasets:
testloader = get_loaders(
dataset,
tokenizer,
seed=0,
seqlen=ppl_seqlen,
test_only=True
)
if "c4" in dataset:
testenc = testloader
else:
testenc = testloader.input_ids
seqlen = ppl_seqlen
nsamples = testenc.numel() // seqlen
use_cache = model.config.use_cache
model.config.use_cache = False
model.eval()
nlls = []
if hasattr(model,'lm_head') and isinstance(model.lm_head, nn.Linear):
classifier = model.lm_head
elif hasattr(model.model,'lm_head'):
# for gptqmodels
classifier = None
elif hasattr(model,'output'):
# for internlm
classifier = model.output
else:
raise NotImplementedError
for i in tqdm(range(nsamples)):
batch = testenc[:, (i * seqlen) : ((i + 1) * seqlen)].to(model.device)
outputs = model.model(batch)
if classifier is not None:
hidden_states = outputs[0]
logits = classifier(hidden_states.to(classifier.weight.dtype))
else:
logits = outputs[0]
shift_logits = logits[:, :-1, :]
shift_labels = testenc[:, (i * seqlen) : ((i + 1) * seqlen)][
:, 1:
].to(shift_logits.device)
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(
shift_logits.view(-1, shift_logits.size(-1)),
shift_labels.view(-1),
)
neg_log_likelihood = loss.float() * seqlen
nlls.append(neg_log_likelihood)
ppl = torch.exp(torch.stack(nlls).sum() / (nsamples * seqlen))
print(f'{dataset}:{ppl}')
results[dataset] = ppl.item()
model.config.use_cache = use_cache
return results
class BlockTrainDataset(Dataset):
def __init__(self, size, seqlen, hidden_size, batch_size, dtype, cache_path='./cache/block_training_data', off_load_to_disk=False):
self.size = size
self.seqlen = seqlen
self.hidden_size = hidden_size
self.dtype = dtype
self.cache_path = cache_path
self.off_load_to_disk = off_load_to_disk
self.batch_size = batch_size
assert size%batch_size == 0
if self.off_load_to_disk:
if not os.path.exists(self.cache_path):
os.makedirs(self.cache_path)
self._initialize_data_on_disk()
else:
self.data = torch.zeros((self.size//self.batch_size, self.batch_size, self.seqlen, self.hidden_size), dtype=self.dtype)
def _initialize_data_on_disk(self):
for idx in range(self.size//self.batch_size):
tensor = torch.zeros((self.batch_size, self.seqlen, self.hidden_size), dtype=self.dtype)
filepath = self._get_file_path(idx)
torch.save(tensor, filepath)
def _get_file_path(self, idx):
return os.path.join(self.cache_path, f"data_{idx}.pt")
def __len__(self):
return self.size//self.batch_size
def __getitem__(self, idx):
if idx >= self.__len__():
raise IndexError("Index out of range")
if self.off_load_to_disk:
filepath = self._get_file_path(idx)
tensor = torch.load(filepath)
else:
tensor = self.data[idx]
return tensor
def update_data(self, idx, new_data):
if self.off_load_to_disk:
filepath = self._get_file_path(idx)
torch.save(new_data.to(self.dtype), filepath)
else:
self.data[idx] = new_data