Skip to content

Add support for numpy.flip in OpenVINO backend #21132

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 2 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 keras/src/backend/openvino/excluded_concrete_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ NumpyDtypeTest::test_digitize
NumpyDtypeTest::test_einsum
NumpyDtypeTest::test_exp2
NumpyDtypeTest::test_eye
NumpyDtypeTest::test_flip
NumpyDtypeTest::test_floor
NumpyDtypeTest::test_hstack
NumpyDtypeTest::test_inner
Expand Down Expand Up @@ -88,6 +87,7 @@ NumpyOneInputOpsCorrectnessTest::test_cumprod
NumpyOneInputOpsCorrectnessTest::test_diag
NumpyOneInputOpsCorrectnessTest::test_diagonal
NumpyOneInputOpsCorrectnessTest::test_exp2
NumpyOneInputOpsCorrectnessTest::test_expm1
Copy link
Contributor

Choose a reason for hiding this comment

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

please revert this change that is not related to PR

NumpyOneInputOpsCorrectnessTest::test_flip
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 the line with NumpyOneInputOpsCorrectnessTest::test_flip

NumpyOneInputOpsCorrectnessTest::test_floor_divide
NumpyOneInputOpsCorrectnessTest::test_hstack
Expand Down
31 changes: 30 additions & 1 deletion keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,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)
Comment on lines +743 to +746
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Hi @rkazants ,
I have a few prior deadlines this week, so I might need a bit of time to incorporate the requested changes.
I’ll get to it as soon as possible.

Thank you for your patience!

return OpenVINOKerasTensor(x)


def floor(x):
Expand Down