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 3 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
61 changes: 60 additions & 1 deletion keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,67 @@ 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]
Comment on lines +1125 to +1142
Copy link
Contributor

Choose a reason for hiding this comment

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



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()

# Promote dtype if not explicitly specified
if dtype is None:
if x_type == Type.boolean:
promoted_dtype = Type.i32
elif x_type in (Type.i8, Type.i16):
promoted_dtype = Type.i32
elif x_type in (Type.u8, Type.u16):
promoted_dtype = Type.u32
else:
promoted_dtype = x_type
else:
promoted_dtype = string_to_ov_type(dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

we don't need any type promotion here. Please remove

Copy link
Author

@Prabha14039 Prabha14039 Apr 21, 2025

Choose a reason for hiding this comment

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

I removed the dtype promotion as you suggested (like uint8 → uint32, etc.), but now a few CI tests are failing because they still expect the promoted dtype (e.g., jnp.prod(uint8) → uint32, but OpenVINO now stays uint8).Could you please let me know that should i remove it or not or waht other ways should i try it

Copy link
Author

Choose a reason for hiding this comment

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

error

Copy link
Author

Choose a reason for hiding this comment

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

have provided the test case that is failing
image


# Cast to promoted dtype if necessary
if x_type != promoted_dtype:
x = ov_opset.convert(x, promoted_dtype).output(0)

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)

return OpenVINOKerasTensor(
ov_opset.reduce_prod(x, axis, keepdims).output(0)
)


def quantile(x, q, axis=None, method="linear", keepdims=False):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ torch-xla==2.6.0;sys_platform != 'darwin'
# Jax.
# Pinned to 0.5.0 on CPU. JAX 0.5.1 requires Tensorflow 2.19 for saved_model_test.
# Note that we test against the latest JAX on GPU.

Copy link
Contributor

Choose a reason for hiding this comment

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

revert this change

jax[cpu]==0.5.0
flax

Expand Down