jax-ml/jax · error · ValueError

len(a) must be at least 1; got shape {a_arr.shape}.

Error message

len(a) must be at least 1; got shape {a_arr.shape}.

What it means

After the ndim check, convolution_matrix also verifies the kernel has at least one element along its last axis (m = a.shape[-1] >= 1). An empty array (e.g. shape (0,) or (3, 0)) raises this ValueError since there is nothing to convolve with.

Source

Thrown at jax/_src/scipy/linalg.py:2859

  Examples:
    >>> jax.scipy.linalg.convolution_matrix(jnp.array([-1, 4, -2]), 5, mode='same')
    Array([[ 4, -1,  0,  0,  0],
           [-2,  4, -1,  0,  0],
           [ 0, -2,  4, -1,  0],
           [ 0,  0, -2,  4, -1],
           [ 0,  0,  0, -2,  4]], dtype=int32)
  """
  n = operator.index(n)
  if n <= 0:
    raise ValueError(f"n must be a positive integer; got {n}.")
  check_arraylike("convolution_matrix", a)
  a_arr = jnp.asarray(a)
  if a_arr.ndim == 0:
    raise ValueError(
        "convolution_matrix: a must be at least 1-dimensional, got a scalar.")
  m = a_arr.shape[-1]
  if m < 1:
    raise ValueError(f"len(a) must be at least 1; got shape {a_arr.shape}.")
  if mode not in ('full', 'valid', 'same'):
    raise ValueError(
        f"mode must be one of 'full', 'valid', 'same'; got {mode!r}.")
  pad_widths = [(0, 0)] * (a_arr.ndim - 1) + [(0, n - 1)]
  az = jnp.pad(a_arr, pad_widths)
  raz = jnp.pad(jnp.flip(a_arr, axis=-1), pad_widths)
  L = m + n - 1
  if mode == 'same':
    trim = min(n, m) - 1
    tb = trim // 2
    te = trim - tb
  elif mode == 'valid':
    tb = min(n, m) - 1
    te = tb
  else:  # 'full'
    tb = 0
    te = 0
  col0 = lax.slice_in_dim(az, tb, L - te, axis=-1)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Skip or special-case empty kernels before the call
  2. Verify kernel length > 0 with a debug assert/print of a.shape
  3. Fix the filtering logic that emptied the kernel

Example fix

# before
C = convolution_matrix(taps[taps != 0], n)  # could be empty
# after
k = taps[taps != 0]
C = convolution_matrix(k if k.size else jnp.zeros(1), n)
Defensive patterns

Strategy: validation

Validate before calling

a_arr = jnp.asarray(a)
if a_arr.shape[-1] < 1:
    raise ValueError('kernel must have at least one element')
C = convolution_matrix(a_arr, n)

Type guard

def has_nonempty_last_axis(x) -> bool:
    return jnp.asarray(x).shape[-1] > 0

Try / catch

try:
    convolution_matrix(a, n)
except ValueError as e:
    if 'len(a) must be at least 1' in str(e):
        raise ValueError('empty convolution kernel') from e
    raise

Prevention

When it happens

Trigger: Passing an empty list [], jnp.zeros((0,)), or an empty batch slice to convolution_matrix.

Common situations: Dynamically trimmed/filtered kernels that end up empty; batches where a mask removed all elements of one item; upstream data-loading returning zero taps.

Related errors


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