Elffuss BraidHub

kikocis/ncnn public

master · 9.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 "scale_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21Scale_vulkan::Scale_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25
26 pipeline_scale = 0;
27 pipeline_scale_pack4 = 0;
28 pipeline_scale_pack8 = 0;
29}
30
31int Scale_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 == 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;
39
40 size_t elemsize;
41 if (opt.use_fp16_storage)
42 {
43 elemsize = elempack * 2u;
44 }
45 else if (opt.use_fp16_packed)
46 {
47 elemsize = elempack == 1 ? 4u : elempack * 2u;
48 }
49 else
50 {
51 elemsize = elempack * 4u;
52 }
53
54 Mat shape_packed;
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
59 if (scale_data_size == -233)
60 {
61 std::vector<vk_specialization_type> specializations(1 + 5);
62 specializations[0].i = 0;
63 specializations[1 + 0].i = shape_packed.dims;
64 specializations[1 + 1].i = shape_packed.w;
65 specializations[1 + 2].i = shape_packed.h;
66 specializations[1 + 3].i = shape_packed.c;
67 specializations[1 + 4].i = shape_packed.cstep;
68
69 Mat local_size_xyz;
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 (shape.dims == 0 || elempack == 1)
91 {
92 pipeline_scale = new Pipeline(vkdev);
93 pipeline_scale->set_optimal_local_size_xyz(local_size_xyz);
94 pipeline_scale->create(LayerShaderType::scale, opt, specializations);
95 }
96
97 // pack4
98 if (shape.dims == 0 || elempack == 4)
99 {
100 pipeline_scale_pack4 = new Pipeline(vkdev);
101 pipeline_scale_pack4->set_optimal_local_size_xyz(local_size_xyz);
102 pipeline_scale_pack4->create(LayerShaderType::scale_pack4, opt, specializations);
103 }
104
105 // pack8
106 if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
107 {
108 pipeline_scale_pack8 = new Pipeline(vkdev);
109 pipeline_scale_pack8->set_optimal_local_size_xyz(local_size_xyz);
110 pipeline_scale_pack8->create(LayerShaderType::scale_pack8, opt, specializations);
111 }
112
113 return 0;
114 }
115
116 if (shape.dims == 0) elempack = opt.use_shader_pack8 && scale_data_size % 8 == 0 ? 8 : scale_data_size % 4 == 0 ? 4 : 1;
117
118 std::vector<vk_specialization_type> specializations(1 + 5);
119 specializations[0].i = bias_term;
120 specializations[1 + 0].i = shape_packed.dims;
121 specializations[1 + 1].i = shape_packed.w;
122 specializations[1 + 2].i = shape_packed.h;
123 specializations[1 + 3].i = shape_packed.c;
124 specializations[1 + 4].i = shape_packed.cstep;
125
126 Mat local_size_xyz(4, 4, std::min(4, scale_data_size / elempack), (void*)0);
127 if (shape_packed.dims == 1)
128 {
129 local_size_xyz.w = std::min(64, shape_packed.w);
130 local_size_xyz.h = 1;
131 local_size_xyz.c = 1;
132 }
133 if (shape_packed.dims == 2)
134 {
135 local_size_xyz.w = std::min(8, shape_packed.w);
136 local_size_xyz.h = std::min(8, shape_packed.h);
137 local_size_xyz.c = 1;
138 }
139 if (shape_packed.dims == 3)
140 {
141 local_size_xyz.w = std::min(4, shape_packed.w);
142 local_size_xyz.h = std::min(4, shape_packed.h);
143 local_size_xyz.c = std::min(4, shape_packed.c);
144 }
145
146 // pack1
147 if (elempack == 1)
148 {
149 pipeline_scale = new Pipeline(vkdev);
150 pipeline_scale->set_optimal_local_size_xyz(local_size_xyz);
151 pipeline_scale->create(LayerShaderType::scale, opt, specializations);
152 }
153
154 // pack4
155 if (elempack == 4)
156 {
157 pipeline_scale_pack4 = new Pipeline(vkdev);
158 pipeline_scale_pack4->set_optimal_local_size_xyz(local_size_xyz);
159 pipeline_scale_pack4->create(LayerShaderType::scale_pack4, opt, specializations);
160 }
161
162 // pack8
163 if (elempack == 8)
164 {
165 pipeline_scale_pack8 = new Pipeline(vkdev);
166 pipeline_scale_pack8->set_optimal_local_size_xyz(local_size_xyz);
167 pipeline_scale_pack8->create(LayerShaderType::scale_pack8, opt, specializations);
168 }
169
170 return 0;
171}
172
173int Scale_vulkan::destroy_pipeline(const Option& /*opt*/)
174{
175 delete pipeline_scale;
176 pipeline_scale = 0;
177
178 delete pipeline_scale_pack4;
179 pipeline_scale_pack4 = 0;
180
181 delete pipeline_scale_pack8;
182 pipeline_scale_pack8 = 0;
183
184 return 0;
185}
186
187int Scale_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
188{
189 if (scale_data_size == -233)
190 return 0;
191
192 int elempack = opt.use_shader_pack8 && scale_data_size % 8 == 0 ? 8 : scale_data_size % 4 == 0 ? 4 : 1;
193
194 Mat scale_data_packed;
195 convert_packing(scale_data, scale_data_packed, elempack, opt);
196
197 if (opt.use_image_storage)
198 {
199 cmd.record_upload(scale_data_packed, scale_data_gpu_image, opt);
200 }
201 else
202 {
203 cmd.record_upload(scale_data_packed, scale_data_gpu, opt);
204 }
205
206 if (bias_term)
207 {
208 Mat bias_data_packed;
209 convert_packing(bias_data, bias_data_packed, elempack, opt);
210
211 if (opt.use_image_storage)
212 {
213 cmd.record_upload(bias_data_packed, bias_data_gpu_image, opt);
214 }
215 else
216 {
217 cmd.record_upload(bias_data_packed, bias_data_gpu, opt);
218 }
219 }
220
221 if (opt.lightmode)
222 {
223 scale_data.release();
224 bias_data.release();
225 }
226
227 return 0;
228}
229
230int Scale_vulkan::forward_inplace(std::vector<VkMat>& bottom_top_blobs, VkCompute& cmd, const Option& /*opt*/) const
231{
232 VkMat& bottom_top_blob = bottom_top_blobs[0];
233 const VkMat& scale_blob = bottom_top_blobs[1];
234
235 int elempack = bottom_top_blob.elempack;
236
237 std::vector<VkMat> bindings(3);
238 bindings[0] = bottom_top_blob;
239 bindings[1] = scale_blob;
240 bindings[2] = bias_data_gpu;
241
242 std::vector<vk_constant_type> constants(5);
243 constants[0].i = bottom_top_blob.dims;
244 constants[1].i = bottom_top_blob.w;
245 constants[2].i = bottom_top_blob.h;
246 constants[3].i = bottom_top_blob.c;
247 constants[4].i = bottom_top_blob.cstep;
248
249 const Pipeline* pipeline = elempack == 8 ? pipeline_scale_pack8
250 : elempack == 4 ? pipeline_scale_pack4
251 : pipeline_scale;
252
253 cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
254
255 return 0;
256}
257
258int Scale_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
259{
260 std::vector<VkMat> bottom_top_blobs(2);
261 bottom_top_blobs[0] = bottom_top_blob;
262 bottom_top_blobs[1] = scale_data_gpu;
263
264 return forward_inplace(bottom_top_blobs, cmd, opt);
265}
266
267int Scale_vulkan::forward_inplace(std::vector<VkImageMat>& bottom_top_blobs, VkCompute& cmd, const Option& /*opt*/) const
268{
269 VkImageMat& bottom_top_blob = bottom_top_blobs[0];
270 const VkImageMat& scale_blob = bottom_top_blobs[1];
271
272 int elempack = bottom_top_blob.elempack;
273
274 std::vector<VkImageMat> bindings(4);
275 bindings[0] = bottom_top_blob;
276 bindings[1] = bottom_top_blob;
277 bindings[2] = scale_blob;
278 bindings[3] = bias_data_gpu_image;
279
280 std::vector<vk_constant_type> constants(5);
281 constants[0].i = bottom_top_blob.dims;
282 constants[1].i = bottom_top_blob.w;
283 constants[2].i = bottom_top_blob.h;
284 constants[3].i = bottom_top_blob.c;
285 constants[4].i = 0; //bottom_top_blob.cstep;
286
287 const Pipeline* pipeline = elempack == 8 ? pipeline_scale_pack8
288 : elempack == 4 ? pipeline_scale_pack4
289 : pipeline_scale;
290
291 cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
292
293 return 0;
294}
295
296int Scale_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
297{
298 std::vector<VkImageMat> bottom_top_blobs(2);
299 bottom_top_blobs[0] = bottom_top_blob;
300 bottom_top_blobs[1] = scale_data_gpu_image;
301
302 return forward_inplace(bottom_top_blobs, cmd, opt);
303}
304
305} // namespace ncnn