|
| 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/mul/binary_mul_wgsl.h> |
| 14 | + |
| 15 | +#include <webgpu/webgpu.h> |
| 16 | + |
| 17 | +#include <stdexcept> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +namespace executorch::backends::webgpu { |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +void mul_impl(WebGPUGraph& graph, const std::vector<int>& args) { |
| 25 | + // aten.mul.Tensor args: [in1, in2, out] (self, other; no alpha) |
| 26 | + const int in1_id = args.at(0); |
| 27 | + const int in2_id = args.at(1); |
| 28 | + const int out_id = args.at(2); |
| 29 | + |
| 30 | + WGPUDevice device = graph.device(); |
| 31 | + |
| 32 | + const auto& in1_tensor = graph.get_tensor(in1_id); |
| 33 | + const auto& in2_tensor = graph.get_tensor(in2_id); |
| 34 | + const auto& out_tensor = graph.get_tensor(out_id); |
| 35 | + |
| 36 | + // Rank guard (NCHW backend is <= 4 dims; 1D dispatch only). |
| 37 | + if (out_tensor.dims.size() > kTensorMetaMaxNdim || |
| 38 | + in1_tensor.dims.size() > kTensorMetaMaxNdim || |
| 39 | + in2_tensor.dims.size() > kTensorMetaMaxNdim) { |
| 40 | + throw std::runtime_error("mul: tensor rank exceeds 4 (MAX_NDIM)"); |
| 41 | + } |
| 42 | + |
| 43 | + const uint32_t out_ndim = static_cast<uint32_t>(out_tensor.dims.size()); |
| 44 | + |
| 45 | + // 3 per-tensor meta uniforms (mirror Vulkan); inputs broadcast-aligned. |
| 46 | + TensorMeta out_meta; |
| 47 | + TensorMeta in1_meta; |
| 48 | + TensorMeta in2_meta; |
| 49 | + fill_tensor_meta_broadcast(out_tensor, out_ndim, &out_meta); |
| 50 | + fill_tensor_meta_broadcast(in1_tensor, out_ndim, &in1_meta); |
| 51 | + fill_tensor_meta_broadcast(in2_tensor, out_ndim, &in2_meta); |
| 52 | + |
| 53 | + // fp32-only: nbytes must equal numel * 4 for every operand. |
| 54 | + if (out_tensor.nbytes != |
| 55 | + static_cast<size_t>(out_meta.numel) * sizeof(float) || |
| 56 | + in1_tensor.nbytes != |
| 57 | + static_cast<size_t>(in1_meta.numel) * sizeof(float) || |
| 58 | + in2_tensor.nbytes != |
| 59 | + static_cast<size_t>(in2_meta.numel) * sizeof(float)) { |
| 60 | + throw std::runtime_error("mul: non-fp32 operand (nbytes != numel * 4)"); |
| 61 | + } |
| 62 | + |
| 63 | + uint32_t wg_size = |
| 64 | + utils::clamp_workgroup_size(device, kBinaryMulWorkgroupSizeX); |
| 65 | + uint32_t workgroup_count = |
| 66 | + utils::compute_1d_workgroup_count(device, out_meta.numel, wg_size, "mul"); |
| 67 | + |
| 68 | + WGPUConstantEntry wg_size_constant = {}; |
| 69 | + wg_size_constant.key = {"wg_size", WGPU_STRLEN}; |
| 70 | + wg_size_constant.value = static_cast<double>(wg_size); |
| 71 | + |
| 72 | + WGPUBuffer out_meta_buf = |
| 73 | + utils::make_uniform(device, &out_meta, sizeof(TensorMeta)); |
| 74 | + WGPUBuffer in1_meta_buf = |
| 75 | + utils::make_uniform(device, &in1_meta, sizeof(TensorMeta)); |
| 76 | + WGPUBuffer in2_meta_buf = |
| 77 | + utils::make_uniform(device, &in2_meta, sizeof(TensorMeta)); |
| 78 | + graph.add_uniform_buffer_bytes(3 * sizeof(TensorMeta)); |
| 79 | + |
| 80 | + WGPUShaderSourceWGSL wgsl_desc = {}; |
| 81 | + wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL; |
| 82 | + wgsl_desc.code = {kBinaryMulWGSL, WGPU_STRLEN}; |
| 83 | + |
| 84 | + WGPUShaderModuleDescriptor shader_desc = {}; |
| 85 | + shader_desc.nextInChain = &wgsl_desc.chain; |
| 86 | + WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc); |
| 87 | + |
| 88 | + // Bind group: in1, in2, out (rw), out_meta, in1_meta, in2_meta (3 uniforms). |
| 89 | + WGPUBindGroupLayoutEntry entries[6] = {}; |
| 90 | + |
| 91 | + entries[0].binding = 0; |
| 92 | + entries[0].visibility = WGPUShaderStage_Compute; |
| 93 | + entries[0].buffer.type = WGPUBufferBindingType_ReadOnlyStorage; |
| 94 | + |
| 95 | + entries[1].binding = 1; |
| 96 | + entries[1].visibility = WGPUShaderStage_Compute; |
| 97 | + entries[1].buffer.type = WGPUBufferBindingType_ReadOnlyStorage; |
| 98 | + |
| 99 | + entries[2].binding = 2; |
| 100 | + entries[2].visibility = WGPUShaderStage_Compute; |
| 101 | + entries[2].buffer.type = WGPUBufferBindingType_Storage; |
| 102 | + |
| 103 | + entries[3].binding = 3; |
| 104 | + entries[3].visibility = WGPUShaderStage_Compute; |
| 105 | + entries[3].buffer.type = WGPUBufferBindingType_Uniform; |
| 106 | + |
| 107 | + entries[4].binding = 4; |
| 108 | + entries[4].visibility = WGPUShaderStage_Compute; |
| 109 | + entries[4].buffer.type = WGPUBufferBindingType_Uniform; |
| 110 | + |
| 111 | + entries[5].binding = 5; |
| 112 | + entries[5].visibility = WGPUShaderStage_Compute; |
| 113 | + entries[5].buffer.type = WGPUBufferBindingType_Uniform; |
| 114 | + |
| 115 | + WGPUBindGroupLayoutDescriptor bgl_desc = {}; |
| 116 | + bgl_desc.entryCount = 6; |
| 117 | + bgl_desc.entries = entries; |
| 118 | + WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc); |
| 119 | + |
| 120 | + WGPUPipelineLayoutDescriptor pl_desc = {}; |
| 121 | + pl_desc.bindGroupLayoutCount = 1; |
| 122 | + pl_desc.bindGroupLayouts = &bgl; |
| 123 | + WGPUPipelineLayout pipeline_layout = |
| 124 | + wgpuDeviceCreatePipelineLayout(device, &pl_desc); |
| 125 | + |
| 126 | + WGPUComputePipelineDescriptor pipeline_desc = {}; |
| 127 | + pipeline_desc.layout = pipeline_layout; |
| 128 | + pipeline_desc.compute.module = shader; |
| 129 | + pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN}; |
| 130 | + pipeline_desc.compute.constantCount = 1; |
| 131 | + pipeline_desc.compute.constants = &wg_size_constant; |
| 132 | + WGPUComputePipeline pipeline = |
| 133 | + wgpuDeviceCreateComputePipeline(device, &pipeline_desc); |
| 134 | + |
| 135 | + WGPUBindGroupEntry bg_entries[6] = {}; |
| 136 | + |
| 137 | + bg_entries[0].binding = 0; |
| 138 | + bg_entries[0].buffer = in1_tensor.buffer; |
| 139 | + bg_entries[0].size = in1_tensor.nbytes; |
| 140 | + |
| 141 | + bg_entries[1].binding = 1; |
| 142 | + bg_entries[1].buffer = in2_tensor.buffer; |
| 143 | + bg_entries[1].size = in2_tensor.nbytes; |
| 144 | + |
| 145 | + bg_entries[2].binding = 2; |
| 146 | + bg_entries[2].buffer = out_tensor.buffer; |
| 147 | + bg_entries[2].size = out_tensor.nbytes; |
| 148 | + |
| 149 | + bg_entries[3].binding = 3; |
| 150 | + bg_entries[3].buffer = out_meta_buf; |
| 151 | + bg_entries[3].size = sizeof(TensorMeta); |
| 152 | + |
| 153 | + bg_entries[4].binding = 4; |
| 154 | + bg_entries[4].buffer = in1_meta_buf; |
| 155 | + bg_entries[4].size = sizeof(TensorMeta); |
| 156 | + |
| 157 | + bg_entries[5].binding = 5; |
| 158 | + bg_entries[5].buffer = in2_meta_buf; |
| 159 | + bg_entries[5].size = sizeof(TensorMeta); |
| 160 | + |
| 161 | + WGPUBindGroupDescriptor bg_desc = {}; |
| 162 | + bg_desc.layout = bgl; |
| 163 | + bg_desc.entryCount = 6; |
| 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(in1_meta_buf); |
| 175 | + wgpuBufferRelease(in2_meta_buf); |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace |
| 179 | + |
| 180 | +WEBGPU_REGISTER_OPERATORS { |
| 181 | + WEBGPU_REGISTER_OP(aten.mul.Tensor, mul_impl); |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace executorch::backends::webgpu |
0 commit comments