#41025·jax

CPU: jitted lax.scan over a chunked linear-attention layer returns wrong values in some processes

Author: AshishKumar4Created Sep 28, 2026Updated Sep 28, 2026

Description

On CPU, a jitted lax.scan over a small chunked linear-attention layer returns wrong values in some processes: errors up to 1e33 against outputs of scale 281, with the same program and inputs. The same function run eagerly, or jitted in other processes, gives the exact result. It looks like a read of uninitialized memory.

python
import math

import jax
import jax.numpy as jnp
import numpy as np

CHUNK = 16


def chunks(x, B, H, T):
    moved = jnp.moveaxis(x, 2, 1)
    return jnp.moveaxis(moved.reshape(B, H, T // CHUNK, CHUNK, *moved.shape[3:]), 2, 0)


def strictly_lower_inverse(a):
    inv = jnp.broadcast_to(jnp.eye(a.shape[-1], dtype=a.dtype), a.shape)
    power = a
    for _ in range(math.ceil(math.log2(a.shape[-1]))):
        inv, power = inv + power @ inv, power @ power
    return inv


def delta_rule(query, key, value, g, beta):
    B, S, H, Dk = key.shape
    pad = (CHUNK - S % CHUNK) % CHUNK
    query, key, value, g = (jnp.pad(x, ((0, 0), (0, pad), (0, 0), (0, 0))) for x in (query, key, value, g))
    beta = jnp.pad(beta, ((0, 0), (0, pad), (0, 0)))
    T = S + pad
    q_c, k_c, g_c = (chunks(x, B, H, T) for x in (query * Dk**-0.5, key, g))
    kb_c, vb_c = (chunks(x * beta[..., None], B, H, T) for x in (key, value))
    gc = jnp.cumsum(g_c, axis=-2)
    inclusive = jnp.tril(jnp.ones((CHUNK, CHUNK), bool))[..., None]
    decay = jnp.where(inclusive, jnp.exp(jnp.where(inclusive, gc[..., :, None, :] - gc[..., None, :, :], 0.0)), 0.0)
    strict = jnp.tril(jnp.ones((CHUNK, CHUNK), bool), -1)
    inv = strictly_lower_inverse(jnp.where(strict, -jnp.einsum("...sd,...td,...std->...st", kb_c, k_c, decay), 0.0))

    def one_chunk(s, step):
        q_i, k_i, v_i, decay_i, kd_i, gc_i = step
        v_i = v_i - kd_i @ s
        out = (q_i * jnp.exp(gc_i)) @ s + jnp.einsum("...sd,...td,...std->...st", q_i, k_i, decay_i) @ v_i
        last = gc_i[..., -1:, :]
        s = s * jnp.exp(last)[..., 0, :, None] + jnp.swapaxes(k_i * jnp.exp(last - gc_i), -1, -2) @ v_i
        return s, out

    state = jnp.zeros((B, H, Dk, value.shape[-1]), jnp.float32)
    _, core = jax.lax.scan(one_chunk, state, (q_c, k_c, inv @ vb_c, decay, inv @ (kb_c * jnp.exp(gc)), gc))
    core = jnp.moveaxis(core, 0, 2).reshape(B, H, T, -1)
    return jnp.moveaxis(core, 1, 2)[:, :S]


B, S, H, D = 2, 12, 4, 32
keys = jax.random.split(jax.random.key(2), 5)
q, k, v = (jax.random.normal(key, (B, S, H, D)) for key in keys[:3])
g = -jax.nn.softplus(jax.random.normal(keys[3], (B, S, H, D)))
beta = jax.nn.sigmoid(jax.random.normal(keys[4], (B, S, H)))
weights = jnp.asarray([1.0, 0.5])


def scanned(h):
    return jax.lax.scan(lambda h, w: (h + delta_rule(q * w, k, h, g, beta), None), h, weights)[0]


with jax.disable_jit():
    expected = np.asarray(scanned(v))
got = np.asarray(jax.jit(scanned)(v))
print(f"max abs error {np.abs(got - expected).max():.3g} (output scale {np.abs(expected).max():.3g})")

Eight fresh processes with jax 0.11.2 (JAX_PLATFORMS=cpu python repro.py):

max abs error 0 (output scale 281)
max abs error 0 (output scale 281)
max abs error 0 (output scale 281)
max abs error 1.77e+21 (output scale 281)
max abs error 0 (output scale 281)
max abs error 3.61e+33 (output scale 281)
max abs error 0 (output scale 281)
max abs error 0 (output scale 281)

Every run should print 0. The outer lax.scan over the two layer weights matters: in our runs the same two layers applied in a Python loop under jax.jit were always exact. On an NVIDIA GPU (RTX 4080) the program is exact.

System info (python version, jaxlib version, accelerator, etc.)

jax:    0.11.2
jaxlib: 0.11.2
numpy:  2.5.3
python: 3.12.13
device info: cpu-1, 1 local devices
platform: Linux 7.0.10, x86_64 (Intel i9-12900K)