Skip to content
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

Support cupy in as_shared_dtype #4232

Merged
merged 7 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from . import dask_array_compat, dask_array_ops, dtypes, npcompat, nputils
from .nputils import nanfirst, nanlast
from .pycompat import dask_array_type
from .pycompat import cupy_array_type, dask_array_type

try:
import dask.array as dask_array
Expand Down Expand Up @@ -158,17 +158,23 @@ def trapz(y, x, axis):
)


def asarray(data):
def asarray(data, xp=np):
return (
data
if (isinstance(data, dask_array_type) or hasattr(data, "__array_function__"))
else np.asarray(data)
else xp.asarray(data)
dcherian marked this conversation as resolved.
Show resolved Hide resolved
)


def as_shared_dtype(scalars_or_arrays):
"""Cast a arrays to a shared dtype using xarray's type promotion rules."""
arrays = [asarray(x) for x in scalars_or_arrays]

if any([isinstance(x, cupy_array_type) for x in scalars_or_arrays]):
import cupy as cp

arrays = [asarray(x, xp=cp) for x in scalars_or_arrays]
else:
arrays = [asarray(x) for x in scalars_or_arrays]
# Pass arrays directly instead of dtypes to result_type so scalars
# get handled properly.
# Note that result_type() safely gets the dtype from dask arrays without
Expand Down
8 changes: 8 additions & 0 deletions xarray/core/pycompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@
sparse_array_type = (sparse.SparseArray,)
except ImportError: # pragma: no cover
sparse_array_type = ()

try:
# solely for isinstance checks
import cupy

cupy_array_type = (cupy.core.core.ndarray,)
jacobtomlinson marked this conversation as resolved.
Show resolved Hide resolved
except ImportError: # pragma: no cover
cupy_array_type = ()
12 changes: 7 additions & 5 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
from .npcompat import IS_NEP18_ACTIVE
from .options import _get_keep_attrs
from .pycompat import dask_array_type, integer_types
from .pycompat import cupy_array_type, dask_array_type, integer_types
from .utils import (
OrderedSet,
_default,
Expand All @@ -45,9 +45,8 @@
)

NON_NUMPY_SUPPORTED_ARRAY_TYPES = (
indexing.ExplicitlyIndexed,
pd.Index,
) + dask_array_type
(indexing.ExplicitlyIndexed, pd.Index,) + dask_array_type + cupy_array_type
)
# https://github.com/python/mypy/issues/224
BASIC_INDEXING_TYPES = integer_types + (slice,) # type: ignore

Expand Down Expand Up @@ -257,7 +256,10 @@ def _as_array_or_item(data):

TODO: remove this (replace with np.asarray) once these issues are fixed
"""
data = np.asarray(data)
if isinstance(data, cupy_array_type):
data = data.get()
else:
data = np.asarray(data)
dcherian marked this conversation as resolved.
Show resolved Hide resolved
if data.ndim == 0:
if data.dtype.kind == "M":
data = np.datetime64(data, "ns")
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ def test_check_data_stays_on_gpu(toy_weather_data):
"""Perform some operations and check the data stays on the GPU."""
freeze = (toy_weather_data["tmin"] <= 0).groupby("time.month").mean("time")
assert isinstance(freeze.data, cp.core.core.ndarray)


def test_where():
from xarray.core.duck_array_ops import where

data = cp.zeros(10)

assert where(data < 1, 1, data).all()
jacobtomlinson marked this conversation as resolved.
Show resolved Hide resolved