{"record":{"id":"d2c97c78010c2779","repo":"jax-ml/jax","slug":"cannot-apply-padding-to-empty-axis","errorCode":null,"errorMessage":"Cannot apply '{}' padding to empty axis","messagePattern":"Cannot apply '(.+?)' padding to empty axis","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":3932,"sourceCode":"    v1_2 = as_scalar_dim(nvals[0]), as_scalar_dim(nvals[1])\n    return tuple(v1_2 for i in range(nd))\n  elif nvals.shape == (1,):\n    # (pad,)\n    v = as_scalar_dim(nvals[0])\n    return tuple((v, v) for i in range(nd))\n  elif nvals.shape == ():\n    # pad\n    v = as_scalar_dim(nvals.flat[0])\n    return tuple((v, v) for i in range(nd))\n  else:\n    raise ValueError(f\"jnp.pad: {name} with {nd=} has unsupported shape {nvals.shape}. \"\n                     f\"Valid shapes are ({nd}, 2), (1, 2), (2,), (1,), or ().\")\n\n\ndef _check_no_padding(axis_padding: tuple[Any, Any], mode: str):\n  if (axis_padding[0] > 0 or axis_padding[1] > 0):\n    msg = \"Cannot apply '{}' padding to empty axis\"\n    raise ValueError(msg.format(mode))\n\n\ndef _pad_constant(array: Array, pad_width: PadValue[int], constant_values: Array) -> Array:\n  nd = np.ndim(array)\n  constant_values = lax._convert_element_type(\n      constant_values, array.dtype, dtypes.is_weakly_typed(array))\n  constant_values_nd = np.ndim(constant_values)\n\n  if constant_values_nd == 0:\n    widths = [(low, high, 0) for (low, high) in pad_width]\n    return lax.pad(array, constant_values, widths)\n\n  if constant_values_nd == 1:\n    if constant_values.shape[-1] == 1:\n      widths = [(low, high, 0) for (low, high) in pad_width]\n      return lax.pad(array, squeeze(constant_values), widths)\n    elif constant_values.shape[-1] != 2:\n      raise ValueError(\"jnp.pad: constant_values has unsupported shape \"","sourceCodeStart":3914,"sourceCodeEnd":3950,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L3914-L3950","documentation":"Padding modes 'wrap'/'symmetric'/'reflect'/'edge' are implemented with slicing that requires a nonempty axis; if any padded axis has size 0 and nonzero padding is requested, JAX raises ValueError because there is no data to reflect/wrap.","triggerScenarios":"jnp.pad(jnp.zeros((0, 3)), ((2, 2), (0, 0)), mode='wrap') or mode='symmetric'/'reflect'/'edge' on an empty axis with padding > 0.","commonSituations":"Batch pipelines where a dynamic batch or sequence dimension can collapse to zero; filtering/selection producing empty arrays before padding.","solutions":["Use mode='constant' when the array may have empty axes","Guard the empty case: skip padding or pad only nonempty axes","Fix upstream logic that produces zero-length axes"],"exampleFix":"// before\njnp.pad(x, ((2, 2), (0, 0)), mode='wrap')\n// after\nmode = 'wrap' if x.shape[0] > 0 else 'constant'\njnp.pad(x, ((2, 2), (0, 0)), mode=mode)","handlingStrategy":"type-guard","validationCode":"mode = 'wrap'\nif any(s == 0 and (b > 0 or a > 0) for s, (b, a) in zip(x.shape, pad_width)) and mode != 'constant':\n    mode = 'constant'","typeGuard":"def can_nonconstant_pad(x, pad_width) -> bool:\n    return all(sz > 0 or (b == 0 and a == 0) for sz, (b, a) in zip(x.shape, pad_width))","tryCatchPattern":null,"preventionTips":["Validate nonempty axes before reflect/wrap/edge padding","Fall back to constant mode for possibly-empty batches"],"tags":["jnp-pad","empty-axis","reflect","wrap"],"backgroundTag":"operation-on-empty-array","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}