Skip to content

Commit

Permalink
Implement deterministic dask tokenization. (#320)
Browse files Browse the repository at this point in the history
* Test deterministic dask tokenization.

* Implement __dask_tokenize__.

* Add missing test dependency.

* Apply black.
  • Loading branch information
danielballan authored Feb 6, 2020
1 parent 517a579 commit c7b40a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion requirements/tests.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dask[array]
pytest>=3.5
pytest-black
pytest-cov
pytest-cov
8 changes: 8 additions & 0 deletions sparse/_coo/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ def __setstate__(self, state):
self.coords, self.data, self.shape, self.fill_value = state
self._cache = None

def __dask_tokenize__(self):
"Produce a deterministic, content-based hash for dask."
from dask.base import normalize_token

return normalize_token(
(type(self), self.coords, self.data, self.shape, self.fill_value)
)

def copy(self, deep=True):
"""Return a copy of the array.
Expand Down
11 changes: 11 additions & 0 deletions sparse/tests/test_dask_interop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from dask.base import tokenize
import sparse


def test_deterministic_token():
a = sparse.COO(data=[1, 2, 3], coords=[10, 20, 30], shape=(40,))
b = sparse.COO(data=[1, 2, 3], coords=[10, 20, 30], shape=(40,))
assert tokenize(a) == tokenize(b)
# One of these things is not like the other....
c = sparse.COO(data=[1, 2, 4], coords=[10, 20, 30], shape=(40,))
assert tokenize(a) != tokenize(c)

0 comments on commit c7b40a5

Please sign in to comment.