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

Code before:
        monkeypatch.setattr("os.environ.get", mock_getenv)
        assert os.environ.get("SCIPY_ARRAY_API") != "1"
        with pytest.raises(
            RuntimeError,
            match="scipy's own support is not enabled.",
        ):
            get_namespace(X_xp)


def test_move_to_array_api_conversions_cupy_to_torch():
    """Check conversion of Cupy to torch."""
    xp_torch = _array_api_for_tests("torch", "cuda")
    xp_cupy = _array_api_for_tests("cupy", None)

    with config_context(array_api_dispatch=True):
        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

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

Code after:
        monkeypatch.setattr("os.environ.get", mock_getenv)
        assert os.environ.get("SCIPY_ARRAY_API") != "1"
        with pytest.raises(
            RuntimeError,
            match="scipy's own support is not enabled.",
        ):
            get_namespace(X_xp)


@pytest.mark.parametrize(
    "array_input, reference",
    [
        pytest.param(("cupy", None), ("torch", "cuda"), id="cupy to torch cuda"),
        pytest.param(("torch", "mps"), ("numpy", None), id="torch mps to numpy"),
        pytest.param(("numpy", None), ("torch", "cuda"), id="numpy to torch cuda"),
        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")