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