From 9cb4258afffe97a9bd5e23514e51af7d1b30b5ef Mon Sep 17 00:00:00 2001 From: sanleo-wq Date: Tue, 13 May 2025 11:35:19 +0200 Subject: [PATCH 1/2] Implement numpy.prod method for OpenVino backend --- .gitignore | 3 ++- keras/src/backend/openvino/numpy.py | 26 +++++++++++++++++++++++++- pytest.ini | 3 +++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 pytest.ini diff --git a/.gitignore b/.gitignore index afd700b49952..07d3abbcbf14 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ examples/**/*.jpg .python-version .coverage *coverage.xml -.ruff_cache \ No newline at end of file +.ruff_cachekeras_venv/ +keras_venv/ \ No newline at end of file diff --git a/keras/src/backend/openvino/numpy.py b/keras/src/backend/openvino/numpy.py index bbd23c633953..ca220cee8fa2 100644 --- a/keras/src/backend/openvino/numpy.py +++ b/keras/src/backend/openvino/numpy.py @@ -1230,7 +1230,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) + + return OpenVINOKerasTensor(result) def quantile(x, q, axis=None, method="linear", keepdims=False): diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 000000000000..83635a5b7b9b --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +env = + KERAS_BACKEND=openvino \ No newline at end of file From cc8ec1870213bb264bbeda7d9eef29250a595533 Mon Sep 17 00:00:00 2001 From: sanleo-wq Date: Tue, 13 May 2025 12:08:17 +0200 Subject: [PATCH 2/2] Delete pytest.ini and clean the gitignore --- .gitignore | 3 +-- pytest.ini | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 pytest.ini diff --git a/.gitignore b/.gitignore index 07d3abbcbf14..762428074e3a 100644 --- a/.gitignore +++ b/.gitignore @@ -19,5 +19,4 @@ examples/**/*.jpg .python-version .coverage *coverage.xml -.ruff_cachekeras_venv/ -keras_venv/ \ No newline at end of file +.ruff_cachekeras_venv/ \ No newline at end of file diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index 83635a5b7b9b..000000000000 --- a/pytest.ini +++ /dev/null @@ -1,3 +0,0 @@ -[pytest] -env = - KERAS_BACKEND=openvino \ No newline at end of file