jax-ml/jax · error · ValueError

padding argument to conv_general_dilated should be a string

Error message

padding argument to conv_general_dilated should be a string or a sequence of (low, high) pairs, got {padding}

What it means

conv_general_dilated's padding parameter must be either a string ('SAME'/'VALID') or a sequence of integer (low, high) pairs, one per spatial dimension. The code coerces each entry with operator.index over a two-element unpack; anything else (nested strings, floats, three-element tuples, non-iterables) re-raises as ValueError.

Source

Thrown at jax/_src/lax/convolution.py:169

    raise ValueError(
        "String padding is not implemented for transposed convolution "
        "using this op. Please either exactly specify the required padding or "
        "use conv_transpose.")
  if rhs_dilation is None:
    rhs_dilation = (1,) * (rhs.ndim - 2)
  if isinstance(padding, str):
    lhs_perm, rhs_perm, _ = dnums
    rhs_shape = np.take(rhs.shape, rhs_perm)[2:]
    effective_rhs_shape = [core.dilate_dim(k, r) for k, r in zip(rhs_shape, rhs_dilation)]
    padding = lax.padtype_to_pads(
        np.take(lhs.shape, lhs_perm)[2:], effective_rhs_shape,
        window_strides, padding)
  else:
    try:
      padding = tuple((operator.index(lo), operator.index(hi))
                      for lo, hi in padding)
    except (ValueError, TypeError) as e:
      raise ValueError(
        "padding argument to conv_general_dilated should be a string or a "
        f"sequence of (low, high) pairs, got {padding}") from e

  preferred_element_type = (
      None if preferred_element_type is None
      else dtypes.check_and_canonicalize_user_dtype(
          preferred_element_type, "conv_general_dilated"
      )
  )
  lhs, rhs = core.auto_insert_reshard(lhs, rhs)
  return conv_general_dilated_p.bind(
      lhs, rhs, window_strides=tuple(window_strides), padding=tuple(padding),
      lhs_dilation=tuple(lhs_dilation), rhs_dilation=tuple(rhs_dilation),
      dimension_numbers=dnums,
      feature_group_count=feature_group_count,
      batch_group_count=batch_group_count,
      precision=lax.canonicalize_precision(precision),
      preferred_element_type=preferred_element_type,

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass explicit pairs of Python ints: [(1, 1), (1, 1)]
  2. Convert torch-style padding: [(p, p) for p in (padding,)*num_spatial_dims]
  3. Use 'SAME' or 'VALID' if you don't need custom padding

Example fix

// before
lax.conv_general_dilated(x, k, (1,1), padding=1)
// after
lax.conv_general_dilated(x, k, (1,1), padding=[(1,1), (1,1)])
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(padding, str):
    padding = [(int(lo), int(hi)) for lo, hi in padding]
    assert all(isinstance(p, tuple) and len(p) == 2 for p in padding)

Type guard

def is_valid_padding(p) -> bool:
    if isinstance(p, str): return p in ('SAME', 'VALID')
    try:
        return all(isinstance(lo, int) and isinstance(hi, int) for lo, hi in p)
    except Exception:
        return False

Prevention

When it happens

Trigger: Passing padding=[(1,1),(1,1.5)], [(0,0,0)], padding=1, or a malformed nested list to lax.conv_general_dilated; also generators that don't yield exactly two ints.

Common situations: Building padding programmatically and producing floats or single ints; converting from frameworks that express padding as a single number (torch nn.Conv2d padding=1) without expanding it to per-dim pairs.

Related errors


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