You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG Handle "auxiliary" array-like arguments paseed to metrics when array API is enabled #33519
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:
importnumpyasnpimporttorchfromsklearnimportconfig_contextfromsklearn.metricsimportexplained_variance_scorey_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?)withconfig_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 deviceswithconfig_context(array_api_dispatch=True):
print(explained_variance_score(y_true, y_pred_torch, multioutput=[0.3, 0.7]))
# Same issue with a numpy arraywithconfig_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:
make the above snippet work by moving multioutput so it follows y_pred
change the order in get_namespace_and_device to check namespaces first
improve the error message for device mismatch so that you can see that these two "CPU" devices are from different namespaces
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).
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.
Activity
betatim commentedon Mar 11, 2026
While debugging this I noticed that we pass in
xptoget_namespace_and_device. In this casexp=torchand because we pass it in we do not check that all the arrays inarray_listare 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
xpis 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_toearlier, 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 thatxpwas passed in).virchan commentedon Mar 20, 2026
Commenting to stay updated.
lucyleeow commentedon Mar 20, 2026
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 😅 )
betatim commentedon Mar 20, 2026
#33515 (comment) is relevant (I think)
MAINT fix ruff formatting and add changelog entry for scikit-learn#33519
Mahajan-Sachin commentedon Apr 28, 2026
Hi, I've investigated this and have a fix ready.
Root cause: In
_check_reg_targets,check_array(multioutput)convertsthe weights to NumPy, but
y_predis already in the torch namespace. When_average()uses both, it sees mismatched namespaces (torch-cpu vs numpy-cpu)and raises the confusing
cpu, cpuerror.Fix: After
check_array, callmove_to(multioutput, xp=xp, device=device)to move the weights into the same namespace as
y_pred.before
lucyleeow commentedon Apr 28, 2026
I think this will be closed by #33778
Edit: but I should add a common metric test for this