-
Notifications
You must be signed in to change notification settings - Fork 1
/
datamodules.py
285 lines (242 loc) · 9.33 KB
/
datamodules.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
import glob
import json
import os
import os.path
from typing import Optional
import pytorch_lightning as pl
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.pre_tokenizers import Whitespace
from tokenizers.trainers import BpeTrainer
from torch.utils.data import DataLoader
from transformers import GPT2Tokenizer
from transformers import PreTrainedTokenizerFast
from data_utils import ChineseWebtextDataset
from data_utils import OscarDataset
from data_utils import WebTextIter
def build_vocab_from_file(vocab_file):
symbols = []
with open(vocab_file, "r", encoding="utf-8") as f:
for line in f:
symb = line.strip().split()[0]
symbols.append(symb)
return {s: i for i, s in enumerate(symbols)}
def build_vocab_from_json(vocab_file):
with open(vocab_file) as json_file:
data = json.load(json_file)
return dict(data)
class OpenWebText2DataModule(pl.LightningDataModule):
def __init__(
self,
sequence_length: int,
batch_size: int,
eval_batch_size: int = None,
data_dir="/datadrive/openwebtext2",
):
super().__init__()
self.batch_size = batch_size
self.eval_batch_size = eval_batch_size if eval_batch_size else 5
self.sequence_length = sequence_length
self.data_dir = data_dir
def setup(self, stage: Optional[str] = None):
files = glob.glob(os.path.join(self.data_dir + "/shards", "*"))
self.train_paths = files[:80]
self.val_paths = files[80:90]
self.test_paths = files[90:]
vocab = build_vocab_from_json(self.data_dir + "/gpt2-vocab.json")
self.vocab = vocab
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
def train_dataloader(self):
train_dataset = WebTextIter(
dataset_paths=self.train_paths,
seq_len=self.sequence_length,
batch_size=self.batch_size,
tokenizer=self.tokenizer,
)
data_loader = DataLoader(train_dataset, batch_size=None, sampler=None)
return data_loader
def val_dataloader(self):
val_dataset = WebTextIter(
dataset_paths=self.val_paths,
seq_len=self.sequence_length,
batch_size=self.eval_batch_size,
tokenizer=self.tokenizer,
)
data_loader = DataLoader(val_dataset, batch_size=None, sampler=None,)
return data_loader
def test_dataloader(self):
test_dataset = WebTextIter(
dataset_paths=self.test_paths,
seq_len=self.sequence_length,
batch_size=self.eval_batch_size,
tokenizer=self.tokenizer,
)
return DataLoader(test_dataset, batch_size=None, sampler=None)
class FileDataModule(OpenWebText2DataModule):
def __init__(
self,
sequence_length: int,
batch_size: int,
eval_batch_size: int = None,
data_dir="/datadrive/",
):
super().__init__(sequence_length, batch_size, eval_batch_size, data_dir)
self.batch_size = batch_size
self.eval_batch_size = eval_batch_size if eval_batch_size else 5
self.sequence_length = sequence_length
self.data_dir = data_dir
def setup(self, stage: Optional[str] = None):
self.train_paths = [self.data_dir + "/train.txt"]
self.val_paths = [self.data_dir + "/valid.txt"]
self.test_paths = [self.data_dir + "/test.txt"]
self.all_paths = self.train_paths + self.val_paths + self.test_paths
if not os.path.exists(self.data_dir + "/tokenizer.json"):
tokenizer = Tokenizer(BPE())
tokenizer.pre_tokenizer = Whitespace()
trainer = BpeTrainer()
tokenizer.train(files=self.all_paths, trainer=trainer)
# special_tokens_dict = ['bos_token','eos_token']
special_tokens_dict = ["<bos>", "<eos>"]
num_added_toks = tokenizer.add_special_tokens(special_tokens_dict)
print("We have added", num_added_toks, "tokens")
tokenizer = tokenizer.save(self.data_dir + "/tokenizer.json")
self.tokenizer = PreTrainedTokenizerFast(
tokenizer_file=self.data_dir + "/tokenizer.json",
unk_token="<unk>",
bos_token="<bos>",
eos_token="<eos>",
)
self.vocab = self.tokenizer.get_vocab()
# self.train_paths = [
# path
# for idx, path in enumerate(all_paths)
# if idx % 10 in (0, 2, 4, 6, 8,)
# ]
# self.valid_paths = [
# path for idx, path in enumerate(all_paths) if idx % 10 in (1, 9)
# ]
# self.test_paths = [
# path for idx, path in enumerate(all_paths) if idx % 10 in (3, 7)
# ]
class ChineseWebtextDataModule(pl.LightningDataModule):
def __init__(
self,
sequence_length: int,
batch_size: int,
token_limit: int,
eval_batch_size: int = None,
data_dir="/datadrive/",
diff_tokenization=False,
):
super().__init__()
self.batch_size = batch_size
self.eval_batch_size = eval_batch_size if eval_batch_size else 5
self.sequence_length = sequence_length
self.data_dir = data_dir
self.token_limit = token_limit
if diff_tokenization:
print("diff tokenizer")
self.tokenizer = PreTrainedTokenizerFast(
tokenizer_file=self.data_dir + "/tokenizer.json",
unk_token="<unk>",
bos_token="<bos>",
eos_token="<eos>",
)
else:
print("same tokenizer")
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
self.vocab = self.tokenizer.get_vocab()
def setup(self, stage: Optional[str] = None):
self.test_file = self.data_dir + "/web_text_zh_testa.json"
self.train_file = self.data_dir + "/web_text_zh_train.json"
self.valid_file = self.data_dir + "/web_text_zh_valid.json"
def train_dataloader(self):
train_dataset = ChineseWebtextDataset(
file=self.train_file,
seq_len=self.sequence_length,
batch_size=self.batch_size,
token_limit=self.token_limit,
tokenizer=self.tokenizer,
)
data_loader = DataLoader(train_dataset, batch_size=None, sampler=None)
return data_loader
def val_dataloader(self):
val_dataset = ChineseWebtextDataset(
file=self.valid_file,
seq_len=self.sequence_length,
batch_size=self.eval_batch_size,
token_limit=self.token_limit,
tokenizer=self.tokenizer,
)
data_loader = DataLoader(val_dataset, batch_size=None, sampler=None,)
return data_loader
def test_dataloader(self):
test_dataset = ChineseWebtextDataset(
file=self.test_file,
seq_len=self.sequence_length,
batch_size=self.eval_batch_size,
token_limit=self.token_limit,
tokenizer=self.tokenizer,
)
return DataLoader(test_dataset, batch_size=None, sampler=None)
class OscarDataModule(pl.LightningDataModule):
def __init__(
self,
sequence_length: int,
batch_size: int,
token_limit: int,
eval_batch_size: int = None,
data_dir="/datadrive/",
diff_tokenization=False,
):
super().__init__()
self.batch_size = batch_size
self.eval_batch_size = eval_batch_size if eval_batch_size else 5
self.sequence_length = sequence_length
self.data_dir = data_dir
self.token_limit = token_limit
if diff_tokenization:
print("diff tokenizer")
self.tokenizer = PreTrainedTokenizerFast(
tokenizer_file=self.data_dir + "/tokenizer.json",
unk_token="<unk>",
bos_token="<bos>",
eos_token="<eos>",
)
else:
print("same tokenizer")
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
self.vocab = self.tokenizer.get_vocab()
def setup(self, stage: Optional[str] = None):
self.test_file = self.data_dir + "/test.txt"
self.train_file = self.data_dir + "/train.txt"
self.valid_file = self.data_dir + "/valid.txt"
def train_dataloader(self):
train_dataset = OscarDataset(
file=self.train_file,
seq_len=self.sequence_length,
batch_size=self.batch_size,
token_limit=self.token_limit,
tokenizer=self.tokenizer,
)
data_loader = DataLoader(train_dataset, batch_size=None, sampler=None)
return data_loader
def val_dataloader(self):
val_dataset = OscarDataset(
file=self.valid_file,
seq_len=self.sequence_length,
batch_size=self.eval_batch_size,
token_limit=self.token_limit,
tokenizer=self.tokenizer,
)
data_loader = DataLoader(val_dataset, batch_size=None, sampler=None,)
return data_loader
def test_dataloader(self):
test_dataset = OscarDataset(
file=self.test_file,
seq_len=self.sequence_length,
batch_size=self.eval_batch_size,
token_limit=self.token_limit,
tokenizer=self.tokenizer,
)
return DataLoader(test_dataset, batch_size=None, sampler=None)