Debian Codegraph @

docs/source/array-design.rst

raw
1 Internal Design
2 ===============
3
4 Overview
5 --------
6
7 .. image:: images/array.svg
8 :width: 40 %
9 :align: right
10 :alt: 12 rectangular blocks arranged as a 4-row, 3-column layout. Each block includes 'x' and its location in the table starting with ('x',0,0) in the top-left, and a size of 5x8.
11
12 Dask arrays define a large array with a grid of blocks of smaller arrays.
13 These arrays may be actual arrays or functions that produce arrays. We
14 define a Dask array with the following components:
15
16 * A Dask graph with a special set of keys designating blocks
17 such as ``('x', 0, 0), ('x', 0, 1), ...`` (See :doc:`Dask graph
18 documentation <graphs>` for more details)
19 * A sequence of chunk sizes along each dimension called ``chunks``,
20 for example ``((5, 5, 5, 5), (8, 8, 8))``
21 * A name to identify which keys in the Dask graph refer to this array, like
22 ``'x'``
23 * A NumPy dtype
24
25 Example
26 ~~~~~~~
27
28 .. code-block:: python
29
30 >>> import dask.array as da
31 >>> x = da.arange(0, 15, chunks=(5,))
32
33 >>> x.name
34 'arange-539766a'
35
36 >>> x.__dask_graph__()
37 <dask.highlevelgraph.HighLevelGraph at 0x7f9f6f686d68>
38
39 >>> dict(x.__dask_graph__()) # somewhat simplified
40 {('arange-539766a', 0): (np.arange, 0, 5),
41 ('arange-539766a', 1): (np.arange, 5, 10),
42 ('arange-539766a', 2): (np.arange, 10, 15)}
43
44 >>> x.chunks
45 ((5, 5, 5),)
46
47 >>> x.dtype
48 dtype('int64')
49
50
51 Keys of the Dask graph
52 ----------------------
53
54 By special convention, we refer to each block of the array with a tuple of the
55 form ``(name, i, j, k)``, with ``i, j, k`` being the indices of the block
56 ranging from ``0`` to the number of blocks in that dimension. The Dask graph
57 must hold key-value pairs referring to these keys. Moreover, it likely also
58 holds other key-value pairs required to eventually compute the desired values
59 (usually organised in a :doc:`HighLevelGraph <high-level-graphs>`, but shown
60 in a flattened form here for illustration):
61
62 .. code-block:: python
63
64 {
65 ('x', 0, 0): (add, 1, ('y', 0, 0)),
66 ('x', 0, 1): (add, 1, ('y', 0, 1)),
67 ...
68 ('y', 0, 0): (getitem, dataset, (slice(0, 1000), slice(0, 1000))),
69 ('y', 0, 1): (getitem, dataset, (slice(0, 1000), slice(1000, 2000)))
70 ...
71 }
72
73 The name of an ``Array`` object can be found in the ``name`` attribute. One
74 can get a nested list of keys with the ``.__dask_keys__()`` method. Additionally,
75 one can flatten down this list with ``dask.array.core.flatten()``. This is sometimes
76 useful when building new dictionaries.
77
78 Chunks
79 ------
80
81 We also store the size of each block along each axis. This is composed of
82 a tuple of tuples such that the length of the outer tuple is equal to the
83 number of dimensions of the array, and the lengths of the inner tuples are
84 equal to the number of blocks along each dimension. In the example illustrated
85 above this value is as follows::
86
87 chunks = ((5, 5, 5, 5), (8, 8, 8))
88
89 Note that these numbers do not necessarily need to be regular. We often create
90 regularly sized grids but blocks change shape after complex slicing. Beware
91 that some operations do expect certain symmetries in the block-shapes. For
92 example, matrix multiplication requires that blocks on each side have
93 anti-symmetric shapes.
94
95 Some ways in which ``chunks`` reflects properties of our array:
96
97 1. ``len(x.chunks) == x.ndim``: the length of chunks is the number of dimensions
98 2. ``tuple(map(sum, x.chunks)) == x.shape``: the sum of each internal chunk is the
99 length of that dimension
100 3. The length of each internal chunk is the number of keys in that dimension.
101 For instance, for ``chunks == ((a, b), (d, e, f))`` and name == ``'x'``
102 our array has tasks with the following keys::
103
104 ('x', 0, 0), ('x', 0, 1), ('x', 0, 2)
105 ('x', 1, 0), ('x', 1, 1), ('x', 1, 2)
106
107
108 Metadata
109 --------
110
111 Many Array operations rely on knowing the dtype (int, float,..) and type (numpy, cupy,...). To
112 keep track of this information, all Dask Array objects have a ``_meta``
113 attribute which contains an empty Numpy object with the same dtypes.
114 For example:
115
116 .. code-block:: python
117
118 >>> np_array = np.arange(15).reshape(3, 5)
119 >>> da_array = da.from_array(np_array, npartitions=2)
120 >>> da_array._meta
121 Empty Array
122 Shape: (0, 0)
123 dtype: int64
124 array([], shape=(0, 0), dtype=int64)
125
126 >>> ddf._meta.dtype
127 dtype: int64
128
129 Internally, Dask Array does its best to propagate this information
130 through all operations, so most of the time a user shouldn't have to worry
131 about this.
132
133
134 Create an Array Object
135 ----------------------
136
137 In order to create an ``da.Array`` object we need a graph with these special
138 keys::
139
140 layer = {('x', 0, 0): ...}
141 dsk = HighLevelGraph.from_collections('x', layer, dependencies=())
142
143 a name specifying which keys this array refers to::
144
145 name = 'x'
146
147 and a chunks tuple::
148
149 chunks = ((5, 5, 5, 5), (8, 8, 8))
150
151 Then, using these elements, one can construct an array::
152
153 x = da.Array(dsk, name, chunks)
154
155 In short, ``dask.array`` operations update Dask graphs, update dtypes, and track chunk
156 shapes.
157
158
159 Example - ``eye`` function
160 --------------------------
161
162 As an example, let's build the ``np.eye`` function for ``dask.array`` to make the
163 identity matrix:
164
165 .. code-block:: python
166
167 def eye(n, blocksize):
168 chunks = ((blocksize,) * (n // blocksize),
169 (blocksize,) * (n // blocksize))
170
171 name = 'eye' + next(tokens) # unique identifier
172
173 layer = {(name, i, j): (np.eye, blocksize)
174 if i == j else
175 (np.zeros, (blocksize, blocksize))
176 for i in range(n // blocksize)
177 for j in range(n // blocksize)}
178 dsk = dask.highlevelgraph.HighLevelGraph.from_collections(name, layer, dependencies=())
179
180 dtype = np.eye(0).dtype # take dtype default from numpy
181
182 return dask.array.Array(dsk, name, chunks, dtype)