|
| 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/quantized_linear/q4gsw_linear_wgsl.h> |
| 13 | + |
| 14 | +#include <webgpu/webgpu.h> |
| 15 | + |
| 16 | +#include <cstdint> |
| 17 | +#include <cstring> |
| 18 | +#include <stdexcept> |
| 19 | + |
| 20 | +namespace executorch::backends::webgpu { |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +// Uniform layout matching the WGSL Params struct (16-byte aligned, 32 bytes). |
| 25 | +struct Q4gswParams { |
| 26 | + uint32_t M; |
| 27 | + uint32_t N; |
| 28 | + uint32_t K; |
| 29 | + uint32_t K_packed; |
| 30 | + uint32_t group_size; |
| 31 | + uint32_t padded_N; |
| 32 | + uint32_t has_bias; |
| 33 | + uint32_t _pad; |
| 34 | +}; |
| 35 | +static_assert(sizeof(Q4gswParams) == 32, "Q4gswParams must be 32 bytes"); |
| 36 | + |
| 37 | +// et_vk.linear_q4gsw args: [in, weight, scales, group_size, bias, out]. |
| 38 | +void q4gsw_linear_impl(WebGPUGraph& graph, const std::vector<int>& args) { |
| 39 | + const int in_id = args.at(0); |
| 40 | + const int weight_id = args.at(1); |
| 41 | + const int scales_id = args.at(2); |
| 42 | + const int group_size_id = args.at(3); |
| 43 | + const int bias_id = args.at(4); |
| 44 | + const int out_id = args.at(5); |
| 45 | + |
| 46 | + WGPUDevice device = graph.device(); |
| 47 | + |
| 48 | + const auto& in = graph.get_tensor(in_id); |
| 49 | + const auto& weight = graph.get_tensor(weight_id); |
| 50 | + const auto& scales = graph.get_tensor(scales_id); |
| 51 | + const auto& out = graph.get_tensor(out_id); |
| 52 | + |
| 53 | + if (in.dims.empty() || weight.dims.size() < 2 || scales.dims.size() < 2) { |
| 54 | + throw std::runtime_error("WebGPU linear_q4gsw: malformed input dims"); |
| 55 | + } |
| 56 | + |
| 57 | + // Shapes from the tensors' own dims (no dtype field at runtime). |
| 58 | + const uint32_t K = static_cast<uint32_t>(in.dims.back()); |
| 59 | + if (K == 0) { |
| 60 | + throw std::runtime_error("WebGPU linear_q4gsw: K == 0"); |
| 61 | + } |
| 62 | + uint64_t in_numel = 1; |
| 63 | + for (int64_t d : in.dims) { |
| 64 | + in_numel *= static_cast<uint64_t>(d); |
| 65 | + } |
| 66 | + const uint32_t M = static_cast<uint32_t>(in_numel / K); |
| 67 | + const uint32_t N = static_cast<uint32_t>(weight.dims[0]); |
| 68 | + const uint32_t K_packed = static_cast<uint32_t>(weight.dims[1]); |
| 69 | + const uint32_t num_groups = static_cast<uint32_t>(scales.dims[0]); |
| 70 | + const uint32_t padded_N = static_cast<uint32_t>(scales.dims[1]); |
| 71 | + if (M == 0 || N == 0) { |
| 72 | + throw std::runtime_error("WebGPU linear_q4gsw: M or N == 0"); |
| 73 | + } |
| 74 | + // int4 packing is 2 nibbles/byte, so K_packed must be ceil(K/2) (guards OOB). |
| 75 | + if (K_packed != (K + 1) / 2) { |
| 76 | + throw std::runtime_error("WebGPU linear_q4gsw: K_packed must be ceil(K/2)"); |
| 77 | + } |
| 78 | + |
| 79 | + // One workgroup per output row (M); validate dispatch before any alloc. |
| 80 | + const uint32_t workgroup_count = |
| 81 | + utils::compute_1d_workgroup_count(device, M, 1, "linear_q4gsw"); |
| 82 | + |
| 83 | + // fp32-only byte-size guards (no runtime dtype); fp16 scales -> bail. |
| 84 | + const uint64_t scales_numel = |
| 85 | + static_cast<uint64_t>(num_groups) * static_cast<uint64_t>(padded_N); |
| 86 | + const uint64_t weight_numel = |
| 87 | + static_cast<uint64_t>(N) * static_cast<uint64_t>(K_packed); |
| 88 | + if (in.nbytes != in_numel * sizeof(float) || |
| 89 | + out.nbytes != static_cast<uint64_t>(M) * N * sizeof(float) || |
| 90 | + scales.nbytes != scales_numel * sizeof(float) || |
| 91 | + weight.nbytes != weight_numel) { |
| 92 | + throw std::runtime_error( |
| 93 | + "WebGPU linear_q4gsw: fp32-only (byte-size mismatch)"); |
| 94 | + } |
| 95 | + |
| 96 | + int64_t group_size = 0; |
| 97 | + if (graph.get_value_type(group_size_id) == WebGPUGraph::ValueType::Int) { |
| 98 | + group_size = graph.get_int(group_size_id); |
| 99 | + } |
| 100 | + if (group_size <= 0) { |
| 101 | + throw std::runtime_error("WebGPU linear_q4gsw: group_size <= 0"); |
| 102 | + } |
| 103 | + |
| 104 | + // Optional bias: real buffer if present, else a dummy for the fixed layout. |
| 105 | + uint32_t has_bias = 0; |
| 106 | + WGPUBuffer bias_buffer = nullptr; |
| 107 | + uint64_t bias_size = 4; |
| 108 | + if (graph.get_value_type(bias_id) == WebGPUGraph::ValueType::Tensor) { |
| 109 | + const auto& bias = graph.get_tensor(bias_id); |
| 110 | + if (bias.buffer == nullptr || bias.nbytes < N * sizeof(float)) { |
| 111 | + throw std::runtime_error( |
| 112 | + "WebGPU linear_q4gsw: bias present but null/undersized"); |
| 113 | + } |
| 114 | + has_bias = 1; |
| 115 | + bias_buffer = bias.buffer; |
| 116 | + bias_size = bias.nbytes; |
| 117 | + } |
| 118 | + if (bias_buffer == nullptr) { |
| 119 | + bias_buffer = graph.create_scratch_buffer(4); |
| 120 | + bias_size = 4; |
| 121 | + } |
| 122 | + |
| 123 | + Q4gswParams params = {}; |
| 124 | + params.M = M; |
| 125 | + params.N = N; |
| 126 | + params.K = K; |
| 127 | + params.K_packed = K_packed; |
| 128 | + params.group_size = static_cast<uint32_t>(group_size); |
| 129 | + params.padded_N = padded_N; |
| 130 | + params.has_bias = has_bias; |
| 131 | + |
| 132 | + WGPUBufferDescriptor uniform_desc = {}; |
| 133 | + uniform_desc.size = sizeof(Q4gswParams); |
| 134 | + uniform_desc.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst; |
| 135 | + uniform_desc.mappedAtCreation = true; |
| 136 | + WGPUBuffer uniform_buffer = wgpuDeviceCreateBuffer(device, &uniform_desc); |
| 137 | + void* mapped = |
| 138 | + wgpuBufferGetMappedRange(uniform_buffer, 0, sizeof(Q4gswParams)); |
| 139 | + std::memcpy(mapped, ¶ms, sizeof(Q4gswParams)); |
| 140 | + wgpuBufferUnmap(uniform_buffer); |
| 141 | + graph.add_uniform_buffer_bytes(sizeof(Q4gswParams)); |
| 142 | + |
| 143 | + WGPUShaderSourceWGSL wgsl_desc = {}; |
| 144 | + wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL; |
| 145 | + wgsl_desc.code = {kQ4gswLinearWGSL, WGPU_STRLEN}; |
| 146 | + WGPUShaderModuleDescriptor shader_desc = {}; |
| 147 | + shader_desc.nextInChain = &wgsl_desc.chain; |
| 148 | + WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc); |
| 149 | + |
| 150 | + // Bind group layout: out (rw) + in/weight/scales/bias (ro storage) + uniform. |
| 151 | + WGPUBindGroupLayoutEntry entries[6] = {}; |
| 152 | + entries[0].binding = 0; |
| 153 | + entries[0].visibility = WGPUShaderStage_Compute; |
| 154 | + entries[0].buffer.type = WGPUBufferBindingType_Storage; |
| 155 | + for (uint32_t i = 1; i <= 4; i++) { |
| 156 | + entries[i].binding = i; |
| 157 | + entries[i].visibility = WGPUShaderStage_Compute; |
| 158 | + entries[i].buffer.type = WGPUBufferBindingType_ReadOnlyStorage; |
| 159 | + } |
| 160 | + entries[5].binding = 5; |
| 161 | + entries[5].visibility = WGPUShaderStage_Compute; |
| 162 | + entries[5].buffer.type = WGPUBufferBindingType_Uniform; |
| 163 | + |
| 164 | + WGPUBindGroupLayoutDescriptor bgl_desc = {}; |
| 165 | + bgl_desc.entryCount = 6; |
| 166 | + bgl_desc.entries = entries; |
| 167 | + WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc); |
| 168 | + |
| 169 | + WGPUPipelineLayoutDescriptor pl_desc = {}; |
| 170 | + pl_desc.bindGroupLayoutCount = 1; |
| 171 | + pl_desc.bindGroupLayouts = &bgl; |
| 172 | + WGPUPipelineLayout pipeline_layout = |
| 173 | + wgpuDeviceCreatePipelineLayout(device, &pl_desc); |
| 174 | + |
| 175 | + const uint32_t wg_size = |
| 176 | + utils::clamp_workgroup_size(device, kQ4gswLinearWorkgroupSizeX); |
| 177 | + WGPUConstantEntry wg_size_constant = {}; |
| 178 | + wg_size_constant.key = {"wg_size", WGPU_STRLEN}; |
| 179 | + wg_size_constant.value = static_cast<double>(wg_size); |
| 180 | + |
| 181 | + WGPUComputePipelineDescriptor pipeline_desc = {}; |
| 182 | + pipeline_desc.layout = pipeline_layout; |
| 183 | + pipeline_desc.compute.module = shader; |
| 184 | + pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN}; |
| 185 | + pipeline_desc.compute.constantCount = 1; |
| 186 | + pipeline_desc.compute.constants = &wg_size_constant; |
| 187 | + WGPUComputePipeline pipeline = |
| 188 | + wgpuDeviceCreateComputePipeline(device, &pipeline_desc); |
| 189 | + |
| 190 | + WGPUBindGroupEntry bg_entries[6] = {}; |
| 191 | + bg_entries[0].binding = 0; |
| 192 | + bg_entries[0].buffer = out.buffer; |
| 193 | + bg_entries[0].size = out.nbytes; |
| 194 | + bg_entries[1].binding = 1; |
| 195 | + bg_entries[1].buffer = in.buffer; |
| 196 | + bg_entries[1].size = in.nbytes; |
| 197 | + bg_entries[2].binding = 2; |
| 198 | + bg_entries[2].buffer = weight.buffer; |
| 199 | + bg_entries[2].size = weight.nbytes; |
| 200 | + bg_entries[3].binding = 3; |
| 201 | + bg_entries[3].buffer = scales.buffer; |
| 202 | + bg_entries[3].size = scales.nbytes; |
| 203 | + bg_entries[4].binding = 4; |
| 204 | + bg_entries[4].buffer = bias_buffer; |
| 205 | + bg_entries[4].size = bias_size; |
| 206 | + bg_entries[5].binding = 5; |
| 207 | + bg_entries[5].buffer = uniform_buffer; |
| 208 | + bg_entries[5].size = sizeof(Q4gswParams); |
| 209 | + |
| 210 | + WGPUBindGroupDescriptor bg_desc = {}; |
| 211 | + bg_desc.layout = bgl; |
| 212 | + bg_desc.entryCount = 6; |
| 213 | + bg_desc.entries = bg_entries; |
| 214 | + WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc); |
| 215 | + |
| 216 | + graph.add_dispatch({pipeline, bind_group, workgroup_count, "linear_q4gsw"}); |
| 217 | + |
| 218 | + wgpuShaderModuleRelease(shader); |
| 219 | + wgpuBindGroupLayoutRelease(bgl); |
| 220 | + wgpuPipelineLayoutRelease(pipeline_layout); |
| 221 | + wgpuBufferRelease(uniform_buffer); |
| 222 | +} |
| 223 | + |
| 224 | +} // namespace |
| 225 | + |
| 226 | +WEBGPU_REGISTER_OPERATORS { |
| 227 | + WEBGPU_REGISTER_OP(et_vk.linear_q4gsw.default, q4gsw_linear_impl); |
| 228 | +} |
| 229 | + |
| 230 | +} // namespace executorch::backends::webgpu |
0 commit comments