jnp.floor_divide / divmod return a quotient larger than x1/x2 for some float16 and bfloat16 inputs
Description
Description
For float16 (and bfloat16) inputs, jnp.floor_divide sometimes returns a quotient that is larger than the true
quotient x1 / x2 (by 1, sometimes 2), contradicting "rounded down ... towards negative infinity" and disagreeing
with numpy.floor_divide. jnp.divmod inherits it, so x2 * (x1 // x2) + x1 % x2 == x1 fails. About 0.3% of
random float16 pairs with x1 in [-2000, 2000] and x2 in [-50, 50] are affected; float32 and float64 are not.
Steps to reproduce
import numpy as np, jax.numpy as jnp
print(np.floor_divide(np.float16(1226), np.float16(1.5))) # 817.0
print(jnp.floor_divide(jnp.float16(1226), jnp.float16(1.5))) # 818.0
Expected
817.0, as NumPy returns.
Suggested fix
Evaluate _float_divmod in float32 for narrower float dtypes and cast the results back.
System info (python version, jaxlib version, accelerator, etc.)
Environment
JAX / jaxlib 0.11.2 (the latest release, CPU) and 0.10.1, Python 3.12, Ubuntu 24.04, x86_64
Thanks for the report! I think this is working as expected.
For low-bit operations, NumPy generally casts inputs to 64-bit to perform the operation, and then downcasts back to the original type. This makes sense on CPU: 64-bit results are much more accurate, and as CPUs are typically optimized for 64-bit operations, it's often as fast or faster than doing the operation at lower precision.
JAX has different constraints: often on accelerators, wider-precision operations can be much slower than their narrow-precision counterparts (an extreme example is on TPU, where float32 matmul is implemented via six bfloat16 matmul passes). In AI modeling contexts, often users explicitly choose 16-bit inputs to achieve speed at the expense of accuracy.
If we silently cast float16 inputs to float32 in the name of accuracy, we no longer serve these users: for this reason, most APIs in JAX are designed to respect the input dtype. So divmod in [b]float16 is fast, but less accurate than it could be – and if the user wants that accuracy, they can do the float32 cast explicitly.
What do you think?
@jakevdp, l loved the idea....I just wanted to enquire if there are chances of violating the semantics of floor division on the way...suppose we return '818' instead of suppose an exact quotient '817.333'. But I agree that preserving the low-precision dtypes is important for accelerator performance. Would a targeted correction without unconditional dtype promotion be feasible, or should this limitation be explicitly documented for low-precision dtypes? I'd appreciate your thoughts.
I think it's possible that we could come up with a better implementation of _float_divmod. These off-by-one errors are not limited to float16 – they will also happen for float32 and float64, albeit at larger values.
@jakevdp I investigated quite a number of approaches for #41206. Direct floor division increased mismatches from 302 to 3,165 per 100k samples, while remainder based correction reduced errors but introduced edge-case regressions. The approach eliminated the observed numerical mismatches in our randomized tests, and all 1,630 operator tests passed. CPU benchmarks were encouraging, though accelerator performance remains unverified.
I tried to solve this in the above PR .Hope it helps to address this issue. But for any inconsistencies, I am free to improve and incorporate changes
Regarding solving this by casting to a wider type – it doesn't actually fix the root issue, it only punts to larger values. As reported above, divmod(1226, 1.5) is off by one in float16 with the current algorithm.
But for similar reasons, you'll find that in float32, JAX is off-by-one in jnp.float32(11_000_000) // jnp.float32(1.5), and NumPy (which uses a slightly different algorithm) is off-by-one in np.float32(11_000_003) // np.float32(1.5).
Similar off-by-one errors appear in float64 in NumPy's np.floor_divide(5000000000000003.0, 1.5) and even in Python's built-in 5000000000000003.0 // 1.5, which you can confirm by computing via the more precise integer floordiv:
>>> x = 5000000000000003
>>> float(x) // 1.5 # off by one
3333333333333334.0
>>> (x * 2) // 3 # correct
3333333333333335
Fundamentally this is a problem with how the standard implementation of float_divmod interacts with the limits of floating point precision. We can't typecast our way out of this.
Yeah, I would agree as well. Casting from float16 to 32 might help to increase accuracy, but it might hurt performance.
Thanks @jakevdp and @the-Shallow for pointing out. I had doubts with this one, and I knew there is a chance of error but ignored it. Noted all the suggestions given by @jakevdp. I realised in the meantime that I was purposely using unnecessary casting which eventually would be a barrier while running them in gpus.
Given the observations in https://github.com/jax-ml/jax/issues/41206#issuecomment-6065249739 about how this is a standard "feature" of divmod near the limits of floating point precision, I'm going to close this.