jax icon indicating copy to clipboard operation
jax copied to clipboard

Negative index of a narrow integer dtype is normalized in that dtype: x[jnp.int8(-1)] returns the wrong element when the axis is longer than 127

Open hiraditya opened this issue 4 weeks ago • 1 comments

Description

A negative index whose dtype is narrower than the axis length is normalized in its own dtype, so adding the axis length overflows. x[jnp.int8(-1)] on a 300-element array returns element 43, not 299: 300 converted to int8 is 44, and -1 + 44 = 43. No error or warning is raised. lax.dynamic_slice, lax.dynamic_update_slice and lax.dynamic_index_in_dim go to the same wrong place. .at[jnp.int8(-1)].set(...) raises OverflowError: Python integer 300 out of bounds for int8 instead.

import numpy as np, jax, jax.numpy as jnp
from jax import lax

x = jnp.arange(300)
i = jnp.int8(-1)
print(x[i], jnp.take(x, i), lax.dynamic_index_in_dim(x, i, keepdims=False))   # 43 43 43
print(lax.dynamic_slice(x, [i], [5]))                                          # [43 44 45 46 47]
print(np.flatnonzero(lax.dynamic_update_slice(x, jnp.full(5, -9), [i]) == -9)) # [43 44 45 46 47]
print(np.arange(300)[np.int8(-1)])                                             # 299
x.at[i].set(-5)   # OverflowError: Python integer 300 out of bounds for int8

With jnp.int16 or jnp.int32 indices every line gives 299, or the slice starting at 295. NumPy gives 299 for the int8 index. The results are the same under jax.jit.

Where

Both normalizations convert the axis size to the index dtype before adding:

  • jax/_src/lax/slicing.py, _dynamic_slice_indices: d_arr = lax.convert_element_type(d, _dtype(i)), then lax.select(i < 0, i + d_arr, i). The static branch does lax.convert_element_type(i + d if i < 0 else i, _dtype(i)).
  • jax/_src/numpy/indexing.py, _normalize_index: axis_size_val = lax._const(index, axis_size). normalize_indices (the path .at[...] takes) builds the same constant with lax._const(idx.index, size), which is where the OverflowError comes from.

Promoting the index to a dtype that can hold the axis size before normalizing (as jnp.ravel_multi_index already checks for) would fix all of these. #6122 was the unsigned version of this and was fixed in 2021.

System info

jax 0.12.0.1, jaxlib 0.12.0.1, CPU, Linux x86_64. The same code is in jax 0.11.0.


Investigation assisted by an AI coding agent (Claude). Every output above was produced by running the commands shown.

hiraditya avatar Nov 05 '26 16:11 hiraditya

Hi, I've opened PR #42389 to fix this.

The issue appears to come from normalising negative indices in their original narrow integer dtype, which can overflow when the axis size exceeds the dtype's representable range.

The fix promotes signed indices to a sufficiently wide dtype before normalisation. It covers NumPy-style indexing, scatter updates and the relevant lax dynamic indexing and slicing operations.

I've also added regression tests for int8 and int16 indices, including eager and JIT-compiled execution. The original reproducer now gives the expected results for all the operations covered by the PR.

edluyuan avatar Nov 06 '26 11:11 edluyuan