fc.cc 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Copyright (c) 2016 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 "paddle/fluid/operators/math/fc.h"
#include "paddle/fluid/operators/jit/kernels.h"
#include "paddle/fluid/operators/math/blas.h"

namespace paddle {
namespace operators {
namespace math {

template <typename T>
class FCFunctor<platform::CPUDeviceContext, T> {
 public:
  void operator()(const platform::CPUDeviceContext& context, const int M,
                  const int N, const int K, const T* X, const T* W, T* Y,
28 29
                  const T* B = nullptr, bool relu = false,
                  bool padding_weights = false) {
30
    auto blas = math::GetBlas<platform::CPUDeviceContext, T>(context);
31 32
    framework::Tensor Y1;
    T* Y1_data = nullptr;
33 34
    auto padding = N % 128 == 0 && K % 128 == 0;
    if (padding) {
35 36 37
      const int NN = N + 4;
      const int KK = K + 4;
      framework::Tensor X1;
38 39
      T* X1_data = X1.mutable_data<T>({M * KK}, platform::CPUPlace());
      Y1_data = Y1.mutable_data<T>({M * (N + 4)}, platform::CPUPlace());
40 41 42 43
#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for
#endif
      for (int i = 0; i < M; i++) {
44
        memcpy(X1_data + i * KK, X + i * K, K * sizeof(T));
45 46 47 48
      }
      framework::Tensor W1;
      T* W1_data = nullptr;
      if (!padding_weights) {
49
        W1_data = W1.mutable_data<T>({(K + 4) * (N + 4)}, platform::CPUPlace());
50 51 52 53
#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for
#endif
        for (int i = 0; i < K; i++) {
54
          memcpy(W1_data + i * NN, W + i * N, N * sizeof(T));
55 56 57 58 59 60 61 62
        }
      }
      blas.GEMM(false, false, M, N, K, static_cast<T>(1.0), X1_data, KK,
                (padding_weights ? W : W1_data), NN, static_cast<T>(0.0),
                Y1_data, NN);
    } else {
      blas.MatMul(M, N, K, X, W, Y);
    }
63
    if (B == NULL) {
64
      if (padding) {
65 66 67 68
#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for
#endif
        for (int i = 0; i < M; i++) {
69
          memcpy(Y + i * N, Y1_data + i * (N + 4), N * sizeof(T));
70 71 72 73 74
        }
      }
      PADDLE_ENFORCE_EQ(relu, false,
                        platform::errors::PermissionDenied(
                            "When bias is NULL, relu can not be true."));
75 76 77 78 79 80 81 82
      return;
    }
    if (relu) {
      auto compute =
          jit::KernelFuncs<jit::VAddReluTuple<T>, platform::CPUPlace>::Cache()
              .At(N);
      for (int i = 0; i < M; i++) {
        T* dst = Y + i * N;
83
        T* src = (padding) ? Y1_data + i * (N + 4) : dst;
84
        compute(B, src, dst, N);
85 86 87 88 89 90 91 92 93 94
      }
    } else {
      auto compute =
          jit::KernelFuncs<jit::VAddTuple<T>, platform::CPUPlace>::Cache().At(
              N);
#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for
#endif
      for (int i = 0; i < M; i++) {
        T* dst = Y + i * N;
95
        T* src = (padding) ? Y1_data + i * (N + 4) : dst;
96
        compute(B, src, dst, N);
97 98 99 100 101 102 103 104 105 106 107
      }
    }
  }
};

template class FCFunctor<platform::CPUDeviceContext, float>;
template class FCFunctor<platform::CPUDeviceContext, double>;

}  // namespace math
}  // namespace operators
}  // namespace paddle