-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
178 lines (146 loc) · 5.73 KB
/
dataloader.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
# SPDX-FileCopyrightText: 2022 Idiap Research Institute
#
# SPDX-License-Identifier: MIT
""" Data module, batch and dataset. """
import glob
import os
import torch
from pytorch_lightning import LightningDataModule
from torch.utils.data import DataLoader, Dataset
class SummarizationDataModule(LightningDataModule):
def __init__(self, args):
super().__init__()
self.data_dir = args.data_dir
self.data_model = 'bart' if args.model == 'bart' else 'bert'
self.filter_model = args.filter_model
self.num_workers = args.num_workers
self.batch_size_train = args.batch_size
self.batch_size_test = 1
self.max_pos = 512 if self.data_model == 'bert' else 1024
self.max_tgt_len = 512
self.tgt_eos_id = 2
def _truncate_bert(self, x):
x['src'] = x['src'][:-1][:self.max_pos - 1] + x['src'][-1:] # slicing notation works with empty inputs
x['tgt'] = x['tgt'][:self.max_tgt_len][:-1] + [self.tgt_eos_id]
x['src_segs'] = x['src_segs'][:self.max_pos]
return x
def _truncate_bart(self, x):
x['src'] = x['src'][:self.max_pos][:-1] + x['src'][-1:]
x['tgt'] = x['tgt'][:self.max_tgt_len][:-1] + x['tgt'][-1:]
return x
def collate(self, data):
assert self.data_model in ['bert', 'bart'], f"Unknown data model: {self.data_model}"
if self.data_model == 'bert':
data = list(map(self._truncate_bert, data))
return SummarizationBatch(data)
else:
return BartBatch(list(map(self._truncate_bart, data)))
def train_dataloader(self):
dataset = SummarizationDataset(
data_dir=self.data_dir,
filter_model=self.filter_model,
split='train',
)
return DataLoader(
dataset=dataset,
batch_size=self.batch_size_train,
shuffle=True,
num_workers=self.num_workers,
collate_fn=self.collate,
pin_memory=True,
)
def val_dataloader(self):
dataset = SummarizationDataset(
data_dir=self.data_dir,
filter_model=self.filter_model,
split='valid',
)
return DataLoader(
dataset=dataset,
batch_size=self.batch_size_test,
shuffle=False,
num_workers=self.num_workers,
collate_fn=self.collate,
pin_memory=True,
)
def test_dataloader(self):
dataset = SummarizationDataset(
data_dir=self.data_dir,
filter_model=self.filter_model,
split='test',
)
return DataLoader(
dataset=dataset,
batch_size=self.batch_size_test,
shuffle=False,
num_workers=self.num_workers,
collate_fn=self.collate,
pin_memory=True,
)
class SummarizationBatch:
def __init__(self, data, pad_id=0):
self.batch_size = len(data)
self.pad_id = pad_id
self.src = torch.tensor(self.pad([x['src'] for x in data]))
self.tgt = torch.tensor(self.pad([x['tgt'] for x in data]))
self.segs = torch.tensor(self.pad([x['src_segs'] for x in data]))
self.mask_src = 1 - (self.src == 0).to(torch.uint8)
self.mask_tgt = 1 - (self.tgt == 0).to(torch.uint8)
self.refdoc = [x['name'] for x in data]
def pad(self, data):
""" Pad `data` to same length with `pad_id`. """
max_len = max(len(x) for x in data)
return [x + [self.pad_id] * (max_len - len(x)) for x in data]
def __len__(self):
return self.batch_size
def to(self, *args, **kwargs):
self.src = self.src.to(*args, **kwargs)
self.tgt = self.tgt.to(*args, **kwargs)
self.segs = self.segs.to(*args, **kwargs)
self.mask_src = self.mask_src.to(*args, **kwargs)
self.mask_tgt = self.mask_tgt.to(*args, **kwargs)
return self
def pin_memory(self):
self.src = self.src.pin_memory()
self.tgt = self.tgt.pin_memory()
self.segs = self.segs.pin_memory()
self.mask_src = self.mask_src.pin_memory()
self.mask_tgt = self.mask_tgt.pin_memory()
return self
class BartBatch:
def __init__(self, data, pad_id=1):
self.batch_size = len(data)
self.pad_id = pad_id
self.src = torch.tensor(self.pad([x['src'] for x in data]))
self.tgt = torch.tensor(self.pad([x['tgt'] for x in data]))
self.mask_src = 1 - (self.src == pad_id).to(torch.uint8)
self.mask_tgt = 1 - (self.tgt == pad_id).to(torch.uint8)
self.refdoc = [x['name'] for x in data]
def pad(self, data):
""" Pad `data` to same length with `pad_id`. """
max_len = max(len(x) for x in data)
return [x + [self.pad_id] * (max_len - len(x)) for x in data]
def __len__(self):
return self.batch_size
def to(self, *args, **kwargs):
self.src = self.src.to(*args, **kwargs)
self.tgt = self.tgt.to(*args, **kwargs)
self.mask_src = self.mask_src.to(*args, **kwargs)
self.mask_tgt = self.mask_tgt.to(*args, **kwargs)
return self
def pin_memory(self):
self.src = self.src.pin_memory()
self.tgt = self.tgt.pin_memory()
self.mask_src = self.mask_src.pin_memory()
self.mask_tgt = self.mask_tgt.pin_memory()
return self
class SummarizationDataset(Dataset):
def __init__(self, data_dir, filter_model, split='train'):
data_files = sorted(glob.glob(os.path.join(data_dir, f'fomc.{filter_model}.{split}.pt')))
self.data = []
for pt in data_files:
self.data.extend(torch.load(pt))
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)