Repository navigation
Description
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).
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.
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!)
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).
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:
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?




Activity
madsbk commentedon Jan 17, 2022
I don't think this is necessary, we did it to make the implementation of
layer.cull()simpler.The cull functions use
all_hlg_keysto identify keys.gjoseph92 commentedon Jan 20, 2022
So would it be possible to rewrite the culling process to not require any layers to generate all their keys?
martindurant commentedon Feb 10, 2022
Totally agree with @gjoseph92 , I was under the impression that there were two major benefits of HLGs in general:
jakirkham commentedon Feb 10, 2022
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)
martindurant commentedon Feb 11, 2022
@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.
timblakely commentedon Sep 20, 2022
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.d-v-b commentedon Sep 20, 2022
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.
martindurant commentedon Sep 20, 2022
+1+1+1
Given that, conceptually, an array layer is like a dict comprehension
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.
gjoseph92 commentedon Sep 20, 2022
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?
rjzamora commentedon Sep 20, 2022
Once serialization is simplified in dask/distributed#6028 (or something else similar?), I'm planning to remove the
Mappingbackend fromLayerand remove bothLayer.cullandHighLevelGraph.cullaltogether. My (strong-ish) opinion is that we should make it impossible to "accidentally" materialize aLayer, by not supportingMapping-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.r-pastor commentedon Apr 17, 2025
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:
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_blocksattribute of theBlockwiselayer would make theget_output_keyscall 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 :)
fix(py): reduce dask task explosion for large tiled TIFF files
fix(py): reduce dask task explosion for large tiled TIFF files