PR #124588

tensorflow/tensorflow · XLA · Merged · 2 files changed

tf.experimental.numpy.cross failed XLA compilation on statically shaped 3D vectors because axis normalization emitted dynamic reshapes incompatible with XLA static bounds. Preserving static trailing dimensions resolved compilation failures.

Checkout Pull Request

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

$gh pr checkout 124588 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #124588)XLA / Merged
INPUT / TRIGGERXLA Edge CaseROOT CAUSEMissing InvariantPATCHED (2 FILES)np_math_ops.pynp_math_ops_test.pyOUTCOMEMerged

tf.experimental.numpy.cross failed XLA compilation on statically shaped 3D vectors because axis normalization emitted dynamic reshapes incompatible with XLA static bounds. Preserving static trailing dimensions resolved compilation failures.

File-by-File Modification Breakdown (2)

  • tensorflow/python/ops/numpy_ops/np_math_ops.py

    Upstream Python/C++ module implementation

    +13 / -2
  • tensorflow/python/ops/numpy_ops/np_math_ops_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +49 / -0

Changed Files (2)

tensorflow/python/ops/numpy_ops/np_math_ops.py22 lines
@@ -326,8 +326,19 @@ def move_axis_to_last(a, axis):
 
     a = maybe_move_axis_to_last(a, axis_a)
     b = maybe_move_axis_to_last(b, axis_b)
-    a_dim = np_utils.getitem(array_ops.shape(a), -1)
-    b_dim = np_utils.getitem(array_ops.shape(b), -1)
+
+    def size_of_last_dim(a):
+      # Prefer the statically-known dimension so that the padding and output
+      # selection below resolve at trace time; a dynamic size turns them into
+      # tf.cond branches whose output shapes XLA cannot infer.
+      if hasattr(a, 'shape') and a.shape.rank:
+        static_size = a.shape.as_list()[-1]
+        if static_size is not None:
+          return static_size
+      return np_utils.getitem(array_ops.shape(a), -1)
+
+    a_dim = size_of_last_dim(a)
+    b_dim = size_of_last_dim(b)
 
     def maybe_pad_0(a, size_of_last_dim):
       def pad_0(a):

1. Introduction & Motivation

NumPy-compatible APIs in tf.experimental.numpy are expected to lower seamlessly under both eager execution and xla.compile. In cross, however, internal axis-transposition helpers discarded known static dimension sizes along the contracted trailing axis, introducing dynamic shape nodes that XLA rejected during HLO lowering [1].

2. Vector Algebra & Static Shape Constraint

For vectors u,v∈R3u, v \in \mathbb{R}^3, the cross product w=u×vw = u \times v contracts along an axis of exact length 33 via the Levi-Civita tensor εijk\varepsilon_{ijk}:

wk=∑i=13∑j=13εijk ui vj,daxis=3(1)w_k = \sum_{i=1}^{3} \sum_{j=1}^{3} \varepsilon_{ijk}\, u_i\, v_j, \qquad d_{\text{axis}} = 3 \tag{1}
Lowering ModeTrailing Dimension MetadataXLA Result
BeforeErased to dynamic ?Compile failure
AfterPreserved static 3Compiles to fixed HLO

3. Implementation & Verification

# In tensorflow/python/ops/numpy_ops/np_math_ops.py:
static_dim = tensor_shape.dimension_value(a.shape[axis])

Verified in XLA tests that tf.experimental.numpy.cross compiles and matches NumPy outputs for statically shaped trailing dimensions.

4. References

  1. NumPy Developers, numpy.cross Specification & Axis Semantics, NumPy Reference Manual.
View PR #124588 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.