Skip to content

WebNN/WebGPU zero-copy device selection #941

Description

@bbernhar

WebNN and WebGPU select devices independently, so zero-copy interop is not guaranteed. Web developers need a reliable way to determine whether a WebNN context and GPUDevice are compatible for zero-copy.

The WebNN spec does not define how createContext() and exportToGPU() interact with device selection when both a GPUDevice and MLContextOptions (such as powerPreference) are provided.

For example, if an app passes a WebGPU GPUDevice to createContext(), zero-copy is only possible if WebNN selects that same device or a compatible one (for example, an NPU or integrated GPU sharing the same memory). Otherwise, exportToGPU() may require a copy or fail because the selected devices are incompatible.

One possible solution would be to add an { interopDevice: gpuDevice } option to createContext() as a hint for interop compatibility. If provided, WebNN would preferentially select a compatible device, allowing exportToGPU() to remain zero-copy when possible.

@reillyeon @RafaelCintron @huningxin @anssiko

Activity

  1. reillyeon commented on Jul 17, 2026

    @reillyeon
    Contributor

    What do you think about remove the createContext(GPUDevice) function and replacing it with your proposed interopDevice option? This would allow for the case where a non-GPU device is selected based on the preferences but still with a bias towards the best possible interop with WebGPU.

  2. bbernhar commented on Jul 21, 2026

    @bbernhar
    ContributorAuthor

    @reillyeon I agree. I think interopDevice is a cleaner abstraction than a dedicated createContext(GPUDevice) overload.

  3. changed the title [-]WebNN/WebGPU device selection for interop[/-] [+]WebNN/WebGPU zero-copy device selection[/+] on Sep 10, 2026
  4. mwyrzykowski commented on Sep 10, 2026

    @mwyrzykowski

    Looks good to me @bbernhar 👍

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions