Elffuss BraidHub

kikocis/ncnn public

master · 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 "multiheadattention_arm.h"
16
17#include "cpu.h"
18#include "layer_type.h"
19
20namespace ncnn {
21
22MultiHeadAttention_arm::MultiHeadAttention_arm()
23{
24#if __ARM_NEON
25 support_packing = true;
26#if NCNN_ARM82
27 support_fp16_storage = cpu_support_arm_asimdhp();
28#endif
29#endif // __ARM_NEON
30
31 support_bf16_storage = false;
32
33 q_gemm = 0;
34 k_gemm = 0;
35 v_gemm = 0;
36 o_gemm = 0;
37
38 qk_gemm = 0;
39 qkv_gemm = 0;
40
41 qk_softmax = 0;
42}
43
44int MultiHeadAttention_arm::create_pipeline(const Option& _opt)
45{
46 Option opt = _opt;
47 opt.use_fp16_storage &= support_fp16_storage;
48 opt.use_bf16_storage &= support_bf16_storage;
49
50 {
51 qk_softmax = ncnn::create_layer_cpu(ncnn::LayerType::Softmax);
52 ncnn::ParamDict pd;
53 pd.set(0, -1);
54 pd.set(1, 1);
55 qk_softmax->load_param(pd);
56 qk_softmax->load_model(ModelBinFromMatArray(0));
57 qk_softmax->create_pipeline(opt);
58 }
59
60 const int qdim = weight_data_size / embed_dim;
61
62 {
63 q_gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm);
64 ncnn::ParamDict pd;
65 pd.set(0, scale);
66 pd.set(1, 1.f);
67 pd.set(2, 0); // transA
68 pd.set(3, 1); // transB
69 pd.set(4, 1); // constantA
70 pd.set(5, 0); // constantB
71 pd.set(6, 1); // constantC
72 pd.set(7, embed_dim); // M
73 pd.set(8, 0); // N
74 pd.set(9, qdim); // K
75 pd.set(10, 1); // constant_broadcast_type_C
76 pd.set(11, 0); // output_N1M
77 pd.set(12, 1); // output_elempack
78 pd.set(14, 0); // output_transpose
79 q_gemm->load_param(pd);
80 Mat weights[2];
81 weights[0] = q_weight_data;
82 weights[1] = q_bias_data;
83 q_gemm->load_model(ModelBinFromMatArray(weights));
84 q_gemm->create_pipeline(opt);
85
86 if (opt.lightmode)
87 {
88 q_weight_data.release();
89 q_bias_data.release();
90 }
91 }
92
93 {
94 k_gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm);
95 ncnn::ParamDict pd;
96 pd.set(2, 0); // transA
97 pd.set(3, 1); // transB
98 pd.set(4, 1); // constantA
99 pd.set(5, 0); // constantB
100 pd.set(6, 1); // constantC
101 pd.set(7, embed_dim); // M
102 pd.set(8, 0); // N
103 pd.set(9, kdim); // K
104 pd.set(10, 1); // constant_broadcast_type_C
105 pd.set(11, 0); // output_N1M
106 pd.set(12, 1); // output_elempack
107 pd.set(14, 0); // output_transpose
108 k_gemm->load_param(pd);
109 Mat weights[2];
110 weights[0] = k_weight_data;
111 weights[1] = k_bias_data;
112 k_gemm->load_model(ModelBinFromMatArray(weights));
113 k_gemm->create_pipeline(opt);
114
115 if (opt.lightmode)
116 {
117 k_weight_data.release();
118 k_bias_data.release();
119 }
120 }
121
122 {
123 v_gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm);
124 ncnn::ParamDict pd;
125 pd.set(2, 0); // transA
126 pd.set(3, 1); // transB
127 pd.set(4, 1); // constantA
128 pd.set(5, 0); // constantB
129 pd.set(6, 1); // constantC
130 pd.set(7, embed_dim); // M
131 pd.set(8, 0); // N
132 pd.set(9, vdim); // K
133 pd.set(10, 1); // constant_broadcast_type_C
134 pd.set(11, 0); // output_N1M
135 pd.set(12, 1); // output_elempack
136 pd.set(14, 0); // output_transpose
137 v_gemm->load_param(pd);
138 Mat weights[2];
139 weights[0] = v_weight_data;
140 weights[1] = v_bias_data;
141 v_gemm->load_model(ModelBinFromMatArray(weights));
142 v_gemm->create_pipeline(opt);
143
144 if (opt.lightmode)
145 {
146 v_weight_data.release();
147 v_bias_data.release();
148 }
149 }
150
151 {
152 o_gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm);
153 ncnn::ParamDict pd;
154 pd.set(2, 1); // transA
155 pd.set(3, 1); // transB
156 pd.set(4, 0); // constantA
157 pd.set(5, 1); // constantB
158 pd.set(6, 1); // constantC
159 pd.set(7, 0); // M = outch
160 pd.set(8, qdim); // N = size
161 pd.set(9, embed_dim); // K = maxk*inch
162 pd.set(10, 4); // constant_broadcast_type_C = null
163 pd.set(11, 0); // output_N1M
164 o_gemm->load_param(pd);
165 Mat weights[2];
166 weights[0] = out_weight_data;
167 weights[1] = out_bias_data;
168 o_gemm->load_model(ModelBinFromMatArray(weights));
169 o_gemm->create_pipeline(opt);
170
171 if (opt.lightmode)
172 {
173 out_weight_data.release();
174 out_bias_data.release();
175 }
176 }
177
178 {
179 qk_gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm);
180 ncnn::ParamDict pd;
181 pd.set(2, 1); // transA
182 pd.set(3, 0); // transB
183 pd.set(4, 0); // constantA
184 pd.set(5, 0); // constantB
185 pd.set(6, attn_mask ? 0 : 1); // constantC
186 pd.set(7, 0); // M
187 pd.set(8, 0); // N
188 pd.set(9, 0); // K
189 pd.set(10, attn_mask ? 3 : -1); // constant_broadcast_type_C
190 pd.set(11, 0); // output_N1M
191 pd.set(12, 1); // output_elempack
192 qk_gemm->load_param(pd);
193 qk_gemm->load_model(ModelBinFromMatArray(0));
194 Option opt1 = opt;
195 opt1.num_threads = 1;
196 qk_gemm->create_pipeline(opt1);
197 }
198
199 {
200 qkv_gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm);
201 ncnn::ParamDict pd;
202 pd.set(2, 0); // transA
203 pd.set(3, 1); // transB
204 pd.set(4, 0); // constantA
205 pd.set(5, 0); // constantB
206 pd.set(6, 1); // constantC
207 pd.set(7, 0); // M
208 pd.set(8, 0); // N
209 pd.set(9, 0); // K
210 pd.set(10, -1); // constant_broadcast_type_C
211 pd.set(11, 0); // output_N1M
212 pd.set(12, 1); // output_elempack
213 pd.set(14, 1); // output_transpose
214 qkv_gemm->load_param(pd);
215 qkv_gemm->load_model(ModelBinFromMatArray(0));
216 Option opt1 = opt;
217 opt1.num_threads = 1;
218 qkv_gemm->create_pipeline(opt1);
219 }
220
221 return 0;
222}
223
224int MultiHeadAttention_arm::destroy_pipeline(const Option& _opt)
225{
226 Option opt = _opt;
227 opt.use_fp16_storage &= support_fp16_storage;
228 opt.use_bf16_storage &= support_bf16_storage;
229
230 if (qk_softmax)
231 {
232 qk_softmax->destroy_pipeline(opt);
233 delete qk_softmax;
234 qk_softmax = 0;
235 }
236
237 if (q_gemm)
238 {
239 q_gemm->destroy_pipeline(opt);
240 delete q_gemm;
241 q_gemm = 0;
242 }
243
244 if (k_gemm)
245 {
246 k_gemm->destroy_pipeline(opt);
247 delete k_gemm;
248 k_gemm = 0;
249 }
250
251 if (v_gemm)
252 {
253 v_gemm->destroy_pipeline(opt);
254 delete v_gemm;
255 v_gemm = 0;
256 }
257
258 if (o_gemm)
259 {
260 o_gemm->destroy_pipeline(opt);
261 delete o_gemm;
262 o_gemm = 0;
263 }
264
265 if (qk_gemm)
266 {
267 qk_gemm->destroy_pipeline(opt);
268 delete qk_gemm;
269 qk_gemm = 0;
270 }
271
272 if (qkv_gemm)
273 {
274 qkv_gemm->destroy_pipeline(opt);
275 delete qkv_gemm;
276 qkv_gemm = 0;
277 }
278
279 return 0;
280}
281
282int MultiHeadAttention_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& _opt) const
283{
284 const Mat& q_blob = bottom_blobs[0];
285 const Mat& k_blob = (bottom_blobs.size() == 1 || (bottom_blobs.size() == 2 && attn_mask)) ? q_blob : bottom_blobs[1];
286 const Mat& v_blob = (bottom_blobs.size() == 1 || (bottom_blobs.size() == 2 && attn_mask)) ? q_blob : (bottom_blobs.size() == 2 || (bottom_blobs.size() == 3 && attn_mask)) ? k_blob : bottom_blobs[2];
287 const Mat& attn_mask_blob = attn_mask ? bottom_blobs[bottom_blobs.size() - 1] : Mat();
288
289 Option opt = _opt;
290 opt.use_fp16_storage &= support_fp16_storage;
291 opt.use_bf16_storage &= support_bf16_storage;
292
293 Mat attn_mask_blob_unpacked;
294 if (attn_mask && attn_mask_blob.elempack != 1)
295 {
296 convert_packing(attn_mask_blob, attn_mask_blob_unpacked, 1, opt);
297 if (attn_mask_blob_unpacked.empty())
298 return -100;
299 }
300 else
301 {
302 attn_mask_blob_unpacked = attn_mask_blob;
303 }
304
305 const int embed_dim_per_head = embed_dim / num_heads;
306 const int src_seqlen = q_blob.h * q_blob.elempack;
307 const int dst_seqlen = k_blob.h * k_blob.elempack;
308
309 // const int elembits = q_blob.elembits();
310
311 size_t elemsize = q_blob.elemsize / q_blob.elempack;
312
313 Mat q_affine;
314 int retq = q_gemm->forward(q_blob, q_affine, opt);
315 if (retq != 0)
316 return retq;
317
318 Mat k_affine;
319 int retk = k_gemm->forward(k_blob, k_affine, opt);
320 if (retk != 0)
321 return retk;
322
323 Mat qk_cross(dst_seqlen, src_seqlen * num_heads, elemsize, opt.blob_allocator);
324 if (qk_cross.empty())
325 return -100;
326
327 std::vector<int> retqks;
328 retqks.resize(num_heads);
329 #pragma omp parallel for num_threads(opt.num_threads)
330 for (int i = 0; i < num_heads; i++)
331 {
332 std::vector<Mat> qk_bottom_blobs(2);
333 qk_bottom_blobs[0] = q_affine.row_range(i * embed_dim_per_head, embed_dim_per_head);
334 qk_bottom_blobs[1] = k_affine.row_range(i * embed_dim_per_head, embed_dim_per_head);
335 if (attn_mask)
336 {
337 const Mat& maskm = attn_mask_blob_unpacked.dims == 3 ? attn_mask_blob_unpacked.channel(i) : attn_mask_blob_unpacked;
338 qk_bottom_blobs.push_back(maskm);
339 }
340 std::vector<Mat> qk_top_blobs(1);
341 qk_top_blobs[0] = qk_cross.row_range(i * src_seqlen, src_seqlen);
342 Option opt1 = opt;
343 opt1.num_threads = 1;
344 retqks[i] = qk_gemm->forward(qk_bottom_blobs, qk_top_blobs, opt1);
345 }
346 for (int i = 0; i < num_heads; i++)
347 {
348 if (retqks[i] != 0)
349 return retqks[i];
350 }
351
352 q_affine.release();
353 k_affine.release();
354
355 int retqk = qk_softmax->forward_inplace(qk_cross, opt);
356 if (retqk != 0)
357 return retqk;
358
359 Mat v_affine;
360 int retv = v_gemm->forward(v_blob, v_affine, opt);
361 if (retv != 0)
362 return retv;
363
364 Mat qkv_cross(src_seqlen, embed_dim_per_head * num_heads, elemsize, opt.blob_allocator);
365 if (qkv_cross.empty())
366 return -100;
367
368 std::vector<int> retqkvs;
369 retqkvs.resize(num_heads);
370 #pragma omp parallel for num_threads(opt.num_threads)
371 for (int i = 0; i < num_heads; i++)
372 {
373 std::vector<Mat> qkv_bottom_blobs(2);
374 qkv_bottom_blobs[0] = qk_cross.row_range(i * src_seqlen, src_seqlen);
375 qkv_bottom_blobs[1] = v_affine.row_range(i * embed_dim_per_head, embed_dim_per_head);
376 std::vector<Mat> qkv_top_blobs(1);
377 qkv_top_blobs[0] = qkv_cross.row_range(i * embed_dim_per_head, embed_dim_per_head);
378 Option opt1 = opt;
379 opt1.num_threads = 1;
380 retqkvs[i] = qkv_gemm->forward(qkv_bottom_blobs, qkv_top_blobs, opt1);
381 }
382 for (int i = 0; i < num_heads; i++)
383 {
384 if (retqkvs[i] != 0)
385 return retqkvs[i];
386 }
387
388 v_affine.release();
389
390 int reto = o_gemm->forward(qkv_cross, top_blobs[0], opt);
391 if (reto != 0)
392 return reto;
393
394 return 0;
395}
396
397} // namespace ncnn