Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 25 additions & 11 deletions dask/array/routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
broadcast_to,
concatenate,
elemwise,
from_array,
implements,
is_scalar_for_elemwise,
map_blocks,
Expand Down Expand Up @@ -259,28 +260,40 @@ def rot90(m, k=1, axes=(0, 1)):
return flip(transpose(m, axes_list), axes[1])


def _tensordot(a, b, axes):
def _tensordot(a, b, axes, is_sparse):
x = max([a, b], key=lambda x: x.__array_priority__)
tensordot = tensordot_lookup.dispatch(type(x))
x = tensordot(a, b, axes=axes)

if len(axes[0]) != 1:
if is_sparse and len(axes[0]) == 1:
return x
else:
ind = [slice(None, None)] * x.ndim
for a in sorted(axes[0]):
ind.insert(a, None)
x = x[tuple(ind)]
return x

return x

def _tensordot_is_sparse(x):
is_sparse = "sparse" in str(type(x._meta))
if is_sparse:
# exclude pydata sparse arrays, no workaround required for these in tensordot
is_sparse = "sparse._coo.core.COO" not in str(type(x._meta))
return is_sparse


@derived_from(np)
def tensordot(lhs, rhs, axes=2):
if not isinstance(lhs, Array):
lhs = from_array(lhs)
if not isinstance(rhs, Array):
rhs = from_array(rhs)

if isinstance(axes, Iterable):
left_axes, right_axes = axes
else:
left_axes = tuple(range(lhs.ndim - axes, lhs.ndim))
right_axes = tuple(range(0, axes))

if isinstance(left_axes, Integral):
left_axes = (left_axes,)
if isinstance(right_axes, Integral):
Expand All @@ -289,23 +302,23 @@ def tensordot(lhs, rhs, axes=2):
left_axes = tuple(left_axes)
if isinstance(right_axes, list):
right_axes = tuple(right_axes)
if len(left_axes) == 1:
is_sparse = _tensordot_is_sparse(lhs) or _tensordot_is_sparse(rhs)
if is_sparse and len(left_axes) == 1:
concatenate = True
else:
concatenate = False

dt = np.promote_types(lhs.dtype, rhs.dtype)

left_index = list(range(lhs.ndim))
right_index = list(range(lhs.ndim, lhs.ndim + rhs.ndim))
out_index = left_index + right_index

adjust_chunks = {}
for l, r in zip(left_axes, right_axes):
Comment thread
GenevieveBuckley marked this conversation as resolved.
Resolved
out_index.remove(right_index[r])
right_index[r] = left_index[l]
if concatenate:
out_index.remove(left_index[l])
Comment thread
GenevieveBuckley marked this conversation as resolved.
Resolved

else:
adjust_chunks[left_index[l]] = lambda c: 1
intermediate = blockwise(
_tensordot,
out_index,
Expand All @@ -315,9 +328,10 @@ def tensordot(lhs, rhs, axes=2):
right_index,
dtype=dt,
concatenate=concatenate,
Comment thread
GenevieveBuckley marked this conversation as resolved.
Resolved
adjust_chunks=adjust_chunks,
axes=(left_axes, right_axes),
is_sparse=is_sparse,
)

if concatenate:
return intermediate
else:
Expand Down
33 changes: 28 additions & 5 deletions dask/array/tests/test_cupy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,30 @@ def test_sparse_concatenate(axis):


@pytest.mark.parametrize("sp_format", ["csr", "csc"])
def test_sparse_dot(sp_format):
@pytest.mark.parametrize(
"input_sizes",
[
{
"x_shape": (4, 8),
"y_shape": (8, 6),
"x_chunks": (2, 4),
"y_chunks": (4, 3),
},
{
"x_shape": (4, 4),
"y_shape": (4, 4),
"x_chunks": (2, 2),
"y_chunks": (2, 2),
},
{
"x_shape": (4, 4),
"y_shape": (4, 4),
"x_chunks": (4, 2),
"y_chunks": (2, 4),
},
],
)
def test_sparse_dot(sp_format, input_sizes):
pytest.importorskip("cupyx")

if sp_format == "csr":
Expand All @@ -61,8 +84,8 @@ def test_sparse_dot(sp_format):
sp_matrix = cupyx.scipy.sparse.csc_matrix
dtype = "f"
density = 0.3
x_shape, x_chunks = (4, 8), (2, 4)
y_shape, y_chunks = (8, 6), (4, 3)
x_shape, x_chunks = input_sizes["x_shape"], input_sizes["x_chunks"]
y_shape, y_chunks = input_sizes["y_shape"], input_sizes["y_chunks"]
x = cupy.random.random(x_shape, dtype=dtype)
y = cupy.random.random(y_shape, dtype=dtype)
x[x < 1 - density] = 0
Expand All @@ -71,8 +94,8 @@ def test_sparse_dot(sp_format):

da_x = da.from_array(x, chunks=x_chunks, asarray=False, fancy=False)
da_y = da.from_array(y, chunks=y_chunks, asarray=False, fancy=False)
da_x = da_x.map_blocks(sp_matrix, dtype=dtype)
da_y = da_y.map_blocks(sp_matrix, dtype=dtype)
da_x = da_x.map_blocks(sp_matrix, meta=sp_matrix(cupy.array([0], dtype=dtype)))
da_y = da_y.map_blocks(sp_matrix, meta=sp_matrix(cupy.array([0], dtype=dtype)))
da_z = da.dot(da_x, da_y).compute()

assert cupyx.scipy.sparse.isspmatrix(da_z)
Expand Down
13 changes: 13 additions & 0 deletions dask/array/tests/test_routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,19 @@ def test_dot_method():
assert_eq(a.dot(b), x.dot(y))


def test_dot_persist_equivalence():
# Regression test for https://github.com/dask/dask/issues/6907
x = da.random.random((4, 4), chunks=(2, 2))
x[x < 0.65] = 0
y = x.persist()
z = x.compute()
r1 = da.dot(x, x).compute()
r2 = da.dot(y, y).compute()
rr = np.dot(z, z)
assert np.allclose(rr, r1)
assert np.allclose(rr, r2)


@pytest.mark.parametrize("shape, chunks", [((20,), (6,)), ((4, 5), (2, 3))])
def test_vdot(shape, chunks):
np.random.seed(1337)
Expand Down
2 changes: 1 addition & 1 deletion dask/array/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_basic(func):
xx = func(x)
yy = func(y)

assert_eq(xx, yy)
assert_eq(xx, yy, check_meta=False)

if yy.shape:
zz = yy.compute()
Expand Down