jax-ml/jax · error · ValueError
bcoo_slice: invalid indices. Got {start_indices=}, {limit_in
Error message
bcoo_slice: invalid indices. Got {start_indices=}, {limit_indices=} and shape={mat.shape} What it means
bcoo_slice requires, for every dimension, 0 <= start <= limit <= shape[dim]. Violating any of these (negative start, limit beyond the axis size, or limit < start) raises this ValueError echoing the offending indices and mat.shape.
Source
Thrown at jax/experimental/sparse/bcoo.py:1992
"""
if not isinstance(mat, BCOO):
raise TypeError(f"bcoo_slice: input should be BCOO array, got type(mat)={type(mat)}")
start_indices = [operator.index(i) for i in start_indices]
limit_indices = [operator.index(i) for i in limit_indices]
if strides is not None:
strides = [operator.index(i) for i in strides]
else:
strides = [1] * mat.ndim
if len(start_indices) != len(limit_indices) != len(strides) != mat.ndim:
raise ValueError(f"bcoo_slice: indices must have size mat.ndim={mat.ndim}")
if len(strides) != mat.ndim:
raise ValueError(f"len(strides) = {len(strides)}; expected {mat.ndim}")
if any(s <= 0 for s in strides):
raise ValueError(f"strides must be a sequence of positive integers; got {strides}")
if not all(0 <= start <= end <= size
for start, end, size in safe_zip(start_indices, limit_indices, mat.shape)):
raise ValueError(f"bcoo_slice: invalid indices. Got {start_indices=}, "
f"{limit_indices=} and shape={mat.shape}")
start_batch, start_sparse, start_dense = split_list(start_indices, [mat.n_batch, mat.n_sparse])
end_batch, end_sparse, end_dense = split_list(limit_indices, [mat.n_batch, mat.n_sparse])
stride_batch, stride_sparse, stride_dense = split_list(strides, [mat.n_batch, mat.n_sparse])
data_slices = []
index_slices = []
for i, (start, end, stride) in enumerate(zip(start_batch, end_batch, stride_batch)):
data_slices.append(slice(None) if mat.data.shape[i] != mat.shape[i] else slice(start, end, stride))
index_slices.append(slice(None) if mat.indices.shape[i] != mat.shape[i] else slice(start, end, stride))
data_slices.append(slice(None))
index_slices.extend([slice(None), slice(None)])
for i, (start, end, stride) in enumerate(zip(start_dense, end_dense, stride_dense)):
data_slices.append(slice(start, end, stride))
new_data = mat.data[tuple(data_slices)]
new_indices = mat.indices[tuple(index_slices)]
new_shape = tuple(View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Clamp indices: start = max(0, start); limit = min(limit, dim_size)
- Replace NumPy-style negative ends with explicit axis lengths (use mat.shape, not -1)
- Assert limits >= starts per axis before calling
Example fix
# before bcoo_slice(mat, start_indices=(0, -1), limit_indices=(4, 10), strides=None) # shape (4, 10) ok but -1 invalid # after bcoo_slice(mat, start_indices=(0, 0), limit_indices=(4, 10))
Defensive patterns
Strategy: validation
Validate before calling
starts = [max(0, s) for s in start_indices] limits = [min(l, d) for l, d in zip(limit_indices, mat.shape)] assert all(s <= l for s, l in zip(starts, limits))
Try / catch
try:
out = bcoo_slice(mat, starts, limits, strides)
except ValueError as e:
if 'invalid indices' in str(e):
limits = [min(l, d) for l, d in zip(limit_indices, mat.shape)]
out = bcoo_slice(mat, starts, limits, strides)
else:
raise Prevention
- Never use NumPy-style -1 endpoints; use mat.shape[i]
- Clamp dynamic limits against the current shape before slicing
When it happens
Trigger: Calling bcoo_slice with start_indices containing negatives, limit_indices exceeding mat.shape, or limits smaller than starts, e.g. start=(5,), limit=(3,) on an axis of size 4.
Common situations: Using -1 as an end index (NumPy convention) instead of the axis size; computing limits from dynamic values without clamping; off-by-one errors after shape changes.
Related errors
- bcoo_slice: input should be BCOO array, got type(mat)={type(
- Slice {idx} along axis {axis} is out of bounds for shape {sh
- batch_dims must be None or satisfy 0 < dim < n_batch. Got {b
- data batch dimensions not compatible for {data.shape=}, {sha
- Invalid {data.shape=} for {nse=}, {n_batch=}, {n_dense=}
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/f77e0a6a6c760dc2.
Report an issue: GitHub.