| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "matmul.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | MatMul::MatMul() |
| 20 | { |
| 21 | one_blob_only = false; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int MatMul::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | transB = pd.get(0, 0); |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static void transpose(const Mat& X, Mat& XT, const Option& opt) |
| 33 | { |
| 34 | const int w = X.w; |
| 35 | const int h = X.h; |
| 36 | |
| 37 | const float* pX = X; |
| 38 | float* pXT = XT; |
| 39 | |
| 40 | #pragma omp parallel for num_threads(opt.num_threads) |
| 41 | for (int i = 0; i < w; i++) |
| 42 | { |
| 43 | float* ptr = pXT + i * h; |
| 44 | for (int j = 0; j < h; j++) |
| 45 | { |
| 46 | ptr[j] = pX[j * w + i]; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void matmul_transb(const Mat& A, const Mat& B, Mat& top_blob, const Option& opt) |
| 52 | { |
| 53 | const int M = A.h; |
| 54 | const int K = A.w; // assert A.w == B.w |
| 55 | const int N = B.h; |
| 56 | |
| 57 | const float* pA = A; |
| 58 | const float* pB = B; |
| 59 | float* pOut = top_blob; |
| 60 | |
| 61 | #pragma omp parallel for num_threads(opt.num_threads) |
| 62 | for (int i = 0; i < M; i++) |
| 63 | { |
| 64 | const float* ptrA = pA + i * K; |
| 65 | float* outptr = pOut + i * N; |
| 66 | |
| 67 | for (int j = 0; j < N; j++) |
| 68 | { |
| 69 | const float* ptrB = pB + j * K; |
| 70 | |
| 71 | float sum = 0.f; |
| 72 | for (int k = 0; k < K; k++) |
| 73 | { |
| 74 | sum += ptrA[k] * ptrB[k]; |
| 75 | } |
| 76 | |
| 77 | *outptr++ = sum; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | int MatMul::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 83 | { |
| 84 | const Mat& A = bottom_blobs[0]; |
| 85 | const Mat& B = bottom_blobs[1]; |
| 86 | Mat& top_blob = top_blobs[0]; |
| 87 | |
| 88 | const int Adims = A.dims; |
| 89 | const int Bdims = B.dims; |
| 90 | const int max_ABdims = std::max(Adims, Bdims); |
| 91 | const size_t elemsize = A.elemsize; |
| 92 | |
| 93 | if (Adims == 1 && Bdims == 1) |
| 94 | { |
| 95 | // dot product |
| 96 | top_blob.create(1, elemsize, opt.blob_allocator); |
| 97 | if (top_blob.empty()) |
| 98 | return -100; |
| 99 | |
| 100 | const int K = A.w; // assert A.w == B.w |
| 101 | const float* ptrA = A; |
| 102 | const float* ptrB = B; |
| 103 | |
| 104 | float sum = 0.f; |
| 105 | for (int k = 0; k < K; k++) |
| 106 | { |
| 107 | sum += ptrA[k] * ptrB[k]; |
| 108 | } |
| 109 | |
| 110 | top_blob[0] = sum; |
| 111 | } |
| 112 | else if (Adims == 2 && Bdims == 2) |
| 113 | { |
| 114 | // matrix multiply |
| 115 | const int M = A.h; |
| 116 | const int N = transB == 0 ? B.w : B.h; |
| 117 | |
| 118 | top_blob.create(N, M, elemsize, opt.blob_allocator); |
| 119 | if (top_blob.empty()) |
| 120 | return -100; |
| 121 | |
| 122 | Mat BT; |
| 123 | if (transB == 0) |
| 124 | { |
| 125 | BT.create(B.h, B.w, elemsize, opt.workspace_allocator); |
| 126 | if (BT.empty()) |
| 127 | return -100; |
| 128 | |
| 129 | transpose(B, BT, opt); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | BT = B; |
| 134 | } |
| 135 | |
| 136 | matmul_transb(A, BT, top_blob, opt); |
| 137 | } |
| 138 | else if (Adims == 1 && Bdims == 2) |
| 139 | { |
| 140 | // matrix multiply |
| 141 | const int N = transB == 0 ? B.w : B.h; |
| 142 | |
| 143 | Mat top_blob1(N, 1, elemsize, opt.blob_allocator); |
| 144 | if (top_blob1.empty()) |
| 145 | return -100; |
| 146 | |
| 147 | Mat A1 = A.reshape(A.w, 1); |
| 148 | |
| 149 | Mat BT; |
| 150 | if (transB == 0) |
| 151 | { |
| 152 | BT.create(B.h, B.w, elemsize, opt.workspace_allocator); |
| 153 | if (BT.empty()) |
| 154 | return -100; |
| 155 | |
| 156 | transpose(B, BT, opt); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | BT = B; |
| 161 | } |
| 162 | |
| 163 | matmul_transb(A1, BT, top_blob1, opt); |
| 164 | |
| 165 | top_blob = top_blob1.reshape(N); |
| 166 | } |
| 167 | else if (Adims == 2 && Bdims == 1) |
| 168 | { |
| 169 | // matrix multiply |
| 170 | const int M = A.h; |
| 171 | |
| 172 | Mat top_blob1(1, M, elemsize, opt.blob_allocator); |
| 173 | if (top_blob1.empty()) |
| 174 | return -100; |
| 175 | |
| 176 | Mat BT = B.reshape(B.w, 1); |
| 177 | |
| 178 | matmul_transb(A, BT, top_blob1, opt); |
| 179 | |
| 180 | top_blob = top_blob1.reshape(M); |
| 181 | } |
| 182 | else if (Adims == 1 && Bdims > 2) |
| 183 | { |
| 184 | // batched matrix multiply |
| 185 | const int N = transB == 0 ? B.w : B.h; |
| 186 | const int batch_size = B.d * B.c; |
| 187 | |
| 188 | Mat top_blob1(N, 1, batch_size, elemsize, opt.blob_allocator); |
| 189 | if (top_blob1.empty()) |
| 190 | return -100; |
| 191 | |
| 192 | Mat A1 = A.reshape(A.w, 1); |
| 193 | Mat B1 = B.reshape(B.w, B.h, batch_size); |
| 194 | |
| 195 | for (int p = 0; p < batch_size; p++) |
| 196 | { |
| 197 | Mat BT; |
| 198 | if (transB == 0) |
| 199 | { |
| 200 | BT.create(B.h, B.w, elemsize, opt.workspace_allocator); |
| 201 | if (BT.empty()) |
| 202 | return -100; |
| 203 | |
| 204 | transpose(B1.channel(p), BT, opt); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | BT = B1.channel(p); |
| 209 | } |
| 210 | |
| 211 | Mat top_blob1_p = top_blob1.channel(p); |
| 212 | matmul_transb(A1, BT, top_blob1_p, opt); |
| 213 | } |
| 214 | |
| 215 | if (Bdims == 3) |
| 216 | top_blob = top_blob1.reshape(N, B.d * B.c); |
| 217 | else |
| 218 | top_blob = top_blob1.reshape(N, B.d, B.c); |
| 219 | } |
| 220 | else if (Adims > 2 && Bdims == 1) |
| 221 | { |
| 222 | // batched matrix multiply |
| 223 | const int M = A.h; |
| 224 | const int batch_size = A.d * A.c; |
| 225 | |
| 226 | Mat top_blob1(1, M, batch_size, elemsize, opt.blob_allocator); |
| 227 | if (top_blob1.empty()) |
| 228 | return -100; |
| 229 | |
| 230 | Mat A1 = A.reshape(A.w, A.h, batch_size); |
| 231 | Mat BT = B.reshape(B.w, 1); |
| 232 | |
| 233 | for (int p = 0; p < batch_size; p++) |
| 234 | { |
| 235 | Mat top_blob1_p = top_blob1.channel(p); |
| 236 | matmul_transb(A1.channel(p), BT, top_blob1_p, opt); |
| 237 | } |
| 238 | |
| 239 | if (Adims == 3) |
| 240 | top_blob = top_blob1.reshape(M, A.d * A.c); |
| 241 | else |
| 242 | top_blob = top_blob1.reshape(M, A.d, A.c); |
| 243 | } |
| 244 | else if (max_ABdims == 3) |
| 245 | { |
| 246 | Mat A1 = Adims == 2 ? A.reshape(A.w, A.h, 1) : A; |
| 247 | Mat B1 = Bdims == 2 ? B.reshape(B.w, B.h, 1) : B; |
| 248 | |
| 249 | const int M = A1.h; |
| 250 | const int N = transB == 0 ? B1.w : B1.h; |
| 251 | const int batch_size = std::max(A1.c, B1.c); |
| 252 | |
| 253 | top_blob.create(N, M, batch_size, elemsize, opt.blob_allocator); |
| 254 | if (top_blob.empty()) |
| 255 | return -100; |
| 256 | |
| 257 | Mat BT0; |
| 258 | if (B1.c == 1) |
| 259 | { |
| 260 | if (transB == 0) |
| 261 | { |
| 262 | BT0.create(B1.h, B1.w, elemsize, opt.workspace_allocator); |
| 263 | if (BT0.empty()) |
| 264 | return -100; |
| 265 | |
| 266 | transpose(B1.channel(0), BT0, opt); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | BT0 = B1.channel(0); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | for (int p = 0; p < batch_size; p++) |
| 275 | { |
| 276 | int Ap = A1.c == 1 ? 0 : p; |
| 277 | int Bp = B1.c == 1 ? 0 : p; |
| 278 | |
| 279 | Mat BT; |
| 280 | if (B1.c == 1) |
| 281 | { |
| 282 | BT = BT0; |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | if (transB == 0) |
| 287 | { |
| 288 | BT.create(B1.h, B1.w, elemsize, opt.workspace_allocator); |
| 289 | if (BT.empty()) |
| 290 | return -100; |
| 291 | |
| 292 | transpose(B1.channel(Bp), BT, opt); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | BT = B1.channel(Bp); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | Mat top_blob_p = top_blob.channel(p); |
| 301 | matmul_transb(A1.channel(Ap), BT, top_blob_p, opt); |
| 302 | } |
| 303 | } |
| 304 | else if (max_ABdims == 4) |
| 305 | { |
| 306 | Mat A1 = Adims == 3 ? A.reshape(A.w, A.h, A.c, 1) : A; |
| 307 | Mat B1 = Bdims == 3 ? B.reshape(B.w, B.h, B.c, 1) : B; |
| 308 | |
| 309 | const int M = A1.h; |
| 310 | const int N = transB == 0 ? B1.w : B1.h; |
| 311 | const int batch_size_d = std::max(A1.d, B1.d); |
| 312 | const int batch_size_c = std::max(A1.c, B1.c); |
| 313 | |
| 314 | top_blob.create(N, M, batch_size_d, batch_size_c, elemsize, opt.blob_allocator); |
| 315 | if (top_blob.empty()) |
| 316 | return -100; |
| 317 | |
| 318 | Mat BT00; |
| 319 | if (B1.d == 1 && B1.c == 1) |
| 320 | { |
| 321 | if (transB == 0) |
| 322 | { |
| 323 | BT00.create(B1.h, B1.w, elemsize, opt.workspace_allocator); |
| 324 | if (BT00.empty()) |
| 325 | return -100; |
| 326 | |
| 327 | transpose(B1.channel(0).depth(0), BT00, opt); |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | BT00 = B1.channel(0).depth(0); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | for (int p = 0; p < batch_size_c; p++) |
| 336 | { |
| 337 | int Ap = A1.c == 1 ? 0 : p; |
| 338 | int Bp = B1.c == 1 ? 0 : p; |
| 339 | |
| 340 | Mat BT0x; |
| 341 | if (B1.d == 1 && B1.c != 1) |
| 342 | { |
| 343 | if (transB == 0) |
| 344 | { |
| 345 | BT0x.create(B1.h, B1.w, elemsize, opt.workspace_allocator); |
| 346 | if (BT0x.empty()) |
| 347 | return -100; |
| 348 | |
| 349 | transpose(B1.channel(Bp).depth(0), BT0x, opt); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | BT0x = B1.channel(Bp).depth(0); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | for (int q = 0; q < batch_size_d; q++) |
| 358 | { |
| 359 | int Ad = A1.d == 1 ? 0 : q; |
| 360 | int Bd = B1.d == 1 ? 0 : q; |
| 361 | |
| 362 | Mat BT; |
| 363 | if (B1.d == 1 && B1.c == 1) |
| 364 | { |
| 365 | BT = BT00; |
| 366 | } |
| 367 | else if (B1.d == 1 && B1.c != 1) |
| 368 | { |
| 369 | BT = BT0x; |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | if (transB == 0) |
| 374 | { |
| 375 | BT.create(B1.h, B1.w, elemsize, opt.workspace_allocator); |
| 376 | if (BT.empty()) |
| 377 | return -100; |
| 378 | |
| 379 | transpose(B1.channel(Bp).depth(Bd), BT, opt); |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | BT = B1.channel(Bp).depth(Bd); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | Mat top_blob_p_q = top_blob.channel(p).depth(q); |
| 388 | matmul_transb(A1.channel(Ap).depth(Ad), BT, top_blob_p_q, opt); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | NCNN_LOGE("impossible matmul %d %d", Adims, Bdims); |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | } // namespace ncnn |