[Bug]: split_list(contiguous=True) creates empty leading shards and starves rank 0 when num_shards > len(list)

Open
#10,395 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #10396 by @xuilen — closed without merging

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
numpy, python
Domain
data

Research direction

Read split_list and its tests in tests/utils/test_split_list.py; reproduce the two-item, four-shard example from the issue. Check that contiguous shards keep elements in order and place remainder items in the leading shards. The issue says a linked pull request is already open, so confirm its status before starting.

Written by the indexing model from the issue text.

Description

Checklist / 检查清单
  • 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]
  • 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.

Dominant language
Python
Stars
16.2k
Forks
1.8k
Avg merge
1d 14h
Merged PRs (30d)
68

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelscope/ms-swift

All issues in modelscope/ms-swift

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.