jax-ml/jax · error · RuntimeError

cannot handle multidimensional aweights

Error message

cannot handle multidimensional aweights

What it means

Observation weights (aweights) in jnp.cov must be a 1D array with one weight per observation. If np.ndim(aweights) > 1, RuntimeError('cannot handle multidimensional aweights') is raised.

Source

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

  if ddof is None:
    ddof = 1 if bias == 0 else 0

  w: Array | None = None
  if fweights is not None:
    fweights = util.ensure_arraylike("cov", fweights)
    if np.ndim(fweights) > 1:
      raise RuntimeError("cannot handle multidimensional fweights")
    if np.shape(fweights)[0] != X.shape[1]:
      raise RuntimeError("incompatible numbers of samples and fweights")
    if not issubdtype(fweights.dtype, np.integer):
      raise TypeError("fweights must be integer.")
    # Ensure positive fweights; note that numpy raises an error on negative fweights.
    w = abs(fweights)
  if aweights is not None:
    aweights = util.ensure_arraylike("cov", aweights)
    if np.ndim(aweights) > 1:
      raise RuntimeError("cannot handle multidimensional aweights")
    if np.shape(aweights)[0] != X.shape[1]:
      raise RuntimeError("incompatible numbers of samples and aweights")
    # Ensure positive aweights: note that numpy raises an error for negative aweights.
    aweights = abs(aweights)
    w = asarray(aweights if w is None else w * aweights)

  if dtype is not None:
    X = X.astype(dtype)
    w = w.astype(dtype) if w is not None else w

  avg, w_sum = reductions.average(X, axis=1, weights=w, returned=True)
  w_sum = w_sum[0]

  if w is None:
    f = X.shape[1] - ddof
  elif ddof == 0:
    f = w_sum
  elif aweights is None:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Flatten: aweights=W.ravel()
  2. Check aweights.ndim == 1 and length equals observation count before calling
  3. Recompute weights as a 1D per-observation vector

Example fix

// before
jnp.cov(m, aweights=w_2d)
// after
jnp.cov(m, aweights=w_2d.ravel())
Defensive patterns

Strategy: validation

Validate before calling

if aweights is not None:
    aweights = jnp.asarray(aweights).ravel()
jnp.cov(m, aweights=aweights)

Prevention

When it happens

Trigger: jnp.cov(m, aweights=W) where W is 2D, e.g. a full weight matrix or a (n, 1) array from slicing with keepdims.

Common situations: Passing a covariance weighting matrix instead of per-observation weights; weights retaining extra axes after reductions.

Related errors


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