#40970·jax

Batched complex64 `jax.ops.segment_sum`/`scatter_add` returns wrong values and faults with CUDA_ERROR_ILLEGAL_ADDRESS on GPU

Author: gRox167Created Sep 25, 2026Updated Sep 28, 2026

Description

jax.vmap(jax.ops.segment_sum) on a complex64 batch returns wrong results on GPU once the batch is large enough, while the same computation row by row is correct. This is not just imprecision: the same call under jax.jit raises CUDA_ERROR_ILLEGAL_ADDRESS, i.e. the generated kernel appears to write out of bounds. float32, complex128 and CPU builds of the identical call are exact.

I hit this while debugging a batched type-1 NUFFT (MRI gridding), but the reproducer below uses only jax.ops.segment_sum -- no third-party library involved.

Minimal reproducer

python
import jax, jax.numpy as jnp, numpy as np

segments = 2**18  # 512*512, fine grid of a 256^2 image at 2x oversampling
updates_per_row = 16384  # nspread**2 (64) * 256 samples

rng = np.random.default_rng(0)
idx = jnp.asarray(rng.integers(0, segments, size=updates_per_row).astype(np.int32))


def segment_sum(row):
    return jax.ops.segment_sum(row, idx, num_segments=segments)


for n_trans in (8, 12, 16):
    x = jnp.asarray(
        (
            rng.normal(size=(n_trans, updates_per_row))
            + 1j * rng.normal(size=(n_trans, updates_per_row))
        ).astype(np.complex64)
    )
    batched = jax.vmap(segment_sum)(x)
    looped = jnp.stack([segment_sum(x[i]) for i in range(n_trans)])
    print(n_trans, float(jnp.abs(batched - looped).max() / jnp.abs(looped).max()))

Observed

 n_trans  total updates       dtype    rel err  verdict
       8         131072   complex64    ~4e-08  ok
      12         196608   complex64    ~4e-08  ok
      16         262144   complex64   1.31e+00  BUG
      16         262144     float32    ~8e-08  ok
      16         262144  complex128    ~2e-16  ok

(The digits of the ok rows move slightly between runs, 3.8e-08 to 8.3e-08; the failing row and the magnitude of the failure are stable. The complex128 row needs jax.config.update("jax_enable_x64", True) before the arrays are built; without it the same shapes give 1.31e+00 for complex64 and ~4e-08 for float32.)

On CPU (same script) every row is 0.00e+00. n_trans = 1 and the per-row loop are exact at all sizes tested, so the defect needs a batched complex64 scatter, not merely a large one.

Under jit it faults

python
x = complex64 (16, 16384)
got = jax.jit(lambda v: jax.vmap(segment_sum)(v))(x)

Consuming got dies with (3/3 runs):

jax.errors.JaxRuntimeError: INTERNAL: CUDA error: Failed to check stream capturing status:
CUDA_ERROR_ILLEGAL_ADDRESS: an illegal memory access was encountered
[executable_name='jit_true_divide']

One earlier run of the same jitted call returned a result instead of faulting, so whether it crashes or silently corrupts depends on where the out-of-bounds write lands.

Shape of the corruption

With one-hot rows (distinct content per row) at n_trans=16, rows i ≡ 2, 3 (mod 4) come back with the contents of row i + 4, and rows 14-15 are garbage. Whole rows are displaced/zeroed rather than perturbed, consistent with an out-of-bounds write into neighbouring buffers. In the NUFFT setting the same defect shows up as exact zeros for the trailing batch rows.

Size dependence

  • At segments = 2**18: 196608 updates per batch are fine, 262144 are broken.
  • The segment count is a co-factor too, not just the update count: with the same 1310720 updates per batch, a 4-row batch is correct at segments = 16384 / 65536 / 147456 and broken at 262144.
  • n_trans = 1 is exact up to 2**20 updates.

What avoids it (data, not a proposed API change)

variant result
bucket_size=4096 correct (1.4e-09)
chunking the batch (<= 3*2**16 updates per call) correct (1.2e-09)
two float32 scatters (real/imag separately) correct (1.1e-09)
bucket_size=16384 still wrong
mode="promise_in_bounds" still wrong
explicit jnp.zeros(S).at[idx].add(row) still wrong

bucket_size avoiding it points at the lowering of the duplicated-index (scatter-add) path for complex64 above this size, but I have not inspected the HLO/codegen -- happy to dump it if that helps.

Impact

Any batched complex64 scatter of this size silently returns wrong data on GPU. Concretely, nufftax's default pure-JAX NUFFT spreading path is exactly jax.vmap(segment_sum) over n_trans * m * nspread**ndim complex64 updates, so jax.vmap(nufftax.nufft2d1) at a 256x256 grid with a 4-coil batch returns exact zeros for trailing coils while a per-transform loop is correct. I am filing a separate report with that project; the reproducer above does not involve it.

Happy to run any additional configuration or provide HLO/IR dumps.

System info

jax:    0.11.2
jaxlib: 0.11.2
numpy:  2.5.3
python: 3.13.5 (main, Jul 11 2025, 22:43:46) [Clang 20.1.4 ]
device info: NVIDIA H100 PCIe, 1 local device (node has 2x H100 PCIe)
platform: Linux-5.14.0-611.54.1.el9_7.x86_64-x86_64-with-glibc2.34
XLA_PYTHON_CLIENT_PREALLOCATE=false
XLA_FLAGS=--xla_gpu_enable_analytical_sol_latency_estimator=false
jax.default_backend() == "gpu", jax_enable_x64 == False

$ nvidia-smi
NVIDIA-SMI 580.159.03   Driver Version: 580.159.03   CUDA Version: 13.0
GPU  NVIDIA H100 PCIe  (81559MiB, ECC on, MIG disabled)