math_function_int8.cpp 3.4 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 {
Z
Zhen Wang 已提交
23 24 25 26

template <>
void matmul(const framework::Tensor &matrix_a, bool trans_a,
            const framework::Tensor &matrix_b, bool trans_b, float alpha,
Z
ZhenWang 已提交
27 28
            framework::Tensor *matrix_out, float beta, bool relu, int32_t *bias,
            bool addOnRow) {
Z
Zhen Wang 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  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 已提交
55
#ifdef _OPENMP
56
    if (bias != nullptr) {
Z
ZhenWang 已提交
57
      gemm.Sgemm_omp(M, N, K, alpha, a, K, matrix_b.data<int8_t>(), N, beta,
Z
ZhenWang 已提交
58
                     matrix_out->data<int8_t>(), N, relu, bias, addOnRow);
59 60
    } else {
      gemm.Sgemm_omp(M, N, K, alpha, a, K, matrix_b.data<int8_t>(), N, beta,
Z
ZhenWang 已提交
61
                     matrix_out->data<int32_t>(), N, relu, bias, addOnRow);
62
    }
Z
Zhen Wang 已提交
63
#else
64 65
    if (bias != nullptr) {
      gemm.Sgemm(M, N, K, alpha, a, K, matrix_b.data<int8_t>(), N, beta,
Z
ZhenWang 已提交
66
                 matrix_out->data<int8_t>(), N, relu, bias, addOnRow);
67 68
    } else {
      gemm.Sgemm(M, N, K, alpha, a, K, matrix_b.data<int8_t>(), N, beta,
Z
ZhenWang 已提交
69
                 matrix_out->data<int32_t>(), N, relu, bias, addOnRow);
70
    }
Z
Zhen Wang 已提交
71
#endif
Z
Zhen Wang 已提交
72
  } else {
Z
Zhen Wang 已提交
73
#ifdef _OPENMP
74
    if (bias != nullptr) {
Z
ZhenWang 已提交
75 76
      gemm.Sgemm_omp(M, N, K, alpha, matrix_a.data<int8_t>(), K,
                     matrix_b.data<int8_t>(), N, beta,
Z
ZhenWang 已提交
77
                     matrix_out->data<int8_t>(), N, relu, bias, addOnRow);
78 79 80
    } else {
      gemm.Sgemm_omp(M, N, K, alpha, matrix_a.data<int8_t>(), K,
                     matrix_b.data<int8_t>(), N, beta,
Z
ZhenWang 已提交
81
                     matrix_out->data<int32_t>(), N, relu, bias, addOnRow);
82
    }
Z
Zhen Wang 已提交
83
#else
84 85 86
    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>(),
Z
ZhenWang 已提交
87
                 N, relu, bias, addOnRow);
88 89 90
    } 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>(),
Z
ZhenWang 已提交
91
                 N, relu, bias, addOnRow);
92
    }
Z
Zhen Wang 已提交
93
#endif
Z
Zhen Wang 已提交
94 95 96 97 98
  }
}
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile