clip_op_mlu.cc 4.4 KB
Newer Older
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 30 31 32 33 34 35
/* 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/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/mlu/mlu_baseop.h"

namespace paddle {
namespace operators {

template <typename T>
class ClipMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");

    auto min = static_cast<T>(ctx.Attr<float>("min"));
    auto max = static_cast<T>(ctx.Attr<float>("max"));

    if (ctx.HasInput("Min")) {
      Tensor min_cpu;
      auto* min_tensor = ctx.Input<Tensor>("Min");
      auto* min_data = min_tensor->data<T>();
      if (platform::is_mlu_place(min_tensor->place())) {
36 37
        paddle::framework::TensorCopySync(
            *min_tensor, platform::CPUPlace(), &min_cpu);
38 39 40 41 42 43 44 45 46 47
        min_data = min_cpu.data<T>();
      }
      min = min_data[0];
    }

    if (ctx.HasInput("Max")) {
      Tensor max_cpu;
      auto* max_tensor = ctx.Input<Tensor>("Max");
      auto* max_data = max_tensor->data<T>();
      if (platform::is_mlu_place(max_tensor->place())) {
48 49
        paddle::framework::TensorCopySync(
            *max_tensor, platform::CPUPlace(), &max_cpu);
50 51 52 53 54 55 56 57
        max_data = max_cpu.data<T>();
      }
      max = max_data[0];
    }
    out->mutable_data<T>(ctx.GetPlace());

    MLUCnnlTensorDesc x_desc(*x);
    MLUCnnlTensorDesc out_desc(*out);
58 59 60
    MLUCnnl::Clip(ctx,
                  x_desc.get(),
                  GetBasePtr(x),
61
                  static_cast<const void*>(&min),
62 63
                  static_cast<const void*>(&max),
                  GetBasePtr(out));
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  }
};

template <typename T>
class ClipGradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    dx->mutable_data<T>(ctx.GetPlace());

    auto* min_tensor = ctx.HasInput("Min") ? ctx.Input<Tensor>("Min") : nullptr;
    auto* max_tensor = ctx.HasInput("Max") ? ctx.Input<Tensor>("Max") : nullptr;

    auto min_val = ctx.Attr<float>("min");
    if (min_tensor) {
      Tensor min_data;
      framework::TensorCopy(
83 84 85 86
          *min_tensor,
          platform::CPUPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          &min_data);
87 88 89 90 91 92 93
      ctx.template device_context<paddle::platform::MLUDeviceContext>().Wait();
      min_val = static_cast<float>(min_data.data<T>()[0]);
    }
    auto max_val = ctx.Attr<float>("max");
    if (max_tensor) {
      Tensor max_data;
      framework::TensorCopy(
94 95 96 97
          *max_tensor,
          platform::CPUPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          &max_data);
98 99 100 101 102 103 104 105
      ctx.template device_context<paddle::platform::MLUDeviceContext>().Wait();
      max_val = static_cast<float>(max_data.data<T>()[0]);
    }

    MLUCnnlTensorDesc x_desc(*x);
    MLUCnnlTensorDesc dx_desc(*dx);
    MLUCnnlTensorDesc dout_desc(*dout);

106 107 108 109 110 111 112 113
    MLUCnnl::HardtanhBackward(ctx,
                              x_desc.get(),
                              GetBasePtr(x),
                              dout_desc.get(),
                              GetBasePtr(dout),
                              max_val,
                              min_val,
                              dx_desc.get(),
114 115 116 117 118 119 120 121 122 123
                              GetBasePtr(dx));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;

124 125
REGISTER_OP_MLU_KERNEL(clip,
                       ops::ClipMLUKernel<float>,
126 127
                       ops::ClipMLUKernel<plat::float16>);

128 129
REGISTER_OP_MLU_KERNEL(clip_grad,
                       ops::ClipGradMLUKernel<float>,
130
                       ops::ClipGradMLUKernel<plat::float16>);