Skip to content

Add update_inplace overload accepting OrtValue for device-to-device copy - #28256

Merged
Tianlei Wu (tianleiwu) merged 7 commits into
mainfrom
copilot/update-ortvalue-inplace-cuda-graph
Apr 29, 2026
Merged

Add update_inplace overload accepting OrtValue for device-to-device copy#28256
Tianlei Wu (tianleiwu) merged 7 commits into
mainfrom
copilot/update-ortvalue-inplace-cuda-graph

Conversation

Copilot AI commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Description

Adds an OrtValue overload to update_inplace so GPU-resident data can be copied directly to another OrtValue without roundtripping through CPU.

  • C++ pybind (onnxruntime_pybind_ortvalue.cc): New update_inplace(const OrtValue*) overload. Uses CreateDataTransferMemCpy for plugin EPs, with fallback to built-in copy functions for CUDA (including GPU↔GPU via GetGPUDataTransfer()), MIGraphX, DML, and CANN.
  • Python wrapper (onnxruntime_inference_collection.py): update_inplace now accepts either a numpy array or an OrtValue, dispatching to the appropriate C++ overload.
  • Tests (onnxruntime_test_python_cudagraph.py): Covers CPU→CPU, GPU→GPU, CPU→GPU, and GPU→CPU OrtValue copy paths.
# Before: requires numpy (CPU) source, even when data is already on GPU
ortvalue_gpu.update_inplace(np_array)

# After: accepts OrtValue directly for device-to-device copy
ortvalue_gpu_src = onnxrt.OrtValue.ortvalue_from_numpy(data, "cuda", 0)
ortvalue_gpu_dst.update_inplace(ortvalue_gpu_src)  # GPU-to-GPU, no CPU roundtrip

Motivation and Context

CUDA graph replay requires inputs at fixed memory addresses. When source data (e.g., encoder output) is already on GPU, the only option was to use external libraries like cuda-python for device-to-device memcpy. This change makes that workflow native to ORT, per the approach suggested in the issue discussion: accept an OrtValue in update_inplace to leverage ORT's existing data transfer infrastructure.

Add a second overload of update_inplace that accepts an OrtValue as source,
enabling device-to-device memory copy (e.g., GPU to GPU) without requiring
data to go through CPU. This is useful for CUDA graph scenarios where inputs
need to be updated in fixed memory addresses.

Changes:
- onnxruntime_pybind_ortvalue.cc: Add pybind overload for OrtValue source
- onnxruntime_inference_collection.py: Update Python wrapper to accept
  both numpy arrays and OrtValues
- onnxruntime_test_python_cudagraph.py: Add tests for OrtValue-to-OrtValue
  update_inplace across different device combinations

Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/a05648d1-3616-4d6e-a448-b8d506c55854

Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Copilot AI changed the title [WIP] Add API to update OrtValue inplace from CUDA buffer Add update_inplace overload accepting OrtValue for device-to-device copy Apr 28, 2026

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/python/onnxruntime_pybind_ortvalue.cc Outdated
Comment thread onnxruntime/python/onnxruntime_pybind_ortvalue.cc Outdated
GitHub Copilot added 2 commits April 28, 2026 11:19
- Create UpdateOrtValueInplace() helper in onnxruntime_pybind_mlvalue for code reuse
- Simplify Python binding to call helper instead of duplicating logic
- Supports device-to-device transfers (CPU, GPU, DML, CANN, MIGraphX)
- Addresses review feedback on PR #28256

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.

Pull request overview

Adds an OrtValue-to-OrtValue overload for OrtValue.update_inplace to enable direct device-to-device updates (notably GPU↔GPU) without requiring a NumPy (CPU) intermediate, and extends Python tests to cover the new copy paths.

Changes:

  • Add a new pybind update_inplace(OrtValue) overload and implement python::UpdateOrtValueInplace(dst, src) device-aware copy logic.
  • Update the Python OrtValue.update_inplace wrapper to accept either a NumPy array or an OrtValue.
  • Add Python tests for CPU↔CPU, GPU↔GPU, CPU→GPU, and GPU→CPU OrtValue copy scenarios (CUDA).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
onnxruntime/test/python/onnxruntime_test_python_cudagraph.py Adds tests validating update_inplace with OrtValue sources across CPU/GPU (CUDA) copy directions.
onnxruntime/python/onnxruntime_pybind_ortvalue.cc Exposes a new update_inplace overload accepting an OrtValue source.
onnxruntime/python/onnxruntime_pybind_mlvalue.h Declares UpdateOrtValueInplace(dst, src) helper for in-place OrtValue tensor updates.
onnxruntime/python/onnxruntime_pybind_mlvalue.cc Implements UpdateOrtValueInplace with plugin EP copy support + EP-specific fallbacks (CUDA/MIGraphX/DML/CANN).
onnxruntime/python/onnxruntime_inference_collection.py Updates Python wrapper dispatch to route OrtValue sources to the new C++ overload.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/python/onnxruntime_pybind_ortvalue.cc Outdated
Comment thread onnxruntime/python/onnxruntime_pybind_mlvalue.cc Outdated
Comment thread onnxruntime/python/onnxruntime_inference_collection.py Outdated

@github-advanced-security github-advanced-security 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.

lintrunner found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/python/onnxruntime_pybind_mlvalue.cc Outdated
Tianlei Wu (tianleiwu) and others added 2 commits April 28, 2026 18:22
The is_dml_device lambda only checked OrtDevice::GPU with Microsoft
vendor, but DML tensors can also use the dedicated OrtDevice::DML
device type. This caused CPU↔DML copies through the OrtValue overload
to incorrectly fall through and throw 'Unable to copy...'.
Co-authored-by: Copilot <copilot@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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tianleiwu
Tianlei Wu (tianleiwu) merged commit 9a41944 into main Apr 29, 2026
94 checks passed
@tianleiwu
Tianlei Wu (tianleiwu) deleted the copilot/update-ortvalue-inplace-cuda-graph branch April 29, 2026 21:11
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.

[Feature Request] Update OrtValue inplace from CUDA buffer for cuda graph

5 participants