|
| 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/TensorMeta.h> |
| 13 | +#include <executorch/backends/webgpu/runtime/ops/select/select_wgsl.h> |
| 14 | + |
| 15 | +#include <webgpu/webgpu.h> |
| 16 | + |
| 17 | +#include <cstdint> |
| 18 | +#include <stdexcept> |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +namespace executorch::backends::webgpu { |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +struct SelectParams { |
| 27 | + uint32_t dim; |
| 28 | + uint32_t index; |
| 29 | + uint32_t _pad[2]; |
| 30 | +}; |
| 31 | + |
| 32 | +// dim/index are required Ints (SymInt throws); no Null default unlike slice. |
| 33 | +int64_t read_scalar(WebGPUGraph& graph, int id, const char* what) { |
| 34 | + if (graph.get_value_type(id) == WebGPUGraph::ValueType::Int) { |
| 35 | + return graph.get_int(id); |
| 36 | + } |
| 37 | + throw std::runtime_error(std::string("select: dynamic/unsupported ") + what); |
| 38 | +} |
| 39 | + |
| 40 | +void select_impl(WebGPUGraph& graph, const std::vector<int>& args) { |
| 41 | + // args: [self, dim, index, out]; output rank = in rank - 1. |
| 42 | + const int in_id = args.at(0); |
| 43 | + const int out_id = args.at(3); |
| 44 | + |
| 45 | + WGPUDevice device = graph.device(); |
| 46 | + const auto& in_tensor = graph.get_tensor(in_id); |
| 47 | + const auto& out_tensor = graph.get_tensor(out_id); |
| 48 | + if (in_tensor.buffer == nullptr || out_tensor.buffer == nullptr) { |
| 49 | + throw std::runtime_error("select: null buffer binding"); |
| 50 | + } |
| 51 | + |
| 52 | + const int in_ndim = static_cast<int>(in_tensor.dims.size()); |
| 53 | + int64_t dim = read_scalar(graph, args.at(1), "dim"); |
| 54 | + if (dim < 0) { |
| 55 | + dim += in_ndim; |
| 56 | + } |
| 57 | + if (dim < 0 || dim >= in_ndim) { |
| 58 | + throw std::runtime_error("select: dim out of range"); |
| 59 | + } |
| 60 | + const int64_t in_size = in_tensor.dims[dim]; |
| 61 | + int64_t index = read_scalar(graph, args.at(2), "index"); |
| 62 | + if (index < 0) { |
| 63 | + index += in_size; |
| 64 | + } |
| 65 | + if (index < 0 || index >= in_size) { |
| 66 | + throw std::runtime_error("select: index out of range"); |
| 67 | + } |
| 68 | + |
| 69 | + TensorMeta out_meta; |
| 70 | + TensorMeta in_meta; |
| 71 | + fill_tensor_meta(out_tensor, &out_meta); |
| 72 | + fill_tensor_meta(in_tensor, &in_meta); |
| 73 | + if (out_tensor.nbytes != |
| 74 | + static_cast<size_t>(out_meta.numel) * sizeof(float) || |
| 75 | + in_tensor.nbytes != static_cast<size_t>(in_meta.numel) * sizeof(float)) { |
| 76 | + throw std::runtime_error("select: non-fp32 operand (nbytes != numel * 4)"); |
| 77 | + } |
| 78 | + |
| 79 | + SelectParams params = {}; |
| 80 | + params.dim = static_cast<uint32_t>(dim); |
| 81 | + params.index = static_cast<uint32_t>(index); |
| 82 | + |
| 83 | + uint32_t wg_size = utils::clamp_workgroup_size(device, kSelectWorkgroupSizeX); |
| 84 | + uint32_t workgroup_count = utils::compute_1d_workgroup_count( |
| 85 | + device, out_meta.numel, wg_size, "select"); |
| 86 | + |
| 87 | + WGPUConstantEntry wg_size_constant = {}; |
| 88 | + wg_size_constant.key = {"wg_size", WGPU_STRLEN}; |
| 89 | + wg_size_constant.value = static_cast<double>(wg_size); |
| 90 | + |
| 91 | + WGPUBuffer out_meta_buf = |
| 92 | + utils::make_uniform(device, &out_meta, sizeof(TensorMeta)); |
| 93 | + WGPUBuffer in_meta_buf = |
| 94 | + utils::make_uniform(device, &in_meta, sizeof(TensorMeta)); |
| 95 | + WGPUBuffer params_buf = |
| 96 | + utils::make_uniform(device, ¶ms, sizeof(SelectParams)); |
| 97 | + graph.add_uniform_buffer_bytes(2 * sizeof(TensorMeta) + sizeof(SelectParams)); |
| 98 | + |
| 99 | + WGPUShaderSourceWGSL wgsl_desc = {}; |
| 100 | + wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL; |
| 101 | + wgsl_desc.code = {kSelectWGSL, WGPU_STRLEN}; |
| 102 | + WGPUShaderModuleDescriptor shader_desc = {}; |
| 103 | + shader_desc.nextInChain = &wgsl_desc.chain; |
| 104 | + WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc); |
| 105 | + |
| 106 | + // Bind group: in, out (rw), out_meta, in_meta, params (3 uniforms). |
| 107 | + WGPUBindGroupLayoutEntry entries[5] = {}; |
| 108 | + entries[0].binding = 0; |
| 109 | + entries[0].visibility = WGPUShaderStage_Compute; |
| 110 | + entries[0].buffer.type = WGPUBufferBindingType_ReadOnlyStorage; |
| 111 | + entries[1].binding = 1; |
| 112 | + entries[1].visibility = WGPUShaderStage_Compute; |
| 113 | + entries[1].buffer.type = WGPUBufferBindingType_Storage; |
| 114 | + entries[2].binding = 2; |
| 115 | + entries[2].visibility = WGPUShaderStage_Compute; |
| 116 | + entries[2].buffer.type = WGPUBufferBindingType_Uniform; |
| 117 | + entries[3].binding = 3; |
| 118 | + entries[3].visibility = WGPUShaderStage_Compute; |
| 119 | + entries[3].buffer.type = WGPUBufferBindingType_Uniform; |
| 120 | + entries[4].binding = 4; |
| 121 | + entries[4].visibility = WGPUShaderStage_Compute; |
| 122 | + entries[4].buffer.type = WGPUBufferBindingType_Uniform; |
| 123 | + |
| 124 | + WGPUBindGroupLayoutDescriptor bgl_desc = {}; |
| 125 | + bgl_desc.entryCount = 5; |
| 126 | + bgl_desc.entries = entries; |
| 127 | + WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc); |
| 128 | + |
| 129 | + WGPUPipelineLayoutDescriptor pl_desc = {}; |
| 130 | + pl_desc.bindGroupLayoutCount = 1; |
| 131 | + pl_desc.bindGroupLayouts = &bgl; |
| 132 | + WGPUPipelineLayout pipeline_layout = |
| 133 | + wgpuDeviceCreatePipelineLayout(device, &pl_desc); |
| 134 | + |
| 135 | + WGPUComputePipelineDescriptor pipeline_desc = {}; |
| 136 | + pipeline_desc.layout = pipeline_layout; |
| 137 | + pipeline_desc.compute.module = shader; |
| 138 | + pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN}; |
| 139 | + pipeline_desc.compute.constantCount = 1; |
| 140 | + pipeline_desc.compute.constants = &wg_size_constant; |
| 141 | + WGPUComputePipeline pipeline = |
| 142 | + wgpuDeviceCreateComputePipeline(device, &pipeline_desc); |
| 143 | + |
| 144 | + WGPUBindGroupEntry bg_entries[5] = {}; |
| 145 | + bg_entries[0].binding = 0; |
| 146 | + bg_entries[0].buffer = in_tensor.buffer; |
| 147 | + bg_entries[0].size = in_tensor.nbytes; |
| 148 | + bg_entries[1].binding = 1; |
| 149 | + bg_entries[1].buffer = out_tensor.buffer; |
| 150 | + bg_entries[1].size = out_tensor.nbytes; |
| 151 | + bg_entries[2].binding = 2; |
| 152 | + bg_entries[2].buffer = out_meta_buf; |
| 153 | + bg_entries[2].size = sizeof(TensorMeta); |
| 154 | + bg_entries[3].binding = 3; |
| 155 | + bg_entries[3].buffer = in_meta_buf; |
| 156 | + bg_entries[3].size = sizeof(TensorMeta); |
| 157 | + bg_entries[4].binding = 4; |
| 158 | + bg_entries[4].buffer = params_buf; |
| 159 | + bg_entries[4].size = sizeof(SelectParams); |
| 160 | + |
| 161 | + WGPUBindGroupDescriptor bg_desc = {}; |
| 162 | + bg_desc.layout = bgl; |
| 163 | + bg_desc.entryCount = 5; |
| 164 | + bg_desc.entries = bg_entries; |
| 165 | + WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc); |
| 166 | + |
| 167 | + graph.add_dispatch({pipeline, bind_group, workgroup_count}); |
| 168 | + |
| 169 | + wgpuShaderModuleRelease(shader); |
| 170 | + wgpuBindGroupLayoutRelease(bgl); |
| 171 | + wgpuPipelineLayoutRelease(pipeline_layout); |
| 172 | + // Drop our refs; the bind group keeps the uniforms alive until release. |
| 173 | + wgpuBufferRelease(out_meta_buf); |
| 174 | + wgpuBufferRelease(in_meta_buf); |
| 175 | + wgpuBufferRelease(params_buf); |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace |
| 179 | + |
| 180 | +WEBGPU_REGISTER_OPERATORS { |
| 181 | + WEBGPU_REGISTER_OP(aten.select_copy.int, select_impl); |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace executorch::backends::webgpu |
0 commit comments