huber_loss_op.h 4.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
yangyaming 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
Y
Yi Wang 已提交
16 17 18
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/hostdevice.h"
Y
yangyaming 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;

template <typename T>
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) {
35
      return static_cast<T>(0.5) * val * val;
Y
yangyaming 已提交
36
    } else {
37
      return delta * (abs_val - static_cast<T>(0.5) * delta);
Y
yangyaming 已提交
38 39 40 41 42 43
    }
  }

  T delta;
};

Q
QI JUN 已提交
44
template <typename DeviceContext, typename T, typename AttrType = T>
45
class HuberLossKernel : public framework::OpKernel<T> {
Y
yangyaming 已提交
46 47 48 49
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* in0 = context.Input<Tensor>("X");
    auto* in1 = context.Input<Tensor>("Y");
50
    auto* out0 = context.Output<Tensor>("Residual");
Y
yangyaming 已提交
51
    auto* out1 = context.Output<Tensor>("Out");
52
    auto delta = static_cast<T>(context.Attr<AttrType>("delta"));
Q
QI JUN 已提交
53 54
    auto& place =
        *context.template device_context<DeviceContext>().eigen_device();
Y
yangyaming 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68

    auto x = EigenVector<T>::Flatten(*in0);
    auto y = EigenVector<T>::Flatten(*in1);
    out0->mutable_data<T>(context.GetPlace());
    auto residual = EigenVector<T>::Flatten(*out0);
    residual.device(place) = y - x;
    out1->mutable_data<T>(context.GetPlace());
    auto loss = EigenVector<T>::Flatten(*out1);
    loss.device(place) = residual.unaryExpr(HuberLossForward<T>(delta));
  }
};

template <typename T>
struct HuberLossBackward {
69 70
  HOSTDEVICE HuberLossBackward(const T& delta, T sign)
      : sign(sign), delta(delta) {}
Y
yangyaming 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84

  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;
      }
    }
  }

85
  T sign;
Y
yangyaming 已提交
86 87 88
  T delta;
};

Q
QI JUN 已提交
89
template <typename DeviceContext, typename T, typename AttrType = T>
90
class HuberLossGradKernel : public framework::OpKernel<T> {
Y
yangyaming 已提交
91 92
 public:
  void Compute(const framework::ExecutionContext& context) const override {
93
    auto* in0 = context.Input<Tensor>("Residual");
Y
yangyaming 已提交
94 95 96 97
    auto* in1 = context.Input<Tensor>(framework::GradVarName("Out"));
    auto* out0 = context.Output<Tensor>(framework::GradVarName("X"));
    auto* out1 = context.Output<Tensor>(framework::GradVarName("Y"));
    auto delta = static_cast<T>(context.op().Attr<AttrType>("delta"));
Q
QI JUN 已提交
98 99
    auto& place =
        *context.template device_context<DeviceContext>().eigen_device();
Y
yangyaming 已提交
100 101 102 103 104 105 106 107

    auto residual = EigenVector<T>::Flatten(*in0);
    auto out_grad = EigenVector<T>::Flatten(*in1);

    if (out0) {
      out0->mutable_data<T>(context.GetPlace());
      auto x_grad = EigenVector<T>::Flatten(*out0);
      x_grad.device(place) =
108 109
          residual.unaryExpr(HuberLossBackward<T>(delta, -1.0));
      x_grad.device(place) = out_grad * x_grad;
Y
yangyaming 已提交
110 111 112 113 114 115
    }

    if (out1) {
      out1->mutable_data<T>(context.GetPlace());
      auto y_grad = EigenVector<T>::Flatten(*out1);
      y_grad.device(place) =
116 117
          residual.unaryExpr(HuberLossBackward<T>(delta, 1.0));
      y_grad.device(place) = out_grad * y_grad;
Y
yangyaming 已提交
118 119 120 121 122 123
    }
  }
};

}  // namespace operators
}  // namespace paddle