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

Implement groupby apply with JIT #11452

Merged
merged 157 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 155 commits
Commits
Show all changes
157 commits
Select commit Hold shift + click to select a range
8db918f
Groupby Apply with JIT (First Commit)
bwyogatama Aug 3, 2022
2d6b4c9
Fix error in Pytest
bwyogatama Aug 4, 2022
fd8680e
JIT Caching Support
bwyogatama Aug 5, 2022
9220658
Add IdxMax and IdxMin
bwyogatama Aug 5, 2022
f4bc7c4
Add IdxMax and IdxMin
bwyogatama Aug 5, 2022
8659149
Dynamic Launch Parameter
bwyogatama Aug 14, 2022
b7ede43
Code cleanup #1
bwyogatama Aug 19, 2022
1dbbb77
Merge remote-tracking branch 'origin/branch-22.10' into fea-groupby-a…
vyasr Sep 23, 2022
f98fc63
Add support for building the JIT functions with the rest of the build.
vyasr Sep 23, 2022
11edd37
Make engine name consistent with tests
vyasr Sep 23, 2022
1e12416
Generalize compiled PTX selection for CUDA arch.
vyasr Sep 23, 2022
d348fb8
Cleanup of strings_udf PTX detection
vyasr Sep 23, 2022
795e580
Fix tests with some hacks so that we can start validating.
vyasr Sep 23, 2022
0ce0a90
Standardize the engine argument handling so that we get clear errors.
vyasr Sep 23, 2022
3493d49
Update style.
vyasr Sep 24, 2022
7f9ea1f
Refactoring C++ function
bwyogatama Oct 26, 2022
3d76a44
Fix bug in C++ and Python Cleanup
Nov 2, 2022
ad878ac
merge latest, resolve conflicts, pass tests
brandon-b-miller Nov 21, 2022
d876ad7
pass style, cleanup
brandon-b-miller Nov 21, 2022
f600196
start to move files
brandon-b-miller Nov 21, 2022
6cbdaf8
starting to refactor
brandon-b-miller Nov 22, 2022
3a11fe1
continue to refactor typing
brandon-b-miller Nov 28, 2022
8161548
move lowering to its own file
brandon-b-miller Nov 28, 2022
52656ab
continue refactoring idxmin and idxmax functions
brandon-b-miller Nov 28, 2022
b9096f3
add tests for idxmin and idxmax, not currently passing
brandon-b-miller Nov 28, 2022
d21a099
normalize call_cuda_functions keys
brandon-b-miller Nov 28, 2022
62aad1e
continued refactoring
brandon-b-miller Nov 28, 2022
9ff058a
refactoring lowering
brandon-b-miller Nov 28, 2022
5f07ca2
continued refactoring
brandon-b-miller Nov 29, 2022
38c3560
Merge branch 'branch-23.02' into groupby-apply-updates
brandon-b-miller Dec 5, 2022
c12a9e3
CMake changes
bwyogatama Jan 2, 2023
e650c21
C++ changes
bwyogatama Jan 2, 2023
0fd6e22
Merge branch 'branch-23.02' into fea-groupby-apply-jit
brandon-b-miller Jan 5, 2023
aabdc5f
Merge branch 'branch-23.02' into fea-groupby-apply-jit
brandon-b-miller Jan 6, 2023
301eea1
style
brandon-b-miller Jan 6, 2023
e50f4a6
found the bug
brandon-b-miller Jan 8, 2023
df1485d
minor refactoring
brandon-b-miller Jan 8, 2023
353078c
Merge branch 'branch-23.02' into fea-groupby-apply-jit
brandon-b-miller Jan 8, 2023
14fe3cb
update/add tests
brandon-b-miller Jan 9, 2023
f7791b4
continue refactoring tests
brandon-b-miller Jan 9, 2023
7f63c90
add docs, switch to partials
brandon-b-miller Jan 12, 2023
902223a
continue addressing reviews
brandon-b-miller Jan 17, 2023
78f8b6f
Update python/cudf/cudf/core/udf/groupby_typing.py
brandon-b-miller Jan 17, 2023
611b864
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
brandon-b-miller Jan 17, 2023
2849680
merge remote
brandon-b-miller Jan 17, 2023
33109f5
address more reviews
brandon-b-miller Jan 17, 2023
07444eb
move utilities around
brandon-b-miller Jan 17, 2023
39eb8f9
template throughout c++
brandon-b-miller Jan 18, 2023
f30cd8b
Merge branch 'branch-23.02' into fea-groupby-apply-jit
brandon-b-miller Jan 18, 2023
6158cb7
cpp code formatting
PointKernel Jan 18, 2023
93df707
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
brandon-b-miller Jan 18, 2023
5ae896a
style
brandon-b-miller Jan 18, 2023
a42d307
Use cudf size_type
PointKernel Jan 18, 2023
0e0b750
Use std limits instead of macros
PointKernel Jan 18, 2023
e891e5f
merge latest, resolve conflicts, don't unset warnings
brandon-b-miller Jan 19, 2023
8188508
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
brandon-b-miller Jan 19, 2023
95fa402
remove redundant comments
brandon-b-miller Jan 19, 2023
3de3add
style fixes
brandon-b-miller Jan 19, 2023
865bb5d
debug statements
brandon-b-miller Jan 20, 2023
7788944
patch numba linker based off groupby ptx file
brandon-b-miller Jan 20, 2023
bdea84c
Fix idxmin/max bug
PointKernel Jan 20, 2023
0110075
Use static_cast to avoid raw casting
PointKernel Jan 20, 2023
b039ce7
Cleanups: reinterpret_cast + remove redundant sync
PointKernel Jan 20, 2023
a8c3a75
Merge branch 'branch-23.02' into fea-groupby-apply-jit
brandon-b-miller Jan 20, 2023
21792c6
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
brandon-b-miller Jan 20, 2023
14dc674
Simplify block min/max logic
PointKernel Jan 20, 2023
3a48a96
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
PointKernel Jan 20, 2023
9766233
Merge remote-tracking branch 'upstream/branch-23.02' into groupby
PointKernel Jan 20, 2023
99af3f2
Replace custom atomic add with cuda atomic_ref
PointKernel Jan 20, 2023
321fdab
Simplify block sum logic
PointKernel Jan 20, 2023
c91a589
Simplify block var logic
PointKernel Jan 20, 2023
aa47763
Merge branch 'branch-23.02' into fea-groupby-apply-jit
brandon-b-miller Jan 20, 2023
cbc13e6
Refactor block idxmin/idxmax
PointKernel Jan 20, 2023
ab20731
Fix a minor bug
PointKernel Jan 20, 2023
f721f9c
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
PointKernel Jan 20, 2023
a24f09e
Fix the floating point min value bug
PointKernel Jan 20, 2023
d908621
Refactor with CUDA CG
PointKernel Jan 20, 2023
595746a
C++ changes: (1) Addressing more reviewer's comment, (2) Replacing cu…
bwyogatama Jan 22, 2023
9a93af7
Merge branch 'branch-23.02' into fea-groupby-apply-jit
brandon-b-miller Jan 23, 2023
9d3c431
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
brandon-b-miller Jan 23, 2023
bb8b7c3
style
brandon-b-miller Jan 23, 2023
1f475f0
Use proper cuda thread scope
PointKernel Jan 23, 2023
0bb68bc
Merge remote-tracking branch 'upstream/branch-23.02' into groupby
PointKernel Jan 23, 2023
209188f
Request cpp review for udf_cpp
PointKernel Jan 23, 2023
826ed25
Remove unsupported data types
PointKernel Jan 23, 2023
1cf91ea
Use exclusively thread 0 to write var output + minor cleanups
PointKernel Jan 23, 2023
381dd00
Addressing more reviewer's commment and fix mean bug
bwyogatama Jan 23, 2023
ee87548
error if nulls are present, dont not patch numba linker in strings_udf
brandon-b-miller Jan 23, 2023
1d4edc8
Style
brandon-b-miller Jan 23, 2023
4ff80f4
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
PointKernel Jan 23, 2023
4fe21fb
Replace int with int32_t
PointKernel Jan 23, 2023
3fbe3ff
Remove unused template specializations
PointKernel Jan 23, 2023
b59d31e
Merge remote-tracking branch 'upstream/branch-23.02' into groupby
PointKernel Jan 23, 2023
9af3670
update utility functions to no longer be strings_udf specific
brandon-b-miller Jan 24, 2023
afd0949
tweak thread guard logic in groupby template
brandon-b-miller Jan 24, 2023
c05e889
Merge branch 'branch-23.02' into fea-groupby-apply-jit
brandon-b-miller Jan 24, 2023
1828ef7
Apply suggestions from code review
brandon-b-miller Jan 24, 2023
6489950
fix small bug
brandon-b-miller Jan 24, 2023
9b83d78
refactor group constructor lowering
brandon-b-miller Jan 24, 2023
73a2ba1
partially address reviews
brandon-b-miller Jan 24, 2023
4dfb790
continue addressing reviews
brandon-b-miller Jan 24, 2023
43be944
extraneous copyright
brandon-b-miller Jan 24, 2023
b5f8f63
fix small comment error in cmake
brandon-b-miller Jan 24, 2023
8bbd725
inline _is_jit_supported_type
brandon-b-miller Jan 24, 2023
2df3216
adjust logic in maybe_patch_numba_linker
brandon-b-miller Jan 24, 2023
e8137e3
Apply suggestions from code review
PointKernel Jan 24, 2023
c5e7445
Correct numerical limits
PointKernel Jan 24, 2023
761261c
Apply suggestions from code review
brandon-b-miller Jan 24, 2023
9884897
fix small syntax error
brandon-b-miller Jan 24, 2023
41b42c7
add an updater to update-version.sh
brandon-b-miller Jan 24, 2023
5855f5c
refactor groupby.apply top level impl into separate methods
brandon-b-miller Jan 24, 2023
d6a3ef2
GroupType.size_type -> GroupType.group_size_type
brandon-b-miller Jan 24, 2023
9b60a62
introduce group_size_type as a global
brandon-b-miller Jan 24, 2023
f0a9af8
use index_default_type in idxmax/idxmin lowering
brandon-b-miller Jan 24, 2023
6708655
rename some utility functions and add docs
brandon-b-miller Jan 24, 2023
3e5149d
tweak previous function
brandon-b-miller Jan 24, 2023
c253b8f
Addressing reviewers' comments
bwyogatama Jan 24, 2023
51d7ec7
Merge branch 'fea-groupby-apply-jit' of https://github.com/bwyogatama…
bwyogatama Jan 24, 2023
bae845d
unused import
brandon-b-miller Jan 24, 2023
e91b641
Replace std numeric limits with cudf device operators
PointKernel Jan 24, 2023
0d35554
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
PointKernel Jan 24, 2023
73892e1
add tests for special values
brandon-b-miller Jan 24, 2023
08cbcb7
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
brandon-b-miller Jan 24, 2023
81bfeb1
Apply suggestions from code review
PointKernel Jan 25, 2023
3d76481
Code formatting
PointKernel Jan 25, 2023
97490af
Add more special value tests
PointKernel Jan 25, 2023
43694f7
Fix bugs when all values are nans
PointKernel Jan 25, 2023
928d404
Fix a result init bug in are_all_nans
PointKernel Jan 25, 2023
8079047
separate out idmax and idxmin tests with special values
brandon-b-miller Jan 25, 2023
11c0eb6
remove redundant tests
brandon-b-miller Jan 25, 2023
3a5afa6
answer is the offsets not just an array of zeroes
brandon-b-miller Jan 25, 2023
4d719b5
dynamically register reductions
brandon-b-miller Jan 25, 2023
5c5e37c
Add corner cases handling to idxmin/idxmax
PointKernel Jan 25, 2023
fac8d70
Apply suggestions from code review
PointKernel Jan 25, 2023
2f9cc76
Remove unroll pragma
PointKernel Jan 25, 2023
961b3b9
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
PointKernel Jan 25, 2023
5e6d4aa
Merge remote-tracking branch 'upstream/branch-23.02' into groupby
PointKernel Jan 25, 2023
6665ef9
address remaining reviews
brandon-b-miller Jan 26, 2023
62b5a99
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
brandon-b-miller Jan 26, 2023
62a8928
fix tests
brandon-b-miller Jan 26, 2023
de6b54c
Code formatting
PointKernel Jan 26, 2023
0c3d5a0
go back to index_default_type = types.int64 explicitly
brandon-b-miller Jan 26, 2023
b0e8c29
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
brandon-b-miller Jan 26, 2023
5a5c0fb
merge resolve conflicts
brandon-b-miller Jan 26, 2023
5db0b6c
style
brandon-b-miller Jan 26, 2023
7e2ca13
Update python/cudf/udf_cpp/groupby/CMakeLists.txt
brandon-b-miller Jan 26, 2023
40b8ce9
Cast mean results to double
PointKernel Jan 26, 2023
bb8e0c3
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
PointKernel Jan 26, 2023
0952784
Merge remote-tracking branch 'upstream/branch-23.02' into groupby
PointKernel Jan 26, 2023
e0d0230
Merge branch 'branch-23.02' into fea-groupby-apply-jit
PointKernel Jan 26, 2023
81860c5
address reviews
brandon-b-miller Jan 27, 2023
2f352bc
minor edits
brandon-b-miller Jan 27, 2023
0b407c8
Compute blockstd via blockvar
PointKernel Jan 27, 2023
568ab97
Merge branch 'fea-groupby-apply-jit' of github.com:bwyogatama/cudf in…
PointKernel Jan 27, 2023
83f8d88
Merge remote-tracking branch 'upstream/branch-23.02' into groupby
PointKernel Jan 27, 2023
dbd5eeb
Use atomic operations to avoid concurrent writes
PointKernel Jan 27, 2023
eaa8ff7
Use int64_t atomic ref
PointKernel Jan 27, 2023
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
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#cpp code owners
cpp/ @rapidsai/cudf-cpp-codeowners
cpp/ @rapidsai/cudf-cpp-codeowners
python/cudf/udf_cpp/ @rapidsai/cudf-cpp-codeowners

#python code owners
python/ @rapidsai/cudf-python-codeowners
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ python/cudf/*/_cuda/*.cpp
python/cudf/*.ipynb
python/cudf/.ipynb_checkpoints
python/*/record.txt
python/cudf/cudf/core/udf/*.ptx
python/cudf_kafka/*/_lib/**/*.cpp
python/cudf_kafka/*/_lib/**/*.h
python/custreamz/*/_lib/**/*.cpp
Expand Down
3 changes: 3 additions & 0 deletions ci/release/update-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ sed_runner 's/'"cudf_version .*)"'/'"cudf_version ${NEXT_FULL_TAG})"'/g' python/
# Strings UDF update
sed_runner 's/'"strings_udf_version .*)"'/'"strings_udf_version ${NEXT_FULL_TAG})"'/g' python/strings_udf/CMakeLists.txt

# Groupby UDF update
sed_runner 's/'"VERSION ${CURRENT_SHORT_TAG}.*"'/'"VERSION ${NEXT_FULL_TAG}"'/g' python/cudf/udf_cpp/CMakeLists.txt

# cpp libcudf_kafka update
sed_runner 's/'"VERSION ${CURRENT_SHORT_TAG}.*"'/'"VERSION ${NEXT_FULL_TAG}"'/g' cpp/libcudf_kafka/CMakeLists.txt

Expand Down
7 changes: 5 additions & 2 deletions python/cudf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# =============================================================================
# Copyright (c) 2022, NVIDIA CORPORATION.
# Copyright (c) 2022-2023, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
Expand All @@ -17,6 +17,8 @@ cmake_minimum_required(VERSION 3.23.1 FATAL_ERROR)
set(cudf_version 23.02.00)

include(../../fetch_rapids.cmake)
include(rapids-cuda)
rapids_cuda_init_architectures(cudf-python)

project(
cudf-python
Expand All @@ -25,7 +27,7 @@ project(
# language to be enabled here. The test project that is built in scikit-build to verify
# various linking options for the python library is hardcoded to build with C, so until
# that is fixed we need to keep C.
C CXX
C CXX CUDA
)

option(FIND_CUDF_CPP "Search for existing CUDF C++ installations before defaulting to local files"
Expand Down Expand Up @@ -117,6 +119,7 @@ endif()
rapids_cython_init()

add_subdirectory(cudf/_lib)
add_subdirectory(udf_cpp/groupby)

include(cmake/Modules/ProtobufHelpers.cmake)
codegen_protoc(cudf/utils/metadata/orc_column_statistics.proto)
Expand Down
11 changes: 9 additions & 2 deletions python/cudf/cudf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2022, NVIDIA CORPORATION.
# Copyright (c) 2018-2023, NVIDIA CORPORATION.

from cudf.utils.gpu_utils import validate_setup

Expand Down Expand Up @@ -88,7 +88,14 @@
pass
else:
# Patch Numba to support CUDA enhanced compatibility.
patch_numba_linker_if_needed()
# cuDF requires a stronger set of conditions than what is
# checked by patch_numba_linker_if_needed due to the PTX
# files needed for JIT Groupby Apply and string UDFs
from cudf.core.udf.groupby_utils import dev_func_ptx
from cudf.core.udf.utils import _setup_numba_linker

_setup_numba_linker(dev_func_ptx)

del patch_numba_linker_if_needed

cuda.set_memory_manager(rmm.RMMNumbaManager)
Expand Down
133 changes: 104 additions & 29 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.utils.utils import GetAttrGetItemMixin, _cudf_nvtx_annotate


Expand Down Expand Up @@ -786,14 +787,83 @@ def pipe(self, func, *args, **kwargs):
"""
return cudf.core.common.pipe(self, func, *args, **kwargs)

def apply(self, function, *args):
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"
)

chunk_results = jit_groupby_apply(
offsets, grouped_values, function, *args
)
result = cudf.Series._from_data(
{None: chunk_results}, index=group_names
)
result.index.names = self.grouping.names
result = result.reset_index()
result[None] = result.pop(0)
return result

def _iterative_groupby_apply(
self, function, group_names, offsets, group_keys, grouped_values, *args
):
ngroups = len(offsets) - 1
if ngroups > self._MAX_GROUPS_BEFORE_WARN:
warnings.warn(
f"GroupBy.apply() performance scales poorly with "
f"number of groups. Got {ngroups} groups. Some functions "
"may perform better by passing engine='jit'",
RuntimeWarning,
)

chunks = [
grouped_values[s:e] for s, e in zip(offsets[:-1], offsets[1:])
]
chunk_results = [function(chk, *args) for chk in chunks]
if not len(chunk_results):
return self.obj.head(0)

if cudf.api.types.is_scalar(chunk_results[0]):
result = cudf.Series._from_data(
{None: chunk_results}, index=group_names
)
result.index.names = self.grouping.names
elif isinstance(chunk_results[0], cudf.Series) and isinstance(
self.obj, cudf.DataFrame
):
result = cudf.concat(chunk_results, axis=1).T
result.index.names = self.grouping.names
else:
result = cudf.concat(chunk_results)
if self._group_keys:
index_data = group_keys._data.copy(deep=True)
index_data[None] = grouped_values.index._column
result.index = cudf.MultiIndex._from_data(index_data)
return result

def apply(self, function, *args, engine="cudf"):
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
bdice marked this conversation as resolved.
Show resolved Hide resolved
"""Apply a python transformation function over the grouped chunk.

Parameters
----------
func : function
function : callable
The python transformation function that will be applied
on the grouped chunk.
args : tuple
Optional positional arguments to pass to the function.
engine: {'cudf', 'jit'}, default 'cudf'
Selects the GroupBy.apply implementation. Use `jit` to
select the numba JIT pipeline. Only certain operations are allowed
within the function when using this option: min, max, sum, mean, var,
std, idxmax, and idxmin and any arithmetic formula involving them are
allowed. Binary operations are not yet supported, so syntax like
`df['x'] * 2` is not yet allowed.
For more information, see the `cuDF guide to user defined functions
Copy link
Contributor

Choose a reason for hiding this comment

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

It sounds like this link should explain the Numba JIT pipeline, but it doesn't. Will we update that notebook in this PR (or before the 23.02 release)?

If we don't have time to update that notebook, maybe we can explain the choice of 'cudf' or 'jit' a little more in this docstring. Currently I don't think users will know enough to decide which is appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

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

There will definitely be a notebook update asap! It's in progress. I'll expound a bit here regardless though.

<https://docs.rapids.ai/api/cudf/stable/user_guide/guide-to-udfs.html>`__.

Examples
--------
Expand Down Expand Up @@ -850,40 +920,45 @@ def mult(df):
a b c
0 1 1 1
2 2 1 3

``engine='jit'`` may be used to accelerate certain functions,
initially those that contain reductions and arithmetic operations
between results of those reductions:
>>> import cudf
>>> df = cudf.DataFrame({'a':[1,1,2,2,3,3], 'b':[1,2,3,4,5,6]})
>>> df.groupby('a').apply(
... lambda group: group['b'].max() - group['b'].min(),
... engine='jit'
... )
a None
0 1 1
1 2 1
2 3 1
"""
if not callable(function):
raise TypeError(f"type {type(function)} is not callable")
group_names, offsets, group_keys, grouped_values = self._grouped()

ngroups = len(offsets) - 1
if ngroups > self._MAX_GROUPS_BEFORE_WARN:
warnings.warn(
f"GroupBy.apply() performance scales poorly with "
f"number of groups. Got {ngroups} groups."
if engine == "jit":
bdice marked this conversation as resolved.
Show resolved Hide resolved
result = self._jit_groupby_apply(
function,
group_names,
offsets,
group_keys,
grouped_values,
*args,
)
elif engine == "cudf":
result = self._iterative_groupby_apply(
function,
group_names,
offsets,
group_keys,
grouped_values,
*args,
)

chunks = [
grouped_values[s:e] for s, e in zip(offsets[:-1], offsets[1:])
]
chunk_results = [function(chk, *args) for chk in chunks]
if not len(chunk_results):
return self.obj.head(0)

if cudf.api.types.is_scalar(chunk_results[0]):
result = cudf.Series(chunk_results, index=group_names)
result.index.names = self.grouping.names
else:
if isinstance(chunk_results[0], cudf.Series) and isinstance(
self.obj, cudf.DataFrame
):
result = cudf.concat(chunk_results, axis=1).T
result.index.names = self.grouping.names
else:
result = cudf.concat(chunk_results)
if self._group_keys:
index_data = group_keys._data.copy(deep=True)
index_data[None] = grouped_values.index._column
result.index = cudf.MultiIndex._from_data(index_data)
raise ValueError(f"Unsupported engine '{engine}'")

if self._sort:
result = result.sort_index()
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/udf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2022, NVIDIA CORPORATION.
# Copyright (c) 2022-2023, NVIDIA CORPORATION.

from functools import lru_cache

Expand All @@ -9,7 +9,7 @@
from cudf.core.udf import api, row_function, utils
from cudf.utils.dtypes import STRING_TYPES

from . import masked_lowering, masked_typing
from . import groupby_lowering, groupby_typing, masked_lowering, masked_typing

_units = ["ns", "ms", "us", "s"]
_datetime_cases = {types.NPDatetime(u) for u in _units}
Expand Down
Loading