Skip to content

Fix dask #89

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

Merged
merged 12 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# macOS specific iles
.DS_Store
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ part of the specification but which are useful for using the array API:
[`x.device`](https://data-apis.org/array-api/latest/API_specification/generated/signatures.array_object.array.device.html)
in the array API specification. Included because `numpy.ndarray` does not
include the `device` attribute and this library does not wrap or extend the
array object. Note that for NumPy, `device(x)` is always `"cpu"`.
array object. Note that for NumPy and dask, `device(x)` is always `"cpu"`.

- `to_device(x, device, /, *, stream=None)`: Equivalent to
[`x.to_device`](https://data-apis.org/array-api/latest/API_specification/generated/signatures.array_object.array.to_device.html).
Included because neither NumPy's, CuPy's, nor PyTorch's array objects
Included because neither NumPy's, CuPy's, Dask's, nor PyTorch's array objects
include this method. For NumPy, this function effectively does nothing since
the only supported device is the CPU, but for CuPy, this method supports
CuPy CUDA
Expand Down Expand Up @@ -240,6 +240,30 @@ Unlike the other libraries supported here, JAX array API support is contained
entirely in the JAX library. The JAX array API support is tracked at
https://github.com/google/jax/issues/18353.

## Dask

If you're using dask with numpy, many of the same limitations that apply to numpy
will also apply to dask. Besides those differences, other limitations include missing
sort functionality (no `sort` or `argsort`), and limited support for the optional `linalg`
and `fft` extensions.

In particular, the `fft` namespace is not compliant with the array API spec. Any functions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, is the plan to implement array_api_compat.dask.{fft, linalg} or wait for support from dask itself? A similar question w.r.t JAX.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't attempted to wrap fft yet - waiting on #78 to do so.

Linalg can only be partially supported by us since there's missing methods in dask.

that you find under the `fft` namespace are the original, unwrapped functions under [`dask.array.fft`](https://docs.dask.org/en/latest/array-api.html#fast-fourier-transforms), which may or may not be Array API compliant. Use at your own risk!

For `linalg`, several methods are missing, for example:
- `cross`
- `det`
- `eigh`
- `eigvalsh`
- `matrix_power`
- `pinv`
- `slogdet`
- `matrix_norm`
- `matrix_rank`
Other methods may only be partially implemented or return incorrect results at times.

The minimum supported Dask version is 2023.12.0.

## Vendoring

This library supports vendoring as an installation method. To vendor the
Expand Down
4 changes: 3 additions & 1 deletion array_api_compat/common/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def device(x: Array, /) -> Device:
out: device
a ``device`` object (see the "Device Support" section of the array API specification).
"""
if is_numpy_array(x):
if is_numpy_array(x) or is_dask_array(x):
# TODO: dask technically can support GPU arrays
# Detecting the array backend isn't easy for dask, though, so just return CPU for now
return "cpu"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I noted on the other PR, it would probably be better to use some kind of basic DaskDevice object here instead of the string "cpu", given that CPU isn't necessarily an accurate description of the device dask is running on. See https://github.com/data-apis/array-api-strict/blob/main/array_api_strict/_array_object.py#L43-L49 for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I return cpu now only if the type of the array backing the dask array is a ndarray.

The rest of the time, I return a DaskDevice.

Is this something close to what you wanted?

(We might be able to do this for cupy, but it's tricky for e.g. multigpu cases I guess)

if is_jax_array(x):
# JAX has .device() as a method, but it is being deprecated so that it
Expand Down
6 changes: 3 additions & 3 deletions array_api_compat/dask/array/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from dask.array import (
arctanh as atanh,
)
from dask.array import (
from numpy import (
bool_ as bool,
)
from dask.array import (
Expand Down Expand Up @@ -67,15 +67,15 @@
uint64,
)

from ..common._helpers import (
from ...common._helpers import (
array_namespace,
device,
get_namespace,
is_array_api_obj,
size,
to_device,
)
from ..internal import _get_all_public_members
from ..._internal import _get_all_public_members
from ._aliases import (
UniqueAllResult,
UniqueCountsResult,
Expand Down
2 changes: 1 addition & 1 deletion array_api_compat/dask/array/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)
from dask.array.linalg import * # noqa: F401, F403

from .._internal import _get_all_public_members
from ..._internal import _get_all_public_members
from ._aliases import (
EighResult,
QRResult,
Expand Down
3 changes: 0 additions & 3 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ def test_is_xp_array(library, func):

@pytest.mark.parametrize("library", ["cupy", "numpy", "torch", "dask.array", "jax.numpy"])
def test_device(library):
if library == "dask.array":
pytest.xfail("device() needs to be fixed for dask")

xp = import_(library, wrapper=True)

# We can't test much for device() and to_device() other than that
Expand Down