jax-ml/jax · error · TypeError

`pad_width` must be of integral type.

Error message

`pad_width` must be of integral type.

What it means

After normalization, each before/after entry of pad_width must be a valid dimension (Python/numpy integer scalar, tracing-safe Dim). Non-integral values such as floats, or non-scalar objects, raise TypeError.

Source

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

    Pad a 1-dimensional array with a custom padding function:

    >>> def custom_pad(row, pad_width, iaxis, kwargs):
    ...   # row represents a 1D slice of the zero-padded array.
    ...   before, after = pad_width
    ...   before_value = kwargs.get('before_value', 0)
    ...   after_value = kwargs.get('after_value', 0)
    ...   row = row.at[:before].set(before_value)
    ...   return row.at[len(row) - after:].set(after_value)
    >>> x = jnp.array([2, 3, 4])
    >>> jnp.pad(x, 2, custom_pad, before_value=-10, after_value=10)
    Array([-10, -10,   2,   3,   4,  10,  10], dtype=int32)
  """

  array = util.ensure_arraylike("pad", array)
  pad_width = _broadcast_to_pairs(pad_width, np.ndim(array), "pad_width")
  if pad_width and not all(core.is_dim(p[0]) and core.is_dim(p[1])
                           for p in pad_width):
    raise TypeError('`pad_width` must be of integral type.')

  if callable(mode):
    return _pad_func(asarray(array), pad_width, mode, **kwargs)

  allowed_kwargs = {
      'empty': [], 'edge': [], 'wrap': [],
      'constant': ['constant_values'],
      'linear_ramp': ['end_values'],
      'maximum': ['stat_length'],
      'mean': ['stat_length'],
      'median': ['stat_length'],
      'minimum': ['stat_length'],
      'reflect': ['reflect_type'],
      'symmetric': ['reflect_type'],
  }
  try:
    unsupported_kwargs = set(kwargs) - set(allowed_kwargs[mode])
  except KeyError:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Coerce to int: jnp.pad(x, (int(w), 0))
  2. Ensure width computations use integer arithmetic (// instead of /)

Example fix

// before
jnp.pad(x, (x.shape[0] / 2, 0))
// after
jnp.pad(x, (x.shape[0] // 2, 0))
Defensive patterns

Strategy: validation

Validate before calling

assert all(isinstance(v, (int, np.integer)) or core.is_dim(v) for p in pad_width for v in p)

Type guard

def int_pairs(pads):
    return all(np.issubdtype(type(v), np.integer) for p in pads for v in p)

Prevention

When it happens

Trigger: jnp.pad(x, (1.5, 2)) or pad_width computed with float arithmetic like (n * 0.1, 0).

Common situations: Computing pad widths with float division or from float config values; passing numpy float64 scalars.

Related errors


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