cross_entropy_op.h 10.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
Qiao Longfei 已提交
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
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
18
#include "paddle/fluid/platform/for_range.h"
19
#include "paddle/phi/core/tensor_utils.h"
20
#include "paddle/phi/kernels/funcs/cross_entropy.h"
21
#include "paddle/phi/kernels/funcs/math.h"
22
#include "paddle/phi/kernels/funcs/math_function.h"
Q
Qiao Longfei 已提交
23 24 25 26

namespace paddle {
namespace operators {

27
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
28
class CrossEntropyOpKernel : public framework::OpKernel<T> {
29
 public:
D
dongzhihong 已提交
30
  void Compute(const framework::ExecutionContext& ctx) const override {
31 32 33
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* labels = ctx.Input<phi::DenseTensor>("Label");
    auto* y = ctx.Output<phi::DenseTensor>("Y");
34
    y->mutable_data<T>(ctx.GetPlace());
C
caoying03 已提交
35

36
    int rank = x->dims().size();
37
    auto label_dims = labels->dims();
38
    phi::DenseTensor x_2d = phi::ReshapeToMatrix(*x, rank - 1);
39
    phi::DenseTensor labels_2d, y_2d;
40 41
    if (label_dims.size() < rank) {
      labels_2d.ShareDataWith(*labels);
42
      labels_2d.Resize({phi::product(label_dims), 1});
43 44

      y_2d.ShareDataWith(*y);
45
      y_2d.Resize({phi::product(y->dims()), 1});
46 47

    } else {
48 49
      labels_2d = phi::ReshapeToMatrix(*labels, rank - 1);
      y_2d = phi::ReshapeToMatrix(*y, rank - 1);
50
    }
51

52
    int axis_dim = x->dims()[rank - 1];
53
    phi::funcs::CrossEntropyFunctor<DeviceContext, T>()(
54 55 56 57 58 59 60
        ctx.template device_context<DeviceContext>(),
        &y_2d,
        &x_2d,
        &labels_2d,
        ctx.Attr<bool>("soft_label"),
        ctx.Attr<int>("ignore_index"),
        axis_dim);
Y
Yan Chunwei 已提交
61 62 63
  }
};

64
template <typename T>
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
class XeSoftlabelGradFunctor {
 public:
  XeSoftlabelGradFunctor(T* dx,
                         const T* dy,     // NOLINT
                         const T* x,      // NOLINT
                         const T* label,  // NOLINT
                         size_t num_classes)
      : dx_(dx), dy_(dy), x_(x), label_(label), num_classes_(num_classes) {}

  HOSTDEVICE void operator()(size_t i) {
    auto row_ids = i / num_classes_;
    dx_[i] = -label_[i] * dy_[row_ids] / x_[i];
  }

 private:
  T* dx_;
  const T* dy_;
  const T* x_;
  const T* label_;
  size_t num_classes_;
};

template <typename T>
class XeGradFunctor {
 public:
  XeGradFunctor(T* dx,
                const T* dy,           // NOLINT
                const T* x,            // NOLINT
                const int64_t* label,  // NOLINT
94 95
                size_t num_classes,
                size_t ignore_index)
96 97 98 99 100 101
      : dx_(dx),
        dy_(dy),
        x_(x),
        label_(label),
        num_classes_(num_classes),
        ignore_index_(ignore_index) {}
102

Y
Yu Yang 已提交
103 104 105
  HOSTDEVICE void operator()(size_t sample_id) {
    auto x_is_true_offset = sample_id * num_classes_ + label_[sample_id];
    for (size_t x_offset = sample_id * num_classes_;
106 107
         x_offset < (sample_id + 1) * num_classes_;
         ++x_offset) {
C
chengduoZH 已提交
108 109 110 111
      dx_[x_offset] = (x_offset != x_is_true_offset ||
                       label_[sample_id] == static_cast<int64_t>(ignore_index_))
                          ? static_cast<T>(0)
                          : -dy_[sample_id] / x_[x_offset];
112 113 114 115 116 117 118 119 120
    }
  }

 private:
  T* dx_;
  const T* dy_;
  const T* x_;
  const int64_t* label_;
  size_t num_classes_;
121
  size_t ignore_index_;
122 123 124
};

template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
125
class CrossEntropyGradientOpKernel : public framework::OpKernel<T> {
Y
Yan Chunwei 已提交
126
 public:
D
dongzhihong 已提交
127
  void Compute(const framework::ExecutionContext& ctx) const override {
128 129 130 131
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* dy = ctx.Input<phi::DenseTensor>(framework::GradVarName("Y"));
    auto* label = ctx.Input<phi::DenseTensor>("Label");
    auto* dx = ctx.Output<phi::DenseTensor>(framework::GradVarName("X"));
132
    T* dx_data = dx->mutable_data<T>(ctx.GetPlace());
Y
Yan Chunwei 已提交
133

134 135 136 137
    // Following computation only depends on the last dimension size. So it's
    // unnecessary to convert tensors to 2-D views.
    int rank = x->dims().size();
    int64_t class_num = x->dims()[rank - 1];
138
    int64_t ignore_index = ctx.Attr<int>("ignore_index");
139
    if (ctx.Attr<bool>("soft_label")) {
140 141 142
      XeSoftlabelGradFunctor<T> functor(dx_data,
                                        dy->data<T>(),
                                        x->data<T>(),
143 144 145 146 147 148
                                        label->data<T>(),
                                        static_cast<size_t>(class_num));
      platform::ForRange<DeviceContext> for_range(
          ctx.template device_context<DeviceContext>(),
          static_cast<size_t>(dx->numel()));
      for_range(functor);
149
    } else {
150 151 152 153 154 155
      XeGradFunctor<T> functor(dx_data,
                               dy->data<T>(),
                               x->data<T>(),
                               label->data<int64_t>(),
                               static_cast<size_t>(class_num),
                               static_cast<size_t>(ignore_index));
156 157 158 159
      platform::ForRange<DeviceContext> for_range(
          ctx.template device_context<DeviceContext>(),
          static_cast<size_t>(dy->numel()));
      for_range(functor);
Q
Qiao Longfei 已提交
160 161 162 163
    }
  }
};

S
sneaxiy 已提交
164 165
template <typename T>
struct HardLabelCrossEntropyForwardFunctor {
166 167 168
  HardLabelCrossEntropyForwardFunctor(const T* x,
                                      T* y,
                                      T* match_x,
S
sneaxiy 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181
                                      const int64_t* label,
                                      int64_t ignore_index,
                                      int64_t feature_size)
      : x_(x),
        y_(y),
        match_x_(match_x),
        label_(label),
        ignore_index_(ignore_index),
        feature_size_(feature_size) {}

  HOSTDEVICE void operator()(int64_t idx) const {
    auto label = label_[idx];
    if (label != ignore_index_) {
182 183
      // don't update to PADDLE_ENFORCE_GE and PADDLE_ENFORCE_LT cause
      // can't use platform::errors::InvalidArgument in HOSTDEVICE
184 185 186 187
      PADDLE_ENFORCE(label >= 0 && label < feature_size_,
                     "Variable value (label) of "
                     "OP(fluid.layers.cross_entropy) expected >= 0 "
                     "and < %ld, but got %ld. Please check label value.",
188 189
                     feature_size_,
                     label);
190

S
sneaxiy 已提交
191
      auto match_x = x_[idx * feature_size_ + label];
192
      y_[idx] = -phi::funcs::TolerableValue<T>()(phi::funcs::real_log(match_x));
S
sneaxiy 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      match_x_[idx] = match_x;
    } else {
      y_[idx] = 0;
      match_x_[idx] = 0;  // any value is ok
    }
  }

  const T* x_;
  T* y_;
  T* match_x_;
  const int64_t* label_;
  int64_t ignore_index_;
  int64_t feature_size_;
};

S
sneaxiy 已提交
208 209
template <typename T>
struct HardLabelCrossEntropyBackwardFunctor {
210 211 212
  HardLabelCrossEntropyBackwardFunctor(T* dx,
                                       const T* dy,
                                       const T* match_x,
S
sneaxiy 已提交
213 214 215 216 217
                                       const int64_t* label,
                                       int64_t ignore_index,
                                       int64_t feature_size)
      : dx_(dx),
        dy_(dy),
S
sneaxiy 已提交
218
        match_x_(match_x),
S
sneaxiy 已提交
219 220 221 222 223 224 225 226 227
        label_(label),
        ignore_index_(ignore_index),
        feature_size_(feature_size) {}

  HOSTDEVICE void operator()(int64_t idx) const {
    auto row_idx = idx / feature_size_;
    auto col_idx = idx % feature_size_;
    auto label = label_[row_idx];
    if (label == col_idx && label != ignore_index_) {
S
sneaxiy 已提交
228
      dx_[idx] = -dy_[row_idx] / match_x_[row_idx];
S
sneaxiy 已提交
229 230 231 232 233 234 235
    } else {
      dx_[idx] = 0;
    }
  }

  T* dx_;
  const T* dy_;
S
sneaxiy 已提交
236
  const T* match_x_;
S
sneaxiy 已提交
237 238 239 240 241 242 243 244 245
  const int64_t* label_;
  int64_t ignore_index_;
  int64_t feature_size_;
};

template <typename DeviceContext, typename T>
class CrossEntropyOpKernel2 : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
246 247 248 249
    auto* x = ctx.Input<phi::DenseTensor>("X");
    auto* label = ctx.Input<phi::DenseTensor>("Label");
    auto* y = ctx.Output<phi::DenseTensor>("Y");
    auto* match_x = ctx.Output<phi::DenseTensor>("MatchX");
S
sneaxiy 已提交
250 251 252

    auto& x_dims = x->dims();
    auto feature_size = x_dims[x_dims.size() - 1];
253
    auto batch_size = phi::product(x->dims()) / feature_size;
S
sneaxiy 已提交
254 255 256 257 258

    auto* p_x = x->data<T>();
    auto* p_label = label->data<int64_t>();
    auto* p_y = y->mutable_data<T>(ctx.GetPlace());
    auto* p_match_x = match_x->mutable_data<T>(ctx.GetPlace());
S
sneaxiy 已提交
259 260 261

    auto ignore_index = ctx.Attr<int>("ignore_index");

S
sneaxiy 已提交
262 263 264 265
    platform::ForRange<DeviceContext> for_range(
        ctx.template device_context<DeviceContext>(), batch_size);
    for_range(HardLabelCrossEntropyForwardFunctor<T>(
        p_x, p_y, p_match_x, p_label, ignore_index, feature_size));
S
sneaxiy 已提交
266 267 268 269 270 271 272
  }
};

template <typename DeviceContext, typename T>
class CrossEntropyGradientOpKernel2 : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
273 274 275 276
    auto* dx = ctx.Output<phi::DenseTensor>(framework::GradVarName("X"));
    auto* dy = ctx.Input<phi::DenseTensor>(framework::GradVarName("Y"));
    auto* match_x = ctx.Input<phi::DenseTensor>("MatchX");
    auto* label = ctx.Input<phi::DenseTensor>("Label");
S
sneaxiy 已提交
277 278 279

    auto* p_dx = dx->mutable_data<T>(ctx.GetPlace());
    auto* p_dy = dy->data<T>();
S
sneaxiy 已提交
280
    auto* p_match_x = match_x->data<T>();
S
sneaxiy 已提交
281 282 283 284 285
    auto* p_label = label->data<int64_t>();

    int64_t ignore_index = ctx.Attr<int>("ignore_index");
    int rank = dx->dims().size();
    int64_t feature_size = dx->dims()[rank - 1];
286
    int64_t batch_size = phi::product(dx->dims()) / feature_size;
S
sneaxiy 已提交
287 288 289 290 291

    platform::ForRange<DeviceContext> for_range(
        ctx.template device_context<DeviceContext>(),
        batch_size * feature_size);
    for_range(HardLabelCrossEntropyBackwardFunctor<T>(
S
sneaxiy 已提交
292
        p_dx, p_dy, p_match_x, p_label, ignore_index, feature_size));
S
sneaxiy 已提交
293 294 295
  }
};

Q
Qiao Longfei 已提交
296 297
}  // namespace operators
}  // namespace paddle