Skip to content

Optimization turned off when using delayed #7587

Open

Description

@chrisroat

What happened:

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:

import dask
import dask.array as da

arr = 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(lambda x: x)(arr)
dask.visualize(delay, optimize_graph=True, filename='delay')

Environment:

  • Dask version: 2021.04.0
  • Python version: 3.9.2
  • Operating System: MacOS 11.2.3
  • Install method (conda, pip, source): conda

Optimized visualization of array:

Optimized visualization after adding delay step.

Activity

  1. jsignell commented on Apr 23, 2021

    @jsignell
    Member

    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

    source

  2. jsignell commented on Apr 23, 2021

    @jsignell
    Member

    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.

  3. chrisroat commented on Apr 23, 2021

    @chrisroat
    ContributorAuthor

    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.

  4. chrisroat commented on Apr 23, 2021

    @chrisroat
    ContributorAuthor

    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)

  5. added
    needs attention
    on Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    delayedneeds attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions