Skip to content

Add regression test coverage for ops.rot90 with batched non-square planes (k > 1) #23694

Description

@Phoebus-Liu

Background

tensorflow/tensorflow#127248 reported that keras.ops.rot90 returned incorrect values for k > 1 when rotating a batched, non-square plane — e.g. an input of shape (2, 2, 3) with axes=(1, 2) and k=2 returned -232.0 where -577.0 was expected.

Root cause: the TensorFlow backend flattened the leading (batch) axes into a (-1, h, w) tensor and reshaped back afterwards, which mixes elements across the batch dimension whenever k > 1 and h != w.

This was fixed by #23435 (merged 2026-08-14), which replaced the reshape-based implementation with tf.reverse + swapaxes applied only to the last two axes. I re-ran the original reproducer from tensorflow#127248 against current master and it now returns the expected -577.0, so the bug itself is resolved.

Problem: the fix is not protected by any test

The test added in #23435, test_non_square_rotation, only covers a rank-2 array ((2, 3)). Every existing test that uses a rank-3 or rank-4 input (test_3d_operations, test_image_processing) only exercises the default k=1.

The combination rank > 2 + non-square plane + k > 1 — precisely the case that was broken — is therefore not covered by any test in the suite.

I verified this by reverting the TensorFlow backend rot90 to its pre-#23435 implementation and re-running NumPyTestRot90: every pre-existing test still passed except test_non_square_rotation, i.e. the batched regression would be silently reintroduced without anyone noticing.

Proposal

Add test_batched_non_square_rotation to keras/src/ops/numpy_test.py, parameterised over 3-D and 4-D inputs with non-square rotation planes, checking k in {0, 1, 2, 3, 4, -1, -2, -3} against numpy.rot90.

Verification performed locally (TensorFlow backend):

  • On current master: 22/22 NumPyTestRot90 tests pass.
  • With the pre-Fix rot90 for non-square arrays in TensorFlow backend #23435 backend restored: the 4 new parameterised cases fail (alongside test_non_square_rotation), confirming the new tests actually catch the regression.
  • 300 randomised cases (rank 2–5, random shapes, random axes including negative indices, random k in [-7, 7]) all match numpy.rot90 on master.

I have the PR ready to link to this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions