{"record":{"id":"9d575c0b3854e665","repo":"jax-ml/jax","slug":"size-must-be-a-positive-integer-got-size","errorCode":null,"errorMessage":"size must be a positive integer; got {size=}","messagePattern":"size must be a positive integer; got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/hijax.py","lineNumber":201,"sourceCode":"  \"\"\"HiJAX primitive for nonzero.\"\"\"\n\n  size: int\n  axes: tuple[int, ...]\n  out_dtype: np.dtype\n\n  def __init__(\n      self,\n      a_aval: core.ShapedArray,\n      *fill_value_avals: core.ShapedArray,\n      size: int,\n      axes: tuple[int, ...],\n      out_dtype: np.dtype):\n    if core.is_symbolic_dim(size):\n      pass\n    else:\n      size = operator.index(size)\n      if size < 0:\n        raise ValueError(f\"size must be a positive integer; got {size=}\")\n    if not dtypes.issubdtype(out_dtype, np.integer):\n      raise ValueError(f\"out_dtype must be integer typed; got {out_dtype=}\")\n    if not all(0 <= ax < a_aval.ndim for ax in axes):\n      raise ValueError(f\"axes out of range for array with {a_aval.ndim} dimensions:  {axes=}\")\n    if len(axes) != len(set(axes)):\n      raise ValueError(f\"duplicate axes are not allowed: {axes=}\")\n    if fill_value_avals and len(fill_value_avals) != len(axes):\n      raise ValueError(f\"Expected {len(axes)} fill values, got {len(fill_value_avals)}\")\n    if any(fv.dtype != out_dtype for fv in fill_value_avals):\n      raise ValueError(f\"Expected fill values to have dtype {out_dtype}, got {fill_value_avals}\")\n    batch_shape = tuple(\n        s for i, s in enumerate(a_aval.shape) if i not in axes\n    )\n    for fv_aval in fill_value_avals:\n      try:\n        broadcasted = lax.broadcast_shapes(fv_aval.shape, batch_shape)\n      except ValueError as e:\n        raise ValueError(","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/hijax.py#L183-L219","documentation":"Raised by the Nonzero HiJAX primitive when the static size argument is a negative integer. size declares how many nonzero entries to return (with padding), so it must be non-negative. Symbolic dimensions skip this check, but concrete negative sizes fail immediately.","triggerScenarios":"Calling jax.numpy nonzero(..., size=-1) or passing a computed size that went negative (e.g. size = n_true - margin without clamping).","commonSituations":"Estimating size from a data-dependent count that can be negative in edge batches; off-by-one arithmetic on an upper bound.","solutions":["Clamp the computed size: size = max(0, computed_size)","Pass an explicit non-negative upper bound on the number of nonzeros"],"exampleFix":"# before\nidx = nonzero(a, size=len(a) - k)  # negative when k > len(a)\n# after\nidx = nonzero(a, size=max(0, len(a) - k))","handlingStrategy":"validation","validationCode":"size = operator.index(size)\nassert size >= 0, size","typeGuard":"def valid_size(n) -> bool:\n    return isinstance(n, int) and n >= 0","tryCatchPattern":null,"preventionTips":["Clamp computed sizes: size = max(0, n)","Treat size as a static compile-time constant in JAX, not a tensor"],"tags":["jax","nonzero","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}