Talip7/scikit-learn-issues-multilabel huggingface dataset:
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
From the traceback I also tried:

```python
>>> 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.

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
I think I get it. It's `get_namespace_and_device` that is wrong when array API dispatch is disabled. I will open a PR with a non-regression test.

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
@ogrisel is it possible that this bug has reappeared? I'm getting the same error again here:
https://github.com/AnswerDotAI/fastcore/actions/runs/12251169586/job/34175356676

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
@jph00, indeed it seems like a regression in scikit-learn 1.6. I am taking a closer look.

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
Reopening. Indeed, we made some changes in #29476  (including in the non-regression test named `test_get_namespace_and_device`) without realizing that it would break calls like `r2_score(torch.ones(3), torch.ones(3))` when array API dispatch is disabled (default). In this case want to rely on the regular `np.asarray` implicit conversion instead.

We need to fix it (either in the `sklearn.utils._array_api.device` function or in the `sklearn.utils._array_api.get_namespace_and_device` function. 

This time, add a non-regression test that directly calls `r2_score(torch.ones(3), torch.ones(3))` (or other metrics functions) with array API dispatch disabled and check that it works. The intent of the test will be clearer and we won't risk changing it without understanding the consequence...

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
@OmarManzoor just to confirm -- have you checked that your PR resolves this issue too?

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
Yes, we added a non-regression test so that hopefully this does not happen again.

I double-checked that the snippet from https://github.com/scikit-learn/scikit-learn/issues/29107#issuecomment-2133617301 works fine on `main`:
```py
In [1]: >>> 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))
Out[1]: -1.0773991838970147
```

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
For completeness, the plan is probably to do a scikit-learn 1.6.1 bug-fix release in January that includes the fix from #30454.

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
Was this done? also getting this error

Labels: Bug, Regression, Array API
Incorrect invalid device error introduced in #25956

### 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 is `device not in {"cpu", None}`, is incorrect -- the device will actually not be a string, but `device(type='cpu')`. Therefore, you should attempt to get the `type` 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

```
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/sklearn/utils/_array_api.py:308, in _check_device_cpu(device)
    306 def _check_device_cpu(device):  # noqa
    307     if device not in {"cpu", None}:
--> 308         raise ValueError(f"Unsupported device for NumPy: {device!r}")

ValueError: Unsupported device for NumPy: device(type='cpu')
```

### Versions

```shell
I've seen this on multiple Linux and Mac versions. My current Mac version:


System:
    python: 3.11.8 (main, Feb 26 2024, 15:36:12) [Clang 14.0.6 ]
executable: /Users/jhoward/miniconda3/bin/python
   machine: macOS-14.3.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 23.3.1
   setuptools: 68.2.2
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: 2.2.1
   matplotlib: 3.8.4
       joblib: 1.4.0
threadpoolctl: 2.2.0

Built with OpenMP: True

threadpoolctl info:
       filepath: /Users/jhoward/miniconda3/lib/libopenblasp-r0.3.21.dylib
         prefix: libopenblas
       user_api: blas
   internal_api: openblas
        version: 0.3.21
    num_threads: 8
threading_layer: pthreads
   architecture: armv8

       filepath: /Users/jhoward/miniconda3/lib/libomp.dylib
         prefix: libomp
       user_api: openmp
   internal_api: openmp
        version: None
    num_threads: 8
```
```

COMMENT:
@girwin1 check your scikit-learn version. This should be fixed in scikit-learn 1.6.1. If you are still getting an error with scikit-learn 1.6.1, please open a separate issue with a stand-alone snippet that reproduces the issue.

Labels: Bug, Regression, Array API