{"record":{"id":"ce2e9b328aa8694b","repo":"jax-ml/jax","slug":"pad-operand-and-padding-value-must-be-same-dtype","errorCode":null,"errorMessage":"pad operand and padding_value must be same dtype: got {} and {}.","messagePattern":"pad operand and padding_value must be same dtype: got (.+?) and (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":7620,"sourceCode":"  out_vma = core.standard_vma_rule('split', operand)\n  out_shapes = _split_shape_rule(operand, sizes=sizes, axis=axis)\n  return [out_vma] * len(out_shapes)\n\nsplit_p = core.Primitive('split')\nsplit_p.multiple_results = True\nsplit_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, \"","sourceCodeStart":7602,"sourceCodeEnd":7638,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L7602-L7638","documentation":"jax.lax.pad (used by jnp.pad's 'constant'-like internal path and directly) requires padding_value to have exactly the same dtype as the operand, because the output dtype is the operand dtype and the fill value must be representable in it. A mismatch raises this TypeError at the dtype-rule stage.","triggerScenarios":"jax.lax.pad(jnp.zeros(3, jnp.float32), 0) — Python int weakly typed to int32/weak but still mismatched under some promotion contexts; padding a float32 array with jnp.int32(0); padding a bfloat16 array with a float32 scalar.","commonSituations":"Padding with default Python 0 or 1 on half-precision (bfloat16) tensors; mixing dtypes after enabling legacy or strict promotion settings; padding int arrays with float values.","solutions":["Cast the padding value: jax.lax.pad(x, jnp.asarray(0, x.dtype), ...);","Prefer jnp.pad(x, pads, mode='constant', constant_values=...) which handles promotion","Standardize dtypes at model input boundaries to avoid mixed-dtype constants"],"exampleFix":"# before\ny = jax.lax.pad(x_bf16, 0.0, config)\n# after\ny = jax.lax.pad(x_bf16, jnp.asarray(0.0, x_bf16.dtype), config)","handlingStrategy":"type-guard","validationCode":"pad_val = jnp.asarray(pad_val, x.dtype)  # or jnp.result_type check","typeGuard":"def same_dtype(x, v) -> bool:\n    return jnp.result_type(x) == jnp.result_type(v)","tryCatchPattern":null,"preventionTips":["Always cast fill values to x.dtype at pad sites","Standardize model dtype (e.g. bfloat16) and cast constants at input boundaries"],"tags":["jax","pad","dtype-mismatch"],"backgroundTag":"dtype-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}