From 9da97738e579dddacfc87e8d685b41e12e23de79 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Thu, 8 Feb 2024 17:15:33 -0700 Subject: [PATCH 1/2] Move _check_device helper to _aliases.py --- array_api_compat/common/_aliases.py | 8 +++++++- array_api_compat/common/_helpers.py | 5 ----- array_api_compat/dask/array/_aliases.py | 2 -- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/array_api_compat/common/_aliases.py b/array_api_compat/common/_aliases.py index 1eeb0594..dc683022 100644 --- a/array_api_compat/common/_aliases.py +++ b/array_api_compat/common/_aliases.py @@ -13,8 +13,14 @@ from typing import NamedTuple from types import ModuleType import inspect +import sys -from ._helpers import _check_device, is_numpy_array, array_namespace +from ._helpers import is_numpy_array, array_namespace + +def _check_device(xp, device): + if xp == sys.modules.get('numpy'): + if device not in ["cpu", None]: + raise ValueError(f"Unsupported device for NumPy: {device!r}") # These functions are modified from the NumPy versions. diff --git a/array_api_compat/common/_helpers.py b/array_api_compat/common/_helpers.py index 5e59c7ea..af045bdc 100644 --- a/array_api_compat/common/_helpers.py +++ b/array_api_compat/common/_helpers.py @@ -154,11 +154,6 @@ def your_function(x, y): # backwards compatibility alias get_namespace = array_namespace -def _check_device(xp, device): - if xp == sys.modules.get('numpy'): - if device not in ["cpu", None]: - raise ValueError(f"Unsupported device for NumPy: {device!r}") - # device() is not on numpy.ndarray and to_device() is not on numpy.ndarray # or cupy.ndarray. They are not included in array objects of this library # because this library just reuses the respective ndarray classes without diff --git a/array_api_compat/dask/array/_aliases.py b/array_api_compat/dask/array/_aliases.py index 14b27070..3bab15cf 100644 --- a/array_api_compat/dask/array/_aliases.py +++ b/array_api_compat/dask/array/_aliases.py @@ -7,7 +7,6 @@ from ..._internal import get_xp from ...common import _aliases, _linalg -from ...common._helpers import _check_device if TYPE_CHECKING: from typing import Optional, Tuple, Union @@ -38,7 +37,6 @@ def dask_arange( device: Optional[Device] = None, **kwargs, ) -> ndarray: - _check_device(xp, device) args = [start] if stop is not None: args.append(stop) From affb1d29f81893108c300422d38881d6726e7b33 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Thu, 8 Feb 2024 17:19:09 -0700 Subject: [PATCH 2/2] Deprecate passing "cpu" to to_device() for cupy arrays --- array_api_compat/common/_helpers.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/array_api_compat/common/_helpers.py b/array_api_compat/common/_helpers.py index af045bdc..9767a987 100644 --- a/array_api_compat/common/_helpers.py +++ b/array_api_compat/common/_helpers.py @@ -16,6 +16,7 @@ import sys import math import inspect +import warnings def is_numpy_array(x): # Avoid importing NumPy if it isn't already @@ -199,7 +200,15 @@ def _cupy_to_device(x, device, /, stream=None): elif device == "cpu": # allowing us to use `to_device(x, "cpu")` # is useful for portable test swapping between - # host and device backends + # host and device backends. This is deprecated. See + # https://github.com/data-apis/array-api-compat/issues/86. + # Implementations should prefer using the device keyword to + # from_dlpack() (starting from the 2023.12 version of the standard) + warnings.warn( + "Using `to_device(x, 'cpu')` on CuPy arrays is deprecated. Use the `device` keyword to `from_dlpack()` instead.", + DeprecationWarning, + stacklevel=3, + ) return x.get() elif not isinstance(device, _Device): raise ValueError(f"Unsupported device {device!r}")