Skip to content

Use object-based DLPack interchange and test numeric dtypes in CI - #33909

Open
Justin Chu (justinchuby) wants to merge 14 commits into
mainfrom
justinchuby-urban-disco
Open

Justin Chu (justinchuby) wants to merge 14 commits into
mainfrom
justinchuby-urban-disco

Conversation

@justinchuby

@justinchuby Justin Chu (justinchuby) commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

Modernize public/native OrtValue DLPack interchange, remove obsolete export helpers, and retain legacy capsule imports.

Fix NumPy ownership, CUDA/ROCm handoffs, and exception-safe batched exports. Add mandatory inference-CI coverage for dtype compatibility and lifetime/error paths.

Motivation and Context

Follow-up to #33848. Make tensor-object interchange safe for framework use, including PyTorch's packed-type limits.

Cover all byte-aligned ORT dtypes in both directions, document and test packed dtype incompatibilities, and retain zero-copy ownership without a redundant OrtValue copy.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 03:43

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.

🟢 Approval recommended

No active review issues are recorded for the inference-only PyTorch DLPack interoperability changes.

No open issues 🎉

What changed in this PR

Adds inference-only PyTorch DLPack interoperability coverage following #33848, validating the existing dtype support without requiring ORT training.

Changes:

  • Tests bidirectional exchange for 18 byte-aligned dtypes, all float8 bit patterns, and compatibility boundaries for five packed dtypes, including zero-copy sharing and ownership.
  • Runs the suite in transformers-enabled Python test jobs when contrib operators are enabled.
  • Removes a redundant OrtValue copy, simplifies shape access, and clarifies ownership and PyTorch compatibility documentation.
File Description
tools/​ci_build/​build.py Adds mandatory DLPack suite execution to eligible test jobs.
onnxruntime/​test/​python/​onnxruntime_test_python_dlpack.py Adds dtype, storage, lifetime, and layout interoperability tests.
onnxruntime/​python/​onnxruntime_pybind_state_common.h Declares the export helper with a reference parameter and clarifies ownership.
onnxruntime/​python/​onnxruntime_pybind_state_common.cc Removes the export helper’s redundant OrtValue copy.
onnxruntime/​python/​onnxruntime_pybind_ortvalue.cc Clarifies capsule ownership in binding documentation.
onnxruntime/​python/​onnxruntime_inference_collection.py Documents buffer lifetime and PyTorch dtype compatibility.
onnxruntime/​core/​dlpack/​dlpack_converter.h Corrects the converter’s ownership comment.
onnxruntime/​core/​dlpack/​dlpack_converter.cc Reuses the dimension span for shape access and stride reservation.

Review effort: Balanced

Remove the legacy Python to_dlpack method and accept protocol objects consistently in native and public from_dlpack. Migrate callers, preserve capsule compatibility and training autograd semantics, and cover native protocol surfaces and producer errors.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@justinchuby Justin Chu (justinchuby) changed the title Test PyTorch DLPack dtype interchange in inference CI Use object-based DLPack interchange and test numeric dtypes in CI Nov 5, 2026
@justinchuby
Justin Chu (justinchuby) requested a balanced review from Copilot November 5, 2026 05:44

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

An existing dlpack property raising AttributeError is still masked by a generic TypeError; descriptor failures must be preserved and covered by regression tests for both importers.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Preserve AttributeError raised by existing __dlpack__ descriptors

onnxruntime/​python/​onnxruntime_pybind_state_common.cc:70

If an existing __dlpack__ property raises AttributeError, this branch clears the producer's exception and replaces it with the generic invalid-capsule TypeError. That still suppresses lookup failures despite the stated error-preservation contract. Distinguish an absent protocol attribute from a failing descriptor, preserve the latter exception, and add an AttributeError-raising property regression for the public and native importers.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Retain NumPy storage in native values, fence CUDA imports and exports, and make batched capsules independently owned and exception-safe. Remove unused dlpack_at and cover ownership, callback failures, vector mutation, and CUDA stream handoffs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Defer NumPy owner destruction without blocking native workers, preserve GPU vendor metadata and matching synchronization, and batch stream waits at ORTModule execution boundaries.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Use ORT_RETURN_IF_ERROR with CUDA_CALL because the file-local CUDA_RETURN_IF_ERROR macro is declared below the provider implementation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

The new CUDA provider method uses CUDA_RETURN_IF_ERROR before it is defined, causing CUDA builds to fail compilation.

3 open issues

Review effort: Balanced

Request no-sync GPU exports when importing with synchronization disabled, preserve descriptor AttributeError, and include MIGraphX interface-only provider lookup.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

The DLPack bindings hold the GIL during device-wide synchronization, blocking other Python threads, and need to release it safely with exclusive capsule ownership established first for imports.

1 open issue
3 resolved since last review

Review effort: Balanced

Claim capsule ownership before synchronization, retain export storage across the unlocked wait, and cover concurrent consumption and failure cleanup. Pin SentencePiece to the version compatible with the protobuf runtime used by transformer CI.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

The DLPack import fails in PyTorch 2.10 when the producer tensor’s CUDA device differs from the current device, also causing the new protocol-path test to fail on multi-GPU systems.

1 open issue
1 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

The change fails to compile with Abseil disabled, risks exhausting memory by retaining NumPy allocations while the main thread is blocked, and lacks successful ROCm/MIGraphX handoff coverage.

3 open issues
1 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

Native ownership cleanup and GPU stream/device restoration require runtime validation on matching CUDA and ROCm builds, including multi-GPU and graph-capture paths, which this review environment cannot provide.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

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

Synchronization and ownership changes remain unverified because matching CUDA/ROCm/MIGraphX hardware was unavailable to validate multi-device restoration, graph-capture safety, and cross-thread cleanup.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants