#6812·espnet

speechlm: synchronize_batches() cannot pad a rank that ends up with zero batches

Author: li-lizheCreated Sep 26, 2026Updated Sep 26, 2026

Follow-up to #6808, split out from a CodeRabbit review comment on that PR because it is pre-existing for CUDA and out of scope for that diff.

What

synchronize_batches() in espnet2/speechlm/dataloader/batch.py pads ranks that have fewer batches than the maximum with

python
batches = batches + batches[-(tgt_n_batches - n_batches):]

For a rank with n_batches == 0, batches[-k:] is an empty list, so the rank stays at zero batches while its peers run tgt_n_batches steps. A rank ending up with zero batches is possible: SingleDataset shards samples by rank, and batchfy() drops samples above batch_token.

The consequence is either ranks running different numbers of steps, or a hang: the rank with no batches leaves its training loop early and stops joining the collectives that the other ranks are still issuing.

This is pre-existing for CUDA, and becomes reachable for non-CUDA accelerators now that #6808 makes the synchronization actually run there instead of returning early.

Suggested fix

The batch counts are already all-gathered from every rank, so every rank can observe the condition and fail together, before any padding is applied:

python
dist.all_gather(n_batches_list, n_batches_tensor)
counts = [t.item() for t in n_batches_list]
if min(counts) == 0:
    raise RuntimeError(f"synchronize_batches: some rank has zero batches: {counts}")
tgt_n_batches = max(counts)

An alternative contract would be to synthesize batches for an empty rank, but that changes the data seen by that rank, so failing loudly seems preferable.