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

Automatically select GroupBy.apply algorithm based on if the UDF is jittable #13113

Merged
4 changes: 4 additions & 0 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def _dtypes(self):
zip(self._data.names, (col.dtype for col in self._data.columns))
)

@property
def _has_nulls(self):
return any(col.has_nulls() for col in self._data.values())

def serialize(self):
header = {
"type-serialized": pickle.dumps(type(self)),
Expand Down
23 changes: 16 additions & 7 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
from cudf.core.column_accessor import ColumnAccessor
from cudf.core.mixins import Reducible, Scannable
from cudf.core.multiindex import MultiIndex
from cudf.core.udf.groupby_utils import jit_groupby_apply
from cudf.core.udf.groupby_utils import (
_jit_groupby_eligible,
jit_groupby_apply,
)
from cudf.utils.utils import GetAttrGetItemMixin, _cudf_nvtx_annotate


Expand Down Expand Up @@ -1144,11 +1147,9 @@ def _jit_groupby_apply(
self, function, group_names, offsets, group_keys, grouped_values, *args
):
# Nulls are not yet supported
for colname in self.grouping.values._data.keys():
if self.obj._data[colname].has_nulls():
raise ValueError(
"Nulls not yet supported with groupby JIT engine"
)
# TODO: don't check this twice under `engine='auto'`
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there (will there be?) a bug for this? Or do you intend to fix it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought this might be a perf hit but since each column caches its null count I think it's actually ok to just leave it.

if self.grouping._obj._has_nulls:
raise ValueError("Nulls not yet supported with groupby JIT engine")

chunk_results = jit_groupby_apply(
offsets, grouped_values, function, *args
Expand Down Expand Up @@ -1198,7 +1199,7 @@ def _iterative_groupby_apply(
result.index = cudf.MultiIndex._from_data(index_data)
return result

def apply(self, function, *args, engine="cudf"):
def apply(self, function, *args, engine="auto"):
Copy link
Contributor

Choose a reason for hiding this comment

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

docstring needs to be updated to discuss new "auto" option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some docs here.

"""Apply a python transformation function over the grouped chunk.

Parameters
Expand Down Expand Up @@ -1290,10 +1291,18 @@ def mult(df):
1 2 1
2 3 1
"""

if self.obj.empty:
return self.obj
if not callable(function):
raise TypeError(f"type {type(function)} is not callable")
group_names, offsets, group_keys, grouped_values = self._grouped()

if engine == "auto":
if _jit_groupby_eligible(grouped_values, function, args):
engine = "jit"
else:
engine = "cudf"
if engine == "jit":
result = self._jit_groupby_apply(
function,
Expand Down
28 changes: 28 additions & 0 deletions python/cudf/cudf/core/udf/groupby_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cupy as cp
import numpy as np
from numba import cuda, types
from numba.core.errors import TypingError
from numba.cuda.cudadrv.devices import get_context
from numba.np import numpy_support
from numba.types import Record
Expand Down Expand Up @@ -202,3 +203,30 @@ def jit_groupby_apply(offsets, grouped_values, function, *args):
specialized[ngroups, tpb](*launch_args)

return output


def _jit_groupby_eligible(frame, func, args):
return (not frame._has_nulls) and _can_be_jitted(frame, func, args)
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved


def _can_be_jitted(frame, func, args):
"""
Determine if this UDF is supported through the JIT engine
by attempting to compile just the function to PTX using the
target set of types
"""
np_field_types = np.dtype(
list(
_supported_dtypes_from_frame(
frame, supported_types=SUPPORTED_GROUPBY_NUMPY_TYPES
).items()
)
)
dataframe_group_type = _get_frame_groupby_type(
np_field_types, frame.index.dtype
)
try:
_get_udf_return_type(dataframe_group_type, func, args)
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
return True
except TypingError:
return False