{"record":{"id":"3990a788df079ad7","repo":"jax-ml/jax","slug":"negative-padding-is-larger-than-the-size-of-the-co","errorCode":null,"errorMessage":"Negative padding is larger than the size of the corresponding dimension: got padding={pads} for lhs_shape[2:]={lhs_shape[2:]}","messagePattern":"Negative padding is larger than the size of the corresponding dimension: got padding=(.+?) for lhs_shape\\[2:\\]=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/convolution.py","lineNumber":909,"sourceCode":"  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):\n  lhs_perm, rhs_perm, out_perm = conv_general_permutations(dimension_numbers)\n  lhs_trans = np.take(lhs_shape, lhs_perm)\n  rhs_trans = np.take(rhs_shape, rhs_perm)\n  out_trans = conv_shape_tuple(lhs_trans, rhs_trans, window_strides, padding)\n  return tuple(np.take(out_trans, np.argsort(out_perm)))","sourceCodeStart":891,"sourceCodeEnd":927,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/convolution.py#L891-L927","documentation":"Explicit negative padding can shrink the effective input below zero; when the padded lhs spatial dims would become negative, conv_shape_tuple raises this ValueError instead of producing a nonsensical shape. Negative padding itself is legal (cropping) but must not exceed the dimension size.","triggerScenarios":"Passing pads like ((-10,-10),(-10,-10)) to conv_general_dilated when the input spatial dims are smaller than 20, e.g. cropping more than the feature-map size.","commonSituations":"Using negative padding to emulate 'valid-ish' output sizes (e.g. kaiming-style cropping with stride>1) computed from formulae that overshoot small inputs; deep stacks where feature maps shrink below the crop amount.","solutions":["Reduce the negative padding so each spatial dim plus (lo+hi) stays >= 0","Use string padding ('SAME'/'VALID') or non-negative pads instead of manual cropping","Add a shape check in your model code to stop stacking before maps get too small"],"exampleFix":"# before\nout = lax.conv_general_dilated(x, w, (2,2), ((-4,-4),(-4,-4)))  # x is 8x8\n# after\nout = lax.conv_general_dilated(x, w, (2,2), ((-2,-2),(-2,-2)))  # 8-4 >= 0","handlingStrategy":"validation","validationCode":"import numpy as np\npadded = np.add(np.array(lhs.shape)[2:], np.sum(np.array(pads), axis=1))\nassert (padded >= 0).all(), f'negative effective dims: {padded}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cap negative padding per layer at a fraction of the expected feature-map size","Assert feature-map sizes between layers in deep cropping stacks"],"tags":["jax","convolution","padding","shape-validation"],"backgroundTag":"value-out-of-range","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}