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

  1. Select the single element you want to write: p.atomic_cas(ref[i], cmp, vals[i]) and ensure vals[i] is scalar
  2. 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

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


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/f71c8866c46fd9f4. Report an issue: GitHub.