You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A graph does not seem to be optimized properly when using delayed.
What you expected to happen:
Adding a delayed step at the end of a graph should not block optimization of the rest of the graph.
Minimal Complete Verifiable Example:
importdaskimportdask.arrayasdaarr=da.zeros((1,2,2,1,2), chunks=1)
# Example of a non-trivial graph that can be optimized.arr=arr.max(2)[...,1].rechunk(-1).transpose(0,2,1)
dask.visualize(arr, optimize_graph=True, filename='array')
delay=dask.delayed(lambdax: x)(arr)
dask.visualize(delay, optimize_graph=True, filename='delay')
It seems like delayed doesn't do optimization on inputs. I guess that's why methods like dask.array.to_delayed have optimize_graph as a kwarg.
The behavior you are looking for could probably be supported, but I'm wondering if it might not make more sense to call .persist() on the array before calling the delayed function on it.
The docs explicitly recommend against calling delayed on other collections:
Don’t call dask.delayed on other Dask collections
When you place a Dask array or Dask DataFrame into a delayed call, that function will receive the NumPy or Pandas equivalent. Beware that if your array is large, then this might crash your workers.
Instead, it’s more common to use methods like da.map_blocks
So, I looked more closely and I don't really see a reason why we shouldn't call optimize on the inputs before computing. I opened a PR that seems to resolve the case that you were looking at.
Thanks! I do want to call delayed on a dask array and get the full numpy array. My use case is projecting down a large array into a small array which does fit in memory. The delayed call saves the array as a small tiff file.
There is a broad class of conversions from one dask to another that will benefit from this, as well as any array-based delayed function that returns a variable shape output.
Activity
jsignell commentedon Apr 23, 2021
It seems like
delayeddoesn't do optimization on inputs. I guess that's why methods likedask.array.to_delayedhaveoptimize_graphas a kwarg.The behavior you are looking for could probably be supported, but I'm wondering if it might not make more sense to call
.persist()on the array before calling thedelayedfunction on it.The docs explicitly recommend against calling delayed on other collections:
source
jsignell commentedon Apr 23, 2021
So, I looked more closely and I don't really see a reason why we shouldn't call optimize on the inputs before computing. I opened a PR that seems to resolve the case that you were looking at.
chrisroat commentedon Apr 23, 2021
Thanks! I do want to call delayed on a dask array and get the full numpy array. My use case is projecting down a large array into a small array which does fit in memory. The delayed call saves the array as a small tiff file.
chrisroat commentedon Apr 23, 2021
There is a broad class of conversions from one dask to another that will benefit from this, as well as any array-based delayed function that returns a variable shape output.
A dask ecosystem example of array->array has a per-block delayed function returning a variable sized output.
A solution in #7589 shows an array to dataframe conversion going through a delayed step.(the use of to_delayed does use optimization)