{"record":{"id":"e94edf23cbcc1f8e","repo":"jax-ml/jax","slug":"negative-dimensions-are-not-allowed-got-n-and","errorCode":null,"errorMessage":"negative dimensions are not allowed, got {N} and {M}","messagePattern":"negative dimensions are not allowed, got (.+?) and (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":5810,"sourceCode":"    return api.device_put(output, device=device)\n  return output\n\n\ndef _eye(N: DimSize, M: DimSize | None = None,\n        k: int | ArrayLike = 0,\n        dtype: DTypeLike | None = None) -> Array:\n  dtype = dtypes.check_and_canonicalize_user_dtype(\n      float if dtype is None else dtype, \"eye\")\n  if isinstance(k, int):\n    k = lax._clip_int_to_valid_range(k, np.int32,\n                                              \"`argument `k` of jax.numpy.eye\")\n  offset = util.ensure_arraylike(\"eye\", k)\n  if not (offset.shape == () and dtypes.issubdtype(offset.dtype, np.integer)):\n    raise ValueError(f\"k must be a scalar integer; got {k}\")\n  N_int = core.canonicalize_dim(N, \"argument of 'N' jnp.eye()\")\n  M_int = N_int if M is None else core.canonicalize_dim(M, \"argument 'M' of jnp.eye()\")\n  if N_int < 0 or M_int < 0:\n    raise ValueError(f\"negative dimensions are not allowed, got {N} and {M}\")\n  i = lax.broadcasted_iota(offset.dtype, (N_int, M_int), 0)\n  j = lax.broadcasted_iota(offset.dtype, (N_int, M_int), 1)\n  return (i + offset == j).astype(dtype)\n\n\n@export\ndef identity(n: DimSize, dtype: DTypeLike | None = None) -> Array:\n  \"\"\"Create a square identity matrix\n\n  JAX implementation of :func:`numpy.identity`.\n\n  Args:\n    n: integer specifying the size of each array dimension.\n    dtype: optional dtype; defaults to floating point.\n\n  Returns:\n    Identity array of shape ``(n, n)``.\n","sourceCodeStart":5792,"sourceCodeEnd":5828,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L5792-L5828","documentation":"jnp.eye validates that the requested number of rows (N) and columns (M) are non-negative after canonicalization to integer dimensions. A negative N or M is meaningless for an eye/identity-like matrix and raises this ValueError echoing both values.","triggerScenarios":"jnp.eye(-3), jnp.eye(3, -1), or N/M derived from shapes that can be negative, e.g. N = a.shape[0] - b.shape[0] where b is longer than a; padding computations like N = n - 2*pad with pad too large.","commonSituations":"Padding/cropping arithmetic that overshoots; shape propagation in variable-length pipelines where the computed size goes negative on short inputs.","solutions":["Clamp computed dimensions: N = max(N, 0) before calling jnp.eye","Fix the upstream size arithmetic (e.g. reduce padding, validate input lengths)","Add an assert on expected minimum size before building the matrix"],"exampleFix":"// before\neye = jnp.eye(x.shape[0] - y.shape[0])  # can be negative\n// after\nn = max(x.shape[0] - y.shape[0], 0)\neye = jnp.eye(n)","handlingStrategy":"validation","validationCode":"N, M = max(int(N), 0), (max(int(M), 0) if M is not None else None)\neye = jnp.eye(N, M)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate computed sizes against 0 before constructing arrays","Add asserts on minimum sequence lengths before padding arithmetic"],"tags":["jax","eye","negative-dimension","valueerror"],"backgroundTag":"negative-dimension-size","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}