Skip to content

BUG Handle "auxiliary" array-like arguments paseed to metrics when array API is enabled #33519

Open

Description

@betatim

This a thing I discovered as part of #33515

The way we handle multioutput=[0.3,0.5] (aka a list or a numpy array) needs some thinking.

The below snippet should work but doesn't:

import numpy as np
import torch
from sklearn import config_context
from sklearn.metrics import explained_variance_score

y_true = np.array([[0.5, 1], [-1, 1], [7, -6]])
y_pred_np = np.array([[0, 2], [-1, 2], [8, -5]])
y_pred_torch = torch.asarray(y_pred_np, dtype=torch.float64)

# Works: string multioutput (the default, also most common use-case?)
with config_context(array_api_dispatch=True):
    print(explained_variance_score(y_true, y_pred_torch, multioutput="uniform_average"))

# multioutput as list
# `check_array` converts it to numpy, then `_average` sees mismatched devices
with config_context(array_api_dispatch=True):
    print(explained_variance_score(y_true, y_pred_torch, multioutput=[0.3, 0.7]))

# Same issue with a numpy array
with config_context(array_api_dispatch=True):
    print(explained_variance_score(y_true, y_pred_torch, multioutput=np.array([0.3, 0.7])))

I think the right thing is to move multioutput the same way we move y_true to match y_pred.

A related thing: the exception you get from the above code is ValueError: Input arrays use different devices: cpu, cpu - I think this is maximally confusing :D The error I was expecting to see was about namespaces mismatching. The reason for this is that get_namespace_and_device checks device mismatch before it checks namespace mismatch. So I think in total we have three things to work on:

  1. make the above snippet work by moving multioutput so it follows y_pred
  2. change the order in get_namespace_and_device to check namespaces first
  3. improve the error message for device mismatch so that you can see that these two "CPU" devices are from different namespaces

xref #31274

Activity

  1. betatim commented on Mar 11, 2026

    @betatim
    MemberAuthor

    While debugging this I noticed that we pass in xp to get_namespace_and_device. In this case xp=torch and because we pass it in we do not check that all the arrays in array_list are from that namespace. So even after moving the check for which device is used later in the function we don't get an error about mismatching namespaces.

    The main motivation for passing in xp is to avoid having to determine it again (it is an optimisation). The downside is that we don't get an error message about mismatches. I'm wondering what we should do here. Somehow using the optimisation makes sense, but maybe we should only use it in places where we are sure that we can't get mismatches?

    In the end I think the bug is not using move_to earlier, but in the case of a bug (like now) the error message is confusing (tells you about mismatching devices) and debugging is a bit confusing (you need to realise that xp was passed in).

  2. moved this to Todo in Array APIon Mar 19, 2026
  3. virchan commented on Mar 20, 2026

    @virchan
    Member

    Commenting to stay updated.

  4. lucyleeow commented on Mar 20, 2026

    @lucyleeow
    Member

    Commenting to stay updated.

    You can click the 'subscribe' button on the right side - under 'notifications'. (It is also handy if you want to unsubscribe from an issue/PR 😅 )

  5. betatim commented on Mar 20, 2026

    @betatim
    MemberAuthor

    #33515 (comment) is relevant (I think)

  6. Mahajan-Sachin commented on Apr 28, 2026

    @Mahajan-Sachin

    Hi, I've investigated this and have a fix ready.

    Root cause: In _check_reg_targets, check_array(multioutput) converts
    the weights to NumPy, but y_pred is already in the torch namespace. When
    _average() uses both, it sees mismatched namespaces (torch-cpu vs numpy-cpu)
    and raises the confusing cpu, cpu error.

    Fix: After check_array, call move_to(multioutput, xp=xp, device=device)
    to move the weights into the same namespace as y_pred.

    before

    Image ## after Image
  7. lucyleeow commented on Apr 28, 2026

    @lucyleeow
    Member

    I think this will be closed by #33778

    Edit: but I should add a common metric test for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions