/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. 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. */ #pragma once #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" #include "paddle/platform/hostdevice.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template using EigenVector = framework::EigenVector; template struct HuberLossForward { HOSTDEVICE HuberLossForward(const T& delta) : delta(delta) {} HOSTDEVICE T operator()(const T& val) const { T abs_val = std::abs(val); if (abs_val <= delta) { return 0.5 * val * val; } else { return delta * (abs_val - 0.5 * delta); } } T delta; }; template class HuberLossKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* in0 = context.Input("X"); auto* in1 = context.Input("Y"); auto* out0 = context.Output("Residual"); auto* out1 = context.Output("Out"); auto delta = static_cast(context.Attr("delta")); auto place = context.GetEigenDevice(); auto x = EigenVector::Flatten(*in0); auto y = EigenVector::Flatten(*in1); out0->mutable_data(context.GetPlace()); auto residual = EigenVector::Flatten(*out0); residual.device(place) = y - x; out1->mutable_data(context.GetPlace()); auto loss = EigenVector::Flatten(*out1); loss.device(place) = residual.unaryExpr(HuberLossForward(delta)); } }; template struct HuberLossBackward { HOSTDEVICE HuberLossBackward(const T& delta, T sign) : sign(sign), delta(delta) {} HOSTDEVICE T operator()(const T& val) const { T abs_val = std::abs(val); if (abs_val <= delta) { return sign * val; } else { if (val > 0) { return sign * delta; } else { return -1 * sign * delta; } } } T sign; T delta; }; template class HuberLossGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { auto* in0 = context.Input("Residual"); auto* in1 = context.Input(framework::GradVarName("Out")); auto* out0 = context.Output(framework::GradVarName("X")); auto* out1 = context.Output(framework::GradVarName("Y")); auto delta = static_cast(context.op().Attr("delta")); auto place = context.GetEigenDevice(); auto residual = EigenVector::Flatten(*in0); auto out_grad = EigenVector::Flatten(*in1); if (out0) { out0->mutable_data(context.GetPlace()); auto x_grad = EigenVector::Flatten(*out0); x_grad.device(place) = out_grad * residual.unaryExpr(HuberLossBackward(delta, -1.0)); } if (out1) { out1->mutable_data(context.GetPlace()); auto y_grad = EigenVector::Flatten(*out1); y_grad.device(place) = out_grad * residual.unaryExpr(HuberLossBackward(delta, 1.0)); } } }; } // namespace operators } // namespace paddle