Skip to content

Support numpy.moveaxis operation #21189

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 @@ -36,7 +36,6 @@ NumpyDtypeTest::test_mean
NumpyDtypeTest::test_median
NumpyDtypeTest::test_meshgrid
NumpyDtypeTest::test_min
NumpyDtypeTest::test_moveaxis
NumpyDtypeTest::test_multiply
NumpyDtypeTest::test_nan
NumpyDtypeTest::test_outer_
Expand Down Expand Up @@ -95,7 +94,6 @@ NumpyOneInputOpsCorrectnessTest::test_mean
NumpyOneInputOpsCorrectnessTest::test_median
NumpyOneInputOpsCorrectnessTest::test_meshgrid
NumpyOneInputOpsCorrectnessTest::test_min
NumpyOneInputOpsCorrectnessTest::test_moveaxis
NumpyOneInputOpsCorrectnessTest::test_nan_to_num
NumpyOneInputOpsCorrectnessTest::test_pad_float16_constant_2
NumpyOneInputOpsCorrectnessTest::test_pad_float32_constant_2
Expand Down
34 changes: 31 additions & 3 deletions keras/src/backend/openvino/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,9 +1024,37 @@ def mod(x1, x2):


def moveaxis(x, source, destination):
raise NotImplementedError(
"`moveaxis` is not supported with openvino backend"
)
if isinstance(source, int):
source = [source]
if isinstance(destination, int):
destination = [destination]

x = get_ov_output(x)
x_shape = x.get_partial_shape()
rank = x_shape.rank.get_length()

source = [s + rank if s < 0 else s for s in source]
destination = [d + rank if d < 0 else d for d in destination]

axes = list(range(rank))

for axis in sorted(source, reverse=True):
axes.pop(axis)

for dest, src in sorted(zip(destination, source)):
axes.insert(dest, src)
Comment on lines +1041 to +1045
Copy link
Contributor

@rkazants rkazants Apr 21, 2025

Choose a reason for hiding this comment

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

I think it is more clear to code this as:

for dest, src in sorted(zip(destination, source)):
    axes[dest] = src

Copy link
Author

Choose a reason for hiding this comment

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

i dont think we can do this because this results in broken permuation
as this assumes axes already has the correct length

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you share a concrete example that shows it will not work?

Copy link
Author

Choose a reason for hiding this comment

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

image

This is happening because the following code assumes that the index is of equal length
but after popping the elements the length is not the same hence it results in index error if dest >= len(axes)

def moveaxis(x, source, destination):

    if isinstance(source, int):
        source = [source]
    if isinstance(destination, int):
        destination = [destination]

    x = get_ov_output(x)
    x_shape = x.get_partial_shape()
    rank = x_shape.rank.get_length()

    source = [s + rank if s < 0 else s for s in source]
    destination = [d + rank if d < 0 else d for d in destination]

    axes = list(range(rank))

    for axis in sorted(source, reverse=True):
        axes.pop(axis)

    for dest, src in sorted(zip(destination, source)):
        axes[dest] = src

    perm = ov_opset.constant(axes, dtype=Type.i32).output(0)

    x_type = x.get_element_type()
    if x_type == Type.bf16:
        x = ov_opset.convert(x, Type.f32).output(0)
        result = ov_opset.transpose(x, perm).output(0)
        return OpenVINOKerasTensor(
            ov_opset.convert(result, Type.bf16).output(0)
        )

    return OpenVINOKerasTensor(ov_opset.transpose(x, perm).output(0))

Copy link
Contributor

Choose a reason for hiding this comment

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

@Prabha14039, once again. Share with me a concrete example with concrete values source and destination for which my code willn't work.

Copy link
Author

Choose a reason for hiding this comment

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

i apologise here is the test case failing
image

Copy link
Contributor

Choose a reason for hiding this comment

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

you need to remove as well:
{BB02C2C5-31A5-4E32-8BED-46BC31665DA8}

Copy link
Author

Choose a reason for hiding this comment

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

image

i am facing the following issue after removing the axes.pop

Copy link
Contributor

Choose a reason for hiding this comment

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

Here I don't see source and destination values. Please create standlalone code that proves my code does not work.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Prabha14039, any update on my latest comment? Thanks!


perm = ov_opset.constant(axes, dtype=Type.i32).output(0)

x_type = x.get_element_type()
if x_type == Type.bf16:
x = ov_opset.convert(x, Type.f32).output(0)
result = ov_opset.transpose(x, perm).output(0)
return OpenVINOKerasTensor(
ov_opset.convert(result, Type.bf16).output(0)
)

return OpenVINOKerasTensor(ov_opset.transpose(x, perm).output(0))


def nan_to_num(x, nan=0.0, posinf=None, neginf=None):
Expand Down