-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathQuantizedLinear.cpp
More file actions
258 lines (227 loc) · 9.24 KB
/
Copy pathQuantizedLinear.cpp
File metadata and controls
258 lines (227 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/backends/webgpu/runtime/WebGPUGraph.h>
#include <executorch/backends/webgpu/runtime/WebGPUUtils.h>
#include <executorch/backends/webgpu/runtime/ops/OperatorRegistry.h>
#include <executorch/backends/webgpu/runtime/ops/quantized_linear/q4gsw_linear_wgsl.h>
#include <webgpu/webgpu.h>
#include <cstdint>
#include <cstring>
#include <stdexcept>
namespace executorch::backends::webgpu {
namespace {
// Uniform layout matching the WGSL Params struct (16-byte aligned, 32 bytes).
struct Q4gswParams {
uint32_t M;
uint32_t N;
uint32_t K;
uint32_t K_packed;
uint32_t group_size;
uint32_t padded_N;
uint32_t has_bias;
uint32_t _pad;
};
static_assert(sizeof(Q4gswParams) == 32, "Q4gswParams must be 32 bytes");
// Register-tile dims; MUST match TM/TN in q4gsw_linear.wgsl.
constexpr int64_t kQ4gswTileM = 4;
constexpr int64_t kQ4gswTileN = 4;
// ceil(a/b) for positive int64 (WebGPUUtils has no ceil-div helper).
inline int64_t q4gsw_ceil_div(int64_t a, int64_t b) {
return (a + b - 1) / b;
}
// et_vk.linear_q4gsw args: [in, weight, scales, group_size, bias, out].
void q4gsw_linear_impl(WebGPUGraph& graph, const std::vector<int>& args) {
const int in_id = args.at(0);
const int weight_id = args.at(1);
const int scales_id = args.at(2);
const int group_size_id = args.at(3);
const int bias_id = args.at(4);
const int out_id = args.at(5);
WGPUDevice device = graph.device();
const auto& in = graph.get_tensor(in_id);
const auto& weight = graph.get_tensor(weight_id);
const auto& scales = graph.get_tensor(scales_id);
const auto& out = graph.get_tensor(out_id);
if (in.dims.empty() || weight.dims.size() < 2 || scales.dims.size() < 2) {
throw std::runtime_error("WebGPU linear_q4gsw: malformed input dims");
}
// Shapes from the tensors' own dims (no dtype field at runtime).
const uint32_t K = static_cast<uint32_t>(in.dims.back());
if (K == 0) {
throw std::runtime_error("WebGPU linear_q4gsw: K == 0");
}
uint64_t in_numel = 1;
for (int64_t d : in.dims) {
in_numel *= static_cast<uint64_t>(d);
}
const uint32_t M = static_cast<uint32_t>(in_numel / K);
if (in_numel % K != 0) {
throw std::runtime_error(
"WebGPU linear_q4gsw: input numel not a multiple of K");
}
const uint32_t N = static_cast<uint32_t>(weight.dims[0]);
const uint32_t K_packed = static_cast<uint32_t>(weight.dims[1]);
const uint32_t num_groups = static_cast<uint32_t>(scales.dims[0]);
const uint32_t padded_N = static_cast<uint32_t>(scales.dims[1]);
if (M == 0 || N == 0) {
throw std::runtime_error("WebGPU linear_q4gsw: M or N == 0");
}
// int4 packing is 2 nibbles/byte, so K_packed must be ceil(K/2) (guards OOB).
if (K_packed != (K + 1) / 2) {
throw std::runtime_error("WebGPU linear_q4gsw: K_packed must be ceil(K/2)");
}
// Weight is read as array<u32>; a non-multiple-of-4 byte count over-reads.
if ((static_cast<uint64_t>(N) * K_packed) % 4u != 0u) {
throw std::runtime_error(
"WebGPU linear_q4gsw: N*K_packed must be a multiple of 4 (u32-packed)");
}
// Register-tiled GEMM: one thread per TM x TN tile; validate before alloc.
const uint32_t wg_size =
utils::clamp_workgroup_size(device, kQ4gswLinearWorkgroupSizeX);
const int64_t total_tiles =
q4gsw_ceil_div(M, kQ4gswTileM) * q4gsw_ceil_div(N, kQ4gswTileN);
if (total_tiles > static_cast<int64_t>(UINT32_MAX)) {
throw std::runtime_error(
"WebGPU linear_q4gsw: tile count exceeds the 1D dispatch limit");
}
const uint32_t workgroup_count = utils::compute_1d_workgroup_count(
device, static_cast<uint32_t>(total_tiles), wg_size, "linear_q4gsw");
// fp32-only byte-size guards (no runtime dtype); fp16 scales -> bail.
const uint64_t scales_numel =
static_cast<uint64_t>(num_groups) * static_cast<uint64_t>(padded_N);
const uint64_t weight_numel =
static_cast<uint64_t>(N) * static_cast<uint64_t>(K_packed);
if (in.nbytes != in_numel * sizeof(float) ||
out.nbytes != static_cast<uint64_t>(M) * N * sizeof(float) ||
scales.nbytes != scales_numel * sizeof(float) ||
weight.nbytes != weight_numel) {
throw std::runtime_error(
"WebGPU linear_q4gsw: fp32-only (byte-size mismatch)");
}
int64_t group_size = 0;
if (graph.get_value_type(group_size_id) == WebGPUGraph::ValueType::Int) {
group_size = graph.get_int(group_size_id);
}
if (group_size <= 0) {
throw std::runtime_error("WebGPU linear_q4gsw: group_size <= 0");
}
// scales is indexed [(k/group_size)*padded_N + n]; guard the table bounds.
const uint32_t gs = static_cast<uint32_t>(group_size);
if (num_groups < (K + gs - 1u) / gs || padded_N < N) {
throw std::runtime_error(
"WebGPU linear_q4gsw: scales dims too small for K/N");
}
// Optional bias: real buffer if present, else a dummy for the fixed layout.
uint32_t has_bias = 0;
WGPUBuffer bias_buffer = nullptr;
uint64_t bias_size = 4;
if (graph.get_value_type(bias_id) == WebGPUGraph::ValueType::Tensor) {
const auto& bias = graph.get_tensor(bias_id);
if (bias.buffer == nullptr || bias.nbytes < N * sizeof(float)) {
throw std::runtime_error(
"WebGPU linear_q4gsw: bias present but null/undersized");
}
has_bias = 1;
bias_buffer = bias.buffer;
bias_size = bias.nbytes;
}
if (bias_buffer == nullptr) {
bias_buffer = graph.create_scratch_buffer(4);
}
Q4gswParams params = {};
params.M = M;
params.N = N;
params.K = K;
params.K_packed = K_packed;
params.group_size = gs;
params.padded_N = padded_N;
params.has_bias = has_bias;
WGPUBufferDescriptor uniform_desc = {};
uniform_desc.size = sizeof(Q4gswParams);
uniform_desc.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst;
uniform_desc.mappedAtCreation = true;
WGPUBuffer uniform_buffer = wgpuDeviceCreateBuffer(device, &uniform_desc);
void* mapped =
wgpuBufferGetMappedRange(uniform_buffer, 0, sizeof(Q4gswParams));
std::memcpy(mapped, ¶ms, sizeof(Q4gswParams));
wgpuBufferUnmap(uniform_buffer);
graph.add_uniform_buffer_bytes(sizeof(Q4gswParams));
WGPUShaderSourceWGSL wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
wgsl_desc.code = {kQ4gswLinearWGSL, WGPU_STRLEN};
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = &wgsl_desc.chain;
WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc);
// Bind group layout: out (rw) + in/weight/scales/bias (ro storage) + uniform.
WGPUBindGroupLayoutEntry entries[6] = {};
entries[0].binding = 0;
entries[0].visibility = WGPUShaderStage_Compute;
entries[0].buffer.type = WGPUBufferBindingType_Storage;
for (uint32_t i = 1; i <= 4; i++) {
entries[i].binding = i;
entries[i].visibility = WGPUShaderStage_Compute;
entries[i].buffer.type = WGPUBufferBindingType_ReadOnlyStorage;
}
entries[5].binding = 5;
entries[5].visibility = WGPUShaderStage_Compute;
entries[5].buffer.type = WGPUBufferBindingType_Uniform;
WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.entryCount = 6;
bgl_desc.entries = entries;
WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc);
WGPUPipelineLayoutDescriptor pl_desc = {};
pl_desc.bindGroupLayoutCount = 1;
pl_desc.bindGroupLayouts = &bgl;
WGPUPipelineLayout pipeline_layout =
wgpuDeviceCreatePipelineLayout(device, &pl_desc);
WGPUConstantEntry wg_size_constant = {};
wg_size_constant.key = {"wg_size", WGPU_STRLEN};
wg_size_constant.value = static_cast<double>(wg_size);
WGPUComputePipelineDescriptor pipeline_desc = {};
pipeline_desc.layout = pipeline_layout;
pipeline_desc.compute.module = shader;
pipeline_desc.compute.entryPoint = {"main", WGPU_STRLEN};
pipeline_desc.compute.constantCount = 1;
pipeline_desc.compute.constants = &wg_size_constant;
WGPUComputePipeline pipeline =
wgpuDeviceCreateComputePipeline(device, &pipeline_desc);
WGPUBindGroupEntry bg_entries[6] = {};
bg_entries[0].binding = 0;
bg_entries[0].buffer = out.buffer;
bg_entries[0].size = out.nbytes;
bg_entries[1].binding = 1;
bg_entries[1].buffer = in.buffer;
bg_entries[1].size = in.nbytes;
bg_entries[2].binding = 2;
bg_entries[2].buffer = weight.buffer;
bg_entries[2].size = weight.nbytes;
bg_entries[3].binding = 3;
bg_entries[3].buffer = scales.buffer;
bg_entries[3].size = scales.nbytes;
bg_entries[4].binding = 4;
bg_entries[4].buffer = bias_buffer;
bg_entries[4].size = bias_size;
bg_entries[5].binding = 5;
bg_entries[5].buffer = uniform_buffer;
bg_entries[5].size = sizeof(Q4gswParams);
WGPUBindGroupDescriptor bg_desc = {};
bg_desc.layout = bgl;
bg_desc.entryCount = 6;
bg_desc.entries = bg_entries;
WGPUBindGroup bind_group = wgpuDeviceCreateBindGroup(device, &bg_desc);
graph.add_dispatch({pipeline, bind_group, workgroup_count, "linear_q4gsw"});
wgpuShaderModuleRelease(shader);
wgpuBindGroupLayoutRelease(bgl);
wgpuPipelineLayoutRelease(pipeline_layout);
wgpuBufferRelease(uniform_buffer);
}
} // namespace
WEBGPU_REGISTER_OPERATORS {
WEBGPU_REGISTER_OP(et_vk.linear_q4gsw.default, q4gsw_linear_impl);
}
} // namespace executorch::backends::webgpu