dask cache delayed function example

Viewed 129

A simple dask cache example. Cache does not work as expected. Let's assume we have a list of data and a series of delayed functions, expected that for a function that encounters the same input to cache/memoize the results according to cachey score.
This example demonstrates that is not the case.

import time
import dask
from dask.cache import Cache
from dask.diagnostics import visualize
from dask.diagnostics import Profiler, ResourceProfiler, CacheProfiler    

def slow_func(x):
    time.sleep(5)
    return x+1

output = []
data = np.ones((100))
for x in data:
    a = dask.delayed(slow_func)(x)
    output.append(a)
total = dask.delayed(sum)(output)

cache = Cache(2e9)
cache.register() 

with Profiler() as prof, ResourceProfiler(dt=0.25) as rprof,CacheProfiler() as cprof:
    total.compute()

visualize([prof, rprof, cprof])  

cache cprof plot

After the initial parallel execution of the function would expect the next iteration upon calling the function with the same value to use a cache version. But obviously does not, dask_key_name is for designating the same output, but i want to assess this function for a variety of inputs and if seeing the same input use cached version. We can tell if this is happening very easily with this function due to the 5 second delay and should see it execute roughly 5 seconds as soon as the first value is cached after execution. This example executes every single function delayed 5 seconds. I am able to create a memoized version using the cachey library directly but this should work using the dask.cache library.

1 Answers

In dask.delayed you may need to specify the pure=True keyword.

You can verify that this worked because all of your dask delayed values will have the same key.

You don't need to use Cache for this if they are all in the same dask.compute call.

Related