wait_for_everyone() raises NotImplementedError on MULTI_CPU when the machine has an MPS accelerator (barrier gets device_ids)
System Info
- accelerate: main (421af8b) — same code on v1.11.0 … v1.15.0
- torch: 2.14.0 (also reproduced with 2.8.0)
- Python 3.12, macOS 24.3 (Apple Silicon,
torch.backends.mps.is_available() == True) - No accelerate config file;
PartialState(cpu=True)/ACCELERATE_USE_CPU=1,DistributedType.MULTI_CPUwith the gloo backend
Information
- The official example scripts
- My own modified scripts
Tasks
- One of the scripts in the examples/ folder of Accelerate or an officially supported
no_trainerscript - My own task or dataset (give details below)
Reproduction
Any multi-process CPU run on a machine that has a non-CUDA accelerator (MPS here) crashes as soon as
wait_for_everyone() (or anything built on it, e.g. main_process_first()) is called:
# repro.py — run with: ACCELERATE_USE_CPU=1 torchrun --nproc_per_node 2 repro.py
from accelerate import PartialState
state = PartialState(cpu=True)
print(state.distributed_type, state.device) # DistributedType.MULTI_CPU cpu:0
state.wait_for_everyone()NotImplementedError: The distributed operator 'c10d::barrier' is not currently implemented for the MPS device.
File ".../accelerate/state.py", line 412, in wait_for_everyone
torch.distributed.barrier(device_ids=[self.local_process_index])torch.multiprocessing.spawn, no torchrun needed)"""
Minimal reproduction: `PartialState.wait_for_everyone()` crashes under `DistributedType.MULTI_CPU`
on a machine that has a non-CUDA accelerator (e.g. Apple Silicon / MPS).
Root cause: `wait_for_everyone` calls `torch.distributed.barrier(device_ids=[local_process_index])`
for MULTI_CPU too. When `device_ids` is given, torch runs the barrier on
`torch._C._get_accelerator()` (MPS here) instead of on the gloo/CPU device, and the MPS backend
has no `c10d::barrier` -> NotImplementedError. Plain `torch.distributed.barrier()` works.
Run: python repro.py
"""
import os
import sys
import traceback
import torch
import torch.multiprocessing as mp
def worker(rank, world_size, port):
os.environ.update(
{
"MASTER_ADDR": "127.0.0.1",
"MASTER_PORT": str(port),
"RANK": str(rank),
"LOCAL_RANK": str(rank),
"WORLD_SIZE": str(world_size),
}
)
from accelerate import PartialState
state = PartialState(cpu=True)
assert state.num_processes == world_size, state.num_processes
print(f"[rank {rank}] distributed_type={state.distributed_type} device={state.device}", flush=True)
try:
state.wait_for_everyone()
print(f"[rank {rank}] wait_for_everyone() OK", flush=True)
ok = True
except Exception as e: # noqa: BLE001
print(f"[rank {rank}] wait_for_everyone() FAILED: {type(e).__name__}: {str(e)[:120]}", flush=True)
traceback.print_exc(limit=1)
ok = False
torch.distributed.destroy_process_group()
sys.exit(0 if ok else 1)
if __name__ == "__main__":
print(f"torch {torch.__version__}, accelerator: {torch._C._get_accelerator()}")
world_size = 2
try:
mp.spawn(worker, args=(world_size, 29677), nprocs=world_size, join=True)
print("RESULT: PASS")
except Exception as e: # noqa: BLE001
print(f"RESULT: FAIL ({type(e).__name__})")
sys.exit(1)What happens: since #3744, wait_for_everyone() passes device_ids=[local_process_index] to
torch.distributed.barrier() for every distributed type, including MULTI_CPU. In torch's barrier,
when device_ids is given the barrier device becomes torch.device(torch._C._get_accelerator().type, device_ids[0])
— i.e. mps:0 on a Mac — and the _get_object_coll_device(group) == "cpu" branch that would have picked
CPU for a gloo group is skipped. MPS has no c10d::barrier, hence the error. Calling
torch.distributed.barrier() without device_ids in the same process group works fine:
# gloo, 2 processes, same machine
dist.barrier(device_ids=[rank]) # NotImplementedError: ... 'c10d::barrier' ... MPS
dist.barrier() # OKThe repo's own multi-CPU test (tests/test_cpu.py) does not catch this because require_cpu skips it whenever
torch_device != "cpu", which is the case on any Mac with MPS.
On a CUDA box the same code path would run the CPU barrier on cuda:<local_rank> (I could not verify this,
no CUDA machine at hand), which at minimum initialises a CUDA context in every process of a --cpu run.
Expected behavior
wait_for_everyone() on MULTI_CPU should synchronise on the CPU/gloo process group regardless of which
accelerator the machine happens to have, as it did before #3744. Only passing device_ids for the
non-CPU distributed types fixes it; happy to open a PR with that one-line change plus a regression test.
Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.
Source: huggingface/accelerate