Elffuss BraidHub

kikocis/ncnn public

e5ca598b9f4c · 11.9 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2023 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 "gemm_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21Gemm_vulkan::Gemm_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25
26 pipeline_gemm = 0;
27}
28
29int Gemm_vulkan::create_pipeline(const Option& opt)
30{
31 // const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
32
33 // int elempack = 1;
34 // if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
35
36 // size_t elemsize;
37 // if (opt.use_fp16_storage)
38 // {
39 // elemsize = elempack * 2u;
40 // }
41 // else if (opt.use_fp16_packed)
42 // {
43 // elemsize = elempack == 1 ? 4u : elempack * 2u;
44 // }
45 // else
46 // {
47 // elemsize = elempack * 4u;
48 // }
49
50 // Mat shape_packed;
51 // if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack);
52
53 if (constantA)
54 {
55 A_data_packed = transA ? A_data.reshape(constantM, constantK) : A_data.reshape(constantK, constantM);
56 }
57 if (constantB)
58 {
59 B_data_packed = transB ? B_data.reshape(constantK, constantN) : B_data.reshape(constantN, constantK);
60 }
61 if (constantC)
62 {
63 C_data_packed = C_data;
64 }
65
66 std::vector<vk_specialization_type> specializations(15);
67 specializations[0].f = alpha;
68 specializations[1].f = beta;
69 specializations[2].i = transA;
70 specializations[3].i = transB;
71 specializations[4].i = constantA;
72 specializations[5].i = constantB;
73 specializations[6].i = constantC;
74 specializations[7].i = constantM;
75 specializations[8].i = constantN;
76 specializations[9].i = constantK;
77 specializations[10].i = constant_broadcast_type_C;
78 specializations[11].i = output_N1M;
79 specializations[12].i = output_elempack;
80 specializations[13].i = output_elemtype;
81 specializations[14].i = output_transpose;
82
83 Mat local_size_xyz;
84 // if (shape_packed.dims == 2)
85 // {
86 // local_size_xyz.w = std::min(8, shape_packed.w);
87 // local_size_xyz.h = std::min(8, shape_packed.h);
88 // local_size_xyz.c = 1;
89 // }
90
91 // pack1
92 // if (shape.dims == 0 || elempack == 1)
93 {
94 pipeline_gemm = new Pipeline(vkdev);
95 pipeline_gemm->set_optimal_local_size_xyz(local_size_xyz);
96 if (opt.use_shader_local_memory)
97 {
98 pipeline_gemm->set_local_size_xyz(8, 8, 1);
99 }
100 pipeline_gemm->create(LayerShaderType::gemm, opt, specializations);
101 }
102
103 if (opt.lightmode)
104 {
105 A_data.release();
106 B_data.release();
107 C_data.release();
108 }
109
110 return 0;
111}
112
113int Gemm_vulkan::destroy_pipeline(const Option& /*opt*/)
114{
115 delete pipeline_gemm;
116 pipeline_gemm = 0;
117
118 return 0;
119}
120
121int Gemm_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
122{
123 if (constantA)
124 {
125 if (support_image_storage && opt.use_image_storage)
126 {
127 cmd.record_upload(A_data_packed, A_data_gpu_image, opt);
128 }
129 else
130 {
131 cmd.record_upload(A_data_packed, A_data_gpu, opt);
132 }
133
134 A_data_packed.release();
135 }
136
137 if (constantB)
138 {
139 if (support_image_storage && opt.use_image_storage)
140 {
141 cmd.record_upload(B_data_packed, B_data_gpu_image, opt);
142 }
143 else
144 {
145 cmd.record_upload(B_data_packed, B_data_gpu, opt);
146 }
147
148 B_data_packed.release();
149 }
150
151 if (constantC)
152 {
153 if (support_image_storage && opt.use_image_storage)
154 {
155 cmd.record_upload(C_data_packed, C_data_gpu_image, opt);
156 }
157 else
158 {
159 cmd.record_upload(C_data_packed, C_data_gpu, opt);
160 }
161
162 C_data_packed.release();
163 }
164
165 return 0;
166}
167
168int Gemm_vulkan::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
169{
170 const VkMat& A0 = constantA ? A_data_gpu : bottom_blobs[0];
171 const VkMat& B0 = constantB ? B_data_gpu : constantA ? bottom_blobs[0] : bottom_blobs[1];
172 const VkMat& C0 = constantC ? C_data_gpu : bottom_blobs[bottom_blobs.size() - 1];
173
174 VkMat A;
175 VkMat B;
176 VkMat C;
177 vkdev->convert_packing(A0, A, 1, cmd, opt);
178 vkdev->convert_packing(B0, B, 1, cmd, opt);
179 vkdev->convert_packing(C0, C, 1, cmd, opt);
180
181 const int M = constantM ? constantM : transA ? A.w : (A.dims == 3 ? A.c : A.h);
182 const int K = constantK ? constantK : transA ? (A.dims == 3 ? A.c : A.h) : A.w;
183 const int N = constantN ? constantN : transB ? (B.dims == 3 ? B.c : B.h) : B.w;
184
185 int broadcast_type_C;
186 if (constantC)
187 {
188 broadcast_type_C = constant_broadcast_type_C;
189 }
190 else
191 {
192 if (C.dims == 1 && C.w == 1)
193 {
194 // scalar
195 broadcast_type_C = 0;
196 }
197 if (C.dims == 1 && C.w == M)
198 {
199 // M
200 // auto broadcast from h to w is the ncnn-style convention
201 broadcast_type_C = 1;
202 }
203 if (C.dims == 1 && C.w == N)
204 {
205 // N
206 broadcast_type_C = 4;
207 }
208 if (C.dims == 2 && C.w == 1 && C.h == M)
209 {
210 // Mx1
211 broadcast_type_C = 2;
212 }
213 if (C.dims == 2 && C.w == N && C.h == M)
214 {
215 // MxN
216 broadcast_type_C = 3;
217 }
218 if (C.dims == 2 && C.w == N && C.h == 1)
219 {
220 // 1xN
221 broadcast_type_C = 4;
222 }
223 }
224
225 int elempack = A.elempack;
226 size_t elemsize = A.elemsize;
227
228 VkMat& top_blob = top_blobs[0];
229 if (output_transpose)
230 {
231 if (output_N1M)
232 top_blob.create(M, 1, N, elemsize, opt.blob_vkallocator);
233 else
234 top_blob.create(M, N, elemsize, opt.blob_vkallocator);
235 }
236 else
237 {
238 if (output_N1M)
239 top_blob.create(N, 1, M, elemsize, opt.blob_vkallocator);
240 else
241 top_blob.create(N, M, elemsize, opt.blob_vkallocator);
242 }
243 if (top_blob.empty())
244 return -100;
245
246 std::vector<VkMat> bindings(4);
247 bindings[0] = top_blob;
248 bindings[1] = A;
249 bindings[2] = B;
250 bindings[3] = C;
251
252 std::vector<vk_constant_type> constants(10);
253 constants[0].i = M;
254 constants[1].i = N;
255 constants[2].i = K;
256 constants[3].i = broadcast_type_C;
257 constants[4].i = A.dims;
258 constants[5].i = A.dims == 3 ? A.cstep : transA ? M : K;
259 constants[6].i = B.dims;
260 constants[7].i = B.dims == 3 ? B.cstep : transB ? K : N;
261 constants[8].i = top_blob.dims;
262 constants[9].i = top_blob.dims == 3 ? top_blob.cstep : top_blob.w;
263
264 const Pipeline* pipeline = pipeline_gemm;
265
266 VkMat dispatcher;
267 dispatcher.w = (N + 1) / 2;
268 dispatcher.h = (M + 1) / 2;
269 dispatcher.c = 1;
270 cmd.record_pipeline(pipeline, bindings, constants, dispatcher);
271
272 int out_elempack = 1;
273 {
274 int outh = output_transpose ? N : M;
275 out_elempack = opt.use_shader_pack8 && outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1;
276 }
277 if (output_elempack)
278 out_elempack = output_elempack;
279
280 if (out_elempack != 1)
281 {
282 VkMat top_blob0;
283 vkdev->convert_packing(top_blob, top_blob0, out_elempack, cmd, opt);
284 top_blobs[0] = top_blob0;
285 }
286
287 return 0;
288}
289
290int Gemm_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
291{
292 std::vector<VkMat> bottom_blobs(1);
293 std::vector<VkMat> top_blobs(1);
294 bottom_blobs[0] = bottom_blob;
295 int ret = forward(bottom_blobs, top_blobs, cmd, opt);
296 top_blob = top_blobs[0];
297 return ret;
298}
299
300int Gemm_vulkan::forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const
301{
302 const VkImageMat& A0 = constantA ? A_data_gpu_image : bottom_blobs[0];
303 const VkImageMat& B0 = constantB ? B_data_gpu_image : constantA ? bottom_blobs[0] : bottom_blobs[1];
304 const VkImageMat& C0 = constantC ? C_data_gpu_image : bottom_blobs[bottom_blobs.size() - 1];
305
306 VkImageMat A;
307 VkImageMat B;
308 VkImageMat C;
309 vkdev->convert_packing(A0, A, 1, cmd, opt);
310 vkdev->convert_packing(B0, B, 1, cmd, opt);
311 vkdev->convert_packing(C0, C, 1, cmd, opt);
312
313 const int M = constantM ? constantM : transA ? A.w : (A.dims == 3 ? A.c : A.h);
314 const int K = constantK ? constantK : transA ? (A.dims == 3 ? A.c : A.h) : A.w;
315 const int N = constantN ? constantN : transB ? (B.dims == 3 ? B.c : B.h) : B.w;
316
317 int broadcast_type_C;
318 if (constantC)
319 {
320 broadcast_type_C = constant_broadcast_type_C;
321 }
322 else
323 {
324 if (C.dims == 1 && C.w == 1)
325 {
326 // scalar
327 broadcast_type_C = 0;
328 }
329 if (C.dims == 1 && C.w == M)
330 {
331 // M
332 // auto broadcast from h to w is the ncnn-style convention
333 broadcast_type_C = 1;
334 }
335 if (C.dims == 1 && C.w == N)
336 {
337 // N
338 broadcast_type_C = 4;
339 }
340 if (C.dims == 2 && C.w == 1 && C.h == M)
341 {
342 // Mx1
343 broadcast_type_C = 2;
344 }
345 if (C.dims == 2 && C.w == N && C.h == M)
346 {
347 // MxN
348 broadcast_type_C = 3;
349 }
350 if (C.dims == 2 && C.w == N && C.h == 1)
351 {
352 // 1xN
353 broadcast_type_C = 4;
354 }
355 }
356
357 int elempack = A.elempack;
358 size_t elemsize = A.elemsize;
359
360 VkImageMat& top_blob = top_blobs[0];
361 if (output_transpose)
362 {
363 if (output_N1M)
364 top_blob.create(M, 1, N, elemsize, opt.blob_vkallocator);
365 else
366 top_blob.create(M, N, elemsize, opt.blob_vkallocator);
367 }
368 else
369 {
370 if (output_N1M)
371 top_blob.create(N, 1, M, elemsize, opt.blob_vkallocator);
372 else
373 top_blob.create(N, M, elemsize, opt.blob_vkallocator);
374 }
375 if (top_blob.empty())
376 return -100;
377
378 std::vector<VkImageMat> bindings(4);
379 bindings[0] = top_blob;
380 bindings[1] = A;
381 bindings[2] = B;
382 bindings[3] = C;
383
384 std::vector<vk_constant_type> constants(10);
385 constants[0].i = M;
386 constants[1].i = N;
387 constants[2].i = K;
388 constants[3].i = broadcast_type_C;
389 constants[4].i = A.dims;
390 constants[5].i = 0; //A.w;
391 constants[6].i = B.dims;
392 constants[7].i = 0; //B.w;
393 constants[8].i = top_blob.dims;
394 constants[9].i = 0; //top_blob.w;
395
396 const Pipeline* pipeline = pipeline_gemm;
397
398 VkImageMat dispatcher;
399 dispatcher.w = (N + 1) / 2;
400 dispatcher.h = (M + 1) / 2;
401 dispatcher.c = 1;
402 cmd.record_pipeline(pipeline, bindings, constants, dispatcher);
403
404 int out_elempack = 1;
405 {
406 int outh = output_transpose ? N : M;
407 out_elempack = opt.use_shader_pack8 && outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1;
408 }
409 if (output_elempack)
410 out_elempack = output_elempack;
411
412 if (out_elempack != 1)
413 {
414 VkImageMat top_blob0;
415 vkdev->convert_packing(top_blob, top_blob0, out_elempack, cmd, opt);
416 top_blobs[0] = top_blob0;
417 }
418
419 return 0;
420}
421
422int Gemm_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const
423{
424 std::vector<VkImageMat> bottom_blobs(1);
425 std::vector<VkImageMat> top_blobs(1);
426 bottom_blobs[0] = bottom_blob;
427 int ret = forward(bottom_blobs, top_blobs, cmd, opt);
428 top_blob = top_blobs[0];
429 return ret;
430}
431
432} // namespace ncnn