activation_mkldnn_op.cc 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.

   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/activation_op.h"
16
#include "paddle/fluid/operators/mkldnn/softplus_mkldnn_op.h"
17
#include "paddle/fluid/platform/mkldnn_reuse.h"
18

19
namespace phi {
20
class DenseTensor;
21
}  // namespace phi
22

23 24 25
namespace paddle {
namespace operators {

26 27 28
using dnnl::memory;
using dnnl::primitive;
using dnnl::stream;
29 30
using framework::DataLayout;
using framework::Tensor;
31 32 33
using platform::GetMKLDNNFormat;
using platform::MKLDNNDeviceContext;
using platform::to_void_cast;
34

35 36 37 38 39 40 41 42 43
template <typename Functor>
class MKLDNNActivationKernel
    : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
    Functor functor;
    functor(ctx);
  }
};
K
Krzysztof Binias 已提交
44

45 46 47 48 49 50 51
template <typename T>
struct SoftplusMKLDNNFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    custom_softplus_eltwise_forward<T>(ctx);
  }
};

52 53 54 55 56
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

57 58 59 60 61 62 63 64 65
#define REGISTER_FWD_ACTIVATION_MKLDNN_KERNEL(act_type, functor) \
  REGISTER_OP_KERNEL(                                            \
      act_type,                                                  \
      MKLDNN,                                                    \
      ::paddle::platform::CPUPlace,                              \
      ops::MKLDNNActivationKernel<ops::functor<float>>,          \
      ops::MKLDNNActivationKernel<ops::functor<paddle::platform::bfloat16>>);

REGISTER_FWD_ACTIVATION_MKLDNN_KERNEL(softplus, SoftplusMKLDNNFunctor);