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/tensorflowXLA'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)
- +13 / -0
tensorflow/compiler/tests/tensor_array_ops_test.py
Python/XLA regression test suite verifying edge-case behavior
- +4 / -0
tensorflow/compiler/tf2xla/kernels/tensor_array_ops.cc
XLA C++ kernel shape-lowering and compile-time invariant checks
Changed Files (2)
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 of rank and shape to an ordered tuple of tensors:
When , the leading extent in Equation (1) does not exist, rendering the decomposition undefined. Enforcing prior to RemoveDim(0) guarantees that every slice has a valid non-negative rank .
| Input Shape | Before Fix | After Fix |
|---|---|---|
shape = [] () | Fatal XLA segfault | InvalidArgumentError |
shape = [N, ...] () | slices written to TensorArray | Identical 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
- TensorFlow XLA
tf2xlaKernel Guidelines,tensorflow/compiler/tf2xla/kernels/tensor_array_ops.cc. - Anandhu P Shaji, Hunting Compiler Bugs and Numerical Traps in TensorFlow,
anandhupshaji.me/posts/tensorflow-stories.html#xla-compiler-segfault.