Skip to content

Commit aad75fd

Browse files
committed
da.asarray should not materialize the graph
1 parent 5ef0e18 commit aad75fd

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

array_api_compat/dask/array/_aliases.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,17 @@ def asarray(
132132
if copy is False:
133133
# copy=False is not yet implemented in dask
134134
raise NotImplementedError("copy=False is not yet implemented")
135-
elif copy is True:
136-
if isinstance(obj, da.Array) and dtype is None:
137-
return obj.copy()
138-
# Go through numpy, since dask copy is no-op by default
139-
obj = np.array(obj, dtype=dtype, copy=True)
140-
return da.array(obj, dtype=dtype)
141-
else:
142-
if not isinstance(obj, da.Array) or dtype is not None and obj.dtype != dtype:
143-
# copy=True to be uniform across dask < 2024.12 and >= 2024.12
144-
# see https://github.com/dask/dask/pull/11524/
145-
obj = np.array(obj, dtype=dtype, copy=True)
146-
return da.from_array(obj)
147-
return obj
148-
149-
return da.asarray(obj, dtype=dtype, **kwargs)
135+
136+
if isinstance(obj, da.Array):
137+
if dtype is not None:
138+
obj = obj.astype(dtype)
139+
return obj.copy() if copy else obj
140+
141+
# copy=None to be uniform across dask < 2024.12 and >= 2024.12
142+
# see https://github.com/dask/dask/pull/11524/
143+
obj = np.asarray(obj, dtype=dtype, copy=True)
144+
return da.from_array(obj)
145+
150146

151147
from dask.array import (
152148
# Element wise aliases

tests/test_dask.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import dask
2+
import numpy as np
3+
import pytest
4+
import array_api_compat.dask.array as xp
5+
6+
@pytest.fixture
7+
def no_compute():
8+
"""
9+
Cause the test to raise if at any point anything calls compute() or persist(),
10+
e.g. as it can be triggered implicitly by __bool__, __array__, etc.
11+
"""
12+
def get(dsk, *args, **kwargs):
13+
raise AssertionError("Called compute() or persist()")
14+
15+
with dask.config.set(scheduler=get):
16+
yield
17+
18+
19+
def test_no_compute(no_compute):
20+
"""Test the no_compute_fixture"""
21+
a = xp.asarray(True)
22+
with pytest.raises(AssertionError, match="Called compute"):
23+
bool(a)
24+
25+
26+
def test_asarray_no_compute(no_compute):
27+
a = xp.arange(10)
28+
xp.asarray(a)
29+
xp.asarray(a, dtype=np.int16)
30+
xp.asarray(a, dtype=a.dtype)
31+
xp.asarray(a, copy=True)
32+
xp.asarray(a, copy=True, dtype=np.int16)
33+
xp.asarray(a, copy=True, dtype=a.dtype)
34+
35+
36+
def test_clip_no_compute(no_compute):
37+
a = xp.arange(10)
38+
xp.clip(a)
39+
xp.clip(a, 1)
40+
xp.clip(a, 1, 8)

0 commit comments

Comments
 (0)