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 "tanh_vulkan.h"
17
#include "layer_shader_type.h"
21
TanH_vulkan::TanH_vulkan()
23
support_vulkan = true;
24
support_image_storage = true;
27
pipeline_tanh_pack4 = 0;
28
pipeline_tanh_pack8 = 0;
31
int TanH_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 || shape.dims == 4) 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);
58
if (shape.dims == 4) shape_packed = Mat(shape.w, shape.h, shape.d, shape.c / elempack, (void*)0, elemsize, elempack);
60
std::vector<vk_specialization_type> specializations(0 + 5);
61
specializations[0 + 0].i = shape_packed.dims;
62
specializations[0 + 1].i = shape_packed.w;
63
specializations[0 + 2].i = shape_packed.h * shape_packed.d;
64
specializations[0 + 3].i = shape_packed.c;
65
specializations[0 + 4].i = shape_packed.cstep;
68
if (shape_packed.dims == 1)
70
local_size_xyz.w = std::min(64, shape_packed.w);
74
if (shape_packed.dims == 2)
76
local_size_xyz.w = std::min(8, shape_packed.w);
77
local_size_xyz.h = std::min(8, shape_packed.h);
80
if (shape_packed.dims == 3)
82
local_size_xyz.w = std::min(4, shape_packed.w);
83
local_size_xyz.h = std::min(4, shape_packed.h);
84
local_size_xyz.c = std::min(4, shape_packed.c);
86
if (shape_packed.dims == 4)
88
local_size_xyz.w = std::min(4, shape_packed.w);
89
local_size_xyz.h = std::min(4, shape_packed.h * shape_packed.d);
90
local_size_xyz.c = std::min(4, shape_packed.c);
94
if (shape.dims == 0 || elempack == 1)
96
pipeline_tanh = new Pipeline(vkdev);
97
pipeline_tanh->set_optimal_local_size_xyz(local_size_xyz);
98
pipeline_tanh->create(LayerShaderType::tanh, opt, specializations);
102
if (shape.dims == 0 || elempack == 4)
104
pipeline_tanh_pack4 = new Pipeline(vkdev);
105
pipeline_tanh_pack4->set_optimal_local_size_xyz(local_size_xyz);
106
pipeline_tanh_pack4->create(LayerShaderType::tanh_pack4, opt, specializations);
110
if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
112
pipeline_tanh_pack8 = new Pipeline(vkdev);
113
pipeline_tanh_pack8->set_optimal_local_size_xyz(local_size_xyz);
114
pipeline_tanh_pack8->create(LayerShaderType::tanh_pack8, opt, specializations);
120
int TanH_vulkan::destroy_pipeline(const Option& /*opt*/)
122
delete pipeline_tanh;
125
delete pipeline_tanh_pack4;
126
pipeline_tanh_pack4 = 0;
128
delete pipeline_tanh_pack8;
129
pipeline_tanh_pack8 = 0;
134
int TanH_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
136
int elempack = bottom_top_blob.elempack;
138
std::vector<VkMat> bindings(1);
139
bindings[0] = bottom_top_blob;
141
std::vector<vk_constant_type> constants(5);
142
constants[0].i = bottom_top_blob.dims;
143
constants[1].i = bottom_top_blob.w;
144
constants[2].i = bottom_top_blob.h * bottom_top_blob.d;
145
constants[3].i = bottom_top_blob.c;
146
constants[4].i = bottom_top_blob.cstep;
148
const Pipeline* pipeline = elempack == 8 ? pipeline_tanh_pack8
149
: elempack == 4 ? pipeline_tanh_pack4
152
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
157
int TanH_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
159
int elempack = bottom_top_blob.elempack;
161
std::vector<VkImageMat> bindings(2);
162
bindings[0] = bottom_top_blob;
163
bindings[1] = bottom_top_blob;
165
std::vector<vk_constant_type> constants(5);
166
constants[0].i = bottom_top_blob.dims;
167
constants[1].i = bottom_top_blob.w;
168
constants[2].i = bottom_top_blob.h * bottom_top_blob.d;
169
constants[3].i = bottom_top_blob.c;
170
constants[4].i = 0; //bottom_top_blob.cstep;
172
const Pipeline* pipeline = elempack == 8 ? pipeline_tanh_pack8
173
: elempack == 4 ? pipeline_tanh_pack4
176
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);