softmax_op.h 3.2 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 16

#pragma once

Q
Qiao Longfei 已提交
17 18 19
#include "paddle/framework/ddim.h"
#include "paddle/framework/operator.h"
#include "paddle/framework/tensor.h"
20
#include "paddle/operators/type_alias.h"
21 22 23 24

namespace paddle {
namespace operators {

Q
qijun 已提交
25
template <typename Place, typename T>
26
class SoftmaxKernel : public OpKernel {
27
public:
28
  void Compute(const ExecutionContext& context) const override {
Q
Qiao Longfei 已提交
29 30
    auto input = context.Input<Tensor>("X");
    auto output = context.Output<Tensor>("Y");
Q
qijun 已提交
31
    output->mutable_data<T>(context.GetPlace());
Q
qijun 已提交
32

33
    auto logits = EigenMatrix<T>::From(*input);
34
    auto softmax = EigenMatrix<T>::From(*output);
Q
qijun 已提交
35 36 37 38 39 40 41 42 43 44 45

    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);

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

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

54
    softmax.device(context.GetEigenDevice<Place>()) =
Q
qijun 已提交
55 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 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 94 95

template <typename Place, typename T>
class SoftmaxGradKernel : public OpKernel {
public:
  void Compute(const ExecutionContext& context) const override {
    std::shared_ptr<Tensor> scale_ = std::make_shared<Tensor>();

    auto Y = context.Input<Tensor>("Y");
    auto dY = context.Input<Tensor>(OperatorBase::GRAD_VAR_NAME("Y"));
    auto dX = context.Output<Tensor>(OperatorBase::GRAD_VAR_NAME("X"));
    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