{"record":{"id":"233d48d3c2ed3a7a","repo":"jax-ml/jax","slug":"slice-sizes-must-be-less-than-or-equal-to-operand","errorCode":null,"errorMessage":"slice_sizes must be less than or equal to operand shape, got slice_sizes {slice_sizes} for operand shape {mat.shape}","messagePattern":"slice_sizes must be less than or equal to operand shape, got slice_sizes (.+?) for operand shape (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":2064,"sourceCode":"      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)\n    indices_sizes.append(1 if indices_is_broadcast else size)\n  data_start.append(zero)","sourceCodeStart":2046,"sourceCodeEnd":2082,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L2046-L2082","documentation":"bcoo_dynamic_slice requires each slice_sizes[i] to satisfy 0 <= slice_sizes[i] <= mat.shape[i]; a window larger than its axis raises TypeError (matching lax.dynamic_slice's contract). The lax abstract-eval check may also surface the same problem first.","triggerScenarios":"Requesting a window of size larger than the corresponding dimension, e.g. slice_sizes=(8,) on an axis of size 4, or negative sizes.","commonSituations":"Hardcoded window sizes applied to inputs of varying shapes; computing sizes as limit - start and getting negatives; assuming slicing past the end clamps like NumPy (it does not).","solutions":["Clamp each size: size = max(0, min(size, mat.shape[axis]))","Compute sizes from actual shape at runtime instead of constants","If you need clamped tail behavior, adjust start and size together before the call"],"exampleFix":"# before\nbcoo_dynamic_slice(mat, (i,), (16,))  # axis size 10\n# after\nbcoo_dynamic_slice(mat, (i,), (min(16, mat.shape[0] - i),))","handlingStrategy":"validation","validationCode":"slice_sizes = tuple(max(0, min(s, dim - start))\n                     for s, dim, start in zip(slice_sizes, mat.shape, start_indices))","typeGuard":null,"tryCatchPattern":"try:\n    out = bcoo_dynamic_slice(mat, starts, sizes)\nexcept TypeError as e:\n    if 'slice_sizes' in str(e):\n        sizes = tuple(min(s, d) for s, d in zip(sizes, mat.shape))\n        out = bcoo_dynamic_slice(mat, starts, sizes)\n    else:\n        raise","preventionTips":["Window sizes must fit within the axis, no NumPy-style clamping","Compute tail windows as min(size, shape[i] - start)"],"tags":["jax","sparse","bcoo","window-too-large","dynamic-slice"],"backgroundTag":"slice-size-exceeds-dimension","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}