Skip to content

Add GetUserProvidedOutput to KernelContext. - #32089

Open
Eric Crawford (ericcraw) wants to merge 1 commit into
microsoft:mainfrom
ericcraw:add_kernel_user_buffer_query
Open

Add GetUserProvidedOutput to KernelContext.#32089
Eric Crawford (ericcraw) wants to merge 1 commit into
microsoft:mainfrom
ericcraw:add_kernel_user_buffer_query

Conversation

@ericcraw

Copy link
Copy Markdown
Contributor

Description

Add KernelContext::GetUserProvidedOutput and the corresponding C API,
KernelContext_GetUserProvidedOutput.

The API allows an execution provider to query whether the caller supplied a
preallocated output tensor for the current kernel output and retrieve it as a
borrowed OrtValue. It does not allocate, resize, or replace output tensors.
The C++ wrapper and runtime execution-frame tracking are included, along with
tests covering Run, IoBinding, missing user outputs, invalid indices, and
direct writes to the caller-provided buffer.

Motivation and Context

Execution providers need to efficiently handle dynamically shaped models where
the caller knows the expected output shape but the EP cannot quickly or easily
determine it before inference execution. This is particularly important for
transformer models with large KV caches.

The new API allows an EP to reuse a caller-provided output buffer directly,
avoiding an intermediate allocation and subsequent copy. Since the API returns
a borrowed buffer without performing dynamic output-shape validation, the EP
must validate the tensor's element type and shape before writing to it.

Provide a method to query preallocated output tensors supplied by the
caller that are available to a kernel. This allows an execution provider
to inspect and write directly to user-provided output buffers whose shapes
may be unknown to the EP before inference but are known by the caller.

This is particularly useful for dynamically shaped transformer models with
large KV caches. An EP can reuse preallocated output tensors without
requiring shape inference before execution or allocating an intermediate
buffer and copying the result afterward. The EP remains responsible for
validating the output tensor's shape and element type before writing to it.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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.

1 participant