gemm.h 19.7 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
Z
zhaojiaying01 已提交
14 15

#pragma once
H
hjchen2 已提交
16
#include <cstring>
17 18
#include <string>
#include "common/log.h"
19
#include "memory/t_malloc.h"
Z
ZhenWang 已提交
20 21 22
#ifdef _OPENMP
#include <omp.h>
#endif
Z
zhaojiaying01 已提交
23

Z
zhaojiaying01 已提交
24 25 26 27
// 矩阵取值运算宏,假设矩阵按行存储
#define A(i, j) A[(i)*lda + (j)]
#define B(i, j) B[(i)*ldb + (j)]
#define C(i, j) C[(i)*ldc + (j)]
Z
zhaojiaying01 已提交
28

Z
zhaojiaying01 已提交
29
#if __aarch64__
Z
Zhen Wang 已提交
30
#define MR_INT8 4
31
#define NR_INT8 4
Z
zhaojiaying01 已提交
32 33 34
#define MR 6
#define NR 16
#else
35
#define MR_INT8 4
36
#define NR_INT8 2
Z
zhaojiaying01 已提交
37
#define MR 6
38
#define NR 8
Z
zhaojiaying01 已提交
39
#endif
Z
zhaojiaying01 已提交
40

W
wangliu 已提交
41
#define s_min(i, j) ((i) < (j) ? (i) : (j))
Z
zhaojiaying01 已提交
42 43 44 45 46

namespace paddle_mobile {
namespace operators {
namespace math {

47 48
class Gemm {
 public:
49 50
  typedef void (Gemm::*FnPack)(int, int, int, const float *, int, float *,
                               const bool);
51 52 53 54 55 56 57
  typedef void (Gemm::*FnAddDot)(int, const float *, const float *, float *,
                                 int);
  FnPack procPackA;
  FnPack procPackB;
  FnAddDot procAddDot;

  void PackMatrixA_6r(int m, int k, int m_tail, const float *A, int lda,
58
                      float *buffer, const bool parallel);
59
  void PackMatrixA_8r(int m, int k, int m_tail, const float *A, int lda,
60
                      float *buffer, const bool parallel);
61
  void PackMatrixB_8c(int k, int n, int n_tail, const float *B, int ldb,
62
                      float *buffer, const bool parallel);
63 64
#if __aarch64__
  void PackMatrixB_12c(int k, int n, int n_tail, const float *B, int ldb,
65
                       float *buffer, const bool parallel);
66
  void PackMatrixB_16c(int k, int n, int n_tail, const float *B, int ldb,
67
                       float *buffer, const bool parallel);
68
#endif
69 70 71 72 73 74 75 76 77

  // 分块矩阵乘法
  void InnerKernel(int mc, int nc, float alpha, const float *a, const float *b,
                   float beta, float *c, float *C, int ldc, bool relu);
  void InnerKernelWithBias(int mc, int nc, float alpha, const float *a,
                           const float *b, float beta, float *c, float *C,
                           int ldc, bool relu, float *bias);

  void InnerKernelWithBn(int mc, int nc, float alpha, const float *a,
78
                         const float *b, float beta, float *c, float *C,
79 80 81 82 83 84 85 86
                         int ldc, bool relu, float *new_scale, float *new_bias);
  void InnerKernelWithBnAdd(int mc, int nc, float alpha, const float *a,
                            const float *b, float beta, float *c, float *C,
                            int ldc, bool relu, float *new_scale,
                            float *new_bias, float *bias);
  void InnerKernelWithPRelu(int mc, int nc, const float *a, const float *b,
                            float *c, float *C, int ldc, float *p,
                            std::string mode, float *bias, float *bias1);
Z
Zhen Wang 已提交
87

88
  // 计算一个更小的 C 矩阵分块
89
#if __aarch64__
90 91 92
  void AddDot6x8(int k, const float *a, const float *b, float *c, int ldc);
  void AddDot8x12(int k, const float *a, const float *b, float *c, int ldc);
  void AddDot6x16(int k, const float *a, const float *b, float *c, int ldc);
93 94 95 96 97
#else
  void AddDot4x4(int k, const float *a, const float *b, float *c, int ldc);
  void AddDot4x8(int k, const float *a, const float *b, float *c, int ldc);
  void AddDot6x8(int k, const float *a, const float *b, float *c, int ldc);
#endif
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

  // 分块矩阵乘法结果回写
  // C = A * B
  void WriteBasic(int mc, int nc, float *c, float *C, int ldc);
  // C = alpha * A * B + beta * C
  void WriteWithAlphaBeta(int mc, int nc, float *c, float *C, int ldc);
  // C = A * B + C
  void WriteWithAdd(int mc, int nc, float *c, float *C, int ldc);
  // C = A * B + bias
  void WriteWithAddV1(int mc, int nc, float *c, float *C, int ldc, float *bias);
  // C = A * B + C, relu(C)
  void WriteWithAddRelu(int mc, int nc, float *c, float *C, int ldc);
  // C = A * B + C,prelu(C)
  void WriteWithAddPRelu(int mc, int nc, float *c, float *C, int ldc, float *p,
                         std::string mode, float *bias, float *bias1);
  // C = A * B + bias ,relu(C)
  void WriteWithAddReluV1(int mc, int nc, float *c, float *C, int ldc,
115
                          float *bias);
116 117 118 119 120 121 122 123
  // C = A * B, batchnorm(C)
  void WriteWithBn(int mc, int nc, float *c, float *C, int ldc,
                   float *new_scale, float *new_bias);
  // C = A * B, batchnorm(C), relu(C)
  void WriteWithBnRelu(int mc, int nc, float *c, float *C, int ldc,
                       float *new_scale, float *new_bias);
  void WriteWithBnAddRelu(int mc, int nc, float *c, float *C, int ldc,
                          float *new_scale, float *new_bias, float *bias1);
Z
Zhen Wang 已提交
124

125 126 127 128 129 130 131 132 133 134 135 136
  // 向量矩阵乘法 (M = 1)
#if __aarch64__
#else
  void VectorKernel(int m, int n, int k, float alpha, const float *A, int lda,
                    const float *B, int ldb, float beta, float *C, int ldc,
                    bool relu);

  void VectorKernelWithBn(int m, int n, int k, float alpha, const float *A,
                          int lda, const float *B, int ldb, float beta,
                          float *C, int ldc, bool relu, float *new_scale,
                          float *new_bias);

137 138 139 140 141 142 143 144 145
  // 向量矩阵乘法结果回写
  // C = A * B
  void VecWriteBasic(int n, float *c, float *C, int ldc);
  // C = alpha * A * B + beta * C
  void VecWriteWithAlphaBeta(int n, float *c, float *C, int ldc);
  // C = A * B + C
  void VecWriteWithAdd(int n, float *c, float *C, int ldc);
  // C = A * B + C, relu(C)
  void VecWriteWithAddRelu(int n, float *c, float *C, int ldc);
146 147 148 149 150 151 152
  // C = A * B, batchnorm(C)
  void VecWriteWithBn(int n, float *c, float *C, int ldc, float *new_scale,
                      float *new_bias);
  // C = A * B, batchnorm(C), relu(C)
  void VecWriteWithBnRelu(int n, float *c, float *C, int ldc, float *new_scale,
                          float *new_bias);
#endif
153

Z
ZhenWang 已提交
154 155 156 157 158
  // 32位 float 矩阵乘法
  void Sgemm(int m, int n, int k, float alpha, const float *A, int lda,
             const float *B, int ldb, float beta, float *C, int ldc, bool relu,
             float *bias);

159 160 161 162
  // 32位 float 矩阵乘法, 并对结果进行 batchnrom
  void SgemmWithBn(int m, int n, int k, float alpha, const float *A, int lda,
                   const float *B, int ldb, float beta, float *C, int ldc,
                   bool relu, float *new_scale, float *new_bias, float *bias);
Z
ZhenWang 已提交
163

164 165 166 167
  void SgemmWithPRelu(int m, int n, int k, const float *A, int lda,
                      const float *B, int ldb, float *C, int ldc, float *p,
                      std::string mode, float *bias, float *bias1);

Z
ZhenWang 已提交
168 169 170 171 172
  // 32位 float 矩阵乘法(openmp 多线程版本)
  void Sgemm_omp(int m, int n, int k, float alpha, const float *A, int lda,
                 const float *B, int ldb, float beta, float *C, int ldc,
                 bool relu, float *bias);

173 174 175 176 177 178 179 180 181
  // 32位 float 矩阵乘法, 并对结果进行 batchnrom(openmp 多线程版本)
  void SgemmWithBn_omp(int m, int n, int k, float alpha, const float *A,
                       int lda, const float *B, int ldb, float beta, float *C,
                       int ldc, bool relu, float *new_scale, float *new_bias,
                       float *bias);

  void SgemmWithPRelu_omp(int m, int n, int k, const float *A, int lda,
                          const float *B, int ldb, float *C, int ldc, float *p,
                          std::string mode, float *bias, float *bias1);
Z
zhaojiaying01 已提交
182

Z
Zhen Wang 已提交
183
  // 8 bits function cluster begins
184
  // 8 bits int small block inner product, data packed k = 1
185 186
  void AddDot4x8(int32_t k, const int8_t *a, const int8_t *b, int32_t *c,
                 int32_t ldc);
187 188 189
  void AddDot6x8(int32_t k, const int8_t *a, const int8_t *b, int32_t *c,
                 int32_t ldc);
  // 8 bits int small block inner product, data packed k = 16
190 191
  void AddDot4x2(int32_t k, const int8_t *a, const int8_t *b, int32_t *c,
                 int32_t ldc);
192
  void AddDot4x4(int32_t k, const int8_t *a, const int8_t *b, int32_t *c,
Z
Zhen Wang 已提交
193 194
                 int32_t ldc);

Z
Zhen Wang 已提交
195
  // 8 bits int inner product
196
  template <typename Otype>
197
  void InnerKernel(int32_t mc, int32_t nc, float alpha, const int8_t *a,
198
                   const int8_t *b, float beta, int32_t *c, Otype *C,
199
                   int32_t ldc, bool relu);
200
  template <typename Otype>
201
  void InnerKernelWithBias(int32_t mc, int32_t nc, float alpha, const int8_t *a,
202
                           const int8_t *b, float beta, int32_t *c, Otype *C,
Z
ZhenWang 已提交
203 204
                           int32_t ldc, bool relu, int32_t *bias,
                           bool addOnRow = false);
Z
Zhen Wang 已提交
205

Z
Zhen Wang 已提交
206
  // 8 bits int pack function
207 208
  void PackMatrixA_4r(int32_t m, int32_t k, int32_t m_tail, const int8_t *A,
                      int32_t lda, int8_t *buffer);
Z
Zhen Wang 已提交
209 210 211 212
  void PackMatrixA_6r(int32_t m, int32_t k, int32_t m_tail, const int8_t *A,
                      int32_t lda, int8_t *buffer);
  void PackMatrixB_8c(int32_t k, int32_t n, int32_t n_tail, const int8_t *B,
                      int32_t ldb, int8_t *buffer);
213 214 215 216 217 218
  void PackMatrixA_4r_16(int32_t m, int32_t k, int32_t m_tail, const int8_t *A,
                         int32_t lda, int8_t *buffer);
  void PackMatrixB_2c_16(int32_t k, int32_t n, int32_t n_tail, const int8_t *B,
                         int32_t ldb, int8_t *buffer);
  void PackMatrixB_4c_16(int32_t k, int32_t n, int32_t n_tail, const int8_t *B,
                         int32_t ldb, int8_t *buffer);
Z
Zhen Wang 已提交
219 220 221 222
  void PackMatrixA_omp_4r(int32_t m, int32_t k, int32_t m_tail, const int8_t *A,
                          int32_t lda, int8_t *buffer);
  void PackMatrixB_omp_8c(int32_t k, int32_t n, int32_t n_tail, const int8_t *B,
                          int32_t ldb, int8_t *buffer);
223 224 225 226
  void PackMatrixA_omp_4r_16(int32_t m, int32_t k, int32_t m_tail,
                             const int8_t *A, int32_t lda, int8_t *buffer);
  void PackMatrixB_omp_2c_16(int32_t k, int32_t n, int32_t n_tail,
                             const int8_t *B, int32_t ldb, int8_t *buffer);
227 228
  void PackMatrixB_omp_4c_16(int32_t k, int32_t n, int32_t n_tail,
                             const int8_t *B, int32_t ldb, int8_t *buffer);
Z
Zhen Wang 已提交
229

Z
Zhen Wang 已提交
230
  // 8 bits int matrix product
231
  template <typename Itype, typename Btype, typename Otype>
Z
ZhenWang 已提交
232 233
  void Sgemm_omp(int32_t m, int32_t n, int32_t k, float alpha, const Itype *A,
                 int32_t lda, const Itype *B, int32_t ldb, float beta, Otype *C,
Z
ZhenWang 已提交
234
                 int32_t ldc, bool relu, Btype *bias, bool addOnRow = false);
Z
ZhenWang 已提交
235 236 237
  template <typename Otype>
  void Sgemm_omp(int32_t m, int32_t n, int32_t k, float alpha, const int8_t *A,
                 int32_t lda, const int8_t *B, int32_t ldb, float beta,
Z
ZhenWang 已提交
238 239
                 Otype *C, int32_t ldc, bool relu, int32_t *bias,
                 bool addOnRow = false);
Z
ZhenWang 已提交
240
  template <typename Itype, typename Btype, typename Otype>
241 242
  void Sgemm(int32_t m, int32_t n, int32_t k, float alpha, const Itype *A,
             int32_t lda, const Itype *B, int32_t ldb, float beta, Otype *C,
Z
ZhenWang 已提交
243
             int32_t ldc, bool relu, Btype *bias, bool addOnRow = false);
244
  template <typename Otype>
245
  void Sgemm(int32_t m, int32_t n, int32_t k, float alpha, const int8_t *A,
246
             int32_t lda, const int8_t *B, int32_t ldb, float beta, Otype *C,
Z
ZhenWang 已提交
247
             int32_t ldc, bool relu, int32_t *bias, bool addOnRow = false);
Z
Zhen Wang 已提交
248
  // 8 bits int write back
Z
Zhen Wang 已提交
249 250
  // C = A * B
  void WriteBasic(int32_t mc, int32_t nc, int32_t *c, int32_t *C, int32_t ldc);
251 252 253
  // C = A * B + bias, scale * relu(C)
  void WriteWithAddReluScale(int32_t mc, int32_t nc, int32_t *c, int8_t *C,
                             int32_t ldc, int32_t *bias, float scale);
Z
ZhenWang 已提交
254
  // C = A * B + bias, scale * C, bias is added on column
255 256
  void WriteWithAddScale(int32_t mc, int32_t nc, int32_t *c, int8_t *C,
                         int32_t ldc, int32_t *bias, float scale);
Z
ZhenWang 已提交
257 258 259
  // C = A * B + bias, scale * C, bias is added on row
  void WriteWithAddScaleT(int32_t mc, int32_t nc, int32_t *c, int8_t *C,
                          int32_t ldc, int32_t *bias, float scale);
Z
Zhen Wang 已提交
260

261 262 263 264
 private:
  int MC = 0;
  int KC = 0;
  int NC = 0;
Z
zhaojiaying01 已提交
265

Z
Zhen Wang 已提交
266
  // 32位 float
267 268 269
  float *packedA;
  float *packedB;
  float *packedC;
Z
Zhen Wang 已提交
270

Z
Zhen Wang 已提交
271
  // 8 bits int
Z
Zhen Wang 已提交
272 273
  int8_t *packedA_int8;
  int8_t *packedB_int8;
274
  int32_t *packedC_int32;
Z
Zhen Wang 已提交
275
  int8_t *zero_int8;
276
};
277

278 279 280 281
// 8 bits int matrix product (m*k x k*n)
template <typename Otype>
void Gemm::Sgemm(int32_t m, int32_t n, int32_t k, float alpha, const int8_t *A,
                 int32_t lda, const int8_t *B, int32_t ldb, float beta,
Z
ZhenWang 已提交
282 283
                 Otype *C, int32_t ldc, bool relu, int32_t *bias,
                 bool addOnRow) {
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
  // L1 data cache is 32 kib (Per Contex-A57, Contex-A72, Contex-A73)
  // L2 cache is 0.5~4 Mib (Contex-A72 cluster)
  int32_t L1 = 32 * 1024;
  int32_t L2 = 512 * 1024;

  const int32_t k_complete = (k + 15) - ((k + 15) & 15);
  KC = k_complete;
  MC = L1 / (KC * sizeof(int8_t));
  NC = L2 / (KC * sizeof(int8_t));

  // make sure MC is multiple of MR_INT8, and NC is multiple of NR_INT8
  if (MC == 0) {
    MC = MR_INT8;
  } else {
    int32_t mblock_num = (m + MC - 1) / MC;
    MC = (m + mblock_num - 1) / mblock_num;
    MC = (MC + MR_INT8 - 1) / MR_INT8 * MR_INT8;
  }
  // DLOG << "mblock_num = " << mblock_num << ", MC = " << MC << "\n";
  if (NC == 0) {
    NC = NR_INT8;
  } else {
    int32_t nblock_num = (n + NC - 1) / NC;
    NC = (n + nblock_num - 1) / nblock_num;
    NC = (NC + NR_INT8 - 1) / NR_INT8 * NR_INT8;
  }
  //  DLOG << "nblock_num = " << nblock_num << ", NC = " << NC << "\n";
  packedA_int8 = static_cast<int8_t *>(
      paddle_mobile::memory::Alloc(sizeof(int8_t) * MC * KC));
  packedB_int8 = static_cast<int8_t *>(
      paddle_mobile::memory::Alloc(sizeof(int8_t) * KC * NC));
  packedC_int32 = static_cast<int32_t *>(
      paddle_mobile::memory::Alloc(sizeof(int32_t) * MC * NC));
  zero_int8 =
      static_cast<int8_t *>(paddle_mobile::memory::Alloc(sizeof(int8_t) * k));

  memset(static_cast<void *>(zero_int8), 0, sizeof(int8_t) * k);
  int32_t mc, nc;
  for (int32_t j = 0; j < n; j += NC) {
    nc = s_min(n - j, NC);
324 325 326
#if __aarch64__
    PackMatrixB_4c_16(k, nc, nc % NR_INT8, &B(0, j), ldb, packedB_int8);
#else
327
    PackMatrixB_2c_16(k, nc, nc % NR_INT8, &B(0, j), ldb, packedB_int8);
328
#endif
329 330 331 332 333 334 335
    for (int32_t i = 0; i < m; i += MC) {
      mc = s_min(m - i, MC);
      PackMatrixA_4r_16(mc, k, mc % MR_INT8, &A(i, 0), lda, packedA_int8);
      if (bias == nullptr) {
        InnerKernel(mc, nc, alpha, packedA_int8, packedB_int8, beta,
                    packedC_int32, &C(i, j), ldc, relu);
      } else {
Z
ZhenWang 已提交
336 337 338 339 340 341 342 343 344
        if (addOnRow) {
          InnerKernelWithBias(mc, nc, alpha, packedA_int8, packedB_int8, beta,
                              packedC_int32, &C(i, j), ldc, relu, bias + j,
                              addOnRow);
        } else {
          InnerKernelWithBias(mc, nc, alpha, packedA_int8, packedB_int8, beta,
                              packedC_int32, &C(i, j), ldc, relu, bias + i,
                              addOnRow);
        }
345 346 347 348 349 350 351 352 353 354
      }
    }
  }

  paddle_mobile::memory::Free(packedA_int8);
  paddle_mobile::memory::Free(packedB_int8);
  paddle_mobile::memory::Free(packedC_int32);
  paddle_mobile::memory::Free(zero_int8);
}

Z
ZhenWang 已提交
355 356 357 358 359
// 8 bits int matrix product (m*k x k*n), omp version
template <typename Otype>
void Gemm::Sgemm_omp(int32_t m, int32_t n, int32_t k, float alpha,
                     const int8_t *A, int32_t lda, const int8_t *B, int32_t ldb,
                     float beta, Otype *C, int32_t ldc, bool relu,
Z
ZhenWang 已提交
360
                     int32_t *bias, bool addOnRow) {
Z
ZhenWang 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
#ifdef _OPENMP
  int32_t max_threads = omp_get_max_threads();
#else
  int32_t max_threads = 1;
#endif

  int32_t L1 = 64 / max_threads * 1024;
  const int32_t k_complete = (k + 15) - ((k + 15) & 15);
  KC = k_complete;
  zero_int8 =
      static_cast<int8_t *>(paddle_mobile::memory::Alloc(sizeof(int8_t) * k));
  memset(static_cast<void *>(zero_int8), 0, sizeof(int8_t) * k);
  if (m > n) {
    // 对 A 分块
    MC = L1 / (KC * sizeof(int8_t));
    if (MC == 0) {
      MC = MR_INT8;
    } else {
      int32_t mblock_num = (m + MC - 1) / MC;
      MC = (m + mblock_num - 1) / mblock_num;
      MC = (MC + MR_INT8 - 1) / MR_INT8 * MR_INT8;
    }
    // 补齐 B
    NC = (n + NR_INT8 - 1) / NR_INT8 * NR_INT8;

    packedB_int8 = static_cast<int8_t *>(
        paddle_mobile::memory::Alloc(sizeof(int8_t) * KC * NC));
#if __aarch64__
389
    PackMatrixB_omp_4c_16(k, n, n % NR_INT8, B, ldb, packedB_int8);
Z
ZhenWang 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
#else
    PackMatrixB_omp_2c_16(k, n, n % NR_INT8, B, ldb, packedB_int8);
#endif
    packedA_int8 = static_cast<int8_t *>(
        paddle_mobile::memory::Alloc(sizeof(int8_t) * MC * KC * max_threads));
  } else {
    // 对 B 分块
    NC = L1 / (KC * sizeof(int8_t));
    if (NC == 0) {
      NC = NR_INT8;
    } else {
      int32_t nblock_num = (n + NC - 1) / NC;
      NC = (n + nblock_num - 1) / nblock_num;
      NC = (NC + NR_INT8 - 1) / NR_INT8 * NR_INT8;
    }
    // 补齐 A
    MC = (m + MR_INT8 - 1) / MR_INT8 * MR_INT8;

    packedA_int8 = static_cast<int8_t *>(
        paddle_mobile::memory::Alloc(sizeof(int8_t) * MC * KC));
#if __aarch64__
411
    PackMatrixA_omp_4r_16(m, k, m % MR_INT8, A, lda, packedA_int8);
Z
ZhenWang 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
#else
    PackMatrixA_omp_4r_16(m, k, m % MR_INT8, A, lda, packedA_int8);
#endif
    packedB_int8 = static_cast<int8_t *>(
        paddle_mobile::memory::Alloc(sizeof(int8_t) * KC * NC * max_threads));
  }
  packedC_int32 = static_cast<int32_t *>(
      paddle_mobile::memory::Alloc(sizeof(int32_t) * MC * NC * max_threads));

  if (m > n) {
#pragma omp parallel for
    for (int32_t i = 0; i < m; i += MC) {
#ifdef _OPENMP
      int32_t local_threads = omp_get_thread_num();
#else
      int32_t local_threads = 0;
#endif

      int32_t mc;
      mc = s_min(m - i, MC);
      int8_t *local_A = packedA_int8 + MC * KC * local_threads;
      int32_t *local_C = packedC_int32 + MC * NC * local_threads;
#if __aarch64__
435
      PackMatrixA_4r_16(mc, k, mc % MR_INT8, &A(i, 0), lda, local_A);
Z
ZhenWang 已提交
436 437 438 439 440 441 442
#else
      PackMatrixA_4r_16(mc, k, mc % MR_INT8, &A(i, 0), lda, local_A);
#endif
      if (bias == nullptr) {
        InnerKernel(mc, n, alpha, local_A, packedB_int8, beta, local_C,
                    &C(i, 0), ldc, relu);
      } else {
Z
ZhenWang 已提交
443 444 445 446 447 448 449
        if (addOnRow) {
          InnerKernelWithBias(mc, n, alpha, local_A, packedB_int8, beta,
                              local_C, &C(i, 0), ldc, relu, bias, addOnRow);
        } else {
          InnerKernelWithBias(mc, n, alpha, local_A, packedB_int8, beta,
                              local_C, &C(i, 0), ldc, relu, bias + i, addOnRow);
        }
Z
ZhenWang 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
      }
    }
  } else {
#pragma omp parallel for
    for (int32_t j = 0; j < n; j += NC) {
#ifdef _OPENMP
      int32_t local_threads = omp_get_thread_num();
#else
      int32_t local_threads = 0;
#endif
      int32_t nc;
      nc = s_min(n - j, NC);
      int8_t *local_B = packedB_int8 + KC * NC * local_threads;
      int32_t *local_C = packedC_int32 + MC * NC * local_threads;
#if __aarch64__
465
      PackMatrixB_4c_16(k, nc, nc % NR_INT8, &B(0, j), ldb, local_B);
Z
ZhenWang 已提交
466 467 468 469 470 471 472
#else
      PackMatrixB_2c_16(k, nc, nc % NR_INT8, &B(0, j), ldb, local_B);
#endif
      if (bias == nullptr) {
        InnerKernel(m, nc, alpha, packedA_int8, local_B, beta, local_C,
                    &C(0, j), ldc, relu);
      } else {
Z
ZhenWang 已提交
473 474 475 476 477 478 479
        if (addOnRow) {
          InnerKernelWithBias(m, nc, alpha, packedA_int8, local_B, beta,
                              local_C, &C(0, j), ldc, relu, bias + j, addOnRow);
        } else {
          InnerKernelWithBias(m, nc, alpha, packedA_int8, local_B, beta,
                              local_C, &C(0, j), ldc, relu, bias, addOnRow);
        }
Z
ZhenWang 已提交
480 481 482 483 484 485 486 487 488 489
      }
    }
  }

  paddle_mobile::memory::Free(packedA_int8);
  paddle_mobile::memory::Free(packedB_int8);
  paddle_mobile::memory::Free(packedC_int32);
  paddle_mobile::memory::Free(zero_int8);
}

Z
zhaojiaying01 已提交
490 491 492
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile