1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6
// in compliance with the License. You may obtain a copy of the License at
8
// https://opensource.org/licenses/BSD-3-Clause
10
// Unless required by applicable law or agreed to in writing, software distributed
11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
// specific language governing permissions and limitations under the License.
15
#include "hardswish_vulkan.h"
17
#include "layer_shader_type.h"
21
HardSwish_vulkan::HardSwish_vulkan()
23
support_vulkan = true;
24
support_image_storage = true;
26
pipeline_hardswish = 0;
27
pipeline_hardswish_pack4 = 0;
28
pipeline_hardswish_pack8 = 0;
31
int HardSwish_vulkan::create_pipeline(const Option& opt)
33
const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
36
if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
37
if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
38
if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
41
if (opt.use_fp16_storage)
43
elemsize = elempack * 2u;
45
else if (opt.use_fp16_packed)
47
elemsize = elempack == 1 ? 4u : elempack * 2u;
51
elemsize = elempack * 4u;
55
if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack);
56
if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack);
57
if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
59
std::vector<vk_specialization_type> specializations(2 + 5);
60
specializations[0].f = alpha;
61
specializations[1].f = beta;
62
specializations[2 + 0].i = shape_packed.dims;
63
specializations[2 + 1].i = shape_packed.w;
64
specializations[2 + 2].i = shape_packed.h;
65
specializations[2 + 3].i = shape_packed.c;
66
specializations[2 + 4].i = shape_packed.cstep;
69
if (shape_packed.dims == 1)
71
local_size_xyz.w = std::min(64, shape_packed.w);
75
if (shape_packed.dims == 2)
77
local_size_xyz.w = std::min(8, shape_packed.w);
78
local_size_xyz.h = std::min(8, shape_packed.h);
81
if (shape_packed.dims == 3)
83
local_size_xyz.w = std::min(4, shape_packed.w);
84
local_size_xyz.h = std::min(4, shape_packed.h);
85
local_size_xyz.c = std::min(4, shape_packed.c);
89
if (shape.dims == 0 || elempack == 1)
91
pipeline_hardswish = new Pipeline(vkdev);
92
pipeline_hardswish->set_optimal_local_size_xyz(local_size_xyz);
93
pipeline_hardswish->create(LayerShaderType::hardswish, opt, specializations);
97
if (shape.dims == 0 || elempack == 4)
99
pipeline_hardswish_pack4 = new Pipeline(vkdev);
100
pipeline_hardswish_pack4->set_optimal_local_size_xyz(local_size_xyz);
101
pipeline_hardswish_pack4->create(LayerShaderType::hardswish_pack4, opt, specializations);
105
if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
107
pipeline_hardswish_pack8 = new Pipeline(vkdev);
108
pipeline_hardswish_pack8->set_optimal_local_size_xyz(local_size_xyz);
109
pipeline_hardswish_pack8->create(LayerShaderType::hardswish_pack8, opt, specializations);
115
int HardSwish_vulkan::destroy_pipeline(const Option& /*opt*/)
117
delete pipeline_hardswish;
118
pipeline_hardswish = 0;
120
delete pipeline_hardswish_pack4;
121
pipeline_hardswish_pack4 = 0;
123
delete pipeline_hardswish_pack8;
124
pipeline_hardswish_pack8 = 0;
129
int HardSwish_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
131
int elempack = bottom_top_blob.elempack;
133
std::vector<VkMat> bindings(1);
134
bindings[0] = bottom_top_blob;
136
std::vector<vk_constant_type> constants(5);
137
constants[0].i = bottom_top_blob.dims;
138
constants[1].i = bottom_top_blob.w;
139
constants[2].i = bottom_top_blob.h;
140
constants[3].i = bottom_top_blob.c;
141
constants[4].i = bottom_top_blob.cstep;
143
const Pipeline* pipeline = elempack == 8 ? pipeline_hardswish_pack8
144
: elempack == 4 ? pipeline_hardswish_pack4
145
: pipeline_hardswish;
147
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
152
int HardSwish_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
154
int elempack = bottom_top_blob.elempack;
156
std::vector<VkImageMat> bindings(2);
157
bindings[0] = bottom_top_blob;
158
bindings[1] = bottom_top_blob;
160
std::vector<vk_constant_type> constants(5);
161
constants[0].i = bottom_top_blob.dims;
162
constants[1].i = bottom_top_blob.w;
163
constants[2].i = bottom_top_blob.h;
164
constants[3].i = bottom_top_blob.c;
165
constants[4].i = 0; //bottom_top_blob.cstep;
167
const Pipeline* pipeline = elempack == 8 ? pipeline_hardswish_pack8
168
: elempack == 4 ? pipeline_hardswish_pack4
169
: pipeline_hardswish;
171
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);