/* 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 class ClipMLUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* x = ctx.Input("X"); auto* out = ctx.Output("Out"); auto min = static_cast(ctx.Attr("min")); auto max = static_cast(ctx.Attr("max")); if (ctx.HasInput("Min")) { Tensor min_cpu; auto* min_tensor = ctx.Input("Min"); auto* min_data = min_tensor->data(); if (platform::is_mlu_place(min_tensor->place())) { paddle::framework::TensorCopySync( *min_tensor, platform::CPUPlace(), &min_cpu); min_data = min_cpu.data(); } min = min_data[0]; } if (ctx.HasInput("Max")) { Tensor max_cpu; auto* max_tensor = ctx.Input("Max"); auto* max_data = max_tensor->data(); if (platform::is_mlu_place(max_tensor->place())) { paddle::framework::TensorCopySync( *max_tensor, platform::CPUPlace(), &max_cpu); max_data = max_cpu.data(); } max = max_data[0]; } out->mutable_data(ctx.GetPlace()); MLUCnnlTensorDesc x_desc(*x); MLUCnnlTensorDesc out_desc(*out); MLUCnnl::Clip(ctx, x_desc.get(), GetBasePtr(x), static_cast(&min), static_cast(&max), GetBasePtr(out)); } }; template class ClipGradMLUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* x = ctx.Input("X"); auto* dout = ctx.Input(framework::GradVarName("Out")); auto* dx = ctx.Output(framework::GradVarName("X")); dx->mutable_data(ctx.GetPlace()); auto* min_tensor = ctx.HasInput("Min") ? ctx.Input("Min") : nullptr; auto* max_tensor = ctx.HasInput("Max") ? ctx.Input("Max") : nullptr; auto min_val = ctx.Attr("min"); if (min_tensor) { Tensor min_data; framework::TensorCopy( *min_tensor, platform::CPUPlace(), ctx.template device_context(), &min_data); ctx.template device_context().Wait(); min_val = static_cast(min_data.data()[0]); } auto max_val = ctx.Attr("max"); if (max_tensor) { Tensor max_data; framework::TensorCopy( *max_tensor, platform::CPUPlace(), ctx.template device_context(), &max_data); ctx.template device_context().Wait(); max_val = static_cast(max_data.data()[0]); } MLUCnnlTensorDesc x_desc(*x); MLUCnnlTensorDesc dx_desc(*dx); MLUCnnlTensorDesc dout_desc(*dout); MLUCnnl::HardtanhBackward(ctx, x_desc.get(), GetBasePtr(x), dout_desc.get(), GetBasePtr(dout), max_val, min_val, dx_desc.get(), GetBasePtr(dx)); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; namespace plat = paddle::platform; REGISTER_OP_MLU_KERNEL(clip, ops::ClipMLUKernel, ops::ClipMLUKernel); REGISTER_OP_MLU_KERNEL(clip_grad, ops::ClipGradMLUKernel, ops::ClipGradMLUKernel);