jax-ml/jax · error · ValueError

Arguments to jax.numpy.lcm must be integers.

Error message

Arguments to jax.numpy.lcm must be integers.

What it means

jax.numpy.lcm computes the least common multiple and, like gcd, requires both promoted arguments to have an integer dtype. After ensure_arraylike and promote_dtypes (and abs), it validates issubdtype(x1.dtype, np.integer) and raises ValueError if the promoted dtype is not integral.

Source

Thrown at jax/_src/numpy/lax_numpy.py:8909

    Array inputs:

    >>> x1 = jnp.array([12, 18, 24])
    >>> x2 = jnp.array([5, 10, 15])
    >>> jnp.lcm(x1, x2)
    Array([ 60,  90, 120], dtype=int32)

    Broadcasting:

    >>> x1 = jnp.array([12])
    >>> x2 = jnp.array([6, 9, 12])
    >>> jnp.lcm(x1, x2)
    Array([12, 36, 12], dtype=int32)
  """
  x1, x2 = util.ensure_arraylike("lcm", x1, x2)
  x1, x2 = util.promote_dtypes(x1, x2)
  x1, x2 = ufuncs.abs(x1), ufuncs.abs(x2)
  if not issubdtype(x1.dtype, np.integer):
    raise ValueError("Arguments to jax.numpy.lcm must be integers.")
  d = gcd(x1, x2)
  return where(d == 0, lax._const(d, 0),
               ufuncs.multiply(x1, ufuncs.floor_divide(x2, d)))


@export
def extract(condition: ArrayLike, arr: ArrayLike,
            *, size: int | None = None, fill_value: ArrayLike = 0) -> Array:
  """Return the elements of an array that satisfy a condition.

  JAX implementation of :func:`numpy.extract`.

  Args:
    condition: array of conditions. Will be converted to boolean and flattened to 1D.
    arr: array of values to extract. Will be flattened to 1D.
    size: optional static size for output. Must be specified in order for ``extract``
      to be compatible with JAX transformations like :func:`~jax.jit` or :func:`~jax.vmap`.
    fill_value: if ``size`` is specified, fill padded entries with this value (default: 0).

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Cast both arguments to an integer dtype: jnp.lcm(x1.astype(int), x2.astype(int))
  2. Fix upstream code that produces float values where integers were intended
  3. Validate dtypes with jnp.issubdtype before calling

Example fix

// before
jnp.lcm(4.0, 6.0)  # ValueError
// after
jnp.lcm(jnp.array(4.0, dtype=jnp.int32), jnp.array(6.0, dtype=jnp.int32))
Defensive patterns

Strategy: validation

Validate before calling

x1 = jnp.asarray(x1).astype(jnp.int32)
x2 = jnp.asarray(x2).astype(jnp.int32)
jnp.lcm(x1, x2)

Type guard

def is_integer_array(x) -> bool:
    return jnp.issubdtype(jnp.asarray(x).dtype, jnp.integer)

Prevention

When it happens

Trigger: Calling jnp.lcm on float arrays or mixed int/float inputs, e.g. jnp.lcm(np.array([2.0]), np.array([3])) — promotion gives float64 and the check fails.

Common situations: Feeding results of float arithmetic or float-loaded datasets into lcm; assuming JAX auto-truncates floats like Python's math.lcm does not.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/598efed0be00ede8. Report an issue: GitHub.