jax-ml/jax · error · ValueError

unsupported mode: {mode}

Error message

unsupported mode: {mode}

What it means

Defensive unreachable-in-practice branch in _convolve_nd: after handling 'same' and 'full' (with 'valid' handled earlier), any other mode string reaches this raise. In practice you only hit it by bypassing the public wrappers or passing a non-standard mode, since the top-of-function check restricts mode to full/same/valid.

Source

Thrown at jax/_src/scipy/signal.py:190

  swap = all(s1 <= s2 for s1, s2 in zip(in1.shape, in2.shape))
  if not (no_swap or swap):
    raise ValueError("One input must be smaller than the other in every dimension.")

  shape_o = in2.shape
  if swap:
    in1, in2 = in2, in1
  shape = in2.shape
  in2 = jnp.flip(in2)

  if mode == 'valid':
    padding = [(0, 0) for s in shape]
  elif mode == 'same':
    padding = [(s - 1 - (s_o - 1) // 2, s - s_o + (s_o - 1) // 2)
               for (s, s_o) in zip(shape, shape_o)]
  elif mode == 'full':
    padding = [(s - 1, s - 1) for s in shape]
  else:
    raise ValueError(f'unsupported mode: {mode}')

  strides = tuple(1 for s in shape)
  result = lax.conv_general_dilated(in1[None, None], in2[None, None], strides,
                                    padding, precision=precision)
  return result[0, 0]


def convolve(in1: Array, in2: Array, mode: ModeString = 'full', method: str = 'auto',
             precision: PrecisionLike = None) -> Array:
  """Convolution of two N-dimensional arrays.

  JAX implementation of :func:`scipy.signal.convolve`.

  Args:
    in1: left-hand input to the convolution.
    in2: right-hand input to the convolution. Must have ``in1.ndim == in2.ndim``.
    mode: controls the size of the output. Available operations are:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Use exactly one of 'full', 'same', 'valid' (lowercase)
  2. Validate/normalize the mode variable against the allowed set before calling convolve/convolve2d/correlate2d

Example fix

// before
jax.scipy.signal.convolve2d(x, k, mode='SAME')
// after
jax.scipy.signal.convolve2d(x, k, mode='same')
Defensive patterns

Strategy: validation

Validate before calling

MODES = ('full', 'same', 'valid')
mode = mode if mode in MODES else 'full'  # or raise early with your own message

Type guard

def is_valid_mode(m: str) -> bool:
    return m in ('full', 'same', 'valid')

Try / catch

try:
    convolve2d(x, k, mode=mode)
except ValueError as e:
    if 'mode' in str(e):
        mode = 'full'  # fallback

Prevention

When it happens

Trigger: Directly calling the private _convolve_nd with an arbitrary mode string; passing a mode variable that is not one of 'full', 'same', 'valid' (which normally trips the earlier check at function entry).

Common situations: Typos like 'Same' or 'SAME' (case-sensitive); passing a tf-style padding string from ported code.

Related errors


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