Skip to content

Implement numpy.prod method for OpenVino backend #21282

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 3 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: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ examples/**/*.jpg
.python-version
.coverage
*coverage.xml
.ruff_cache
.ruff_cachekeras_venv/
Copy link
Contributor

Choose a reason for hiding this comment

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

please remove changes here.
Switch on tests for prod.

26 changes: 25 additions & 1 deletion keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,31 @@ 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")
x = get_ov_output(x)
original_type = x.get_element_type()

if dtype is not None:
ov_type = OPENVINO_DTYPES[standardize_dtype(dtype)]
x = ov_opset.convert(x, ov_type).output(0)

if isinstance(axis, tuple) and len(axis) == 0:
return OpenVINOKerasTensor(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_const = ov_opset.constant(axis, Type.i32).output(0)
result = ov_opset.reduce_prod(x, axis_const, keepdims).output(0)

if dtype is None and original_type != result.get_element_type():
result = ov_opset.convert(result, original_type).output(0)
Comment on lines +1268 to +1269
Copy link
Contributor

Choose a reason for hiding this comment

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

according to the logic, if dtype is None, original_type is always equal to result's one. Please correct me.
That is because reshape and reduce_prod preserve tensor type:)


return OpenVINOKerasTensor(result)


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