EigenGemm.cpp 3.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
H
hedaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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 <glog/logging.h>
16
#include "paddle/function/EigenThreadDevice.h"
H
hedaoyuan 已提交
17 18 19 20 21 22 23

namespace paddle {

template <class T>
struct EigenBlasGemm {
  typedef Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, int>,
                           Eigen::Aligned>
24
      EigenMatrix;
H
hedaoyuan 已提交
25 26 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 53 54 55 56 57 58

  static void compute(const bool transA,
                      const bool transB,
                      const int M,
                      const int N,
                      const int K,
                      const T alpha,
                      const T* A,
                      const int lda,
                      const T* B,
                      const int ldb,
                      const T beta,
                      T* C,
                      const int ldc) {
    Eigen::array<int, 2> sizeA;
    if (transA) {
      sizeA[0] = K;
      sizeA[1] = M;
      CHECK_EQ(M, lda);
    } else {
      sizeA[0] = M;
      sizeA[1] = K;
      CHECK_EQ(K, lda);
    }
    Eigen::array<int, 2> sizeB;
    if (transB) {
      sizeB[0] = N;
      sizeB[1] = K;
      CHECK_EQ(K, ldb);
    } else {
      sizeB[0] = K;
      sizeB[1] = N;
      CHECK_EQ(N, ldb);
    }
59 60 61
    Eigen::array<int, 2> sizeC = {{M, ldc}};
    Eigen::array<int, 2> offsetC = {{0, 0}};
    Eigen::array<int, 2> extentC = {{M, N}};
H
hedaoyuan 已提交
62

63 64 65
    const EigenMatrix a(const_cast<T*>(A), sizeA);
    const EigenMatrix b(const_cast<T*>(B), sizeB);
    EigenMatrix c(C, sizeC);
H
hedaoyuan 已提交
66 67 68 69 70 71 72

    typedef typename Eigen::Tensor<T, 2>::DimensionPair DimPair;
    Eigen::array<DimPair, 1> dims;
    dims[0] = DimPair(1, 0);
    dims[0].first = transA ? 0 : 1;
    dims[0].second = transB ? 1 : 0;

73
    auto* device = EigenDeviceWarpper::device();
74 75
    if (N == ldc) {
      if (alpha == T(1) && beta == T(0)) {
76
        c.device(*device) = a.contract(b, dims);
77
      } else if (alpha == T(1) && beta == T(1)) {
78
        c.device(*device) += a.contract(b, dims);
79
      } else {
80
        c.device(*device) = alpha * a.contract(b, dims) + beta * c;
81
      }
H
hedaoyuan 已提交
82
    } else {
83
      if (alpha == T(1) && beta == T(0)) {
84
        c.slice(offsetC, extentC).device(*device) = a.contract(b, dims);
85
      } else if (alpha == T(1) && beta == T(1)) {
86
        c.slice(offsetC, extentC).device(*device) += a.contract(b, dims);
87
      } else {
88
        c.slice(offsetC, extentC).device(*device) =
89 90
            alpha * a.contract(b, dims) + beta * c.slice(offsetC, extentC);
      }
H
hedaoyuan 已提交
91
    }
92
    EigenDeviceWarpper::free_device(device);
H
hedaoyuan 已提交
93 94 95 96
  }
};

#ifdef PADDLE_TYPE_DOUBLE
97
template struct EigenBlasGemm<double>;
H
hedaoyuan 已提交
98
#else
99
template struct EigenBlasGemm<float>;
H
hedaoyuan 已提交
100 101 102
#endif

}  // namespace paddle