modified_huber_loss_op.cu 2.8 KB
Newer Older
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

L
Luo Tao 已提交
3 4 5 6 7 8 9 10 11 12 13
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. */
Y
yangyaming 已提交
14 15
#include <thrust/for_each.h>
#include <thrust/tuple.h>
16 17
#include "paddle/framework/op_registry.h"
#include "paddle/operators/modified_huber_loss_op.h"
Y
yangyaming 已提交
18
#include "paddle/platform/hostdevice.h"
19 20 21 22 23 24

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

Y
yangyaming 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
struct ModifiedHuberLossBackward {
  template <typename Tuple>
  HOSTDEVICE void operator()(Tuple t) const {
    auto inter_val = thrust::get<1>(t);
    auto y_val = thrust::get<2>(t);
    auto out_grad = thrust::get<3>(t);
    if (inter_val < -1) {
      thrust::get<0>(t) = -4 * (2 * y_val - 1) * out_grad;
    } else if (inter_val < 1) {
      thrust::get<0>(t) = -2 * (1 - inter_val) * (2 * y_val - 1) * out_grad;
    } else {
      thrust::get<0>(t) = 0;
    }
  }
};

41
template <typename T>
Y
Yu Yang 已提交
42
class ModifiedHuberLossGradGPUKernel : public framework::OpKernel<T> {
43 44
 public:
  void Compute(const framework::ExecutionContext& context) const override {
Y
yangyaming 已提交
45
    auto* in0 = context.Input<Tensor>("Y");
46
    auto* in1 = context.Input<Tensor>("IntermediateVal");
Y
yangyaming 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    auto* in2 = context.Input<Tensor>(framework::GradVarName("Out"));
    auto* out0 = context.Output<Tensor>(framework::GradVarName("X"));

    if (out0) {
      auto counts = framework::product(in1->dims());
      auto y_ptr = thrust::device_pointer_cast(in0->data<T>());
      auto inter_val_ptr = thrust::device_pointer_cast(in1->data<T>());
      auto out_grad_ptr = thrust::device_pointer_cast(in2->data<T>());
      thrust::device_ptr<T> x_grad_ptr(
          out0->mutable_data<T>(context.GetPlace()));

      auto iter_begin = thrust::make_zip_iterator(
          thrust::make_tuple(x_grad_ptr, inter_val_ptr, y_ptr, out_grad_ptr));

      auto iter_end = thrust::make_zip_iterator(
          thrust::make_tuple(x_grad_ptr + counts, inter_val_ptr + counts,
                             y_ptr + counts, out_grad_ptr + counts));

      thrust::for_each(iter_begin, iter_end, ModifiedHuberLossBackward());
    }
67 68 69 70 71 72 73
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Q
QI JUN 已提交
74
REGISTER_OP_CUDA_KERNEL(
75
    modified_huber_loss,
Q
QI JUN 已提交
76 77 78
    ops::ModifiedHuberLossKernel<paddle::platform::CUDADeviceContext, float>);
REGISTER_OP_CUDA_KERNEL(modified_huber_loss_grad,
                        ops::ModifiedHuberLossGradGPUKernel<float>);