jax-ml/jax · error · ValueError
jax.numpy.var does not yet support real dtype parameters whe
Error message
jax.numpy.var does not yet support real dtype parameters when computing the variance of an array of complex values. The semantics of numpy.var seem unclear in this case. Please comment on https://github.com/jax-ml/jax/issues/2283 if this behavior is important to you.
What it means
jax.numpy.var (and nanvar) explicitly refuses to compute the variance of a complex-valued array when the dtype parameter is a real (non-complex) dtype. Because NumPy's own semantics for this case are ambiguous, JAX raises a ValueError and points to GitHub issue #2283 rather than guessing.
Source
Thrown at jax/_src/numpy/reductions.py:1175
normalizer = lax.sub(normalizer, lax.convert_element_type(correction, computation_dtype))
result = sum(centered, axis, dtype=computation_dtype, keepdims=keepdims, where=where)
result = lax.div(result, normalizer).astype(dtype)
with config.debug_nans(False):
result = _where(normalizer > 0, result, np.nan)
return result
def _var_promote_types(a_dtype: DTypeLike, dtype: DTypeLike | None) -> tuple[DType, DType]:
if dtype:
if (not dtypes.issubdtype(dtype, np.complexfloating) and
dtypes.issubdtype(a_dtype, np.complexfloating)):
msg = ("jax.numpy.var does not yet support real dtype parameters when "
"computing the variance of an array of complex values. The "
"semantics of numpy.var seem unclear in this case. Please comment "
"on https://github.com/jax-ml/jax/issues/2283 if this behavior is "
"important to you.")
raise ValueError(msg)
computation_dtype = dtype
else:
if not dtypes.issubdtype(a_dtype, np.inexact):
dtype = dtypes.to_inexact_dtype(a_dtype)
computation_dtype = dtype
else:
dtype = np.array(0, a_dtype).real.dtype
computation_dtype = a_dtype
return _upcast_f16(computation_dtype), np.dtype(dtype)
@export
def std(a: ArrayLike, axis: Axis = None, dtype: DTypeLike | None = None,
out: None = None, ddof: int = 0, keepdims: bool = False, *,
where: ArrayLike | None = None, mean: ArrayLike | None = None,
correction: int | float | None = None) -> Array:
r"""Compute the standard deviation along a given axis.
View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Pass a complex dtype (e.g. dtype=jnp.complex64) or omit the dtype argument so JAX chooses it
- Compute variance on the magnitude/real projection: jnp.var(jnp.abs(x)) or jnp.var(x.real)
- If you need NumPy parity, follow up on the linked issue jax-ml/jax#2283
Example fix
// before var = jnp.var(complex_spec, dtype=jnp.float32) // after var = jnp.var(jnp.abs(complex_spec), dtype=jnp.float32)
Defensive patterns
Strategy: validation
Validate before calling
import jax.numpy as jnp, numpy as np
def safe_var(x, dtype=None):
if np.issubdtype(x.dtype, np.complexfloating) and dtype is not None and not np.issubdtype(dtype, np.complexfloating):
x = jnp.abs(x) # or require complex dtype
return jnp.var(x, dtype=dtype) Type guard
def is_complex_real_dtype_mismatch(x, dtype) -> bool:
import numpy as np
return np.issubdtype(x.dtype, np.complexfloating) and dtype is not None and not np.issubdtype(dtype, np.complexfloating) Try / catch
try:
v = jnp.var(x, dtype=dtype)
except ValueError as e:
if 'complex' in str(e):
v = jnp.var(jnp.abs(x), dtype=dtype)
else:
raise Prevention
- Never pass explicit real dtypes to variance of complex arrays
- Centralize dtype policy in one helper for complex pipelines
- Take jnp.abs of spectra before real-valued statistics
When it happens
Trigger: Calling jnp.var(x, dtype=jnp.float32) or jnp.nanvar(x, dtype=jnp.float64) where x has a complex dtype (e.g. complex64); also any dtype that is real while a.dtype is complexfloating inside _var_promote_types.
Common situations: Porting NumPy signal-processing or FFT-magnitude code that computes variance of complex spectra while explicitly passing a float dtype; reusing a dtype derived from a real array on complex data.
Related errors
- Unsupported scalar attribute type: {type(val)}
- top_k is not compatible with complex inputs.
- `a` array must be integer typed
- Weights cannot be complex types.
- dtype parameter is not supported by Buffer.__array__.
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/9abae1c611e6eed0.
Report an issue: GitHub.