Debian Codegraph @

docs/source/array-sparse.rst

raw
1 Sparse Arrays
2 =============
3
4 By swapping out in-memory NumPy arrays with in-memory sparse arrays, we can
5 reuse the blocked algorithms of Dask's Array to achieve parallel and distributed
6 sparse arrays.
7
8 The blocked algorithms in Dask Array normally parallelize around in-memory
9 NumPy arrays. However, if another in-memory array library supports the NumPy
10 interface, then it too can take advantage of Dask Array's parallel algorithms.
11 In particular the `sparse <https://github.com/pydata/sparse/>`_ array library
12 satisfies a subset of the NumPy API and works well with (and is tested against)
13 Dask Array.
14
15 Example
16 -------
17
18 Say we have a Dask array with mostly zeros:
19
20 .. code-block:: python
21
22 rng = da.random.default_rng()
23 x = rng.random((100000, 100000), chunks=(1000, 1000))
24 x[x < 0.95] = 0
25
26 We can convert each of these chunks of NumPy arrays into a **sparse.COO** array:
27
28 .. code-block:: python
29
30 import sparse
31 s = x.map_blocks(sparse.COO)
32
33 Now, our array is not composed of many NumPy arrays, but rather of many
34 sparse arrays. Semantically, this does not change anything. Operations that
35 work will continue to work identically (assuming that the behavior of ``numpy``
36 and ``sparse`` are identical), but performance characteristics and storage costs
37 may change significantly:
38
39 .. code-block:: python
40
41 >>> s.sum(axis=0)[:100].compute()
42 <COO: shape=(100,), dtype=float64, nnz=100>
43
44 >>> _.todense()
45 array([ 4803.06859272, 4913.94964525, 4877.13266438, 4860.7470773 ,
46 4938.94446802, 4849.51326473, 4858.83977856, 4847.81468485,
47 ... ])
48
49 Requirements
50 ------------
51
52 Any in-memory library that copies the NumPy ndarray interface should work here.
53 The `sparse <https://github.com/pydata/sparse/>`_ library is a minimal
54 example. In particular, an in-memory library should implement at least the
55 following operations:
56
57 1. Simple slicing with slices, lists, and elements (for slicing, rechunking,
58 reshaping, etc)
59 2. A ``concatenate`` function matching the interface of ``np.concatenate``.
60 This must be registered in ``dask.array.core.concatenate_lookup``
61 3. All ufuncs must support the full ufunc interface, including ``dtype=`` and
62 ``out=`` parameters (even if they don't function properly)
63 4. All reductions must support the full ``axis=`` and ``keepdims=`` keywords
64 and behave like NumPy in this respect
65 5. The array class should follow the ``__array_priority__`` protocol and be
66 prepared to respond to other arrays of lower priority
67 6. If ``dot`` support is desired, a ``tensordot`` function matching the
68 interface of ``np.tensordot`` should be registered in
69 ``dask.array.core.tensordot_lookup``
70
71 The implementation of other operations like reshape, transpose, etc.,
72 should follow standard NumPy conventions regarding shape and dtype. Not
73 implementing these is fine; the parallel ``dask.array`` will err at runtime if
74 these operations are attempted.
75
76
77 Mixed Arrays
78 ------------
79
80 Dask's Array supports mixing different kinds of in-memory arrays. This relies
81 on the in-memory arrays knowing how to interact with each other when necessary.
82 When two arrays interact, the functions from the array with the highest
83 ``__array_priority__`` will take precedence (for example, for concatenate,
84 tensordot, etc.).