Skip to content

Support numpy.prod operation #21188

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 @@ -41,7 +41,6 @@ NumpyDtypeTest::test_multiply
NumpyDtypeTest::test_nan
NumpyDtypeTest::test_outer_
NumpyDtypeTest::test_power
NumpyDtypeTest::test_prod
NumpyDtypeTest::test_quantile
NumpyDtypeTest::test_ravel
NumpyDtypeTest::test_repeat
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_ravel
NumpyOneInputOpsCorrectnessTest::test_real
NumpyOneInputOpsCorrectnessTest::test_reciprocal
Expand Down
46 changes: 45 additions & 1 deletion keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,52 @@ def pad(x, pad_width, mode="constant", constant_values=None):
)


"""
Helper Function to convert the string dtype to ov type
"""


def string_to_ov_type(dtype_str):
from openvino.runtime import Type

mapping = {
"bool": Type.boolean,
"int8": Type.i8,
"int16": Type.i16,
"int32": Type.i32,
"int64": Type.i64,
"uint8": Type.u8,
"uint16": Type.u16,
"uint32": Type.u32,
"uint64": Type.u64,
"float16": Type.f16,
"float32": Type.f32,
"float64": Type.f64,
}
return mapping[dtype_str]


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)

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

if isinstance(axis, tuple):
axis = list(axis)
axis = ov_opset.constant(axis, Type.i32).output(0)

result = ov_opset.reduce_prod(x, axis, keepdims).output(0)

if dtype:
result = ov_opset.convert(result, string_to_ov_type(dtype)).output(0)

return OpenVINOKerasTensor(result)


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