-
Notifications
You must be signed in to change notification settings - Fork 8
/
data_loader_dali.py
159 lines (135 loc) · 6.19 KB
/
data_loader_dali.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
import logging
import torch
import numpy as np
from torch.utils.data import DataLoader, Dataset, DistributedSampler
from torch import Tensor
#concurrent futures
import concurrent.futures as cf
# distributed stuff
import torch.distributed as dist
from utils import comm
#dali stuff
from nvidia.dali.pipeline import Pipeline
import nvidia.dali.fn as fn
import nvidia.dali.types as types
from nvidia.dali.plugin.pytorch import DALIGenericIterator, LastBatchPolicy
# es helper
import utils.dali_es_helper as esh
def get_data_loader(params, files_pattern, distributed, train):
dataloader = DaliDataLoader(params, files_pattern, train)
if train:
return dataloader, None, None
else:
return dataloader, None
class DaliDataLoader(object):
def get_pipeline(self):
pipeline = Pipeline(batch_size = self.batch_size,
num_threads = 2,
device_id = self.device_index,
py_num_workers = self.num_data_workers,
py_start_method='spawn',
seed = self.model_seed)
with pipeline: # get input and target
# get input and target
inp, tar = fn.external_source(source = esh.ERA5ES(self.location,
self.train,
self.batch_size,
self.dt,
self.img_size,
self.n_in_channels,
self.n_out_channels,
self.num_shards,
self.shard_id,
self.limit_nsamples,
enable_logging = False,
seed=self.global_seed),
num_outputs = 2,
layout = ["CHW", "CHW"],
batch = False,
no_copy = True,
parallel = True)
# upload to GPU
inp = inp.gpu()
tar = tar.gpu()
if self.normalize:
inp = fn.normalize(inp,
device = "gpu",
axis_names = "HW",
batch = False,
mean = self.in_bias,
stddev = self.in_scale)
tar = fn.normalize(tar,
device = "gpu",
axis_names = "HW",
batch = False,
mean = self.out_bias,
stddev = self.out_scale)
pipeline.set_outputs(inp, tar)
return pipeline
def __init__(self, params, location, train, seed = 333):
# set up seeds
# this one is the same on all ranks
self.global_seed = seed
# this one is the same for all ranks of the same model
model_id = comm.get_world_rank() // comm.get_size("model")
self.model_seed = self.global_seed + model_id
# this seed is supposed to be diffferent for every rank
self.local_seed = self.global_seed + comm.get_world_rank()
self.num_data_workers = params.num_data_workers
self.device_index = torch.cuda.current_device()
self.batch_size = int(params.local_batch_size)
self.location = location
self.train = train
self.dt = params.dt
self.n_in_channels = params.n_in_channels
self.n_out_channels = params.n_out_channels
self.img_size = params.img_size
self.limit_nsamples = params.limit_nsamples if train else params.limit_nsamples_val
# load stats
self.normalize = True
means = np.load(params.global_means_path)[0][:self.n_in_channels]
stds = np.load(params.global_stds_path)[0][:self.n_in_channels]
self.in_bias = means
self.in_scale = stds
means = np.load(params.global_means_path)[0][:self.n_out_channels]
stds = np.load(params.global_stds_path)[0][:self.n_out_channels]
self.out_bias = means
self.out_scale = stds
# set sharding
if dist.is_initialized():
self.num_shards = params.data_num_shards
self.shard_id = params.data_shard_id
else:
self.num_shards = 1
self.shard_id = 0
# get img source data
extsource = esh.ERA5ES(self.location,
self.train,
self.batch_size,
self.dt,
self.img_size,
self.n_in_channels,
self.n_out_channels,
self.num_shards,
self.shard_id,
self.limit_nsamples,
seed=self.global_seed)
self.num_batches = extsource.num_steps_per_epoch
del extsource
# create pipeline
self.pipeline = self.get_pipeline()
self.pipeline.start_py_workers()
self.pipeline.build()
# create iterator
self.iterator = DALIGenericIterator([self.pipeline], ['inp', 'tar'],
auto_reset = True,
last_batch_policy = LastBatchPolicy.DROP,
prepare_first_batch = True)
def __len__(self):
return self.num_batches
def __iter__(self):
#self.iterator.reset()
for token in self.iterator:
inp = token[0]['inp']
tar = token[0]['tar']
yield inp, tar