Skip to content
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 partition-wise Select support to cuDF-Polars #17495

Merged
merged 17 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions python/cudf_polars/cudf_polars/experimental/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from functools import reduce
from typing import TYPE_CHECKING, Any

import cudf_polars.experimental.io # noqa: F401
from cudf_polars.dsl.ir import IR, Cache, Projection, Union
import cudf_polars.experimental.io
import cudf_polars.experimental.select # noqa: F401
from cudf_polars.dsl.ir import IR, Cache, 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.dispatch import (
Expand Down Expand Up @@ -226,6 +227,8 @@ def _lower_ir_pwise(

lower_ir_node.register(Projection, _lower_ir_pwise)
lower_ir_node.register(Cache, _lower_ir_pwise)
lower_ir_node.register(Filter, _lower_ir_pwise)
lower_ir_node.register(HStack, _lower_ir_pwise)


def _generate_ir_tasks_pwise(
Expand All @@ -245,3 +248,6 @@ def _generate_ir_tasks_pwise(

generate_ir_tasks.register(Projection, _generate_ir_tasks_pwise)
generate_ir_tasks.register(Cache, _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)
60 changes: 60 additions & 0 deletions python/cudf_polars/cudf_polars/experimental/select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: Apache-2.0
"""Parallel Select Logic."""

from __future__ import annotations

from typing import TYPE_CHECKING

from cudf_polars.dsl.ir import Select
from cudf_polars.dsl.traversal import traversal
from cudf_polars.experimental.base import PartitionInfo
from cudf_polars.experimental.dispatch import 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


_PARTWISE = (
"Literal",
"LiteralColumn",
"Col",
"ColRef",
rjzamora marked this conversation as resolved.
Show resolved Hide resolved
"BooleanFunction",
rjzamora marked this conversation as resolved.
Show resolved Hide resolved
"StringFunction",
"TemporalFunction",
"Filter",
"Cast",
"Ternary",
"BinOp",
"UnaryFunction",
rjzamora marked this conversation as resolved.
Show resolved Hide resolved
)
rjzamora marked this conversation as resolved.
Show resolved Hide resolved


@lower_ir_node.register(Select)
def _(
ir: Select, rec: LowerIRTransformer
) -> tuple[IR, MutableMapping[IR, PartitionInfo]]:
(child,) = ir.children
child, partition_info = rec(child)
new_node = ir.reconstruct([child])

# Search the expression graph for "complex" operations
for ne in ir.exprs:
for expr in traversal(ne.value):
rjzamora marked this conversation as resolved.
Show resolved Hide resolved
if type(expr).__name__ not in _PARTWISE:
# TODO: Handle non-partition-wise expressions.
if partition_info[child].count > 1:
raise NotImplementedError(
f"Expr {type(expr)} does not support multiple partitions."
)
else: # pragma: no cover
partition_info[new_node] = PartitionInfo(count=1)
return new_node, partition_info
rjzamora marked this conversation as resolved.
Show resolved Hide resolved

# Remaining Select ops are partition-wise
partition_info[new_node] = PartitionInfo(count=partition_info[child].count)
return new_node, partition_info
54 changes: 54 additions & 0 deletions python/cudf_polars/tests/experimental/test_select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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.testing.asserts import assert_gpu_result_equal


@pytest.fixture(scope="module")
def engine():
return pl.GPUEngine(
raise_on_fail=True,
executor="dask-experimental",
executor_options={"max_rows_per_partition": 3},
)


@pytest.fixture(scope="module")
def df():
return pl.LazyFrame(
{
"a": [1, 2, 3, 4, 5, 6, 7],
"b": [1, 1, 1, 1, 1, 1, 1],
}
)


def test_select(df, engine):
query = df.select(
pl.col("a") + pl.col("b"), (pl.col("a") * 2 + pl.col("b")).alias("d")
)
assert_gpu_result_equal(query, engine=engine)


def test_select_reduce_raises(df, engine):
query = df.select(
(pl.col("a") + pl.col("b")).max(),
(pl.col("a") * 2 + pl.col("b")).alias("d").mean(),
)
with pytest.raises(
pl.exceptions.ComputeError,
match="NotImplementedError",
):
assert_gpu_result_equal(query, engine=engine)
Comment on lines +44 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer assert_ir_translation_raises for these kind of things, I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parallel execution is currently independent of IR translation. When we raise an error, it's because we ran into a non-"pointwise" Select operation (with multiple partitions) after the IR was already translated successfully.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, thanks.

I think for now this is fine, perhaps in the parallel execution environment we don't want "early/eager" fallback. But it might be worthwhile thinking about.

We can think of this lowering as another step in the "IR translation" phase.



def test_select_with_cse_no_agg(df, engine):
expr = pl.col("a") + pl.col("a")
query = df.select(expr, (expr * 2).alias("b"), ((expr * 2) + 10).alias("c"))
assert_gpu_result_equal(query, engine=engine)
Loading