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

Add bincount method to array class #1874

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion mlx/array.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright © 2023-2024 Apple Inc.
#include <functional>
#include <unordered_map>

Expand Down Expand Up @@ -341,4 +340,22 @@ array::ArrayIterator::reference array::ArrayIterator::operator*() const {
return reshape(slice(arr, start, end), shape);
};

array bincount(const array& input, int max_val) {
std::unordered_map<int, int> counts;
for (int i = 0; i < input.size(); ++i) {
int val = input.data<int>()[i];
if (val < 0 || val >= max_val) {
throw std::out_of_range("Input value out of range");
}
counts[val]++;
}

std::vector<int> result(max_val, 0);
for (const auto& [val, count] : counts) {
result[val] = count;
}

return array(result);
}

} // namespace mlx::core
4 changes: 3 additions & 1 deletion mlx/array.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright © 2023 Apple Inc.
#pragma once

#include <algorithm>
Expand Down Expand Up @@ -446,6 +445,9 @@ class array {

~array();

// Declare the bincount method in the array class
static array bincount(const array& input, int max_val);

private:
// Initialize the arrays data
template <typename It>
Expand Down
20 changes: 18 additions & 2 deletions python/src/array.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright © 2023-2024 Apple Inc.
#include <cstdint>
#include <cstring>
#include <sstream>
Expand Down Expand Up @@ -1394,5 +1393,22 @@ void init_array(nb::module_& m) {
"dtype"_a,
nb::kw_only(),
"stream"_a = nb::none(),
"See :func:`view`.");
"See :func:`view`.")
.def_static(
"bincount",
[](const mx::array& input, int max_val) {
return mx::array::bincount(input, max_val);
},
"input"_a,
"max_val"_a,
R"pbdoc(
Count occurrences of each value in an array.

Args:
input (array): Input array.
max_val (int): Maximum value in the input array.

Returns:
array: Array with the counts of each value in the input array.
)pbdoc");
}
37 changes: 35 additions & 2 deletions python/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,39 @@ def t():
used = get_mem()
self.assertEqual(expected, used)

def test_bincount(self):
# Test case 1: Basic functionality
input_array = mx.array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3])
max_val = 4
expected_output = mx.array([1, 2, 3, 4])
self.assertTrue(mx.array_equal(mx.array.bincount(input_array, max_val), expected_output))

# Test case 2: Input with zeros
input_array = mx.array([0, 0, 0, 1, 1, 2])
max_val = 3
expected_output = mx.array([3, 2, 1])
self.assertTrue(mx.array_equal(mx.array.bincount(input_array, max_val), expected_output))

# Test case 3: Input with negative values (should raise an error)
input_array = mx.array([0, -1, 1, 2])
max_val = 3
with self.assertRaises(ValueError):
mx.array.bincount(input_array, max_val)

if __name__ == "__main__":
unittest.main()
# Test case 4: Input with values out of range (should raise an error)
input_array = mx.array([0, 1, 2, 3, 4])
max_val = 3
with self.assertRaises(ValueError):
mx.array.bincount(input_array, max_val)

# Test case 5: Empty input array
input_array = mx.array([])
max_val = 3
expected_output = mx.array([0, 0, 0])
self.assertTrue(mx.array_equal(mx.array.bincount(input_array, max_val), expected_output))

# Test case 6: Input with all values the same
input_array = mx.array([2, 2, 2, 2])
max_val = 3
expected_output = mx.array([0, 0, 4])
self.assertTrue(mx.array_equal(mx.array.bincount(input_array, max_val), expected_output))