PR #124929

tensorflow/tensorflow · XLA · Merged · 2 files changed

XLA's TensorArray.unstack kernel lacked a rank >= 1 precondition prior to RemoveDim(0), causing rank-0 scalar inputs to crash XLA shape lowering with a segmentation fault. This fix enforces rank validation at kernel entry and adds an XLA unit test.

Checkout Pull Request

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

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

XLA's TensorArray.unstack kernel lacked a rank >= 1 precondition prior to RemoveDim(0), causing rank-0 scalar inputs to crash XLA shape lowering with a segmentation fault. This fix enforces rank validation at kernel entry and adds an XLA unit test.

File-by-File Modification Breakdown (2)

  • tensorflow/compiler/tests/tensor_array_ops_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +13 / -0
  • tensorflow/compiler/tf2xla/kernels/tensor_array_ops.cc

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

    +4 / -0

Changed Files (2)

tensorflow/compiler/tests/tensor_array_ops_test.py19 lines
@@ -1165,5 +1165,18 @@ def fn():
       self.assertEqual(size0_v, 2)
       self.assertEqual(size1_v, 4)
 
+  def testTensorArrayUnstackScalarRaisesError(self):
+    with self.session(), self.test_scope():
+      ta = tensor_array_ops.TensorArray(dtype=dtypes.float32, size=3)
+      scalar = constant_op.constant(1.0)
+
+      def fn():
+        return ta.unstack(scalar)
+
+      with self.assertRaisesRegex(
+          (errors.InvalidArgumentError, ValueError),
+          "scatter/unstack requires value to have rank >= 1, got scalar"):
+        self.evaluate(xla.compile(fn))
+
 if __name__ == "__main__":
   test.main()

1. Introduction & Motivation

TensorArray.unstack splits an input tensor along axis 0 and writes each resulting slice into sequential slots of a TensorArray. Because this operation is frequently used inside dynamic control-flow loops and sequence models compiled with xla.compile, malformed or rank-0 inputs from upstream shape inference must fail predictably with a descriptive error rather than crashing the host C++ runtime [1].

2. Mathematical & Shape Formulation

Formally, unstack maps a tensor VV of rank k=dims(V)k = \text{dims}(V) and shape (d0,d1,…,dk−1)(d_0, d_1, \dots, d_{k-1}) to an ordered tuple of d0d_0 tensors:

unstack(V)=(V[0,… ],  V[1,… ],  …,  V[d0−1,… ]),requires k=dims(V)≥1(1)\text{unstack}(V) = \big( V[0, \dots],\; V[1, \dots],\; \dots,\; V[d_0 - 1, \dots] \big), \qquad \text{requires } k = \text{dims}(V) \ge 1 \tag{1}

When k=0k = 0, the leading extent d0d_0 in Equation (1) does not exist, rendering the decomposition undefined. Enforcing k≥1k \ge 1 prior to RemoveDim(0) guarantees that every slice has a valid non-negative rank k−1≥0k - 1 \ge 0.

Input ShapeBefore FixAfter Fix
shape = [] (k=0k=0)Fatal XLA segfaultInvalidArgumentError
shape = [N, ...] (k≥1k \ge 1)NN slices written to TensorArrayIdentical HLO output

3. Implementation & Verification

In tensorflow/compiler/tf2xla/kernels/tensor_array_ops.cc, I added the OP_REQUIRES(ctx, value_shape.dims() >= 1, ...) guard before dimension stripping. In tensorflow/compiler/tests/tensor_array_ops_test.py, I added an XLA compilation regression test confirming that scalar inputs raise InvalidArgumentError.

4. References

  1. TensorFlow XLA tf2xla Kernel Guidelines, tensorflow/compiler/tf2xla/kernels/tensor_array_ops.cc.
  2. Anandhu P Shaji, Hunting Compiler Bugs and Numerical Traps in TensorFlow, anandhupshaji.me/posts/tensorflow-stories.html#xla-compiler-segfault.
View PR #124929 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.