Skip to content

Commit 39d956d

Browse files
committed
Start work on compute graph
1 parent f8a53ee commit 39d956d

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,39 @@ static void ggml_backend_webgpu_free(ggml_backend_t backend) {
199199
// TODO: cleanup
200200
}
201201

202+
// Returns true if node has enqueued work into the queue, false otherwise
203+
static bool ggml_webgpu_encode_node(webgpu_context ctx, ggml_tensor * node){
204+
if (ggml_is_empty(node)) {
205+
return false;
206+
}
207+
208+
WEBGPU_LOG_DEBUG("ggml_webgpu_encode_node(" << node << ", " << ggml_op_name(node->op) << ")");
209+
210+
switch (node->op) {
211+
// no-op
212+
case GGML_OP_NONE:
213+
// these next four ops modify the logical view of the tensor, but do not change its data
214+
case GGML_OP_RESHAPE:
215+
case GGML_OP_VIEW:
216+
case GGML_OP_PERMUTE:
217+
case GGML_OP_TRANSPOSE:
218+
return false;
219+
default:
220+
return false;
221+
}
222+
}
223+
224+
static ggml_status ggml_backend_webgpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
225+
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_graph_compute(" << cgraph->n_nodes << " nodes)");
226+
227+
ggml_backend_webgpu_context * backend_ctx = static_cast<ggml_backend_webgpu_context *>(backend->context);
228+
webgpu_context ctx = backend_ctx->webgpu_ctx;
229+
230+
for (int i = 0; i < cgraph->n_nodes; i++) {
231+
ggml_webgpu_encode_node(ctx, cgraph->nodes[i]);
232+
}
233+
}
234+
202235
static ggml_backend_i ggml_backend_webgpu_i = {
203236
/* .get_name = */ ggml_backend_webgpu_name,
204237
/* .free = */ ggml_backend_webgpu_free,
@@ -209,8 +242,8 @@ static ggml_backend_i ggml_backend_webgpu_i = {
209242
/* .graph_plan_create = */ NULL,
210243
/* .graph_plan_free = */ NULL,
211244
/* .graph_plan_update = */ NULL,
212-
/* .graph_plan_compute = */ NULL, // TODO
213-
/* .graph_compute = */ NULL,
245+
/* .graph_plan_compute = */ NULL,
246+
/* .graph_compute = */ ggml_backend_webgpu_graph_compute,
214247
/* .event_record = */ NULL,
215248
/* .event_wait = */ NULL,
216249
};
@@ -228,7 +261,6 @@ static void ggml_backend_webgpu_buffer_free_buffer(ggml_backend_buffer_t buffer)
228261

229262
// Returns the "fake" base pointer.
230263
static void * ggml_backend_webgpu_buffer_get_base(ggml_backend_buffer_t buffer) {
231-
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_buffer_get_base()");
232264
GGML_UNUSED(buffer);
233265
return webgpu_ptr_base;
234266
}
@@ -354,13 +386,11 @@ static size_t ggml_backend_webgpu_buffer_type_get_max_size(ggml_backend_buffer_t
354386
/* GGML Backend Device Interface */
355387

356388
static const char * ggml_backend_webgpu_device_get_name(ggml_backend_dev_t dev) {
357-
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_device_get_name()");
358389
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
359390
return ctx->device_name.c_str();
360391
}
361392

362393
static const char * ggml_backend_webgpu_device_get_description(ggml_backend_dev_t dev) {
363-
WEBGPU_LOG_DEBUG("ggml_backend_webgpu_device_get_description()");
364394
ggml_backend_webgpu_device_context * ctx = static_cast<ggml_backend_webgpu_device_context *>(dev->context);
365395
return ctx->device_desc.c_str();
366396
}
@@ -499,6 +529,13 @@ static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const
499529

500530
// what should we support first?
501531
switch (op->op) {
532+
case GGML_OP_NONE:
533+
case GGML_OP_RESHAPE:
534+
case GGML_OP_VIEW:
535+
case GGML_OP_PERMUTE:
536+
case GGML_OP_TRANSPOSE:
537+
return true;
538+
502539
default:
503540
return false;
504541
}

0 commit comments

Comments
 (0)