From 54739490707d4838630887a666f124c0a60003bb Mon Sep 17 00:00:00 2001 From: Reese Levine Date: Wed, 8 Apr 2026 06:08:29 -0700 Subject: [PATCH] webgpu : Query for adapter support when registering WebGPU backend (#21579) --- ggml/src/ggml-webgpu/ggml-webgpu.cpp | 41 +++++++++++++++++++--------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/ggml/src/ggml-webgpu/ggml-webgpu.cpp b/ggml/src/ggml-webgpu/ggml-webgpu.cpp index 3d038924b7..b8df0f4dd0 100644 --- a/ggml/src/ggml-webgpu/ggml-webgpu.cpp +++ b/ggml/src/ggml-webgpu/ggml-webgpu.cpp @@ -4033,8 +4033,14 @@ ggml_backend_reg_t ggml_backend_webgpu_reg() { WEBGPU_LOG_DEBUG("ggml_backend_webgpu_reg()"); static ggml_backend_webgpu_reg_context ctx; + static ggml_backend_reg reg = { + /* .api_version = */ GGML_BACKEND_API_VERSION, + /* .iface = */ ggml_backend_webgpu_reg_i, + /* .context = */ &ctx, + }; + ctx.name = GGML_WEBGPU_NAME; - ctx.device_count = 1; + ctx.device_count = 0; wgpu::InstanceDescriptor instance_descriptor{}; std::vector instance_features = { wgpu::InstanceFeatureName::TimedWaitAny }; @@ -4053,19 +4059,28 @@ ggml_backend_reg_t ggml_backend_webgpu_reg() { ctx.webgpu_global_ctx = webgpu_global_context(new webgpu_global_context_struct()); ctx.webgpu_global_ctx->instance = std::move(inst); -#ifdef __EMSCRIPTEN__ - if (ctx.webgpu_global_ctx->instance == nullptr) { - GGML_LOG_ERROR("ggml_webgpu: Failed to create WebGPU instance. Make sure either -sASYNCIFY or -sJSPI is set\n"); - return nullptr; - } -#endif - GGML_ASSERT(ctx.webgpu_global_ctx->instance != nullptr); + wgpu::Adapter adapter; + if (ctx.webgpu_global_ctx->instance != nullptr) { + wgpu::RequestAdapterOptions options = {}; + + // probe for adapter support + ctx.webgpu_global_ctx->instance.WaitAny( + ctx.webgpu_global_ctx->instance.RequestAdapter( + &options, wgpu::CallbackMode::AllowSpontaneous, + [&adapter](wgpu::RequestAdapterStatus status, wgpu::Adapter _adapter, const char * message) { + if (status != wgpu::RequestAdapterStatus::Success) { + GGML_LOG_ERROR("ggml_webgpu: Failed to get an adapter: %s\n", message); + return; + } + adapter = std::move(_adapter); + }), + UINT64_MAX); + } + + if (adapter != nullptr) { + ctx.device_count = 1; + } - static ggml_backend_reg reg = { - /* .api_version = */ GGML_BACKEND_API_VERSION, - /* .iface = */ ggml_backend_webgpu_reg_i, - /* .context = */ &ctx, - }; return ® }