Repository navigation
[Data] Fix unify_tensor_arrays - #60389
srinathk10 wants to merge 6 commits into
Conversation
Signed-off-by: Srinath Krishnamachari <[email protected]>
|
/gemini summary |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request provides a critical fix for unify_tensor_arrays to correctly handle concatenation of tensor arrays with different types or shapes. The main issue was that PyArrow's chunked_array validation requires type identity, which was not guaranteed by the previous implementation.
The changes correctly address this by:
- Tracking distinct tensor types by their object identity (
id()) instead of relying on__eq__. - Ensuring all arrays are re-wrapped with the same unified type instance after unification.
- Adding a fix to
_pad_shape_with_singleton_axesto handle list-like shape inputs correctly.
New tests have been added that specifically verify that all chunks in a concatenated tensor array share the same type instance, confirming the fix. The changes are well-implemented and the tests are thorough. This is a solid improvement.
Summary of ChangesThis pull request resolves a critical issue in Ray Data's tensor array unification logic, specifically when dealing with PyArrow extension types. The changes ensure that when multiple tensor arrays are combined, the resulting unified arrays consistently share the same underlying PyArrow type instance. This prevents validation errors that could occur during operations like concatenation, especially when handling tensors with varying shapes or dimensions, thereby improving the robustness and reliability of tensor processing within Ray Data. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
|
There was a problem hiding this comment.
Code Review
This pull request addresses a bug in unify_tensor_arrays concerning the unification of different tensor array types, which previously caused errors in PyArrow. The solution involves correctly identifying distinct type instances by their object ID and ensuring that all unified arrays share a single type instance, a requirement for PyArrow's chunked array validation. The changes are well-reasoned and clearly commented. Furthermore, the new tests effectively target the resolved bug, which is a significant enhancement. My review offers suggestions for improving type safety and making the new tests more robust.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: Srinath Krishnamachari <[email protected]>
Signed-off-by: Srinath Krishnamachari <[email protected]>
Signed-off-by: Srinath Krishnamachari <[email protected]>
Signed-off-by: Srinath Krishnamachari <[email protected]>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses a critical issue in unify_tensor_arrays where PyArrow's C++ validation failed due to type identity mismatches and incorrect deduplication of tensor types with varying dimensions. The changes correctly handle type identity for PyArrow's strict validation, perform logical deduplication, and ensure all arrays are re-wrapped with a single, shared type instance after unification. The logic also correctly pads shapes for variable-shaped tensors with different numbers of dimensions. The addition of comprehensive tests significantly improves confidence in the correctness of the fix.
| ) | ||
| converted = arr.to_var_shaped_tensor_array(ndim=unified_tensor_type.ndim) | ||
| # Re-wrap storage with the shared unified type instance | ||
| unified_arrs.append(unified_tensor_type.wrap_array(converted.storage)) |
There was a problem hiding this comment.
Which test would fail w/o this?
There was a problem hiding this comment.
test_different_ndims_unify_with_padding will fail without this
Signed-off-by: Srinath Krishnamachari <[email protected]>
| unified_arrs.append(arr) | ||
| else: | ||
| unified_arrs.append(unified_tensor_type.wrap_array(arr.storage)) | ||
| return unified_arrs |
There was a problem hiding this comment.
Mixed v1 and v2 tensor types cause failure
Medium Severity
When unifying arrays with mixed ArrowTensorType (v1) and ArrowTensorTypeV2 (v2) that have identical shapes, the code attempts to wrap one storage format with the other's type at line 1494. Since v1 uses pa.list_ storage while v2 uses pa.large_list storage, calling wrap_array with incompatible storage types fails. The code treats them as logically distinct types but assumes wrap_array can handle the storage, which it cannot when the storage formats differ.
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
|
This pull request has been automatically closed because there has been no more activity in the 14 days Please feel free to reopen or open a new pull request if you'd still like this to be addressed. Again, you can always ask for help on our discussion forum or Ray's public slack channel. Thanks again for your contribution! |
…able-shaped one (#67266) ## Description `ArrowVariableShapedTensorArray.to_var_shaped_tensor_array()` fails whenever the target `ndim` is larger than the array's own, so a tensor column that is fixed-shape in one block and variable-shaped with fewer dimensions in another can't be concatenated. Two lines were wrong, and null rows weren't handled: - `shapes_array.to_pylist()` returns lists, and `_pad_shape_with_singleton_axes()` does `(1,) * k + shape`, so it raised `TypeError: can only concatenate tuple (not "list") to tuple`. The shapes are now converted with `tuple(s)`. - `pa.StructArray.from_arrays()` was called without field names and raised `ValueError: Must pass either names or fields`. It now passes `["data", "shape"]`, the same as the other two places in the file that build this storage. - Null rows have a `None` shape, so they are skipped when padding, and the source null mask is passed to `StructArray.from_arrays()` so they stay null. - When every row is null or there are no rows, `pa.array()` infers a null type for the padded shapes and wrapping the storage fails with `Incompatible storage type`. The shape type is now passed explicitly (raised in review). Added `test_unify_fixed_shaped_with_lower_ndim_variable_shaped`, which unifies and concatenates a fixed-shape 2D array with a variable-shaped 1D one in both orders, `..._null_rows`, which does the same with a sliced array that has a null row, and `..._no_valid_rows`, which uses an all-null and an empty variable-shaped array in both orders. Existing tests only unified arrays with the same `ndim`, so this branch wasn't covered. The null-row tests read rows one at a time because `to_numpy()` on null variable-shaped rows fails on master regardless of this change (#67236 addresses that). The 1D tensors come back as `(1, n)` because of the existing singleton-axis padding. This PR doesn't change that. ## Related issues Closes #67265 ## Additional information - Not a duplicate: #60389 had the same two changes as part of a larger rework of `unify_tensor_arrays`, but it was closed as stale in February. This PR only takes those two lines, handles null rows, and adds tests. I searched open PRs for `to_var_shaped_tensor_array`, `_pad_shape_with_singleton_axes`, `unify_tensor_arrays` and tensor-related keywords; #66997 and #67236 touch other functions. - Tests, run locally on master `7da1b4c` (`python/ray/data` from source on a nightly wheel), Python 3.12.3, pyarrow 26.0.0, numpy 2.5.4: - `pytest python/ray/data/tests/test_tensor_extension.py -k unify_fixed_shaped_with_lower_ndim`: 21 passed. Without the fix, the first 9 fail with `TypeError`. With the `None` guard but no mask, the null-row test fails because the null row becomes valid. Without the explicit shape type, the 12 `no_valid_rows` cases fail with `Incompatible storage type`. - The repro in #67265 and its `repartition(1).take_all()` variants in both union orders now succeed. - `pytest python/ray/data/tests/test_tensor_extension.py python/ray/data/tests/test_transform_pyarrow.py python/ray/data/tests/test_tensor.py python/ray/data/tests/test_arrow_block.py python/ray/data/tests/test_union.py`: 356 passed, 3 skipped, 5 failed. The 5 fail the same way on unmodified master in my environment: `test_tensor_array_scalar_cast` (3 formats), `test_variable_shaped_null_rows_to_pandas_unchanged`, and `test_tensors_sort[ARROW_NATIVE]`. - `pre-commit run --files <changed files>`: passed. - AI assistance was used to prepare this PR. --------- Signed-off-by: rayo5o <[email protected]>
Description
[Data] Fix unify_tensor_arrays
Problem
When concatenating PyArrow tables containing tensor extension types with varying dimensions (e.g., mixing 2D grayscale and 3D RGB image tensors), PyArrow would fail with:
ArrowTypeError: Array chunks must all be same typeDiscovered this issue with #59560
Root Cause
Related issues
Additional information