jax icon indicating copy to clipboard operation
jax copied to clipboard

jnp.[nan]quantile & jnp.[nan]percentile fails on empty reduction axes

Open edluyuan opened this issue 4 months ago • 3 comments

Description

The Problem

jnp.nanquantile raises an indexing or gather error when the reduction axis is empty, however, NumPy returns NaNs with the expected reduced shape.

Reproduction

import jax
import jax.numpy as jnp
import numpy as np

x_np = np.empty((0, 4), dtype=np.float32)
x_jax = jnp.asarray(x_np)

print(np.nanquantile(x_np, 0.5, axis=0))
print(jnp.nanquantile(x_jax, 0.5, axis=0))
print(jax.jit(lambda x: jnp.nanquantile(x, 0.5, axis=0))(x_jax))

NumPy returns:

[nan nan nan nan]

JAX instead raises an indexing / gather error. but JAX should match:

np.nanquantile(np.empty((0, 4)), 0.5, axis=0)

and return NaNs with the appropriate reduced shape for an empty reduction axis.

I also observed the failure with:

  • axis=None
  • tuple-valued axes
  • keepdims=True
  • scalar and vector q
  • eager execution and jax.jit

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

jax:    0.11.1.dev20260728+7238390c24
jaxlib: 0.11.0
numpy:  2.5.1
python: 3.12.12 (main, Oct 14 2025, 21:38:21) [Clang 20.1.4 ]
device info: cpu-1, 1 local devices"
process_count: 1
platform: uname_result(system='Darwin', node='Eds-MacBook-Pro.local', release='24.6.0', version='Darwin Kernel Version 24.6.0: Wed Nov  5 21:30:44 PST 2025; root:xnu-11417.140.69.705.2~1/RELEASE_ARM64_T6041', machine='arm64')

edluyuan avatar Jul 27 '26 19:07 edluyuan

Opened PR #39613 with a fix. When the reduction axis is empty, _quantile now delegates to nanmean (matching NumPy behaviour) instead of proceeding to the gather step which crashes.

AloysJehwin avatar Jul 31 '26 10:07 AloysJehwin

Could a maintainer add a review label to PR #39613? Thanks!

AloysJehwin avatar Jul 31 '26 10:07 AloysJehwin

I've opened PR #39475 with the fixes and accompanying tests. Could a maintainer add the review label when convenient? Thanks!

edluyuan avatar Jul 31 '26 10:07 edluyuan