Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backends/webgpu/runtime/WebGPUBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Result<DelegateHandle*> WebGPUBackend::init(
}

try {
graph->build(flatbuffer_data, constant_data);
graph->build(flatbuffer_data, constant_data, context.get_named_data_map());
} catch (const std::exception& e) {
ET_LOG(Error, "WebGPU graph build failed: %s", e.what());
graph->~WebGPUGraph();
Expand Down
29 changes: 28 additions & 1 deletion backends/webgpu/runtime/WebGPUGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <executorch/backends/webgpu/runtime/ops/OperatorRegistry.h>

#include <executorch/backends/vulkan/serialization/schema_generated.h>
#include <executorch/runtime/core/named_data_map.h>

#include <executorch/backends/webgpu/runtime/WebGPUDevice.h>
#include <webgpu/wgpu.h>
Expand Down Expand Up @@ -93,7 +94,8 @@ WebGPUGraph::~WebGPUGraph() {

void WebGPUGraph::build(
const void* flatbuffer_data,
const uint8_t* constant_data) {
const uint8_t* constant_data,
const executorch::runtime::NamedDataMap* named_data_map) {
if (!device_) {
auto* ctx = get_default_webgpu_context();
if (ctx) {
Expand Down Expand Up @@ -165,6 +167,31 @@ void WebGPUGraph::build(
const uint8_t* src = constant_data + vk_bytes->offset();
wgpuQueueWriteBuffer(
queue_, tensor.buffer, 0, src, tensor.nbytes);
} else if (
vk_bytes->named_key() != nullptr &&
named_data_map != nullptr) {
// Constant stored in the PTE named-data map.
auto buf =
named_data_map->get_data(vk_bytes->named_key()->c_str());
if (!buf.ok()) {
throw std::runtime_error(
std::string("WebGPU: named constant '") +
vk_bytes->named_key()->c_str() +
"' not found in NamedDataMap");
}
if (buf->size() < tensor.nbytes) {
throw std::runtime_error(
std::string("WebGPU: named constant '") +
vk_bytes->named_key()->c_str() + "' undersized: have " +
std::to_string(buf->size()) + " bytes, need " +
std::to_string(tensor.nbytes));
}
wgpuQueueWriteBuffer(
queue_, tensor.buffer, 0, buf->data(), tensor.nbytes);
buf->Free();
} else {
throw std::runtime_error(
"WebGPU: constant has no inline offset and no named-data key");
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion backends/webgpu/runtime/WebGPUGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <unordered_map>
#include <vector>

#include <executorch/runtime/core/named_data_map.h>

namespace executorch {
namespace backends {
namespace webgpu {
Expand Down Expand Up @@ -66,7 +68,10 @@ class WebGPUGraph {

// Build the graph from a deserialized VkGraph flatbuffer and constant data.
// The flatbuffer_data pointer must remain valid during build().
void build(const void* flatbuffer_data, const uint8_t* constant_data);
void build(
const void* flatbuffer_data,
const uint8_t* constant_data,
const executorch::runtime::NamedDataMap* named_data_map = nullptr);

// Copy input tensor data from host pointers into GPU buffers.
void copy_inputs(const std::vector<std::pair<const void*, size_t>>& inputs);
Expand Down
Loading