Skip to content

Commit d462aa2

Browse files
Mohamed-Ashraf27311happy
authored andcommitted
[OpenVino BackEnd]support np.count_nonzero for ov BackEnd (keras-team#20945)
* suppoer np.count_nonzero for ov BackEnd * modifing function vars to lowercase
1 parent 288be4d commit d462aa2

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

keras/src/backend/openvino/excluded_concrete_tests.txt

-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ NumpyDtypeTest::test_bitwise
1616
NumpyDtypeTest::test_ceil
1717
NumpyDtypeTest::test_concatenate
1818
NumpyDtypeTest::test_correlate
19-
NumpyDtypeTest::test_count_nonzero
2019
NumpyDtypeTest::test_cross
2120
NumpyDtypeTest::test_cumprod
2221
NumpyDtypeTest::test_cumsum_bool
@@ -97,7 +96,6 @@ NumpyOneInputOpsCorrectnessTest::test_array
9796
NumpyOneInputOpsCorrectnessTest::test_bitwise_invert
9897
NumpyOneInputOpsCorrectnessTest::test_conj
9998
NumpyOneInputOpsCorrectnessTest::test_correlate
100-
NumpyOneInputOpsCorrectnessTest::test_count_nonzero
10199
NumpyOneInputOpsCorrectnessTest::test_cumprod
102100
NumpyOneInputOpsCorrectnessTest::test_diag
103101
NumpyOneInputOpsCorrectnessTest::test_diagonal

keras/src/backend/openvino/numpy.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,21 @@ def cosh(x):
473473

474474

475475
def count_nonzero(x, axis=None):
476-
raise NotImplementedError(
477-
"`count_nonzero` is not supported with openvino backend"
478-
)
476+
x = get_ov_output(x)
477+
zero_constant = ov_opset.constant(0, dtype=Type.i32).output(0)
478+
zero_constant = ov_opset.convert_like(zero_constant, x)
479+
x = ov_opset.not_equal(x, zero_constant).output(0)
480+
x = ov_opset.convert(x, Type.i32).output(0)
481+
if axis is None:
482+
flatten_shape = ov_opset.constant([-1], Type.i32).output(0)
483+
x = ov_opset.reshape(x, flatten_shape, False).output(0)
484+
axis = 0
485+
if isinstance(axis, tuple):
486+
axis = list(axis)
487+
if axis == []:
488+
return OpenVINOKerasTensor(x)
489+
axis = ov_opset.constant(axis, Type.i32).output(0)
490+
return OpenVINOKerasTensor(ov_opset.reduce_sum(x, axis, False).output(0))
479491

480492

481493
def cross(x1, x2, axisa=-1, axisb=-1, axisc=-1, axis=None):

0 commit comments

Comments
 (0)