jax-ml/jax · error · ValueError
bcoo_slice: indices must have size mat.ndim={mat.ndim}
Error message
bcoo_slice: indices must have size mat.ndim={mat.ndim} What it means
bcoo_slice validates that start_indices, limit_indices, and (if given) strides all have length equal to mat.ndim. The chained comparison len(start_indices) != len(limit_indices) != len(strides) != mat.ndim fails when any of these lengths mismatch the array's rank, raising ValueError.
Source
Thrown at jax/experimental/sparse/bcoo.py:1984
indices of each slice.
limit_indices: sequence of integers of length `mat.ndim` specifying the ending
indices of each slice
strides: (not implemented) sequence of integers of length `mat.ndim` specifying
the stride for each slice
Returns:
out: BCOO array containing the slice.
"""
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))View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Make all three sequences have exactly mat.ndim entries: one start, one limit, and one stride per dimension
- Use strides=None to let bcoo_slice default strides to 1 in every dimension
- Build indices programmatically from mat.ndim rather than hardcoding
Example fix
# before (2-D mat) bcoo_slice(mat, start_indices=(0,), limit_indices=(4,), strides=(1,)) # after bcoo_slice(mat, start_indices=(0, 0), limit_indices=(4, 4), strides=(1, 1))
Defensive patterns
Strategy: validation
Validate before calling
assert len(start_indices) == len(limit_indices) == mat.ndim strides = strides or [1] * mat.ndim assert len(strides) == mat.ndim
Prevention
- Derive index-list lengths from mat.ndim at runtime
- Prefer strides=None unless you actually need striding
When it happens
Trigger: Calling bcoo_slice with start_indices/limit_indices lists whose length differs from mat.ndim, or supplying strides of the wrong length, e.g. slicing a 2-D BCOO with a single (1,) start/limit tuple and no strides handled incorrectly.
Common situations: Assuming slicing only applies to sparse/dense dimensions and forgetting batch dimensions; reusing index lists computed for a different-shaped array; passing Python scalars instead of per-dimension sequences.
Related errors
- bcoo_dynamic_slice: indices must have size mat.ndim={mat.ndi
- len(strides) = {len(strides)}; expected {mat.ndim}
- bcoo_multiply_sparse: arrays must have same number of dimens
- Invalid {n_batch=}, {n_dense=} for {shape=}
- all keys need to be the same shape
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/7190c0e630082a1c.
Report an issue: GitHub.