jax-ml/jax · error · TypeError
Integers cannot be raised to negative powers, got integer_po
Error message
Integers cannot be raised to negative powers, got integer_pow({x}, {y}) What it means
lax.integer_pow implements x**y for integer bases with a compile-time integer exponent y. A negative exponent on integers would require fractional results, so its dtype rule raises TypeError immediately.
Source
Thrown at jax/_src/lax/lax.py:4915
y_dtype = dtypes.dtype(y)
assert dtypes.issubdtype(y_dtype, np.inexact)
return convert_element_type(mul(g, mul(log(_replace_zero(x)), ans)), y_dtype)
ad.defjvp2(pow_p, _pow_jvp_lhs, _pow_jvp_rhs)
def _pow_lower(ctx, x, y):
x_aval, y_aval = ctx.avals_in
if x_aval.dtype != y_aval.dtype:
out_aval, = ctx.avals_out
y_aval = y_aval.update(dtype=out_aval.dtype)
y = hlo.convert(mlir.aval_to_ir_type(ctx.module_context, y_aval), y)
ctx = ctx.replace(avals_in=[x_aval, y_aval])
return _nary_lower_hlo(hlo.power, ctx, x, y)
mlir.register_lowering(pow_p, _pow_lower)
def _integer_pow_dtype_rule(x, *, y):
dtype = unop_dtype_rule(_identity, _int | _float | _complex, 'integer_pow', x)
if y < 0 and dtypes.issubdtype(dtype, np.integer):
raise TypeError("Integers cannot be raised to negative powers, got "
f"integer_pow({x}, {y})")
return dtype
def _integer_pow_jvp(g, x, *, y):
if y == 0:
return _zeros(g)
if y == 1:
return g
if y == 2:
return mul(g, mul(_const(x, y), x))
return mul(g, mul(_const(x, y), integer_pow(x, y - 1)))
integer_pow_p = standard_primitive(
_attrgetter('shape'), _integer_pow_dtype_rule, 'integer_pow',
sharding_rule=_attrgetter('sharding'), vma_rule=lambda x, **_: x.mat.varying)
batching.defvectorized(integer_pow_p)
ad.defjvp(integer_pow_p, _integer_pow_jvp)
View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Cast the base to float first: x.astype(jnp.float32) ** -1
- Use explicit reciprocals: 1.0 / x (which promotes) or jnp.reciprocal(x.astype(float))
- Keep integer exponents non-negative for integer bases
Example fix
// before inv = int_array ** -1 // after inv = int_array.astype(jnp.float32) ** -1 # or: inv = 1.0 / int_array
Defensive patterns
Strategy: validation
Validate before calling
if y < 0 and jnp.issubdtype(x.dtype, jnp.integer):
x = x.astype(jnp.float32)
out = x ** y Type guard
def safe_integer_pow(x, y) -> bool:
import numpy as np
return not (y < 0 and np.issubdtype(x.dtype, np.integer)) Prevention
- Write 1.0/x instead of x**-1
- Cast integer tensors to float before reciprocal-style math
When it happens
Trigger: x ** -1 or x ** -2 on integer arrays (which lower to integer_pow), e.g. jnp.asarray(2) ** -1, or int32 tensors raised to -1 for reciprocal computation.
Common situations: Writing 1/x as x**-1 on integer tensors; reciprocal/inverse computations on counts or indices; exponents computed as -k where k positive on int inputs.
Related errors
- full must be called with scalar fill_value, got fill_value.s
- offset must be an integer, got {offset!r}
- {} does not accept dtype {}. Accepted dtypes are subtypes of
- {} does not accept dtype {} at position {}. Accepted dtypes
- the first argument to pow must have an inexact dtype (float
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/a43179455f5df75f.
Report an issue: GitHub.