@@ -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