PR #124586

tensorflow/tensorflow · XLA · Merged · 2 files changed

XLA's TransposeOp rejected negative permutation indices such as perm=[-1, 0] instead of wrapping them modulo tensor rank. I normalized negative indices prior to bounds validation so XLA matches eager TensorFlow behavior.

Checkout Pull Request

Inspect the full commit history and diff locally from upstream tensorflow/tensorflow.

$gh pr checkout 124586 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #124586)XLA / Merged
INPUT / TRIGGERXLA Edge CaseROOT CAUSEMissing InvariantPATCHED (2 FILES)binary_ops_test.pytranspose_op.ccOUTCOMEMerged

XLA's TransposeOp rejected negative permutation indices such as perm=[-1, 0] instead of wrapping them modulo tensor rank. I normalized negative indices prior to bounds validation so XLA matches eager TensorFlow behavior.

File-by-File Modification Breakdown (2)

  • tensorflow/compiler/tests/binary_ops_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +15 / -0
  • tensorflow/compiler/tf2xla/kernels/transpose_op.cc

    XLA C++ kernel shape-lowering and compile-time invariant checks

    +5 / -1

Changed Files (2)

tensorflow/compiler/tests/binary_ops_test.py29 lines
@@ -1527,6 +1527,16 @@ def testTranspose(self):
           np.array([[1, 2], [3, 4]], dtype=dtype),
           np.array([1, 0], dtype=np.int32),
           expected=np.array([[1, 3], [2, 4]], dtype=dtype))
+      self._testBinary(
+          array_ops.transpose,
+          np.array([[1, 2], [3, 4]], dtype=dtype),
+          np.array([-1, 0], dtype=np.int32),
+          expected=np.array([[1, 3], [2, 4]], dtype=dtype))
+      self._testBinary(
+          array_ops.transpose,
+          np.zeros(shape=[1, 0, 4], dtype=dtype),
+          np.array([-2, -1, -3], dtype=np.int32),
+          expected=np.zeros(shape=[0, 4, 1], dtype=dtype))
 
   def testConjugateTranspose(self):
     for dtype in self.complex_types:
@@ -1545,6 +1555,11 @@ def testConjugateTranspose(self):
           np.array([[1 - 1j, 2 + 2j], [3 - 3j, 4 + 4j]], dtype=dtype),
           np.array([1, 0], dtype=np.int32),
           expected=np.array([[1 + 1j, 3 + 3j], [2 - 2j, 4 - 4j]], dtype=dtype))
+      self._testBinary(
+          array_ops.conjugate_transpose,
+          np.array([[1 - 1j, 2 + 2j], [3 - 3j, 4 + 4j]], dtype=dtype),
+          np.array([-1, 0], dtype=np.int32),
+          expected=np.array([[1 + 1j, 3 + 3j], [2 - 2j, 4 - 4j]], dtype=dtype))
 
   def testCross(self):
     for dtype in self.float_types:

1. Introduction & Motivation

Standard TensorFlow and NumPy indexing conventions treat negative axis indices as offsets from the final dimension. Under XLA, TransposeOp performed raw non-negative bounds checks directly on the user-supplied perm tensor, causing valid expressions like tf.transpose(x, perm=[-1, 0]) to fail compilation.

2. Modular Axis Normalization

Given an input tensor XX of rank r=dims(X)r = \text{dims}(X) and a permutation vector p=(p0,…,pr−1)∈Zrp = (p_0, \dots, p_{r-1}) \in \mathbb{Z}^r, canonical axis normalization maps each entry via:

pi′={pi+rif −r≤pi<0piif 0≤pi<r(1)p'_i = \begin{cases} p_i + r & \text{if } -r \le p_i < 0 \\ p_i & \text{if } 0 \le p_i < r \end{cases} \tag{1}

A valid permutation must then satisfy 0≤pi′<r0 \le p'_i < r and pairwise distinctness (pi′≠pj′p'_i \neq p'_j for i≠ji \neq j).

Permutation perm (r=2r=2)Normalized p′p'Before FixAfter Fix
[-1, 0][1, 0]Rejected by XLACompiles cleanly
[-3, 0]Out of rangeRejected by XLAExplicit bounds error

3. Implementation & Verification

// In tensorflow/compiler/tf2xla/kernels/transpose_op.cc:
if (d < 0) d += rank;
OP_REQUIRES(ctx, d >= 0 && d < rank, errors::InvalidArgument("Invalid perm index"));

XLA unit tests confirm negative permutation indices now compile and produce identical results to eager execution.

4. References

  1. TensorFlow Core API Specification, tf.transpose Negative Permutation Axis Semantics.
View PR #124586 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.