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

  1. Replace non-positive strides with 1 (or the intended positive step)
  2. For reversal, flip the array via BCOO dense round-trip or sparse reversal utilities, not negative strides
  3. 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

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


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/4dd4096ba7ead619. Report an issue: GitHub.