From 9f279c99efd328964b3d2c0dffa301a780a2065c Mon Sep 17 00:00:00 2001 From: sanleo-wq Date: Tue, 13 May 2025 14:50:24 +0200 Subject: [PATCH] implement numpy.mechgrid in OpenVino backend --- .gitignore | 3 +- .../openvino/excluded_concrete_tests.txt | 2 - keras/src/backend/openvino/numpy.py | 60 ++++++++++++++++++- pytest.ini | 3 + 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 pytest.ini diff --git a/.gitignore b/.gitignore index afd700b49952..7bce46435932 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_cache +keras_venv/ \ No newline at end of file diff --git a/keras/src/backend/openvino/excluded_concrete_tests.txt b/keras/src/backend/openvino/excluded_concrete_tests.txt index 7371a2b9a5b8..7dd4cd195d91 100644 --- a/keras/src/backend/openvino/excluded_concrete_tests.txt +++ b/keras/src/backend/openvino/excluded_concrete_tests.txt @@ -36,7 +36,6 @@ NumpyDtypeTest::test_matmul_ NumpyDtypeTest::test_max NumpyDtypeTest::test_mean NumpyDtypeTest::test_median -NumpyDtypeTest::test_meshgrid NumpyDtypeTest::test_minimum_python_types NumpyDtypeTest::test_moveaxis NumpyDtypeTest::test_multiply @@ -95,7 +94,6 @@ NumpyOneInputOpsCorrectnessTest::test_logaddexp NumpyOneInputOpsCorrectnessTest::test_max NumpyOneInputOpsCorrectnessTest::test_mean NumpyOneInputOpsCorrectnessTest::test_median -NumpyOneInputOpsCorrectnessTest::test_meshgrid NumpyOneInputOpsCorrectnessTest::test_moveaxis NumpyOneInputOpsCorrectnessTest::test_pad_float16_constant_2 NumpyOneInputOpsCorrectnessTest::test_pad_float32_constant_2 diff --git a/keras/src/backend/openvino/numpy.py b/keras/src/backend/openvino/numpy.py index bbd23c633953..c086f123066e 100644 --- a/keras/src/backend/openvino/numpy.py +++ b/keras/src/backend/openvino/numpy.py @@ -1046,9 +1046,63 @@ def median(x, axis=None, keepdims=False): def meshgrid(*x, indexing="xy"): - raise NotImplementedError( - "`meshgrid` is not supported with openvino backend" - ) + if not x: + return [] + + if indexing not in ["xy", "ij"]: + raise ValueError( + f"indexing parameter must be either 'xy' or 'ij', got {indexing}" + ) + + arrays = [] + for array in x: + arrays.append(get_ov_output(array)) + + ndim = len(arrays) + s0 = arrays[0].get_element_type() + + output = [] + + for i, xi in enumerate(arrays): + shape = [1] * ndim + + if indexing == "xy" and ndim > 1: + if i == 0: + shape[1] = -1 + elif i == 1: + shape[0] = -1 + else: + shape[i] = -1 + else: + shape[i] = -1 + + reshape_node = ov_opset.reshape( + xi, ov_opset.constant(shape, Type.i32), False + ).output(0) + + output_shape = [1] * ndim + for j, xj in enumerate(arrays): + xj_shape = xj.get_partial_shape().to_shape() + if xj_shape and xj_shape[0] > 0: + output_shape[ + j + if indexing == "ij" + else ( + 1 + if j == 0 and ndim > 1 + else 0 + if j == 1 and ndim > 1 + else j + ) + ] = xj_shape[0] + + broadcast_node = ov_opset.broadcast( + reshape_node, ov_opset.constant(output_shape, Type.i32) + ).output(0) + + output.append(OpenVINOKerasTensor(broadcast_node)) + + return output def min(x, axis=None, keepdims=False, initial=None): 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