fc_functor.cc 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2022 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/phi/kernels/funcs/fc_functor.h"

17
#include "paddle/phi/backends/all_context.h"
18
#include "paddle/phi/kernels/funcs/blas/blas.h"
19
#include "paddle/phi/kernels/funcs/jit/kernels.h"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

namespace phi {
namespace funcs {

template <typename DeviceContext, typename T>
void FCFunctor<DeviceContext, T>::operator()(const DeviceContext& context,
                                             const int M,
                                             const int N,
                                             const int K,
                                             const T* X,
                                             const T* W,
                                             T* Y,
                                             const T* B,
                                             bool relu,
                                             bool padding_weights) {
  auto blas = GetBlas<DeviceContext, T>(context);
36
  phi::DenseTensor Y1;
37 38 39 40
  T* Y1_data = nullptr;
  if (padding_weights) {
    const int NN = N + 4;
    const int KK = K + 4;
41
    phi::DenseTensor X1;
42 43 44 45 46
    X1.Resize({M * KK});
    T* X1_data = context.template HostAlloc<T>(&X1);

    Y1.Resize({M * (N + 4)});
    Y1_data = context.template HostAlloc<T>(&Y1);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for
#endif
    for (int i = 0; i < M; i++) {
      memcpy(X1_data + i * KK, X + i * K, K * sizeof(T));
    }
    blas.GEMM(false,
              false,
              M,
              N,
              K,
              static_cast<T>(1.0),
              X1_data,
              KK,
              W,
              NN,
              static_cast<T>(0.0),
              Y1_data,
              NN);
  } else {
    blas.MatMul(M, N, K, X, W, Y);
  }
69
  if (B == nullptr) {
70 71 72 73 74 75 76 77 78 79 80 81 82 83
    if (padding_weights) {
#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for
#endif
      for (int i = 0; i < M; i++) {
        memcpy(Y + i * N, Y1_data + i * (N + 4), N * sizeof(T));
      }
    }
    PADDLE_ENFORCE_EQ(
        relu,
        false,
        errors::PermissionDenied("When bias is NULL, relu can not be true."));
    return;
  }
84 85
  auto compute = relu ? phi::jit::KernelFuncs<phi::jit::VAddReluTuple<T>,
                                              phi::CPUPlace>::Cache()
86
                            .At(N)
87 88
                      : phi::jit::KernelFuncs<phi::jit::VAddTuple<T>,
                                              phi::CPUPlace>::Cache()
89
                            .At(N);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#ifdef PADDLE_WITH_MKLML
#pragma omp parallel for
#endif
  for (int i = 0; i < M; i++) {
    T* dst = Y + i * N;
    T* src = (padding_weights) ? Y1_data + i * (N + 4) : dst;
    compute(B, src, dst, N);
  }
}

template class FCFunctor<CPUContext, float>;
template class FCFunctor<CPUContext, double>;

}  // namespace funcs
}  // namespace phi