Skip to content

GH-37484: [Python] Add a FixedSizeTensorScalar class - #37533

Merged
pitrou merged 45 commits into
apache:mainfrom
rok:37484
Feb 8, 2024
Merged

GH-37484: [Python] Add a FixedSizeTensorScalar class#37533
pitrou merged 45 commits into
apache:mainfrom
rok:37484

Conversation

@rok

@rokrok commented Sep 4, 2023

Copy link
Copy Markdown
Member

Rationale for this change

When working with FixedSizeTensorArray we want to access individual tensors. This would be enabled by adding:

defFixedSizeTensorScalar(pa.ExtensionScalar):
defto_numpy_ndarray(): ...

See #37484.

What changes are included in this PR?

This adds FixedSizeTensorScalar and tests for it.

Are there any user-facing changes?

Yes, when calling FixedSizeTensorArray[i] we would get back FixedSizeTensorScalar instead of ExtensionScalar.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #37484has been automatically assigned in GitHub to PR creator.

@alippai

Copy link
Copy Markdown
Contributor

Would the numpy array api or https://data-apis.org/array-api/latest/purpose_and_scope.html add any value here?

@rok

rok commented Sep 5, 2023

Copy link
Copy Markdown
MemberAuthor

@alippai This PR would effectively implement a __pos__(i) method. Which should per array-api return an array while pyarrow typically returns scalar. I'm not sure how to reconcile this. We should probably have a broader discussion on the mailing list about adopting array API.

@AlenkaFAlenkaF left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! Added two suggestions, otherwise the Pyhton part LGTM.

Comment threadpython/pyarrow/array.pxi Outdated
Comment threadpython/pyarrow/array.pxi Outdated
Comment threadpython/pyarrow/types.pxi Outdated
@AlenkaF

Copy link
Copy Markdown
Member

Would the numpy array api or https://data-apis.org/array-api/latest/purpose_and_scope.html add any value here?

I am very much hoping we could implement DLPack in Arrow: #33984. Specially for the new tensor arrays, it would be very beneficial!

@alippai This PR would effectively implement a __pos__(i) method. Which should per array-api return an array while pyarrow typically returns scalar. I'm not sure how to reconcile this. We should probably have a broader discussion on the mailing list about adopting array API.

+1

@AlenkaF

Copy link
Copy Markdown
Member

One more thing, can the change in the C++ code (GetTensor feature) also be reflected in the title of the PR and the description? I am not sure how we want to rename it, so I haven't made any change.

@github-actionsgithub-actionsBot added awaiting changes Awaiting changes awaiting change review Awaiting change review and removed awaiting committer review Awaiting committer review awaiting changes Awaiting changes labels Sep 24, 2023
@AlenkaFAlenkaF added this to the 15.0.0 milestone Nov 16, 2023

@jorisvandenbosschejorisvandenbossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't yet look in detail, but added some quick drive-by comments. And thanks for working on this!

Can you also add some tests for the new Scalar class?

Currently, for the Python bindings, you added a get_tensor(i) method on the array class, but wouldn't make sense to (also/instead) add a to_tensor() method on the scalar class, since this is to get a Tensor for a single element (scalar) of the array?

Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc Outdated
Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc Outdated
Comment threadpython/pyarrow/array.pxi Outdated
@github-actionsgithub-actionsBot added awaiting changes Awaiting changes and removed awaiting change review Awaiting change review labels Dec 1, 2023
@github-actionsgithub-actionsBot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Dec 2, 2023
@github-actionsgithub-actionsBot added awaiting changes Awaiting changes awaiting change review Awaiting change review and removed awaiting change review Awaiting change review awaiting changes Awaiting changes labels Dec 2, 2023

@rokrok left a comment

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the review @pitrou ! Unfortunately I couldn't work on it immediately.
I think I addressed all of your points.

Comment threadpython/pyarrow/array.pxi Outdated

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed this here: #33948 (review)
I would personally prefer to_numpy, but perhaps we can have a discussion about this under a separate issue?
cc @jorisvandenbossche@AlenkaF

Comment threadpython/pyarrow/scalar.pxi Outdated
Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc Outdated
Comment threadcpp/src/arrow/extension/fixed_shape_tensor.h Outdated
Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc Outdated

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked_pointer_cast causes a segfault here. Not sure if it's an issue or not.

Comment threadpython/pyarrow/array.pxi Outdated

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per #37533 (review)ravel shouldn't cause copy if memory layout doesn't change. And we're not actively trying to change the memory layout here.

Comment threadpython/pyarrow/array.pxi Outdated
Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc Outdated
Comment threadpython/pyarrow/tests/test_extension_type.py Outdated

@pitroupitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the update. This is really getting good, just a couple comments on specific points.

Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc Outdated
Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc Outdated
Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc Outdated
Comment threadcpp/src/arrow/extension/fixed_shape_tensor.cc
Comment threadcpp/src/arrow/extension/tensor_internal.h
Comment threadpython/pyarrow/array.pxi Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I wouldn't be so sure, example:

>>>obj=np.arange(24).reshape(2,3,4)[:,::2]
>>>objarray([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[20, 21, 22, 23]]])
>>>obj.strides
(96, 64, 8)
>>>permutation= (-np.array(obj.strides)).argsort(kind='stable')
>>>permutationarray([0, 1, 2]) # permutation is ok>>>values=np.ravel(obj, order="K")
>>>valuesarray([ 0, 1, 2, 3, 8, 9, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23])
>>>values[0] =999>>>valuesarray([999, 1, 2, 3, 8, 9, 10, 11, 12, 13, 14, 15, 20,
21, 22, 23])
>>>objarray([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[20, 21, 22, 23]]])
# values is a copy! which we can also check using:>>>values.ctypes.data==obj.ctypes.dataFalse# base addresses are different

Of course, we may not really care about this, since the conversion is probably correct anyway. But we may want to add a mention in the docstring that the conversion is only zero-copy if the input array is contiguous.

Comment threadpython/pyarrow/tests/test_extension_type.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's an issue either, but it would be worth taking a debugger and understanding exactly what happens :-)

@rok

rok commented Feb 8, 2024

Copy link
Copy Markdown
MemberAuthor

Thanks a lot for the update. This is really getting good, just a couple comments on specific points.

Thanks for the helpful review @pitrou, I'm happy to see this moving forward! I've addressed your points, please let me know if more changes are needed.

Comment threadpython/pyarrow/array.pxi
Comment threadpython/pyarrow/array.pxi Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as_strided can be a later PR if desired. The docstring addition is good for now!

@pitrou

Copy link
Copy Markdown
Member

@github-actions crossbow submit -g python -g wheel

@pitrou

Copy link
Copy Markdown
Member

It may be nice to later add a doc section for tensors here:
https://arrow.apache.org/docs/python/

@github-actions

Copy link
Copy Markdown

Revision: bf2ca0e

Submitted crossbow builds: ursacomputing/crossbow @ actions-2a16c8cab9

TaskStatus
test-conda-python-3.10GitHub Actions
test-conda-python-3.10-cython2GitHub Actions
test-conda-python-3.10-hdfs-2.9.2GitHub Actions
test-conda-python-3.10-hdfs-3.2.1GitHub Actions
test-conda-python-3.10-pandas-latestGitHub Actions
test-conda-python-3.10-pandas-nightlyGitHub Actions
test-conda-python-3.10-spark-v3.5.0GitHub Actions
test-conda-python-3.10-substraitGitHub Actions
test-conda-python-3.11GitHub Actions
test-conda-python-3.11-dask-latestGitHub Actions
test-conda-python-3.11-dask-upstream_develGitHub Actions
test-conda-python-3.11-hypothesisGitHub Actions
test-conda-python-3.11-pandas-upstream_develGitHub Actions
test-conda-python-3.11-spark-masterGitHub Actions
test-conda-python-3.12GitHub Actions
test-conda-python-3.8GitHub Actions
test-conda-python-3.8-pandas-1.0GitHub Actions
test-conda-python-3.8-spark-v3.5.0GitHub Actions
test-conda-python-3.9GitHub Actions
test-conda-python-3.9-pandas-latestGitHub Actions
test-cuda-pythonGitHub Actions
test-debian-11-python-3Azure
test-fedora-38-python-3Azure
test-ubuntu-20.04-python-3Azure
test-ubuntu-22.04-python-3GitHub Actions
wheel-macos-big-sur-cp310-arm64GitHub Actions
wheel-macos-big-sur-cp311-arm64GitHub Actions
wheel-macos-big-sur-cp312-arm64GitHub Actions
wheel-macos-big-sur-cp38-arm64GitHub Actions
wheel-macos-big-sur-cp39-arm64GitHub Actions
wheel-macos-catalina-cp310-amd64GitHub Actions
wheel-macos-catalina-cp311-amd64GitHub Actions
wheel-macos-catalina-cp312-amd64GitHub Actions
wheel-macos-catalina-cp38-amd64GitHub Actions
wheel-macos-catalina-cp39-amd64GitHub Actions
wheel-manylinux-2-28-cp310-amd64GitHub Actions
wheel-manylinux-2-28-cp310-arm64GitHub Actions
wheel-manylinux-2-28-cp311-amd64GitHub Actions
wheel-manylinux-2-28-cp311-arm64GitHub Actions
wheel-manylinux-2-28-cp312-amd64GitHub Actions
wheel-manylinux-2-28-cp312-arm64GitHub Actions
wheel-manylinux-2-28-cp38-amd64GitHub Actions
wheel-manylinux-2-28-cp38-arm64GitHub Actions
wheel-manylinux-2-28-cp39-amd64GitHub Actions
wheel-manylinux-2-28-cp39-arm64GitHub Actions
wheel-manylinux-2014-cp310-amd64GitHub Actions
wheel-manylinux-2014-cp310-arm64GitHub Actions
wheel-manylinux-2014-cp311-amd64GitHub Actions
wheel-manylinux-2014-cp311-arm64GitHub Actions
wheel-manylinux-2014-cp312-amd64GitHub Actions
wheel-manylinux-2014-cp312-arm64GitHub Actions
wheel-manylinux-2014-cp38-amd64GitHub Actions
wheel-manylinux-2014-cp38-arm64GitHub Actions
wheel-manylinux-2014-cp39-amd64GitHub Actions
wheel-manylinux-2014-cp39-arm64GitHub Actions
wheel-windows-cp310-amd64GitHub Actions
wheel-windows-cp311-amd64GitHub Actions
wheel-windows-cp312-amd64GitHub Actions
wheel-windows-cp38-amd64GitHub Actions
wheel-windows-cp39-amd64GitHub Actions

@rok

rok commented Feb 8, 2024

Copy link
Copy Markdown
MemberAuthor

Added an issue for the docs: #39998
Thanks for your reviews @pitrou@jorisvandenbossche@AlenkaF@alippai !

and the rest of the dimensions will match the permuted shape of the fixed
shape tensor.

The conversion is zero-copy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: this is only if the conversion to numpy is zero-copy (i.e. primitive numeric data without nulls)

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, added to VariableShapeTensor PR 8ca3bf7

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 026188e.

There were 9 benchmark results indicating a performance regression:

The full Conbench report has more details. It also includes information about 7 possible false positives for unstable benchmarks that are known to sometimes produce them.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] Add a FixedSizeTensorScalar class

6 participants

@rok@alippai@AlenkaF@pitrou@jorisvandenbossche@raulcd