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

Prevent pylibcudf serialization in cudf-polars #17449

Open
wants to merge 14 commits into
base: branch-25.04
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
110 changes: 59 additions & 51 deletions python/cudf_polars/cudf_polars/dsl/expressions/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


class Agg(Expr):
__slots__ = ("name", "op", "options", "request")
__slots__ = ("name", "options", "request")
_non_child = ("dtype", "name", "options")

def __init__(
Expand All @@ -46,58 +46,11 @@ def __init__(
raise NotImplementedError(
f"Unsupported aggregation {name=}"
) # pragma: no cover; all valid aggs are supported
# TODO: nan handling in groupby case
if name == "min":
req = plc.aggregation.min()
elif name == "max":
req = plc.aggregation.max()
elif name == "median":
req = plc.aggregation.median()
elif name == "n_unique":
# TODO: datatype of result
req = plc.aggregation.nunique(null_handling=plc.types.NullPolicy.INCLUDE)
elif name == "first" or name == "last":
req = None
elif name == "mean":
req = plc.aggregation.mean()
elif name == "sum":
req = plc.aggregation.sum()
elif name == "std":
# TODO: handle nans
req = plc.aggregation.std(ddof=options)
elif name == "var":
# TODO: handle nans
req = plc.aggregation.variance(ddof=options)
elif name == "count":
req = plc.aggregation.count(
null_handling=plc.types.NullPolicy.EXCLUDE
if not options
else plc.types.NullPolicy.INCLUDE
)
elif name == "quantile":
if name == "quantile":
_, quantile = self.children
if not isinstance(quantile, Literal):
raise NotImplementedError("Only support literal quantile values")
req = plc.aggregation.quantile(
quantiles=[quantile.value.as_py()], interp=Agg.interp_mapping[options]
)
else:
raise NotImplementedError(
f"Unreachable, {name=} is incorrectly listed in _SUPPORTED"
) # pragma: no cover
self.request = req
op = getattr(self, f"_{name}", None)
if op is None:
op = partial(self._reduce, request=req)
elif name in {"min", "max"}:
op = partial(op, propagate_nans=options)
elif name in {"count", "sum", "first", "last"}:
pass
else:
raise NotImplementedError(
f"Unreachable, supported agg {name=} has no implementation"
) # pragma: no cover
self.op = op
self.request = None
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to somehow validate that the name is supported within __init__ so that we catch a problem at translation time.

Copy link
Member Author

Choose a reason for hiding this comment

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


_SUPPORTED: ClassVar[frozenset[str]] = frozenset(
[
Expand All @@ -124,6 +77,46 @@ def __init__(
"linear": plc.types.Interpolation.LINEAR,
}

def _fill_request(self):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can just define request as a property (or cached property), and move this same logic there?

@property
def request(self):
   ...
``

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. Done in faf42d0 .

if self.request is None:
# TODO: nan handling in groupby case
if self.name == "min":
req = plc.aggregation.min()
elif self.name == "max":
req = plc.aggregation.max()
elif self.name == "median":
req = plc.aggregation.median()
elif self.name == "n_unique":
# TODO: datatype of result
req = plc.aggregation.nunique(
null_handling=plc.types.NullPolicy.INCLUDE
)
elif self.name == "first" or self.name == "last":
req = None
elif self.name == "mean":
req = plc.aggregation.mean()
elif self.name == "sum":
req = plc.aggregation.sum()
elif self.name == "std":
# TODO: handle nans
req = plc.aggregation.std(ddof=self.options)
elif self.name == "var":
# TODO: handle nans
req = plc.aggregation.variance(ddof=self.options)
elif self.name == "count":
req = plc.aggregation.count(null_handling=plc.types.NullPolicy.EXCLUDE)
elif self.name == "quantile":
_, quantile = self.children
req = plc.aggregation.quantile(
quantiles=[quantile.value.as_py()],
interp=Agg.interp_mapping[self.options],
)
else:
raise NotImplementedError(
f"Unreachable, {self.name=} is incorrectly listed in _SUPPORTED"
) # pragma: no cover
self.request = req

def collect_agg(self, *, depth: int) -> AggInfo:
"""Collect information about aggregations in groupbys."""
if depth >= 1:
Expand All @@ -134,6 +127,7 @@ def collect_agg(self, *, depth: int) -> AggInfo:
raise NotImplementedError("Nan propagation in groupby for min/max")
(child,) = self.children
((expr, _, _),) = child.collect_agg(depth=depth + 1).requests
self._fill_request()
request = self.request
# These are handled specially here because we don't set up the
# request for the whole-frame agg because we can avoid a
Expand Down Expand Up @@ -240,7 +234,21 @@ def do_evaluate(
f"Agg in context {context}"
) # pragma: no cover; unreachable

self._fill_request()

op = getattr(self, f"_{self.name}", None)
if op is None:
op = partial(self._reduce, request=self.request)
elif self.name in {"min", "max"}:
op = partial(op, propagate_nans=self.options)
elif self.name in {"count", "sum", "first", "last"}:
pass
else:
raise NotImplementedError(
f"Unreachable, supported agg {self.name=} has no implementation"
) # pragma: no cover

# Aggregations like quantiles may have additional children that were
# preprocessed into pylibcudf requests.
child = self.children[0]
return self.op(child.evaluate(df, context=context, mapping=mapping))
return op(child.evaluate(df, context=context, mapping=mapping))
10 changes: 8 additions & 2 deletions python/cudf_polars/cudf_polars/dsl/expressions/string.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: Apache-2.0
# TODO: remove need for this
# ruff: noqa: D101
Expand Down Expand Up @@ -138,7 +138,7 @@ def _validate_input(self):
)
pattern = self.children[1].value.as_py()
try:
self._regex_program = plc.strings.regex_program.RegexProgram.create(
plc.strings.regex_program.RegexProgram.create(
pattern,
flags=plc.strings.regex_flags.RegexFlags.DEFAULT,
)
Expand Down Expand Up @@ -218,6 +218,12 @@ def do_evaluate(
)
return Column(plc.strings.find.contains(column.obj, pattern))
else:
assert isinstance(arg, Literal)
pattern = arg.value.as_py()
self._regex_program = plc.strings.regex_program.RegexProgram.create(
pattern,
flags=plc.strings.regex_flags.RegexFlags.DEFAULT,
)
return Column(
plc.strings.contains.contains_re(column.obj, self._regex_program)
)
Expand Down
4 changes: 1 addition & 3 deletions python/cudf_polars/cudf_polars/dsl/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,13 +883,11 @@ def __init__(
raise NotImplementedError("dynamic group by")
if any(GroupBy.check_agg(a.value) > 1 for a in self.agg_requests):
raise NotImplementedError("Nested aggregations in groupby")
self.agg_infos = [req.collect_agg(depth=0) for req in self.agg_requests]
self._non_child_args = (
self.keys,
self.agg_requests,
maintain_order,
options,
self.agg_infos,
Comment on lines 899 to -892
Copy link
Member

Choose a reason for hiding this comment

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

I also found that options can be an unserializable Polars objects (though the only thing we actually use in do_evaluate is options.slice).

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, do you see any tests we could rely immediately upon for this case?

)

@staticmethod
Expand Down Expand Up @@ -927,7 +925,6 @@ def do_evaluate(
agg_requests: Sequence[expr.NamedExpr],
maintain_order: bool, # noqa: FBT001
options: Any,
agg_infos: Sequence[expr.AggInfo],
df: DataFrame,
):
"""Evaluate and return a dataframe."""
Expand All @@ -947,6 +944,7 @@ def do_evaluate(
# TODO: uniquify
requests = []
replacements: list[expr.Expr] = []
agg_infos = [req.collect_agg(depth=0) for req in agg_requests]
Copy link
Member

Choose a reason for hiding this comment

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

This is definitely a good way to avoid AggInfo serialization. The disadvantage is that we don't raise an error from collect_agg when the GroupBy object is created at translation time.

for info in agg_infos:
for pre_eval, req, rep in info.requests:
if pre_eval is None:
Expand Down
Loading