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 "eltwise_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21Eltwise_vulkan::Eltwise_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25
26 pipeline_eltwise[0] = 0;
27 pipeline_eltwise[1] = 0;
28 pipeline_eltwise_pack4[0] = 0;
29 pipeline_eltwise_pack4[1] = 0;
30 pipeline_eltwise_pack8[0] = 0;
31 pipeline_eltwise_pack8[1] = 0;
32}
33
34int Eltwise_vulkan::create_pipeline(const Option& opt)
35{
36 const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
37
38 int elempack = 1;
39 if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
40 if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
41 if (shape.dims == 3 || shape.dims == 4) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
42
43 size_t elemsize;
44 if (opt.use_fp16_storage)
45 {
46 elemsize = elempack * 2u;
47 }
48 else if (opt.use_fp16_packed)
49 {
50 elemsize = elempack == 1 ? 4u : elempack * 2u;
51 }
52 else
53 {
54 elemsize = elempack * 4u;
55 }
56
57 Mat shape_packed;
58 if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack);
59 if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack);
60 if (shape.dims == 3 || shape.dims == 4) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
61
62 std::vector<vk_specialization_type> specializations(2 + 5);
63 specializations[0].i = op_type;
64 specializations[1].i = coeffs.w == 0 ? 0 : 1;
65 specializations[2 + 0].i = shape_packed.dims;
66 specializations[2 + 1].i = shape_packed.w;
67 specializations[2 + 2].i = shape_packed.h * shape_packed.d;
68 specializations[2 + 3].i = shape_packed.c;
69 specializations[2 + 4].i = shape_packed.cstep;
70
71 Mat local_size_xyz;
72 if (shape_packed.dims == 1)
73 {
74 local_size_xyz.w = std::min(64, shape_packed.w);
75 local_size_xyz.h = 1;
76 local_size_xyz.c = 1;
77 }
78 if (shape_packed.dims == 2)
79 {
80 local_size_xyz.w = std::min(8, shape_packed.w);
81 local_size_xyz.h = std::min(8, shape_packed.h);
82 local_size_xyz.c = 1;
83 }
84 if (shape_packed.dims == 3)
85 {
86 local_size_xyz.w = std::min(4, shape_packed.w);
87 local_size_xyz.h = std::min(4, shape_packed.h);
88 local_size_xyz.c = std::min(4, shape_packed.c);
89 }
90 if (shape_packed.dims == 4)
91 {
92 local_size_xyz.w = std::min(4, shape_packed.w);
93 local_size_xyz.h = std::min(4, shape_packed.h * shape_packed.d);
94 local_size_xyz.c = std::min(4, shape_packed.c);
95 }
96
97 // pack1
98 if (shape.dims == 0 || elempack == 1)
99 {
100 pipeline_eltwise[0] = new Pipeline(vkdev);
101 pipeline_eltwise[0]->set_optimal_local_size_xyz(local_size_xyz);
102 pipeline_eltwise[0]->create(LayerShaderType::eltwise, opt, specializations);
103 pipeline_eltwise[1] = new Pipeline(vkdev);
104 pipeline_eltwise[1]->set_optimal_local_size_xyz(local_size_xyz);
105 pipeline_eltwise[1]->create(LayerShaderType::eltwise, opt, specializations);
106 }
107
108 // pack4
109 if (shape.dims == 0 || elempack == 4)
110 {
111 pipeline_eltwise_pack4[0] = new Pipeline(vkdev);
112 pipeline_eltwise_pack4[0]->set_optimal_local_size_xyz(local_size_xyz);
113 pipeline_eltwise_pack4[0]->create(LayerShaderType::eltwise_pack4, opt, specializations);
114 pipeline_eltwise_pack4[1] = new Pipeline(vkdev);
115 pipeline_eltwise_pack4[1]->set_optimal_local_size_xyz(local_size_xyz);
116 pipeline_eltwise_pack4[1]->create(LayerShaderType::eltwise_pack4, opt, specializations);
117 }
118
119 // pack8
120 if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
121 {
122 pipeline_eltwise_pack8[0] = new Pipeline(vkdev);
123 pipeline_eltwise_pack8[0]->set_optimal_local_size_xyz(local_size_xyz);
124 pipeline_eltwise_pack8[0]->create(LayerShaderType::eltwise_pack8, opt, specializations);
125 pipeline_eltwise_pack8[1] = new Pipeline(vkdev);
126 pipeline_eltwise_pack8[1]->set_optimal_local_size_xyz(local_size_xyz);
127 pipeline_eltwise_pack8[1]->create(LayerShaderType::eltwise_pack8, opt, specializations);
128 }
129
130 return 0;
131}
132
133int Eltwise_vulkan::destroy_pipeline(const Option& /*opt*/)
134{
135 delete pipeline_eltwise[0];
136 delete pipeline_eltwise[1];
137 pipeline_eltwise[0] = 0;
138 pipeline_eltwise[1] = 0;
139
140 delete pipeline_eltwise_pack4[0];
141 delete pipeline_eltwise_pack4[1];
142 pipeline_eltwise_pack4[0] = 0;
143 pipeline_eltwise_pack4[1] = 0;
144
145 delete pipeline_eltwise_pack8[0];
146 delete pipeline_eltwise_pack8[1];
147 pipeline_eltwise_pack8[0] = 0;
148 pipeline_eltwise_pack8[1] = 0;
149
150 return 0;
151}
152
153int Eltwise_vulkan::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
154{
155 const VkMat& bottom_blob = bottom_blobs[0];
156 const VkMat& bottom_blob1 = bottom_blobs[1];
157
158 int elempack = bottom_blob.elempack;
159
160 VkMat& top_blob = top_blobs[0];
161 top_blob.create_like(bottom_blob, opt.blob_vkallocator);
162 if (top_blob.empty())
163 return -100;
164
165 std::vector<VkMat> bindings(3);
166 bindings[0] = bottom_blob;
167 bindings[1] = bottom_blob1;
168 bindings[2] = top_blob;
169
170 std::vector<vk_constant_type> constants(5 + 2);
171 constants[0].i = top_blob.dims;
172 constants[1].i = top_blob.w;
173 constants[2].i = top_blob.h * top_blob.d;
174 constants[3].i = top_blob.c;
175 constants[4].i = top_blob.cstep;
176 constants[5].f = coeffs.w == 0 ? 1.f : coeffs[0];
177 constants[6].f = coeffs.w == 0 ? 1.f : coeffs[1];
178
179 const Pipeline* pipeline = elempack == 8 ? pipeline_eltwise_pack8[1]
180 : elempack == 4 ? pipeline_eltwise_pack4[1]
181 : pipeline_eltwise[1];
182
183 cmd.record_pipeline(pipeline, bindings, constants, top_blob);
184
185 for (size_t b = 2; b < bottom_blobs.size(); b++)
186 {
187 std::vector<VkMat> bindings(3);
188 bindings[0] = top_blob;
189 bindings[1] = bottom_blobs[b];
190 bindings[2] = top_blob; // TODO use separated pipeline ?
191
192 std::vector<vk_constant_type> constants(5 + 2);
193 constants[0].i = top_blob.dims;
194 constants[1].i = top_blob.w;
195 constants[2].i = top_blob.h * top_blob.d;
196 constants[3].i = top_blob.c;
197 constants[4].i = top_blob.cstep;
198 constants[5].f = 1.f;
199 constants[6].f = coeffs.w == 0 ? 1 : coeffs[b];
200
201 const Pipeline* pipeline = elempack == 8 ? pipeline_eltwise_pack8[b % 2]
202 : elempack == 4 ? pipeline_eltwise_pack4[b % 2]
203 : pipeline_eltwise[b % 2];
204
205 cmd.record_pipeline(pipeline, bindings, constants, top_blob);
206 }
207
208 return 0;
209}
210
211int Eltwise_vulkan::forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const
212{
213 const VkImageMat& bottom_blob = bottom_blobs[0];
214 const VkImageMat& bottom_blob1 = bottom_blobs[1];
215
216 int elempack = bottom_blob.elempack;
217
218 VkImageMat& top_blob = top_blobs[0];
219 top_blob.create_like(bottom_blob, opt.blob_vkallocator);
220 if (top_blob.empty())
221 return -100;
222
223 std::vector<VkImageMat> bindings(3);
224 bindings[0] = bottom_blob;
225 bindings[1] = bottom_blob1;
226 bindings[2] = top_blob;
227
228 std::vector<vk_constant_type> constants(5 + 2);
229 constants[0].i = top_blob.dims;
230 constants[1].i = top_blob.w;
231 constants[2].i = top_blob.h * top_blob.d;
232 constants[3].i = top_blob.c;
233 constants[4].i = 0; //top_blob.cstep;
234 constants[5].f = coeffs.w == 0 ? 1.f : coeffs[0];
235 constants[6].f = coeffs.w == 0 ? 1.f : coeffs[1];
236
237 const Pipeline* pipeline = elempack == 8 ? pipeline_eltwise_pack8[1]
238 : elempack == 4 ? pipeline_eltwise_pack4[1]
239 : pipeline_eltwise[1];
240
241 cmd.record_pipeline(pipeline, bindings, constants, top_blob);
242
243 for (size_t b = 2; b < bottom_blobs.size(); b++)
244 {
245 std::vector<VkImageMat> bindings(3);
246 bindings[0] = top_blob;
247 bindings[1] = bottom_blobs[b];
248 bindings[2] = top_blob; // TODO use separated pipeline ?
249
250 std::vector<vk_constant_type> constants(5 + 2);
251 constants[0].i = top_blob.dims;
252 constants[1].i = top_blob.w;
253 constants[2].i = top_blob.h * top_blob.d;
254 constants[3].i = top_blob.c;
255 constants[4].i = 0; //top_blob.cstep;
256 constants[5].f = 1.f;
257 constants[6].f = coeffs.w == 0 ? 1 : coeffs[b];
258
259 const Pipeline* pipeline = elempack == 8 ? pipeline_eltwise_pack8[b % 2]
260 : elempack == 4 ? pipeline_eltwise_pack4[b % 2]
261 : pipeline_eltwise[b % 2];
262
263 cmd.record_pipeline(pipeline, bindings, constants, top_blob);
264 }
265
266 return 0;
267}
268
269} // namespace ncnn