jax-ml/jax · error · ValueError
strides must be a sequence of positive integers; got {stride
Error message
strides must be a sequence of positive integers; got {strides} What it means
bcoo_slice only supports strided slicing with strictly positive integer strides; any stride <= 0 raises ValueError because negative or zero strides are not implemented for the BCOO representation (unlike dense lax.slice with jax sometimes allowing negative semantics elsewhere).
Source
Thrown at jax/experimental/sparse/bcoo.py:1988
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))
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)):View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Replace non-positive strides with 1 (or the intended positive step)
- For reversal, flip the array via BCOO dense round-trip or sparse reversal utilities, not negative strides
- Validate/clip strides before calling bcoo_slice
Example fix
# before bcoo_slice(mat, (0, 0), (8, 8), strides=(-1, 2)) # after bcoo_slice(mat, (0, 0), (8, 8), strides=(1, 2))
Defensive patterns
Strategy: validation
Validate before calling
strides = strides or [1] * mat.ndim
if any(s <= 0 for s in strides):
strides = [max(1, s) for s in strides] # or raise early with context Prevention
- Remember BCOO slicing has no negative-stride (reversal) support
- Sanitize user-supplied step values before calling bcoo_slice
When it happens
Trigger: Passing a strides sequence containing 0 or a negative number, e.g. strides=(0, 1) or strides=(-1, 1), to bcoo_slice.
Common situations: Porting NumPy slice syntax with step=-1 (reversal) to BCOO; passing 0 expecting 'no stride along this axis'; computing strides from user input without sanitization.
Related errors
- len(strides) = {len(strides)}; expected {mat.ndim}
- 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=}
- indices batch dimensions not compatible for {indices.shape=}
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/4dd4096ba7ead619.
Report an issue: GitHub.