|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/backends/webgpu/runtime/WebGPUGraph.h> |
| 10 | +#include <executorch/backends/webgpu/runtime/WebGPUUtils.h> |
| 11 | +#include <executorch/backends/webgpu/runtime/ops/OperatorRegistry.h> |
| 12 | +#include <executorch/backends/webgpu/runtime/ops/embedding/embedding_wgsl.h> |
| 13 | + |
| 14 | +#include <webgpu/webgpu.h> |
| 15 | + |
| 16 | +#include <cstdint> |
| 17 | +#include <stdexcept> |
| 18 | + |
| 19 | +namespace executorch::backends::webgpu { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +struct EmbeddingParams { |
| 24 | + uint32_t num_elements; |
| 25 | + uint32_t dim; |
| 26 | + uint32_t _pad[2]; |
| 27 | +}; |
| 28 | + |
| 29 | +// aten.embedding: out[row, :] = weight[indices[row], :] (fp32 weight, i32 idx). |
| 30 | +void embedding_impl(WebGPUGraph& graph, const std::vector<int>& args) { |
| 31 | + const int weight_id = args.at(0); |
| 32 | + const int indices_id = args.at(1); |
| 33 | + const int out_id = args.at(args.size() - 1); |
| 34 | + |
| 35 | + WGPUDevice device = graph.device(); |
| 36 | + const auto& weight = graph.get_tensor(weight_id); |
| 37 | + const auto& indices = graph.get_tensor(indices_id); |
| 38 | + const auto& out = graph.get_tensor(out_id); |
| 39 | + |
| 40 | + if (weight.buffer == nullptr || indices.buffer == nullptr || |
| 41 | + out.buffer == nullptr) { |
| 42 | + throw std::runtime_error("embedding: null buffer binding"); |
| 43 | + } |
| 44 | + if (weight.dims.size() != 2) { |
| 45 | + throw std::runtime_error("embedding: weight must be 2D [vocab, dim]"); |
| 46 | + } |
| 47 | + const uint32_t dim = static_cast<uint32_t>(weight.dims[1]); |
| 48 | + if (dim == 0) { |
| 49 | + throw std::runtime_error("embedding: dim == 0"); |
| 50 | + } |
| 51 | + const size_t out_numel = out.nbytes / sizeof(float); |
| 52 | + // Index is the int32 downcast of the int64 indices (mirror index op). |
| 53 | + const size_t index_numel = indices.nbytes / sizeof(int32_t); |
| 54 | + if (out.nbytes != out_numel * sizeof(float) || |
| 55 | + weight.nbytes % sizeof(float) != 0 || |
| 56 | + indices.nbytes != index_numel * sizeof(int32_t)) { |
| 57 | + throw std::runtime_error( |
| 58 | + "embedding: fp32 weight/out + i32 indices required"); |
| 59 | + } |
| 60 | + if (out_numel != index_numel * dim) { |
| 61 | + throw std::runtime_error("embedding: out numel != num_indices * dim"); |
| 62 | + } |
| 63 | + |
| 64 | + uint32_t num_elements = static_cast<uint32_t>(out_numel); |
| 65 | + uint32_t wg_size = |
| 66 | + utils::clamp_workgroup_size(device, kEmbeddingWorkgroupSizeX); |
| 67 | + utils::WgCount workgroup_count = utils::compute_2d_workgroup_count( |
| 68 | + device, num_elements, wg_size, "embedding"); |
| 69 | + |
| 70 | + WGPUConstantEntry wg_size_constant = {}; |
| 71 | + wg_size_constant.key = {"wg_size", WGPU_STRLEN}; |
| 72 | + wg_size_constant.value = static_cast<double>(wg_size); |
| 73 | + |
| 74 | + EmbeddingParams params = {}; |
| 75 | + params.num_elements = num_elements; |
| 76 | + params.dim = dim; |
| 77 | + WGPUBuffer uniform_buffer = |
| 78 | + utils::make_uniform(device, ¶ms, sizeof(EmbeddingParams)); |
| 79 | + graph.add_uniform_buffer_bytes(sizeof(EmbeddingParams)); |
| 80 | + |
| 81 | + WGPUShaderSourceWGSL wgsl_desc = {}; |
| 82 | + wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL; |
| 83 | + wgsl_desc.code = {kEmbeddingWGSL, WGPU_STRLEN}; |
| 84 | + WGPUShaderModuleDescriptor shader_desc = {}; |
| 85 | + shader_desc.nextInChain = &wgsl_desc.chain; |
| 86 | + WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc); |
| 87 | + |
| 88 | + WGPUBindGroupLayoutEntry entries[4] = {}; |
| 89 | + entries[0].binding = 0; |
| 90 | + entries[0].visibility = WGPUShaderStage_Compute; |
| 91 | + entries[0].buffer.type = WGPUBufferBindingType_ReadOnlyStorage; |
| 92 | + entries[1].binding = 1; |
| 93 | + entries[1].visibility = WGPUShaderStage_Compute; |
| 94 | + entries[1].buffer.type = WGPUBufferBindingType_ReadOnlyStorage; |
| 95 | + entries[2].binding = 2; |
| 96 | + entries[2].visibility = WGPUShaderStage_Compute; |
| 97 | + entries[2].buffer.type = WGPUBufferBindingType_Storage; |
| 98 | + entries[3].binding = 3; |
| 99 | + entries[3].visibility = WGPUShaderStage_Compute; |
| 100 | + entries[3].buffer.type = WGPUBufferBindingType_Uniform; |
| 101 | + |
| 102 | + WGPUBindGroupLayoutDescriptor bgl_desc = {}; |
| 103 | + bgl_desc.entryCount = 4; |
| 104 | + bgl_desc.entries = entries; |
| 105 | + WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc); |
| 106 | + |
| 107 | + WGPUPipelineLayoutDescriptor pl_desc = {}; |
| 108 | + pl_desc.bindGroupLayoutCount = 1; |
| 109 | + pl_desc.bindGroupLayouts = &bgl; |
| 110 | + WGPUPipelineLayout pipeline_layout = |
| 111 | + wgpuDeviceCreatePipelineLayout(device, &pl_desc); |
| 112 | + |
| 113 | + WGPUComputePipelineDescriptor pipeline_desc = {}; |
| 114 | + pipeline_desc.layout = pipeline_layout; |
| 115 | + pipeline_desc.compute.module = shader; |
| 116 | + pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN}; |
| 117 | + pipeline_desc.compute.constantCount = 1; |
| 118 | + pipeline_desc.compute.constants = &wg_size_constant; |
| 119 | + WGPUComputePipeline pipeline = |
| 120 | + wgpuDeviceCreateComputePipeline(device, &pipeline_desc); |
| 121 | + |
| 122 | + WGPUBindGroupEntry bg_entries[4] = {}; |
| 123 | + bg_entries[0].binding = 0; |
| 124 | + bg_entries[0].buffer = weight.buffer; |
| 125 | + bg_entries[0].size = weight.nbytes; |
| 126 | + bg_entries[1].binding = 1; |
| 127 | + bg_entries[1].buffer = indices.buffer; |
| 128 | + bg_entries[1].size = indices.nbytes; |
| 129 | + bg_entries[2].binding = 2; |
| 130 | + bg_entries[2].buffer = out.buffer; |
| 131 | + bg_entries[2].size = out.nbytes; |
| 132 | + bg_entries[3].binding = 3; |
| 133 | + bg_entries[3].buffer = uniform_buffer; |
| 134 | + bg_entries[3].size = sizeof(EmbeddingParams); |
| 135 | + |
| 136 | + WGPUBindGroupDescriptor bg_desc = {}; |
| 137 | + bg_desc.layout = bgl; |
| 138 | + bg_desc.entryCount = 4; |
| 139 | + bg_desc.entries = bg_entries; |
| 140 | + WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc); |
| 141 | + |
| 142 | + graph.add_dispatch( |
| 143 | + {pipeline, bind_group, workgroup_count.x, "", workgroup_count.y}); |
| 144 | + |
| 145 | + wgpuShaderModuleRelease(shader); |
| 146 | + wgpuBindGroupLayoutRelease(bgl); |
| 147 | + wgpuPipelineLayoutRelease(pipeline_layout); |
| 148 | + wgpuBufferRelease(uniform_buffer); |
| 149 | +} |
| 150 | + |
| 151 | +} // namespace |
| 152 | + |
| 153 | +WEBGPU_REGISTER_OPERATORS { |
| 154 | + WEBGPU_REGISTER_OP(aten.embedding.default, embedding_impl); |
| 155 | +} |
| 156 | + |
| 157 | +} // namespace executorch::backends::webgpu |
0 commit comments