{"record":{"id":"db79049822744347","repo":"jax-ml/jax","slug":"invalid-dimension-range-passed-to-collapse-opera","errorCode":null,"errorMessage":"Invalid dimension range passed to collapse: {operand.shape}[{start_dimension}:{stop_dimension}]","messagePattern":"Invalid dimension range passed to collapse: (.+?)\\[(.+?):(.+?)\\]","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":3987,"sourceCode":"\n  For example, if ``operand`` is an array with shape ``[2, 3, 4]``,\n  ``collapse(operand, 0, 2).shape == [6, 4]``. The elements of the collapsed\n  dimension are laid out major-to-minor, i.e., with the lowest-numbered\n  dimension as the slowest varying dimension.\n\n  Args:\n    operand: an input array.\n    start_dimension: the start of the dimensions to collapse (inclusive).\n    stop_dimension: the end of the dimensions to collapse (exclusive). Pass None\n      to collapse all the dimensions after start.\n\n  Returns:\n    An array where dimensions ``[start_dimension, stop_dimension)`` have been\n    collapsed (raveled) into a single dimension.\n  \"\"\"\n  lo, hi, _ = slice(start_dimension, stop_dimension).indices(len(operand.shape))\n  if hi < lo:\n    raise ValueError(f\"Invalid dimension range passed to collapse: {operand.shape}\"\n                     f\"[{start_dimension}:{stop_dimension}]\")\n  size = math.prod(operand.shape[lo:hi])\n  new_shape = operand.shape[:lo] + (size,) + operand.shape[hi:]\n  return reshape(operand, new_shape)\n\n\ndef batch_matmul(lhs: Array, rhs: Array,\n                 precision: PrecisionLike = None) -> Array:\n  \"\"\"Batch matrix multiplication.\"\"\"\n  if _min(lhs.ndim, rhs.ndim) < 2:\n    raise ValueError('Arguments to batch_matmul must be at least 2D, got {}, {}'\n                     .format(lhs.ndim, rhs.ndim))\n  if lhs.ndim != rhs.ndim:\n    raise ValueError('Arguments to batch_matmul must have same ndim, got {}, {}'\n                     .format(lhs.ndim, rhs.ndim))\n  lhs_contract = (lhs.ndim - 1,)\n  rhs_contract = (rhs.ndim - 2,)\n  batch = tuple(range(lhs.ndim - 2))","sourceCodeStart":3969,"sourceCodeEnd":4005,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L3969-L4005","documentation":"jax.lax.collapse ravels dimensions [start, stop) of an array into one. Python slice semantics clamp the range, and if the resolved stop index is below the start (hi < lo), the range is empty/negative in an unsupported way and JAX raises this ValueError.","triggerScenarios":"Calling lax.collapse(x, start, stop) where stop normalizes before start, e.g. collapse(x, 2, 1), collapse(x, 0, -1) on small arrays, or stop computed as start - k by mistake.","commonSituations":"Passing negative stop_dimension expecting exclusive positive semantics; computing stop = start + k with a negative k; using -1 as 'until the end' (which is not how collapse works — omit it or pass x.ndim instead).","solutions":["Use positive absolute indices: for 'to the end' pass operand.ndim (or None-style equivalent by computing it)","Validate start <= stop and both within [0, x.ndim] before calling","Check your slice arithmetic (stop - start should be >= 0 after resolution)"],"exampleFix":"// before\ncollapsed = lax.collapse(x, 1, -1)\n// after\ncollapsed = lax.collapse(x, 1, x.ndim)","handlingStrategy":"validation","validationCode":"start, stop = int(start), int(stop)\nassert 0 <= start <= stop <= x.ndim, f'bad collapse range {start}:{stop}'\nout = lax.collapse(x, start, stop)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use absolute non-negative indices; pass x.ndim for 'to the end'","Never use -1 as stop for collapse (slice semantics differ)"],"tags":["jax","lax","collapse","dimension-range","value-error"],"backgroundTag":"invalid-axis-range","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}