-
Notifications
You must be signed in to change notification settings - Fork 186
/
batcher.py
265 lines (224 loc) · 8.39 KB
/
batcher.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
""" batching """
import random
from collections import defaultdict
from toolz.sandbox import unzip
from cytoolz import curry, concat, compose
from cytoolz import curried
import torch
import torch.multiprocessing as mp
# Batching functions
def coll_fn(data):
source_lists, target_lists = unzip(data)
# NOTE: independent filtering works because
# source and targets are matched properly by the Dataset
sources = list(filter(bool, concat(source_lists)))
targets = list(filter(bool, concat(target_lists)))
assert all(sources) and all(targets)
return sources, targets
def coll_fn_extract(data):
def is_good_data(d):
""" make sure data is not empty"""
source_sents, extracts = d
return source_sents and extracts
batch = list(filter(is_good_data, data))
assert all(map(is_good_data, batch))
return batch
@curry
def tokenize(max_len, texts):
return [t.lower().split()[:max_len] for t in texts]
def conver2id(unk, word2id, words_list):
word2id = defaultdict(lambda: unk, word2id)
return [[word2id[w] for w in words] for words in words_list]
@curry
def prepro_fn(max_src_len, max_tgt_len, batch):
sources, targets = batch
sources = tokenize(max_src_len, sources)
targets = tokenize(max_tgt_len, targets)
batch = list(zip(sources, targets))
return batch
@curry
def prepro_fn_extract(max_src_len, max_src_num, batch):
def prepro_one(sample):
source_sents, extracts = sample
tokenized_sents = tokenize(max_src_len, source_sents)[:max_src_num]
cleaned_extracts = list(filter(lambda e: e < len(tokenized_sents),
extracts))
return tokenized_sents, cleaned_extracts
batch = list(map(prepro_one, batch))
return batch
@curry
def convert_batch(unk, word2id, batch):
sources, targets = unzip(batch)
sources = conver2id(unk, word2id, sources)
targets = conver2id(unk, word2id, targets)
batch = list(zip(sources, targets))
return batch
@curry
def convert_batch_copy(unk, word2id, batch):
sources, targets = map(list, unzip(batch))
ext_word2id = dict(word2id)
for source in sources:
for word in source:
if word not in ext_word2id:
ext_word2id[word] = len(ext_word2id)
src_exts = conver2id(unk, ext_word2id, sources)
sources = conver2id(unk, word2id, sources)
tar_ins = conver2id(unk, word2id, targets)
targets = conver2id(unk, ext_word2id, targets)
batch = list(zip(sources, src_exts, tar_ins, targets))
return batch
@curry
def convert_batch_extract_ptr(unk, word2id, batch):
def convert_one(sample):
source_sents, extracts = sample
id_sents = conver2id(unk, word2id, source_sents)
return id_sents, extracts
batch = list(map(convert_one, batch))
return batch
@curry
def convert_batch_extract_ff(unk, word2id, batch):
def convert_one(sample):
source_sents, extracts = sample
id_sents = conver2id(unk, word2id, source_sents)
binary_extracts = [0] * len(source_sents)
for ext in extracts:
binary_extracts[ext] = 1
return id_sents, binary_extracts
batch = list(map(convert_one, batch))
return batch
@curry
def pad_batch_tensorize(inputs, pad, cuda=True):
"""pad_batch_tensorize
:param inputs: List of size B containing torch tensors of shape [T, ...]
:type inputs: List[np.ndarray]
:rtype: TorchTensor of size (B, T, ...)
"""
tensor_type = torch.cuda.LongTensor if cuda else torch.LongTensor
batch_size = len(inputs)
max_len = max(len(ids) for ids in inputs)
tensor_shape = (batch_size, max_len)
tensor = tensor_type(*tensor_shape)
tensor.fill_(pad)
for i, ids in enumerate(inputs):
tensor[i, :len(ids)] = tensor_type(ids)
return tensor
@curry
def batchify_fn(pad, start, end, data, cuda=True):
sources, targets = tuple(map(list, unzip(data)))
src_lens = [len(src) for src in sources]
tar_ins = [[start] + tgt for tgt in targets]
targets = [tgt + [end] for tgt in targets]
source = pad_batch_tensorize(sources, pad, cuda)
tar_in = pad_batch_tensorize(tar_ins, pad, cuda)
target = pad_batch_tensorize(targets, pad, cuda)
fw_args = (source, src_lens, tar_in)
loss_args = (target, )
return fw_args, loss_args
@curry
def batchify_fn_copy(pad, start, end, data, cuda=True):
sources, ext_srcs, tar_ins, targets = tuple(map(list, unzip(data)))
src_lens = [len(src) for src in sources]
sources = [src for src in sources]
ext_srcs = [ext for ext in ext_srcs]
tar_ins = [[start] + tgt for tgt in tar_ins]
targets = [tgt + [end] for tgt in targets]
source = pad_batch_tensorize(sources, pad, cuda)
tar_in = pad_batch_tensorize(tar_ins, pad, cuda)
target = pad_batch_tensorize(targets, pad, cuda)
ext_src = pad_batch_tensorize(ext_srcs, pad, cuda)
ext_vsize = ext_src.max().item() + 1
fw_args = (source, src_lens, tar_in, ext_src, ext_vsize)
loss_args = (target, )
return fw_args, loss_args
@curry
def batchify_fn_extract_ptr(pad, data, cuda=True):
source_lists, targets = tuple(map(list, unzip(data)))
src_nums = list(map(len, source_lists))
sources = list(map(pad_batch_tensorize(pad=pad, cuda=cuda), source_lists))
# PAD is -1 (dummy extraction index) for using sequence loss
target = pad_batch_tensorize(targets, pad=-1, cuda=cuda)
remove_last = lambda tgt: tgt[:-1]
tar_in = pad_batch_tensorize(
list(map(remove_last, targets)),
pad=-0, cuda=cuda # use 0 here for feeding first conv sentence repr.
)
fw_args = (sources, src_nums, tar_in)
loss_args = (target, )
return fw_args, loss_args
@curry
def batchify_fn_extract_ff(pad, data, cuda=True):
source_lists, targets = tuple(map(list, unzip(data)))
src_nums = list(map(len, source_lists))
sources = list(map(pad_batch_tensorize(pad=pad, cuda=cuda), source_lists))
tensor_type = torch.cuda.FloatTensor if cuda else torch.FloatTensor
target = tensor_type(list(concat(targets)))
fw_args = (sources, src_nums)
loss_args = (target, )
return fw_args, loss_args
def _batch2q(loader, prepro, q, single_run=True):
epoch = 0
while True:
for batch in loader:
q.put(prepro(batch))
if single_run:
break
epoch += 1
q.put(epoch)
q.put(None)
class BucketedGenerater(object):
def __init__(self, loader, prepro,
sort_key, batchify,
single_run=True, queue_size=8, fork=True):
self._loader = loader
self._prepro = prepro
self._sort_key = sort_key
self._batchify = batchify
self._single_run = single_run
if fork:
ctx = mp.get_context('forkserver')
self._queue = ctx.Queue(queue_size)
else:
# for easier debugging
self._queue = None
self._process = None
def __call__(self, batch_size: int):
def get_batches(hyper_batch):
indexes = list(range(0, len(hyper_batch), batch_size))
if not self._single_run:
# random shuffle for training batches
random.shuffle(hyper_batch)
random.shuffle(indexes)
hyper_batch.sort(key=self._sort_key)
for i in indexes:
batch = self._batchify(hyper_batch[i:i+batch_size])
yield batch
if self._queue is not None:
ctx = mp.get_context('forkserver')
self._process = ctx.Process(
target=_batch2q,
args=(self._loader, self._prepro,
self._queue, self._single_run)
)
self._process.start()
while True:
d = self._queue.get()
if d is None:
break
if isinstance(d, int):
print('\nepoch {} done'.format(d))
continue
yield from get_batches(d)
self._process.join()
else:
i = 0
while True:
for batch in self._loader:
yield from get_batches(self._prepro(batch))
if self._single_run:
break
i += 1
print('\nepoch {} done'.format(i))
def terminate(self):
if self._process is not None:
self._process.terminate()
self._process.join()