Elffuss BraidHub

kikocis/ncnn public

master · 6.5 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4//
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
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
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.
14
15#include "prelu_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21PReLU_vulkan::PReLU_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25
26 pipeline_prelu = 0;
27 pipeline_prelu_pack4 = 0;
28 pipeline_prelu_pack8 = 0;
29}
30
31int PReLU_vulkan::create_pipeline(const Option& opt)
32{
33 const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
34
35 int elempack = 1;
36 if (shape.dims == 0) elempack = opt.use_shader_pack8 && num_slope % 8 == 0 ? 8 : num_slope % 4 == 0 ? 4 : 1;
37 if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
38 if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
39 if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
40
41 size_t elemsize;
42 if (opt.use_fp16_storage)
43 {
44 elemsize = elempack * 2u;
45 }
46 else if (opt.use_fp16_packed)
47 {
48 elemsize = elempack == 1 ? 4u : elempack * 2u;
49 }
50 else
51 {
52 elemsize = elempack * 4u;
53 }
54
55 Mat shape_packed;
56 if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack);
57 if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack);
58 if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
59
60 std::vector<vk_specialization_type> specializations(2 + 5);
61 specializations[0].i = num_slope;
62 specializations[1].f = num_slope == 1 ? slope_data[0] : 1.f;
63 specializations[2 + 0].i = shape_packed.dims;
64 specializations[2 + 1].i = shape_packed.w;
65 specializations[2 + 2].i = shape_packed.h;
66 specializations[2 + 3].i = shape_packed.c;
67 specializations[2 + 4].i = shape_packed.cstep;
68
69 Mat local_size_xyz(4, 4, std::min(4, num_slope / elempack), (void*)0);
70 if (shape_packed.dims == 1)
71 {
72 local_size_xyz.w = std::min(64, shape_packed.w);
73 local_size_xyz.h = 1;
74 local_size_xyz.c = 1;
75 }
76 if (shape_packed.dims == 2)
77 {
78 local_size_xyz.w = std::min(8, shape_packed.w);
79 local_size_xyz.h = std::min(8, shape_packed.h);
80 local_size_xyz.c = 1;
81 }
82 if (shape_packed.dims == 3)
83 {
84 local_size_xyz.w = std::min(4, shape_packed.w);
85 local_size_xyz.h = std::min(4, shape_packed.h);
86 local_size_xyz.c = std::min(4, shape_packed.c);
87 }
88
89 // pack1
90 if (num_slope == 1 || elempack == 1)
91 {
92 pipeline_prelu = new Pipeline(vkdev);
93 pipeline_prelu->set_optimal_local_size_xyz(local_size_xyz);
94 pipeline_prelu->create(LayerShaderType::prelu, opt, specializations);
95 }
96
97 // pack4
98 if (num_slope == 1 || elempack == 4)
99 {
100 pipeline_prelu_pack4 = new Pipeline(vkdev);
101 pipeline_prelu_pack4->set_optimal_local_size_xyz(local_size_xyz);
102 pipeline_prelu_pack4->create(LayerShaderType::prelu_pack4, opt, specializations);
103 }
104
105 // pack8
106 if ((opt.use_shader_pack8 && num_slope == 1) || elempack == 8)
107 {
108 pipeline_prelu_pack8 = new Pipeline(vkdev);
109 pipeline_prelu_pack8->set_optimal_local_size_xyz(local_size_xyz);
110 pipeline_prelu_pack8->create(LayerShaderType::prelu_pack8, opt, specializations);
111 }
112
113 return 0;
114}
115
116int PReLU_vulkan::destroy_pipeline(const Option& /*opt*/)
117{
118 delete pipeline_prelu;
119 pipeline_prelu = 0;
120
121 delete pipeline_prelu_pack4;
122 pipeline_prelu_pack4 = 0;
123
124 delete pipeline_prelu_pack8;
125 pipeline_prelu_pack8 = 0;
126
127 return 0;
128}
129
130int PReLU_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
131{
132 if (num_slope > 1)
133 {
134 int elempack = opt.use_shader_pack8 && num_slope % 8 == 0 ? 8 : num_slope % 4 == 0 ? 4 : 1;
135
136 Mat slope_data_packed;
137 convert_packing(slope_data, slope_data_packed, elempack, opt);
138
139 if (opt.use_image_storage)
140 {
141 cmd.record_upload(slope_data_packed, slope_data_gpu_image, opt);
142 }
143 else
144 {
145 cmd.record_upload(slope_data_packed, slope_data_gpu, opt);
146 }
147
148 if (opt.lightmode)
149 {
150 slope_data.release();
151 }
152 }
153
154 return 0;
155}
156
157int PReLU_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
158{
159 int elempack = bottom_top_blob.elempack;
160
161 std::vector<VkMat> bindings(2);
162 bindings[0] = bottom_top_blob;
163 bindings[1] = slope_data_gpu;
164
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;
169 constants[3].i = bottom_top_blob.c;
170 constants[4].i = bottom_top_blob.cstep;
171
172 const Pipeline* pipeline = elempack == 8 ? pipeline_prelu_pack8
173 : elempack == 4 ? pipeline_prelu_pack4
174 : pipeline_prelu;
175
176 cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
177
178 return 0;
179}
180
181int PReLU_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
182{
183 int elempack = bottom_top_blob.elempack;
184
185 std::vector<VkImageMat> bindings(3);
186 bindings[0] = bottom_top_blob;
187 bindings[1] = bottom_top_blob;
188 bindings[2] = slope_data_gpu_image;
189
190 std::vector<vk_constant_type> constants(5);
191 constants[0].i = bottom_top_blob.dims;
192 constants[1].i = bottom_top_blob.w;
193 constants[2].i = bottom_top_blob.h;
194 constants[3].i = bottom_top_blob.c;
195 constants[4].i = 0; //bottom_top_blob.cstep;
196
197 const Pipeline* pipeline = elempack == 8 ? pipeline_prelu_pack8
198 : elempack == 4 ? pipeline_prelu_pack4
199 : pipeline_prelu;
200
201 cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
202
203 return 0;
204}
205
206} // namespace ncnn