Debian Codegraph @

docs/source/caching.rst

raw
1 Opportunistic Caching
2 =====================
3
4 Dask usually removes intermediate values as quickly as possible in order to
5 make space for more data to flow through your computation. However, in some
6 cases, we may want to hold onto intermediate values, because they might be
7 useful for future computations in an interactive session.
8
9 We need to balance the following concerns:
10
11 1. Intermediate results might be useful in future unknown computations
12 2. Intermediate results also fill up memory, reducing space for the rest of our
13 current computation
14
15 Negotiating between these two concerns helps us to leverage the memory that we
16 have available to speed up future, unanticipated computations. Which intermediate results
17 should we keep?
18
19 This document explains an experimental, opportunistic caching mechanism that automatically
20 picks out and stores useful tasks.
21
22
23 Motivating Example
24 ------------------
25
26 Consider computing the maximum value of a column in a CSV file:
27
28 .. code-block:: python
29
30 >>> import dask.dataframe as dd
31 >>> df = dd.read_csv('myfile.csv')
32 >>> df.columns
33 ['first-name', 'last-name', 'amount', 'id', 'timestamp']
34
35 >>> df.amount.max().compute()
36 1000
37
38 Even though our full dataset may be too large to fit in memory, the single
39 ``df.amount`` column may be small enough to hold in memory just in case it
40 might be useful in the future. This is often the case during data exploration,
41 because we investigate the same subset of our data repeatedly before moving on.
42
43 For example, we may now want to find the minimum of the amount column:
44
45 .. code-block:: python
46
47 >>> df.amount.min().compute()
48 -1000
49
50 Under normal operations, this would need to read through the entire CSV file over
51 again. This is somewhat wasteful and stymies interactive data exploration.
52
53
54 Two Simple Solutions
55 --------------------
56
57 If we know ahead of time that we want both the maximum and minimum, we can
58 compute them simultaneously. Dask will share intermediates intelligently,
59 reading through the dataset only once:
60
61 .. code-block:: python
62
63 >>> dd.compute(df.amount.max(), df.amount.min())
64 (1000, -1000)
65
66 If we know that this column fits in memory, then we can also explicitly
67 compute the column and then continue forward with straight Pandas:
68
69 .. code-block:: python
70
71 >>> amount = df.amount.compute()
72 >>> amount.max()
73 1000
74 >>> amount.min()
75 -1000
76
77 If either of these solutions work for you, great. Otherwise, continue on for a third approach.
78
79
80 Automatic Opportunistic Caching
81 -------------------------------
82
83 Another approach is to watch *all* intermediate computations, and *guess* which
84 ones might be valuable to keep for the future. Dask has an *opportunistic
85 caching mechanism* that stores intermediate tasks that show the following
86 characteristics:
87
88 1. Expensive to compute
89 2. Cheap to store
90 3. Frequently used
91
92 We can activate a fixed sized cache as a callback_:
93
94 .. _callback: diagnostics-local.html#custom-callbacks
95
96 .. code-block:: python
97
98 >>> from dask.cache import Cache
99 >>> cache = Cache(2e9) # Leverage two gigabytes of memory
100 >>> cache.register() # Turn cache on globally
101
102 Now the cache will watch every small part of the computation and judge the
103 value of that part based on the three characteristics listed above (expensive
104 to compute, cheap to store, and frequently used).
105
106 Dask will hold on to 2GB of the
107 best intermediate results it can find, evicting older results as better results
108 come in. If the ``df.amount`` column fits in 2GB, then probably all of it will
109 be stored while we keep working on it.
110
111 If we start work on something else,
112 then the ``df.amount`` column will likely be evicted to make space for other
113 more timely results:
114
115 .. code-block:: python
116
117 >>> df.amount.max().compute() # slow the first time
118 1000
119 >>> df.amount.min().compute() # fast because df.amount is in the cache
120 -1000
121 >>> df.id.nunique().compute() # starts to push out df.amount from cache
122
123
124 Cache tasks, not expressions
125 ----------------------------
126
127 This caching happens at the low-level scheduling layer, not the high-level
128 Dask DataFrame or Dask Array layer. We don't explicitly cache the column
129 ``df.amount``. Instead, we cache the hundreds of small pieces of that column
130 that form the dask graph. It could be that we end up caching only a fraction
131 of the column.
132
133 This means that the opportunistic caching mechanism described above works for *all* Dask
134 computations, as long as those computations employ a consistent naming scheme
135 (as all of Dask DataFrame, Dask Array, and Dask Delayed do).
136
137 You can see which tasks are held by the cache by inspecting the following
138 attributes of the cache object:
139
140 .. code-block:: python
141
142 >>> cache.cache.data
143 <stored values>
144 >>> cache.cache.heap.heap
145 <scores of items in cache>
146 >>> cache.cache.nbytes
147 <number of bytes per item in cache>
148
149 The cache object is powered by cachey_, a tiny library for opportunistic
150 caching.
151
152 .. _cachey: https://github.com/blaze/cachey
153
154 Disclaimer
155 ----------
156 Opportunistic caching is not available when using the distributed scheduler.
157
158 Restricting your cache to a fixed size like 2GB requires Dask to accurately count
159 the size of each of our objects in memory. This can be tricky, particularly
160 for Pythonic objects like lists and tuples, and for DataFrames that contain
161 object dtypes.
162
163 It is entirely possible that the caching mechanism will
164 *undercount* the size of objects, causing it to use up more memory than
165 anticipated, which can lead to blowing up RAM and crashing your session.