Repository navigation
[Data][2.56.1] Fix TensorDtype.__from_arrow__ crash on empty tensor columns (#64767) - #64794
Conversation
…64767) ## Description `Dataset.to_pandas()` (and any Arrow → pandas batch conversion) crashes on Ray 2.56 when a tensor-extension column has zero-size elements, i.e. `element_shape` contains a `0`. This happens naturally when a per-frame / per-group operator emits an empty tensor for some rows (e.g. a detection op that finds nothing in a frame). `TensorDtype.__from_arrow__` reshapes the reconstructed values with a `-1` dimension: ```python values = values.reshape((-1,) + self.element_shape) ``` numpy cannot infer a `-1` dimension from a size-0 array when another dimension is `0`, so this raises: ``` ValueError: cannot reshape array of size 0 into shape (0) ``` Minimal repro (fails on 2.56.0, worked on 2.55): ```python import ray, numpy as np ray.data.from_items([{"pts": np.zeros((0,), np.float32)} for _ in range(3)]).to_pandas() ``` This regressed in 2.56 via #61733, which added the reshape (2.55's `__from_arrow__` simply returned `TensorArray(values)` with no reshape). ## Related issues Closes #64766. ## Additional information The fix uses the explicit row count instead of `-1` (numpy no longer has to infer a dimension), and builds the empty buffer directly for the zero-row `object`-dtype case, since `np.stack` requires at least one array: ```python num_rows = len(array) if values.dtype == object: values = ( np.stack(values) if num_rows > 0 else np.empty((0,) + self.element_shape, dtype=self._dtype) ) values = values.reshape((num_rows,) + self.element_shape) ``` Added `test_tensors_in_tables_pandas_roundtrip_empty_elements`, parametrized over element shapes `(0,)` and `(0, 2)` plus the existing tensor-format (V1 / V2 / ARROW_NATIVE) and auto-cast fixtures. Verified locally that the V1 and V2 paths crash before this fix and pass after; the native `pa.fixed_shape_tensor` path did not crash (it does not route through `__from_arrow__`) and continues to work. Signed-off-by: Marwan Sarieddine <[email protected]> Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Ayush Kumar <[email protected]> (cherry picked from commit 34607f7) Signed-off-by: elliot-barn <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where converting empty tensor columns or tensors with zero-size dimensions to Pandas fails because NumPy cannot infer a -1 dimension from a size-0 array. The fix explicitly checks the row count and constructs an empty buffer when the row count is zero, or reshapes using the explicit row count. A regression test is also added. Feedback points out a potential IndexError when handling an empty ChunkedArray with zero chunks, which would fail on array.chunk(0) before reaching the new logic.
| # infer a -1 dimension from a size-0 array ("cannot reshape array of | ||
| # size 0 into shape (0)"). See | ||
| # https://github.com/ray-project/ray/issues/64766. | ||
| num_rows = len(array) |
There was a problem hiding this comment.
There is a potential IndexError when array is an empty pa.ChunkedArray with num_chunks == 0.
In the preceding block (lines 447-466), if array is a pa.ChunkedArray and num_chunks is 0, the condition array.num_chunks > 1 evaluates to False. The code then executes the else branch:
values = array.chunk(0).to_numpy(zero_copy_only=False)This will raise IndexError: Index 0 out of bounds before reaching the new check num_rows = len(array) at line 477.
To prevent this, we should handle empty chunked arrays or empty arrays early in __from_arrow__, or check array.num_chunks == 0 in the chunked array handling block.
Cherry-pick of #64767 onto the
releases/2.56.1branch.Why are these changes needed?
Fixes a 2.56 regression where converting tensor-extension columns to pandas crashed on zero-size tensor elements. Replaces numpy's
-1dimension inference with an explicit row count and builds the buffer directly for empty arrays instead of usingnp.stack.Related issue number
Cherry-pick of #64767
Checks
Cherry-pick applied cleanly (no conflicts). DCO sign-off added.
🤖 Generated with Claude Code