From 7e529be79ea9ddbda176d1ebf3e1bf3078be1a4e Mon Sep 17 00:00:00 2001 From: Praroop Chanda Date: Fri, 4 Apr 2025 02:10:40 -0500 Subject: [PATCH] Add support for numpy.flip in OpenVINO backend --- .../openvino/excluded_concrete_tests.txt | 2 -- keras/src/backend/openvino/numpy.py | 31 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/keras/src/backend/openvino/excluded_concrete_tests.txt b/keras/src/backend/openvino/excluded_concrete_tests.txt index 3000452394c..3dc00d90482 100644 --- a/keras/src/backend/openvino/excluded_concrete_tests.txt +++ b/keras/src/backend/openvino/excluded_concrete_tests.txt @@ -23,7 +23,6 @@ NumpyDtypeTest::test_einsum NumpyDtypeTest::test_exp2 NumpyDtypeTest::test_expm1 NumpyDtypeTest::test_eye -NumpyDtypeTest::test_flip NumpyDtypeTest::test_floor NumpyDtypeTest::test_hstack NumpyDtypeTest::test_identity @@ -92,7 +91,6 @@ NumpyOneInputOpsCorrectnessTest::test_diag NumpyOneInputOpsCorrectnessTest::test_diagonal NumpyOneInputOpsCorrectnessTest::test_exp2 NumpyOneInputOpsCorrectnessTest::test_expm1 -NumpyOneInputOpsCorrectnessTest::test_flip NumpyOneInputOpsCorrectnessTest::test_floor_divide NumpyOneInputOpsCorrectnessTest::test_hstack NumpyOneInputOpsCorrectnessTest::test_imag diff --git a/keras/src/backend/openvino/numpy.py b/keras/src/backend/openvino/numpy.py index bc0276d4373..c6c0690c10d 100644 --- a/keras/src/backend/openvino/numpy.py +++ b/keras/src/backend/openvino/numpy.py @@ -707,7 +707,36 @@ def expm1(x): def flip(x, axis=None): - raise NotImplementedError("`flip` is not supported with openvino backend") + if axis == () or axis == []: + return x + x = get_ov_output(x) + rank = x.get_partial_shape().rank.get_length() + if axis is None: + axis = list(range(rank)) + else: + if np.isscalar(axis): + axis = [axis] + else: + axis = list(axis) + axis = [ax if ax >= 0 else ax + rank for ax in axis] + shape_of_x = ov_opset.shape_of(x) + for ax in sorted(axis): + dim_node = ov_opset.gather( + shape_of_x, + ov_opset.constant(ax, Type.i64).output(0), + ov_opset.constant(0, Type.i64).output(0), + ).output(0) + start = ov_opset.subtract( + dim_node, ov_opset.constant(1, Type.i64).output(0) + ).output(0) + stop = ov_opset.constant(-1, Type.i64).output(0) + step = ov_opset.constant(-1, Type.i64).output(0) + + reversed_indices = ov_opset.range(start, stop, step, Type.i64).output(0) + x = ov_opset.gather( + x, reversed_indices, ov_opset.constant(ax, Type.i64).output(0) + ).output(0) + return OpenVINOKerasTensor(x) def floor(x):