{"record":{"id":"16dbde98820b1e4c","repo":"jax-ml/jax","slug":"stat-length-of-0-yields-no-value-for-padding","errorCode":null,"errorMessage":"stat_length of 0 yields no value for padding","messagePattern":"stat_length of 0 yields no value for padding","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":4120,"sourceCode":"    ramp_after = flip(ramp_after, axis)\n\n    array = lax.concatenate([ramp_before, array, ramp_after], dimension=axis)\n  return array\n\n\ndef _pad_stats(array: Array, pad_width: PadValue[int],\n               stat_length: PadValue[int] | None,\n               stat_func: PadStatFunc) -> Array:\n  nd = np.ndim(array)\n  for i in range(nd):\n    if stat_length is None:\n      stat_before = stat_func(array, axis=i, keepdims=True)\n      stat_after = stat_before\n    else:\n      array_length = array.shape[i]\n      length_before, length_after = stat_length[i]\n      if length_before == 0 or length_after == 0:\n        raise ValueError(\"stat_length of 0 yields no value for padding\")\n\n      # Limit stat_length to length of array.\n      length_before = min(length_before, array_length)\n      length_after = min(length_after, array_length)\n\n      slice_before = lax_slicing.slice_in_dim(array, 0, length_before, axis=i)\n      slice_after = lax_slicing.slice_in_dim(array, -length_after, None, axis=i)\n      stat_before = stat_func(slice_before, axis=i, keepdims=True)\n      stat_after = stat_func(slice_after, axis=i, keepdims=True)\n\n    if np.issubdtype(array.dtype, np.integer):\n      stat_before = round(stat_before)\n      stat_after = round(stat_after)\n\n    stat_before = lax._convert_element_type(\n        stat_before, array.dtype, dtypes.is_weakly_typed(array))\n    stat_after = lax._convert_element_type(\n        stat_after, array.dtype, dtypes.is_weakly_typed(array))","sourceCodeStart":4102,"sourceCodeEnd":4138,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L4102-L4138","documentation":"For mode='maximum'/'minimum'/'mean'/'median', jnp.pad computes the statistic over stat_length elements of the original array. A stat_length of 0 on either side means there is no data to compute a statistic from, so it raises ValueError.","triggerScenarios":"jnp.pad(x, 2, mode='mean', stat_length=(0, 3)) or stat_length entries of 0 in the per-axis tuples.","commonSituations":"Passing stat_length that includes 0 to mean 'no stat region'; per-axis stat_length lists built dynamically where some entries default to 0.","solutions":["Use stat_length >= 1 on both sides, or omit stat_length (defaults to whole axis)","Build per-axis stat_length with max(1, value)"],"exampleFix":"// before\njnp.pad(x, 2, mode='mean', stat_length=(0, 3))\n// after\njnp.pad(x, 2, mode='mean', stat_length=(1, 3))","handlingStrategy":"validation","validationCode":"sl = np.broadcast_to(np.asarray(stat_length), (x.ndim, 2))\nassert (sl > 0).all(), 'stat_length must be >= 1'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Omit stat_length unless you need it; clamp entries with max(1, v)"],"tags":["jnp-pad","stat-length","statistical-mode"],"backgroundTag":"invalid-argument-value","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}