jax-ml/jax · error · ValueError

stat_length of 0 yields no value for padding

Error message

stat_length of 0 yields no value for padding

What it means

For mode='maximum'/'minimum'/'mean'/'median', jnp.pad computes the statistic over stat_length elements of the original array. A stat_length of 0 on either side means there is no data to compute a statistic from, so it raises ValueError.

Source

Thrown at jax/_src/numpy/lax_numpy.py:4120

    ramp_after = flip(ramp_after, axis)

    array = lax.concatenate([ramp_before, array, ramp_after], dimension=axis)
  return array


def _pad_stats(array: Array, pad_width: PadValue[int],
               stat_length: PadValue[int] | None,
               stat_func: PadStatFunc) -> Array:
  nd = np.ndim(array)
  for i in range(nd):
    if stat_length is None:
      stat_before = stat_func(array, axis=i, keepdims=True)
      stat_after = stat_before
    else:
      array_length = array.shape[i]
      length_before, length_after = stat_length[i]
      if length_before == 0 or length_after == 0:
        raise ValueError("stat_length of 0 yields no value for padding")

      # Limit stat_length to length of array.
      length_before = min(length_before, array_length)
      length_after = min(length_after, array_length)

      slice_before = lax_slicing.slice_in_dim(array, 0, length_before, axis=i)
      slice_after = lax_slicing.slice_in_dim(array, -length_after, None, axis=i)
      stat_before = stat_func(slice_before, axis=i, keepdims=True)
      stat_after = stat_func(slice_after, axis=i, keepdims=True)

    if np.issubdtype(array.dtype, np.integer):
      stat_before = round(stat_before)
      stat_after = round(stat_after)

    stat_before = lax._convert_element_type(
        stat_before, array.dtype, dtypes.is_weakly_typed(array))
    stat_after = lax._convert_element_type(
        stat_after, array.dtype, dtypes.is_weakly_typed(array))

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Use stat_length >= 1 on both sides, or omit stat_length (defaults to whole axis)
  2. Build per-axis stat_length with max(1, value)

Example fix

// before
jnp.pad(x, 2, mode='mean', stat_length=(0, 3))
// after
jnp.pad(x, 2, mode='mean', stat_length=(1, 3))
Defensive patterns

Strategy: validation

Validate before calling

sl = np.broadcast_to(np.asarray(stat_length), (x.ndim, 2))
assert (sl > 0).all(), 'stat_length must be >= 1'

Prevention

When it happens

Trigger: jnp.pad(x, 2, mode='mean', stat_length=(0, 3)) or stat_length entries of 0 in the per-axis tuples.

Common situations: Passing stat_length that includes 0 to mean 'no stat region'; per-axis stat_length lists built dynamically where some entries default to 0.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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