{"record":{"id":"f71c8866c46fd9f4","repo":"jax-ml/jax","slug":"val-must-be-scalar","errorCode":null,"errorMessage":"val must be scalar.","messagePattern":"val must be scalar\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/pallas/triton/primitives.py","lineNumber":627,"sourceCode":"  \"\"\"\n  return _atomic_rmw(\n      x_ref_or_view, idx, val, mask=mask, atomic_type=AtomicOpType.XOR\n  )\n\n\natomic_cas_p = jax_core.Primitive(\"atomic_cas\")\n\n\n@atomic_cas_p.def_effectful_abstract_eval\ndef _atomic_cas_abstract_eval(ref_aval, cmp_aval, val_aval):\n  if cmp_aval.dtype != val_aval.dtype or cmp_aval.shape != val_aval.shape:\n    raise ValueError(\"cmp and val must have identical dtypes and shapes\")\n  if ref_aval.shape:\n    raise ValueError(\"ref must be scalar.\")\n  if cmp_aval.shape:\n    raise ValueError(\"cmp must be scalar.\")\n  if val_aval.shape:\n    raise ValueError(\"val must be scalar.\")\n  return jax_core.ShapedArray(val_aval.shape, val_aval.dtype), {\n      state.WriteEffect(0)\n  }\n\n\ndef atomic_cas(ref, cmp, val):\n  \"\"\"Performs an atomic compare-and-swap of the value in the ref with the\n\n  given value.\n\n  Args:\n    ref: The ref to operate on.\n    cmp: The expected value to compare against.\n    val: The value to swap in.\n\n  Returns:\n    The value at the given index prior to the atomic operation.\n  \"\"\"","sourceCodeStart":609,"sourceCodeEnd":645,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/pallas/triton/primitives.py#L609-L645","documentation":"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.","triggerScenarios":"Passing a vector/array as val, e.g. p.atomic_cas(ref[0], 0, vals) where vals.shape == (4,).","commonSituations":"Intending elementwise swap of a block and passing the block as val; values extracted from a block with shape preserved.","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"],"exampleFix":"# before\np.atomic_cas(ref[0], cmp, val_block)  # val_block.shape == (4,)\n# after\np.atomic_cas(ref[0], cmp, val_block[0])","handlingStrategy":"type-guard","validationCode":"val = jnp.squeeze(val)","typeGuard":"def is_scalar(x): return getattr(x, 'shape', ()) == ()","tryCatchPattern":null,"preventionTips":["Index into blocks to extract the single element to write"],"tags":["jax","pallas","triton","atomic-cas","shape"],"backgroundTag":"invalid-argument-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}