mlx icon indicating copy to clipboard operation
mlx copied to clipboard

init(backend="any") selects ring singleton instead of JACCL when launched via mlx.launch --backend jaccl

Open machiabeli opened this issue 7 months ago • 2 comments

Observed Behavior

When using mlx.launch --backend jaccl --hostfile hosts.json -- python3 -m mlx_lm.server ..., the server process calls mx.distributed.init() (which defaults to backend="any"). The init function tries ring before jaccl, and ring creates a singleton group of size 1 because MLX_RANK is set but MLX_HOSTFILE is not. JACCL is never attempted.

The result is that distributed inference silently falls back to single-node, even though JACCL env vars (MLX_JACCL_COORDINATOR, MLX_IBV_DEVICES) are correctly set by the launcher.

Expected Behavior

When mlx.launch --backend jaccl sets JACCL-specific env vars, init(backend="any") should initialize JACCL, not ring.

Steps to Reproduce

mlx.launch --backend jaccl --hostfile hosts.json -- \
  python3 -c "import mlx.core as mx; g = mx.distributed.init(); print(f'rank={g.rank()} size={g.size()}')"

# Expected: size=N (number of nodes in hostfile)
# Actual: size=1 on each rank (ring singleton)

Root Cause

In distributed.cpp, init(backend="any") tries backends in order: nccl -> ring -> mpi -> jaccl. On Mac, ring::init(false) sees MLX_RANK (set by launch_jaccl) but no MLX_HOSTFILE, creates a size-1 group, and returns non-null. The init function treats this as success and never tries JACCL.

Possible Approaches

  1. Env var hint from launcher: launch_jaccl() sets MLX_DISTRIBUTED_BACKEND=jaccl, and init(backend="any") checks this env var first before the default try order.

  2. Reorder try order: Try jaccl before ring when JACCL env vars are present.

  3. Fix ring singleton: ring::init() could return nullptr when MLX_HOSTFILE is not set, instead of creating a singleton.

Each has trade-offs. Would love to hear which approach (if any) the team prefers.

Workaround

We have been using a Python import hook (_mlx_backend_fix.py + .pth file) on all cluster nodes since March 2026 that patches mx.distributed.init to read MLX_DISTRIBUTED_BACKEND from the environment. It works but must be redeployed after every pip install --upgrade mlx.

Environment

  • macOS 26.4 (M3 Ultra, 512GB)
  • MLX 0.31.2
  • 3-node Thunderbolt 5 RDMA cluster
  • JACCL and JACCL-Ring backends

machiabeli avatar Apr 22 '26 18:04 machiabeli

Ring isn't the cause. ring::init() returns nullptr when MLX_HOSTFILE is unset instead of creating a singleton, so any moves past ring. Same code in v0.30.6, v0.31.2 and current main.

It falls into MPI, which is also tried before JACCL.

mpi::init(false) succeeds as soon as libmpi.dylib dlopens and MPI_Init returns MPI_SUCCESS. Outside mpirun, Open MPI's singleton init does exactly that and hands back an MPI_COMM_WORLD of size 1. That counts as success, so the group is returned as "mpi" and JACCL is never attempted.

Reproduced on two nodes over Thunderbolt 5 (M5 Max and M4 Pro, macOS 26.5.1, 0.32.1). Same command and same JACCL environment throughout; the only variable is whether Open MPI is installed:

Open MPI call result
no init() size=2 on both ranks
yes init() size=1, rank=0 on both ranks
yes init() with MLX_MPI_LIBNAME=/nonexistent.dylib size=2 on both ranks
yes init(backend="jaccl") size=2 on both ranks

brew install open-mpi was the entire difference between the first two rows. The third row rules out ring: with MPI hidden, ring is still tried first, still declines, and JACCL forms the group.

This is easy to hit because the distributed docs recommend installing Open MPI, so anyone who set MPI up first and later moved to JACCL has libmpi.dylib on disk. The failure is also invisible. A size-1 MPI group is indistinguishable from the EmptyGroup returned when nothing initializes, so the model loads and generates, just with no tensor parallelism.

@machiabeli, worth confirming that's your case. On a cluster node:

python -c "import mlx.core as mx; print(mx.distributed.is_available('mpi'))"

If that prints True, init(backend="jaccl") restores JACCL with no code change.

I've opened #4210, which warns here rather than changing which backend wins, since the selection order is a call for you to make.

erwinzhang7 avatar Aug 12 '26 15:08 erwinzhang7

Closed by #4210.

zcbenz avatar Aug 16 '26 04:08 zcbenz