{"record":{"id":"60f86934f844c442","repo":"jax-ml/jax","slug":"with-memory-space-constraint-only-supports-arrays","errorCode":null,"errorMessage":"with_memory_space_constraint only supports arrays.","messagePattern":"with_memory_space_constraint only supports arrays\\.","errorType":"validation","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/_src/pallas/core.py","lineNumber":1624,"sourceCode":"  @contextlib.contextmanager\n  def tracing_context(self) -> Generator[None]:\n    raise NotImplementedError()\n    yield\n\n\nwith_memory_space_constraint_p = jax_core.Primitive(\n    'with_memory_space_constraint')\n\n@with_memory_space_constraint_p.def_impl\ndef with_memory_space_constraint_impl(x, *, memory_space):\n  del x, memory_space\n  raise ValueError(\"Cannot eagerly run with_memory_space_constraint.\")\n\n\n@with_memory_space_constraint_p.def_abstract_eval\ndef with_memory_space_constraint_abstract_eval(x, *, memory_space):\n  if not isinstance(x, jax_core.ShapedArray):\n    raise NotImplementedError(\"with_memory_space_constraint only supports \"\n                              \"arrays.\")\n  return x.update(memory_space=memory_space)\n\ndef with_memory_space_constraint_lowering_rule(ctx, x, *, memory_space):\n  del ctx, memory_space\n  return [x]\nmlir.register_lowering(\n    with_memory_space_constraint_p, with_memory_space_constraint_lowering_rule\n)\n\n\ndef with_memory_space_constraint_batching_rule(\n    axis_data, batched_args, batch_dims, *, memory_space\n):\n  del axis_data  # Unused; the constraint does not depend on the mapped axis.\n  (x,), (bdim,) = batched_args, batch_dims\n  out = with_memory_space_constraint_p.bind(x, memory_space=memory_space)\n  return out, bdim  # the computed value and where the batch axis ended up in it","sourceCodeStart":1606,"sourceCodeEnd":1642,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/pallas/core.py#L1606-L1642","documentation":"During abstract evaluation, with_memory_space_constraint requires its input to be a ShapedArray so it can attach a memory_space annotation via aval.update. Passing a token, other abstract value type, or a non-array tracer triggers NotImplementedError. The memory-space annotation machinery simply does not support non-array abstract values.","triggerScenarios":"Passing a token or non-ShapedArray tracer (e.g. an effect token, or output of another exotic primitive) through with_memory_space_constraint during tracing; using it on values produced by ops whose avals are not ShapedArray.","commonSituations":"Composing Pallas kernels with stateful APIs that thread tokens; jax version changes introducing new aval types; annotated user-defined tracers flowing into memory-space constraint logic.","solutions":["Only apply with_memory_space_constraint to array-valued tracers; route tokens/other values around it","Check the intermediate value with jax.core.get_aval(x) to confirm it is a ShapedArray before applying the constraint","Upgrade jax — support for additional aval types may have been added in newer releases","Reorder your computation so the constraint is applied before any transformation that changes the aval type"],"exampleFix":"# before\nout = with_memory_space_constraint(token_or_exotic, memory_space=ms)\n\n# after\nfrom jax.core import ShapedArray\nif isinstance(jax.core.get_aval(v), ShapedArray):\n    out = with_memory_space_constraint(v, memory_space=ms)\nelse:\n    out = v  # leave non-array values unconstrained","handlingStrategy":"type-guard","validationCode":"import jax.core as jc\naval = jc.get_aval(x)\nassert isinstance(aval, jc.ShapedArray), f'unsupported aval {type(aval).__name__}'","typeGuard":"def is_shaped_array_val(x) -> bool:\n    import jax.core as jc\n    return isinstance(jc.get_aval(x), jc.ShapedArray)","tryCatchPattern":"try:\n    y = with_memory_space_constraint(x, memory_space=ms)\nexcept NotImplementedError as e:\n    if 'only supports arrays' in str(e):\n        y = x  # skip constraint for non-array values\n    else:\n        raise","preventionTips":["Inspect avals with jax.core.get_aval before applying memory-space constraints","Keep tokens and non-array tracers out of memory-space annotated pipelines","Track jax release notes for aval-type support in pallas"],"tags":["jax","pallas","abstract-value","shapedarray","type-mismatch"],"backgroundTag":"unsupported-abstract-value-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}