Skip to content

Use DataFrameIOLayer in DataFrame.from_delayed - #8852

Merged
rjzamora merged 17 commits into
dask:mainfrom
rjzamora:from-delayed-blockwise
Apr 22, 2022
Merged

rjzamora merged 17 commits into
dask:mainfrom
rjzamora:from-delayed-blockwise

Conversation

@rjzamora

@rjzamora rjzamora commented Mar 25, 2022 •

Copy link
Copy Markdown
Member

This PR rewrites the logic in dask.dataframe.io.from_delayed to enable blockwise fusion with Delayed IO functions. Take the following Delayed-based custom IO code for example:

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

def read_files(input_paths):
    dfs = [pd.read_parquet(path) for path in input_paths]
    return pd.concat(dfs)

files_per_task = 2
input_paths = ["tmpdir/part.0.parquet", "tmpdir/part.1.parquet", "tmpdir/part.2.parquet"]
files_groups = np.array_split(input_paths, files_per_task)
result = dd.from_delayed(
    map(dask.delayed(read_files), files_groups),
    meta=read_files(input_paths[:1]),
).to_parquet("new_tmpdir")

This code uses pd.read_parquet directly to aggregate specific files in an input parquet dataset into a smaller number of files in an output dataset. With the current implementation of from_delayed, this code results in the following "optimized" graph:

Screen Shot 2022-03-25 at 4 19 49 PM

Note that neither the delayed read_files tasks nor the following to-parquet tasks are fused into the same task as from-delayed. This means different workers may end up running the read_files and to-parquet tasks for the same partition. After this PR, this problem is resolved:

Screen Shot 2022-03-25 at 4 17 49 PM

Note that the move to DataFrameIOLayer works for both Delayed and distributed.Future objects.

TODO:

  • Add test coverage
  • Possibly revise the delayed/delayed_deps attribute design in BlockwiseDep

@rjzamora

Copy link
Copy Markdown
Member Author

cc @ian-r-rose

@ian-r-rose
ian-r-rose self-requested a review March 25, 2022 21:31
Comment thread dask/blockwise.py Resolved
@rajeee

rajeee commented Mar 25, 2022

Copy link
Copy Markdown

Excellent. Do you think this would drastically improve the scheduler time and memory requirement as well?
Check this answer: https://stackoverflow.com/a/71531717/2105924
It seems like currently, from_delayed ends up having to read all the files before being able to form a task graph and schedule the tasks, which ends up blowing up the memory consumption and OOM errors.

@ian-r-rose

Copy link
Copy Markdown
Collaborator

Thanks for this @rjzamora, and sorry for being slow to take a look. This is cool!

I'm struggling a bit with the design here -- in particular, I'm worried about adding yet more complexity to Blockwise for a fairly specialized use-case. What else might use BlockwiseDep.delayed? Probably just Bag.from_delayed and Array.from_delayed. It's not that those aren't important, but I was hoping that we might accomplish the goal here without adding a rarely-used flag to BlockwiseDep.

I took a crack at what this could look like without modifying blockwise at all (see commit here). In short, it creates a pass-through vanilla blockwise layer that maps a delayed key into a block index. This almost does what I want, but it messes up the dependencies when doing blockwise optimization (compute(optimize_graph=False) succeeds). So then I found myself wanting to add a flag to my BlockwiseDepDict to handle the dependencies differently, and then I'm back to what you've done here :).

But perhaps there is still a way to get the dependency information needed off of full_graph in the optimization phase? I'm not sure, and I've probably missed something important.

@rjzamora

rjzamora commented Apr 6, 2022

Copy link
Copy Markdown
Member Author

@ian-r-rose - Thanks for taking a look at this! (sorry for the delay - I was off last week)

I took a crack at what this could look like without modifying blockwise at all (see commit here). In short, it creates a pass-through vanilla blockwise layer that maps a delayed key into a block index. This almost does what I want, but it messes up the dependencies when doing blockwise optimization (compute(optimize_graph=False) succeeds). So then I found myself wanting to add a flag to my BlockwiseDepDict to handle the dependencies differently, and then I'm back to what you've done here :).

Yep - That is pretty much the same process I went through :)

But perhaps there is still a way to get the dependency information needed off of full_graph in the optimization phase? I'm not sure, and I've probably missed something important.

I'll probably try to give this another go today or tomorrow, but I'm still very open to suggestions (note that I left this as "Draft", because I was still hoping I had missed something obvious).

I do think it is also worth sharing that I see this kind of improvement as a way to reduce the number of cases we need to explicitly optimize/support in to/read_parquet (and other IO API). Right now, the primary drawbacks of using something like from_delayed (with delayed read_parquet/csv/etc tasks) are:

  1. You loose out on HLG task fusion, and are thus much more likely to run into memory issues.
  2. You are responsible for specifying the divisions yourself if you want them to be defined

Moving from_delayed to Blockwise doesn't help with the second point, but it does resolve the first issue.

This leads me to your question @rajeee - I do expect this change to improve memory requirements, but not necessarily for the original form of your stack-overflow question. As a result of point (2) above, you would need to also supply the divisions= argument to from_delayed. Otherwise the repartition operation will likely do something painful (because the initial divisions would be unknown).

@rajeee

rajeee commented Apr 6, 2022

Copy link
Copy Markdown

This leads me to your question @rajeee - I do expect this change to improve memory requirements, but not necessarily for the original form of your stack-overflow question. As a result of point (2) above, you would need to also supply the divisions= argument to from_delayed. Otherwise the repartition operation will likely do something painful (because the initial divisions would be unknown).

Specifying division in the from_delayed isn't a big deal. I was kind of already doing that in the next line in my SO question. Let me know when this is sort of ready to test; I am very curious to test this on my dataset that was failing previously and see if it succeeds this time.

@ian-r-rose

Copy link
Copy Markdown
Collaborator

Yep - That is pretty much the same process I went through :)

Yeah, figured as much :). I played with another experiment here which more-or-less works by embedding the delayed dsks as subgraphs, and pulling a similar trick to SubgraphCallable. I don't necessarily feel like it's a great solution, but it does keep some of the from_delayed logic a bit more encapsulated.

All of this is in the service of trying to avoid giving BlockwiseDeps dependencies, I suppose. Maybe I would feel better about this approach if BlockwiseDep had a more generic set of explicit dependencies, rather than special casing Delayed objects.

@rjzamora

rjzamora commented Apr 6, 2022

Copy link
Copy Markdown
Member Author

I played with another experiment here which more-or-less works by embedding the delayed dsks as subgraphs, and pulling a similar trick to SubgraphCallable. I don't necessarily feel like it's a great solution, but it does keep some of the from_delayed logic a bit more encapsulated.

Nice - Does this work when a list of Futures are passed to from_delayed? I guess it may work, but would need to duplicate the computation.

@ian-r-rose

Copy link
Copy Markdown
Collaborator

Nice - Does this work when a list of Futures are passed to from_delayed? I guess it may work, but would need to duplicate the computation.

I don't think so -- to be clear, I'm not really proposing the above, just playing around to try to understand the constraints better. Another variant (that I was hoping to avoid) would be to make core blockwise or map_partitions so simple to use that it could more-or-less replace from_delayed, or from_delayed could become a thin wrapper around it in the easy cases:

ddf = dask.dataframe.io.map_partitions(read_files, files_groups)

@rjzamora

rjzamora commented Apr 7, 2022 •

Copy link
Copy Markdown
Member Author

@ian-r-rose - Got it. I share your motivation to move in the direction of simplicity :)

For the case of building a collection from delayed/future objects, it really feels like we need to tweak BlockwiseDep in some way to capture the posibility of it returning "valid" key dependencies. My last commit attempts to do this by adding a valid property to BlockwiseDep. I admit that this property is unlikely to make sense for non-Delayed dependencies, but I suppose it could also be used to return key-dependency names that do not conform to typical collection names (e.g. (<name>-<token>, (23,))).

ddf = dask.dataframe.io.map_partitions(read_files, files_groups)

You make a good point that we could also consider implementing/pushing simpler alternatives to from_delayed, but I do suspect that we will need something similar to the changes in this PR to handle collection generation from general delayed/future objects.

@rjzamora
rjzamora marked this pull request as ready for review April 7, 2022 16:35

@ian-r-rose ian-r-rose left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rjzamora, I do like how this is getting simpler.

I think I'm coming around to your implementation, even if I don't understand everything yet.

Comment thread dask/blockwise.py Resolved
Comment thread dask/blockwise.py Resolved
Comment thread dask/blockwise.py Resolved
Comment thread dask/blockwise.py Outdated
Comment on lines +69 to +70
def valid(self) -> bool:
"""Whether this object will return "valid" dependency keys.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way of writing this might be something along the lines of produces_keys, akin to produces_tasks. I know we are getting rid of produces_tasks, but to me it's a clearer way of communicating what this means.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay - produces_tasks seems reasonable to me.

Comment thread dask/blockwise.py Outdated
Comment on lines +1367 to +1375
valid_keys = product(
*(
list(range(nbi))
for nbi in layers[layer].io_deps[k].numblocks
)
)
new_deps |= {
layers[layer].io_deps[k][valid_key] for valid_key in valid_keys
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I wrong to be thinking that a simpler way to get the set of valid deps would be to read them off of full_graph.dependencies?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grr - I'm still trying to work this one out. We certainly can pull out the valid dependencies from the intial version of full_graph, but this can become tricky by the time we get to the final version of full_graph (since this function is called somewhat recursively).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay - I think (hope) d7ca3f1 does what you had in mind. (Although, I may need to clean up the exact code I used)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rjzamora, that is what I had in mind.

I'm not 100% sure it's better, but to me it feels more idiomatic to not explicitly iterate over the keys for a non-materialized graph at this stage. What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do get the sense that it is better. It also seems like it is the "correct" thing to do even without the new produces_keys feature. For example, I am now realizing that we may be able to remove the block below it:

                elif k not in io_names:
                    new_deps.add(k)

because these dependencies should already be in new_deps as well.

@rjzamora rjzamora Apr 8, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. I was able to just replace the original new_deps logic by pulling the dependencies straight from full_graph.dependencies. This seems like a nice improvement over the original _optimize_blockwise code anyway (hope CI passes). Scratch that - Seems that we still need to handle some edge cases for now (rolling that second change back).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, bummer. Thanks for checking

@rjzamora
rjzamora requested a review from ian-r-rose April 20, 2022 14:08

@ian-r-rose ian-r-rose left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rjzamora, I'm happy with where this stands, and it wouldn't be too difficult to modify it down the line.

I don't fully see all the consequences of using fuse_roots like this. It's interesting: this is similar to where we were before from_array became blockwise.

Comment thread dask/blockwise.py Resolved
Comment thread dask/dataframe/io/io.py Resolved
)
+ 1
)
# NOTE: Fusion requires `optimize_blockwise`` and `fuse_roots`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't really appreciated this. At the end of this, we are fully materialized. Are there cases where it would be a mistake to do that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great point. I had honestly forgotten that fuse_roots will return a materialized Layer, but that makes sense. Although we may be able to rewrite fuse roots to avoid materialization, we may not get much out of that. What we really want is the graph to remain HLG when dsk.cull is called. So, perhaps we just want to move the cull operation to occur in between optimize_blockwise and fuse_roots? I'll throw up a small separate PR to check if this idea breaks anything :)

@rjzamora

rjzamora commented Apr 22, 2022 •

Copy link
Copy Markdown
Member Author

Thanks for the review @ian-r-rose - I'll plan to merge this later today if there aren't any other concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dataframe highlevelgraph Issues relating to HighLevelGraphs. io

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants