softmax_op.h 3.4 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
    math::SoftmaxFunctor<DeviceContext, T, false>()(
D
dengkaipeng 已提交
78 79
        context.template device_context<DeviceContext>(), axis_dim, &X_2d,
        &Out_2d);
80 81
  }
};
Q
Qiao Longfei 已提交
82

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

94 95
    // allocate memory on device.
    dX->mutable_data<T>(context.GetPlace());
96 97 98
    if (dX->numel() == 0) {
      return;
    }
Q
Qiao Longfei 已提交
99

100 101
    const int n = SizeToAxis(axis, dX->dims());
    const int d = SizeFromAxis(axis, dX->dims());
D
dengkaipeng 已提交
102
    Tensor dX_2d, Out_2d, dOut_2d;
103 104 105
    dX_2d.ShareDataWith(*dX).Resize({n, d});
    Out_2d.ShareDataWith(*Out).Resize({n, d});
    dOut_2d.ShareDataWith(*dOut).Resize({n, d});
F
fengjiayi 已提交
106

Q
QI JUN 已提交
107
    math::SoftmaxGradFunctor<DeviceContext, T>()(
D
dengkaipeng 已提交
108 109
        context.template device_context<DeviceContext>(), axis_dim, &Out_2d,
        &dOut_2d, &dX_2d);
Q
Qiao Longfei 已提交
110 111 112
  }
};

113 114
}  // namespace operators
}  // namespace paddle