Skip to content

Duplicated computations with mixed array/dataframe delayed output #7545

Open

Description

@chrisroat

What happened:

A computation from a delayed call returning two outputs is done twice when one output is array and one output is a dataframe. Interestingly, if both outputs are arrays, or both outputs are dataframes, the duplication does not occur.

If the two outputs (one array, one dataframe) are fed to another delayed call, a computation on the output of that 2nd delayed call does not cause in the extra computation.

In the code below, compute is printed twice under 'Individually'.

What you expected to happen:

compute should be printed once under 'Individually'.

Minimal Complete Verifiable Example:

import dask
import dask.array as da
import dask.dataframe as dd
import numpy as np
import pandas as pd

def calc():
    print('compute')
    return np.zeros(1), pd.DataFrame({'z': [1]})

res = dask.delayed(calc, nout=2)()
res0 = da.from_delayed(res[0], shape=(1,), dtype=np.float64)
res1 = dd.from_delayed(res[1], meta=[('z', np.int64)])

print('Individually')
_ = dask.compute(res0, res1, scheduler='synchronous')

def comb(a, b):
    return a, b
res_comb = dask.delayed(comb)(res0, res1)

print()
print('Combined')
_ = dask.compute(res_comb, scheduler='synchronous')
Individually
compute
compute

Combined
compute

Anything else we need to know?:

I do not think this is a meta/dtype/shape inference issue, as duplication isn't happening with the "Combined" computation.

Environment:

  • Dask version: 2021.04.0+16.ge83379d5
  • Python version: 3.8.8
  • Operating System: MacOS 11.2.3
  • Install method (conda, pip, source): conda

Activity

  1. chrisroat commented on Apr 10, 2021

    @chrisroat
    ContributorAuthor

    I've found that turning off optimization causes this problem to go away. The optimizer is somehow deciding to inline the calc computation when the outputs are mixed like this.

  2. jrbourbeau commented on Apr 12, 2021

    @jrbourbeau
    Member

    Thanks for the nice example @chrisroat, I'm able to reproduce the same behavior. Looking through dask.compute, we optimize each type of dask collection independently here:

    dask/dask/base.py

    Lines 331 to 342 in 09862ed

    if optimize_graph:
    groups = groupby(optimization_function, collections)
    graphs = []
    for opt, val in groups.items():
    dsk, keys = _extract_graph_and_keys(val)
    dsk = opt(dsk, keys, **kwargs)
    for opt in optimizations:
    dsk = opt(dsk, keys, **kwargs)
    graphs.append(dsk)

    That is, we optimize Dask arrays using the the default optimizer for Dask arrays, then optimize Dask DataFrames using the default optimizer for Dask DataFrames, etc. Since each of these optimization passes is done independently, when we are doing the array optimization the optimizer doesn't know that the calc function is being used elsewhere (in the Dask DataFrame computation) so low-level task fusion results in the calc function being inlined. The same thing also happens when the DataFrame optimizer runs too.

    I don't necessarily have a good long-term fix for this at the moment, though as a temporary workaround you can try disabling low-level task fusion by setting the optimization.fuse.active configuration value to False. This is slightly better than disabling optimizations altogether.

    cc'ing @crusaderky @madsbk who have thought about graph optimizations recently and may be interested in this (though no obligation)

  3. chrisroat commented on Apr 22, 2021

    @chrisroat
    ContributorAuthor

    Thanks for the tip on turning off fuse. I also found #7587 which is related, and maybe a duplicate from an implementation point of view - both issues might be solved by some sort of global optimization.

    My graphs are pretty large, so I do rely on a lot of the optimization to avoid the scheduler falling over. I'm breaking up my pipelines to keep the graphs under 100k and avoid these issues.

  4. 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

    arraydataframeneeds attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions