8000 ENH: Add numba engine for rolling apply by mroeschke · Pull Request #30151 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add numba engine for rolling apply #30151

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

Merged
merged 56 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
3b9bff8
Add numba to import_optional_dependencies
Dec 8, 2019
9a302bf
Start adding keywords
Dec 8, 2019
0e9a600
Modify apply for numba and cython
Dec 9, 2019
36a77ed
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 9, 2019
dbb2a9b
Add numba as optional dependency
Dec 9, 2019
f0e9a4d
Add premil tests
Dec 9, 2019
1250aee
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 10, 2019
4e7fd1a
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 11, 2019
cb976cf
Add numba to requirements-dev, type and reorder signature in apply
Dec 11, 2019
45420bb
Move numba routines to its own file
Dec 11, 2019
17851cf
Adjust signature in top level function as well
Dec 11, 2019
20767ca
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 11, 2019
9619f8d
Generate requirements-dev.txt using script
Dec 11, 2019
66fa69c
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 13, 2019
8000
b8908ea
Add skip test decorator, add numba to a few builds
Dec 13, 2019
135f2ad
black
Dec 13, 2019
34a5687
don't rejit a user's jitted function
Dec 13, 2019
6da8199
Add numba/cython comparison test
Dec 13, 2019
123f77e
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 17, 2019
54e74d1
Remove typing for now
Dec 17, 2019
04d3530
Remove sub description for doc failures?
Dec 17, 2019
4bbf587
Fix test function
Dec 17, 2019
f849bc7
test user predefined jit function, clarify docstring
Dec 17, 2019
0c30e48
Apply engine kwargs to function as well
Dec 17, 2019
c4c952e
Clairfy documentation
Dec 17, 2019
8645976
Clarify what engine_kwargs applies to
Dec 17, 2019
987c916
Start section for numba rolling apply
Dec 17, 2019
b775684
Lint
Dec 17, 2019
2e04e60
clarify note
Dec 17, 2019
9b20ff5
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 18, 2019
0c14033
Add apply function cache to save compiled numba functions
Dec 19, 2019
c7106dc
Add performance example
Dec 19, 2019
1640085
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 21, 2019
2846faf
Remove whitespace
Dec 21, 2019
5a645c0
Address lint errors and separate apply tests
Dec 22, 2019
6bac000
Add whatsnew note
Dec 22, 2019
6f1c73f
Skip apply tests for numba not installed, lint
Dec 22, 2019
a890337
Add typing
Dec 22, 2019
0a9071c
Add more typing
Dec 22, 2019
9d8d40b
Formatting cleanups
Dec 23, 2019
84c3491
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 24, 2019
a429206
Address Jeff's comments
Dec 24, 2019
5826ad9
Black
Dec 24, 2019
cf7571b
Add clarification
Dec 24, 2019
4bc9787
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 24, 2019
18eed60
Move function to module level
Dec 24, 2019
f715b55
move cache check higher up
Dec 24, 2019
6a765bf
Address Will's comments
Dec 24, 2019
af3fe50
Type Callable in generate_numba_apply_func
Dec 24, 2019
eb7b5e1
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 24, 2019
f7dfcf4
use ellipsis, cannot specify np.ndarray as well
Dec 24, 2019
a42a960
Remove trailing whitespace in apply docstring
Dec 24, 2019
d019830
Address Will's and Brock's comments
Dec 25, 2019
29d145f
Fix typing
Dec 25, 2019
248149c
Merge remote-tracking branch 'upstream/master' into numba_rolling_apply
Dec 26, 2019
a3da51e
Address followup comments
Dec 26, 2019
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
Prev Previous commit
Next Next commit
Add apply function cache to save compiled numba functions
  • Loading branch information
Matt Roeschke committed Dec 19, 2019
commit 0c140330b36ad25ff86d1b4ce3d615d050517928
1 change: 1 addition & 0 deletions pandas/core/window/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def _apply(
floor: int = 1,
is_weighted: bool = False,
name: Optional[str] = None,
use_numba_cache: Optional = False,
**kwargs,
):
"""
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/window/numba_.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@


def _generate_numba_apply_func(
Copy link
Contributor

Choose a reason for hiding this comment

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

ok with this being non-private (e.g. generate_numba_apply_func), unless its only used in this module

args: Tuple, kwargs: Dict, func: Callable, engine_kwargs: Optional[Dict]
args: Tuple,
kwargs: Dict,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
kwargs: Dict,
kwargs: Dict[str, Any],

func: Callable,
Copy link
Member

Choose a reason for hiding this comment

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

Does Callable need to return an ndarray? Adding sub-types to Callable helps immensely so would be preferable to add if so

Copy link
Member Author

Choose a reason for hiding this comment

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

Callable here should take in an ndarray and potentially *args and return a scalar. What would be the best way to type that?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm not sure about the *args piece but here are the docs for it:

https://docs.python.org/3/library/typing.html#typing.Callable

So I would think Callable[[np.ndarray, ...], Scalar] if it works otherwise Callable[..., Scalar] is the best we can do.

Scalar can be imported from pandas._typing

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. I'll give that a shot.

Copy link
Member Author

Choose a reason for hiding this comment

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

Had to go with Callable[..., Scalar] as also specifying np.ndarray was raising errors.

engine_kwargs: Optional[Dict],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
engine_kwargs: Optional[Dict],
engine_kwargs: Optional[Dict[str, Any]],

Copy link
Member

Choose a reason for hiding this comment

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

Slightly more explicit would be preferable

function_cache: Dict,
Copy link
Member

Choose a reason for hiding this comment

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

Can you add subtypes here? Is this Dict[Callable, ???]?

):
"""
Generate a numba jitted apply function specified by values from engine_kwargs.
Expand Down Expand Up @@ -37,6 +41,10 @@ def _generate_numba_apply_func(
else:
loop_range = range

# Return an already compiled version of roll_apply if available
if func in function_cache:
return function_cache[func]

def make_rolling_apply(func):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move make_rolling_apply to module scope (out of this function)?

also don't you need to actually assign to the cache? (on a miss)

Copy link
Member Author

Choose a reason for hiding this comment

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

The cache assignment happens here https://github.com/pandas-dev/pandas/pull/30151/files#diff-0de5c5d9abfcdd141e83701eaaec4358R541 (the function needs to run on some data first)

Copy link
Contributor

Choose a reason for hiding this comment

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

then i wouldn’t even check the cache here ; that must be at the higher level

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay sure thing. I'll move it up then.


if isinstance(func, numba.targets.registry.CPUDispatcher):
Expand Down
24 changes: 19 additions & 5 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def __init__(
self.win_freq = None
self.axis = obj._get_axis_number(axis) if axis is not None else None
self.validate()
self._numba_func_cache = dict()

@property
def _constructor(self):
Expand Down Expand Up @@ -443,6 +444,7 @@ def _apply(
floor: int = 1,
is_weighted: bool = False,
name: Optional[str] = None,
use_numba_cache: Optional[bool] = False,
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 a reason we would not want this ever? (e.g. shouldn't we always cache)

Copy link
Member Author

Choose a reason for hiding this comment

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

The variable name is a bit of a misnomer. use_numba_cache tells a downstream routine (_apply) that we can cache the numba function

i.e. this is False for every other rolling method that cant cache yet and can be True for apply

Copy link
Contributor

Choose a reason for hiding this comment

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

k cool, can you document that somewhere

Copy link
Member

Choose a reason for hiding this comment

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

This can just be annotated as bool no? Or do we need to explicitly handle None?

Copy link
Member

Choose a reason for hiding this comment

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

seconding Will's question. this appears above too

**kwargs,
):
"""
Expand All @@ -455,10 +457,11 @@ def _apply(
func : callable function to apply
center : bool
require_min_periods : int
floor: int
is_weighted
name: str,
floor : int
is_weighted : bool
name : str,
compatibility with groupby.rolling
use_numba_cache : bool
**kwargs
additional arguments for rolling function and window function

Expand Down Expand Up @@ -533,6 +536,9 @@ def calc(x):
result = calc(values)
result = np.asarray(result)

if use_numba_cache:
self._numba_func_cache[name] = func

if center:
result = self._center_window(result, window)

Expand Down Expand Up @@ -1303,13 +1309,21 @@ def apply(
elif engine == "numba":
if raw is False:
raise ValueError("raw must be `True` when using the numba engine")
apply_func = _generate_numba_apply_func(args, kwargs, func, engine_kwargs)
apply_func = _generate_numba_apply_func(
args, kwargs, func, engine_kwargs, self._numba_func_cache
)
else:
raise ValueError("engine must be either 'numba' or 'cython'")

# TODO: Why do we always pass center=False?
# name=func for WindowGroupByMixin._apply
return self._apply(apply_func, center=False, floor=0, name=func)
return self._apply(
apply_func,
center=False,
floor=0,
name=func,
use_numba_cache=engine == "numba",
)

def _generate_cython_apply_func(self, args, kwargs, raw, offset, func):
from pandas import Series
Expand Down
0