activation_op_mlu.cc 3.5 KB
Newer Older
F
fwenguang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (c) 2021 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 Licnse. */

#include <memory>
#include <string>

#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/operators/activation_op.h"
#include "paddle/fluid/operators/mlu/mlu_baseop.h"
#include "paddle/fluid/platform/device/mlu/device_context.h"
23
#include "paddle/pten/core/ddim.h"
F
fwenguang 已提交
24 25 26 27 28 29

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

30
template <cnnlActivationMode_t act_mode, typename T>
F
fwenguang 已提交
31 32 33 34 35
class ActivationMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("X");
    auto* output = ctx.Output<Tensor>("Out");
36
    float alpha = ctx.HasAttr("alpha") ? ctx.Attr<float>("alpha") : 1.0f;
F
fwenguang 已提交
37 38 39

    output->mutable_data<T>(ctx.GetPlace());

40
    MLUCnnlActivationDesc act_desc(act_mode, alpha);
41 42 43 44
    MLUCnnlTensorDesc input_desc(*input, CNNL_LAYOUT_ARRAY,
                                 ToCnnlDataType(input->dtype()));
    MLUCnnlTensorDesc output_desc(*output, CNNL_LAYOUT_ARRAY,
                                  ToCnnlDataType(output->dtype()));
F
fwenguang 已提交
45

46
    MLUCnnl::Active(ctx, act_desc.get(), input_desc.get(),
F
fwenguang 已提交
47 48 49 50 51 52
                    reinterpret_cast<const void*>(input->data<T>()),
                    output_desc.get(),
                    reinterpret_cast<void*>(output->data<T>()));
  }
};

53
template <cnnlActivationMode_t act_mode, typename T>
F
fwenguang 已提交
54 55 56 57 58 59
class ActivationGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
60
    float alpha = ctx.HasAttr("alpha") ? ctx.Attr<float>("alpha") : 1.0f;
F
fwenguang 已提交
61 62 63

    dx->mutable_data<T>(ctx.GetPlace());

64 65 66 67 68 69
    MLUCnnlTensorDesc dout_desc(*dout, CNNL_LAYOUT_ARRAY,
                                ToCnnlDataType(dout->dtype()));
    MLUCnnlTensorDesc out_desc(*out, CNNL_LAYOUT_ARRAY,
                               ToCnnlDataType(out->dtype()));
    MLUCnnlTensorDesc dx_desc(*dx, CNNL_LAYOUT_ARRAY,
                              ToCnnlDataType(dx->dtype()));
70
    MLUCnnlActivationDesc act_desc(act_mode, alpha);
F
fwenguang 已提交
71
    MLUCnnl::ActiveGrad(
72
        ctx, act_desc.get(), nullptr, nullptr, nullptr, nullptr,
F
fwenguang 已提交
73 74 75 76 77 78 79 80 81 82 83 84
        dout_desc.get(), reinterpret_cast<const void*>(dout->data<T>()),
        out_desc.get(), reinterpret_cast<const void*>(out->data<T>()),
        dx_desc.get(), reinterpret_cast<void*>(dx->data<T>()));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OP_MLU_KERNEL(
85 86
    relu, ops::ActivationMLUKernel<CNNL_ACTIVATION_RELU, float>,
    ops::ActivationMLUKernel<CNNL_ACTIVATION_RELU, paddle::platform::float16>);
F
fwenguang 已提交
87
REGISTER_OP_MLU_KERNEL(
88 89
    relu_grad, ops::ActivationGradMLUKernel<CNNL_ACTIVATION_RELU, float>,
    ops::ActivationGradMLUKernel<CNNL_ACTIVATION_RELU,
F
fwenguang 已提交
90
                                 paddle::platform::float16>);