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

Optimize memory allocation for counts #159

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
26 changes: 20 additions & 6 deletions numbagg/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,18 +432,21 @@ def __init__(
values_dtypes, labels_dtypes
)
]
nargs = len(signature[0])
for sig in signature:
if not isinstance(sig, tuple):
raise TypeError(f"signatures for ndmoving must be tuples: {signature}")
if len(sig) != 3:
if len(sig) != nargs:
raise TypeError(
"signature has wrong number of arguments != 3: " f"{signature}"
f"signature has wrong number of argument != {nargs}: "
f"{signature}"
)
if any(ndim(arg) != 0 for arg in sig):
raise ValueError(
"all arguments in signature for ndreduce must be scalars: "
f" {signature}"
)
self.needs_count = nargs == 4

self.signature = signature

Expand All @@ -456,12 +459,18 @@ def gufunc(self, core_ndim, *, target):
numba_sig = []
slices = (slice(None),) * core_ndim
for input_sig in self.signature:
values, labels, out = input_sig
new_sig = (values[slices], labels[slices], out[:])
if self.needs_count:
values, labels, counts, out = input_sig
new_sig = (values[slices], labels[slices], counts[:], out[:])
else:
values, labels, out = input_sig
new_sig = (values[slices], labels[slices], out[:]) # type: ignore[assignment]
numba_sig.append(new_sig)

first_sig = numba_sig[0]
gufunc_sig = ",".join(2 * [_gufunc_arg_str(first_sig[0])]) + ",(z)"
gufunc_sig = ",".join(2 * [_gufunc_arg_str(first_sig[0])]) + (
",(z)" if not self.needs_count else ",(z),(z)"
)
vectorize = numba.guvectorize(
numba_sig,
gufunc_sig,
Expand Down Expand Up @@ -540,6 +549,7 @@ def __call__(
)
values = np.moveaxis(values, axis, -1)
gufunc = self.gufunc(1, target=target)

else:
values_shape = tuple(values.shape[ax] for ax in axis)
if labels.shape != values_shape:
Expand All @@ -556,7 +566,11 @@ def __call__(
# while `prod` uses 1. So we don't initialize with a value here, and instead
# rely on the function to do so.
result = np.empty(broadcast_shape + (num_labels,), values.dtype)
gufunc(values, labels, result)
if self.needs_count:
counts = np.zeros(result.shape, dtype=np.int64)
gufunc(values, labels, counts, result)
else:
gufunc(values, labels, result)
return result


Expand Down
29 changes: 25 additions & 4 deletions numbagg/grouped.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import numpy as np
from numba.types import float32, float64, int32, int64

from .decorators import groupndreduce


@groupndreduce.wrap()
def group_nanmean(values, labels, out):
counts = np.zeros(out.shape, dtype=labels.dtype)
dtypes = [
(int32, int32, int32),
(int32, int64, int32),
(int64, int32, int64),
(int64, int64, int64),
(float32, int32, float32),
(float32, int64, float32),
(float64, int32, float64),
(float64, int64, float64),
]
dtypes_counts = [
(int32, int32, int64, int32),
(int32, int64, int64, int32),
(int64, int32, int64, int64),
(int64, int64, int64, int64),
(float32, int32, int64, float32),
(float32, int64, int64, float32),
(float64, int32, int64, float64),
(float64, int64, int64, float64),
]
Comment on lines +6 to +25
Copy link
Collaborator

@max-sixty max-sixty Dec 18, 2023

Choose a reason for hiding this comment

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

We've since changed how we specify the signature to include int8, so I'll take a pass at updating this. In particular, we should update the int8 tests to include a function that uses the counts signature.

(When I apply this patch, the tests fail:

diff --git a/numbagg/test/test_grouped.py b/numbagg/test/test_grouped.py
index e399ab5..595e0a6 100644
--- a/numbagg/test/test_grouped.py
+++ b/numbagg/test/test_grouped.py
@@ -373,9 +373,9 @@ def test_int8_again(dtype):
     expected = pd.DataFrame(array.T).groupby(by).sum().T.astype(dtype)
 
     # https://github.com/numbagg/numbagg/issues/213
-    assert_almost_equal(group_nansum(array, by, axis=-1), expected)
+    assert_almost_equal(group_nanmean(array, by, axis=-1), expected)
     # Amazingly it can also be more incorrect with another run!
-    assert_almost_equal(group_nansum(array, by, axis=-1), expected)
+    assert_almost_equal(group_nanmean(array, by, axis=-1), expected)
 
 
 def test_dimensionality():

)



@groupndreduce.wrap(signature=dtypes_counts)
def group_nanmean(values, labels, counts, out):
out[:] = 0.0

for indices in np.ndindex(values.shape):
Expand Down
Loading