Skip to content

Commit 3b6eff2

Browse files
committed
change behavior when batch_size > ready_indexes
Signed-off-by: 0oshowero0 <o0shower0o@outlook.com>
1 parent 0af123b commit 3b6eff2

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

tests/test_samplers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,8 @@ def test_rank_aware_sampler_batch_size_larger_than_ready(self):
568568
# When world_size == dp_world_size, fetches_per_batch=1, consumed returned immediately
569569
sampled, consumed = sampler.sample(ready_indexes, batch_size, dp_group=0, dp_world_size=2, world_size=2)
570570

571-
assert sampled == [0, 1]
572-
assert consumed == [0, 1]
573-
assert len(sampled) == len(ready_indexes)
571+
assert sampled == []
572+
assert consumed == []
574573

575574
def test_rank_aware_sampler_zero_batch_size(self):
576575
"""Test behavior with zero batch size."""

transfer_queue/sampler/rank_aware_sampler.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class RankAwareSampler(BaseSampler):
3232
- First rank in a DP group to call :meth:`sample` performs actual sampling from
3333
``ready_indexes`` and caches the result
3434
- Subsequent ranks in the same DP group retrieve the cached indices
35-
- Once all ranks in the DP group have fetched their samples, the indices are
36-
marked as consumed
35+
- Once all ranks in the DP group have fetched their samples, the cached state is
36+
cleaned up.
3737
3838
3939
Please refer to our roadmap for more details:
@@ -83,36 +83,34 @@ def sample(
8383
**kwargs: Additional keyword arguments (ignored).
8484
8585
Returns:
86-
List of sampled global indices. The length is
87-
min(batch_size, len(ready_indexes)), and may be smaller than
88-
batch_size if fewer ready samples are available.
86+
List of sampled global indices. Typically, has length `batch_size`,
87+
or returns an empty list if samples are insufficient.
8988
9089
List of global indices that should be labeled as consumed
9190
(will never be retrieved by other dp_groups in the future).
9291
9392
Raises:
9493
RuntimeError: If ``world_size`` is not divisible by ``dp_world_size``.
95-
96-
Note:
97-
The ``world_size // dp_world_size`` calculation determines how many
98-
times each batch should be fetched (once per TP/PP/... rank group).
9994
"""
10095

10196
# Check if this DP group already has sampled data cached
10297
data_for_dp_group = self._states.get(dp_group, None)
10398

10499
# Calculate how many times this batch should be fetched across all ranks
105-
if world_size % dp_world_size != 0:
100+
if dp_world_size <= 0 or world_size % dp_world_size != 0:
106101
raise RuntimeError(f"world_size ({world_size}) is not divisible by dp_world_size ({dp_world_size})")
107102

108103
fetches_per_batch = world_size // dp_world_size
109104

110105
if data_for_dp_group is None:
111-
# Initialize state for this DP group
112-
self._states[dp_group] = {}
113-
114106
# Select first batch_size indices from ready_indexes
115107
sampled_indexes = ready_indexes[:batch_size]
108+
109+
if len(sampled_indexes) < batch_size:
110+
return [], []
111+
112+
# Initialize state for this DP group
113+
self._states[dp_group] = {}
116114
consumed_indexes = sampled_indexes
117115

118116
# Cache the sampled indices for other ranks in this DP group

0 commit comments

Comments
 (0)