Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DistributedBatchSampler add num_replicas and rank #26315

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions python/paddle/incubate/hapi/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class DistributedBatchSampler(BatchSampler):
`__len__` for BatchSampler to get sample
number of data source.
batch_size(int): sample indice number in a mini-batch indices.
num_replicas(int, optional): porcess number in distributed training.
If :attr:`num_replicas` is None, :attr:`num_replicas` will be
retrieved from :code:`paddle.fluid.dygraph.parallel.ParallenEnv`.
Default None.
rank(int, optional): the rank of the current process among :attr:`num_replicas`
processes. If :attr:`rank` is None, :attr:`rank` is retrieved from
:code:`paddle.fluid.dygraph.parallel.ParallenEnv`. Default None.
shuffle(bool): whther to shuffle indices order before genrating
batch indices. Default False.
drop_last(bool): whether drop the last incomplete batch dataset size
Expand Down Expand Up @@ -84,7 +91,13 @@ def __len__(self):
break
"""

def __init__(self, dataset, batch_size, shuffle=False, drop_last=False):
def __init__(self,
dataset,
batch_size,
num_replicas=None,
rank=None,
shuffle=False,
drop_last=False):
self.dataset = dataset

assert isinstance(batch_size, int) and batch_size > 0, \
Expand All @@ -96,9 +109,21 @@ def __init__(self, dataset, batch_size, shuffle=False, drop_last=False):
assert isinstance(drop_last, bool), \
"drop_last should be a boolean number"

if num_replicas is not None:
assert isinstance(num_replicas, int) and num_replicas > 0, \
"num_replicas should be a positive integer"
self.nranks = num_replicas
else:
self.nranks = ParallelEnv().nranks

if rank is not None:
assert isinstance(rank, int) and rank >= 0, \
"rank should be a non-negative integer"
self.local_rank = rank
else:
self.local_rank = ParallelEnv().local_rank

self.drop_last = drop_last
self.nranks = ParallelEnv().nranks
self.local_rank = ParallelEnv().local_rank
self.epoch = 0
self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.nranks))
self.total_size = self.num_samples * self.nranks
Expand Down
20 changes: 17 additions & 3 deletions python/paddle/incubate/hapi/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def test_fit_dygraph(self):
def test_fit_static(self):
self.fit(False)

def test_fit_dynamic_with_rank(self):
self.fit(True, 2, 0)

def test_fit_static_with_rank(self):
self.fit(False, 2, 0)

def test_evaluate_dygraph(self):
self.evaluate(True)

Expand All @@ -184,7 +190,7 @@ def test_predict_static(self):
def test_prepare_context(self):
prepare_distributed_context()

def fit(self, dynamic):
def fit(self, dynamic, num_replicas=None, rank=None):
fluid.enable_dygraph(self.device) if dynamic else None
seed = 333
fluid.default_startup_program().random_seed = seed
Expand All @@ -204,9 +210,17 @@ def fit(self, dynamic):
np.testing.assert_allclose(result['acc'], self.acc1)

train_sampler = DistributedBatchSampler(
self.train_dataset, batch_size=64, shuffle=False)
self.train_dataset,
batch_size=64,
shuffle=False,
num_replicas=num_replicas,
rank=rank)
val_sampler = DistributedBatchSampler(
self.val_dataset, batch_size=64, shuffle=False)
self.val_dataset,
batch_size=64,
shuffle=False,
num_replicas=num_replicas,
rank=rank)

train_loader = fluid.io.DataLoader(
self.train_dataset,
Expand Down