{"record":{"id":"ff6d57570c43850e","repo":"jax-ml/jax","slug":"wrong-number-of-explicit-pads-for-convolution-exp","errorCode":null,"errorMessage":"Wrong number of explicit pads for convolution: expected {}, got {}.","messagePattern":"Wrong number of explicit pads for convolution: expected (.+?), got (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/convolution.py","lineNumber":904,"sourceCode":"  new_shape = list(np.delete(x.shape, src))\n  new_shape[dst] *= x.shape[src]\n  return lax.reshape(x, new_shape, perm)\n\ndef _reshape_axis_out_of(src, size1, x):\n  shape = list(x.shape)\n  size2, ragged = divmod(shape[src], size1)\n  assert not ragged\n  shape[src:src+1] = [size1, size2]\n  return lax.reshape(x, shape)\n\n\ndef conv_shape_tuple(lhs_shape, rhs_shape, strides, pads, batch_group_count=1):\n  \"\"\"Compute the shape tuple of a conv given input shapes in canonical order.\"\"\"\n  if isinstance(pads, str):\n    pads = lax.padtype_to_pads(lhs_shape[2:], rhs_shape[2:], strides, pads)\n  if len(pads) != len(lhs_shape) - 2:\n    msg = \"Wrong number of explicit pads for convolution: expected {}, got {}.\"\n    raise TypeError(msg.format(len(lhs_shape) - 2, len(pads)))\n\n  lhs_padded = np.add(lhs_shape[2:], np.sum(np.array(pads).reshape(-1, 2),\n                                              axis=1))\n  if np.any(lhs_padded < 0):\n    raise ValueError(\"Negative padding is larger than the size of the corresponding dimension: \"\n                     f\"got padding={pads} for lhs_shape[2:]={lhs_shape[2:]}\")\n  out_space = tuple(map(core.stride_dim, lhs_padded, rhs_shape[2:], strides))\n  if batch_group_count > 1:\n    assert lhs_shape[0] % batch_group_count == 0\n    out_shape_0 = lhs_shape[0] // batch_group_count\n  else:\n    out_shape_0 = lhs_shape[0]\n  out_shape = (out_shape_0, rhs_shape[0])\n  return tuple(out_shape + tuple(out_space))\n\n\ndef conv_general_shape_tuple(lhs_shape, rhs_shape, window_strides, padding,\n                             dimension_numbers):","sourceCodeStart":886,"sourceCodeEnd":922,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/convolution.py#L886-L922","documentation":"conv_shape_tuple computes the output shape of a convolution from canonical (N,H,W,C) input shapes. When padding is given as an explicit list of (lo,hi) pairs, there must be exactly one pair per spatial dimension (lhs rank minus 2). Any other length raises this TypeError.","triggerScenarios":"Passing pads as e.g. [(0,0)] for a 2-spatial-dim conv, or passing a flat list of 4 ints instead of 2 pairs, to functions like lax.conv_general_dilated / conv_general_shape_tuple with explicit pads.","commonSituations":"Hand-constructing padding lists after reading XLA conv specs; converting 'SAME'/'VALID' logic into explicit pads incorrectly; mixing up rank between 1D/2D convs.","solutions":["Provide pads as a sequence of (low, high) pairs, one per spatial dim: ((0,0),(1,1)) for 2D","Or pass padding as a string 'SAME'/'VALID' and let JAX compute pads","Double-check the spatial rank: len(pads) must equal len(lhs_shape)-2"],"exampleFix":"# before\nout = lax.conv_general_dilated(x, w, (1,1), [(0,0,1,1)])\n# after\nout = lax.conv_general_dilated(x, w, (1,1), ((0,0),(1,1)))","handlingStrategy":"validation","validationCode":"assert len(pads) == len(lhs.shape) - 2 and all(len(p) == 2 for p in pads), f'pads {pads} for rank {lhs.ndim}'","typeGuard":"def valid_pads(pads, spatial_ndim) -> bool:\n    return (isinstance(pads, str) and pads in ('SAME','VALID')) or (\n        isinstance(pads, (list, tuple)) and len(pads) == spatial_ndim and\n        all(len(p) == 2 for p in pads))","tryCatchPattern":null,"preventionTips":["Use 'SAME'/'VALID' unless cropping is genuinely needed","Represent pads as tuple-of-pairs in config files, not flat lists"],"tags":["jax","convolution","padding","shape-validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}