Multi-process GPU: the processes of one computation compute different persistent-cache keys when only some GPUs have NVLink, and a warm run hangs in sharded autotuning
Description
On a host where only some GPUs share NVLink, the processes of one multi-process job compute different persistent compilation cache keys for the same program. Only process 0 writes the cache, so on a warm run the processes whose key differs from process 0's compile while the others load. The compiling ones then wait forever in sharded autotuning for shares from peers that never compile.
_hash_accelerator_config in jax/_src/cache_key.py hashes xla_client.get_topology_for_devices(devices).fingerprint(). On GPU that fingerprint includes the local device's NVLink active_links count. On 4x RTX 3090 with GPU0-GPU1 on NVLink and GPU2-GPU3 not, processes on GPU0/GPU1 get one fingerprint and those on GPU2/GPU3 another. For a program that spans all four processes, every other part of the key is identical. cache_key.get already removes the device assignment so the key stays the same across processes; the fingerprint breaks that.
Reproduction: one process per GPU through jax.distributed.initialize, on GPU1 (NVLink) and GPU2 (no NVLink), sharing one new cache directory. run.sh 1,2 runs it cold, then warm.
import logging
import os
import sys
import time
rank = int(sys.argv[1])
gpus = sys.argv[2].split(",")
os.environ["CUDA_VISIBLE_DEVICES"] = gpus[rank]
os.environ["JAX_ENABLE_COMPILATION_CACHE"] = "true"
os.environ["JAX_COMPILATION_CACHE_DIR"] = os.environ["REPRO_CACHE"]
os.environ["JAX_PERSISTENT_CACHE_MIN_COMPILE_TIME_SECS"] = "0"
os.environ["JAX_PERSISTENT_CACHE_MIN_ENTRY_SIZE_BYTES"] = "-1"
import jax
import jax.numpy as jnp
import numpy as np
from jax._src.lib import xla_client
from jax.sharding import NamedSharding, PartitionSpec as P
class Keys(logging.Handler):
"""Prints each compile's cache key and whether the persistent cache held it."""
def emit(self, record):
message = record.getMessage()
if "cache hit for" in message or "CACHE MISS for" in message:
module = message.split("'")[1]
key = message.rsplit("'", 2)[-2]
print(f"[{rank}] {'hit ' if 'hit for' in message else 'miss'} {module} key ...{key[-12:]}", flush=True)
logger = logging.getLogger("jax._src.compiler")
logger.setLevel(logging.DEBUG)
logger.addHandler(Keys())
logger.propagate = False
jax.distributed.initialize(coordinator_address=f"127.0.0.1:{os.environ['REPRO_PORT']}",
num_processes=len(gpus), process_id=rank)
fingerprint = xla_client.get_topology_for_devices(jax.local_devices()).fingerprint()
print(f"[{rank}] GPU{gpus[rank]} topology fingerprint {fingerprint:016x}, jax {jax.__version__}", flush=True)
def local(x, w):
for _ in range(6):
x = jnp.tanh(x @ w)
return jnp.sum(x)
def pool(x, w):
for _ in range(6):
x = jnp.tanh(x @ w)
return jnp.sum(x)
began = time.perf_counter()
device = jax.local_devices()[0]
value = float(jax.jit(local)(jax.device_put(np.ones((2048, 1536), np.float32), device),
jax.device_put(np.eye(1536, dtype=np.float32) * 0.5, device)))
print(f"[{rank}] local ran in {time.perf_counter() - began:.1f}s: {value}", flush=True)
mesh = jax.make_mesh((jax.device_count(),), ("data",))
x = jax.make_array_from_process_local_data(NamedSharding(mesh, P("data")),
np.ones((2048, 1536), np.float32) * (1 + rank))
w = jax.device_put(np.eye(1536, dtype=np.float32) * 0.5, NamedSharding(mesh, P()))
began = time.perf_counter()
value = float(jax.jit(pool)(x, w))
print(f"[{rank}] pool ran in {time.perf_counter() - began:.1f}s: {value}", flush=True)# run.sh <gpus> [python]: repro.py's cold run, then its warm run over the same new cache directory,
# one process per GPU of the comma-separated <gpus>, each run bounded to REPRO_SECONDS (240).
gpus=$1
python=${2:-python}
here=$(cd "$(dirname "$0")" && pwd)
cache=$(mktemp -d "${REPRO_CACHE_ROOT:-/tmp}/repro-cache.XXXX")
count=$(echo "$gpus" | tr , '\n' | wc -l)
for run in cold warm; do
port=$((20000 + RANDOM % 20000))
began=$(date +%s)
echo "== $run run over $cache"
for ((rank = 0; rank < count; rank++)); do
REPRO_CACHE=$cache REPRO_PORT=$port timeout -k 10 "${REPRO_SECONDS:-240}" "$python" "$here/repro.py" $rank "$gpus" 2>&1 \
| grep -E "^\[[0-9]+\] " &
done
wait
echo "== $run run took $(($(date +%s) - began)) s"
done
rm -rf "$cache"Output (jax 0.11.2), warm run, trimmed: rank 0 loads jit_pool and rank 1 misses it, and neither finishes:
[1] GPU2 topology fingerprint d5bfe87ebbdab996, jax 0.11.2
[1] miss jit_pool key ...c15ec8a6e12a
[0] GPU1 topology fingerprint 2834ad2e578f5a6a, jax 0.11.2
[0] hit jit_pool key ...51c54fe47207
== warm run took 160 sThe same program on GPUs 0,1 or 2,3, whose processes fingerprint alike, loads on both processes and runs.
Proposed fix: for a computation that spans processes, hash a fingerprint every process agrees on. Each process publishes its topology fingerprint once through the distributed client's key-value store, and _hash_accelerator_config hashes the sorted set of the fingerprints of the computation's processes. A job whose processes fingerprint alike keeps the key it has today, since a one-element set hashes the same bytes. Processes on different accelerators (compute capability, driver, cuDNN, cuBLAS) should still not share a key; in that case the computation can get no key and every process compiles. An implementation with unit tests is on AshishKumar4/jax cache-key-pool-fingerprints. With it, the warm run above gives one key on both ranks and loads on both (3 s).
Workaround: JAX_ENABLE_COMPILATION_CACHE=false for multi-process GPU jobs.
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.11.2
jaxlib: 0.11.2
numpy: 2.5.3
python: 3.12.3
device: 4x NVIDIA RTX 3090 (GPU0-GPU1 NV4, GPU2-GPU3 PHB), one process per GPUSource: jax-ml/jax