[Bug]: split_list(contiguous=True) creates empty leading shards and starves rank 0 when num_shards > len(list)
Checklist / 检查清单
- [x] I have searched existing issues, and this is a new bug report. / 我已经搜索过现有的 issues,确认这是一个新的 bug report。
Bug Description / Bug 描述
When calling split_list(ori_list, num_shards, contiguous=True) where num_shards > len(ori_list) (or when remainder exists across uneven shards), the function uses np.linspace(0, len(ori_list), num_shards + 1, dtype=np.int64) for slice indexing.
Because float truncation occurs when casting np.linspace to np.int64, for example with split_list([1, 2], 4, contiguous=True):
np.linspace(0, 2, 5, dtype=np.int64)produces[0, 0, 1, 1, 2].- The resulting slices are:
- Shard 0:
ori_list[0:0]->[](Rank 0 gets empty!) - Shard 1:
ori_list[0:1]->[1] - Shard 2:
ori_list[1:1]->[] - Shard 3:
ori_list[1:2]->[2]
- Shard 0:
- Resulting shards:
[[], [1], [], [2]].
This causes Rank 0 (shard 0, usually the primary/master worker in distributed data processing) to receive an empty shard [], while items are scattered across subsequent ranks.
In contrast, split_list(..., contiguous=False) and standard partitioning tools (torch.chunk, np.array_split) always pack non-empty elements into the leading shards: [[1], [2], [], []].
How to Reproduce / 如何复现
from swift.utils import split_list
# 2 items split across 4 shards with contiguous=True
res = split_list([1, 2], 4, contiguous=True)
print(res)
# Actual output: [[], [1], [], [2]] (Rank 0 gets empty list!)
# Expected output: [[1], [2], [], []] (consistent with contiguous=False and standard chunking)
Additional Information / 补充信息
Using integer arithmetic q, r = divmod(len(ori_list), num_shards) ensures balanced contiguous slices without float truncation artifacts, and places any remainder elements in the first r shards.
A patch with updated unit tests in tests/utils/test_split_list.py is ready to submit.