Repository navigation
DEBUG torch incompatibility with np.sum and np.repeat (pure debug PR, will be closed soon) - #33028
StefanieSenger wants to merge 15 commits into
Conversation
|
Looking at the CI pipeline output, it seems that The question I was asking is then, does the following give the same error on main: import torch
from sklearn import config_context
from sklearn.metrics import average_precision_score
y_scores = torch.tensor([0.1, 0.4, 0.35, 0.8], device='cpu')
y_true = torch.tensor([0, 0, 1, 1], device='cpu')
sw = torch.tensor([1, 2, 1, 1], device='cpu')
with config_context(array_api_dispatch=False):
average_precision_score(y_true, y_scores, sample_weight=sw)I tried locally and it does not error for me. Does it work for you @StefanieSenger ? If it works it means we've changed something in this PR, such that sample weight is no longer converted to numpy. |
|
Now I see.
It doesn't error for me either. Sorry, I didn't understand what "it" was referring to before you posted the code. It would have never occurred to me that feeding torch types into This one.scikit-learn/sklearn/metrics/tests/test_common.py Lines 2000 to 2048 in 4c7b08f Why is out-of-the-box support of array types on cpu an assumption we can make? Does it also hold on any other function or method without array api support?
True. I will investigate that. |
|
I understand the confusion now. I was also confused, see discussion in #32676. It seemed that others knew what was going on so I assumed it was just me 😅 |
That discussion is very enlightening. I didn't know that "passing a PyTorch cpu tensor has been working for ever" but was undocumented in the past. |
|
Actually, this test fails on main, too: import torch
from sklearn import config_context
from sklearn.metrics import average_precision_score
y_score = torch.tensor([[0.1, 0.4, 0.8], [0.1, 0.1, 0.0], [0.1, 0.4, 0.1], [0.7, 0.1, 0.1]], device='cpu')
y_true = torch.tensor([2, 0, 1, 1], device='cpu')
sw = torch.tensor([1, 2, 1, 1], device='cpu')
with config_context(array_api_dispatch=False):
average_precision_score(y_true, y_score, sample_weight=sw, average="weighted")In a binary case, we return via |
|
So if we assume that torch tensors on cpu had been supported as input on metrics before, this would have been a bug before, but since we didn't document this and nobody reported, maybe not a severe bug. In #32909, we could call |
AFAICT it seems only when
What do 'similar' metrics (with the |
Yes, and additionally
There is only |
|
Summary: I would add if sample_weight is not None:
sample_weight = column_or_1d(sample_weight)which prevents the error from happening on my branch. More detailed analysis: In one case (I think) it gets passed through a In the other cases, it just seems to work by coincidence: y_score = torch.tensor([[1, 0, 0], [0, 1, 1]], device='cpu')
y_true = torch.tensor([[1, 0, 0], [0, 1, 1]], device='cpu')
sw = torch.tensor([1, 2], device='cpu')
with config_context(array_api_dispatch=False):
coverage_error(y_true, y_score, sample_weight=sw) scikit-learn/sklearn/metrics/_ranking.py Line 1499 in 75bf0c6 gets a torch array for Similarly y_score = torch.tensor([[1, 0, 0], [0, 1, 1]], device='cpu')
y_true = torch.tensor([[1, 0, 0], [0, 1, 1]], device='cpu')
sw = torch.tensor([1, 2], device='cpu')
with config_context(array_api_dispatch=False):
ndcg_score(y_true, y_score, sample_weight=sw)
scikit-learn/sklearn/metrics/_ranking.py Line 2015 in 75bf0c6 gets passed a torch array and can again deal with it. There are 2-3 other places where (There is also a call to |
Interesting. (it seems that the tensor input just seems to work for some np functions but not others). I found some documentation about this here: https://numpy.org/doc/stable/user/basics.interoperability.html, in particular this section: https://numpy.org/devdocs/user/basics.interoperability.html#operating-on-foreign-objects-without-converting Unfortunately, later on in https://numpy.org/devdocs/user/basics.interoperability.html#example-pytorch-tensors it says:
But digging into the code, at least I found def _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs):
passkwargs = {k: v for k, v in kwargs.items()
if v is not np._NoValue}
if type(obj) is not mu.ndarray:
try:
reduction = getattr(obj, method)
except AttributeError:
pass
else:
# This branch is needed for reductions like any which don't
# support a dtype.
if dtype is not None:
return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)
else:
return reduction(axis=axis, out=out, **passkwargs)
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)which shows that if the object is not a numpy array, it tries to get the method from the array. I could not find any documentation of this behaviour, and the interoperability page above is a little misleading if anything. Vaguely useful, but also not clear, there is a list of numpy array methods that also have corresponding numpy functions: https://numpy.org/doc/1.21/reference/arrays.ndarray.html#array-methods - tl;dr - some numpy functions will also have a tensor method, and some of these will work and some of these won't, you won't know until you try? 🤷 FYI mentioned this to Olivier in the array API meeting and he simply suggested that we should improve tests for this but it is not a priority. |
|
Thanks for the deep dive, @lucyleeow!
That indeed sounds a bit chaotic. Though I think if users don't report a bug around it (this particular one must have existed for years) then it is maybe not a problem. Or the users know their way around by converting torch to numpy beforehand. Are you fine with this as a solution? if sample_weight is not None:
sample_weight = column_or_1d(sample_weight) |
Agreed. I meant to add, for the other metrics that 'just seem to work' (from _ranking.py I noted
Yes! Looking at just the metrics in _ranking.py, most of the ones that explicitly do something with |
|
Thanks for having a closer look at this @lucyleeow. I think we can now close it. The other metrics that might fail on not converted input types ( |
This is to debug a CI failure in #32909 and discuss solutions.
Specifically this discussion: https://github.com/scikit-learn/scikit-learn/pull/32909/changes#r2627102404
EDIT: It is a bug on main (#33028 (comment)) and does not only affect the mentioned PR.