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

Q
Qiao Longfei 已提交
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

Q
Qiao Longfei 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

Q
Qiao Longfei 已提交
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

#pragma once
Y
Yi Wang 已提交
16 17
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/softmax.h"
18 19 20 21

namespace paddle {
namespace operators {

D
dongzhihong 已提交
22
using Tensor = framework::Tensor;
23
using DDim = framework::DDim;
D
dongzhihong 已提交
24

25 26 27 28 29 30
static inline int CanonicalAxis(const int axis, const int rank) {
  if (axis < 0) {
    return axis + rank;
  }
  return axis;
}
31

32 33 34 35
static inline int SizeToAxis(const int axis, DDim dims) {
  int size = 1;
  for (int i = 0; i < axis; i++) {
    size *= dims[i];
36
  }
37 38
  return size;
}
39

40 41 42 43
static inline int SizeFromAxis(const int axis, DDim dims) {
  int size = 1;
  for (int i = axis; i < dims.size(); i++) {
    size *= dims[i];
44
  }
45
  return size;
46 47
}

48 49 50 51 52 53 54 55
static inline int SizeOutAxis(const int axis, DDim dims) {
  int size = 1;
  for (int i = axis + 1; i < dims.size(); i++) {
    size *= dims[i];
  }
  return size;
}

Q
QI JUN 已提交
56
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
57
class SoftmaxKernel : public framework::OpKernel<T> {
58
 public:
D
dongzhihong 已提交
59
  void Compute(const framework::ExecutionContext& context) const override {
60
    auto* X = context.Input<Tensor>("X");
F
fengjiayi 已提交
61
    auto* Out = context.Output<Tensor>("Out");
62 63 64
    const int rank = X->dims().size();
    const int axis = CanonicalAxis(context.Attr<int>("axis"), rank);
    int axis_dim = X->dims()[axis];
Q
qijun 已提交
65

C
caoying03 已提交
66
    // allocate memory on device.
F
fengjiayi 已提交
67
    Out->mutable_data<T>(context.GetPlace());
68 69 70
    if (Out->numel() == 0) {
      return;
    }
Q
qijun 已提交
71

72 73
    const int n = SizeToAxis(axis, X->dims());
    const int d = SizeFromAxis(axis, X->dims());
D
dengkaipeng 已提交
74
    Tensor X_2d, Out_2d;
75 76
    X_2d.ShareDataWith(*X).Resize({n, d});
    Out_2d.ShareDataWith(*Out).Resize({n, d});
77

78
#ifdef PADDLE_ON_INFERENCE
J
Jacek Czaja 已提交
79
    math::SoftmaxFunctor<DeviceContext, T, true>()(
D
dengkaipeng 已提交
80 81
        context.template device_context<DeviceContext>(), axis_dim, &X_2d,
        &Out_2d);
82 83
#else
    math::SoftmaxFunctor<DeviceContext, T, false>()(
D
dengkaipeng 已提交
84 85
        context.template device_context<DeviceContext>(), axis_dim, &X_2d,
        &Out_2d);
86
#endif
87 88
  }
};
Q
Qiao Longfei 已提交
89

Q
QI JUN 已提交
90
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
91
class SoftmaxGradKernel : public framework::OpKernel<T> {
92
 public:
D
dongzhihong 已提交
93
  void Compute(const framework::ExecutionContext& context) const override {
F
fengjiayi 已提交
94 95
    auto* Out = context.Input<Tensor>("Out");
    auto* dOut = context.Input<Tensor>(framework::GradVarName("Out"));
96
    auto* dX = context.Output<Tensor>(framework::GradVarName("X"));
97 98 99
    const int rank = dX->dims().size();
    const int axis = CanonicalAxis(context.Attr<int>("axis"), rank);
    int axis_dim = dX->dims()[axis];
Q
Qiao Longfei 已提交
100

101 102
    // allocate memory on device.
    dX->mutable_data<T>(context.GetPlace());
103 104 105
    if (dX->numel() == 0) {
      return;
    }
Q
Qiao Longfei 已提交
106

107 108
    const int n = SizeToAxis(axis, dX->dims());
    const int d = SizeFromAxis(axis, dX->dims());
D
dengkaipeng 已提交
109
    Tensor dX_2d, Out_2d, dOut_2d;
110 111 112
    dX_2d.ShareDataWith(*dX).Resize({n, d});
    Out_2d.ShareDataWith(*Out).Resize({n, d});
    dOut_2d.ShareDataWith(*dOut).Resize({n, d});
F
fengjiayi 已提交
113

Q
QI JUN 已提交
114
    math::SoftmaxGradFunctor<DeviceContext, T>()(
D
dengkaipeng 已提交
115 116
        context.template device_context<DeviceContext>(), axis_dim, &Out_2d,
        &dOut_2d, &dX_2d);
Q
Qiao Longfei 已提交
117 118 119
  }
};

120 121
}  // namespace operators
}  // namespace paddle