| 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 "gemm_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | #include "arm_usability.h" |
| 22 | |
| 23 | #include "cpu.h" |
| 24 | |
| 25 | namespace ncnn { |
| 26 | |
| 27 | #include "gemm_bf16s_fp16s.h" |
| 28 | #include "gemm_fp16s.h" |
| 29 | |
| 30 | #if NCNN_INT8 |
| 31 | #include "gemm_int8_fp16s.h" |
| 32 | #endif |
| 33 | |
| 34 | extern void pack_A_tile(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk); |
| 35 | |
| 36 | static int gemm_arm_fp16s(const Mat& A, const Mat& B, const Mat& C, Mat& top_blob, int broadcast_type_C, int transA, int transB, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt) |
| 37 | { |
| 38 | const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack; |
| 39 | const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w; |
| 40 | const int N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w; |
| 41 | |
| 42 | // NCNN_LOGE("M/N/K = %d %d %d", M, N, K); |
| 43 | |
| 44 | int TILE_M, TILE_N, TILE_K; |
| 45 | get_optimal_tile_mnk_bf16s_fp16s(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT); |
| 46 | |
| 47 | // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K); |
| 48 | |
| 49 | int nn_M = (M + TILE_M - 1) / TILE_M; |
| 50 | int nn_N = (N + TILE_N - 1) / TILE_N; |
| 51 | int nn_K = (K + TILE_K - 1) / TILE_K; |
| 52 | |
| 53 | Mat ATX(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, nT, 2u, opt.workspace_allocator); |
| 54 | if (ATX.empty()) |
| 55 | return -100; |
| 56 | Mat BT(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 2u, opt.workspace_allocator); |
| 57 | if (BT.empty()) |
| 58 | return -100; |
| 59 | |
| 60 | const int nn_NK = nn_N * nn_K; |
| 61 | |
| 62 | // pack B |
| 63 | #pragma omp parallel for num_threads(nT) |
| 64 | for (int ppjk = 0; ppjk < nn_NK; ppjk++) |
| 65 | { |
| 66 | const int ppj = ppjk / nn_K; |
| 67 | const int ppk = ppjk % nn_K; |
| 68 | |
| 69 | const int j = ppj * TILE_N; |
| 70 | const int k = ppk * TILE_K; |
| 71 | |
| 72 | const int max_jj = std::min((N - j), TILE_N); |
| 73 | const int max_kk = std::min((K - k), TILE_K); |
| 74 | |
| 75 | Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1); |
| 76 | |
| 77 | if (transB) |
| 78 | { |
| 79 | pack_B_tile_bf16_fp16(B, BT_tile, j, max_jj, k, max_kk); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | transpose_pack_B_tile_bf16_fp16(B, BT_tile, j, max_jj, k, max_kk); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | Mat topT; |
| 88 | if (K > TILE_K || broadcast_type_C == 3 || output_transpose) |
| 89 | { |
| 90 | topT.create(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator); |
| 91 | if (topT.empty()) |
| 92 | return -100; |
| 93 | } |
| 94 | |
| 95 | #pragma omp parallel for num_threads(nT) |
| 96 | for (int ppi = 0; ppi < nn_M; ppi++) |
| 97 | { |
| 98 | const int i = ppi * TILE_M; |
| 99 | |
| 100 | // shadowed variable for less openmp task args |
| 101 | const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack; |
| 102 | const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w; |
| 103 | |
| 104 | const int max_ii = std::min((M - i), TILE_M); |
| 105 | |
| 106 | Mat topT_tile; |
| 107 | if (K > TILE_K || broadcast_type_C == 3 || output_transpose) |
| 108 | topT_tile = topT.channel(get_omp_thread_num()); |
| 109 | |
| 110 | for (int j = 0; j < N; j += TILE_N) |
| 111 | { |
| 112 | const int max_jj = std::min((N - j), TILE_N); |
| 113 | |
| 114 | if (broadcast_type_C == 3) |
| 115 | { |
| 116 | pack_A_tile(C, topT_tile, i, max_ii, j, max_jj); |
| 117 | } |
| 118 | |
| 119 | const Mat& CT_tile = broadcast_type_C == 3 ? topT_tile : C; |
| 120 | |
| 121 | for (int k = 0; k < K; k += TILE_K) |
| 122 | { |
| 123 | const int max_kk = std::min((K - k), TILE_K); |
| 124 | |
| 125 | // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk); |
| 126 | |
| 127 | Mat AT_tile = ATX.channel(get_omp_thread_num()).row_range(k / TILE_K, 1); |
| 128 | |
| 129 | Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1); |
| 130 | |
| 131 | if (j == 0) |
| 132 | { |
| 133 | if (transA) |
| 134 | { |
| 135 | transpose_pack_A_tile_bf16_fp16(A, AT_tile, i, max_ii, k, max_kk); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | pack_A_tile_bf16_fp16(A, AT_tile, i, max_ii, k, max_kk); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | bool k_end = !output_transpose && k + TILE_K >= K; |
| 144 | float _alpha = k + TILE_K >= K ? alpha : 1.f; |
| 145 | |
| 146 | gemm_transB_packed_tile_fp16s(AT_tile, BT_tile, CT_tile, topT_tile, top_blob, broadcast_type_C, _alpha, i, max_ii, j, max_jj, k, max_kk, k_end); |
| 147 | } |
| 148 | |
| 149 | if (output_transpose) |
| 150 | { |
| 151 | transpose_unpack_output_tile_fp32_to_fp16(topT_tile, top_blob, i, max_ii, j, max_jj); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int gemm_AT_arm_fp16s(const Mat& AT, const Mat& B, const Mat& C, Mat& top_blob, int broadcast_type_C, int M, int K, int transB, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt) |
| 160 | { |
| 161 | const int N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w; |
| 162 | |
| 163 | // NCNN_LOGE("M/N/K = %d %d %d", M, N, K); |
| 164 | |
| 165 | int TILE_M, TILE_N, TILE_K; |
| 166 | get_optimal_tile_mnk_bf16s_fp16s(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT); |
| 167 | |
| 168 | // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K); |
| 169 | |
| 170 | int nn_M = (M + TILE_M - 1) / TILE_M; |
| 171 | int nn_N = (N + TILE_N - 1) / TILE_N; |
| 172 | int nn_K = (K + TILE_K - 1) / TILE_K; |
| 173 | |
| 174 | Mat BT(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 2u, opt.workspace_allocator); |
| 175 | if (BT.empty()) |
| 176 | return -100; |
| 177 | |
| 178 | const int nn_NK = nn_N * nn_K; |
| 179 | |
| 180 | // pack B |
| 181 | #pragma omp parallel for num_threads(nT) |
| 182 | for (int ppjk = 0; ppjk < nn_NK; ppjk++) |
| 183 | { |
| 184 | const int ppj = ppjk / nn_K; |
| 185 | const int ppk = ppjk % nn_K; |
| 186 | |
| 187 | const int j = ppj * TILE_N; |
| 188 | const int k = ppk * TILE_K; |
| 189 | |
| 190 | const int max_jj = std::min((N - j), TILE_N); |
| 191 | const int max_kk = std::min((K - k), TILE_K); |
| 192 | |
| 193 | Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1); |
| 194 | |
| 195 | if (transB) |
| 196 | { |
| 197 | pack_B_tile_bf16_fp16(B, BT_tile, j, max_jj, k, max_kk); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | transpose_pack_B_tile_bf16_fp16(B, BT_tile, j, max_jj, k, max_kk); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | Mat topT; |
| 206 | if (K > TILE_K || broadcast_type_C == 3 || output_transpose) |
| 207 | { |
| 208 | topT.create(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator); |
| 209 | if (topT.empty()) |
| 210 | return -100; |
| 211 | } |
| 212 | |
| 213 | #pragma omp parallel for num_threads(nT) |
| 214 | for (int ppi = 0; ppi < nn_M; ppi++) |
| 215 | { |
| 216 | const int i = ppi * TILE_M; |
| 217 | |
| 218 | const int max_ii = std::min((M - i), TILE_M); |
| 219 | |
| 220 | Mat topT_tile; |
| 221 | if (K > TILE_K || broadcast_type_C == 3 || output_transpose) |
| 222 | topT_tile = topT.channel(get_omp_thread_num()); |
| 223 | |
| 224 | for (int j = 0; j < N; j += TILE_N) |
| 225 | { |
| 226 | const int max_jj = std::min((N - j), TILE_N); |
| 227 | |
| 228 | if (broadcast_type_C == 3) |
| 229 | { |
| 230 | pack_A_tile(C, topT_tile, i, max_ii, j, max_jj); |
| 231 | } |
| 232 | |
| 233 | const Mat& CT_tile = broadcast_type_C == 3 ? topT_tile : C; |
| 234 | |
| 235 | for (int k = 0; k < K; k += TILE_K) |
| 236 | { |
| 237 | const int max_kk = std::min((K - k), TILE_K); |
| 238 | |
| 239 | // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk); |
| 240 | |
| 241 | Mat AT_tile = AT.channel(i / TILE_M).row_range(k / TILE_K, 1); |
| 242 | |
| 243 | Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1); |
| 244 | |
| 245 | bool k_end = !output_transpose && k + TILE_K >= K; |
| 246 | float _alpha = k + TILE_K >= K ? alpha : 1.f; |
| 247 | |
| 248 | gemm_transB_packed_tile_fp16s(AT_tile, BT_tile, CT_tile, topT_tile, top_blob, broadcast_type_C, _alpha, i, max_ii, j, max_jj, k, max_kk, k_end); |
| 249 | } |
| 250 | |
| 251 | if (output_transpose) |
| 252 | { |
| 253 | transpose_unpack_output_tile_fp32_to_fp16(topT_tile, top_blob, i, max_ii, j, max_jj); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static int gemm_BT_arm_fp16s(const Mat& A, const Mat& BT, const Mat& C, Mat& top_blob, int broadcast_type_C, int N, int K, int transA, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt) |
| 262 | { |
| 263 | const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack; |
| 264 | |
| 265 | // NCNN_LOGE("M/N/K = %d %d %d", M, N, K); |
| 266 | |
| 267 | int TILE_M, TILE_N, TILE_K; |
| 268 | get_optimal_tile_mnk_bf16s_fp16s(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT); |
| 269 | |
| 270 | // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K); |
| 271 | |
| 272 | int nn_M = (M + TILE_M - 1) / TILE_M; |
| 273 | // int nn_N = (N + TILE_N - 1) / TILE_N; |
| 274 | |
| 275 | Mat ATX(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, nT, 2u, opt.workspace_allocator); |
| 276 | if (ATX.empty()) |
| 277 | return -100; |
| 278 | |
| 279 | Mat topT; |
| 280 | if (K > TILE_K || broadcast_type_C == 3 || output_transpose) |
| 281 | { |
| 282 | topT.create(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator); |
| 283 | if (topT.empty()) |
| 284 | return -100; |
| 285 | } |
| 286 | |
| 287 | #pragma omp parallel for num_threads(nT) |
| 288 | for (int ppi = 0; ppi < nn_M; ppi++) |
| 289 | { |
| 290 | const int i = ppi * TILE_M; |
| 291 | |
| 292 | // shadowed variable for less openmp task args |
| 293 | const int M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack; |
| 294 | const int K = transA ? (A.dims == 3 ? A.c : A.h) * A.elempack : A.w; |
| 295 | |
| 296 | const int max_ii = std::min((M - i), TILE_M); |
| 297 | |
| 298 | Mat topT_tile; |
| 299 | if (K > TILE_K || broadcast_type_C == 3 || output_transpose) |
| 300 | topT_tile = topT.channel(get_omp_thread_num()); |
| 301 | |
| 302 | for (int j = 0; j < N; j += TILE_N) |
| 303 | { |
| 304 | const int max_jj = std::min((N - j), TILE_N); |
| 305 | |
| 306 | if (broadcast_type_C == 3) |
| 307 | { |
| 308 | pack_A_tile(C, topT_tile, i, max_ii, j, max_jj); |
| 309 | } |
| 310 | |
| 311 | const Mat& CT_tile = broadcast_type_C == 3 ? topT_tile : C; |
| 312 | |
| 313 | for (int k = 0; k < K; k += TILE_K) |
| 314 | { |
| 315 | const int max_kk = std::min((K - k), TILE_K); |
| 316 | |
| 317 | // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk); |
| 318 | |
| 319 | Mat AT_tile = ATX.channel(get_omp_thread_num()).row_range(k / TILE_K, 1); |
| 320 | |
| 321 | Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1); |
| 322 | |
| 323 | if (j == 0) |
| 324 | { |
| 325 | if (transA) |
| 326 | { |
| 327 | transpose_pack_A_tile_bf16_fp16(A, AT_tile, i, max_ii, k, max_kk); |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | pack_A_tile_bf16_fp16(A, AT_tile, i, max_ii, k, max_kk); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | bool k_end = !output_transpose && k + TILE_K >= K; |
| 336 | float _alpha = k + TILE_K >= K ? alpha : 1.f; |
| 337 | |
| 338 | gemm_transB_packed_tile_fp16s(AT_tile, BT_tile, CT_tile, topT_tile, top_blob, broadcast_type_C, _alpha, i, max_ii, j, max_jj, k, max_kk, k_end); |
| 339 | } |
| 340 | |
| 341 | if (output_transpose) |
| 342 | { |
| 343 | transpose_unpack_output_tile_fp32_to_fp16(topT_tile, top_blob, i, max_ii, j, max_jj); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int gemm_AT_BT_arm_fp16s(const Mat& AT, const Mat& BT, const Mat& C, Mat& top_blob, int broadcast_type_C, int M, int N, int K, int output_transpose, float alpha, int constant_TILE_M, int constant_TILE_N, int constant_TILE_K, int nT, const Option& opt) |
| 352 | { |
| 353 | // NCNN_LOGE("M/N/K = %d %d %d", M, N, K); |
| 354 | |
| 355 | int TILE_M, TILE_N, TILE_K; |
| 356 | get_optimal_tile_mnk_bf16s_fp16s(M, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, nT); |
| 357 | |
| 358 | // NCNN_LOGE("TILE M/N/K = %d %d %d", TILE_M, TILE_N, TILE_K); |
| 359 | |
| 360 | int nn_M = (M + TILE_M - 1) / TILE_M; |
| 361 | // int nn_N = (N + TILE_N - 1) / TILE_N; |
| 362 | |
| 363 | Mat topT; |
| 364 | if (K > TILE_K || broadcast_type_C == 3 || output_transpose) |
| 365 | { |
| 366 | topT.create(TILE_N * TILE_M, 1, nT, 4u, opt.workspace_allocator); |
| 367 | if (topT.empty()) |
| 368 | return -100; |
| 369 | } |
| 370 | |
| 371 | #pragma omp parallel for num_threads(nT) |
| 372 | for (int ppi = 0; ppi < nn_M; ppi++) |
| 373 | { |
| 374 | const int i = ppi * TILE_M; |
| 375 | |
| 376 | const int max_ii = std::min((M - i), TILE_M); |
| 377 | |
| 378 | Mat topT_tile; |
| 379 | if (K > TILE_K || broadcast_type_C == 3 || output_transpose) |
| 380 | topT_tile = topT.channel(get_omp_thread_num()); |
| 381 | |
| 382 | for (int j = 0; j < N; j += TILE_N) |
| 383 | { |
| 384 | const int max_jj = std::min((N - j), TILE_N); |
| 385 | |
| 386 | if (broadcast_type_C == 3) |
| 387 | { |
| 388 | pack_A_tile(C, topT_tile, i, max_ii, j, max_jj); |
| 389 | } |
| 390 | |
| 391 | const Mat& CT_tile = broadcast_type_C == 3 ? topT_tile : C; |
| 392 | |
| 393 | for (int k = 0; k < K; k += TILE_K) |
| 394 | { |
| 395 | const int max_kk = std::min((K - k), TILE_K); |
| 396 | |
| 397 | // NCNN_LOGE("max_ii/jj/kk = %d %d %d", max_ii, max_jj, max_kk); |
| 398 | |
| 399 | Mat AT_tile = AT.channel(i / TILE_M).row_range(k / TILE_K, 1); |
| 400 | |
| 401 | Mat BT_tile = BT.channel(j / TILE_N).row_range(k / TILE_K, 1); |
| 402 | |
| 403 | bool k_end = !output_transpose && k + TILE_K >= K; |
| 404 | float _alpha = k + TILE_K >= K ? alpha : 1.f; |
| 405 | |
| 406 | gemm_transB_packed_tile_fp16s(AT_tile, BT_tile, CT_tile, topT_tile, top_blob, broadcast_type_C, _alpha, i, max_ii, j, max_jj, k, max_kk, k_end); |
| 407 | } |
| 408 | |
| 409 | if (output_transpose) |
| 410 | { |
| 411 | transpose_unpack_output_tile_fp32_to_fp16(topT_tile, top_blob, i, max_ii, j, max_jj); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | int Gemm_arm::create_pipeline_fp16s(const Option& opt) |
| 420 | { |
| 421 | if (constantA) |
| 422 | { |
| 423 | const int M = constantM; |
| 424 | const int K = constantK; |
| 425 | |
| 426 | int TILE_M, TILE_N, TILE_K; |
| 427 | get_optimal_tile_mnk_bf16s_fp16s(M, 0, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, opt.num_threads); |
| 428 | |
| 429 | const int nn_M = (M + TILE_M - 1) / TILE_M; |
| 430 | |
| 431 | AT_data.create(TILE_K * TILE_M, (K + TILE_K - 1) / TILE_K, (M + TILE_M - 1) / TILE_M, 2u, (Allocator*)0); |
| 432 | if (AT_data.empty()) |
| 433 | return -100; |
| 434 | |
| 435 | #pragma omp parallel for num_threads(opt.num_threads) |
| 436 | for (int ppj = 0; ppj < nn_M; ppj++) |
| 437 | { |
| 438 | const int i = ppj * TILE_M; |
| 439 | |
| 440 | for (int k = 0; k < K; k += TILE_K) |
| 441 | { |
| 442 | const int max_ii = std::min((M - i), TILE_M); |
| 443 | const int max_kk = std::min((K - k), TILE_K); |
| 444 | |
| 445 | Mat AT_tile = AT_data.channel(i / TILE_M).row_range(k / TILE_K, 1); |
| 446 | |
| 447 | if (transA) |
| 448 | { |
| 449 | transpose_pack_A_tile_fp32_to_fp16(A_data, AT_tile, i, max_ii, k, max_kk); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | pack_A_tile_fp32_to_fp16(A_data, AT_tile, i, max_ii, k, max_kk); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | if (opt.lightmode) |
| 459 | A_data.release(); |
| 460 | } |
| 461 | |
| 462 | if (constantB) |
| 463 | { |
| 464 | const int N = constantN; |
| 465 | const int K = constantK; |
| 466 | |
| 467 | int TILE_M, TILE_N, TILE_K; |
| 468 | get_optimal_tile_mnk_bf16s_fp16s(0, N, K, constant_TILE_M, constant_TILE_N, constant_TILE_K, TILE_M, TILE_N, TILE_K, opt.num_threads); |
| 469 | |
| 470 | const int nn_N = (N + TILE_N - 1) / TILE_N; |
| 471 | |
| 472 | BT_data.create(TILE_K * TILE_N, (K + TILE_K - 1) / TILE_K, (N + TILE_N - 1) / TILE_N, 2u, (Allocator*)0); |
| 473 | if (BT_data.empty()) |
| 474 | return -100; |
| 475 | |
| 476 | #pragma omp parallel for num_threads(opt.num_threads) |
| 477 | for (int ppj = 0; ppj < nn_N; ppj++) |
| 478 | { |
| 479 | const int j = ppj * TILE_N; |
| 480 | |
| 481 | for (int k = 0; k < K; k += TILE_K) |
| 482 | { |
| 483 | const int max_jj = std::min((N - j), TILE_N); |
| 484 | const int max_kk = std::min((K - k), TILE_K); |
| 485 | |
| 486 | Mat BT_tile = BT_data.channel(j / TILE_N).row_range(k / TILE_K, 1); |
| 487 | |
| 488 | if (transB) |
| 489 | { |
| 490 | pack_B_tile_fp32_to_fp16(B_data, BT_tile, j, max_jj, k, max_kk); |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | transpose_pack_B_tile_fp32_to_fp16(B_data, BT_tile, j, max_jj, k, max_kk); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if (opt.lightmode) |
| 500 | B_data.release(); |
| 501 | } |
| 502 | |
| 503 | if (constantC && constant_broadcast_type_C != -1) |
| 504 | { |
| 505 | CT_data = C_data; |
| 506 | |
| 507 | #if __ARM_NEON |
| 508 | if (constant_broadcast_type_C == 3 && opt.use_packing_layout) |
| 509 | { |
| 510 | int C_elempack = constantM % 4 == 0 ? 4 : 1; |
| 511 | convert_packing(C_data, CT_data, C_elempack, opt); |
| 512 | } |
| 513 | #endif // __ARM_NEON |
| 514 | |
| 515 | // pre-multiply C with beta |
| 516 | if (beta != 1.f) |
| 517 | { |
| 518 | Mat C2; |
| 519 | C2.create_like(CT_data); |
| 520 | |
| 521 | const int size = CT_data.total() * CT_data.elempack; |
| 522 | for (int i = 0; i < size; i++) |
| 523 | { |
| 524 | C2[i] = CT_data[i] * beta; |
| 525 | } |
| 526 | |
| 527 | CT_data = C2; |
| 528 | } |
| 529 | |
| 530 | if (opt.lightmode) |
| 531 | C_data.release(); |
| 532 | } |
| 533 | |
| 534 | if (constantA || constantB || constantC) |
| 535 | { |
| 536 | nT = opt.num_threads; |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | int Gemm_arm::forward_fp16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 543 | { |
| 544 | int M; |
| 545 | int N; |
| 546 | if (constantA && constantB) |
| 547 | { |
| 548 | M = constantM; |
| 549 | N = constantN; |
| 550 | } |
| 551 | else if (constantA) |
| 552 | { |
| 553 | const Mat& B = bottom_blobs[0]; |
| 554 | M = constantM; |
| 555 | N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w; |
| 556 | } |
| 557 | else if (constantB) |
| 558 | { |
| 559 | const Mat& A = bottom_blobs[0]; |
| 560 | M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack; |
| 561 | N = constantN; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | const Mat& A = bottom_blobs[0]; |
| 566 | const Mat& B = bottom_blobs[1]; |
| 567 | M = transA ? A.w : (A.dims == 3 ? A.c : A.h) * A.elempack; |
| 568 | N = transB ? (B.dims == 3 ? B.c : B.h) * B.elempack : B.w; |
| 569 | } |
| 570 | |
| 571 | Mat C; |
| 572 | int broadcast_type_C = 0; |
| 573 | if (constantC) |
| 574 | { |
| 575 | C = CT_data; |
| 576 | broadcast_type_C = constant_broadcast_type_C; |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | if (constantA && constantB) |
| 581 | { |
| 582 | C = bottom_blobs.size() == 1 ? bottom_blobs[0] : Mat(); |
| 583 | } |
| 584 | else if (constantA) |
| 585 | { |
| 586 | C = bottom_blobs.size() == 2 ? bottom_blobs[1] : Mat(); |
| 587 | } |
| 588 | else if (constantB) |
| 589 | { |
| 590 | C = bottom_blobs.size() == 2 ? bottom_blobs[1] : Mat(); |
| 591 | } |
| 592 | else |
| 593 | { |
| 594 | C = bottom_blobs.size() == 3 ? bottom_blobs[2] : Mat(); |
| 595 | } |
| 596 | |
| 597 | if (!C.empty()) |
| 598 | { |
| 599 | if (C.dims == 1 && C.w == 1) |
| 600 | { |
| 601 | // scalar |
| 602 | broadcast_type_C = 0; |
| 603 | } |
| 604 | if (C.dims == 1 && C.w * C.elempack == M) |
| 605 | { |
| 606 | // M |
| 607 | // auto broadcast from h to w is the ncnn-style convention |
| 608 | broadcast_type_C = 1; |
| 609 | } |
| 610 | if (C.dims == 1 && C.w * C.elempack == N) |
| 611 | { |
| 612 | // N |
| 613 | broadcast_type_C = 4; |
| 614 | } |
| 615 | if (C.dims == 2 && C.w == 1 && C.h * C.elempack == M) |
| 616 | { |
| 617 | // Mx1 |
| 618 | broadcast_type_C = 2; |
| 619 | } |
| 620 | if (C.dims == 2 && C.w == N && C.h * C.elempack == M) |
| 621 | { |
| 622 | // MxN |
| 623 | broadcast_type_C = 3; |
| 624 | } |
| 625 | if (C.dims == 2 && C.w == N && C.h * C.elempack == 1) |
| 626 | { |
| 627 | // 1xN |
| 628 | broadcast_type_C = 4; |
| 629 | } |
| 630 | |
| 631 | // cast to fp32 |
| 632 | { |
| 633 | Mat CT_data; |
| 634 | cast_float16_to_float32(C, CT_data); |
| 635 | C = CT_data; |
| 636 | } |
| 637 | |
| 638 | // pre-multiply C with beta |
| 639 | if (beta != 1.f) |
| 640 | { |
| 641 | Mat CT_data; |
| 642 | CT_data.create_like(C, opt.workspace_allocator); |
| 643 | |
| 644 | const int size = C.total() * C.elempack; |
| 645 | for (int i = 0; i < size; i++) |
| 646 | { |
| 647 | CT_data[i] = C[i] * beta; |
| 648 | } |
| 649 | |
| 650 | C = CT_data; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | int out_elempack = 1; |
| 656 | if (opt.use_packing_layout) |
| 657 | { |
| 658 | int outh = output_transpose ? N : M; |
| 659 | out_elempack = outh % 4 == 0 ? 4 : 1; |
| 660 | } |
| 661 | if (output_elempack) |
| 662 | out_elempack = output_elempack; |
| 663 | size_t out_elemsize = 2u * out_elempack; |
| 664 | |
| 665 | Mat& top_blob = top_blobs[0]; |
| 666 | if (output_transpose) |
| 667 | { |
| 668 | if (output_N1M) |
| 669 | top_blob.create(M, 1, N / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 670 | else |
| 671 | top_blob.create(M, N / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | if (output_N1M) |
| 676 | top_blob.create(N, 1, M / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 677 | else |
| 678 | top_blob.create(N, M / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 679 | } |
| 680 | if (top_blob.empty()) |
| 681 | return -100; |
| 682 | |
| 683 | int _nT = nT ? nT : opt.num_threads; |
| 684 | if (nT != 0 && opt.num_threads != nT) |
| 685 | { |
| 686 | // force num_threads the same as in create_pipeline |
| 687 | // so we could use pre-packed A/B from the same tile config |
| 688 | NCNN_LOGE("opt.num_threads %d changed, gemm will use load-time value %d", opt.num_threads, nT); |
| 689 | } |
| 690 | |
| 691 | int ret = 0; |
| 692 | if (constantA && constantB) |
| 693 | { |
| 694 | ret = gemm_AT_BT_arm_fp16s(AT_data, BT_data, C, top_blob, broadcast_type_C, constantM, constantN, constantK, output_transpose, alpha, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt); |
| 695 | } |
| 696 | else if (constantA) |
| 697 | { |
| 698 | const Mat& B = bottom_blobs[0]; |
| 699 | ret = gemm_AT_arm_fp16s(AT_data, B, C, top_blob, broadcast_type_C, constantM, constantK, transB, output_transpose, alpha, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt); |
| 700 | } |
| 701 | else if (constantB) |
| 702 | { |
| 703 | const Mat& A = bottom_blobs[0]; |
| 704 | ret = gemm_BT_arm_fp16s(A, BT_data, C, top_blob, broadcast_type_C, constantN, constantK, transA, output_transpose, alpha, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt); |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | const Mat& A = bottom_blobs[0]; |
| 709 | const Mat& B = bottom_blobs[1]; |
| 710 | ret = gemm_arm_fp16s(A, B, C, top_blob, broadcast_type_C, transA, transB, output_transpose, alpha, constant_TILE_M, constant_TILE_N, constant_TILE_K, _nT, opt); |
| 711 | } |
| 712 | |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | #if NCNN_INT8 |
| 717 | void compute_A_tile_fp16_int8_scales_vfpv4(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii) |
| 718 | { |
| 719 | compute_A_tile_fp16_int8_scales(A, scales, B_scale, out_descales, i, max_ii); |
| 720 | } |
| 721 | |
| 722 | void transpose_compute_A_tile_fp16_int8_scales_vfpv4(const Mat& A, Mat& scales, float B_scale, Mat& out_descales, int i, int max_ii) |
| 723 | { |
| 724 | transpose_compute_A_tile_fp16_int8_scales(A, scales, B_scale, out_descales, i, max_ii); |
| 725 | } |
| 726 | |
| 727 | void pack_A_tile_fp16_to_int8_vfpv4(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales) |
| 728 | { |
| 729 | pack_A_tile_fp16_to_int8(A, AT, i, max_ii, k, max_kk, scales); |
| 730 | } |
| 731 | |
| 732 | void transpose_pack_A_tile_fp16_to_int8_vfpv4(const Mat& A, Mat& AT, int i, int max_ii, int k, int max_kk, const Mat& scales) |
| 733 | { |
| 734 | transpose_pack_A_tile_fp16_to_int8(A, AT, i, max_ii, k, max_kk, scales); |
| 735 | } |
| 736 | |
| 737 | void compute_B_fp16_int8_scale_vfpv4(const Mat& B, float& scale) |
| 738 | { |
| 739 | compute_B_fp16_int8_scale(B, scale); |
| 740 | } |
| 741 | |
| 742 | void pack_B_tile_fp16_to_int8_vfpv4(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale) |
| 743 | { |
| 744 | pack_B_tile_fp16_to_int8(B, BT, j, max_jj, k, max_kk, scale); |
| 745 | } |
| 746 | |
| 747 | void transpose_pack_B_tile_fp16_to_int8_vfpv4(const Mat& B, Mat& BT, int j, int max_jj, int k, int max_kk, float scale) |
| 748 | { |
| 749 | transpose_pack_B_tile_fp16_to_int8(B, BT, j, max_jj, k, max_kk, scale); |
| 750 | } |
| 751 | |
| 752 | void unpack_output_tile_int32_to_fp16_vfpv4(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta) |
| 753 | { |
| 754 | unpack_output_tile_int32_to_fp16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta); |
| 755 | } |
| 756 | |
| 757 | void transpose_unpack_output_tile_int32_to_fp16_vfpv4(const Mat& topT, const Mat& C, Mat& top_blob, int broadcast_type_C, int i, int max_ii, int j, int max_jj, const Mat& descales, float alpha, float beta) |
| 758 | { |
| 759 | transpose_unpack_output_tile_int32_to_fp16(topT, C, top_blob, broadcast_type_C, i, max_ii, j, max_jj, descales, alpha, beta); |
| 760 | } |
| 761 | #endif // NCNN_INT8 |
| 762 | |
| 763 | } // namespace ncnn |