{"record":{"id":"3ac937bfce6d3540","repo":"jax-ml/jax","slug":"reduce-precision-mantissa-bits-must-be-non-negati","errorCode":null,"errorMessage":"reduce_precision: mantissa_bits must be non-negative; got {mantissa_bits}","messagePattern":"reduce_precision: mantissa_bits must be non-negative; got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":8817,"sourceCode":"mlir.register_lowering(reduce_or_p, partial(_unary_reduce_lower, hlo.OrOp,\n                                            _get_bitwise_or_identity))\nmlir.register_lowering(reduce_and_p, partial(_unary_reduce_lower, hlo.AndOp,\n                                             _get_bitwise_and_identity))\nmlir.register_lowering(reduce_xor_p, partial(_unary_reduce_lower, hlo.XorOp,\n                                             _get_bitwise_or_identity))\nmlir.register_lowering(reduce_min_p, partial(_unary_reduce_lower, mlir.min_hlo,\n                                             _get_min_identity))\nmlir.register_lowering(reduce_max_p, partial(_unary_reduce_lower, mlir.max_hlo,\n                                             _get_max_identity))\n\n\ndef _reduce_precision_shape_rule(operand, *, exponent_bits, mantissa_bits):\n  exponent_bits = operator.index(exponent_bits)\n  mantissa_bits = operator.index(mantissa_bits)\n  if exponent_bits < 1:\n    raise ValueError(f\"reduce_precision: exponent_bits must be positive; got {exponent_bits}\")\n  if mantissa_bits < 0:\n    raise ValueError(f\"reduce_precision: mantissa_bits must be non-negative; got {mantissa_bits}\")\n  return operand.shape\n\ndef _reduce_precision_sharding_rule(operand, *, exponent_bits, mantissa_bits):\n  return operand.sharding\n\ndef _reduce_precision_memory_space_rule(operand, *, exponent_bits, mantissa_bits):\n  return operand.memory_space\n\ndef _reduce_precision_ur_rule(operand, *, exponent_bits, mantissa_bits):\n  out_unreduced = core.getu(operand)\n  kind = UnreducedKind.sum if out_unreduced else None\n  return out_unreduced, core.getr(operand), kind\n\nreduce_precision_p = standard_primitive(\n    _reduce_precision_shape_rule,\n    partial(unop_dtype_rule, _identity, _float, 'reduce_precision'),\n    name='reduce_precision', sharding_rule=_reduce_precision_sharding_rule,\n    vma_rule=partial(core.standard_vma_rule, 'reduce_precision'),","sourceCodeStart":8799,"sourceCodeEnd":8835,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L8799-L8835","documentation":"reduce_precision's mantissa_bits parameter must be >= 0. A negative mantissa means no significand bits, which describes no representable floating-point format, so the shape rule rejects it.","triggerScenarios":"lax.reduce_precision(x, exponent_bits=5, mantissa_bits=-1); typically from computed bit budgets in quantization experiments.","commonSituations":"Sweeps over (exponent_bits, mantissa_bits) that include negative mantissa values; off-by-one errors in format-conversion code (e.g., FP8 E4M3 handled as mantissa=3 but E5M2 mishandled).","solutions":["Guard the sweep: only call reduce_precision when mantissa_bits >= 0 and exponent_bits >= 1.","For FP8 E5M2 simulation use exponent_bits=5, mantissa_bits=2.","Fix the bit-allocation arithmetic that produced a negative mantissa."],"exampleFix":"# before\ny = lax.reduce_precision(x, 5, -1)\n# after\ny = lax.reduce_precision(x, 5, 2)","handlingStrategy":"validation","validationCode":"assert exponent_bits >= 1 and mantissa_bits >= 0, (exponent_bits, mantissa_bits)\ny = lax.reduce_precision(x, exponent_bits, mantissa_bits)","typeGuard":"def valid_fp_format(e, m):\n    return e >= 1 and m >= 0","tryCatchPattern":null,"preventionTips":["Validate quantization sweeps before each call.","Remember FP8 formats: E4M3 (4,3), E5M2 (5,2)."],"tags":["jax","reduce-precision","quantization","validation"],"backgroundTag":"invalid-argument-range","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}