jax-ml/jax · error · ValueError
`next_power_of_2` requires a non-negative integer.
Error message
`next_power_of_2` requires a non-negative integer.
What it means
pallas.utils.next_power_of_2(x) computes the smallest power of two >= x and only accepts non-negative integers. A negative input raises ValueError because bit_length-based computation is undefined for negatives.
Source
Thrown at jax/_src/pallas/utils.py:107
>>> align_to(5, 4) # 5 is not a multiple of 4, so rounds up to 8
8
"""
return cdiv(a, alignment) * alignment
def strides_from_shape(shape: tuple[int, ...]) -> tuple[int, ...]:
size = np.prod(shape)
strides = []
for s in shape:
size = size // s
strides.append(int(size))
return tuple(strides)
def next_power_of_2(x: int) -> int:
"""Returns the next power of two greater than or equal to `x`."""
if x < 0:
raise ValueError("`next_power_of_2` requires a non-negative integer.")
return 1 if x == 0 else 2 ** (x - 1).bit_length()
def pattern_match_scan_to_fori_loop(
jaxpr: jax_core.Jaxpr, num_consts: int, num_carry: int
) -> tuple[jax_core.Jaxpr, bool]:
num_extensive_inputs = len(jaxpr.invars) - num_consts - num_carry
num_extensive_outputs = len(jaxpr.outvars) - num_carry
if num_extensive_outputs:
raise ValueError(
f"Scan with {num_extensive_outputs} extensive output(s) is not"
" supported."
)
if num_extensive_inputs:
raise ValueError(
f"Scan with {num_extensive_inputs} extensive argument(s) is not"
f" supported. Found {num_consts} consts and {num_carry} carry"
" arguments."View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Fix the upstream computation so the argument is non-negative
- Guard with max(0, x) if negatives are legitimately possible
- Validate block sizes with an assert before calling
Example fix
# before next_power_of_2(n - block_size) # after next_power_of_2(max(0, n - block_size))
Defensive patterns
Strategy: validation
Validate before calling
assert isinstance(x, int) and x >= 0, 'next_power_of_2 needs a non-negative int'
Prevention
- Clamp computed sizes: max(0, n)
- Assert block-size formulas for small inputs
When it happens
Trigger: Calling next_power_of_2 with a negative number, e.g. next_power_of_2(-8); typically the negative comes from a computed block size or shape expression (e.g. padding - kernel).
Common situations: Kernel grid/block-size math producing negative values when problem dimensions are smaller than assumed; accidental sign error in a size formula.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Sum of sizes {n} must be equal to dimension {axis} of the op
- Cannot pull iota along dimension {dimension} with None block
- Invalid memory space: {memory_space!r}
- dimension_semantics must be the same length as grid.
- Grid must consist of Python integers and JAX Arrays: {grid_t
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/8cc102d3e90f2121.
Report an issue: GitHub.