-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathrmsnorm_vulkan.cpp
More file actions
389 lines (333 loc) · 15.8 KB
/
Copy pathrmsnorm_vulkan.cpp
File metadata and controls
389 lines (333 loc) · 15.8 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2025 Futz12 <pchar.cn>
// SPDX-License-Identifier: BSD-3-Clause
#include "rmsnorm_vulkan.h"
#include "layer_shader_type.h"
namespace ncnn {
RMSNorm_vulkan::RMSNorm_vulkan()
{
support_vulkan = true;
support_vulkan_packing = true;
// pack1
pipeline_rmsnorm_square = 0;
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32 = 0;
pipeline_rmsnorm_reduce_sum4_fp32[0] = 0;
pipeline_rmsnorm_reduce_sum4_fp32[1] = 0;
pipeline_rmsnorm_reduce_mean = 0;
pipeline_rmsnorm_coeffs = 0;
pipeline_rmsnorm_norm = 0;
// pack4
pipeline_rmsnorm_square_pack4 = 0;
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32_pack4 = 0;
pipeline_rmsnorm_reduce_sum4_fp32_pack4[0] = 0;
pipeline_rmsnorm_reduce_sum4_fp32_pack4[1] = 0;
pipeline_rmsnorm_reduce_mean_pack4 = 0;
pipeline_rmsnorm_coeffs_pack4 = 0;
pipeline_rmsnorm_norm_pack4 = 0;
// subgroup
pipeline_rmsnorm_reduce_subgroup = 0;
pipeline_rmsnorm_reduce_subgroup_pack4 = 0;
}
int RMSNorm_vulkan::create_pipeline(const Option& opt)
{
// Reuse the LN reduce_sum4 / reduce_mean size and local workgroup configuration [from LN code].
// square: only compute x^2; coeffs: a = 1 / sqrt(rms + eps); norm: v = v * a; (if affine: v *= gamma)
{
pipeline_rmsnorm_square = new Pipeline(vkdev);
pipeline_rmsnorm_square->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_square->create(LayerShaderType::rmsnorm_square, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_square_pack4 = new Pipeline(vkdev);
pipeline_rmsnorm_square_pack4->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_square_pack4->create(LayerShaderType::rmsnorm_square_pack4, opt, std::vector<vk_specialization_type>());
}
{
// Same as LN's reduce_sum4 (first pass fp16->fp32, then iterative fp32 passes alternating between two pipelines) and reduce_mean (see LN).
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32 = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32->set_optimal_local_size_xyz(16, 4, 1);
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32->create(LayerShaderType::layernorm_reduce_sum4_fp16_to_fp32, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_reduce_sum4_fp32[0] = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_sum4_fp32[0]->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_reduce_sum4_fp32[0]->create(LayerShaderType::layernorm_reduce_sum4_fp32, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_reduce_sum4_fp32[1] = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_sum4_fp32[1]->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_reduce_sum4_fp32[1]->create(LayerShaderType::layernorm_reduce_sum4_fp32, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32_pack4 = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32_pack4->set_optimal_local_size_xyz(16, 4, 1);
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32_pack4->create(LayerShaderType::layernorm_reduce_sum4_fp16_to_fp32_pack4, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_reduce_sum4_fp32_pack4[0] = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_sum4_fp32_pack4[0]->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_reduce_sum4_fp32_pack4[0]->create(LayerShaderType::layernorm_reduce_sum4_fp32_pack4, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_reduce_sum4_fp32_pack4[1] = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_sum4_fp32_pack4[1]->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_reduce_sum4_fp32_pack4[1]->create(LayerShaderType::layernorm_reduce_sum4_fp32_pack4, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_reduce_mean = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_mean->set_optimal_local_size_xyz(1, 8, 8);
pipeline_rmsnorm_reduce_mean->create(LayerShaderType::layernorm_reduce_mean, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_reduce_mean_pack4 = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_mean_pack4->set_optimal_local_size_xyz(1, 8, 8);
pipeline_rmsnorm_reduce_mean_pack4->create(LayerShaderType::layernorm_reduce_mean_pack4, opt, std::vector<vk_specialization_type>());
}
{
// coeffs: only eps is used as a specialization constant
std::vector<vk_specialization_type> spec(1);
spec[0].f = eps;
pipeline_rmsnorm_coeffs = new Pipeline(vkdev);
pipeline_rmsnorm_coeffs->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_coeffs->create(LayerShaderType::rmsnorm_coeffs, opt, spec);
pipeline_rmsnorm_coeffs_pack4 = new Pipeline(vkdev);
pipeline_rmsnorm_coeffs_pack4->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_coeffs_pack4->create(LayerShaderType::rmsnorm_coeffs_pack4, opt, spec);
}
{
// norm: requires affine and affine_size (which determine the mapping from inner_id to group_id, identical to LayerNorm)
std::vector<vk_specialization_type> spec(2);
spec[0].i = affine;
spec[1].i = affine_size;
pipeline_rmsnorm_norm = new Pipeline(vkdev);
pipeline_rmsnorm_norm->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_norm->create(LayerShaderType::rmsnorm_norm, opt, spec);
pipeline_rmsnorm_norm_pack4 = new Pipeline(vkdev);
pipeline_rmsnorm_norm_pack4->set_optimal_local_size_xyz(8, 8, 1);
pipeline_rmsnorm_norm_pack4->create(LayerShaderType::rmsnorm_norm_pack4, opt, spec);
}
if (vkdev->info.support_subgroup_ops() & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT)
{
pipeline_rmsnorm_reduce_subgroup = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_subgroup->set_local_size_xyz(256, 1, 1);
pipeline_rmsnorm_reduce_subgroup->create(LayerShaderType::rmsnorm_reduce_subgroup, opt, std::vector<vk_specialization_type>());
pipeline_rmsnorm_reduce_subgroup_pack4 = new Pipeline(vkdev);
pipeline_rmsnorm_reduce_subgroup_pack4->set_local_size_xyz(256, 1, 1);
pipeline_rmsnorm_reduce_subgroup_pack4->create(LayerShaderType::rmsnorm_reduce_subgroup_pack4, opt, std::vector<vk_specialization_type>());
}
return 0;
}
int RMSNorm_vulkan::destroy_pipeline(const Option&)
{
// pack1
delete pipeline_rmsnorm_square;
delete pipeline_rmsnorm_reduce_sum4_fp16_to_fp32;
delete pipeline_rmsnorm_reduce_sum4_fp32[0];
delete pipeline_rmsnorm_reduce_sum4_fp32[1];
delete pipeline_rmsnorm_reduce_mean;
delete pipeline_rmsnorm_coeffs;
delete pipeline_rmsnorm_norm;
pipeline_rmsnorm_square = 0;
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32 = 0;
pipeline_rmsnorm_reduce_sum4_fp32[0] = 0;
pipeline_rmsnorm_reduce_sum4_fp32[1] = 0;
pipeline_rmsnorm_reduce_mean = 0;
pipeline_rmsnorm_coeffs = 0;
pipeline_rmsnorm_norm = 0;
// pack4
delete pipeline_rmsnorm_square_pack4;
delete pipeline_rmsnorm_reduce_sum4_fp16_to_fp32_pack4;
delete pipeline_rmsnorm_reduce_sum4_fp32_pack4[0];
delete pipeline_rmsnorm_reduce_sum4_fp32_pack4[1];
delete pipeline_rmsnorm_reduce_mean_pack4;
delete pipeline_rmsnorm_coeffs_pack4;
delete pipeline_rmsnorm_norm_pack4;
pipeline_rmsnorm_square_pack4 = 0;
pipeline_rmsnorm_reduce_sum4_fp16_to_fp32_pack4 = 0;
pipeline_rmsnorm_reduce_sum4_fp32_pack4[0] = 0;
pipeline_rmsnorm_reduce_sum4_fp32_pack4[1] = 0;
pipeline_rmsnorm_reduce_mean_pack4 = 0;
pipeline_rmsnorm_coeffs_pack4 = 0;
pipeline_rmsnorm_norm_pack4 = 0;
delete pipeline_rmsnorm_reduce_subgroup;
pipeline_rmsnorm_reduce_subgroup = 0;
delete pipeline_rmsnorm_reduce_subgroup_pack4;
pipeline_rmsnorm_reduce_subgroup_pack4 = 0;
return 0;
}
int RMSNorm_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
{
if (affine == 0) return 0;
cmd.record_upload(gamma_data, gamma_data_gpu, opt);
return 0;
}
int RMSNorm_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
{
// Same 1D unpacking logic and cstep/elempack handling as LayerNorm (see LN for reference).
const int dims = bottom_top_blob.dims;
const int w = dims == 1 ? bottom_top_blob.w * bottom_top_blob.elempack : bottom_top_blob.w;
const int h = bottom_top_blob.h * bottom_top_blob.d;
const int channels = bottom_top_blob.c;
const size_t elemsize = dims == 1 ? bottom_top_blob.elemsize * bottom_top_blob.elempack : bottom_top_blob.elemsize;
const int elempack = dims == 1 ? 1 : bottom_top_blob.elempack;
const size_t cstep = dims == 1 ? bottom_top_blob.cstep * bottom_top_blob.elempack : bottom_top_blob.cstep;
if (affine_size == 0)
return 0;
int group_size = 0;
int num_groups_per_channel = 0;
if (dims == 1)
{
group_size = w;
num_groups_per_channel = 1;
}
else if (dims == 2)
{
group_size = w;
num_groups_per_channel = h;
}
else
{
if (affine_size == w)
{
group_size = w;
num_groups_per_channel = h;
}
else if (dims == 4 && affine_size == w * bottom_top_blob.h)
{
group_size = affine_size;
num_groups_per_channel = bottom_top_blob.d;
}
else
{
group_size = w * h;
num_groups_per_channel = 1;
}
}
int num_groups_total = num_groups_per_channel * channels;
VkMat rms_workspace(num_groups_total, 4u * elempack, elempack, opt.workspace_vkallocator);
const Pipeline* pipeline_reduce_subgroup = elempack == 4 ? pipeline_rmsnorm_reduce_subgroup_pack4 : pipeline_rmsnorm_reduce_subgroup;
if (pipeline_reduce_subgroup)
{
std::vector<VkMat> bindings(2);
bindings[0] = bottom_top_blob;
bindings[1] = rms_workspace;
std::vector<vk_constant_type> constants(3);
constants[0].i = group_size;
constants[1].i = num_groups_per_channel;
constants[2].i = (int)cstep;
VkMat dispatcher;
dispatcher.w = 1;
dispatcher.h = num_groups_total;
dispatcher.c = 1;
cmd.record_pipeline(pipeline_reduce_subgroup, bindings, constants, dispatcher);
}
else
{
// 1) x -> x^2
VkMat square_workspace(w, h, channels, elemsize, elempack, opt.workspace_vkallocator);
{
std::vector<VkMat> bindings(2);
bindings[0] = bottom_top_blob;
bindings[1] = square_workspace;
std::vector<vk_constant_type> constants(4);
constants[0].i = w;
constants[1].i = h;
constants[2].i = channels;
constants[3].i = cstep;
const Pipeline* pipe_sq = elempack == 4 ? pipeline_rmsnorm_square_pack4 : pipeline_rmsnorm_square;
cmd.record_pipeline(pipe_sq, bindings, constants, square_workspace);
}
// 2) reduce sum4 (square) -> ... -> mean
{
int reduced_w = (group_size + 3) / 4;
VkMat sqsum_workspace;
sqsum_workspace.create(reduced_w, num_groups_per_channel, channels, 4u * elempack, elempack, opt.workspace_vkallocator);
{
std::vector<VkMat> bindings(2);
bindings[0] = square_workspace;
bindings[1] = sqsum_workspace;
std::vector<vk_constant_type> constants(8);
constants[0].i = group_size;
constants[1].i = num_groups_per_channel;
constants[2].i = channels;
constants[3].i = square_workspace.cstep;
constants[4].i = reduced_w;
constants[5].i = num_groups_per_channel;
constants[6].i = channels;
constants[7].i = sqsum_workspace.cstep;
VkMat dispatcher;
dispatcher.w = reduced_w;
dispatcher.h = num_groups_per_channel;
dispatcher.c = channels;
const Pipeline* p_reduce = elempack == 4 ? pipeline_rmsnorm_reduce_sum4_fp16_to_fp32_pack4
: pipeline_rmsnorm_reduce_sum4_fp16_to_fp32;
cmd.record_pipeline(p_reduce, bindings, constants, dispatcher);
}
int pb = 1;
while (sqsum_workspace.w > 1)
{
int current_w = sqsum_workspace.w;
reduced_w = (current_w + 3) / 4;
VkMat sqsum_reduced;
sqsum_reduced.create(reduced_w, num_groups_per_channel, channels, 4u * elempack, elempack, opt.workspace_vkallocator);
std::vector<VkMat> bindings(2);
bindings[0] = sqsum_workspace;
bindings[1] = sqsum_reduced;
std::vector<vk_constant_type> constants(8);
constants[0].i = current_w;
constants[1].i = num_groups_per_channel;
constants[2].i = channels;
constants[3].i = sqsum_workspace.cstep;
constants[4].i = reduced_w;
constants[5].i = num_groups_per_channel;
constants[6].i = channels;
constants[7].i = sqsum_reduced.cstep;
VkMat dispatcher;
dispatcher.w = reduced_w;
dispatcher.h = num_groups_per_channel;
dispatcher.c = channels;
const Pipeline* p_iter = elempack == 4 ? pipeline_rmsnorm_reduce_sum4_fp32_pack4[pb % 2]
: pipeline_rmsnorm_reduce_sum4_fp32[pb % 2];
cmd.record_pipeline(p_iter, bindings, constants, dispatcher);
pb++;
sqsum_workspace = sqsum_reduced;
}
{
std::vector<VkMat> bindings(2);
bindings[0] = sqsum_workspace;
bindings[1] = rms_workspace;
std::vector<vk_constant_type> constants(5);
constants[0].i = sqsum_workspace.w;
constants[1].i = num_groups_per_channel;
constants[2].i = channels;
constants[3].i = sqsum_workspace.cstep;
constants[4].f = (float)group_size;
VkMat dispatcher;
dispatcher.w = 1;
dispatcher.h = num_groups_per_channel;
dispatcher.c = channels;
const Pipeline* p_mean = elempack == 4 ? pipeline_rmsnorm_reduce_mean_pack4 : pipeline_rmsnorm_reduce_mean;
cmd.record_pipeline(p_mean, bindings, constants, dispatcher);
}
}
}
// 3) coeffs (a) from rms
VkMat coeffs_workspace(num_groups_total, elemsize, elempack, opt.workspace_vkallocator); // only a, no b
{
std::vector<VkMat> bindings(2);
bindings[0] = coeffs_workspace;
bindings[1] = rms_workspace;
std::vector<vk_constant_type> constants(2);
constants[0].i = num_groups_per_channel;
constants[1].i = channels;
VkMat dispatcher;
dispatcher.w = 1;
dispatcher.h = num_groups_per_channel;
dispatcher.c = channels;
const Pipeline* p_coeffs = elempack == 4 ? pipeline_rmsnorm_coeffs_pack4 : pipeline_rmsnorm_coeffs;
cmd.record_pipeline(p_coeffs, bindings, constants, dispatcher);
}
// 4) norm: v = v * a; (affine? v *= gamma)
{
std::vector<VkMat> bindings(3);
bindings[0] = bottom_top_blob;
bindings[1] = coeffs_workspace;
bindings[2] = gamma_data_gpu;
std::vector<vk_constant_type> constants(4);
constants[0].i = w;
constants[1].i = h;
constants[2].i = channels;
constants[3].i = cstep;
const Pipeline* p_norm = elempack == 4 ? pipeline_rmsnorm_norm_pack4 : pipeline_rmsnorm_norm;
VkMat dispatcher;
dispatcher.w = w;
dispatcher.h = h;
dispatcher.c = channels;
cmd.record_pipeline(p_norm, bindings, constants, dispatcher);
}
return 0;
}
} // namespace ncnn