{"record":{"id":"37925162b9cb8370","repo":"jax-ml/jax","slug":"only-1-dimensional-input-supported","errorCode":null,"errorMessage":"only 1-dimensional input supported.","messagePattern":"only 1-dimensional input supported\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":2963,"sourceCode":"\n    >>> jit_bincount = jax.jit(jnp.bincount, static_argnames=['length'])\n    >>> jit_bincount(x, length=5)\n    Array([0, 2, 1, 3, 0], dtype=int32)\n\n    Any negative numbers are clipped to the first bin, and numbers beyond the\n    specified ``length`` are dropped:\n\n    >>> x = jnp.array([-1, -1, 1, 3, 10])\n    >>> jnp.bincount(x, length=5)\n    Array([2, 1, 0, 1, 0], dtype=int32)\n  \"\"\"\n  x = util.ensure_arraylike(\"bincount\", x)\n  if x.dtype == bool:\n    x = lax.convert_element_type(x, 'int32')\n  if not issubdtype(x.dtype, np.integer):\n    raise TypeError(f\"x argument to bincount must have an integer type; got {x.dtype}\")\n  if np.ndim(x) != 1:\n    raise ValueError(\"only 1-dimensional input supported.\")\n  minlength = core.concrete_or_error(\n      operator.index, minlength,\n      \"The error occurred because of argument 'minlength' of jnp.bincount.\")\n  if length is None:\n    x_arr = core.concrete_or_error(\n        asarray, x,\n        \"The error occurred because of argument 'x' of jnp.bincount. \"\n        \"To avoid this error, pass a static `length` argument.\")\n    length = max(minlength, x_arr.size and int(max(0, x_arr.max())) + 1)\n  else:\n    length = core.concrete_dim_or_error(\n        length,\n        \"The error occurred because of argument 'length' of jnp.bincount.\")\n\n  if weights is None:\n    weights = np.array(1, dtype=dtypes.int_)\n  else:\n    xts = core.typeof(x).sharding","sourceCodeStart":2945,"sourceCodeEnd":2981,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L2945-L2981","documentation":"jnp.bincount only accepts 1-dimensional arrays; bincount is defined as a per-value histogram over a flat vector, and multi-dimensional input is rejected.","triggerScenarios":"Calling jnp.bincount on a 2D array, e.g. jnp.bincount(jnp.array([[1,2],[3,4]])), or on labels of shape (batch, seq_len).","commonSituations":"Batched label counting in ML pipelines where labels carry a batch dimension; forgetting to ravel after reductions.","solutions":["Flatten first: jnp.bincount(labels.ravel())","If you need per-row counts, vmap over rows: jax.vmap(lambda r: jnp.bincount(r, length=k))(labels)","Consider jnp.histogram-style or scatter-based counting for multi-dim use"],"exampleFix":"// before\njnp.bincount(labels)  # labels.shape == (B, N)\n// after\njnp.bincount(labels.ravel())","handlingStrategy":"validation","validationCode":"if x.ndim != 1:\n    x = x.ravel()","typeGuard":"def is_1d(a) -> bool:\n    return a.ndim == 1","tryCatchPattern":null,"preventionTips":["ravel batched labels before counting","Use vmap for per-row bincounts"],"tags":["jax","bincount","ndim","shape-validation"],"backgroundTag":"argument-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}