{"record":{"id":"159e1d8668be21a6","repo":"jax-ml/jax","slug":"length-of-padding-config-must-equal-the-number-of","errorCode":null,"errorMessage":"length of padding_config must equal the number of axes of operand, got padding_config {padding_config} for operand shape {op_shape}","messagePattern":"length of padding_config must equal the number of axes of operand, got padding_config (.+?) for operand shape (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":7629,"sourceCode":"            _split_vma_rule, _split_ur_rule, None))\nsplit_p.def_impl(partial(dispatch.apply_primitive, split_p))\nad.deflinear2(split_p, _split_transpose_rule)\nbatching.primitive_batchers[split_p] = _split_batch_rule\nmlir.register_lowering(split_p, _split_lower)\n\ndef _pad_dtype_rule(operand, padding_value, *, padding_config):\n  if operand.dtype != padding_value.dtype:\n    msg = \"pad operand and padding_value must be same dtype: got {} and {}.\"\n    raise TypeError(msg.format(operand.dtype, padding_value.dtype))\n\n  return input_dtype(operand, padding_value)\n\ndef _pad_shape_rule(operand, padding_value, *, padding_config):\n  if np.ndim(padding_value) != 0:\n    raise ValueError(f\"padding_value must be a scalar; got {np.shape(padding_value)=}\")\n  op_shape = np.shape(operand)\n  if not len(padding_config) == np.ndim(operand):\n    raise ValueError(\"length of padding_config must equal the number of axes \"\n                     f\"of operand, got padding_config {padding_config} \"\n                     f\"for operand shape {op_shape}\")\n  if not all(i >= 0 for _, _, i in padding_config):\n    raise ValueError(\"interior padding in padding_config must be nonnegative, \"\n                     f\"got padding_config {padding_config}\")\n  result = tuple(l + h + core.dilate_dim(d, i + 1)\n                 for (l, h, i), d in zip(padding_config, op_shape))\n  if not all(d >= 0 for d in result):\n    msg = (f\"Dimension size after padding is not at least 0, \"\n           f\"got result shape {result}, for padding_config {padding_config}\"\n           f\" and operand shape {op_shape}\")\n    raise ValueError(msg)\n  return result\n\ndef _pad_sharding_rule(operand, padding_value, *, padding_config):\n  # TODO(yashkatariya): Once JAX supports uneven sharding at the top level,\n  # change this logic to `return operand.sharding` directly.\n  out_shape = _pad_shape_rule(operand, padding_value,","sourceCodeStart":7611,"sourceCodeEnd":7647,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L7611-L7647","documentation":"padding_config for jax.lax.pad must contain exactly one (low, high, interior) triple per operand dimension. This ValueError fires when len(padding_config) != operand.ndim, e.g. 2 triples for a 3-D array.","triggerScenarios":"Passing [(1,1,0),(1,1,0)] for a rank-3 tensor; reusing a 2-D conv padding config on 3-D input; hardcoding pad widths after adding a batch/channel axis.","commonSituations":"Adding channels-first/batch dims to a pipeline without updating pad configs; adapting image padding code for video (rank+1); configs shared across models with different ranks.","solutions":["Build the config from the operand's ndim: config = tuple(pad for _ in range(x.ndim))","Derive per-axis widths programmatically instead of hardcoding","Check x.ndim and len(padding_config) with an assert before calling lax.pad"],"exampleFix":"# before\nconfig = [(1,1,0), (1,1,0)]\ny = jax.lax.pad(x3d, 0, config)  # x3d.ndim == 3\n# after\nconfig = [(1,1,0)] * x3d.ndim\ny = jax.lax.pad(x3d, 0, config)","handlingStrategy":"validation","validationCode":"assert len(padding_config) == x.ndim, (len(padding_config), x.ndim)","typeGuard":"def config_matches_rank(config, x) -> bool:\n    return len(config) == x.ndim","tryCatchPattern":null,"preventionTips":["Build padding_config as [(l, h, i)] * x.ndim or per-axis from the shape","Never hardcode pad configs across models of different rank"],"tags":["jax","pad","config-mismatch","rank-error"],"backgroundTag":"padding-config-invalid","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}