{"record":{"id":"ce0e1e0db653b647","repo":"jax-ml/jax","slug":"zero-size-array-to-reduction-operation-name-whic","errorCode":null,"errorMessage":"zero-size array to reduction operation {name} which has no identity","messagePattern":"zero-size array to reduction operation (.+?) which has no identity","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/reductions.py","lineNumber":130,"sourceCode":"  # object methods. For example `np.sum(x)` will call `x.sum()` if the `sum()` method\n  # exists, passing along all its arguments.\n  if out is not None:\n    raise NotImplementedError(f\"The 'out' argument to jnp.{name} is not supported.\")\n  a = ensure_arraylike(name, a)\n  where_ = check_where(name, where_)\n  axis = core.concrete_or_error(None, axis, f\"axis argument to jnp.{name}().\")\n\n  if initial is None and not has_identity and where_ is not None:\n    raise ValueError(f\"reduction operation {name} does not have an identity, so to use a \"\n                     f\"where mask one has to specify 'initial'\")\n\n  a = preproc(a) if preproc else a\n  pos_dims, dims = _reduction_dims(a, axis)\n\n  if initial is None and not has_identity:\n    shape = np.shape(a)\n    if not _all(shape[d] >= 1 for d in pos_dims):\n      raise ValueError(f\"zero-size array to reduction operation {name} which has no identity\")\n\n  result_dtype: DType\n  if dtype is None:\n    result_dtype = a.dtype\n    if promote_integers:\n      result_dtype = _promote_integer_dtype(result_dtype)\n  else:\n    result_dtype = dtypes.check_and_canonicalize_user_dtype(dtype, name)\n\n  if upcast_f16_for_computation and dtypes.issubdtype(result_dtype, np.inexact):\n    computation_dtype = _upcast_f16(result_dtype)\n  else:\n    computation_dtype = result_dtype\n  a = lax.convert_element_type(a, computation_dtype)\n  op = op if computation_dtype != np.bool_ else bool_op\n  # NB: in XLA, init_val must be an identity for the op, so the user-specified\n  # initial value must be applied afterward.\n  init_val = _reduction_init_val(a, init_val)","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/reductions.py#L112-L148","documentation":"A reduction without an identity element (e.g. max/min) was applied to a zero-size array: some axis being reduced over has length 0, so there is no element to produce a result and no neutral value to fall back on.","triggerScenarios":"jnp.max(jnp.zeros((0, 3)), axis=0); dynamically-shaped data that becomes empty after filtering/slicing; reductions over an empty batch dimension under jit with concrete shapes.","commonSituations":"Empty batches in training loops after filtering; slicing to nothing (x[x > 100] when nothing matches) then reducing; padding code producing zero-length axes.","solutions":["Check shapes before reducing: assert all(s > 0 for s in shape)","Supply initial: jnp.max(x, axis=0, initial=-jnp.inf)","Use a reduction with identity (sum returns 0, any/all have identities) where semantics permit"],"exampleFix":"// before\nm = jnp.max(filtered, axis=0)\n// after\nm = jnp.max(filtered, axis=0, initial=-jnp.inf) if filtered.size else default_val","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp\nif any(s == 0 for s in jnp.shape(x)):\n    result = default  # skip empty reduction\nelse:\n    result = jnp.max(x, axis=0)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check for zero-size axes after filtering/slicing","Use initial= to give identity-less reductions a fallback"],"tags":["jax","reductions","empty-array"],"backgroundTag":"reduction-of-empty-array","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}