sequence_softmax_op.h 4.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

    http://www.apache.org/licenses/LICENSE-2.0

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. */

#pragma once

Y
Yi Wang 已提交
17
#include "paddle/fluid/framework/op_registry.h"
18 19 20 21 22 23 24

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using LoDTensor = framework::LoDTensor;

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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
template <typename DeviceContext, typename T>
struct SequenceSoftmaxFunctor {
  void operator()(
      const DeviceContext &ctx, const LoDTensor &x,
      const framework::Vector<size_t> &ref_lod, /*expand referenced lod*/
      LoDTensor *out);
};

template <typename DeviceContext, typename T>
struct SequenceSoftmaxGradFunctor {
  void operator()(const DeviceContext &ctx, const LoDTensor &dout,
                  const LoDTensor &out,
                  const framework::Vector<size_t> &ref_lod, /*referenced lod*/
                  LoDTensor *dx);
};

template <typename T>
struct SequenceSoftmaxFunctor<platform::CPUDeviceContext, T> {
  void operator()(const platform::CPUDeviceContext &ctx, const LoDTensor &x,
                  const framework::Vector<size_t> &ref_lod, /*referenced lod*/
                  LoDTensor *out) {
    size_t hight = ref_lod.size() - 1;
    const T *in_data = x.data<T>();
    T *out_data = out->mutable_data<T>(ctx.GetPlace());
    for (size_t i = 0; i < hight; ++i) {
      size_t span = ref_lod[i + 1] - ref_lod[i];
      T result = 0;
      for (size_t j = 0; j < span; ++j) {
        result += exp(in_data[ref_lod[i] + j]);
      }
      for (size_t j = 0; j < span; ++j) {
        out_data[ref_lod[i] + j] = exp(in_data[ref_lod[i] + j]) / result;
      }
    }
  }
};

template <typename T>
struct SequenceSoftmaxGradFunctor<platform::CPUDeviceContext, T> {
  void operator()(const platform::CPUDeviceContext &ctx, const LoDTensor &dout,
                  const LoDTensor &out,
                  const framework::Vector<size_t> &ref_lod, /*referenced lod*/
                  LoDTensor *dx) {
    size_t hight = ref_lod.size() - 1;

    const T *softmax_grad_data = dout.data<T>();
    const T *softmax = out.data<T>();
    T *dx_data = dx->mutable_data<T>(ctx.GetPlace());

    for (size_t i = 0; i < hight; ++i) {
      size_t span = ref_lod[i + 1] - ref_lod[i];
      T result = 0;
      for (size_t j = 0; j < span; ++j) {
        result += softmax_grad_data[ref_lod[i] + j] * softmax[ref_lod[i] + j];
      }

      for (size_t j = 0; j < span; ++j) {
        dx_data[ref_lod[i] + j] = (softmax_grad_data[ref_lod[i] + j] - result) *
                                  softmax[ref_lod[i] + j];
      }
    }
  }
};

Q
QI JUN 已提交
89
template <typename DeviceContext, typename T>
90
class SequenceSoftmaxKernel : public framework::OpKernel<T> {
91
 public:
92 93 94
  void Compute(const framework::ExecutionContext &ctx) const override {
    auto *x = ctx.Input<LoDTensor>("X");
    auto *out = ctx.Output<LoDTensor>("Out");
95 96

    auto lod = x->lod();
97 98
    auto dims = x->dims();

99
    const size_t level = lod.size() - 1;
100 101 102 103
    PADDLE_ENFORCE_EQ(dims[0], static_cast<int64_t>(lod[level].back()),
                      "The first dimension of Input(X) should be equal to the "
                      "sum of all sequences' lengths.");
    PADDLE_ENFORCE_EQ(dims[0], x->numel(),
104 105
                      "The width of each timestep in Input(X) of "
                      "SequenceSoftmaxOp should be 1.");
106 107

    out->mutable_data<T>(ctx.GetPlace());
108 109 110 111

    SequenceSoftmaxFunctor<DeviceContext, T> seq_softmax_functor;
    seq_softmax_functor(ctx.template device_context<DeviceContext>(), *x,
                        lod[level], out);
112 113 114
  }
};

Q
QI JUN 已提交
115
template <typename DeviceContext, typename T>
116
class SequenceSoftmaxGradKernel : public framework::OpKernel<T> {
117
 public:
118 119 120 121 122 123 124
  void Compute(const framework::ExecutionContext &ctx) const override {
    auto *out = ctx.Input<LoDTensor>("Out");
    auto *out_grad = ctx.Input<LoDTensor>(framework::GradVarName("Out"));
    auto *x = ctx.Input<LoDTensor>("X");
    auto *x_grad = ctx.Output<LoDTensor>(framework::GradVarName("X"));
    if (!x_grad) {
      return;
125
    }
126

127
    x_grad->set_lod(x->lod());
128 129 130
    auto lod = x->lod();
    const size_t level = lod.size() - 1;
    x_grad->mutable_data<T>(ctx.GetPlace());
131 132 133 134

    SequenceSoftmaxGradFunctor<DeviceContext, T> seq_softmax_grad_functor;
    seq_softmax_grad_functor(ctx.template device_context<DeviceContext>(),
                             *out_grad, *out, lod[level], x_grad);
135
  }
136 137 138 139
};

}  // namespace operators
}  // namespace paddle