-
Notifications
You must be signed in to change notification settings - Fork 915
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
Add multi-partition DataFrameScan
support to cuDF-Polars
#17441
Merged
rapids-bot
merged 31 commits into
rapidsai:branch-25.02
from
rjzamora:cudf-polars-multi-dataframe-scan
Dec 3, 2024
Merged
Changes from 9 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
f651515
add multi-partition DataFrameScan IR node
rjzamora 17fa65a
add multi-partition DataFrameScan IR node
rjzamora e7e2a37
avoid redirection
rjzamora b587ea3
Merge remote-tracking branch 'upstream/branch-25.02' into cudf-polars…
rjzamora 7da3209
adjust coverage
rjzamora 7acdee2
pull in _from_pydf change needed by 17364
rjzamora dcede57
Merge branch 'branch-25.02' into cudf-polars-multi-dataframe-scan
rjzamora 69a76ee
avoid reconstruction (future concern)
rjzamora a7be622
Merge branch 'cudf-polars-multi-dataframe-scan' of github.com:rjzamor…
rjzamora c311590
apply code review suggestion
rjzamora f6bb5d1
Merge remote-tracking branch 'upstream/branch-25.02' into cudf-polars…
rjzamora c8af6fc
roll back unnecessary changes
rjzamora a765cbc
rename to executor_options
rjzamora b18121b
fix coverage
rjzamora 325051b
Merge remote-tracking branch 'upstream/branch-25.02' into cudf-polars…
rjzamora 1be7228
improve test coverage
rjzamora 36f59f1
split logic into dedicated io.py file
rjzamora 0b63126
remove __init__.py additions
rjzamora c6eb1b8
Apply suggestions from code review
rjzamora 35e5493
Merge remote-tracking branch 'upstream/branch-25.02' into cudf-polars…
rjzamora 5cfef05
Merge branch 'branch-25.02' into cudf-polars-multi-dataframe-scan
rjzamora c031b01
Merge branch 'cudf-polars-multi-dataframe-scan' of github.com:rjzamor…
rjzamora 93f6a86
fix code suggestions
rjzamora 646ddba
refactor to avoid circular imports
rjzamora e0929e4
fix test coverage
rjzamora 46531aa
Merge branch 'branch-25.02' into cudf-polars-multi-dataframe-scan
rjzamora 8fb2833
remove problematic default mapping
rjzamora 925cb47
Merge remote-tracking branch 'upstream/branch-25.02' into cudf-polars…
rjzamora 503ad59
improve comment
rjzamora 043d268
move back lower_ir_node default
rjzamora 332ced3
Merge remote-tracking branch 'upstream/branch-25.02' into cudf-polars…
rjzamora 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 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 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 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 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,82 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""Parallel IO Logic.""" | ||
|
||
from __future__ import annotations | ||
|
||
import math | ||
from functools import cached_property | ||
from typing import TYPE_CHECKING, Any | ||
|
||
import polars as pl | ||
|
||
from cudf_polars.dsl.ir import DataFrameScan | ||
from cudf_polars.experimental.parallel import ( | ||
PartitionInfo, | ||
generate_ir_tasks, | ||
get_key_name, | ||
lower_ir_node, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import MutableMapping | ||
|
||
from cudf_polars.dsl.ir import IR | ||
from cudf_polars.experimental.parallel import LowerIRTransformer | ||
|
||
|
||
## | ||
## DataFrameScan | ||
## | ||
|
||
|
||
class ParDataFrameScan(DataFrameScan): | ||
"""Parallel DataFrameScan.""" | ||
|
||
@property | ||
def _max_n_rows(self) -> int: | ||
"""Row-count threshold for splitting a DataFrame.""" | ||
parallel_options = self.config_options.get("parallel_options", {}) | ||
return parallel_options.get("num_rows_threshold", 1_000_000) | ||
|
||
@cached_property | ||
def _count(self) -> int: | ||
"""Partition count.""" | ||
total_rows = max(self.df.shape()[0], 1) | ||
return math.ceil(total_rows / self._max_n_rows) | ||
|
||
def _tasks( | ||
self, partition_info: MutableMapping[IR, PartitionInfo] | ||
) -> MutableMapping[Any, Any]: | ||
"""Task graph.""" | ||
assert ( | ||
partition_info[self].count == self._count | ||
), "Inconsistent ParDataFrameScan partitioning." | ||
total_rows = max(self.df.shape()[0], 1) | ||
stride = math.ceil(total_rows / self._count) | ||
key_name = get_key_name(self) | ||
return { | ||
(key_name, i): ( | ||
self.do_evaluate, | ||
self.schema, | ||
pl.DataFrame._from_pydf(self.df.slice(offset, stride)), | ||
self.projection, | ||
self.predicate, | ||
) | ||
for i, offset in enumerate(range(0, total_rows, stride)) | ||
} | ||
|
||
|
||
@lower_ir_node.register(ParDataFrameScan) | ||
def _( | ||
ir: ParDataFrameScan, rec: LowerIRTransformer | ||
) -> tuple[IR, MutableMapping[IR, PartitionInfo]]: | ||
# Avoid reconstruction if we need to re-lower | ||
return ir, {ir: PartitionInfo(count=ir._count)} # pragma: no cover | ||
|
||
|
||
@generate_ir_tasks.register(ParDataFrameScan) | ||
def _( | ||
ir: ParDataFrameScan, partition_info: MutableMapping[IR, PartitionInfo] | ||
) -> MutableMapping[Any, Any]: | ||
return ir._tasks(partition_info) |
This file contains 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 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
43 changes: 43 additions & 0 deletions
43
python/cudf_polars/tests/experimental/test_dataframescan.py
This file contains 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,43 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
import polars as pl | ||
|
||
from cudf_polars import Translator | ||
from cudf_polars.experimental.parallel import lower_ir_graph | ||
from cudf_polars.testing.asserts import assert_gpu_result_equal | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def df(): | ||
return pl.LazyFrame( | ||
{ | ||
"x": range(30_000), | ||
"y": ["cat", "dog", "fish"] * 10_000, | ||
"z": [1.0, 2.0, 3.0, 4.0, 5.0] * 6_000, | ||
} | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("num_rows_threshold", [1_000, 1_000_000]) | ||
def test_parallel_dataframescan(df, num_rows_threshold): | ||
total_row_count = len(df.collect()) | ||
engine = pl.GPUEngine( | ||
raise_on_fail=True, | ||
parallel_options={"num_rows_threshold": num_rows_threshold}, | ||
executor="dask-experimental", | ||
) | ||
assert_gpu_result_equal(df, engine=engine) | ||
|
||
# Check partitioning | ||
qir = Translator(df._ldf.visit(), engine).translate_ir() | ||
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. nit: we need to remember to check that we didn't get any errors. I will t ry and open a PR that does this automatically. |
||
ir, info = lower_ir_graph(qir) | ||
count = info[ir].count | ||
if num_rows_threshold < total_row_count: | ||
assert count > 1 | ||
else: | ||
assert count == 1 |
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'd like to nest all multi-gpu options within the
"parallel_options"
moving forward (to avoid adding more top-level keys).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.
We might imagine that these options are executor-specific, does it make sense to have a nesting that is:
So the executor argument is either a name, or a
("name", name-specific-options)
?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.
That seems fine to me. Any opinion on this @pentschev ?
I do think it's a good idea to consider how the number of these options will inevitably grow over time (and that they will probably be executor-specific).
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.
Hmm. The
str | tuple[str, dict]
logic actually feels a bit clumsy when I think about how to implement it.How about we just rename
"parallel_options"
to"executor_options"
(to make it clear that the options are executor-specific)? This still allows us to validate that the specified arguments are actually supported by the "active" executor.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.
As much as I agree that it is indeed clumsy it feels like we'll soon need to have nested options and inevitably make
"executor_options"
require acceptingstr | tuple[str, dict]
, so we may as well just do that inexecutor
and with that allow as many levels of nested options as needed as part ofexecutor
. I think a better alternative may be an abstract base classExecutor
that we can specialize with the options we need for each executor.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 do think this is the best long-term solution, but I also don't think it will be difficult to migrate from the
"executor_options"
approach currently used in this PR.I don't think I understand why it is inevitable that
"executor_options"
would need to acceptstr | tuple[str, dict]
. However, I do see why it would be useful to attach all executor-specific options to anExecutor
object. That said, I don't really want to deal with serialization/etc in this PR :)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.
It's possible I'm overestimating the amount of options we'll end up introducing here, but once we need nested options we'll need something more complex like the
tuple[str, dict]
, or the abstract base class. Thus why I think it's inevitable.