|
| 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/expand_copy/expand_copy_wgsl.h> |
| 14 | + |
| 15 | +#include <webgpu/webgpu.h> |
| 16 | + |
| 17 | +#include <stdexcept> |
| 18 | + |
| 19 | +namespace executorch::backends::webgpu { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +// out coord -> in coord; size-1 in-dims broadcast via clamp (mirrors mul). |
| 24 | +void expand_copy_impl(WebGPUGraph& graph, const std::vector<int>& args) { |
| 25 | + const int in_id = args.at(0); |
| 26 | + const int out_id = args.at(args.size() - 1); |
| 27 | + |
| 28 | + if (graph.get_value_type(in_id) != WebGPUGraph::ValueType::Tensor || |
| 29 | + graph.get_value_type(out_id) != WebGPUGraph::ValueType::Tensor) { |
| 30 | + throw std::runtime_error("expand_copy: in/out arg is not a tensor"); |
| 31 | + } |
| 32 | + |
| 33 | + WGPUDevice device = graph.device(); |
| 34 | + const auto& in_tensor = graph.get_tensor(in_id); |
| 35 | + const auto& out_tensor = graph.get_tensor(out_id); |
| 36 | + |
| 37 | + TensorMeta out_meta; |
| 38 | + TensorMeta in_meta; |
| 39 | + fill_tensor_meta(out_tensor, &out_meta); |
| 40 | + fill_tensor_meta_broadcast(in_tensor, out_meta.ndim, &in_meta); |
| 41 | + if (out_tensor.nbytes != |
| 42 | + static_cast<size_t>(out_meta.numel) * sizeof(float) || |
| 43 | + in_tensor.nbytes != static_cast<size_t>(in_meta.numel) * sizeof(float)) { |
| 44 | + throw std::runtime_error( |
| 45 | + "expand_copy: non-fp32 operand (nbytes != numel*4)"); |
| 46 | + } |
| 47 | + |
| 48 | + uint32_t wg_size = |
| 49 | + utils::clamp_workgroup_size(device, kExpandCopyWorkgroupSizeX); |
| 50 | + uint32_t workgroup_count = utils::compute_1d_workgroup_count( |
| 51 | + device, out_meta.numel, wg_size, "expand_copy"); |
| 52 | + |
| 53 | + WGPUConstantEntry wg_size_constant = {}; |
| 54 | + wg_size_constant.key = {"wg_size", WGPU_STRLEN}; |
| 55 | + wg_size_constant.value = static_cast<double>(wg_size); |
| 56 | + |
| 57 | + WGPUBuffer out_meta_buf = |
| 58 | + utils::make_uniform(device, &out_meta, sizeof(TensorMeta)); |
| 59 | + WGPUBuffer in_meta_buf = |
| 60 | + utils::make_uniform(device, &in_meta, sizeof(TensorMeta)); |
| 61 | + graph.add_uniform_buffer_bytes(2 * sizeof(TensorMeta)); |
| 62 | + |
| 63 | + WGPUShaderSourceWGSL wgsl_desc = {}; |
| 64 | + wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL; |
| 65 | + wgsl_desc.code = {kExpandCopyWGSL, WGPU_STRLEN}; |
| 66 | + WGPUShaderModuleDescriptor shader_desc = {}; |
| 67 | + shader_desc.nextInChain = &wgsl_desc.chain; |
| 68 | + WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc); |
| 69 | + |
| 70 | + WGPUBindGroupLayoutEntry entries[4] = {}; |
| 71 | + entries[0].binding = 0; |
| 72 | + entries[0].visibility = WGPUShaderStage_Compute; |
| 73 | + entries[0].buffer.type = WGPUBufferBindingType_ReadOnlyStorage; |
| 74 | + entries[1].binding = 1; |
| 75 | + entries[1].visibility = WGPUShaderStage_Compute; |
| 76 | + entries[1].buffer.type = WGPUBufferBindingType_Storage; |
| 77 | + entries[2].binding = 2; |
| 78 | + entries[2].visibility = WGPUShaderStage_Compute; |
| 79 | + entries[2].buffer.type = WGPUBufferBindingType_Uniform; |
| 80 | + entries[3].binding = 3; |
| 81 | + entries[3].visibility = WGPUShaderStage_Compute; |
| 82 | + entries[3].buffer.type = WGPUBufferBindingType_Uniform; |
| 83 | + |
| 84 | + WGPUBindGroupLayoutDescriptor bgl_desc = {}; |
| 85 | + bgl_desc.entryCount = 4; |
| 86 | + bgl_desc.entries = entries; |
| 87 | + WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc); |
| 88 | + |
| 89 | + WGPUPipelineLayoutDescriptor pl_desc = {}; |
| 90 | + pl_desc.bindGroupLayoutCount = 1; |
| 91 | + pl_desc.bindGroupLayouts = &bgl; |
| 92 | + WGPUPipelineLayout pipeline_layout = |
| 93 | + wgpuDeviceCreatePipelineLayout(device, &pl_desc); |
| 94 | + |
| 95 | + WGPUComputePipelineDescriptor pipeline_desc = {}; |
| 96 | + pipeline_desc.layout = pipeline_layout; |
| 97 | + pipeline_desc.compute.module = shader; |
| 98 | + pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN}; |
| 99 | + pipeline_desc.compute.constantCount = 1; |
| 100 | + pipeline_desc.compute.constants = &wg_size_constant; |
| 101 | + WGPUComputePipeline pipeline = |
| 102 | + wgpuDeviceCreateComputePipeline(device, &pipeline_desc); |
| 103 | + |
| 104 | + WGPUBindGroupEntry bg_entries[4] = {}; |
| 105 | + bg_entries[0].binding = 0; |
| 106 | + bg_entries[0].buffer = in_tensor.buffer; |
| 107 | + bg_entries[0].size = in_tensor.nbytes; |
| 108 | + bg_entries[1].binding = 1; |
| 109 | + bg_entries[1].buffer = out_tensor.buffer; |
| 110 | + bg_entries[1].size = out_tensor.nbytes; |
| 111 | + bg_entries[2].binding = 2; |
| 112 | + bg_entries[2].buffer = out_meta_buf; |
| 113 | + bg_entries[2].size = sizeof(TensorMeta); |
| 114 | + bg_entries[3].binding = 3; |
| 115 | + bg_entries[3].buffer = in_meta_buf; |
| 116 | + bg_entries[3].size = sizeof(TensorMeta); |
| 117 | + |
| 118 | + WGPUBindGroupDescriptor bg_desc = {}; |
| 119 | + bg_desc.layout = bgl; |
| 120 | + bg_desc.entryCount = 4; |
| 121 | + bg_desc.entries = bg_entries; |
| 122 | + WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc); |
| 123 | + |
| 124 | + graph.add_dispatch({pipeline, bind_group, workgroup_count}); |
| 125 | + |
| 126 | + wgpuShaderModuleRelease(shader); |
| 127 | + wgpuBindGroupLayoutRelease(bgl); |
| 128 | + wgpuPipelineLayoutRelease(pipeline_layout); |
| 129 | + wgpuBufferRelease(out_meta_buf); |
| 130 | + wgpuBufferRelease(in_meta_buf); |
| 131 | +} |
| 132 | + |
| 133 | +} // namespace |
| 134 | + |
| 135 | +WEBGPU_REGISTER_OPERATORS { |
| 136 | + WEBGPU_REGISTER_OP(aten.expand_copy.default, expand_copy_impl); |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace executorch::backends::webgpu |
0 commit comments