gelu_op_npu.cc 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 License. */

#include <memory>
#include <string>

18 19 20
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/tensor.h"
21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class GeluNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
29
    auto* x = ctx.Input<phi::DenseTensor>("X");
30

31
    auto* out = ctx.Output<phi::DenseTensor>("Out");
32 33 34 35 36 37 38 39 40

    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

L
Leo Chen 已提交
41
    const auto& runner = NpuOpRunner("Gelu", {*x}, {*out}, {});
42 43 44 45 46 47 48 49
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class GeluGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
50 51
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* dout = ctx.Input<phi::DenseTensor>(framework::GradVarName("Out"));
52

53
    auto* dx = ctx.Output<phi::DenseTensor>(framework::GradVarName("X"));
54 55 56 57 58 59 60 61 62

    auto place = ctx.GetPlace();

    dx->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

63 64 65 66 67 68
    // NOTE(pangyoki): In the original implementation of GeluGrad op, the input
    // is {*dout, *x, out}, where out = Gelu(x). However, we find that variable
    // `out` was not actually used. In order to improve performance, the
    // useless GELU operation was deleted.
    // We directly use `*dout` as a placeholder to replace `out`, it will not
    // be used in calculations.
L
Leo Chen 已提交
69
    const auto& runner_dx =
70
        NpuOpRunner("GeluGrad", {*dout, *x, *dout}, {*dx}, {});
L
Leo Chen 已提交
71
    runner_dx.Run(stream);
72 73 74 75 76 77 78 79 80
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OP_NPU_KERNEL(
81 82
    gelu,
    ops::GeluNPUKernel<paddle::platform::NPUDeviceContext, float>,
83 84 85 86 87 88 89 90
    ops::GeluNPUKernel<paddle::platform::NPUDeviceContext,
                       paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    gelu_grad,
    ops::GeluGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::GeluGradNPUKernel<paddle::platform::NPUDeviceContext,
                           paddle::platform::float16>);