-
Notifications
You must be signed in to change notification settings - Fork 512
/
base_sampler.py
405 lines (339 loc) · 15.7 KB
/
base_sampler.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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
import copy
import math
import random
from typing import Any, Iterator, List, Tuple
import numpy as np
import torch
import torch.distributed as dist
from torch.utils.data.sampler import Sampler
class BaseSampler(Sampler):
"""Base class for standard and DataParallel Sampler.
Every subclass should implement `__iter__` method, providing a way to iterate
over indices of dataset elements.
Args:
opts: Command line arguments.
n_data_samples: Number of samples in the dataset.
is_training: Training mode or not.
"""
def __init__(
self,
opts: argparse.Namespace,
n_data_samples: int,
is_training: bool = False,
*args,
**kwargs,
) -> None:
# max between 1 and number of available GPUs. 1 because for supporting CPUs
n_gpus: int = max(1, torch.cuda.device_count())
batch_size_gpu0: int = get_batch_size_from_opts(opts, is_training=is_training)
n_samples_per_gpu = int(math.ceil(n_data_samples * 1.0 / n_gpus))
total_size = n_samples_per_gpu * n_gpus
indexes = [idx for idx in range(n_data_samples)]
# This ensures that we can divide the batches evenly across GPUs
indexes += indexes[: (total_size - n_data_samples)]
assert total_size == len(indexes)
self.img_indices = indexes
self.n_samples = total_size
self.batch_size_gpu0 = batch_size_gpu0
self.n_gpus = n_gpus
self.shuffle = True if is_training else False
self.epoch = 0
self.num_repeats = 1
self.trunc_rep_aug = False
self.start_shuffling_from_epoch = getattr(
opts, "sampler.start_shuffling_from_epoch"
)
if is_training:
# enable these arguments for repeated data augmentation
# https://openaccess.thecvf.com/content_CVPR_2020/papers/Hoffer_Augment_Your_Batch_Improving_Generalization_Through_Instance_Repetition_CVPR_2020_paper.pdf
self.num_repeats = getattr(opts, "sampler.num_repeats")
self.trunc_rep_aug = getattr(opts, "sampler.truncated_repeat_aug_sampler")
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
if cls != BaseSampler:
# Don't re-register arguments in subclasses that don't override `add_arguments()`.
return parser
# add sampler-specific arguments
group = parser.add_argument_group(cls.__name__)
group.add_argument(
"--sampler.name",
type=str,
default=None,
help=(
"Name of the sampler. Defaults to None (i.e., user needs to specify the sampler if using MAP-style datasets)."
"Note that this argument is not applicable to iterable datasets."
),
)
group.add_argument(
"--sampler.num-repeats",
type=int,
default=1,
help="Repeat the training dataset samples by this factor in each epoch (aka repeated augmentation). "
"This effectively increases samples per epoch. As an example, if dataset has 10000 samples "
"and sampler.num_repeats is set to 2, then total samples in each epoch would be 20000. "
"Defaults to 1.",
)
group.add_argument(
"--sampler.truncated-repeat-aug-sampler",
action="store_true",
default=False,
help="When enabled, it restricts the sampler to load a subset of the training dataset such that"
"number of samples obtained after repetition are the same as the original dataset."
"As an example, if dataset has 10000 samples, sampler.num_repeats is set to 2, and "
"sampler.truncated_repeat_aug_sampler is enabled, then the sampler would sample "
"10000 samples in each epoch. Defaults to False.",
)
group.add_argument(
"--sampler.start-shuffling-from-epoch",
default=0,
type=int,
help="Shuffle data indices during training from this epoch onwards. Defaults to 0 (i.e., shuffle from the first epoch).",
)
return parser
def get_indices(self) -> List[int]:
"""Returns a list of indices of dataset elements to iterate over.
...note:
If repeated augmentation is enabled, then indices will be repeated.
"""
img_indices = copy.deepcopy(self.img_indices)
if self.shuffle:
random.seed(self.epoch)
if self.epoch >= self.start_shuffling_from_epoch:
random.shuffle(img_indices)
if self.num_repeats > 1:
# Apply repeated augmentation
"""Assume that we have [0, 1, 2, 3] samples. With repeated augmentation,
we first repeat the samples [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3] and then select 4
samples [0, 0, 0, 1]. Note that we do shuffle at the beginning, so samples are not the
same at every iteration.
"""
n_samples_before_repeat = len(img_indices)
img_indices = np.repeat(img_indices, repeats=self.num_repeats)
img_indices = list(img_indices)
if self.trunc_rep_aug:
img_indices = img_indices[:n_samples_before_repeat]
return img_indices
def __iter__(self) -> Iterator[Tuple[Any, ...]]:
raise NotImplementedError
def __len__(self) -> int:
return len(self.img_indices) * (1 if self.trunc_rep_aug else self.num_repeats)
def set_epoch(self, epoch: int) -> None:
"""Helper function to set epoch in each sampler."""
self.epoch = epoch
def update_scales(
self, epoch: int, is_master_node: bool = False, *args, **kwargs
) -> None:
"""Helper function to update scales in each sampler. This is typically useful in variable-batch sampler.
Subclass is expected to implement this function. By default, we do not do anything
"""
def update_indices(self, new_indices: List[int]) -> None:
"""Update indices to new indices. This function might be useful for sample-efficient training."""
self.img_indices = new_indices
def extra_repr(self) -> str:
extra_repr_str = (
f"\n\t num_repeat={self.num_repeats}"
f"\n\t trunc_rep_aug={self.trunc_rep_aug}"
)
return extra_repr_str
def __repr__(self) -> str:
return "{}({}\n)".format(self.__class__.__name__, self.extra_repr())
class BaseSamplerDDP(Sampler):
"""Base class for DistributedDataParallel Sampler.
Every subclass should implement `__iter__` method, providing a way to iterate
over indices of dataset elements.
Args:
opts: Command line arguments.
n_data_samples: Number of samples in the dataset.
is_training: Training or validation mode.
"""
def __init__(
self,
opts: argparse.Namespace,
n_data_samples: int,
is_training: bool = False,
*args,
**kwargs,
) -> None:
batch_size_gpu0: int = get_batch_size_from_opts(opts, is_training=is_training)
if not dist.is_available():
raise RuntimeError("Requires distributed package to be available")
num_replicas = dist.get_world_size()
rank = dist.get_rank()
gpus_node_i = max(1, torch.cuda.device_count())
num_samples_per_replica = int(math.ceil(n_data_samples * 1.0 / num_replicas))
total_size = num_samples_per_replica * num_replicas
img_indices = [idx for idx in range(n_data_samples)]
img_indices += img_indices[: (total_size - n_data_samples)]
assert len(img_indices) == total_size
self.img_indices = img_indices
self.n_samples_per_replica = num_samples_per_replica
self.shuffle = True if is_training else False
self.epoch = 0
self.rank = rank
self.batch_size_gpu0 = batch_size_gpu0
self.num_replicas = num_replicas
self.skip_sample_indices = []
self.node_id = rank // gpus_node_i
self.num_nodes = max(1, num_replicas // gpus_node_i)
self.local_rank = rank % gpus_node_i
self.num_gpus_node_i = gpus_node_i
self.sharding = False
self.num_repeats = 1
self.trunc_rep_aug = False
self.disable_shuffle_sharding = False
if is_training:
self.sharding = getattr(opts, "sampler.use_shards")
self.num_repeats = getattr(opts, "sampler.num_repeats")
self.trunc_rep_aug = getattr(opts, "sampler.truncated_repeat_aug_sampler")
self.disable_shuffle_sharding = getattr(
opts, "sampler.disable_shuffle_sharding"
)
sample_multiplier = 1 if self.trunc_rep_aug else self.num_repeats
self.n_samples_per_replica = num_samples_per_replica * sample_multiplier
self.start_shuffling_from_epoch = getattr(
opts, "sampler.start_shuffling_from_epoch"
)
def get_indices_rank_i(self) -> List[int]:
"""Returns a list of indices of dataset elements for each rank to iterate over.
...note:
1. If repeated augmentation is enabled, then indices will be repeated.
2. If sharding is enabled, then each rank will process a subset of the dataset.
"""
img_indices = copy.deepcopy(self.img_indices)
if self.shuffle:
random.seed(self.epoch)
if self.sharding:
"""If we have 8 samples, say [0, 1, 2, 3, 4, 5, 6, 7], and we have two nodes,
then node 0 will receive first 4 samples and node 1 will receive last 4 samples.
note:
This strategy is useful when dataset is large and we want to process subset of dataset on each node.
"""
# compute number pf samples per node.
# Each node may have multiple GPUs
# Node id = rank // num_gpus_per_rank
samples_per_node = int(math.ceil(len(img_indices) / self.num_nodes))
indices_node_i = img_indices[
self.node_id
* samples_per_node : (self.node_id + 1)
* samples_per_node
]
# Ensure that each node has equal number of samples
if len(indices_node_i) < samples_per_node:
indices_node_i += indices_node_i[
: (samples_per_node - len(indices_node_i))
]
# Note: For extremely large datasets, we may want to disable shuffling for efficient data loading
if (
not self.disable_shuffle_sharding
and self.epoch >= self.start_shuffling_from_epoch
):
# shuffle the indices within a node.
random.shuffle(indices_node_i)
if self.num_repeats > 1:
"""Assume that we have [0, 1, 2, 3] samples in rank_i. With repeated augmentation,
we first repeat the samples [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3] and then select 4
samples [0, 0, 0, 1]. Note shuffling at the beginning
"""
# Apply repeated augmentation
n_samples_before_repeat = len(indices_node_i)
indices_node_i = np.repeat(indices_node_i, repeats=self.num_repeats)
indices_node_i = list(indices_node_i)
if self.trunc_rep_aug:
indices_node_i = indices_node_i[:n_samples_before_repeat]
# divide the samples among each GPU in a node
indices_rank_i = indices_node_i[
self.local_rank : len(indices_node_i) : self.num_gpus_node_i
]
else:
"""If we have 8 samples, say [0, 1, 2, 3, 4, 5, 6, 7], and we have two nodes,
then node 0 will receive [0, 2, 4, 6] and node 1 will receive [1, 3, 4, 7].
note:
This strategy is useful when each data sample is stored independently, and is
default in many frameworks
"""
if self.epoch >= self.start_shuffling_from_epoch:
random.shuffle(img_indices)
if self.num_repeats > 1:
# Apply repeated augmentation
n_samples_before_repeat = len(img_indices)
img_indices = np.repeat(img_indices, repeats=self.num_repeats)
img_indices = list(img_indices)
if self.trunc_rep_aug:
img_indices = img_indices[:n_samples_before_repeat]
# divide the samples among each GPU in a node
indices_rank_i = img_indices[
self.rank : len(img_indices) : self.num_replicas
]
else:
indices_rank_i = img_indices[
self.rank : len(self.img_indices) : self.num_replicas
]
return indices_rank_i
def __iter__(self) -> Iterator[Tuple[Any, ...]]:
raise NotImplementedError
def __len__(self) -> int:
return (len(self.img_indices) // self.num_replicas) * (
1 if self.trunc_rep_aug else self.num_repeats
)
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
if cls != BaseSamplerDDP:
# Don't re-register arguments in subclasses that don't override `add_arguments()`.
return parser
group = parser.add_argument_group(cls.__name__)
group.add_argument(
"--sampler.use-shards",
action="store_true",
default=False,
help="Use data sharding. Only applicable to DDP. Defaults to False.",
)
group.add_argument(
"--sampler.disable-shuffle-sharding",
action="store_true",
default=False,
help="Disable shuffling while sharding for extremely large datasets. Defaults to False.",
)
return parser
def set_epoch(self, epoch: int) -> None:
"""Helper function to set epoch in each sampler."""
self.epoch = epoch
def update_scales(
self, epoch: int, is_master_node: bool = False, *args, **kwargs
) -> None:
"""Helper function to update scales in each sampler. This is typically useful in variable-batch sampler
Subclass is expected to implement this function. By default, we do not do anything
"""
def update_indices(self, new_indices: List[int]) -> None:
"""Update indices to new indices. This function might be useful for sample-efficient training."""
self.img_indices = new_indices
def extra_repr(self) -> str:
extra_repr_str = (
f"\n\t num_repeat={self.num_repeats}"
f"\n\t trunc_rep_aug={self.trunc_rep_aug}"
f"\n\t sharding={self.sharding}"
f"\n\t disable_shuffle_sharding={self.disable_shuffle_sharding}"
)
return extra_repr_str
def __repr__(self):
return "{}({}\n)".format(self.__class__.__name__, self.extra_repr())
def get_batch_size_from_opts(
opts: argparse.Namespace, is_training: bool = False
) -> int:
"""Helper function to extract batch size for training or validation/test
Args:
opts: command line argument
is_training: Training or validation mode. Default: False
Returns:
Returns an integer
"""
batch_size_gpu0 = int(
getattr(opts, "dataset.train_batch_size0")
if is_training
else getattr(opts, "dataset.val_batch_size0")
)
return batch_size_gpu0