#6807·espnet

[NPU] synchronize_batches() is a silent no-op on non-CUDA accelerators

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

What happens

synchronize_batches() in espnet2/speechlm/dataloader/batch.py is gated on CUDA and creates its collective tensors with device="cuda":

python
if not torch.cuda.is_available() or not dist.is_initialized():
    return batches
n_batches_tensor = torch.tensor([n_batches], dtype=torch.long, device="cuda")

On a non-CUDA accelerator (e.g. Ascend NPU, where torch.cuda.is_available() is False while torch.npu.is_available() is True) the early return turns the function into a silent no-op: ranks whose sharded dataset produces a different number of batches are never padded to a common length. The docstring states the function "Ensures all GPU ranks have the same number of batches", so silently skipping the synchronization can desync distributed training instead of failing loudly.

Real-machine evidence

Ascend 910B4, 2 processes via torchrun, real HCCL process group, synchronize_batches() with the unmodified file (rank 0 has 5 batches, rank 1 has 3):

[rank0] current npu device=0 in=5 out=5 expected_out=5
[rank1] current npu device=1 in=3 out=3 expected_out=5      <- not synchronized

Proposed fix

Resolve the current accelerator instead of assuming CUDA, and drop the CUDA-only gate:

python
device = _current_accelerator_device()   # torch.accelerator, else cuda
if device is None:
    return batches                      # CPU-only run

With the fix the same script prints in=3 out=5 on rank 1. I have it ready and verified on the same 2-rank HCCL setup - opening a PR right after this.