Skip to content

Culling massive Blockwise graphs is very slow, not constant-time #8570

Open

Description

@gjoseph92

tl;dr: Could get_all_external_keys and get_output_keys be avoided in HLG culling?

In some workflows, it can be desirable to create a dask Array/DataFrame structure representing some full-size, enormous dataset, then immediately use slicing to sub-select out only a tiny part of it, then work with that. Now that we can use Blockwise for IO (xref #7417), this is an especially appealing pattern, because it should be constant-time to construct the massive graph, since nothing has to be materialized.

I had hoped it would also be linear-time to cull this massive graph, but it appears currently that it's not.

Here's an example where I'm trying to create an xarray representing the Landsat-8 collection at full resolution over the entire continental US. This is a 10PB, 136-million-chunk array that involves 1.3 billion data loading tasks. Here I'm using gjoseph92/stackstac#116 (so the data loading graph is fully blockwise) and #8560 (so we know fuse_roots isn't materializing the graph unnecessarily).

Screen Shot 2022-01-14 at 10 37 54 AM

Then I'm sub-selecting a single chunk out of those 136 million. Based on what I know of the graph, this should cull down to 4 tasks.

Screen Shot 2022-01-14 at 10 37 59 AM

Here's the HLG for reference

You can see the first layer is materialized with ~100,000 tasks, but the big, 100-million-task one is Blockwise. (#8497 sure would be nice here!)

Screen Shot 2022-01-14 at 10 30 55 AM

But when I try to optimize this graph, I see memory usage shoot up until it crashes the kernel on the 32GB machine. Interrupting the kernel after a few seconds makes it pretty clear what's going on: HighLevelGraph.cull is calling get_all_external_keys, which is forcing the generation of all 1.3 billion keys (or 136 million keys? not sure).

Screen Shot 2022-01-14 at 10 23 19 AM

Even if it didn't call get_all_external_keys, I see that HighLevelGraph.cull is still calling get_output_keys on every layer. For reference:

dask/dask/highlevelgraph.py

Lines 944 to 970 in 358a5e3

keys_set = set(flatten(keys))
all_ext_keys = self.get_all_external_keys()
ret_layers = {}
ret_key_deps = {}
for layer_name in reversed(self._toposort_layers()):
layer = self.layers[layer_name]
# Let's cull the layer to produce its part of `keys`.
# Note: use .intersection rather than & because the RHS is
# a collections.abc.Set rather than a real set, and using &
# would take time proportional to the size of the LHS, which
# if there is no culling can be much bigger than the RHS.
output_keys = keys_set.intersection(layer.get_output_keys())
if output_keys:
culled_layer, culled_deps = layer.cull(output_keys, all_ext_keys)
# Update `keys` with all layer's external key dependencies, which
# are all the layer's dependencies (`culled_deps`) excluding
# the layer's output keys.
external_deps = set()
for d in culled_deps.values():
external_deps |= d
external_deps -= culled_layer.get_output_keys()
keys_set |= external_deps
# Save the culled layer and its key dependencies
ret_layers[layer_name] = culled_layer
ret_key_deps.update(culled_deps)

Why is it necessary for HLG.cull to ask the layer for all its keys, intersect them itself, then pass that back into the layer? And why is it necessary for cull functions to take all_hlg_keys?

I had thought the interface would be simply: HLG.cull tells each layer the necessary output keys; the layer figures out the rest on its own. If it needs to generate all its keys internally and do that intersection, fine, but for layers that don't need to do this, shouldn't the optimization be available?

@rjzamora @madsbk why does culling work this way? Would it be possible to write Blockwise culling without this, in a way that's truly linear-time to only the number of final keys?

cc @ian-r-rose @TomAugspurger

Activity

  1. added
    highlevelgraph
    on Jan 14, 2022
  2. madsbk commented on Jan 17, 2022

    @madsbk
    Contributor

    Why is it necessary for HLG.cull to ask the layer for all its keys, intersect them itself, then pass that back into the layer?

    I don't think this is necessary, we did it to make the implementation of layer.cull() simpler.

    And why is it necessary for cull functions to take all_hlg_keys?

    The cull functions use all_hlg_keys to identify keys.

  3. gjoseph92 commented on Jan 20, 2022

    @gjoseph92
    CollaboratorAuthor

    So would it be possible to rewrite the culling process to not require any layers to generate all their keys?

  4. martindurant commented on Feb 10, 2022

    @martindurant
    Member

    Totally agree with @gjoseph92 , I was under the impression that there were two major benefits of HLGs in general:

    • smaller transfer size, since we do something similar to using a generator/comprehension rather than a concrete set of keys and tasks
    • that you can select/update from a large graph at no cost, effectively changing the start/end of the comprehension implied in the first bullet.
    • (possibilities for higher-level optimisations)
  5. jakirkham commented on Feb 10, 2022

    @jakirkham
    Member

    I could be wrong about this, but given the proposed changes around HLG transfer ( dask/distributed#5581 ) there is an opportunity to improve transfer size (if more work is still needed there)

  6. martindurant commented on Feb 11, 2022

    @martindurant
    Member

    @jakirkham , that seems like an orthogonal discussion. In the case here, the HLG is expensive to construct before even starting to send it, even though in principle the selection should result in a small graph without having to iterate over the original. So it's point 2 in my list which is the surprising omission.

  7. timblakely commented on Sep 20, 2022

    @timblakely

    Ran into this as well. Was working with a 700k x 270k x 2k volume and even __setitem__-ing a small 5x5x5 slice was quite slow. Digging into it showed that it was enumerating >400k blocks to cull down to the single one it needed.

    Sounds like #9216 may help, but I'm not familiar enough with the inner working of Layers to know for sure.

  8. d-v-b commented on Sep 20, 2022

    @d-v-b
    Member

    I'm also frequently running into this issue. In particular, slow slicing prevents dask arrays from being a performant backend for chunk-aware data visualization tools like neuroglancer.

  9. martindurant commented on Sep 20, 2022

    @martindurant
    Member

    +1+1+1

    Given that, conceptually, an array layer is like a dict comprehension

    llg = {f"name_{keys}": (func, args) for keys in zip(axis_range0, axis_range1, ...)}
    

    where how to make the args is layer-specific, it should be possible to cull by changing the start/stop/step of the axes ranges. We should never be materialising the full set of keys.

  10. gjoseph92 commented on Sep 20, 2022

    @gjoseph92
    CollaboratorAuthor

    Yup, agreed this definitely shouldn't be the behavior, and the complexity of the current design makes this hard to avoid. I don't know if there's any upcoming work planned around this @jrbourbeau @ian-r-rose @rjzamora?

  11. rjzamora commented on Sep 20, 2022

    @rjzamora
    Member

    I don't know if there's any upcoming work planned around this @jrbourbeau @ian-r-rose @rjzamora?

    Once serialization is simplified in dask/distributed#6028 (or something else similar?), I'm planning to remove the Mapping backend from Layer and remove both Layer.cull and HighLevelGraph.cull altogether. My (strong-ish) opinion is that we should make it impossible to "accidentally" materialize a Layer, by not supporting Mapping-like attributes. Instead, the Layer should cull itself when it is explicitly materialized by the scheduler. I think a simple but strict Layer API can go a long way.

  12. added
    needs attention
    on Aug 19, 2024
  13. r-pastor commented on Apr 17, 2025

    @r-pastor

    Also ran into this problem recently while working on a web app that access a big archive of geospatial data.

    A unidimensional simplified example of the issue can be run with the following:

    da.full((1e8), fill_value=np.nan, chunks=(10))[0].compute()  # This takes a few seconds to complete

    As @gjoseph92 commented, the problem seems to be with the fact that all the keys of the graph are created when just one is actually needed.

    If we assume that when there is a layer on the graph performing a selection such as the getitem there is a lot of branches that can be pruned, there should be a way of optimizing the process by not creating such keys at all.

    I have briefly looked into the code with that assumption in mind and seems like setting the output_blocks attribute of the Blockwise layer would make the get_output_keys call produce just the desired tasks. The value to set such attribute can be retrieved from the getitem task dependency.
    In any case, my naive attempt of implementing it has broken a lot of tests. Furthermore I am not even sure if this is the correct track to solve the problem as the logic of the HLG cull and all the object involved is not straight forward to me.

    Could some of the maintainers provide some guidance on how to fix this issue? Happy to give it a try myself but I would need some support to get started :)

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

    highlevelgraphneeds attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions