Skip to content

Fix WebGPU instance creation for caller-supplied GPUDevice - #1

Open
4n4ny4 wants to merge 1 commit into
mainfrom
t-anaanand-microsoft-fix-webgpu-external-device
Open

Fix WebGPU instance creation for caller-supplied GPUDevice#1
4n4ny4 wants to merge 1 commit into
mainfrom
t-anaanand-microsoft-fix-webgpu-external-device

Conversation

@4n4ny4

@4n4ny4 4n4ny4 commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Problem

Sessions that import a caller-supplied GPUDevice hang inside WebGpuContext::Wait().

onnxruntime/wasm/post-webgpu.js created the WGPUInstance with a null descriptor, so the instance lacked the TimedWaitAny feature. WebGpuContext::Wait() calls instance_.WaitAny(f, UINT64_MAX), and emdawnwebgpu returns wait status 3 because timed waits were never enabled. The run never completes.

This only reproduces when the caller supplies their own GPUDevice, which is why the default path is unaffected and the issue went unnoticed.

Change

  • Factor the native instance descriptor into WebGpuContext::CreateWebGpuInstance() so there is a single definition of what the native waiter requires.
  • Export it to WASM as OrtCreateWebGpuInstance and use it from post-webgpu.js, so the JS-created instance matches.
  • Reject instance creation failure explicitly instead of proceeding with a null handle.
  • Reset custom-device registration in a finally block, so a failed session creation no longer poisons subsequent attempts.

Validation

New browser unit test js/web/test/unittests/backends/wasm/test-webgpu-device.ts covers the custom-device path, the default path, repeated session lifecycle, and cleanup after a forced creation failure. Full browser unit suite passes.

Notes

Pure correctness fix — no performance claims. This is a prerequisite for any benchmark that shares a single GPUDevice between runtimes.

Sessions that import an external GPUDevice hung inside
WebGpuContext::Wait(). post-webgpu.js created the WGPUInstance with a
null descriptor, so the instance lacked the TimedWaitAny feature, while
WebGpuContext::Wait() calls instance_.WaitAny(f, UINT64_MAX).
emdawnwebgpu returned wait status 3 and the run never completed. This
only reproduces when the caller supplies their own GPUDevice, which is
why the default path was unaffected.

Factor the native instance descriptor into
WebGpuContext::CreateWebGpuInstance(), export it to WASM as
OrtCreateWebGpuInstance, and use it from post-webgpu.js so the
JS-created instance matches what the native waiter requires. Creation
failure is now rejected explicitly, and custom-device registration is
reset in a finally block so a failed session creation no longer poisons
subsequent attempts.

Adds a browser unit test covering the custom and default device paths,
repeated session lifecycle, and cleanup after a creation failure.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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