Wrong GPU results with donate_argnums depending on output order
Description
The return value of a function with donate_argnums can give incorrect results on CUDA GPUs depending on output order. For example, the following function:
def f(state, val):
y = val - val
ys = (y, y, y)
return (ys, state)
ys, new_state = jax.jit(f, donate_argnums=(0,))(
(jnp.float32(1), jnp.float32(2), jnp.float32(3)),
jnp.float32(0),
)
The result is
new_state (Array(1., dtype=float32), Array(2., dtype=float32), Array(2., dtype=float32))
ys (Array(0., dtype=float32), Array(2., dtype=float32), Array(0., dtype=float32))
While the expected result should be (note the last item of new_state and the second item of ys):
new_state (Array(1., dtype=float32), Array(2., dtype=float32), Array(3., dtype=float32))
ys (Array(0., dtype=float32), Array(0., dtype=float32), Array(0., dtype=float32))
If we don't specify donate_argnums or return (new_state, ys) instead of (ys, new_state), the results will be the expected one.
This problem also does not happen on CPUs, or if the state and result tuple contain less than 3 items.
Full script
import os
import jax
from jax import numpy as jnp
donate = os.environ.get("DONATE", "1") != "0"
state_first = os.environ.get("STATE_FIRST", "0") != "0"
def f(state, val):
y = val - val
ys = (y, y, y)
return (state, ys) if state_first else (ys, state)
out = jax.jit(f, donate_argnums=(0,) if donate else ())(
(jnp.float32(1), jnp.float32(2), jnp.float32(3)),
jnp.float32(0),
)
new_state, ys = out if state_first else out[::-1]
print("new_state", new_state)
print("ys", ys)
assert all(jnp.array_equal(y, jnp.float32(0)) for y in ys)
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.11.0
jaxlib: 0.11.0
numpy: 2.4.4
python: 3.12.13 (main, Apr 14 2026, 14:29:00) [Clang 22.1.3 ]
device info: NVIDIA A100-SXM4-80GB-1, 1 local devices"
process_count: 1
platform: uname_result(system='Linux', node='', release='5.15.152.bsk.10-amd64', version='#5.15.152.bsk.10 SMP Debian 5.15.152.bsk.10 Tue Aug 19 08:45:09 ', machine='x86_64')
NVIDIA-SMI 535.261.03 Driver Version: 535.261.03 CUDA Version: 12.2
The mechanism is exactly what your title suggests, and it's fully traceable in jax's own Python source. _set_up_aliases in jax/_src/interpreters/mlir.py (~line 1490) builds implicit donation aliases with a plain FIFO queue keyed only on (aval, memory_kind), shape, dtype, and memory kind, with zero awareness of which output actually derives from which input. It walks the flattened output leaves in pytree order and greedily hands each one the next available donated input of matching shape. That's precisely why reordering (ys, state) to (state, ys) changes the outcome: it changes which leaf gets matched first in the FIFO queue, and why you need 3+ same-shaped leaves to see it.
What I can't trace from this repo is the second half: why a mismatched alias hint actually produces corrupted values on GPU instead of just silently-safe extra buffer reuse. That's XLA's buffer-assignment and copy-insertion machinery, which lives in openxla/xla as a compiled dependency, not in jax-ml/jax's Python source, so I can't confirm the GPU-vs-CPU asymmetry beyond noting the aliasing hint itself is identical on both backends and CPU apparently tolerates it safely while GPU doesn't.
For what it's worth, this isn't a first occurrence: #20088 (closed) reported a similar donation-plus-something producing garbage values, different trigger (sharding constraint, CPU) but the same underlying shape-based-aliasing-without-dataflow-awareness category. Given the fix here means making _set_up_aliases dataflow-aware rather than shape-aware, a real change to core donation semantics, that feels like something for the jax team to scope rather than something to hand a PR for unprompted.
Still reproduces on jax/jaxlib 0.12.1 (RTX 4060). Same pattern as the report: only GPU + donation + the (ys, state) order gives wrong values, and CPU is fine.
I don't think JAX's alias pairing is the problem. The HLO going into XLA is legal: tuple(sub, sub, sub, p0, p1, p2) with outputs {0,1,2} aliased to params {0,1,2}, and copy insertion starts out with one copy per output. It goes wrong after scheduling. The GPU compiler runs RemoveUnnecessaryCopies a second time, which drops all three copies of sub, and then relies on AddSpecialCaseCopies to make the root buffers distinct again. That only adds one back.
The cause is in ComputeSpecialCaseCopies (xla/hlo/transforms/copy_insertion.cc). When a repeated buffer shows up at an aliased output, it copies the index where the buffer was first seen, but seen keeps pointing at that first index. With three repeats, the third one copies {0} again, so {1} and {2} still share a buffer even though they're aliased to different parameters. The buffer assignment dump shows it: parameter 1's allocation holds sub, state_1 and state_2. With two repeats the logic is fine, which is why you need at least three items.
I have a small fix (point seen at the index that keeps the buffer) and a unit test in copy_insertion_test.cc that fails on main and passes with the fix. I can open it as an openxla/xla PR, unless someone on the XLA side is already on it.