activation_op_mlu.cc 3.6 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 23 24 25 26 27 28 29
/* 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/ddim.h"
#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"

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 45 46
    MLUCnnlTensorDesc input_desc(
        *input, CNNL_LAYOUT_ARRAY,
        ToCnnlDataType(framework::TransToProtoVarType(input->dtype())));
    MLUCnnlTensorDesc output_desc(
        *output, CNNL_LAYOUT_ARRAY,
        ToCnnlDataType(framework::TransToProtoVarType(output->dtype())));
F
fwenguang 已提交
47

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

55
template <cnnlActivationMode_t act_mode, typename T>
F
fwenguang 已提交
56 57 58 59 60 61
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"));
62
    float alpha = ctx.HasAttr("alpha") ? ctx.Attr<float>("alpha") : 1.0f;
F
fwenguang 已提交
63 64 65

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

66 67 68 69 70 71 72 73 74
    MLUCnnlTensorDesc dout_desc(
        *dout, CNNL_LAYOUT_ARRAY,
        ToCnnlDataType(framework::TransToProtoVarType(dout->dtype())));
    MLUCnnlTensorDesc out_desc(
        *out, CNNL_LAYOUT_ARRAY,
        ToCnnlDataType(framework::TransToProtoVarType(out->dtype())));
    MLUCnnlTensorDesc dx_desc(
        *dx, CNNL_LAYOUT_ARRAY,
        ToCnnlDataType(framework::TransToProtoVarType(dx->dtype())));
75
    MLUCnnlActivationDesc act_desc(act_mode, alpha);
F
fwenguang 已提交
76
    MLUCnnl::ActiveGrad(
77
        ctx, act_desc.get(), nullptr, nullptr, nullptr, nullptr,
F
fwenguang 已提交
78 79 80 81 82 83 84 85 86 87 88 89
        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(
90 91
    relu, ops::ActivationMLUKernel<CNNL_ACTIVATION_RELU, float>,
    ops::ActivationMLUKernel<CNNL_ACTIVATION_RELU, paddle::platform::float16>);
F
fwenguang 已提交
92
REGISTER_OP_MLU_KERNEL(
93 94
    relu_grad, ops::ActivationGradMLUKernel<CNNL_ACTIVATION_RELU, float>,
    ops::ActivationGradMLUKernel<CNNL_ACTIVATION_RELU,
F
fwenguang 已提交
95
                                 paddle::platform::float16>);