jax-ml/jax · error · ValueError

stride_axis is out of range

Error message

stride_axis is out of range

What it means

In strided roll mode, stride_axis must be a valid non-negative index into x's shape. Negative or out-of-bounds stride_axis values are rejected (no negative-index normalization).

Source

Thrown at jax/_src/pallas/mosaic/primitives.py:138

def roll(
    x: jax.Array,
    shift: jax.Array | int,
    axis: int,
    *,
    stride: int | None = None,
    stride_axis: int | None = None,
) -> jax.Array:
  if isinstance(shift, int) and shift < 0:
    raise ValueError("shift must be non-negative.")
  if axis < 0 or axis >= len(x.shape):
    raise ValueError("axis is out of range.")
  if (stride is None) != (stride_axis is None):
    raise ValueError("stride and stride_axis must be both specified or not.")
  if stride is not None and stride_axis is not None:
    if stride < 0:
      raise ValueError("stride must be non-negative.")
    if stride_axis < 0 or stride_axis >= len(x.shape):
      raise ValueError("stride_axis is out of range")
    if axis == stride_axis:
      raise ValueError("expected axis and stride_axis are different.")
  return roll_p.bind(
      x, shift, axis=axis, stride=stride, stride_axis=stride_axis
  )


@roll_p.def_abstract_eval
def _roll_abstract_eval(x, shift, **_):
  del shift
  return x


def _roll_lowering_rule(
    ctx: mlir.LoweringRuleContext, x, shift, *, axis, stride, stride_axis
):
  def _roll(x, shift):
    if stride is None:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Normalize: stride_axis = stride_axis % len(x.shape)
  2. Check bounds before the call
  3. Prefer explicit non-negative indices in Pallas code unlike jnp idioms

Example fix

# before
y = roll(x, 2, axis=0, stride=8, stride_axis=-1)

# after
y = roll(x, 2, axis=0, stride=8, stride_axis=len(x.shape) - 1)
Defensive patterns

Strategy: validation

Validate before calling

stride_axis = stride_axis % len(x.shape) if stride_axis is not None else None
assert stride_axis is None or 0 <= stride_axis < len(x.shape)

Prevention

When it happens

Trigger: roll(..., stride=4, stride_axis=-1) or stride_axis >= len(x.shape).

Common situations: Using -1 for the natural 'last dim' stride axis as you would in jnp; changing the rank of the operand so a hardcoded stride_axis no longer fits.

Related errors


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