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