-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
53 lines (49 loc) · 2.46 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
from torch.utils.data import DataLoader, Dataset
from data_sequential import DataSequential
from torch.utils.data.distributed import DistributedSampler
def get_dataloader(args, tokenizer):
train_dataset = DataSequential(args, tokenizer, 'train')
val_dataset = DataSequential(args, tokenizer, 'valid')
test_dataset = DataSequential(args, tokenizer, 'test')
if args.distributed:
train_sampler = DistributedSampler(train_dataset)
valid_sampler = DistributedSampler(val_dataset)
test_sampler = DistributedSampler(test_dataset)
else:
train_sampler, valid_sampler, test_sampler = None, None, None
if args.distributed:
train_loader = DataLoader(train_dataset,
batch_size=args.batch_size,
collate_fn=train_dataset.collate_fn,
shuffle=False,
pin_memory=True,
sampler=train_sampler)
val_loader = DataLoader(val_dataset,
batch_size=args.batch_size * 4,
collate_fn=train_dataset.collate_fn,
shuffle=False,
pin_memory=True,
sampler=valid_sampler)
test_loader = DataLoader(test_dataset,
batch_size=args.batch_size * 4,
collate_fn=train_dataset.collate_fn,
shuffle=False,
pin_memory=True,
sampler=test_sampler)
else:
train_loader = DataLoader(train_dataset,
batch_size=args.batch_size,
collate_fn=train_dataset.collate_fn,
shuffle=True
)
val_loader = DataLoader(val_dataset,
batch_size=args.batch_size * 4,
shuffle=False,
collate_fn=val_dataset.collate_fn
)
test_loader = DataLoader(test_dataset,
batch_size=args.batch_size * 4,
shuffle=False,
collate_fn=test_dataset.collate_fn
)
return train_loader, val_loader, test_loader