{"record":{"id":"8420b8d8847ea875","repo":"jax-ml/jax","slug":"bcoo-dynamic-slice-indices-must-have-size-mat-ndi","errorCode":null,"errorMessage":"bcoo_dynamic_slice: indices must have size mat.ndim={mat.ndim}","messagePattern":"bcoo_dynamic_slice: indices must have size mat\\.ndim=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":2062,"sourceCode":"      integers with length equal to `ndim(operand)`. Inside a JIT compiled\n      function, only static values are supported (all JAX arrays inside JIT\n      must have statically known size).\n\n  Returns:\n    out: BCOO array containing the slice.\n  \"\"\"\n  slice_sizes = tuple(operator.index(i) for i in slice_sizes)\n  # Use abstract eval to validate inputs.\n  jax.jit(lax.dynamic_slice, static_argnames=(\"slice_sizes\",)).eval_shape(\n          jax.ShapeDtypeStruct(mat.shape, mat.dtype), start_indices,\n          slice_sizes=slice_sizes)\n  if not isinstance(mat, BCOO):\n    raise TypeError(f\"bcoo_slice: input should be BCOO array, got type(mat)={type(mat)}\")\n  start_indices = tuple(jnp.asarray(i) for i in start_indices)\n  assert all(jnp.issubdtype(i.dtype, np.integer) for i in start_indices)\n  assert all(i.shape == () for i in start_indices)\n  if len(start_indices) != len(slice_sizes) != mat.ndim:\n    raise ValueError(f\"bcoo_dynamic_slice: indices must have size mat.ndim={mat.ndim}\")\n  if not all(0 <= slice_size <= axis_size for slice_size, axis_size in zip(slice_sizes, mat.shape)):\n    raise TypeError(\"slice_sizes must be less than or equal to operand shape, \"\n                    f\"got slice_sizes {slice_sizes} for operand shape {mat.shape}\")\n\n  start_batch, start_sparse, start_dense = split_list(start_indices, [mat.n_batch, mat.n_sparse])\n  size_batch, size_sparse, size_dense = split_list(slice_sizes, [mat.n_batch, mat.n_sparse])\n\n  data_start = []\n  data_sizes = []\n  indices_start = []\n  indices_sizes = []\n  zero = _const(start_indices[0] if start_indices else np.int32, 0)\n  for i, (start, size) in enumerate(zip(start_batch, size_batch)):\n    data_is_broadcast = mat.data.shape[i] != mat.shape[i]\n    indices_is_broadcast = mat.indices.shape[i] != mat.shape[i]\n    data_start.append(zero if data_is_broadcast else start)\n    data_sizes.append(1 if data_is_broadcast else size)\n    indices_start.append(zero if indices_is_broadcast else start)","sourceCodeStart":2044,"sourceCodeEnd":2080,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L2044-L2080","documentation":"bcoo_dynamic_slice validates that len(start_indices) == len(slice_sizes) == mat.ndim via a chained comparison; any mismatch raises ValueError. start_indices must be one scalar index per dimension and slice_sizes one size per dimension.","triggerScenarios":"Calling bcoo_dynamic_slice with fewer/more start indices or slice sizes than mat.ndim, e.g. a single start index and size for a 2-D BCOO array.","commonSituations":"Porting lax.dynamic_slice code between arrays of different rank; assuming batch dimensions are excluded from the index lists; building slice_sizes dynamically with the wrong length.","solutions":["Provide exactly mat.ndim scalar start indices and mat.ndim slice sizes","Construct the tuples from mat.ndim at runtime rather than hardcoding","Note the lax-level eval_shape validation also runs first — fix lengths so both checks pass"],"exampleFix":"# before (mat is 2-D)\nbcoo_dynamic_slice(mat, start_indices=(i,), slice_sizes=(4,))\n# after\nbcoo_dynamic_slice(mat, start_indices=(i, 0), slice_sizes=(4, 4))","handlingStrategy":"validation","validationCode":"start_indices = tuple(start_indices)\nslice_sizes = tuple(slice_sizes)\nassert len(start_indices) == len(slice_sizes) == mat.ndim","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build start/size tuples from mat.ndim, not fixed literals","Remember batch dims count toward ndim in the index lists"],"tags":["jax","sparse","bcoo","validation","shape-mismatch"],"backgroundTag":"index-length-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}