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

Conversation

brandon-b-miller
Copy link
Contributor

Closes #13103

@brandon-b-miller brandon-b-miller added feature request New feature or request numba Numba issue non-breaking Non-breaking change python labels Apr 11, 2023
@brandon-b-miller brandon-b-miller self-assigned this Apr 11, 2023
@github-actions github-actions bot added the Python Affects Python cuDF API. label Apr 11, 2023
@brandon-b-miller brandon-b-miller marked this pull request as ready for review April 24, 2023 14:06
@brandon-b-miller brandon-b-miller requested a review from a team as a code owner April 24, 2023 14:06
@brandon-b-miller
Copy link
Contributor Author

hi all, this should be ready for review.

Copy link
Contributor

@vyasr vyasr left a comment

Choose a reason for hiding this comment

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

One minor suggestion and a question, but LGTM.

python/cudf/cudf/core/udf/groupby_utils.py Outdated Show resolved Hide resolved
python/cudf/cudf/core/udf/groupby_utils.py Show resolved Hide resolved
Copy link
Contributor

@wence- wence- left a comment

Choose a reason for hiding this comment

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

Two minor quibbles.

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.

@@ -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.

@brandon-b-miller brandon-b-miller requested a review from wence- May 11, 2023 14:28
Copy link
Contributor

@wence- wence- left a comment

Choose a reason for hiding this comment

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

Thanks!

python/cudf/cudf/core/groupby/groupby.py Show resolved Hide resolved
@@ -1252,7 +1249,7 @@ def apply(self, function, *args, engine="cudf"):
on the grouped chunk.
args : tuple
Optional positional arguments to pass to the function.
engine: {'cudf', 'jit'}, default 'cudf'
engine: {'cudf', 'jit'}, default 'auto'
Copy link
Contributor

Choose a reason for hiding this comment

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

For my knowledge: Is there a performance cost to the fallback? i.e. Does the JIT attempt have measurable overhead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it does have measurable overhead. One way of measuring it is with

import cudf
df = cudf.DataFrame({
    'a':[0,1,1],
    'b':[1,2,3]
})

def func(grp):
    # binops can't be jitted without refcounting
    return grp + grp

grouped = df.groupby('a')

import cProfile
cProfile.run('grouped.apply(func)', sort='cumtime')

For this I get

        1    0.000    0.000    0.213    0.213 groupby_utils.py:207(_can_be_jitted)

Meaning it's quite impactful. However if this becomes a problem users should be able to obtain the old behavior by just passing engine='cudf'.

Copy link
Contributor

Choose a reason for hiding this comment

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

Full disclosure, we anticipated this and I was OK with it. I think the tradeoff is generally worthwhile. If we think it isn't then I think we'd just stop this work and remove 'auto' altogether.

Also I just noticed that 'auto' is not listed in the set of valid engine arguments here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to clarify — your snippet above is saying the JIT attempt costs ~200ms? That sounds right to me. I would think a cache could also be used here if needed to prevent multiple failed attempts from paying the overhead for the same function.

I am supportive of this change, because when it does pay off, it’s a big win. Just want to make sure we’re putting in the appropriate amount of engineering effort to mitigate the downside risk.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I just noticed that 'auto' is not listed in the set of valid engine arguments here.

Fixed this.

@brandon-b-miller
Copy link
Contributor Author

/merge

@rapids-bot rapids-bot bot merged commit 89feac7 into rapidsai:branch-23.06 May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request non-breaking Non-breaking change numba Numba issue Python Affects Python cuDF API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEA] Attempt to JIT GroupBy.apply functions by default and fall back to iterative algorithm
4 participants