Skip to content

Commit 0902ff4

Browse files
[OpenVino BackEnd]support np.count_nonzero for ov BackEnd (#20945)
* suppoer np.count_nonzero for ov BackEnd * modifing function vars to lowercase
1 parent 21c8997 commit 0902ff4

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
@@ -17,7 +17,6 @@ NumpyDtypeTest::test_bitwise
1717
NumpyDtypeTest::test_ceil
1818
NumpyDtypeTest::test_concatenate
1919
NumpyDtypeTest::test_correlate
20-
NumpyDtypeTest::test_count_nonzero
2120
NumpyDtypeTest::test_cross
2221
NumpyDtypeTest::test_cumprod
2322
NumpyDtypeTest::test_cumsum_bool
@@ -99,7 +98,6 @@ NumpyOneInputOpsCorrectnessTest::test_bincount
9998
NumpyOneInputOpsCorrectnessTest::test_bitwise_invert
10099
NumpyOneInputOpsCorrectnessTest::test_conj
101100
NumpyOneInputOpsCorrectnessTest::test_correlate
102-
NumpyOneInputOpsCorrectnessTest::test_count_nonzero
103101
NumpyOneInputOpsCorrectnessTest::test_cumprod
104102
NumpyOneInputOpsCorrectnessTest::test_diag
105103
NumpyOneInputOpsCorrectnessTest::test_diagonal

keras/src/backend/openvino/numpy.py

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

445445

446446
def count_nonzero(x, axis=None):
447-
raise NotImplementedError(
448-
"`count_nonzero` is not supported with openvino backend"
449-
)
447+
x = get_ov_output(x)
448+
zero_constant = ov_opset.constant(0, dtype=Type.i32).output(0)
449+
zero_constant = ov_opset.convert_like(zero_constant, x)
450+
x = ov_opset.not_equal(x, zero_constant).output(0)
451+
x = ov_opset.convert(x, Type.i32).output(0)
452+
if axis is None:
453+
flatten_shape = ov_opset.constant([-1], Type.i32).output(0)
454+
x = ov_opset.reshape(x, flatten_shape, False).output(0)
455+
axis = 0
456+
if isinstance(axis, tuple):
457+
axis = list(axis)
458+
if axis == []:
459+
return OpenVINOKerasTensor(x)
460+
axis = ov_opset.constant(axis, Type.i32).output(0)
461+
return OpenVINOKerasTensor(ov_opset.reduce_sum(x, axis, False).output(0))
450462

451463

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

0 commit comments

Comments
 (0)