Skip to content

graph became invalid in 2021.10.0 #8292

Open

Description

@chrisroat

What happened:

Error raised during graph.validate() for a graph which was valid in 2021.9.1.

This graph creates a dataframe per chunk of an array, then merges the dataframes along one axis of the array. A final per-chunk analysis is then run, where a merged dataframe is used in the analysis of each chunk that was used to make it.

What you expected to happen:

The constructed graph should validate.

Minimal Complete Verifiable Example:

import functools
import operator

import dask
import dask.array as da
import dask.dataframe as dd

import numpy as np

META = dd.utils.make_meta([("foo", int)])


def test_sequential():
    image = da.zeros((2, 1), chunks=(1, -1))

    indices = list(np.ndindex(*image.numblocks))
    slices = da.core.slices_from_chunks(image.chunks)
    chunks = list(map(functools.partial(operator.getitem, image), slices))

    # Create one dataframe per chunk.
    ddf_all = np.empty(image.numblocks, dtype=object)
    for idx_chunk, chunk in zip(indices, chunks):
        ddf_delayed = dask.delayed(lambda: None)(chunk)
        ddf_all[idx_chunk] = dd.from_delayed(ddf_delayed, meta=META)

    # Merge dataframes along first axis.
    ddf_array = np.empty(image.numblocks[2:], dtype=object)
    for idx_spatial in np.ndindex(*ddf_array.shape):
        idx_all = (...,) + idx_spatial
        ddf = dd.concat(ddf_all[idx_all].flatten().tolist())
        ddf_array[idx_spatial] = ddf

    # Run a per-chunk calculation using the merged dataframes,
    # where a single merged dataframe is passed back to the all chunks that
    # went into making it.
    signal = []
    for idx_chunk, chunk in zip(indices, chunks):
        ddf = ddf_array[idx_chunk[2:]]
        ddf_signal_chunk = dask.delayed(lambda: None)(chunk, ddf)
        signal.append(ddf_signal_chunk)

    ddf_signal = dd.from_delayed(signal, meta=META)
    ddf_signal.__dask_graph__().validate()  # FAILS

Output:

E               ValueError: incorrect dependencies['finalize-287aac88-1505-4627-8949-4d20bf676833']: {'getitem-5a7a378a35301a5d6727950e7814a5c2'} expected {'concat-732322a8bf255193f89c61560c08bcdb'}

Environment:

  • Dask version: 2021.10.0
  • Python version: 3.8.10
  • Operating System: Ubuntu 20.1
  • Install method (conda, pip, source): pip

Activity

  1. jrbourbeau commented on Oct 25, 2021

    @jrbourbeau
    Member

    Thanks @chrisroat -- I'm able to confirm the exception you're seeing. Running your example through Dask's git history, #8174 is where your example started to fail. cc @gjoseph92 for visibility

  2. jakirkham commented on Oct 25, 2021

    @jakirkham
    Member

    cc @rjzamora (for awareness)

  3. chrisroat commented on Oct 25, 2021

    @chrisroat
    ContributorAuthor

    Ah, bummer! I was a big fan of the PR.

  4. jakirkham commented on Oct 25, 2021

    @jakirkham
    Member

    This change sticks out to me ( #8174 (comment) )

  5. chrisroat commented on Nov 3, 2021

    @chrisroat
    ContributorAuthor

    Here is a more simplified version of the above (thank you new BlockView!), where doing a dd.concat on some dd.from_delayed dataframes is broken, as compared to directly using dd.from_delayed on a list of delayed objects.

    import dask
    import dask.array as da
    import dask.dataframe as dd
    
    META = dd.utils.make_meta([("foo", int)])
    
    image = da.zeros((2, 1), chunks=1)
    
    def make_ddf_broken():
        return dd.concat([
            dd.from_delayed(dask.delayed(lambda: None)(image.blocks[0]), meta=META),
            dd.from_delayed(dask.delayed(lambda: None)(image.blocks[1]), meta=META),
        ])
    
    def make_ddf_works():
        ddf = [
            dask.delayed(lambda: None)(image.blocks[0]),
            dask.delayed(lambda: None)(image.blocks[1]),
        ]
        return dd.from_delayed(ddf, meta=META)
    
    ddf = make_ddf_broken()
    #ddf = make_ddf_works()
    
    ddf_final = [
        dask.delayed(lambda: None)(image.blocks[0], ddf),
        dask.delayed(lambda: None)(image.blocks[1], ddf),
    ]
    ddf_final = dd.from_delayed(ddf_final, meta=META)
    ddf_final.__dask_graph__().validate()
  6. jrbourbeau commented on Nov 4, 2021

    @jrbourbeau
    Member

    Thank you for the simplified example @chrisroat. Out of curiosity, does this regression result in your computation failing? Or is it just the validate() method that fails? I ask because if I replace ddf_final.__dask_graph__().validate() with ddf_final.compute() in the second example I get the following error

    Traceback (most recent call last):
      File "/Users/james/projects/dask/dask/foo.py", line 31, in <module>
        ddf_final.compute()
      File "/Users/james/projects/dask/dask/dask/base.py", line 288, in compute
        (result,) = compute(self, traverse=False, **kwargs)
      File "/Users/james/projects/dask/dask/dask/base.py", line 570, in compute
        results = schedule(dsk, keys, **kwargs)
      File "/Users/james/projects/dask/dask/dask/threaded.py", line 79, in get
        results = get_async(
      File "/Users/james/projects/dask/dask/dask/local.py", line 507, in get_async
        raise_exception(exc, tb)
      File "/Users/james/projects/dask/dask/dask/local.py", line 315, in reraise
        raise exc
      File "/Users/james/projects/dask/dask/dask/local.py", line 220, in execute_task
        result = _execute_task(task, data)
      File "/Users/james/projects/dask/dask/dask/core.py", line 119, in _execute_task
        return func(*(_execute_task(a, cache) for a in args))
    TypeError: <lambda>() takes 0 positional arguments but 1 was given

    with both the latest main branch as well as the 2021.09.1 release

  7. chrisroat commented on Nov 4, 2021

    @chrisroat
    ContributorAuthor

    Your error is because the computations are bogus lambda function. The example provided is simply creating the graph. My tests fail during assert_eq which validates the graph prior to even running compute.

    What I am experimenting with is whether the make_ddf_works is a valid substitute. It seems to create a valid graph and passes the unit tests. I'm trying out our regression tests to be sure the computation is still valid.

  8. chrisroat commented on Nov 4, 2021

    @chrisroat
    ContributorAuthor

    So the regression tests are passing, which makes me feel confident the computation isn't screwed up.

    Is it expected that dd.from_delayed on a list of delayed objects should result in a single dataframe, effectively doing a dd.concat?

  9. gjoseph92 commented on Nov 5, 2021

    @gjoseph92
    Collaborator

    Is it expected that dd.from_delayed on a list of delayed objects should result in a single dataframe, effectively doing a dd.concat?

    Based on the docs, I think so:

        dfs : list of Delayed
            An iterable of ``dask.delayed.Delayed`` objects, such as come from
            ``dask.delayed`` These comprise the individual partitions of the
            resulting dataframe.
    

    Is that not what you were expecting?

  10. chrisroat commented on Nov 5, 2021

    @chrisroat
    ContributorAuthor

    My expectations aside, your read of the documents makes it clear what is correct. :) Though technically, I'd be curious what happens if each delayed object is itself multiple partitions.

    For that part of the example, I had just been doing the from_delayed individually, and then doing a concat. It's nice to do it with fewer lines of code! It is interesting that the same graph isn't generated both ways.

  11. gjoseph92 commented on Nov 5, 2021

    @gjoseph92
    Collaborator

    I'd be curious what happens if each delayed object is itself multiple partitions.

    What do you mean? Like if your delayed object referenced a list of pandas DataFrames, like delayed([pd.DataFrame(...), pd.DataFrame(...)])? In that case, it would just be invalid.

    If you think about how DataFrame.to_delayed() essentially "explodes" the dask DataFrame into a list of delayeds, where each partition is its own delayed object, from_delayed is the inverse: you give one delayed object per partition, and they're reassembled into a DataFrame.

    It is interesting that the same graph isn't generated both ways.

    I'm not surprised they're different. concat has all sorts of other things to do: joining columns, checking dtypes, etc. It's for combining logically separate DataFrames into one. It's adding actual pd.conat tasks to the graph.

    from_delayed is saying "together, these delayed objects comprise one logical DataFrame; I want you to represent them as such. Trust me, they already have the same columns and dytpe and everything.". It's really just graph-manipulation; no extra tasks (just aliases) get added to the graph.

    Nonetheless, from_delayed -> concat should still produce a valid graph.

  12. chrisroat commented on Nov 9, 2021

    @chrisroat
    ContributorAuthor

    Thanks for the better understanding of from_delayed/to_delayed.

    It is interesting that the same graph isn't generated both ways.

    I'm not surprised they're different. concat has all sorts of other things to do: joining columns, checking dtypes, etc. It's for combining logically separate DataFrames into one. It's adding actual pd.conat tasks to the graph.

    Ah, I see. I would think the metadata would be enough to skip adding tasks that weren't necessary (in this case, all meta are identical in columns/types).

    from_delayed is saying "together, these delayed objects comprise one logical DataFrame; I want you to represent them as such. Trust me, they already have the same columns and dytpe and everything.". It's really just graph-manipulation; no extra tasks (just aliases) get added to the graph.

    Nonetheless, from_delayed -> concat should still produce a valid graph.

    Agreed. I will skip using concat myself, both because of this possible bug and because I don't need the extra logic.

  13. gjoseph92 commented on Nov 9, 2021

    @gjoseph92
    Collaborator

    I would think the metadata would be enough to skip adding tasks that weren't necessary (in this case, all meta are identical in columns/types).

    Ah, if only that were the case! That's worth opening an issue for. I wish we more efficiently supported that workflow of explode chunks -> do things to chunks -> combine back into DataFrame/Array.

  14. jakirkham commented on Nov 9, 2021

    @jakirkham
    Member

    I wish we more efficiently supported that workflow of explode chunks -> do things to chunks -> combine back into DataFrame/Array.

    With Array we have .blocks and DataFrames we have .partitions.

  15. jsignell commented on Dec 20, 2021

    @jsignell
    Member

    Is this issue good to close? It seems like the discussion is mostly around building understanding.

  16. added
    needs attention
    on Dec 20, 2021
  17. chrisroat commented on Dec 20, 2021

    @chrisroat
    ContributorAuthor

    It seems like a bug (to me) that simple operations lead to invalid graphs. My concern would be that it could by hiding something subtle. Of course, I could be wrong, and my repro case may be incorrectly coded: #8292 (comment)

  18. jsignell commented on Dec 20, 2021

    @jsignell
    Member

    I just toyed around with a slightly modified example using make_df_works. Some notable differences:

    import dask
    import dask.array as da
    import dask.dataframe as dd
    
    META = dd.utils.make_meta([("foo", int)])
    
    image = da.zeros((2, 1), chunks=1)
    
    def make_ddf_broken():
        return dd.concat([
            dd.from_delayed(dask.delayed(lambda x: x)(image.blocks[0]), meta=META),
            dd.from_delayed(dask.delayed(lambda x: x)(image.blocks[1]), meta=META),
        ])
    
    def make_ddf_works():
        ddf = [
            dask.delayed(lambda x: x)(image.blocks[0]),
            dask.delayed(lambda x: x)(image.blocks[1]),
        ]
        return dd.from_delayed(ddf, meta=META)
    
    ddf = make_ddf_broken()
    ddf = make_ddf_works()
    
    ddf_final = [
        dask.delayed(lambda x: x)(image.blocks[0], ddf),
        dask.delayed(lambda x: x)(image.blocks[1], ddf),
    ]
    ddf_final = dd.from_delayed(ddf_final, meta=META)
    ddf_final.__dask_graph__().validate()
    
  19. chrisroat commented on Dec 20, 2021

    @chrisroat
    ContributorAuthor

    Do you have thoughts on why the graph generated with make_ddf_broken does not work?

  20. jsignell commented on Dec 21, 2021

    @jsignell
    Member

    Do you have thoughts on why the graph generated with make_ddf_broken does not work?

    I have one main thoughtsthat is not totally tested. If it is correct we can do a better job with error handling: dd.from_delayed is meant to handle a list of delayed objects. In make_ddf_broken you are passing in a single delayed object.

  21. jsignell commented on Dec 21, 2021

    @jsignell
    Member

    No that's not it. I just looked and the first thing Dask does if you pass a Delayed is just wrap it in a list.

  22. removed
    needs attention
    on Sep 9, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions