Repository navigation
[Data] Handle empty NumPy tensors in Arrow conversion - #66508
Conversation
Signed-off-by: Wenyi Xu <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request adds support for converting empty multidimensional NumPy tensors to PyArrow arrays, ensuring they retain their shape and data type. It also includes unit tests to verify this behavior and the handling of empty scalar columns. A review comment suggests using duck-typing (hasattr(column_values, "ndim")) instead of isinstance(column_values, np.ndarray) to support empty multidimensional tensors from other array-like libraries such as PyTorch, TensorFlow, or JAX.
Signed-off-by: Wenyi Xu <[email protected]>
| if len(column_values) > 0 and _should_convert_to_tensor( | ||
| column_values, column_name | ||
| # Empty multidimensional arrays still carry tensor shape and dtype. | ||
| if (hasattr(column_values, "ndim") and column_values.ndim > 1) or ( |
There was a problem hiding this comment.
Does this case also need a check for len(column_values) == 0? If so, I would add it so that we can lean towards native arrays
There was a problem hiding this comment.
Good point, added len(column_values) == 0 to keep the new branch limited to empty inputs and let non-empty ones go through the existing check.
Signed-off-by: Wenyi Xu <[email protected]>
|
hi @xuwenyihust you have some tests failing; can you take a look? |
Signed-off-by: Wenyi Xu <[email protected]>
Hi @richardliaw, looks like a dependency download issue: URLs for xz-5.2.7.tar.gz returned HTTP 522 before the tests started. I've re-triggered CI to check if it happens again. |
) ## Description I noticed that returning an empty NumPy tensor batch from `map_batches` fails when `enable_fallback_to_arrow_object_ext_type=False`. It looks like the `len(column_values) > 0` check skips tensor conversion for these arrays, even though they still have a shape and dtype. Ray skips Arrow tensor conversion for the empty array and tries Arrow, then pandas. Both failed. I expected the empty array to use the tensor conversion path, instead of fallback. ## Validation Reproduced on Ray 2.58.0. Tested the fix with local master ``` DataContext.get_current().enable_fallback_to_arrow_object_ext_type = False ds = ray.data.from_numpy(np.ones((2, 2, 2), dtype=np.int32)) ds.map_batches( lambda batch: {key: value[:0] for key, value in batch.items()}, batch_format="numpy", ).materialize() ``` ## Additional information - No overlapping open PR was found. - I used AI assistance for the investigation, implementation, and testing. - I reviewed all changes and ran the relevant tests locally. --------- Signed-off-by: Wenyi Xu <[email protected]>
…conciliation and struct alignment (#66521) (#67995) ## Description Cherry-pick of #66521 (master commit 6fa1a28) onto `releases/2.60.1`. This is a prerequisite for the #67579 pick, #67999. The two sit in a 3-PR stack on `releases/2.60.1` under #68019 (the #67806 pick); merge it with `gh stack merge 68019`. #66521 fixes null-type handling when Ray Data reconciles Arrow schemas across blocks: - A nested field that is all-null in one block and a struct in another crashes `_backfill_missing_fields` with `TypeError: 'pyarrow.lib.DataType' object is not iterable`. - `_reconcile_field` reconciles `[null, list<null>, list<int64>]` to `null` instead of `list<int64>`. Without it, two of the tests #67579 adds fail on this branch. Both are the case where one block's tensor column is fixed-shape, another's is null, and a third's has a different shape. They fail with `ArrowTypeError: Casting from 'extension<ray.data.arrow_variable_shaped_tensor<...>>' to different extension type 'extension<ray.data.arrow_tensor_v2<...>>' not permitted`. ## Related issues Related to #66521. Needed by the #67579 pick. ## Additional information ### Cherry-pick details Applied cleanly with `git cherry-pick -x`. It has the same 2 files, +233/-30, and the same `git patch-id --stable` as the master commit. After the pick, `unit/test_transform_pyarrow.py` is identical to master at 6fa1a28. `transform_pyarrow.py` differs only by #66197's rename of `PreparedChunkedTensorTake` to `PreparedTensorTake`. #66197 is the only master commit that touched these files between the branch cut and #66521, and this change doesn't depend on it. To find what #67579 needed, I applied each master-only commit that touched `transform_pyarrow.py` or the tensor extensions on top of the #67579 pick: #66521, #65961, #66508, #66197, #66703 and #66522. #66521 alone makes the two failing tests pass, and none of the others changes the result. Not a duplicate: this branch doesn't contain #66521, and no PR on any branch references it. ### Testing | Command | Result | |---|---| | `pre-commit run --files <the 2 changed files>` | Passed | | `pytest python/ray/data/tests/unit/test_transform_pyarrow.py python/ray/data/tests/test_transform_pyarrow.py python/ray/data/tests/test_arrow_block.py` | 145 passed, 3 skipped | The #67579 pick's description has the results with both picks applied. I ran these against the Ray 2.60.0 release wheel, with this branch's `python/ray/data` and `python/ray/tests` copied over it. The wheel was built from `releases/2.60.0` at b7f4259, and its Python sources match `releases/2.60.1` except for `_version.py`. The environment was Python 3.12 with pandas 2.3.3 and pyarrow 23.0.1, the versions CI pins. Building Ray from source for `bazel test` wasn't practical on my machine. AI assistance was used to prepare this cherry-pick. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: dragongu <[email protected]> Signed-off-by: Sai Miduthuri <[email protected]> Co-authored-by: dragongu <[email protected]>
Description
I noticed that returning an empty NumPy tensor batch from
map_batchesfails whenenable_fallback_to_arrow_object_ext_type=False.It looks like the
len(column_values) > 0check skips tensor conversion for these arrays, even though they still have a shape and dtype.Ray skips Arrow tensor conversion for the empty array and tries Arrow, then pandas. Both failed.
I expected the empty array to use the tensor conversion path, instead of fallback.
Validation
Reproduced on Ray 2.58.0. Tested the fix with local master
Additional information