softmax_op.h 3.4 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 {
Q
Qiao Longfei 已提交
31 32
    auto input = context.Input<Tensor>("X");
    auto output = context.Output<Tensor>("Y");
Q
qijun 已提交
33
    output->mutable_data<T>(context.GetPlace());
Q
qijun 已提交
34

35
    auto logits = EigenMatrix<T>::From(*input);
36
    auto softmax = EigenMatrix<T>::From(*output);
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
    auto shifted_logits = (logits - logits.maximum(along_class)
                                        .eval()
                                        .reshape(batch_by_one)
                                        .broadcast(one_by_class));
Q
qijun 已提交
52

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

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

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

    auto Y = context.Input<Tensor>("Y");
71 72
    auto dY = context.Input<Tensor>(framework::GradVarName("Y"));
    auto dX = context.Output<Tensor>(framework::GradVarName("X"));
Q
Qiao Longfei 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    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;
  }
};

96 97
}  // namespace operators
}  // namespace paddle