-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
ENH: Allow JIT compilation with an internal API #61032
Changes from 3 commits
7ec827e
bc2a178
8b420cc
6a9ee5a
444de67
7e1e855
58fb30d
c239fc9
9567152
2ff333f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
This file is here as an example, this code will live in the Numba and | ||
Bodo libraries. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Literal, | ||
) | ||
|
||
import bodo | ||
|
||
import pandas as pd | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Callable | ||
|
||
from pandas._typing import ( | ||
AggFuncType, | ||
Axis, | ||
) | ||
|
||
|
||
def __pandas_udf__( | ||
jit_decorator: Callable, | ||
obj: pd.Series | pd.DataFrame, | ||
method: Literal["apply", "map"], | ||
func: AggFuncType, | ||
axis: Axis, | ||
raw: bool, | ||
result_type: Literal["expand", "reduce", "broadcast"] | None, | ||
args: tuple, | ||
kwargs: dict[str, Any], | ||
by_row: Literal[False, "compat"], | ||
): | ||
if isinstance(obj, pd.DataFrame) and method == "apply": | ||
if result_type is not None: | ||
raise NotImplementedError( | ||
"engine='bodo' not supported when result_type is not None" | ||
) | ||
|
||
if raw: | ||
raise NotImplementedError("engine='bodo' not supported when raw=True") | ||
if isinstance(func, str) and axis != 1: | ||
raise NotImplementedError( | ||
"engine='bodo' only supports axis=1 when func is the name of a " | ||
"user-defined function" | ||
) | ||
if args or kwargs: | ||
raise NotImplementedError( | ||
"engine='bodo' not supported when args or kwargs are specified" | ||
) | ||
|
||
@jit_decorator | ||
def jit_func(df, func, axis): | ||
return df.apply(func, axis=axis) | ||
|
||
return jit_func(obj, func, axis) | ||
else: | ||
raise NotImplementedError( | ||
f"engine='bodo' not supported for {obj.__name__}.{method}" | ||
) | ||
|
||
|
||
bodo.jit.__pandas_udf__ = __pandas_udf__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10256,6 +10256,7 @@ def apply( | |
by_row: Literal[False, "compat"] = "compat", | ||
engine: Literal["python", "numba"] = "python", | ||
engine_kwargs: dict[str, bool] | None = None, | ||
jit: Callable | None = None, | ||
**kwargs, | ||
): | ||
""" | ||
|
@@ -10345,6 +10346,15 @@ def apply( | |
Pass keyword arguments to the engine. | ||
This is currently only used by the numba engine, | ||
see the documentation for the engine argument for more information. | ||
|
||
jit : function, optional | ||
Decorator to JIT compile the execution. The main available options are | ||
``numba.jit``, ``numba.njit`` or ``bodo.jit``. Parameters can be used in | ||
the same way as the decorators, for example ``numba.jit(parallel=True)``. | ||
|
||
Refer to the the [1]_ and [2]_ documentation to learn about limitations | ||
on what code can be JIT compiled. | ||
|
||
**kwargs | ||
Additional keyword arguments to pass as keywords arguments to | ||
`func`. | ||
|
@@ -10367,6 +10377,13 @@ def apply( | |
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation` | ||
for more details. | ||
|
||
References | ||
---------- | ||
.. [1] `Numba documentation | ||
<https://numba.readthedocs.io/en/stable/index.html>`_ | ||
.. [2] `Bodo documentation | ||
<https://docs.bodo.ai/latest/>`/ | ||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame([[4, 9]] * 3, columns=["A", "B"]) | ||
|
@@ -10435,7 +10452,34 @@ def apply( | |
0 1 2 | ||
1 1 2 | ||
2 1 2 | ||
""" | ||
|
||
Advanced users can speed up their code by using a Just-in-time (JIT) compiler | ||
with ``apply``. The main JIT compilers available for pandas are Numba and Bodo. | ||
In general, JIT compilation is only possible when the function passed to | ||
``apply`` has type stability (variables in the function do not change their | ||
type during the execution). | ||
|
||
>>> import bodo | ||
>>> df.apply(lambda x: x.A + x.B, axis=1, jit=bodo.jit(parallel=True)) | ||
|
||
Note that JIT compilation is only recommended for functions that take a | ||
significant amount of time to run. Fast functions are unlikely to run faster | ||
with JIT compilation. | ||
""" | ||
if hasattr(jit, "__pandas_udf__"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if jit is provided but doesnt have this attribute? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if engine or enging_kwargs are passed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should error out I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea here is that the new parameter would make Also, I agree, if the value of the parameter doesn't implement the interface, we should raise an exception. It should probably be quite specific, on what is expected and which versions of Numba or Bodo can be used. But I think it's easy to provide something that is clear to users if they pass something that doesn't implement this interface. |
||
return jit.__pandas_udf__( | ||
jit_decorator=jit, | ||
obj=self, | ||
method="apply", | ||
func=func, | ||
args=args, | ||
kwargs=kwargs, | ||
axis=axis, | ||
raw=raw, | ||
result_type=result_type, | ||
by_row=by_row, | ||
) | ||
|
||
from pandas.core.apply import frame_apply | ||
|
||
op = frame_apply( | ||
|
@@ -10567,9 +10611,11 @@ def _append( | |
|
||
index = Index( | ||
[other.name], | ||
name=self.index.names | ||
if isinstance(self.index, MultiIndex) | ||
else self.index.name, | ||
name=( | ||
self.index.names | ||
if isinstance(self.index, MultiIndex) | ||
else self.index.name | ||
), | ||
) | ||
row_df = other.to_frame().T | ||
# infer_objects is needed for | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the 3rd party implementation isn't a jit? e.g. it is just parallel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you rename the parameter name to for example
executor
? I'm happy with that. I guess that would make naming more accurate if this is used for other use cases as running in parallel, which is possible with this interface. Do you have a specific use case in mind?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id just use 'engine', avoid having multiple similar keywords
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, that makes sense, thanka for the feedback. This is how it's implemented now in the PR, just using the existing parameter
engine
.