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

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
D
dongzhihong 已提交
16 17
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
18 19 20 21

namespace paddle {
namespace operators {

D
dongzhihong 已提交
22 23 24 25 26
using Tensor = framework::Tensor;
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;

Q
qijun 已提交
27
template <typename Place, typename T>
D
dongzhihong 已提交
28
class SoftmaxKernel : public framework::OpKernel {
29
 public:
D
dongzhihong 已提交
30
  void Compute(const framework::ExecutionContext& context) const override {
31 32
    auto X = context.Input<Tensor>("Logits");
    auto Y = context.Output<Tensor>("Out");
C
caoying03 已提交
33
    Y->mutable_data<T>(context.GetPlace());
Q
qijun 已提交
34

C
caoying03 已提交
35
    auto logits = EigenMatrix<T>::From(*X);
36
    auto out = EigenMatrix<T>::From(*Y);
Q
qijun 已提交
37 38 39 40 41 42 43 44 45 46 47

    const int kBatchDim = 0;
    const int kClassDim = 1;

    const int batch_size = logits.dimension(kBatchDim);
    const int num_classes = logits.dimension(kClassDim);

    Eigen::DSizes<int, 1> along_class(kClassDim);
    Eigen::DSizes<int, 2> batch_by_one(batch_size, 1);
    Eigen::DSizes<int, 2> one_by_class(1, num_classes);

D
dongzhihong 已提交
48 49 50 51 52
    auto shifted_logits = (logits -
                           logits.maximum(along_class)
                               .eval()
                               .reshape(batch_by_one)
                               .broadcast(one_by_class));
Q
qijun 已提交
53

54
    out.device(context.GetEigenDevice<Place>()) = shifted_logits.exp();
Q
qijun 已提交
55

56 57 58
    out.device(context.GetEigenDevice<Place>()) =
        (out *
         out.sum(along_class)
D
dongzhihong 已提交
59 60 61 62
             .inverse()
             .eval()
             .reshape(batch_by_one)
             .broadcast(one_by_class));
63 64
  }
};
Q
Qiao Longfei 已提交
65 66

template <typename Place, typename T>
D
dongzhihong 已提交
67
class SoftmaxGradKernel : public framework::OpKernel {
68
 public:
D
dongzhihong 已提交
69
  void Compute(const framework::ExecutionContext& context) const override {
Q
Qiao Longfei 已提交
70 71
    std::shared_ptr<Tensor> scale_ = std::make_shared<Tensor>();

72 73 74
    auto Y = context.Input<Tensor>("Out");
    auto dY = context.Input<Tensor>(framework::GradVarName("Out"));
    auto dX = context.Output<Tensor>(framework::GradVarName("Logits"));
Q
Qiao Longfei 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    dX->mutable_data<T>(context.GetPlace());

    const int batch_size = Y->dims()[0];
    const int class_num = Y->dims()[1];

    Eigen::DSizes<int, 1> along_class(1);
    Eigen::DSizes<int, 2> batch_by_one(batch_size, 1);
    Eigen::DSizes<int, 2> one_by_class(1, class_num);

    auto Y_eigen = EigenMatrix<T>::From(*Y);
    auto dY_eigen = EigenMatrix<T>::From(*dY);
    auto dX_eigen = EigenMatrix<T>::From(*dX);
    auto place = context.GetEigenDevice<Place>();

    auto dot = (Y_eigen * dY_eigen)
                   .sum(along_class)
                   .eval()
                   .reshape(batch_by_one)
                   .broadcast(one_by_class);
    dX_eigen.device(place) = (dY_eigen - dot) * Y_eigen;
  }
};

98 99
}  // namespace operators
}  // namespace paddle