-
-
Notifications
You must be signed in to change notification settings - Fork 25.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect invalid device error introduced in #25956 #29107
Comments
Avoids incorrect `ValueError: Unsupported device for NumPy: device(type='cpu')` by using `type` attr if available.
Do you have a minimal reproducer. Looking at our code, I think that we expect to have a call to the I'm not the most familiar with the array API work so, ping @betatim and @ogrisel. |
Looking at the code, this function |
I tried to craft a minimal reproducer by mixing numpy and torch inputs as follows: >>> import numpy as np
>>> import array_api_compat.torch as xp
>>> import sklearn
>>> sklearn.set_config(array_api_dispatch=True)
>>> from sklearn.metrics import r2_score
>>> r2_score(xp.arange(10, device="mps"), xp.arange(10, device="mps"), sample_weight=xp.asarray(range(10), device="mps"))
1.0
>>> r2_score(np.arange(10), xp.arange(10, device="mps"), sample_weight=xp.arange(10, device="mps"))
Traceback (most recent call last):
Cell In[16], line 1
r2_score(np.arange(10), xp.arange(10, device="mps"), sample_weight=xp.arange(10, device="mps"))
File ~/code/scikit-learn/sklearn/utils/_param_validation.py:213 in wrapper
return func(*args, **kwargs)
File ~/code/scikit-learn/sklearn/metrics/_regression.py:1214 in r2_score
xp, _, device_ = get_namespace_and_device(
File ~/code/scikit-learn/sklearn/utils/_array_api.py:572 in get_namespace_and_device
*get_namespace(*array_list, **skip_remove_kwargs),
File ~/code/scikit-learn/sklearn/utils/_array_api.py:553 in get_namespace
namespace, is_array_api_compliant = array_api_compat.get_namespace(*arrays), True
File ~/miniforge3/envs/dev/lib/python3.11/site-packages/array_api_compat/common/_helpers.py:372 in array_namespace
raise TypeError(f"Multiple namespaces for array inputs: {namespaces}")
TypeError: Multiple namespaces for array inputs: {<module 'array_api_compat.numpy' from '/Users/ogrisel/miniforge3/envs/dev/lib/python3.11/site-packages/array_api_compat/numpy/__init__.py'>, <module 'array_api_compat.torch' from '/Users/ogrisel/miniforge3/envs/dev/lib/python3.11/site-packages/array_api_compat/torch/__init__.py'>} but it's raising the expected error message that states that |
From the traceback I also tried: >>> import numpy as np
>>> import sklearn
>>> sklearn.set_config(array_api_dispatch=True)
>>> from sklearn.metrics import r2_score
>>> import torch
>>> x1, x2 = torch.randn(20, 5), torch.randn(20, 5)
>>> r2_score(x2.view(-1), x1.view(-1))
-0.8340672254562378 but I cannot reproduce either. |
Ok I understand, it's happening when array API dispatch is disabled and regular numpy code is expected: >>> import numpy as np
>>> import sklearn
>>> sklearn.set_config(array_api_dispatch=False)
>>> from sklearn.metrics import r2_score
>>> import torch
>>> x1, x2 = torch.randn(20, 5), torch.randn(20, 5)
>>> r2_score(x2.view(-1), x1.view(-1))
Traceback (most recent call last):
Cell In[27], line 7
r2_score(x2.view(-1), x1.view(-1))
File ~/code/scikit-learn/sklearn/utils/_param_validation.py:213 in wrapper
return func(*args, **kwargs)
File ~/code/scikit-learn/sklearn/metrics/_regression.py:1242 in r2_score
return _assemble_r2_explained_variance(
File ~/code/scikit-learn/sklearn/metrics/_regression.py:897 in _assemble_r2_explained_variance
output_scores = xp.ones([n_outputs], device=device, dtype=dtype)
File ~/code/scikit-learn/sklearn/utils/_array_api.py:314 in wrapped_func
_check_device_cpu(kwargs.pop("device", None))
File ~/code/scikit-learn/sklearn/utils/_array_api.py:308 in _check_device_cpu
raise ValueError(f"Unsupported device for NumPy: {device!r}")
ValueError: Unsupported device for NumPy: device(type='cpu') so this is indeed a regression: when array API dispatch is disabled, the usual implicit conversion from torch CPU tensors to NumPy arrays should still happen and for some reason, we get a non-None device where we don't expect it. I need to investigate. |
I think I get it. It's |
@ogrisel is it possible that this bug has reappeared? I'm getting the same error again here: |
@jph00, indeed it seems like a regression in scikit-learn 1.6. I am taking a closer look. |
Reopening. Indeed, we made some changes in #29476 (including in the non-regression test named We need to fix it (either in the This time, add a non-regression test that directly calls |
Describe the bug
#25956 introduced a new
sklearn.utils._array_api._check_device_cpu
function to test whether a tensor is on CPU. However, the implementation of the test, which isdevice not in {"cpu", None}
, is incorrect -- the device will actually not be a string, butdevice(type='cpu')
. Therefore, you should attempt to get thetype
attr, and use that if available.Steps/Code to Reproduce
You can view a sample error here:
https://github.com/fastai/fastai/actions/runs/9232979440/job/25404873935
Expected Results
ValueError: Unsupported device for NumPy: device(type='cpu')
should not be thrown.Actual Results
Versions
The text was updated successfully, but these errors were encountered: