{"record":{"id":"457499b62d5422d0","repo":"jax-ml/jax","slug":"padding-value-must-be-a-scalar-got-np-shape-padd","errorCode":null,"errorMessage":"padding_value must be a scalar; got {np.shape(padding_value)=}","messagePattern":"padding_value must be a scalar; got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":7626,"sourceCode":"split_p.def_abstract_eval(\n    partial(standard_multi_result_abstract_eval, split_p, _split_shape_rule,\n            _split_dtype_rule, _split_weak_type_rule, _split_sharding_rule,\n            _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):","sourceCodeStart":7608,"sourceCodeEnd":7644,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L7608-L7644","documentation":"The padding_value in jax.lax.pad must be a scalar (0-D). If an array with any non-empty shape is passed, the pad shape rule raises this ValueError echoing np.shape(padding_value). The value is broadcast into every padded cell, so non-scalars are meaningless here.","triggerScenarios":"jax.lax.pad(x, jnp.zeros((1,)), config); passing a length-1 vector or a per-axis padding array as padding_value.","commonSituations":"Confusing per-axis pad widths (which go in padding_config) with the fill value; reusing a weights array as fill; passing constant_values arrays from another API.","solutions":["Use a true scalar: jax.lax.pad(x, jnp.asarray(0, x.dtype), config) or the Python scalar 0","Put per-axis low/high/interior amounts in padding_config, not in padding_value","For constant-valued padding use jnp.pad(x, widths, constant_values=c)"],"exampleFix":"# before\ny = jax.lax.pad(x, jnp.zeros((1,)), config)\n# after\ny = jax.lax.pad(x, 0, config)","handlingStrategy":"validation","validationCode":"assert np.ndim(padding_value) == 0, np.shape(padding_value)","typeGuard":"def is_scalar(v) -> bool:\n    return np.ndim(v) == 0","tryCatchPattern":null,"preventionTips":["Keep fill values as Python/jnp scalars; widths belong in padding_config","Review lax.pad call sites when porting per-axis padding logic"],"tags":["jax","pad","scalar-required","shape-validation"],"backgroundTag":"non-scalar-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}