jax-ml/jax · error · ValueError

Invalid order '{ord}' for vector norm.

Error message

Invalid order '{ord}' for vector norm.

What it means

jnp.linalg.vector_norm accepts numeric or the sentinel values jnp.inf/-jnp.inf for ord, but not the strings 'inf' or '-inf' (unlike NumPy, where numpy.inf is a float). Any other unrecognized string ord also triggers it. JAX deliberately rejects string spellings because its ord parameter is typed as int | str | float but only supports specific named orders ('fro', 'nuc' for matrix norm contexts) and numeric/inf values.

Source

Thrown at jax/_src/numpy/linalg.py:1794

    return reductions.amax(ufuncs.abs(x), axis=axis, keepdims=keepdims, initial=0)
  elif ord == -np.inf:
    return reductions.amin(ufuncs.abs(x), axis=axis, keepdims=keepdims)
  elif ord == 0:
    return reductions.sum(x != 0, dtype=jnp.finfo(lax.dtype(x)).dtype,
                          axis=axis, keepdims=keepdims)
  elif ord == 1:
    # Numpy has a special case for ord == 1 as an optimization. We don't
    # really need the optimization (XLA could do it for us), but the Numpy
    # code has slightly different type promotion semantics, so we need a
    # special case too.
    return reductions.sum(ufuncs.abs(x), axis=axis, keepdims=keepdims)
  elif isinstance(ord, str):
    msg = f"Invalid order '{ord}' for vector norm."
    if ord == "inf":
      msg += "Use 'jax.numpy.inf' instead."
    if ord == "-inf":
      msg += "Use '-jax.numpy.inf' instead."
    raise ValueError(msg)
  else:
    abs_x = ufuncs.abs(x)
    ord_arr = lax._const(abs_x, ord)
    ord_inv = lax._const(abs_x, 1. / ord_arr)
    out = reductions.sum(abs_x ** ord_arr, axis=axis, keepdims=keepdims)
    return ufuncs.power(out, ord_inv)

@export
def vecdot(x1: ArrayLike, x2: ArrayLike, /, *, axis: int = -1,
           precision: lax.PrecisionLike = None,
           preferred_element_type: DTypeLike | None = None) -> Array:
  """Compute the (batched) vector conjugate dot product of two arrays.

  JAX implementation of :func:`numpy.linalg.vecdot`.

  Args:
    x1: left-hand side array.
    x2: right-hand side array. Size of ``x2[axis]`` must match size of ``x1[axis]``,

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Replace ord='inf' with ord=jnp.inf and ord='-inf' with ord=-jnp.inf (as the error message itself instructs).
  2. If ord comes from config/CLI, convert it: ord = float(ord_str) so 'inf' becomes a real infinity float.
  3. For L1/L2 use ord=1 / ord=2 (numeric).

Example fix

// before
n = jnp.linalg.vector_norm(x, ord='inf')
// after
n = jnp.linalg.vector_norm(x, ord=float('inf'))
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(ord, str):
    ord = float(ord)  # 'inf' -> inf; raises for genuinely bad names
n = jnp.linalg.vector_norm(x, ord=ord)

Type guard

def valid_ord(o) -> bool:
    return not isinstance(o, str)

Try / catch

try:
    n = jnp.linalg.vector_norm(x, ord=ord)
except ValueError as e:
    if 'Invalid order' in str(e):
        n = jnp.linalg.vector_norm(x, ord=float(ord))
    else:
        raise

Prevention

When it happens

Trigger: Calling jnp.linalg.vector_norm(x, ord='inf') or ord='-inf'; passing an unsupported string like ord='l2' or ord=1.5-norm names.

Common situations: Porting NumPy/SciPy code that used ord=float('inf') or np.inf where the value got stringified (e.g. read from a config/CLI arg as 'inf'); copy-pasting sklearn-style string norm names into JAX code.

Related errors


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