jax-ml/jax · error · ValueError
val must be scalar.
Error message
val must be scalar.
What it means
The replacement operand of pallas.triton atomic_cas must be a scalar (empty shape). A val with any non-empty shape fails the abstract-eval validation.
Source
Thrown at jax/_src/pallas/triton/primitives.py:627
"""
return _atomic_rmw(
x_ref_or_view, idx, val, mask=mask, atomic_type=AtomicOpType.XOR
)
atomic_cas_p = jax_core.Primitive("atomic_cas")
@atomic_cas_p.def_effectful_abstract_eval
def _atomic_cas_abstract_eval(ref_aval, cmp_aval, val_aval):
if cmp_aval.dtype != val_aval.dtype or cmp_aval.shape != val_aval.shape:
raise ValueError("cmp and val must have identical dtypes and shapes")
if ref_aval.shape:
raise ValueError("ref must be scalar.")
if cmp_aval.shape:
raise ValueError("cmp must be scalar.")
if val_aval.shape:
raise ValueError("val must be scalar.")
return jax_core.ShapedArray(val_aval.shape, val_aval.dtype), {
state.WriteEffect(0)
}
def atomic_cas(ref, cmp, val):
"""Performs an atomic compare-and-swap of the value in the ref with the
given value.
Args:
ref: The ref to operate on.
cmp: The expected value to compare against.
val: The value to swap in.
Returns:
The value at the given index prior to the atomic operation.
"""View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Select the single element you want to write: p.atomic_cas(ref[i], cmp, vals[i]) and ensure vals[i] is scalar
- Reshape/extract scalars with .reshape(()) where appropriate
Example fix
# before p.atomic_cas(ref[0], cmp, val_block) # val_block.shape == (4,) # after p.atomic_cas(ref[0], cmp, val_block[0])
Defensive patterns
Strategy: type-guard
Validate before calling
val = jnp.squeeze(val)
Type guard
def is_scalar(x): return getattr(x, 'shape', ()) == ()
Prevention
- Index into blocks to extract the single element to write
When it happens
Trigger: Passing a vector/array as val, e.g. p.atomic_cas(ref[0], 0, vals) where vals.shape == (4,).
Common situations: Intending elementwise swap of a block and passing the block as val; values extracted from a block with shape preserved.
Related errors
- ref must be scalar.
- cmp must be scalar.
- Only arguments with shape [..., 1] are supported.
- Only equal-sized splits are supported.
- other cannot be a block if pointer is not a block
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/f71c8866c46fd9f4.
Report an issue: GitHub.