`jnp.linalg.norm` narrows float16 back *before* the `sqrt`, discarding the float32 headroom it just paid for (wrong primal, silently-zero gradient)
Description
For float16 input, jnp.linalg.norm already upcasts to float32 to accumulate
sum(x * conj(x)) — and then converts the sum back to float16 before taking the
sqrt. The narrowing happens exactly one operation too early: the sum of squares is
the quantity that needs the range, and the sqrt is what would have brought it back
into float16 range.
The result is inf for norms that are perfectly representable in float16, and
jax.grad then returns a silent 0.0 for every entry, since it evaluates
x / ‖x‖ as x / inf. There is no inf or nan in the gradient to signal that
anything went wrong.
This is not a request for a scaling/hypot guard — the float32 accumulation buffer is
already there, and simply keeping it until after the sqrt returns the exact answer.
Reproducer
import jax, jax.numpy as jnp, torch
x_jax = jnp.full((3, 3), 100, dtype=jnp.float16)
x_torch = torch.full((3, 3), 100.0, dtype=torch.float16, requires_grad=True)
y = torch.linalg.norm(x_torch); y.backward()
# ||x|| = sqrt(9 * 100^2) = 300, comfortably representable in float16 (max 65504);
# only the intermediate sum(x^2) = 90000 is not.
print("true value / grad :", 300.0, "/", round(100 / 300, 4))
print("jnp.linalg.norm :", jnp.linalg.norm(x_jax))
print("jax.grad :", jax.grad(jnp.linalg.norm)(x_jax).ravel()[:3])
print("torch.linalg.norm :", y.detach())
print("torch grad :", x_torch.grad.ravel()[:3])
print("\njaxpr:")
print(jax.make_jaxpr(jnp.linalg.norm)(x_jax))
x32 = jnp.full((3, 3), 100, dtype=jnp.float32)
print("same math, narrowing after the sqrt instead of before:",
jnp.sqrt(jnp.sum(jnp.square(x32))).astype(jnp.float16))
Actual output
true value / grad : 300.0 / 0.3333
jnp.linalg.norm : inf
jax.grad : [0. 0. 0.]
torch.linalg.norm : tensor(300., dtype=torch.float16)
torch grad : tensor([0.3333, 0.3333, 0.3333], dtype=torch.float16)
jaxpr:
{ lambda ; a:f16[3,3]. let
b:f16[] = jit[
name=norm
jaxpr={ lambda ; a:f16[3,3]. let
c:f16[3,3] = mul a a
d:f32[3,3] = convert_element_type[new_dtype=float32 weak_type=False] c
e:f32[] = reduce_sum[axes=(0, 1) out_sharding=None] d
f:f16[] = convert_element_type[new_dtype=float16 weak_type=False] e
b:f16[] = sqrt f
in (b,) }
] a
in (b,) }
same math, narrowing after the sqrt instead of before: 300.0
Related issues
#5816 (underflow) and #26248 (gradient at ‖x‖ = 0) both concern the fact that
sqrt(Σx²) has no scaling guard — a dtype-independent property that NumPy and PyTorch
share, and where rescaling was set aside over the cost of a second reduction.
This report is narrower and needs none of that: only the float16 path is affected, JAX already allocates the wider accumulator, and the fix is to stop discarding it one operation early.
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.11.1.dev20260810
jaxlib: 0.11.1.dev20260810
numpy: 2.2.6
python: 3.12.13 | packaged by conda-forge | (main, Mar 5 2026, 16:50:00) [GCC 14.3.0]
device info: cpu-1, 1 local devices"
process_count: 1
platform: uname_result(system='Linux', node='xx', release='6.8.0-134-generic', version='#134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026', machine='x86_64')
JAX_ENABLE_X64=1
There is always some ambiguity in user-intent when it comes to bfloat16 operations.
It sounds like the behavior you want is "cast inputs to float32, compute the norm, then cast the outputs back to bfloat16".
If that's what you want, you can spell it explicitly like this:
jnp.linalg.norm(x.astype('float32')).astype('bfloat16')
In general, if the user doesn't cast the input, then we typically do the computation at the precision of the input.
One exception to that is sum, because when sums are accumulated at bfloat16 precision the rounding error can be very extreme; for example:
In [1]: import jax.numpy as jnp
In [2]: x = jnp.ones(1000, dtype='bfloat16')
In [3]: sum(x) # Python sum does no implicit cast
Out[3]: Array(256, dtype=bfloat16)
Once bfloat16 hits 256, the next representable number is 258, so adding 1 at bfloat16 precision has no effect. This tends to cause surprising behavior, so we implicitly cast to float32 in the case of sum only.
Overall I'd say this is working as intended: when you opt-in to using bfloat16, you are opting in to its limited floating point resolution. If you don't want limited floating point resolution, you shouldn't be using bfloat16.
Does that make sense?
Thanks for the explanation. I can see now why we want to upcast sum when at low precision. That is, when the partial sum grows much larger than the next term being added, the addition can have no effect at all in finite precision, because the increment falls below the ULP.
That said, I’d like to revise my earlier suggestion of “compute at higher precision, cast to input precision at the end”, to instead rewrite the norm in a more numerically stable form.
This touches on a well-known issue with norm (see Higham, Accuracy and Stability of Numerical Algorithms, §27.8, chapter screenshot attached). sum(x^2) grows quickly and overflows easily, and low precision makes this worse simply because its representable range is smaller. This is mentioned in #5816 and #26248.
A standard fix (used in LAPACK's dnrm2 ) is to scale before summing:
def norm(a):
t = jnp.max(jnp.abs(a))
c = jnp.abs(a / t) ** 2
e = jnp.sum(c)
return jnp.sqrt(e) * t
Dividing by the max magnitude keeps every term ≤ 1 before the sum, which avoids the overflow.
Does this fix look reasonable? @jakevdp @mattjj Happy to put together a PR.
Separately: the existing tests compare against numpy.linalg.norm (linalg_test.py#L542), which has the same float16 overflow behavior. A fix like this would disagree with NumPy even though it'd be the more accurate result. I'll raise that with the NumPy folks separately.