-
Notifications
You must be signed in to change notification settings - Fork 942
Handle empty aggregations in multi-partition cudf.polars group_by #18277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TomAugspurger
wants to merge
20
commits into
rapidsai:branch-25.06
Choose a base branch
from
TomAugspurger:tom/groupby-empty
base: branch-25.06
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
81046c8
Handle empty aggregations in cudf.polars experimental group_by
TomAugspurger d0d0160
merge
TomAugspurger 7bf42bb
more tests
TomAugspurger f8163a5
Added test for single-partition groupby
TomAugspurger 48df984
Use distinct IR node for empty groupby
TomAugspurger 7f42f81
Improved repr for PartitionInfo
TomAugspurger 5c28c29
Multi-partition Distinct
TomAugspurger 81f9434
Added unique tests
TomAugspurger 37dce83
Merge
TomAugspurger 74a077c
remove unused special case
TomAugspurger e1ba97d
refactor engine
TomAugspurger f2d7085
Coverage
TomAugspurger 2b2131a
Remove redundant engine fixtures
TomAugspurger fd0eb84
Naming things
TomAugspurger 613b34f
Move shuffle to shuffle
TomAugspurger 9dd183a
move imports
TomAugspurger 5c1404d
revert
TomAugspurger 95d48a7
revert 613b34f73c9a9cd7c0f1e127454e3c115ffc84f4
TomAugspurger 2780f01
Fixed the shuffle move
TomAugspurger aef7f3f
Update import
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,18 +9,29 @@ | |
from functools import reduce | ||
from typing import TYPE_CHECKING, Any, ClassVar | ||
|
||
import cudf_polars.experimental.groupby | ||
import cudf_polars.experimental.io | ||
import cudf_polars.experimental.join | ||
import cudf_polars.experimental.select | ||
import cudf_polars.experimental.shuffle # noqa: F401 | ||
from cudf_polars.dsl.ir import IR, Cache, Filter, HStack, Projection, Select, Union | ||
from cudf_polars.dsl.expr import Col, NamedExpr | ||
from cudf_polars.dsl.ir import ( | ||
IR, | ||
Cache, | ||
Distinct, | ||
Filter, | ||
HStack, | ||
Projection, | ||
Select, | ||
Union, | ||
) | ||
from cudf_polars.dsl.traversal import CachingVisitor, traversal | ||
from cudf_polars.experimental.base import PartitionInfo, _concat, get_key_name | ||
from cudf_polars.experimental.base import ( | ||
PartitionInfo, | ||
_concat, | ||
get_key_name, | ||
) | ||
from cudf_polars.experimental.dispatch import ( | ||
generate_ir_tasks, | ||
lower_ir_node, | ||
) | ||
from cudf_polars.experimental.shuffle import _maybe_shuffle_frame | ||
from cudf_polars.utils.config import ConfigOptions | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import MutableMapping | ||
|
@@ -302,3 +313,46 @@ def _generate_ir_tasks_pwise( | |
generate_ir_tasks.register(Filter, _generate_ir_tasks_pwise) | ||
generate_ir_tasks.register(HStack, _generate_ir_tasks_pwise) | ||
generate_ir_tasks.register(Select, _generate_ir_tasks_pwise) | ||
generate_ir_tasks.register(Distinct, _generate_ir_tasks_pwise) | ||
|
||
|
||
@lower_ir_node.register(Distinct) | ||
def _( | ||
ir: Distinct, rec: LowerIRTransformer | ||
) -> tuple[IR, MutableMapping[IR, PartitionInfo]]: | ||
""" | ||
Lower a Distinct node. | ||
|
||
Distinct is implemented as a set of per-partition Distinct operations, | ||
followed by a Shuffle, followed by a final Distinct. | ||
""" | ||
child, partition_info = rec(ir.children[0]) | ||
subset = ir.subset or set(ir.schema) | ||
partitioned_on = tuple( | ||
NamedExpr(name, Col(dtype, name)) | ||
for name, dtype in ir.schema.items() | ||
if name in subset | ||
) | ||
|
||
output_count = partition_info[child].count | ||
if output_count == 1: | ||
new_node = ir.reconstruct([child]) | ||
partition_info[new_node] = PartitionInfo(count=1, partitioned_on=partitioned_on) | ||
return new_node, partition_info | ||
else: | ||
config_options = ConfigOptions({}) | ||
new_child = _maybe_shuffle_frame( | ||
child, | ||
partitioned_on, | ||
partition_info, | ||
config_options, | ||
output_count, | ||
) | ||
Comment on lines
+344
to
+350
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: For groupby we always do tree-reduce, here we're always doing shuffle. I guess both are fine, but maybe we want to be consistent? |
||
if child != new_child: | ||
ir = ir.reconstruct([new_child]) | ||
|
||
partition_info[ir] = PartitionInfo( | ||
count=output_count, partitioned_on=partitioned_on | ||
) | ||
|
||
return ir, partition_info |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
import polars as pl | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def engine(): | ||
""" | ||
Fixture for a `GPUEngine` with the `dask-experimental` executor. | ||
|
||
Sets the maximum number of rows per partition to 4. | ||
""" | ||
return pl.GPUEngine( | ||
raise_on_fail=True, | ||
executor="dask-experimental", | ||
executor_options={"max_rows_per_partition": 4}, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from __future__ import annotations | ||
|
||
from cudf_polars.experimental.base import PartitionInfo | ||
|
||
|
||
def test_partition_info_repr(): | ||
partition_info = PartitionInfo(count=10, partitioned_on=("a", "b")) | ||
assert repr(partition_info) == "PartitionInfo(count=10, partitioned_on=('a', 'b'))" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think removing these lines will break a lot of things, because we are registering dispatch functions in these modules.