clip_op_mlu.cc 4.5 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
/* 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 {
25 26
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* out = ctx.Output<phi::DenseTensor>("Out");
27 28 29 30 31

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

    if (ctx.HasInput("Min")) {
32
      phi::DenseTensor min_cpu;
33
      auto* min_tensor = ctx.Input<phi::DenseTensor>("Min");
34 35
      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
        min_data = min_cpu.data<T>();
      }
      min = min_data[0];
    }

    if (ctx.HasInput("Max")) {
44
      phi::DenseTensor max_cpu;
45
      auto* max_tensor = ctx.Input<phi::DenseTensor>("Max");
46 47
      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
  }
};

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

76 77 78 79
    auto* min_tensor =
        ctx.HasInput("Min") ? ctx.Input<phi::DenseTensor>("Min") : nullptr;
    auto* max_tensor =
        ctx.HasInput("Max") ? ctx.Input<phi::DenseTensor>("Max") : nullptr;
80 81 82

    auto min_val = ctx.Attr<float>("min");
    if (min_tensor) {
83
      phi::DenseTensor min_data;
84
      framework::TensorCopy(
85 86 87 88
          *min_tensor,
          platform::CPUPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          &min_data);
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) {
94
      phi::DenseTensor max_data;
95
      framework::TensorCopy(
96 97 98 99
          *max_tensor,
          platform::CPUPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          &max_data);
100 101 102 103 104 105 106 107
      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);

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

}  // namespace operators
}  // namespace paddle

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

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

130 131
REGISTER_OP_MLU_KERNEL(clip_grad,
                       ops::ClipGradMLUKernel<float>,
132
                       ops::ClipGradMLUKernel<plat::float16>);