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

[OpenVINO backend]: Support numpy.bincount #20940

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions keras/src/backend/openvino/excluded_concrete_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ NumpyDtypeTest::test_argpartition
NumpyDtypeTest::test_argsort
NumpyDtypeTest::test_array
NumpyDtypeTest::test_bincount
NumpyDtypeTest::test_average_
NumpyDtypeTest::test_bitwise
NumpyDtypeTest::test_ceil
NumpyDtypeTest::test_concatenate
Expand Down Expand Up @@ -95,6 +96,7 @@ NumpyOneInputOpsCorrectnessTest::test_argmin
NumpyOneInputOpsCorrectnessTest::test_argpartition
NumpyOneInputOpsCorrectnessTest::test_argsort
NumpyOneInputOpsCorrectnessTest::test_array
NumpyOneInputOpsCorrectnessTest::test_average
NumpyOneInputOpsCorrectnessTest::test_bincount
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you should only remove lines corresponding to bincount tests.
Now you are adding for average for some reason

Copy link
Author

Choose a reason for hiding this comment

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

sorry, while resolving the merge conflict I took a merge with the master branch and it was an incoming change so I did kept a combination of both current and incoming, I will remove it.
Thank you

NumpyOneInputOpsCorrectnessTest::test_bitwise_invert
NumpyOneInputOpsCorrectnessTest::test_conj
Expand Down
33 changes: 31 additions & 2 deletions keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,38 @@ def average(x, axis=None, weights=None):


def bincount(x, weights=None, minlength=0, sparse=False):
raise NotImplementedError(
"`bincount` is not supported with openvino backend"
if x is None:
raise ValueError("input x is None")
if sparse:
raise ValueError("Unsupported value `sparse=True`")
x = get_ov_output(x)
x_type = x.get_element_type()
minlength = get_ov_output(minlength)
minlength = ov_opset.convert(minlength, x_type).output(0)
const_one = ov_opset.constant(1, x_type).output(0)
const_zero = ov_opset.constant(0, x_type).output(0)
max_element = ov_opset.reduce_max(x, const_zero, keep_dims=False).output(0)
depth = ov_opset.add(max_element, const_one).output(0)
depth = ov_opset.maximum(depth, minlength).output(0)
one_hot = ov_opset.one_hot(x, depth, const_one, const_zero, axis=-1).output(
0
)
if weights is not None:
weights = get_ov_output(weights)
weights_type = weights.get_element_type()
weights_new = ov_opset.reshape(weights, [-1, 1], False).output(0)
one_hot = ov_opset.convert(one_hot, weights_type).output(0)
final_one_hot = ov_opset.multiply(one_hot, weights_new).output(0)
final_output = ov_opset.reduce_sum(
final_one_hot, const_zero, keep_dims=False
).output(0)
return OpenVINOKerasTensor(final_output)
else:
final_output = ov_opset.reduce_sum(
one_hot, const_zero, keep_dims=False
).output(0)
final_output = ov_opset.convert(final_output, Type.i32).output(0)
return OpenVINOKerasTensor(final_output)


def broadcast_to(x, shape):
Expand Down
4 changes: 2 additions & 2 deletions keras/src/ops/numpy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3579,8 +3579,8 @@ def test_bincount(self, sparse_input, sparse_arg):
)
self.assertSparse(output, sparse_input or sparse_arg)

x = knp.expand_dims(x, 0)
weights = knp.expand_dims(weights, 0)
x = np.expand_dims(x, 0)
weights = np.expand_dims(weights, 0)
Comment on lines -3582 to +3583
Copy link
Contributor

Choose a reason for hiding this comment

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

please revert these changes

Copy link
Author

Choose a reason for hiding this comment

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

Earlier the openvino CI was failing because of this as in the implementation of expand_dims if input is not an instance of openvinokerastensor it was going in else statement causing a false assert.


expected_output = np.array([[0, 0, 4, 2, 5, 0, 2]])
output = knp.bincount(
Expand Down
Loading