Repository navigation
Conversation
|
Nice work @bryanwweber - This is a pretty complicated problem.
I could be missing something, but I do think we should avoid defining a stand-alone graph/collection for the index. If the chunks are known, then it seems like we should absolutely try to calculate the local range index in the same task that converts the underlying chunk array into a DataFrame partition. For example, it wouldn't work now, but it would be nice if we could just just use a |
rjzamora
left a comment
There was a problem hiding this comment.
Thanks again for this Bryan - Perhaps these suggestions will help?
|
@rjzamora Thanks for the suggestion! @ian-r-rose and I talked about this, I had considered creating a new |
Yeah - I suppose you could define all this logic in a |
This condition is checked in _meta_from_array, so this line of code is never covered.
Splits several large tests that have different goals. Uses match keyword argument to pytest.raises.
Co-authored-by: Richard (Rick) Zamora <[email protected]>
| def test_create_index_is_blockwise(): | ||
| x = da.ones((10, 3), chunks=(4, 2)) | ||
| # The index will be a range from 0-10 with three partitions. Each partition index's | ||
| # starting value will be 4 higher than the previous one since chunks=(4, 2). | ||
| index_mapping = { | ||
| (0,): (0, 4), | ||
| (1,): (4, 8), | ||
| (2,): (8, 10), | ||
| } | ||
| df = dd.from_dask_array(x, ["a", "b", "c"]) | ||
| layer = hlg_layer(df.dask, "from-dask-array") | ||
| # The layer to create the DataFrame should create the index using a BlockwiseDep | ||
| # If that is the case, then the io_deps dictionary will not be empty. | ||
| assert layer.io_deps | ||
| # Also test that the BlockwiseDepDict mapping attribute corresponds to the | ||
| # RangeIndex that will be created for `x`. There's only one item in the io_deps | ||
| # dictionary here, so we avoid a loop with this line. This is specific to | ||
| # BlockwiseDepDict, so it'll break if from_dask_array is refactored to use a | ||
| # different means of generating the index. | ||
| blk_dep = next(iter(layer.io_deps.values())) | ||
| assert blk_dep.mapping == index_mapping |
There was a problem hiding this comment.
@rjzamora Thanks for the suggestions! I adopted them as-is, other than linting 😄
I also added this test, which I'm about 80% sure is valuable. I wanted to assert that the layer creating the index is a HLG/blockwise, since that's what your change enabled. I think this test does that, but it does depend on the specific implementation using BlockwiseDepDict which is... not great? At least, it'll break if the implementation is changed, which IMO means it's not a great test. Happy for your thoughts though!
There was a problem hiding this comment.
I think I agree with your intuition that checking for BlockwiseDepDict usage is not ideal, because we may decide to use a different kind of BlockwiseDep or tweak the design in the future. I do like how thorough this test is, but my preference would be to simply assert that isisntance(layer, Blockwise) here, and to rely on the assert_eq in other tests to ensure that the index is being set correctly.
One other thing you can test is whether dask.blockwise.optimize_blockwise fuses everything down to one Blockwise layer. For example:
from dask.blockwise import Blockwise, optimize_blockwise
graph = optimize_blockwise(df.dask, df.__dask_keys__())
assert len(graph.layers) == 1
assert isinstance(hlg_layer(graph, "from-dask-array"), Blockwise)(note: I didn't check that this works yet)
There was a problem hiding this comment.
rely on the
assert_eqin other tests to ensure that the index is being set correctly.
I'm not worried here about the index being set correctly, I'm trying to test that the index is being created by a BlockwiseDep. I also have other tests that check whether or not layer is an HLG at least, the ones that are like assert not hlg_layer_topographical(df.dask, -1).is_materialized(). Unfortunately, the .is_materialized() is true even if the index creation is done non-Blockwise, that is, those tests passed before I adopted your changes. So I was looking for something deeper in the HLG to really assert that the index was a BlockwiseDep.
The easiest way to do that is to assert all(isinstance(dep, BlockwiseDep) for dep in layer.io_deps.values()). I didn't want to import BlockwiseDep into the test module directly though, since it felt icky (don't have a better word 😄) and too specific again. I also considered materializing the graph in this test and checking there (I'm not sure if the BlockwiseDep would be materialized into the low-level graph), but this worked, so I figured I'd push it up for feedback 😄
I think it's not super important to really assert that the index is generated by a BlockwiseDep, but it also felt weird to apply a bunch of changes to the code and not check that they were doing what they were supposed to be doing.
One other thing you can test is whether
dask.blockwise.optimize_blockwisefuses everything down to oneBlockwiselayer.
Happy to add a test for this! Just a note that the BlockwiseDepDict doesn't show up in the layers though, so this would be an assertion that Blockwise is doing what it's supposed to, which is presumably covered elsewhere.
There was a problem hiding this comment.
I agree with both you and @rjzamora that delving into the specific shape of BlockwiseDepDict is not ideal, and is best left as an implementation detail.
assert_eq to verify the index still seems like a good idea here: if the range index construction is wrong then this would catch it.
Unfortunately, the .is_materialized() is true even if the index creation is done non-Blockwise
I'm not sure I understand what you mean here, can you elaborate? We should be able to verify some things about the HLG before materialization.
There was a problem hiding this comment.
I think it's not super important to really assert that the index is generated by a BlockwiseDep, but it also felt weird to apply a bunch of changes to the code and not check that they were doing what they were supposed to be doing.
Right - I would argue that it is not important at all that the index is generated by a BlockwiseDep, and so there is no reason to test this. We only care that from_dask_array produces a single Blockwise layer. The fact that we use BlockwiseDep for the index is just to make this possible.
There was a problem hiding this comment.
I would argue that it is not important at all that the index is generated by a BlockwiseDep, and so there is no reason to test this.
Thanks @rjzamora, I think it makes sense to drop the test then.
Unfortunately, the .is_materialized() is true even if the index creation is done non-Blockwise
I'm not sure I understand what you mean here, can you elaborate? We should be able to verify some things about the HLG before materialization.
@ian-r-rose Sorry, that was a brain fart. I meant hlg_layer_topological(df.dask, -1).is_materialized() is False, so that assert not ... is True and the test passes. This happened even when I was constructing the low-level graph directly (dsk = {} ...), because the from-dask-array task hadn't been materialized even though its dependencies were created as a materialized graph. I don't recall what happens if I try to check all the layers though, but I think the point is moot now since I agree with Rick that it makes sense to drop this test as being too specific to the implementation.
ian-r-rose
left a comment
There was a problem hiding this comment.
This is looking really great @bryanwweber
| def test_create_index_is_blockwise(): | ||
| x = da.ones((10, 3), chunks=(4, 2)) | ||
| # The index will be a range from 0-10 with three partitions. Each partition index's | ||
| # starting value will be 4 higher than the previous one since chunks=(4, 2). | ||
| index_mapping = { | ||
| (0,): (0, 4), | ||
| (1,): (4, 8), | ||
| (2,): (8, 10), | ||
| } | ||
| df = dd.from_dask_array(x, ["a", "b", "c"]) | ||
| layer = hlg_layer(df.dask, "from-dask-array") | ||
| # The layer to create the DataFrame should create the index using a BlockwiseDep | ||
| # If that is the case, then the io_deps dictionary will not be empty. | ||
| assert layer.io_deps | ||
| # Also test that the BlockwiseDepDict mapping attribute corresponds to the | ||
| # RangeIndex that will be created for `x`. There's only one item in the io_deps | ||
| # dictionary here, so we avoid a loop with this line. This is specific to | ||
| # BlockwiseDepDict, so it'll break if from_dask_array is refactored to use a | ||
| # different means of generating the index. | ||
| blk_dep = next(iter(layer.io_deps.values())) | ||
| assert blk_dep.mapping == index_mapping |
There was a problem hiding this comment.
I agree with both you and @rjzamora that delving into the specific shape of BlockwiseDepDict is not ideal, and is best left as an implementation detail.
assert_eq to verify the index still seems like a good idea here: if the range index construction is wrong then this would catch it.
Unfortunately, the .is_materialized() is true even if the index creation is done non-Blockwise
I'm not sure I understand what you mean here, can you elaborate? We should be able to verify some things about the HLG before materialization.
Add a docstring explaining the types of the arguments.
|
@ian-r-rose @rjzamora I just pushed an update that drops the too-specific test and moves |
rjzamora
left a comment
There was a problem hiding this comment.
Thanks again for working on this @bryanwweber
I tried adding an optimize_blockwise test to your branch, but quickly learned that this is technically a "reduction" (since "from-dask-array-*" technically depends on two "collections"), and blockwise fusion does not work for such a case :/
It is not immediately clear to me "how" we could go about achieving blockwise fusion in this case, but perhaps that doesn't gain us much anyway. Either way, this PR is a nice improvement.
| if is_series_like(meta): | ||
| kwargs = {"dtype": x.dtype, "name": meta.name, "initializer": type(meta)} | ||
| else: | ||
| kwargs = {"columns": meta.columns, "initializer": type(meta)} |
There was a problem hiding this comment.
Just a note that the index and intializer can both be defined in arrays_and_indices, rather than being included as kwargs. However, I don't think that gains us any readability or performance, so I'm good with what you already have.
There was a problem hiding this comment.
Thanks @rjzamora! I wasn't sure what the indices for type(meta) would be, presumably the same as the input? In any case, I'm glad to know that's possible for the future.
There was a problem hiding this comment.
I believe if they are scalars then the indices would be None. But I also think it's fine as-is in kwargs
ian-r-rose
left a comment
There was a problem hiding this comment.
Left a few nitpicky comments, which I think are optional, but I'm happy where this stands now!
That's unfortunate! In what way does it depend on two collections? It would be nice, for instance, to be able to fuse |
Exactly my thought. I suppose you do get full fusion if there is not |
|
Thanks @ian-r-rose! I added the two changes you suggested 👍 I'm seeing (seemingly) unrelated and random failures in parts of the test suite here on CI. One time was on Ubuntu/py3.10, the next was on Windows/py3.10... |
That's really unfortunate, how hard do you think it would be to remove that restriction? I suppose we are talking about this block? Lines 1293 to 1305 in 9000abd |
Yep - That's exactly where we loose the optimization. I'll probably need to think a bit about this one. There is probably a good reason why we have avoided fusing reductions so fare, but it definitely seems like something we should be able to do (even if it is for a very restricted set of cases...). |
|
From the git blame: #4207 I'll bet we can (at the very least) make an exception for |
|
(I should note, I see no reason to block this PR on the above discussion) |
|
FYI I'm done with changes and all tests are green |
|
Thanks for the nice improvement @bryanwweber ! We can (hopefully) address blockwise fusion in a follow-up. |
@ian-r-rose - Just to follow up on this. It does seem that the "reduction" issue is easy to resolve. However, there is also a trickier problem that I didn't see before: The need for
I'll have to think if there is a way around this for the general case. We can certainly avoid setting |
from_dask_arraymaterializes the graph unnecessarily #8496pre-commit run --all-filesThis change uses
dask.blockwiseto generate the high-level graph fordask.dataframe.from_dask_array. If no index is given and the chunks are known, the graph to generate an index is still created directly. I don't see a way around this, but suggestions are very welcome! cc @ian-r-rose @gjoseph92 @rjzamora