Skip to content

[OpenVINO Backend] Support for numpy.flip #21230

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: 0 additions & 2 deletions keras/src/backend/openvino/excluded_concrete_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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 @@ -82,7 +81,6 @@ NumpyOneInputOpsCorrectnessTest::test_cumprod
NumpyOneInputOpsCorrectnessTest::test_diag
NumpyOneInputOpsCorrectnessTest::test_diagonal
NumpyOneInputOpsCorrectnessTest::test_exp2
NumpyOneInputOpsCorrectnessTest::test_flip
NumpyOneInputOpsCorrectnessTest::test_floor_divide
NumpyOneInputOpsCorrectnessTest::test_hstack
NumpyOneInputOpsCorrectnessTest::test_imag
Expand Down
44 changes: 43 additions & 1 deletion keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,49 @@ def expm1(x):


def flip(x, axis=None):
raise NotImplementedError("`flip` is not supported with openvino backend")
if axis in ((), []):
return x
x = get_ov_output(x)
rank = x.get_partial_shape().rank.get_length()

if axis is None:
axes = list(range(rank))
else:
if np.isscalar(axis):
axes = [int(axis)]
else:
axes = [int(a) for a in axis]
axes = [(a + rank) if a < 0 else a for a in axes]
for ax in sorted(axes):
shape = ov_opset.shape_of(x)

seq_dim = ov_opset.gather(
shape,
ov_opset.constant(ax, Type.i64),
ov_opset.constant(0, Type.i64),
).output(0)

batch_axis = 0 if ax != 0 else 1
batch_dim = ov_opset.gather(
shape,
ov_opset.constant(batch_axis, Type.i64),
ov_opset.constant(0, Type.i64),
).output(0)

dims_vec = ov_opset.unsqueeze(
batch_dim,
ov_opset.constant([0], Type.i64),
).output(0)

seq_lengths = ov_opset.broadcast(seq_dim, dims_vec).output(0)

x = ov_opset.reverse_sequence(
x,
seq_lengths,
batch_axis,
ax,
).output(0)
return OpenVINOKerasTensor(x)


def floor(x):
Expand Down
Loading