Skip to content

GH-38868: [Python] Add dlpack producer to FixedShapeTensorArray/Scalar - #51159

Open
AntoinePrv wants to merge 5 commits into
apache:mainfrom
AntoinePrv:dl-fix-shape-tensor-array
Open

GH-38868: [Python] Add dlpack producer to FixedShapeTensorArray/Scalar#51159
AntoinePrv wants to merge 5 commits into
apache:mainfrom
AntoinePrv:dl-fix-shape-tensor-array

Conversation

@AntoinePrv

@AntoinePrv AntoinePrv commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Rationale for this change

Have the same coverage as to_numpy.

What changes are included in this PR?

  • FixedShapeTensorArray.__dlpack__ explicitly calling to_tensor
  • FixedShapeTensorArray.__dlpack_device__ (defaulted)
  • FixedShapeTensorScalar.__dlpack__ explicitly calling to_tensor
  • FixedShapeTensorScalar.__dlpack_device__ calling C++ device extraction on the underlying array.

If GH-51122 get merged first, I'll add the consuming side here, otherwise if this one gets merged first, I'll add it there.

Are these changes tested?

Yes a few since this does not introduce new logic.

Are there any user-facing changes?

Additions only.

Copilot AI lite review requested due to automatic review settings September 4, 2026 12:51
@AntoinePrv
AntoinePrv requested a review from rok as a code owner September 4, 2026 12:51
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

Thanks for opening a pull request!

This pull request has been automatically converted to a draft because its title doesn't match Arrow's required format.

If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose

Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename the pull request title in the following format?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

After updating the title, you can mark the pull request as ready for review.

See also:

@github-actions
github-actions Bot marked this pull request as draft September 4, 2026 12:52
@AntoinePrv AntoinePrv changed the title Add dlpack producer to FixedShapeTensorArray/Scalar GH-38868: [Python] Add dlpack producer to FixedShapeTensorArray/Scalar Sep 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

It introduces a backward-incompatible change to the public Array.__dlpack__ signature and contains a newly added incorrect docstring for FixedShapeTensorScalar.__dlpack__.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR extends PyArrow’s DLPack producer support to FixedShapeTensorArray and FixedShapeTensorScalar, with accompanying tests to match the existing to_numpy-level coverage, and refactors NumPy version gating in DLPack tests.

Changes:

  • Add a reusable requires_numpy_version() marker and apply it to version-dependent DLPack tests.
  • Implement __dlpack__ for FixedShapeTensorArray and both __dlpack__ / __dlpack_device__ for FixedShapeTensorScalar.
  • Add new DLPack roundtrip tests for permuted fixed-shape tensor arrays and scalars.
File summaries
File Description
python/pyarrow/tests/test_dlpack.py Adds NumPy-version gating helper and new tests for fixed-shape tensor array/scalar DLPack export behavior.
python/pyarrow/scalar.pxi Adds DLPack producer/device hooks for FixedShapeTensorScalar.
python/pyarrow/array.pxi Adds DLPack producer for FixedShapeTensorArray and changes Array.__dlpack__ signature.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread python/pyarrow/array.pxi
Comment thread python/pyarrow/scalar.pxi
@AntoinePrv
AntoinePrv marked this pull request as ready for review September 4, 2026 13:24
Copilot AI review requested due to automatic review settings September 4, 2026 13:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

FixedShapeTensorScalar.__dlpack_device__ can dereference null storage for invalid scalars, risking a crash instead of raising a Python exception.

Review details

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

python/pyarrow/scalar.pxi:1616

  • __dlpack_device__ dereferences ext.value / storage.value without checking scalar validity. For a null (invalid) FixedShapeTensorScalar this can be a null pointer and crash instead of raising a Python exception. Please guard on self.is_valid before using the storage scalar.
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@AntoinePrv

Copy link
Copy Markdown
Collaborator Author

@AlenkaF @rok @pitrou this one is ready too

Sign up for free to 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.

2 participants