-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathConvolution.cpp
652 lines (579 loc) · 19.2 KB
/
Convolution.cpp
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
* 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/vulkan/runtime/graph/ops/OperatorRegistry.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Staging.h>
#include <executorch/backends/vulkan/runtime/graph/ops/utils/StagingUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/KernelUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h>
namespace vkcompute {
void resize_conv2d_node(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& extra_args) {
vTensorPtr out = graph->get_tensor(args[0].refs[0]);
vTensorPtr self = graph->get_tensor(args[1].refs[0]);
size_t ndim = self->sizes().size();
std::vector<int64_t> new_out_sizes(ndim);
const bool transposed = graph->get_bool(extra_args[4]);
// Batch, Channel
if (ndim == 4) {
new_out_sizes.at(ndim - 4) = self->sizes().at(ndim - 4);
}
TensorRefPtr weight_ref = graph->get_tref(extra_args[0]);
const auto& weight_sizes = weight_ref->sizes;
new_out_sizes.at(ndim - 3) =
transposed ? weight_sizes.at(ndim - 3) : weight_sizes.at(ndim - 4);
// Height, Width
const auto& new_out_sizes_hw = calc_out_sizes_hw(
*graph,
self->sizes(),
extra_args[0],
/*kernel_size_only = */ false,
{extra_args[1], extra_args[2], extra_args[3], extra_args[5]},
transposed);
new_out_sizes.at(ndim - 2) = new_out_sizes_hw.at(0);
new_out_sizes.at(ndim - 1) = new_out_sizes_hw.at(1);
out->virtual_resize(new_out_sizes);
}
void resize_conv1d_node(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& extra_args) {
vTensorPtr out = graph->get_tensor(args[0].refs[0]);
vTensorPtr self = graph->get_tensor(args[1].refs[0]);
TensorRefPtr weight_ref = graph->get_tref(extra_args[0]);
int64_t stride_size = graph->get_int_list(extra_args[1])->at(0);
int64_t padding_size = graph->get_int_list(extra_args[2])->at(0);
int64_t dilation_size = graph->get_int_list(extra_args[3])->at(0);
const std::vector<int64_t>& weight_sizes = weight_ref->sizes;
const std::vector<int64_t>& in_sizes = self->sizes();
size_t ndim = in_sizes.size();
std::vector<int64_t> new_out_sizes(ndim);
int64_t kernel_size = weight_sizes.at(2);
int64_t in_length = in_sizes.at(2);
new_out_sizes.at(0) = in_sizes.at(0);
new_out_sizes.at(1) = weight_sizes.at(0);
new_out_sizes.at(2) = calc_out_size(
in_length, kernel_size, stride_size, padding_size, dilation_size, false);
out->virtual_resize(new_out_sizes);
}
ValueRef prepack_biases(
ComputeGraph& graph,
const ValueRef vref,
const ValueRef weight,
const bool transposed,
const utils::StorageType storage_type,
const utils::GPUMemoryLayout memory_layout) {
auto sizes = graph.sizes_of(weight);
const int64_t out_channels = transposed ? sizes.at(1) : sizes.at(0);
ValueRef v = graph.add_tensor(
{out_channels}, graph.dtype_of(weight), storage_type, memory_layout);
vTensorPtr t = graph.get_tensor(v);
vkapi::ShaderInfo shader = get_nchw_to_tensor_shader(*t);
graph.prepack_nodes().emplace_back(new PrepackNode(
graph,
shader,
graph.create_global_wg_size(v),
graph.create_local_wg_size(v),
vref,
v,
{t->sizes_ubo()},
// Specialization constants
{t->hashed_layout()}));
return v;
}
enum class Conv2dMethod : uint8_t {
Depthwise,
Pointwise,
SlidingWindow,
Transposed,
};
vkapi::ShaderInfo get_conv2d_shader(
ComputeGraph& graph,
const api::vTensor& t_out,
const bool prepack_weights,
const Conv2dMethod method,
const ValueRef weight,
const bool clamp_out = false,
const bool stride_equals_dilation = false) {
std::string kernel_name;
kernel_name.reserve(kShaderNameReserve);
switch (method) {
case Conv2dMethod::Depthwise:
kernel_name = "conv2d_dw";
if (!prepack_weights) {
if (!stride_equals_dilation) {
kernel_name += "_sned";
}
const auto& weight_sizes = graph.get_tref(weight)->sizes;
if (weight_sizes.at(2) == 3 && weight_sizes.at(3) == 3) {
kernel_name += "_output_tile_3x3";
}
if (weight_sizes.at(2) == 5 && weight_sizes.at(3) == 5) {
kernel_name += "_output_tile_5x5";
}
}
break;
case Conv2dMethod::Pointwise:
if (prepack_weights) {
kernel_name = "conv2d";
} else {
kernel_name = "conv2d_pw";
}
break;
case Conv2dMethod::SlidingWindow:
kernel_name = "conv2d";
break;
case Conv2dMethod::Transposed:
kernel_name = "conv_transpose2d";
break;
}
if (prepack_weights) {
kernel_name += "_prepack_weights";
} else if (clamp_out) {
kernel_name += "_clamp";
}
add_dtype_suffix(kernel_name, t_out);
return VK_KERNEL_FROM_STR(kernel_name);
}
std::vector<int64_t> get_final_sizes(
const std::vector<int64_t>& original_sizes,
const Conv2dMethod method) {
int64_t batch_padded = utils::align_up_4(utils::val_at(-4, original_sizes));
int64_t channels_padded =
utils::align_up_4(utils::val_at(-3, original_sizes));
int64_t height = utils::val_at(-2, original_sizes);
int64_t width = utils::val_at(-1, original_sizes);
switch (method) {
case Conv2dMethod::Depthwise:
return std::vector<int64_t>{4, batch_padded / 4, height * width};
case Conv2dMethod::Pointwise:
case Conv2dMethod::SlidingWindow:
return std::vector<int64_t>{
4, batch_padded * height / 4, channels_padded * width};
case Conv2dMethod::Transposed:
return std::vector<int64_t>{
4, channels_padded * height / 4, batch_padded * width};
}
}
ValueRef prepack_weights(
ComputeGraph& graph,
const ValueRef vref,
const Conv2dMethod method) {
const auto original_sizes = graph.sizes_of(vref);
const auto final_sizes = get_final_sizes(original_sizes, method);
ValueRef v = graph.add_tensor(
final_sizes,
graph.dtype_of(vref),
utils::kTexture2D,
utils::kChannelsPacked);
vTensorPtr t = graph.get_tensor(v);
vkapi::ShaderInfo shader =
get_conv2d_shader(graph, *t, /*prepack_weights = */ true, method, vref);
graph.prepack_nodes().emplace_back(new PrepackNode(
graph,
shader,
graph.create_global_wg_size(v),
graph.create_local_wg_size(v),
vref,
v,
{t->sizes_ubo(),
graph.create_params_buffer(
utils::make_ivec4(original_sizes, /*reverse = */ true))},
// Specialization constants
{SV(t->packed_dim())}));
return v;
}
void check_conv_args(const api::vTensor& in, const api::vTensor& out) {
VK_CHECK_COND(check_packed_dim_is(in, WHCN::kChannelsDim));
VK_CHECK_COND(check_packed_dim_is(out, WHCN::kChannelsDim));
}
struct Conv2dParams final {
utils::ivec2 overlay_region;
int in_group_size;
};
struct OutputParams final {
float out_min;
float out_max;
};
Conv2dParams create_conv2d_params(
ComputeGraph& graph,
const ValueRef weight,
const Kernel2dParams& p,
const bool transposed) {
const auto& overlay_region = utils::make_ivec2({
p.kernel_size[0] + (p.kernel_size[0] - 1) * (p.dilation[0] - 1),
p.kernel_size[1] + (p.kernel_size[1] - 1) * (p.dilation[1] - 1),
});
const auto weight_sizes = graph.sizes_of(weight);
const int32_t in_group_size = utils::safe_downcast<int32_t>(
utils::align_up_4(transposed ? weight_sizes.at(0) : weight_sizes.at(1)));
return {overlay_region, in_group_size};
}
void check_conv2d_params(const Kernel2dParams& p, const bool transposed) {
if (transposed) {
if (p.dilation[0] > 1 || p.dilation[1] > 1) {
VK_THROW(
"aten.convolution.default: transposed = true, dilation > 1 is not supported yet!");
}
}
if ((p.padding[0] > 0 && p.kernel_size[0] > 1 && p.dilation[0] > 1) ||
(p.padding[1] > 0 && p.kernel_size[1] > 1 && p.dilation[1] > 1)) {
VK_THROW(
"aten.convolution.default: padding > 0 while dilation, kernel_size > 1 is not supported yet!");
}
}
Conv2dMethod get_conv2d_method(
ComputeGraph& graph,
const ValueRef weight,
const int64_t groups,
const bool transposed) {
const auto weight_sizes = graph.sizes_of(weight);
if (!transposed && weight_sizes.at(0) == groups && weight_sizes.at(1) == 1) {
return Conv2dMethod::Depthwise;
}
if (groups > 1) {
VK_THROW("aten.convolution.default: groups > 1 is not supported yet!");
}
if (transposed) {
return Conv2dMethod::Transposed;
}
if (weight_sizes.at(2) == 1 && weight_sizes.at(3) == 1) {
return Conv2dMethod::Pointwise;
}
return Conv2dMethod::SlidingWindow;
}
utils::uvec2 get_conv2d_dw_dispatch_divisor(
const std::vector<int64_t>& weight_sizes) {
if (weight_sizes.at(2) == 3 && weight_sizes.at(3) == 3) {
return {4u, 2u};
}
if (weight_sizes.at(2) == 5 && weight_sizes.at(3) == 5) {
return {4u, 2u};
}
return {4u, 2u};
}
utils::uvec3 create_conv2d_global_wg_size(
ComputeGraph& graph,
const Conv2dMethod method,
const ValueRef out,
const ValueRef weight_data,
const bool stride_equals_dilation) {
if (method == Conv2dMethod::Pointwise) {
const utils::uvec3 image_extents = graph.logical_limits_of(out);
return {
utils::div_up(image_extents[0u], 2u),
utils::div_up(image_extents[1u], 2u),
image_extents[2u]};
} else if (method == Conv2dMethod::Depthwise && stride_equals_dilation) {
const utils::uvec3 image_extents = graph.create_global_wg_size(out);
const utils::uvec2 div =
get_conv2d_dw_dispatch_divisor(graph.get_tref(weight_data)->sizes);
return {
utils::div_up(image_extents[0], div[0]),
utils::div_up(image_extents[1], div[1]),
image_extents[2]};
} else {
return graph.create_global_wg_size(out);
}
}
void add_conv2d_node(
ComputeGraph& graph,
const ValueRef in,
const ValueRef weight_data,
const ValueRef bias,
const ValueRef stride,
const ValueRef padding,
const ValueRef dilation,
const ValueRef transposed,
const ValueRef output_padding,
const ValueRef groups,
const ValueRef out_min,
const ValueRef out_max,
const ValueRef out,
const bool clamp_out) {
const bool transposed_val = graph.get_bool(transposed);
float out_min_val = 0.0f;
float out_max_val = 0.0f;
if (out_min != kDummyValueRef) {
out_min_val = graph.extract_scalar<float>(out_min);
}
if (out_max != kDummyValueRef) {
out_max_val = graph.extract_scalar<float>(out_max);
}
const int64_t groups_val = graph.get_int(groups);
const Conv2dMethod method =
get_conv2d_method(graph, weight_data, groups_val, transposed_val);
ValueRef arg_weight = prepack_weights(graph, weight_data, method);
ValueRef arg_bias = prepack_biases(
graph,
bias,
weight_data,
transposed_val,
/* storage_type = */ utils::kTexture2D,
/* memory_layout = */ utils::kWidthPacked);
vTensorPtr t_in = graph.get_tensor(in);
vTensorPtr t_out = graph.get_tensor(out);
if (t_in->sizes().at(0) > 1) {
VK_THROW("conv2d: input batch size > 1 is not supported yet!");
}
check_conv_args(*t_in, *t_out);
Kernel2dParams kernel_params = create_kernel2d_params(
graph,
weight_data,
/*kernel_size_only = */ false,
stride,
padding,
dilation);
Conv2dParams extra_params =
create_conv2d_params(graph, weight_data, kernel_params, transposed_val);
const bool stride_equals_dilation =
(kernel_params.stride[0] == kernel_params.dilation[0] &&
kernel_params.stride[1] == kernel_params.dilation[1]);
OutputParams out_params = {out_min_val, out_max_val};
check_conv2d_params(kernel_params, transposed_val);
vkapi::ShaderInfo shader = get_conv2d_shader(
graph,
*t_out,
/*prepack_weights = */ false,
method,
weight_data,
clamp_out,
stride_equals_dilation);
utils::uvec3 wg_size = create_conv2d_global_wg_size(
graph, method, out, weight_data, stride_equals_dilation);
if (method == Conv2dMethod::Pointwise || method == Conv2dMethod::Depthwise) {
wg_size = {wg_size[0] * wg_size[1] * wg_size[2], 1, 1};
}
if (method == Conv2dMethod::Pointwise) {
const utils::ivec4 kernel_param_size_stride = {
kernel_params.kernel_size[0],
kernel_params.kernel_size[1],
kernel_params.stride[0],
kernel_params.stride[1]};
const utils::ivec4 kernel_param_pad_dial = {
kernel_params.padding[0],
kernel_params.padding[1],
kernel_params.dilation[0],
kernel_params.dilation[1]};
graph.execute_nodes().emplace_back(new DispatchNode(
graph,
shader,
wg_size,
graph.create_local_wg_size(wg_size),
// Inputs and Outputs
{{out, vkapi::MemoryAccessType::WRITE},
{{in, arg_weight, arg_bias}, vkapi::MemoryAccessType::READ}},
// Shader params buffers
{},
// Specialization Constants
{},
// Resizing Logic
resize_conv2d_node,
{weight_data, stride, padding, dilation, transposed, output_padding},
{
graph.logical_limits_pc_of(out),
graph.sizes_pc_of(in),
PushConstantDataInfo(
&kernel_param_size_stride, sizeof(kernel_param_size_stride)),
PushConstantDataInfo(
&kernel_param_pad_dial, sizeof(kernel_param_pad_dial)),
PushConstantDataInfo(
&extra_params, sizeof(extra_params), sizeof(utils::ivec4)),
PushConstantDataInfo(&out_params, sizeof(out_params)),
}));
} else {
graph.execute_nodes().emplace_back(new DispatchNode(
graph,
shader,
wg_size,
graph.create_local_wg_size(wg_size),
// Inputs and Outputs
{{out, vkapi::MemoryAccessType::WRITE},
{{in, arg_weight, arg_bias}, vkapi::MemoryAccessType::READ}},
// Shader params buffers
{
t_out->logical_limits_ubo(),
t_in->sizes_ubo(),
graph.create_params_buffer(kernel_params),
graph.create_params_buffer(extra_params),
graph.create_params_buffer(out_params),
},
// Specialization Constants
{},
// Resizing Logic
resize_conv2d_node,
{weight_data, stride, padding, dilation, transposed, output_padding}));
}
}
void add_conv1d_node(
ComputeGraph& graph,
const ValueRef in,
const ValueRef weight,
const ValueRef bias,
const ValueRef stride,
const ValueRef padding,
const ValueRef dilation,
const ValueRef groups,
const ValueRef out_min,
const ValueRef out_max,
const ValueRef out,
const bool clamp_out) {
ValueRef arg_weight = prepack_standard(
graph, weight, graph.storage_type_of(out), utils::kChannelsPacked);
ValueRef arg_bias = prepack_biases(
graph,
bias,
weight,
/*transposed = */ false,
/*storage_type = */ utils::kTexture3D,
/*memory_layout = */ utils::kChannelsPacked);
float out_min_val = 0.0f;
float out_max_val = 0.0f;
if (out_min != kDummyValueRef) {
out_min_val = graph.extract_scalar<float>(out_min);
}
if (out_max != kDummyValueRef) {
out_max_val = graph.extract_scalar<float>(out_max);
}
vTensorPtr t_in = graph.get_tensor(in);
vTensorPtr t_weight = graph.get_tensor(arg_weight);
vTensorPtr t_bias = graph.get_tensor(arg_bias);
vTensorPtr t_out = graph.get_tensor(out);
const int64_t groups_val = graph.get_int(groups);
std::vector<int64_t> in_sizes = t_in->sizes();
std::vector<int64_t> weight_sizes = t_weight->sizes();
std::vector<int64_t> out_sizes = t_out->sizes();
check_conv_args(*t_in, *t_out);
int32_t in_channels = in_sizes.at(1);
int32_t out_channels = weight_sizes.at(0);
int32_t kernel_size = weight_sizes.at(2);
int32_t stride_size = graph.get_int_list(stride)->at(0);
int32_t padding_size = graph.get_int_list(padding)->at(0);
int32_t dilation_size = graph.get_int_list(dilation)->at(0);
int32_t in_group_size = static_cast<int64_t>(in_channels / groups_val);
int32_t out_group_size = static_cast<int64_t>(out_channels / groups_val);
utils::uvec3 global_size = {1, static_cast<uint32_t>(out_channels), 1};
utils::uvec3 local_size = {1, 64, 1};
Kernel1dParams kernel_params = {
kernel_size,
stride_size,
padding_size,
dilation_size,
in_group_size,
out_group_size};
OutputParams out_params = {out_min_val, out_max_val};
std::string kernel_name("conv1d");
if (clamp_out) {
kernel_name += "_clamp";
}
kernel_name.reserve(kShaderNameReserve);
add_dtype_suffix(kernel_name, *t_out);
graph.execute_nodes().emplace_back(new DispatchNode(
graph,
VK_KERNEL_FROM_STR(kernel_name),
global_size,
local_size,
// Inputs and Outputs
{{out, vkapi::MemoryAccessType::WRITE},
{{in, arg_weight, arg_bias}, vkapi::MemoryAccessType::READ}},
// Shader params buffers
{
t_out->logical_limits_ubo(),
t_in->sizes_ubo(),
graph.create_params_buffer(kernel_params),
graph.create_params_buffer(out_params),
},
// Specialization Constants
{t_out->hashed_layout(),
t_in->hashed_layout(),
t_weight->hashed_layout(),
t_bias->hashed_layout()},
// Resizing Logic
resize_conv1d_node,
{weight, stride, padding, dilation}));
}
void conv(ComputeGraph& graph, const std::vector<ValueRef>& args) {
int64_t in_ndim = graph.get_tensor(args[0])->sizes().size();
if (in_ndim == 4) {
if (args.size() == 10) {
// ordinary conv2d
return add_conv2d_node(
graph,
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[6],
args[7],
args[8],
/*out_min = */ kDummyValueRef,
/*out_max = */ kDummyValueRef,
args[9],
false);
} else {
// conv2d with clamp
return add_conv2d_node(
graph,
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[6],
args[7],
args[8],
args[9],
args[10],
args[11],
true);
}
} else {
if (args.size() == 10) {
// ordinary conv1d
return add_conv1d_node(
graph,
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[8],
/*out_min = */ kDummyValueRef,
/*out_max = */ kDummyValueRef,
args[9],
false);
} else {
// conv1d with clamp
return add_conv1d_node(
graph,
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[8],
args[9],
args[10],
args[11],
true);
}
}
}
REGISTER_OPERATORS {
VK_REGISTER_OP(aten.convolution.default, conv);
VK_REGISTER_OP(conv_with_clamp.default, conv);
VK_REGISTER_OP(et_vk.conv_with_clamp.default, conv);
}
} // namespace vkcompute