modified_huber_loss_op.h 3.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
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
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14 15 16

#pragma once

Y
Yi Wang 已提交
17 18
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
19
#include "paddle/phi/core/hostdevice.h"
20 21 22 23

namespace paddle {
namespace operators {

24 25
template <typename T,
          int MajorType = Eigen::RowMajor,
26 27 28 29 30 31
          typename IndexType = Eigen::DenseIndex>
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;

template <typename T>
struct CheckLabelValue {
  HOSTDEVICE T operator()(const T& val) const {
32
    PADDLE_ENFORCE_EQ(
33 34
        val == static_cast<T>(0) || val == static_cast<T>(1),
        true,
35 36 37 38
        platform::errors::InvalidArgument(
            "Input(label) value of modified_huber_loss_op expected to be 0 "
            "or 1, but got %ld. Please check label value.",
            val));
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  }
};

template <typename T>
struct ModifiedHuberLossForward {
  HOSTDEVICE T operator()(const T& val) const {
    if (val < -1) {
      return -4 * val;
    } else if (val < 1) {
      return (1 - val) * (1 - val);
    } else {
      return static_cast<T>(0);
    }
  }
};

H
huangjiyi 已提交
55
template <typename T, typename DeviceContext>
Y
Yu Yang 已提交
56
class ModifiedHuberLossKernel : public framework::OpKernel<T> {
57 58
 public:
  void Compute(const framework::ExecutionContext& context) const override {
59 60 61 62
    auto* in0 = context.Input<phi::DenseTensor>("X");
    auto* in1 = context.Input<phi::DenseTensor>("Y");
    auto* out0 = context.Output<phi::DenseTensor>("IntermediateVal");
    auto* out1 = context.Output<phi::DenseTensor>("Out");
63 64 65

    out0->mutable_data<T>(context.GetPlace());
    out1->mutable_data<T>(context.GetPlace());
Q
QI JUN 已提交
66 67
    auto& place =
        *context.template device_context<DeviceContext>().eigen_device();
68 69 70 71 72 73 74 75 76 77 78 79 80 81

    auto x = EigenVector<T>::Flatten(*in0);
    auto y = EigenVector<T>::Flatten(*in1);
    // make sure value's of Y in {0, 1}
    y.unaryExpr(CheckLabelValue<T>());
    auto inter_val = EigenVector<T>::Flatten(*out0);
    // scale y to {-1, +1} and compute x * y
    inter_val.device(place) = x * (2 * y - static_cast<T>(1));
    auto loss = EigenVector<T>::Flatten(*out1);
    loss.device(place) = inter_val.unaryExpr(ModifiedHuberLossForward<T>());
  }
};

// CPU backward kernel
H
huangjiyi 已提交
82
template <typename T, typename DeviceContext>
Y
Yu Yang 已提交
83
class ModifiedHuberLossGradCPUKernel : public framework::OpKernel<T> {
84 85
 public:
  void Compute(const framework::ExecutionContext& context) const override {
86 87 88 89
    auto* in0 = context.Input<phi::DenseTensor>("Y");
    auto* in1 = context.Input<phi::DenseTensor>("IntermediateVal");
    auto* in2 = context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
    auto* out0 = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
90 91

    if (out0) {
Y
yangyaming 已提交
92 93 94
      const T* y_ptr = in0->data<T>();
      const T* inter_val_ptr = in1->data<T>();
      const T* out_grad_ptr = in2->data<T>();
95
      size_t counts = static_cast<size_t>(phi::product(in1->dims()));
Y
yangyaming 已提交
96 97 98 99 100 101 102 103 104 105
      T* x_grad_ptr = out0->mutable_data<T>(context.GetPlace());
      for (size_t i = 0; i < counts; ++i) {
        if (inter_val_ptr[i] < -1) {
          x_grad_ptr[i] = -4 * (2 * y_ptr[i] - 1) * out_grad_ptr[i];
        } else if (inter_val_ptr[i] < 1) {
          x_grad_ptr[i] = -2 * (1 - inter_val_ptr[i]) * (2 * y_ptr[i] - 1) *
                          out_grad_ptr[i];
        } else {
          x_grad_ptr[i] = 0;
        }
106 107 108 109 110 111 112
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle