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

Aperta
#10,395 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #10396 di @xuilen — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
numpy, python
Ambito
data

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
16.2k
Fork
1.8k
Merge medio
1g 14h
PR unite (30g)
68

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelscope/ms-swift

Tutte le issue di modelscope/ms-swift

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.