Skip to content

[WebGPU] Report copy_tensors misuse instead of terminating the process - #32315

Merged
Ananya Anand (4n4ny4) merged 1 commit into
microsoft:mainfrom
4n4ny4:webgpu-copy-tensors-misuse
Sep 2, 2026
Merged

Ananya Anand (4n4ny4) merged 1 commit into
microsoft:mainfrom
4n4ny4:webgpu-copy-tensors-misuse

Conversation

@4n4ny4

Copy link
Copy Markdown
Contributor

Description

BufferManager::MemCpy reports misuse with ORT_ENFORCE, which throws: an aliased src/dst
pair, an undersized destination and a still-mapped buffer all throw rather than returning a
Status. The shared data transfer reaches it through WebGpuDataTransferImpl::CopyTensorsImpl,
which is a noexcept C ABI callback and only handled a returned non-OK Status. The exception
escaped a noexcept frame, so std::terminate took the process down with no message and no
stack.

This wraps the callback body so any throw becomes an OrtStatus, which is what the
surrounding C API already expects.

The two sibling callbacks are deliberately left alone: CanCopyImpl only calls C ABI function
pointers, and ReleaseImpl returns void, so converting a throw there would mean swallowing it
rather than reporting it, which is a separate decision from this one.

Motivation and Context

Not currently reachable from Python, because copy_tensors rejects every WebGPU OrtValue
outright. It becomes reachable as soon as the supported copies are allowed, which is what
#32074 does, so it is worth fixing at the boundary on its own rather than landing inside a
larger change.

The regression test for it uses the session-scoped allocation APIs under discussion in #32074
and is held there rather than duplicated here.

Copilot AI balanced review requested due to automatic review settings August 28, 2026 19:40

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

Converts WebGPU copy failures into OrtStatus errors instead of allowing exceptions to terminate the process.

Changes:

  • Adds exception handling around the noexcept C ABI callback.
  • Extracts the potentially throwing copy implementation into a helper.

πŸ’‘ Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

BufferManager::MemCpy reports misuse with ORT_ENFORCE, which throws: an aliased
src/dst pair, an undersized destination and a still-mapped buffer all throw
rather than returning a Status. The shared data transfer reaches it through
WebGpuDataTransferImpl::CopyTensorsImpl, a noexcept C ABI callback that only
handled a returned non-OK Status, so the exception escaped a noexcept frame and
std::terminate ended the process with no message.

Split the body into CopyTensorsOrThrow and wrap the callback in
API_IMPL_BEGIN/API_IMPL_END, the conversion the rest of the C API already uses,
so an ORT_ENFORCE failure is reported as ORT_FAIL rather than ending the
process.

Cover it with a self-copy of a device tensor in WebGpuPluginSharedAllocatorTest.
That reaches the aliased-buffer enforce and asserts both that an error status
comes back and that the tensor still round trips afterwards. Without the
conversion the test fast-fails the process rather than failing.
@4n4ny4
Ananya Anand (4n4ny4) merged commit c4231ee into microsoft:main Sep 2, 2026
90 checks passed
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