jax icon indicating copy to clipboard operation
jax copied to clipboard

Numerical Discrepancies between Pallas Kernel and Standard JAX with bfloat16

Open depksingh opened this issue 10 months ago • 4 comments

Description

We are observing small numerical differences between a JAX function compiled with @jax.jit and an equivalent operation implemented as a Pallas kernel when using jnp.bfloat16 data types. The operations involve taking the mean of the squares of a tiled 2D array row-wise, followed by a reciprocal square root. While all intermediate computations within both versions are explicitly cast to jnp.float32, the final results, when cast back to jnp.bfloat16, show differences in a small number of elements.

The standard JAX version and the Pallas kernel version produce bit-identical results when the input and output types are jnp.float32. The discrepancies only appear when downcasting the float32 results to jnp.bfloat16.

We suspect this might be due to slightly different floating-point summation orders within the jnp.mean operation between XLA's standard optimization and the Pallas/Mosaic tiled execution, leading to minute differences in the float32 results. These differences, although small in float32, cross rounding boundaries when converting to bfloat16, especially after being transformed by lax.rsqrt.

Want to check if this discrepancy is expected and is occurring due to how the Pallas handles reduction/sqrt operations wrt Jax, or I'm missing something. Any help would be really appreciated.

Code
# Simple mean 2d kernel for percision mismatch

import jax
import jax.numpy as jnp
from jax.experimental import pallas as pl
import math

def mean_kernel_2d(x, out):
  x_r = x[...]
  x_r = x_r.astype(jnp.float32)
  mean = jnp.mean(lax.square(x_r), axis=-1, keepdims=True)
  mean = lax.rsqrt(mean)
  
  out[...] =  mean.astype(out.dtype)

def mean_fun_2d(x: jax.Array) -> jax.Array:
  TILE_SIZE = 256
  # Example, assuming x.shape is (N, M)
  N, M = x.shape
  output_shape = jax.ShapeDtypeStruct((N, 1), dtype=x.dtype)

  return pl.pallas_call(
      mean_kernel_2d,
      grid=(N // TILE_SIZE, 1),
      in_specs=[pl.BlockSpec((TILE_SIZE, M), lambda i, j: (i, j))],
      out_specs=pl.BlockSpec((TILE_SIZE, 1), lambda i, j: (i, j)),
      out_shape=output_shape,
      debug=False,
  )(x)


key = jax.random.PRNGKey(2)
dtype = jnp.bfloat16
rows = 8192
cols = 1024
x_dummy = jax.random.normal(key, (rows, cols), dtype=dtype)

jitted_mean_fun_2d = jax.jit(mean_fun_2d)
result_pallas = jitted_mean_fun_2d(x_dummy)

@jax.jit
def tmp_fun(x):
  tmp =  jnp.mean(lax.square(x.astype(jnp.float32)), axis=-1, keepdims=True)
  tmp = lax.rsqrt(tmp)
  return tmp.astype(jnp.bfloat16)

val_jax = tmp_fun(x_dummy) 

print(result_pallas.dtype, val_jax.dtype)

abs_diff = jnp.abs(result_pallas.astype(jnp.bfloat16) - val_jax.astype(jnp.bfloat16))
print(f"Max absolute difference: {jnp.max(abs_diff)}")
val_jax.astype(jnp.bfloat16)

diff_count = jnp.sum(~jnp.equal(result_pallas.astype(jnp.bfloat16), val_jax.astype(jnp.bfloat16)))
print(f"Number of differing elements: {diff_count}")

close = jnp.allclose(result_pallas.astype(jnp.bfloat16), val_jax.astype(jnp.bfloat16), atol=1e-8)
print(f"All close within 1e-6: {close}")

Output I'm getting

Image

System info (python version, jaxlib version, accelerator, etc.)

jax: 0.8.2.dev20251209
jaxlib: 0.8.2.dev20251209
numpy: 2.4.0rc1
python: 3.12.11
device_info: TPU7x-8, 8 local devices
process_count: 1

depksingh avatar Jan 17 '26 08:01 depksingh

I can't seem to reproduce this in colab on TPUv5 (also using JAX+Jaxlib version 0.8.2), so this could be a TPU generation-specific issue.

I'm getting the following output:

bfloat16 bfloat16
Max absolute difference: 0
Number of differing elements: 0
All close within 1e-6: True

Unfortunately I don't have access to TPUv7 atm so I can't help debug further.

justinjfu avatar Jan 19 '26 20:01 justinjfu

I can't seem to reproduce this in colab on TPUv5 (also using JAX+Jaxlib version 0.8.2), so this could be a TPU generation-specific issue.

I'm getting the following output:

bfloat16 bfloat16
Max absolute difference: 0
Number of differing elements: 0
All close within 1e-6: True

Unfortunately I don't have access to TPUv7 atm so I can't help debug further.

Yeah, seems like for v5e the above code is showing same results, so I used v5e and reran above code with PRNG key (5), which gave me the instability repro too, can you please try with PRNGKey(5).

Image

I used these deps version in v5e jax-0.8.2 jaxlib-0.8.2 libtpu-0.0.32

depksingh avatar Jan 20 '26 15:01 depksingh

I don't think this is an issue with the mean reduction actually. If you replace square with abs (needs to be positive for rsqrt) the code seems to be bitwise identical for all seeds I tested. Also the issue shows up for f32 and not just bf16 (if you remove the cast to bf16 inside of the kernel). I think someone internal would have to check the underlying implementation to see if there's a difference.

justinjfu avatar Jan 21 '26 22:01 justinjfu

0.00390625 is half of 1 ULP for bf16, so it is acceptable I think. On newer TPUs, Pallas/Mosaic may have faster impl for lots of ops like rsqrt, but the cost is we may see +/- 1 ULP numerical differences.

yueshengys avatar Apr 21 '26 19:04 yueshengys