Skip to content

Commit bf1c65c

Browse files
authored
[WIP] ENH Adds tree reduction to bincount (#7183)
* ENH Adds tree reduction to bincount * CLN Uses meta from small bincount * REV Revert change * ENH Early exit if not a list
1 parent 67d8574 commit bf1c65c

2 files changed

Lines changed: 31 additions & 27 deletions

File tree

‎dask/array/routines.py‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,10 @@ def gradient(f, *varargs, **kwargs):
620620
return results
621621

622622

623-
def _bincount_sum(bincounts, dtype=int):
623+
def _bincount_agg(bincounts, dtype, **kwargs):
624+
if not isinstance(bincounts, list):
625+
return bincounts
626+
624627
n = max(map(len, bincounts))
625628
out = zeros_like_safe(bincounts[0], shape=n, dtype=dtype)
626629
for b in bincounts:
@@ -629,43 +632,38 @@ def _bincount_sum(bincounts, dtype=int):
629632

630633

631634
@derived_from(np)
632-
def bincount(x, weights=None, minlength=0):
635+
def bincount(x, weights=None, minlength=0, split_every=None):
633636
if x.ndim != 1:
634637
raise ValueError("Input array must be one dimensional. Try using x.ravel()")
635638
if weights is not None:
636639
if weights.chunks != x.chunks:
637640
raise ValueError("Chunks of input array x and weights must match.")
638641

639642
token = tokenize(x, weights, minlength)
640-
name = "bincount-" + token
641-
final_name = "bincount-sum" + token
642-
# Call np.bincount on each block, possibly with weights
643+
args = [x, "i"]
643644
if weights is not None:
644-
dsk = {
645-
(name, i): (np.bincount, (x.name, i), (weights.name, i), minlength)
646-
for i, _ in enumerate(x.__dask_keys__())
647-
}
648-
dtype = np.bincount([1], weights=[1]).dtype
645+
meta = np.bincount([1], weights=[1])
646+
args.extend([weights, "i"])
649647
else:
650-
dsk = {
651-
(name, i): (np.bincount, (x.name, i), None, minlength)
652-
for i, _ in enumerate(x.__dask_keys__())
653-
}
654-
dtype = np.bincount([]).dtype
648+
meta = np.bincount([])
655649

656-
dsk[(final_name, 0)] = (_bincount_sum, list(dsk), dtype)
657-
graph = HighLevelGraph.from_collections(
658-
final_name, dsk, dependencies=[x] if weights is None else [x, weights]
650+
chunked_counts = blockwise(
651+
partial(np.bincount, minlength=minlength), "i", *args, token=token, meta=meta
659652
)
660653

661-
if minlength == 0:
662-
chunks = ((np.nan,),)
663-
else:
664-
chunks = ((minlength,),)
665-
666-
meta = meta_from_array(x, 1, dtype=dtype)
654+
from .reductions import _tree_reduce
667655

668-
return Array(graph, final_name, chunks, meta=meta)
656+
output = _tree_reduce(
657+
chunked_counts,
658+
aggregate=partial(_bincount_agg, dtype=meta.dtype),
659+
axis=(0,),
660+
keepdims=False,
661+
dtype=meta.dtype,
662+
split_every=split_every,
663+
concatenate=False,
664+
)
665+
output._meta = meta
666+
return output
669667

670668

671669
@derived_from(np)

‎dask/array/tests/test_routines.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,16 @@ def test_bincount():
535535
assert da.bincount(d, minlength=6).name == da.bincount(d, minlength=6).name
536536

537537

538-
def test_bincount_with_weights():
538+
@pytest.mark.parametrize(
539+
"weights",
540+
[
541+
np.array([1, 2, 1, 0.5, 1], dtype=np.float32),
542+
np.array([1, 2, 1, 0, 1], dtype=np.int32),
543+
],
544+
)
545+
def test_bincount_with_weights(weights):
539546
x = np.array([2, 1, 5, 2, 1])
540547
d = da.from_array(x, chunks=2)
541-
weights = np.array([1, 2, 1, 0.5, 1])
542548

543549
dweights = da.from_array(weights, chunks=2)
544550
e = da.bincount(d, weights=dweights, minlength=6)

0 commit comments

Comments
 (0)