Skip to content

[Keras 3 OpenVINO Backend]: Support numpy.prod operation #21405

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

Open
wants to merge 5 commits 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: 0 additions & 2 deletions keras/src/backend/openvino/excluded_concrete_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ NumpyDtypeTest::test_meshgrid
NumpyDtypeTest::test_minimum_python_types
NumpyDtypeTest::test_multiply
NumpyDtypeTest::test_power
NumpyDtypeTest::test_prod
NumpyDtypeTest::test_quantile
NumpyDtypeTest::test_repeat
NumpyDtypeTest::test_roll
Expand Down Expand Up @@ -104,7 +103,6 @@ NumpyOneInputOpsCorrectnessTest::test_pad_int16_constant_2
NumpyOneInputOpsCorrectnessTest::test_pad_int8_constant_2
NumpyOneInputOpsCorrectnessTest::test_pad_uint8_constant_2
NumpyOneInputOpsCorrectnessTest::test_pad_int32_constant_2
NumpyOneInputOpsCorrectnessTest::test_prod
NumpyOneInputOpsCorrectnessTest::test_real
NumpyOneInputOpsCorrectnessTest::test_repeat
NumpyOneInputOpsCorrectnessTest::test_reshape
Expand Down
32 changes: 31 additions & 1 deletion keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,37 @@ def pad(x, pad_width, mode="constant", constant_values=None):


def prod(x, axis=None, keepdims=False, dtype=None):
raise NotImplementedError("`prod` is not supported with openvino backend")
if axis == () or axis == []:
return x
x = get_ov_output(x)
x_type = x.get_element_type()
if axis is None:
flatten_shape = ov_opset.constant([-1], Type.i32).output(0)
x = ov_opset.reshape(x, flatten_shape, False).output(0)
axis = 0
elif isinstance(axis, tuple):
axis = list(axis)
axis = ov_opset.constant(axis, Type.i32).output(0)
if x_type == Type.boolean:
result = ov_opset.reduce_logical_and(x, axis, keepdims).output(0)
result = ov_opset.convert(result, Type.i32).output(0)
return OpenVINOKerasTensor(result)
promotion_map = {
Type.i8: Type.i32,
Type.i16: Type.i32,
Type.u8: Type.u32,
Type.u16: Type.u32,
Type.boolean: Type.i32,
}
promoted_type = promotion_map.get(x_type, x_type)
if x_type != promoted_type:
x = ov_opset.convert(x, promoted_type).output(0)
result = ov_opset.reduce_prod(x, axis, keepdims).output(0)
if dtype is not None:
dtype_string = standardize_dtype(dtype)
target_type = OPENVINO_DTYPES[dtype_string]
result = ov_opset.convert(result, target_type).output(0)
return OpenVINOKerasTensor(result)


def quantile(x, q, axis=None, method="linear", keepdims=False):
Expand Down