Skip to content

Commit 5473949

Browse files
authored
webgpu : Query for adapter support when registering WebGPU backend (#21579)
1 parent dcdcbad commit 5473949

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

ggml/src/ggml-webgpu/ggml-webgpu.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,8 +4033,14 @@ ggml_backend_reg_t ggml_backend_webgpu_reg() {
40334033
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_reg()");
40344034

40354035
static ggml_backend_webgpu_reg_context ctx;
4036+
static ggml_backend_reg reg = {
4037+
/* .api_version = */ GGML_BACKEND_API_VERSION,
4038+
/* .iface = */ ggml_backend_webgpu_reg_i,
4039+
/* .context = */ &ctx,
4040+
};
4041+
40364042
ctx.name = GGML_WEBGPU_NAME;
4037-
ctx.device_count = 1;
4043+
ctx.device_count = 0;
40384044

40394045
wgpu::InstanceDescriptor instance_descriptor{};
40404046
std::vector<wgpu::InstanceFeatureName> instance_features = { wgpu::InstanceFeatureName::TimedWaitAny };
@@ -4053,19 +4059,28 @@ ggml_backend_reg_t ggml_backend_webgpu_reg() {
40534059
ctx.webgpu_global_ctx = webgpu_global_context(new webgpu_global_context_struct());
40544060
ctx.webgpu_global_ctx->instance = std::move(inst);
40554061

4056-
#ifdef __EMSCRIPTEN__
4057-
if (ctx.webgpu_global_ctx->instance == nullptr) {
4058-
GGML_LOG_ERROR("ggml_webgpu: Failed to create WebGPU instance. Make sure either -sASYNCIFY or -sJSPI is set\n");
4059-
return nullptr;
4062+
wgpu::Adapter adapter;
4063+
if (ctx.webgpu_global_ctx->instance != nullptr) {
4064+
wgpu::RequestAdapterOptions options = {};
4065+
4066+
// probe for adapter support
4067+
ctx.webgpu_global_ctx->instance.WaitAny(
4068+
ctx.webgpu_global_ctx->instance.RequestAdapter(
4069+
&options, wgpu::CallbackMode::AllowSpontaneous,
4070+
[&adapter](wgpu::RequestAdapterStatus status, wgpu::Adapter _adapter, const char * message) {
4071+
if (status != wgpu::RequestAdapterStatus::Success) {
4072+
GGML_LOG_ERROR("ggml_webgpu: Failed to get an adapter: %s\n", message);
4073+
return;
4074+
}
4075+
adapter = std::move(_adapter);
4076+
}),
4077+
UINT64_MAX);
4078+
}
4079+
4080+
if (adapter != nullptr) {
4081+
ctx.device_count = 1;
40604082
}
4061-
#endif
4062-
GGML_ASSERT(ctx.webgpu_global_ctx->instance != nullptr);
40634083

4064-
static ggml_backend_reg reg = {
4065-
/* .api_version = */ GGML_BACKEND_API_VERSION,
4066-
/* .iface = */ ggml_backend_webgpu_reg_i,
4067-
/* .context = */ &ctx,
4068-
};
40694084
return &reg;
40704085
}
40714086

0 commit comments

Comments
 (0)