{"record":{"id":"825a1cf3f47febc5","repo":"jax-ml/jax","slug":"size-must-be-positive-and-not-greater-than-the-siz","errorCode":null,"errorMessage":"size must be positive and not greater than the size of the array axis; got {size=} for a.shape[axis]={arr.shape[0]}","messagePattern":"size must be positive and not greater than the size of the array axis; got (.+?) for a\\.shape\\[axis\\]=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":9068,"sourceCode":"  if axis is None:\n    axis = 0\n    arr = ravel(arr)\n  else:\n    arr = moveaxis(arr, axis, 0)\n  condition_arr, extra = condition_arr[:arr.shape[0]], condition_arr[arr.shape[0]:]\n  arr = arr[:condition_arr.shape[0]]\n\n  if size is None:\n    msg = (\"The size argument of jnp.compress must be specified in order to use \"\n           \"jnp.compress within JAX transformations like jax.jit, jax.vmap, and \"\n           \"jax.grad. For more information, refer to the jnp.compress documentation.\")\n    condition_arr = core.concrete_or_error(None, condition_arr, msg)\n    extra = core.concrete_or_error(None, extra, msg)\n    if extra.any():\n      raise ValueError(\"condition contains entries that are out of bounds\")\n    result = arr[condition_arr]\n  elif not 0 <= size <= arr.shape[0]:\n    raise ValueError(\"size must be positive and not greater than the size of the array axis;\"\n                     f\" got {size=} for a.shape[axis]={arr.shape[0]}\")\n  else:\n    mask = expand_dims(condition_arr, range(1, arr.ndim))\n    arr = where(mask, arr, array(fill_value, dtype=arr.dtype))\n    result = arr[argsort(condition_arr, stable=True, descending=True)][:size]\n  return moveaxis(result, 0, axis)\n\n\n@export\n@api.jit(static_argnames=('rowvar', 'bias', 'ddof', 'dtype'))\ndef cov(m: ArrayLike, y: ArrayLike | None = None, rowvar: bool = True,\n        bias: bool = False, ddof: int | None = None,\n        fweights: ArrayLike | None = None,\n        aweights: ArrayLike | None = None,\n        dtype: DTypeLike | None = None) -> Array:\n  r\"\"\"Estimate the weighted sample covariance.\n\n  JAX implementation of :func:`numpy.cov`.","sourceCodeStart":9050,"sourceCodeEnd":9086,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L9050-L9086","documentation":"jnp.compress validates its `size` (needed for jit compatibility): when specified it must be positive and no larger than the number of elements along the compressed axis, otherwise the selection is out of range.","triggerScenarios":"jnp.compress(condition, a, size=n) with n negative or n greater than the length of the selected axis, e.g. size=5 for an axis of length 3.","commonSituations":"Computing size dynamically (e.g. int(cond.sum()) plus an offset) inside jit where the relationship to axis length isn't enforced; off-by-one errors when size equals axis length + 1.","solutions":["Clamp or validate size before the call: size = min(size, a.shape[axis])","Fix off-by-one in the computation producing size","Use size = a.shape[axis] if you meant to include all selected entries"],"exampleFix":"// before\njnp.compress(cond, a, size=5)  # a.shape[0] == 3\n// after\njnp.compress(cond, a, size=min(5, a.shape[0]))","handlingStrategy":"validation","validationCode":"size = min(max(size, 0), a.shape[axis])\njnp.compress(cond, a, size=size, axis=axis)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp size into [0, axis_len]","Derive size from condition.sum() with min() guard","Watch off-by-one when size == axis length"],"tags":["jax","size-validation","out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}