math_function_int8.cpp 3.5 KB
Newer Older
Z
Zhen Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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. */

#include <cstring>
#include <string>
#include "operators/math/gemm.h"
#include "operators/math/math_function.h"

namespace paddle_mobile {
namespace operators {
namespace math {
23 24 25 26
void matmul_int8(const framework::Tensor &matrix_a, bool trans_a,
                 const framework::Tensor &matrix_b, bool trans_b, float alpha,
                 framework::Tensor *matrix_out, float beta, bool relu,
                 int32_t *bias) {
Z
Zhen Wang 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  auto dim_a = matrix_a.dims();
  auto dim_b = matrix_b.dims();
  auto dim_out = matrix_out->dims();
  PADDLE_MOBILE_ENFORCE(
      dim_a.size() == 2 && dim_b.size() == 2 && dim_out.size() == 2,
      "The input and output of matmul be matrix");

  int32_t M = dim_out[0];
  int32_t N = dim_out[1];
  int32_t K = (!trans_a) ? dim_a[1] : dim_a[0];
  Gemm gemm;

  if (trans_a) {
    int32_t numel = matrix_a.numel();
    int32_t m = matrix_a.dims()[0];
    int32_t n = matrix_a.dims()[1];
    int8_t *tmp = (int8_t *)(matrix_a.data<int8_t>());  // NOLINT
    int8_t *a = static_cast<int8_t *>(
        paddle_mobile::memory::Alloc(sizeof(int8_t) * numel));
    int32_t index = 0;
    for (int32_t j = 0; j < n; j++) {
      for (int32_t i = 0; i < m; i++) {
        a[index++] = tmp[i * n + j];
      }
    }

Z
Zhen Wang 已提交
53
#ifdef _OPENMP
54 55 56 57 58 59 60 61
    if (bias != nullptr) {
      // TODO(wzzju):gemm.Sgemm_omp_with_bias, now use single thread instead.
      gemm.Sgemm(M, N, K, alpha, a, K, matrix_b.data<int8_t>(), N, beta,
                 matrix_out->data<int8_t>(), N, relu, bias);
    } else {
      gemm.Sgemm_omp(M, N, K, alpha, a, K, matrix_b.data<int8_t>(), N, beta,
                     matrix_out->data<int32_t>(), N, relu, bias);
    }
Z
Zhen Wang 已提交
62
#else
63 64 65 66 67 68 69
    if (bias != nullptr) {
      gemm.Sgemm(M, N, K, alpha, a, K, matrix_b.data<int8_t>(), N, beta,
                 matrix_out->data<int8_t>(), N, relu, bias);
    } else {
      gemm.Sgemm(M, N, K, alpha, a, K, matrix_b.data<int8_t>(), N, beta,
                 matrix_out->data<int32_t>(), N, relu, bias);
    }
Z
Zhen Wang 已提交
70
#endif
Z
Zhen Wang 已提交
71
  } else {
Z
Zhen Wang 已提交
72
#ifdef _OPENMP
73 74 75 76 77 78 79 80 81 82
    if (bias != nullptr) {
      // TODO(wzzju):gemm.Sgemm_omp_with_bias, now use single thread instead.
      gemm.Sgemm(M, N, K, alpha, matrix_a.data<int8_t>(), K,
                 matrix_b.data<int8_t>(), N, beta, matrix_out->data<int8_t>(),
                 N, relu, bias);
    } else {
      gemm.Sgemm_omp(M, N, K, alpha, matrix_a.data<int8_t>(), K,
                     matrix_b.data<int8_t>(), N, beta,
                     matrix_out->data<int32_t>(), N, relu, bias);
    }
Z
Zhen Wang 已提交
83
#else
84 85 86 87 88 89 90 91 92
    if (bias != nullptr) {
      gemm.Sgemm(M, N, K, alpha, matrix_a.data<int8_t>(), K,
                 matrix_b.data<int8_t>(), N, beta, matrix_out->data<int8_t>(),
                 N, relu, bias);
    } else {
      gemm.Sgemm(M, N, K, alpha, matrix_a.data<int8_t>(), K,
                 matrix_b.data<int8_t>(), N, beta, matrix_out->data<int32_t>(),
                 N, relu, bias);
    }
Z
Zhen Wang 已提交
93
#endif
Z
Zhen Wang 已提交
94 95 96 97 98
  }
}
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile