Skip to content

Commit eba5b12

Browse files
committed
add alias paddle.utils.data.DistributedSampler
1 parent 44b28a7 commit eba5b12

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

python/paddle/utils/data/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
TensorDataset,
2626
random_split,
2727
)
28+
from .distributed import DistributedSampler
2829
from .sampler import (
2930
BatchSampler,
3031
RandomSampler,
@@ -42,6 +43,7 @@
4243
'Subset',
4344
'random_split',
4445
'BatchSampler',
46+
'DistributedSampler',
4547
'RandomSampler',
4648
'Sampler',
4749
'SequentialSampler',
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from typing import TYPE_CHECKING
18+
19+
from paddle.io import DistributedBatchSampler
20+
21+
if TYPE_CHECKING:
22+
from collections.abc import Sized
23+
24+
25+
class DistributedSampler(DistributedBatchSampler):
26+
def __init__(
27+
self,
28+
dataset: Sized,
29+
num_replicas: int | None = None,
30+
rank: int | None = None,
31+
shuffle: bool = True,
32+
drop_last: bool = False,
33+
) -> None:
34+
super().__init__(
35+
dataset,
36+
1,
37+
num_replicas,
38+
rank,
39+
shuffle,
40+
drop_last,
41+
)

test/legacy_test/test_api_compatibility_part1.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,5 +2396,54 @@ def test_Compatibility(self):
23962396
paddle.set_rng_state(new_state=states)
23972397

23982398

2399+
# Test DistributedSampler compatibility
2400+
class TestDistributedSamplerAPI(unittest.TestCase):
2401+
def setUp(self):
2402+
np.random.seed(2025)
2403+
2404+
class SimpleDataset:
2405+
def __init__(self, size):
2406+
self.size = size
2407+
2408+
def __len__(self):
2409+
return self.size
2410+
2411+
self.dataset = SimpleDataset(100)
2412+
2413+
def test_dygraph_Compatibility(self):
2414+
"""Test DistributedSampler as alias for DistributedBatchSampler"""
2415+
# 1. positional arguments
2416+
sampler1 = paddle.utils.data.DistributedSampler(
2417+
self.dataset, 2, 0, False, False
2418+
)
2419+
# 2. keyword arguments
2420+
sampler2 = paddle.utils.data.DistributedSampler(
2421+
dataset=self.dataset,
2422+
num_replicas=2,
2423+
rank=0,
2424+
shuffle=False,
2425+
drop_last=False,
2426+
)
2427+
# Verify both samplers produce same batches
2428+
batches1 = list(sampler1)
2429+
batches2 = list(sampler2)
2430+
self.assertEqual(len(batches1), len(batches2))
2431+
for b1, b2 in zip(batches1, batches2):
2432+
self.assertEqual(b1, b2)
2433+
2434+
def test_set_epoch(self):
2435+
"""Test set_epoch method"""
2436+
sampler = paddle.utils.data.DistributedSampler(
2437+
self.dataset, num_replicas=2, rank=0, shuffle=True
2438+
)
2439+
# Collect samples from epoch 0
2440+
sampler.set_epoch(0)
2441+
batches0 = list(sampler)
2442+
# Collect samples from epoch 1
2443+
sampler.set_epoch(1)
2444+
batches1 = list(sampler)
2445+
self.assertEqual(len(batches0), len(batches1))
2446+
2447+
23992448
if __name__ == '__main__':
24002449
unittest.main()

0 commit comments

Comments
 (0)