-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDataLoader.py
469 lines (391 loc) · 24.8 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import numpy as np
import time
import torch
from torch.utils.data import Dataset, TensorDataset, DataLoader, RandomSampler, SequentialSampler
import torch.utils.data.distributed
class MySimpleQADataset(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids=None, decoder_attention_mask=None,
in_metadata=None, out_metadata=None,
is_training=False,
answer_as_prefix=False):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = None if decoder_input_ids is None else torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = None if decoder_attention_mask is None else torch.LongTensor(decoder_attention_mask)
self.in_metadata = list(zip(range(len(input_ids)), range(1, 1+len(input_ids)))) \
if in_metadata is None else in_metadata
self.out_metadata = list(zip(range(len(decoder_input_ids)), range(1, 1+len(decoder_input_ids)))) \
if is_training and out_metadata is None else out_metadata
self.is_training = is_training
self.answer_as_prefix = answer_as_prefix
assert len(self.input_ids)==len(self.attention_mask)==self.in_metadata[-1][-1]
assert not self.is_training or len(self.decoder_input_ids)==len(self.decoder_attention_mask)==self.out_metadata[-1][-1]
def __len__(self):
return len(self.in_metadata)
def __getitem__(self, idx):
if not self.is_training:
idx = self.in_metadata[idx][0]
if self.answer_as_prefix:
out_idx = self.out_metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx], \
self.decoder_input_ids[out_idx], self.decoder_attention_mask[out_idx]
return self.input_ids[idx], self.attention_mask[idx]
in_idx = np.random.choice(range(*self.in_metadata[idx]))
out_idx = np.random.choice(range(*self.out_metadata[idx]))
return self.input_ids[in_idx], self.attention_mask[in_idx], \
self.decoder_input_ids[out_idx], self.decoder_attention_mask[out_idx]
class MySimpleQALMFilteringDataset(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids=None, decoder_attention_mask=None,
in_metadata=None, out_metadata=None,
is_training=False,
answer_as_prefix=False):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = None if decoder_input_ids is None else torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = None if decoder_attention_mask is None else torch.LongTensor(decoder_attention_mask)
self.in_metadata = list(zip(range(len(input_ids)), range(1, 1+len(input_ids)))) \
if in_metadata is None else in_metadata
self.out_metadata = list(zip(range(len(decoder_input_ids)), range(1, 1+len(decoder_input_ids)))) \
if is_training and out_metadata is None else out_metadata
self.is_training = is_training
self.answer_as_prefix = answer_as_prefix
assert len(self.input_ids)==len(self.attention_mask)==self.in_metadata[-1][-1]
assert not self.is_training or len(self.decoder_input_ids)==len(self.decoder_attention_mask)==self.out_metadata[-1][-1]
def __len__(self):
return len(self.in_metadata)
def __getitem__(self, idx):
assert not self.is_training
idx = self.in_metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx], \
self.decoder_input_ids[idx], self.decoder_attention_mask[idx]
class MySimpleQADatasetForPair(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids=None, decoder_attention_mask=None, metadata=None,
is_training=False):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = None if decoder_input_ids is None else torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = None if decoder_attention_mask is None else torch.LongTensor(decoder_attention_mask)
self.metadata = metadata
self.is_training = is_training
assert len(self.input_ids)==len(self.attention_mask)
assert not self.is_training or len(self.input_ids)==len(self.decoder_input_ids)==len(self.decoder_attention_mask)==self.metadata[-1][-1]
assert self.metadata[-1][-1]==len(self.input_ids)
def __len__(self):
return len(self.metadata) if self.is_training else len(self.input_ids)
def __getitem__(self, idx):
if not self.is_training:
idx = self.metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx]
idx = np.random.choice(range(*self.metadata[idx]))
return self.input_ids[idx], self.attention_mask[idx], \
self.decoder_input_ids[idx], self.decoder_attention_mask[idx]
class MySimpleQGDataset(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids=None, decoder_attention_mask=None,
in_metadata=None, out_metadata=None,
is_training=False):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = None if decoder_input_ids is None else torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = None if decoder_attention_mask is None else torch.LongTensor(decoder_attention_mask)
self.in_metadata = list(zip(range(len(input_ids)), range(1, 1+len(input_ids)))) if in_metadata is None else in_metadata
self.out_metadata = list(zip(range(len(decoder_input_ids)), range(1, 1+len(decoder_input_ids)))) if is_training and out_metadata is None else out_metadata
self.is_training = is_training
assert len(self.input_ids)==len(self.attention_mask)==self.in_metadata[-1][-1]
assert not self.is_training or len(self.decoder_input_ids)==len(self.decoder_attention_mask)==self.out_metadata[-1][-1]==len(self.in_metadata)==len(self.out_metadata)
def __len__(self):
if self.is_training:
return len(self.out_metadata)
return len(self.in_metadata)
def __getitem__(self, idx):
if not self.is_training:
idx = self.in_metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx]
in_idx = np.random.choice(range(*self.in_metadata[idx]))
out_idx = np.random.choice(range(*self.out_metadata[idx]))
return self.input_ids[in_idx], self.attention_mask[in_idx], \
self.decoder_input_ids[out_idx], self.decoder_attention_mask[out_idx]
class MySimpleQGWeightedLossDataset(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids=None, decoder_attention_mask=None,
in_metadata=None, out_metadata=None,
is_training=False,
weighted_position=None):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = None if decoder_input_ids is None else torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = None if decoder_attention_mask is None else torch.LongTensor(decoder_attention_mask)
self.in_metadata = list(zip(range(len(input_ids)), range(1, 1+len(input_ids)))) if in_metadata is None else in_metadata
self.out_metadata = list(zip(range(len(decoder_input_ids)), range(1, 1+len(decoder_input_ids)))) if is_training and out_metadata is None else out_metadata
self.is_training = is_training
self.weighted_position = None if weighted_position is None else torch.LongTensor(weighted_position)
assert len(self.input_ids)==len(self.attention_mask)==self.in_metadata[-1][-1]
assert not self.is_training or len(self.decoder_input_ids)==len(self.decoder_attention_mask)==self.out_metadata[-1][-1]==len(self.in_metadata)==len(self.out_metadata)
def __len__(self):
if self.is_training:
return len(self.out_metadata)
return len(self.in_metadata)
def __getitem__(self, idx):
if not self.is_training:
idx = self.in_metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx]
in_idx = np.random.choice(range(*self.in_metadata[idx]))
out_idx = np.random.choice(range(*self.out_metadata[idx]))
return self.input_ids[in_idx], self.attention_mask[in_idx], \
self.decoder_input_ids[out_idx], self.decoder_attention_mask[out_idx], self.weighted_position[out_idx]
class MySimpleQGDynamicDataset(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids=None, decoder_attention_mask=None,
in_metadata=None, out_metadata=None,
is_training=False, discard_not_found_answers=None):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = None if decoder_input_ids is None else torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = None if decoder_attention_mask is None else torch.LongTensor(decoder_attention_mask)
self.in_metadata = list(zip(range(len(input_ids)), range(1, 1+len(input_ids)))) if in_metadata is None else in_metadata
self.out_metadata = list(zip(range(len(decoder_input_ids)), range(1, 1+len(decoder_input_ids)))) if is_training and out_metadata is None else out_metadata
self.is_training = is_training
self.discard_not_found_answers = discard_not_found_answers
assert len(self.input_ids)==len(self.attention_mask)==self.in_metadata[-1][-1]
assert not self.is_training or len(self.decoder_input_ids)==len(self.decoder_attention_mask)==self.out_metadata[-1][-1]==len(self.in_metadata)==len(self.out_metadata)
def __len__(self):
if self.is_training:
return len(self.out_metadata)
return len(self.in_metadata)
def __getitem__(self, idx):
if not self.is_training:
idx = self.in_metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx], sum(self.discard_not_found_answers[idx])
in_idx = np.random.choice(range(*self.in_metadata[idx]))
out_idx = np.random.choice(range(*self.out_metadata[idx]))
return self.input_ids[in_idx], self.attention_mask[in_idx], \
self.decoder_input_ids[out_idx], self.decoder_attention_mask[out_idx], sum(self.discard_not_found_answers[in_idx])
class MySimpleQGDynamicWeightedLossDataset(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids=None, decoder_attention_mask=None,
in_metadata=None, out_metadata=None,
is_training=False, discard_not_found_answers=None,
weighted_position=None):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = None if decoder_input_ids is None else torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = None if decoder_attention_mask is None else torch.LongTensor(decoder_attention_mask)
self.in_metadata = list(zip(range(len(input_ids)), range(1, 1+len(input_ids)))) if in_metadata is None else in_metadata
self.out_metadata = list(zip(range(len(decoder_input_ids)), range(1, 1+len(decoder_input_ids)))) if is_training and out_metadata is None else out_metadata
self.is_training = is_training
self.discard_not_found_answers = discard_not_found_answers
self.weighted_position = None if weighted_position is None else torch.LongTensor(weighted_position)
assert len(self.input_ids)==len(self.attention_mask)==self.in_metadata[-1][-1]
assert not self.is_training or len(self.decoder_input_ids)==len(self.decoder_attention_mask)==self.out_metadata[-1][-1]==len(self.in_metadata)==len(self.out_metadata)
def __len__(self):
if self.is_training:
return len(self.out_metadata)
return len(self.in_metadata)
def __getitem__(self, idx):
if not self.is_training:
idx = self.in_metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx], sum(self.discard_not_found_answers[idx])
in_idx = np.random.choice(range(*self.in_metadata[idx]))
out_idx = np.random.choice(range(*self.out_metadata[idx]))
return self.input_ids[in_idx], self.attention_mask[in_idx], \
self.decoder_input_ids[out_idx], self.decoder_attention_mask[out_idx], \
sum(self.discard_not_found_answers[in_idx]), self.weighted_position[out_idx]
class MyQADataset(Dataset):
def __init__(self, data,
is_training=False, train_M=None, test_M=None):
self.data = data #.dictionify()
self.positive_input_ids = self.tensorize("positive_input_ids")
self.positive_input_mask = self.tensorize("positive_input_mask")
self.positive_token_type_ids = self.tensorize("positive_token_type_ids")
assert len(self.positive_input_ids)==len(self.positive_input_mask)==len(self.positive_token_type_ids)
if is_training:
self.positive_start_positions = self.tensorize("positive_start_positions")
self.positive_end_positions = self.tensorize("positive_end_positions")
self.positive_answer_mask = self.tensorize("positive_answer_mask")
self.negative_input_ids = self.tensorize("negative_input_ids")
self.negative_input_mask = self.tensorize("negative_input_mask")
self.negative_token_type_ids = self.tensorize("negative_token_type_ids")
assert len(self.negative_input_ids)==len(self.negative_input_mask)==len(self.negative_token_type_ids)
assert len(self.positive_input_ids)==\
len(self.positive_start_positions)==len(self.positive_end_positions)==len(self.positive_answer_mask)
assert all([len(positive_input_ids)>0 for positive_input_ids in self.positive_input_ids])
self.is_training = is_training
self.train_M = train_M
self.test_M = test_M
def __len__(self):
return len(self.positive_input_ids)
def __getitem__(self, idx):
if not self.is_training:
input_ids = self.positive_input_ids[idx][:self.test_M]
input_mask = self.positive_input_mask[idx][:self.test_M]
token_type_ids = self.positive_token_type_ids[idx][:self.test_M]
return [self._pad(t, self.test_M) for t in [input_ids, input_mask, token_type_ids]]
# sample positive
positive_idx = np.random.choice(len(self.positive_input_ids[idx]))
#positive_idx = 0
positive_input_ids = self.positive_input_ids[idx][positive_idx]
positive_input_mask = self.positive_input_mask[idx][positive_idx]
positive_token_type_ids = self.positive_token_type_ids[idx][positive_idx]
positive_start_positions = self.positive_start_positions[idx][positive_idx]
positive_end_positions = self.positive_end_positions[idx][positive_idx]
positive_answer_mask = self.positive_answer_mask[idx][positive_idx]
# sample negatives
negative_idxs = np.random.permutation(range(len(self.negative_input_ids[idx])))[:self.train_M-1]
negative_input_ids = [self.negative_input_ids[idx][i] for i in negative_idxs]
negative_input_mask = [self.negative_input_mask[idx][i] for i in negative_idxs]
negative_token_type_ids = [self.negative_token_type_ids[idx][i] for i in negative_idxs]
negative_input_ids, negative_input_mask, negative_token_type_ids = \
[self._pad(t, self.train_M-1) for t in [negative_input_ids, negative_input_mask, negative_token_type_ids]]
# aggregate
input_ids = torch.cat([positive_input_ids.unsqueeze(0), negative_input_ids], dim=0)
input_mask = torch.cat([positive_input_mask.unsqueeze(0), negative_input_mask], dim=0)
token_type_ids = torch.cat([positive_token_type_ids.unsqueeze(0), negative_token_type_ids], dim=0)
start_positions, end_positions, answer_mask = \
[self._pad([t], self.train_M) for t in [positive_start_positions,
positive_end_positions,
positive_answer_mask]]
return input_ids, input_mask, token_type_ids, start_positions, end_positions, answer_mask
def tensorize(self, key):
return [torch.LongTensor(t) for t in self.data[key]] if key in self.data.keys() else None
def _pad(self, input_ids, M):
if len(input_ids)==0:
return torch.zeros((M, self.negative_input_ids[0].size(1)), dtype=torch.long)
if type(input_ids)==list:
input_ids = torch.stack(input_ids)
if len(input_ids)==M:
return input_ids
return torch.cat([input_ids,
torch.zeros((M-input_ids.size(0), input_ids.size(1)), dtype=torch.long)],
dim=0)
class MyQAGenDataset(Dataset):
def __init__(self,
input_ids, attention_mask,
decoder_input_ids=None, decoder_attention_mask=None,
in_metadata=None, out_metadata=None,
is_training=False):
self.input_ids = torch.LongTensor(input_ids)
self.attention_mask = torch.LongTensor(attention_mask)
self.decoder_input_ids = None if decoder_input_ids is None else torch.LongTensor(decoder_input_ids)
self.decoder_attention_mask = None if decoder_attention_mask is None else torch.LongTensor(decoder_attention_mask)
self.in_metadata = list(zip(range(len(input_ids)), range(1, 1 + len(input_ids)))) if in_metadata is None else in_metadata
self.out_metadata = list(zip(range(len(decoder_input_ids)), range(1, 1 + len(decoder_input_ids)))) if is_training and out_metadata is None else out_metadata
self.is_training = is_training
assert len(self.input_ids) == len(self.attention_mask) == self.in_metadata[-1][-1]
assert not self.is_training or len(self.decoder_input_ids) == len(self.decoder_attention_mask) == self.out_metadata[-1][-1]
def __len__(self):
return len(self.in_metadata)
def __getitem__(self, idx):
if not self.is_training:
idx = self.in_metadata[idx][0]
return self.input_ids[idx], self.attention_mask[idx]
in_idx = np.random.choice(range(*self.in_metadata[idx]))
out_idx = np.random.choice(range(*self.out_metadata[idx]))
return self.input_ids[in_idx], self.attention_mask[in_idx], \
self.decoder_input_ids[out_idx], self.decoder_attention_mask[out_idx]
class MyRerankerDataset(Dataset):
def __init__(self, data,
is_training=False, train_MP=None, train_MN=None, test_M=None):
self.data = data #.dictionify()
self.positive_input_ids = self.tensorize("positive_input_ids")
self.positive_input_mask = self.tensorize("positive_input_mask")
self.positive_token_type_ids = self.tensorize("positive_token_type_ids")
assert len(self.positive_input_ids)==len(self.positive_input_mask)==len(self.positive_token_type_ids)
if is_training:
self.positive_start_positions = self.tensorize("positive_start_positions")
self.positive_end_positions = self.tensorize("positive_end_positions")
self.positive_answer_mask = self.tensorize("positive_answer_mask")
self.negative_input_ids = self.tensorize("negative_input_ids")
self.negative_input_mask = self.tensorize("negative_input_mask")
self.negative_token_type_ids = self.tensorize("negative_token_type_ids")
assert len(self.negative_input_ids)==len(self.negative_input_mask)==len(self.negative_token_type_ids)
assert all([len(positive_input_ids)>0 for positive_input_ids in self.positive_input_ids])
self.is_training = is_training
self.train_MP = train_MP
self.train_MN = train_MN
self.test_M = test_M
def __len__(self):
return len(self.positive_input_ids)
def __getitem__(self, idx):
if not self.is_training:
input_ids = self.positive_input_ids[idx][:self.test_M]
input_mask = self.positive_input_mask[idx][:self.test_M]
token_type_ids = self.positive_token_type_ids[idx][:self.test_M]
return [self._pad(t, self.test_M) for t in [input_ids, input_mask, token_type_ids]]
# sample positive
positive_idxs = np.random.permutation(range(len(self.positive_input_ids[idx])))[:self.train_MP]
positive_input_ids = [self.positive_input_ids[idx][i] for i in positive_idxs]
positive_input_mask = [self.positive_input_mask[idx][i] for i in positive_idxs]
positive_token_type_ids = [self.positive_token_type_ids[idx][i] for i in positive_idxs]
labels = torch.LongTensor(list(range(len(positive_idxs))) + [-1] * (self.train_MP - len(positive_idxs)))
positive_input_ids, positive_input_mask, positive_token_type_ids = \
[self._pad(t, self.train_MP) for t in [positive_input_ids, positive_input_mask, positive_token_type_ids]]
# sample negatives
negative_idxs = np.random.permutation(range(len(self.negative_input_ids[idx])))[:self.train_MN]
negative_input_ids = [self.negative_input_ids[idx][i] for i in negative_idxs]
negative_input_mask = [self.negative_input_mask[idx][i] for i in negative_idxs]
negative_token_type_ids = [self.negative_token_type_ids[idx][i] for i in negative_idxs]
negative_input_ids, negative_input_mask, negative_token_type_ids = \
[self._pad(t, self.train_MN) for t in [negative_input_ids, negative_input_mask, negative_token_type_ids]]
# aggregate
input_ids = torch.cat([positive_input_ids, negative_input_ids], dim=0)
input_mask = torch.cat([positive_input_mask, negative_input_mask], dim=0)
token_type_ids = torch.cat([positive_token_type_ids, negative_token_type_ids], dim=0)
return input_ids, input_mask, token_type_ids, labels
def tensorize(self, key):
return [torch.LongTensor(t) for t in self.data[key]] if key in self.data.keys() else None
def _pad(self, input_ids, M):
if len(input_ids)==0:
return torch.zeros((M, self.negative_input_ids[0].size(1)), dtype=torch.long)
if type(input_ids)==list:
input_ids = torch.stack(input_ids)
if len(input_ids)==M:
return input_ids
return torch.cat([input_ids,
torch.zeros((M-input_ids.size(0), input_ids.size(1)), dtype=torch.long)],
dim=0)
class MyDataLoader(DataLoader):
def __init__(self, args, dataset, is_training, batch_size=None, **kwargs):
if is_training:
sampler = RandomSampler(dataset) if args.is_distributed == 0 else torch.utils.data.distributed.DistributedSampler(dataset)
batch_size = args.train_batch_size if batch_size is None else batch_size
else:
sampler=SequentialSampler(dataset)
batch_size = args.predict_batch_size if batch_size is None else batch_size
super(MyDataLoader, self).__init__(dataset, sampler=sampler, batch_size=batch_size, **kwargs)
class MyQAGenDataLoader(DataLoader):
def __init__(self, args, dataset, is_training, batch_size=None, **kwargs):
if is_training:
sampler = RandomSampler(dataset) if args.is_distributed == 0 else torch.utils.data.distributed.DistributedSampler(dataset)
batch_size = batch_size
else:
sampler = SequentialSampler(dataset)
batch_size = args.predict_batch_size if batch_size is None else batch_size
super(MyQAGenDataLoader, self).__init__(dataset, sampler=sampler, batch_size=batch_size, **kwargs)