Skip to content

Commit 17c5411

Browse files
reeselevineggerganov
authored andcommitted
ggml: Add initial WebGPU backend (llama/14521)
* Minimal setup of webgpu backend with dawn. Just prints out the adapter and segfaults * Initialize webgpu device * Making progress on setting up the backend * Finish more boilerplate/utility functions * Organize file and work on alloc buffer * Add webgpu_context to prepare for actually running some shaders * Work on memset and add shader loading * Work on memset polyfill * Implement set_tensor as webgpu WriteBuffer, remove host_buffer stubs since webgpu doesn't support it * Implement get_tensor and buffer_clear * Finish rest of setup * Start work on compute graph * Basic mat mul working * Work on emscripten build * Basic WebGPU backend instructions * Use EMSCRIPTEN flag * Work on passing ci, implement 4d tensor multiplication * Pass thread safety test * Implement permuting for mul_mat and cpy * minor cleanups * Address feedback * Remove division by type size in cpy op * Fix formatting and add github action workflows for vulkan and metal (m-series) webgpu backends * Fix name * Fix macos dawn prefix path
1 parent ae1bb2c commit 17c5411

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

ggml/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ option(GGML_VULKAN_MEMORY_DEBUG "ggml: enable Vulkan memory debug ou
181181
option(GGML_VULKAN_SHADER_DEBUG_INFO "ggml: enable Vulkan shader debug info" OFF)
182182
option(GGML_VULKAN_VALIDATE "ggml: enable Vulkan validation" OFF)
183183
option(GGML_VULKAN_RUN_TESTS "ggml: run Vulkan tests" OFF)
184+
option(GGML_WEBGPU "ggml: use WebGPU" OFF)
185+
option(GGML_WEBGPU_DEBUG "ggml: enable WebGPU debug output" OFF)
184186
option(GGML_METAL "ggml: use Metal" ${GGML_METAL_DEFAULT})
185187
option(GGML_METAL_USE_BF16 "ggml: use bfloat if available" OFF)
186188
option(GGML_METAL_NDEBUG "ggml: disable Metal debugging" OFF)
@@ -270,6 +272,7 @@ set(GGML_PUBLIC_HEADERS
270272
include/ggml-rpc.h
271273
include/ggml-sycl.h
272274
include/ggml-vulkan.h
275+
include/ggml-webgpu.h
273276
include/gguf.h)
274277

275278
set_target_properties(ggml PROPERTIES PUBLIC_HEADER "${GGML_PUBLIC_HEADERS}")

ggml/include/ggml-webgpu.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "ggml.h"
4+
#include "ggml-backend.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#define GGML_WEBGPU_NAME "WebGPU"
11+
12+
// Needed for examples in ggml
13+
GGML_BACKEND_API ggml_backend_t ggml_backend_webgpu_init(void);
14+
15+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_webgpu_reg(void);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif

ggml/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ ggml_add_backend(MUSA)
370370
ggml_add_backend(RPC)
371371
ggml_add_backend(SYCL)
372372
ggml_add_backend(Vulkan)
373+
ggml_add_backend(WebGPU)
373374
ggml_add_backend(OpenCL)
374375

375376
foreach (target ggml-base ggml)

ggml/src/ggml-backend-reg.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
#include "ggml-vulkan.h"
4646
#endif
4747

48+
#ifdef GGML_USE_WEBGPU
49+
#include "ggml-webgpu.h"
50+
#endif
51+
4852
#ifdef GGML_USE_OPENCL
4953
#include "ggml-opencl.h"
5054
#endif
@@ -173,6 +177,9 @@ struct ggml_backend_registry {
173177
#ifdef GGML_USE_VULKAN
174178
register_backend(ggml_backend_vk_reg());
175179
#endif
180+
#ifdef GGML_USE_WEBGPU
181+
register_backend(ggml_backend_webgpu_reg());
182+
#endif
176183
#ifdef GGML_USE_OPENCL
177184
register_backend(ggml_backend_opencl_reg());
178185
#endif

0 commit comments

Comments
 (0)