tf.keras.ops.numpy.rot90 returns incorrect values for two rotations of a rectangular plane · Issue #127248 · tensorflow/tensorflow · GitHub
Skip to content

tf.keras.ops.numpy.rot90 returns incorrect values for two rotations of a rectangular plane #127248

Description

@ALinrunrun

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

tf 2.21.0, also reproduced on tf 2.22.0-dev20260904

Custom code

Yes

OS platform and distribution

Linux Ubuntu 22.04

Mobile device

No response

Python version

Python 3.13.5

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

tf.keras.ops.numpy.rot90 returns incorrect forward values for k=2 when rotating a rectangular plane.

A rotation with k=2 should be exactly equivalent to reversing along both selected axes. In the reproducer below, the input has shape (2, 2, 3) and rot90 is applied with axes=(1, 2), so the selected plane has unequal dimensions. The expected result is computed from the corresponding index permutation.

For the weighted selected output, the expected value is -577.0, but TensorFlow returns -232.0.

Expected behavior?

tf.keras.ops.numpy.rot90(x, k=2, axes=(1, 2)) should reverse the selected rectangular plane along both selected axes. For the reproducer below, the weighted selected output should be -577.0, not -232.0.

Standalone code to reproduce the issue

import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["TF_NUM_INTRAOP_THREADS"] = "1"
os.environ["TF_NUM_INTEROP_THREADS"] = "1"

import tensorflow as tf

def target(t):
    x = tf.reshape(
        tf.stack([(i + 1) * t + i for i in range(12)]),
        (2, 2, 3),
    )
    y = tf.keras.ops.numpy.rot90(
        x,
        k=2,
        axes=(1, 2),
    )
    return y[0, 0, 0] - 2 * y[1, 1, 2] + 3 * y[1, 0, 1]

x = tf.constant(-24.0, dtype=tf.float64)

actual = target(x)
expected = -577.0

print("actual:", actual.numpy())
print("expected:", expected)

if abs(float(actual) - expected) > 1e-9:
    print("BUG REPRODUCED: tf.keras.ops.numpy.rot90 returns incorrect values for two rotations of a rectangular plane")
else:
    print("not reproduced")

Relevant log output

actual: -232.0
expected: -577.0
BUG REPRODUCED: tf.keras.ops.numpy.rot90 returns incorrect values for two rotations of a rectangular plane

Activity

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions