{"record":{"id":"c2e81c3863443e07","repo":"jax-ml/jax","slug":"wrong-number-of-pads-for-spatial-dimensions","errorCode":null,"errorMessage":"Wrong number of pads for spatial dimensions","messagePattern":"Wrong number of pads for spatial dimensions","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax_reference.py","lineNumber":459,"sourceCode":"      return [\n          (pad_size // 2, pad_size - pad_size // 2) for pad_size in pad_sizes\n      ]\n    else:\n      return [\n          (pad_size - pad_size // 2, pad_size // 2) for pad_size in pad_sizes\n      ]\n  else:\n    return [(0, 0)] * len(in_shape)\n\ndef _conv_view(lhs, rhs_shape, window_strides, pads, pad_value):\n  \"\"\"Compute the view (and its axes) of a convolution or window reduction.\"\"\"\n  if (_min(lhs.ndim, len(rhs_shape)) < 2 or lhs.ndim != len(rhs_shape)\n      or lhs.shape[1] != rhs_shape[1]):\n    raise ValueError('Dimension mismatch')\n  if len(window_strides) != len(rhs_shape) - 2:\n    raise ValueError('Wrong number of strides for spatial dimensions')\n  if len(pads) != len(rhs_shape) - 2:\n    raise ValueError('Wrong number of pads for spatial dimensions')\n\n  lhs = _pad(lhs, [(0, 0)] * 2 + list(pads), pad_value)\n  in_shape = lhs.shape[2:]\n  filter_shape = rhs_shape[2:]\n  dim = len(filter_shape)  # number of 'spatial' dimensions in convolution\n\n  out_strides = np.multiply(window_strides, lhs.strides[2:])\n  view_strides = lhs.strides[:1] + tuple(out_strides) + lhs.strides[1:]\n\n  out_shape = np.floor_divide(\n      np.subtract(in_shape, filter_shape), window_strides) + 1\n  view_shape = lhs.shape[:1] + tuple(out_shape) + rhs_shape[1:]\n\n  view = np.lib.stride_tricks.as_strided(lhs, view_shape, view_strides)\n\n  view_axes = list(range(view.ndim))\n  sum_axes = view_axes[-dim-1:]\n  rhs_axes = [view.ndim] + sum_axes","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax_reference.py#L441-L477","documentation":"The NumPy reference conv/window view requires exactly one (lo, hi) padding pair per spatial dimension (rhs rank minus 2). More or fewer pads raise this ValueError.","triggerScenarios":"Passing padding as full-rank (N+2 pairs) or a flat list of numbers instead of per-spatial-dim pairs.","commonSituations":"Reusing padding configs from lax.reduce_window (which is full-rank) with the reference conv API (spatial-only).","solutions":["Provide pads as a sequence of (low, high) tuples, one per spatial dimension","Convert 'SAME'/'VALID' strings via jax.lax.padtype_to_pads before calling"],"exampleFix":"# before\npads = ((0,0),(1,1),(1,1),(0,0))  # full rank\nlax_reference._conv_view(..., pads, ...)\n# after\npads = ((1,1),(1,1))  # spatial only\nlax_reference._conv_view(..., pads, ...)","handlingStrategy":"validation","validationCode":"assert len(pads) == len(rhs_shape) - 2 and all(len(p) == 2 for p in pads)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use jax.lax.padtype_to_pads for SAME/VALID conversions"],"tags":["jax","numpy-reference","padding","shape-validation","convolution"],"backgroundTag":"convolution-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}