Repository: scikit-learn/scikit-learn
File: sklearn/utils/tests/test_array_api.py (Python)

Code before:
        device_torch = xp_torch.asarray([1], device="cuda").device

        array_cupy = xp_cupy.asarray([1, 2, 3], device=None)

        (array_out,) = move_to(array_cupy, xp=xp_torch, device=device_torch)
        assert get_namespace(array_out)[0] == xp_torch
        assert device(array_out) == device_torch


def test_move_to_array_api_conversions_numpy_to_torch_cuda():
    """Check conversion of Numpy to torch."""
    xp_torch = _array_api_for_tests("torch", "cuda")

    with config_context(array_api_dispatch=True):
        device_torch = xp_torch.asarray([1], device="cuda").device

        array_np = numpy.asarray([1, 2, 3], device=None)

        (array_out,) = move_to(array_np, xp=xp_torch, device=device_torch)

        assert get_namespace(array_out)[0] == xp_torch
        assert device(array_out) == device_torch


def test_move_to_array_api_conversions_numpy_to_torch_mps():
    """Check conversion of Numpy to torch mps."""
    xp_torch = _array_api_for_tests("torch", "mps")

    with config_context(array_api_dispatch=True):
        device_torch = xp_torch.asarray([1], device="mps").device

        array_np = numpy.asarray([1, 2, 3], device=None)

        (array_out,) = move_to(array_np, xp=xp_torch, device=device_torch)

        assert get_namespace(array_out)[0] == xp_torch
        assert device(array_out) == device_torch


def test_move_to_array_api_conversions_strict_tor_torch():
    """Check conversion of array-api-strict CPU_DEVICE to torch."""
    try:
        import array_api_strict

        array_api_strict_available = True
    except ImportError:
        array_api_strict_available = False

    if not array_api_strict_available:
        pytest.mark.skip(reason="array-api-strict not available")


Review comment (suggestion):
```suggestion
    """Check conversion of NumPy to torch mps."""
```

Code after:
        pytest.param(("numpy", None), ("torch", "mps"), id="numpy to torch mps"),
        pytest.param(
            ("array_api_strict", None),
            ("torch", "mps"),
            id="array_api_strict to torch mps",
        ),
    ],
)
def test_move_to_array_api_conversions(array_input, reference):
    """Check conversion between various namespace and devices."""
    if array_input[0] == "array_api_strict":
        array_api_strict = pytest.importorskip(
            "array_api_strict", reason="array-api-strict not available"
        )
    xp = _array_api_for_tests(reference[0], reference[1])
    xp_array = _array_api_for_tests(array_input[0], array_input[1])

    with config_context(array_api_dispatch=True):
        device_ = device(xp.asarray([1], device=reference[1]))

        if array_input[0] == "array_api_strict":
            array_device = array_api_strict.Device("CPU_DEVICE")
        else:
            array_device = array_input[1]
        array = xp_array.asarray([1, 2, 3], device=array_device)

        array_out = move_to(array, xp=xp, device=device_)
        assert get_namespace(array_out)[0] == xp
        assert device(array_out) == device_


def test_move_to_sparse():
    """Check sparse inputs are handled correctly."""
    xp_numpy = _array_api_for_tests("numpy", None)
    xp_torch = _array_api_for_tests("torch", "cpu")

    sparse1 = sp.csr_array([0, 1, 2, 3])
    sparse2 = sp.csr_array([0, 1, 0, 1])
    numpy_array = numpy.array([1, 2, 3])

    with config_context(array_api_dispatch=True):
        device_cpu = xp_torch.asarray([1]).device

        # sparse and None to NumPy
        result1, result2 = move_to(sparse1, None, xp=xp_numpy, device=None)
        assert result1 is sparse1
        assert result2 is None

        # sparse to non-NumPy
        msg = r"Sparse arrays are only accepted \(and passed through\)"
        with pytest.raises(TypeError, match=msg):