/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. 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 #include "paddle/fluid/framework/op_registry.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; using LoDTensor = framework::LoDTensor; template struct SequenceSoftmaxFunctor { void operator()( const DeviceContext &ctx, const LoDTensor &x, const framework::Vector &ref_lod, /*expand referenced lod*/ LoDTensor *out); }; template struct SequenceSoftmaxGradFunctor { void operator()(const DeviceContext &ctx, const LoDTensor &dout, const LoDTensor &out, const framework::Vector &ref_lod, /*referenced lod*/ LoDTensor *dx); }; template struct SequenceSoftmaxFunctor { void operator()(const platform::CPUDeviceContext &ctx, const LoDTensor &x, const framework::Vector &ref_lod, /*referenced lod*/ LoDTensor *out) { size_t hight = ref_lod.size() - 1; const T *in_data = x.data(); T *out_data = out->mutable_data(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 struct SequenceSoftmaxGradFunctor { void operator()(const platform::CPUDeviceContext &ctx, const LoDTensor &dout, const LoDTensor &out, const framework::Vector &ref_lod, /*referenced lod*/ LoDTensor *dx) { size_t hight = ref_lod.size() - 1; const T *softmax_grad_data = dout.data(); const T *softmax = out.data(); T *dx_data = dx->mutable_data(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]; } } } }; template class SequenceSoftmaxKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { auto *x = ctx.Input("X"); auto *out = ctx.Output("Out"); auto lod = x->lod(); auto dims = x->dims(); PADDLE_ENFORCE_EQ(lod.empty(), false, "Input(X) Tensor of SequenceSoftmaxOp does not contain " "LoD information."); const size_t level = lod.size() - 1; PADDLE_ENFORCE_GT( lod.size(), 0U, "The LoD level of Input X should be larger than 0 (lod.size() > 0)."); PADDLE_ENFORCE_EQ(dims[0], static_cast(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(), "The width of each timestep in Input(X) of " "SequenceSoftmaxOp should be 1."); out->mutable_data(ctx.GetPlace()); SequenceSoftmaxFunctor seq_softmax_functor; seq_softmax_functor(ctx.template device_context(), *x, lod[level], out); } }; template class SequenceSoftmaxGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { auto *out = ctx.Input("Out"); auto *out_grad = ctx.Input(framework::GradVarName("Out")); auto *x = ctx.Input("X"); auto *x_grad = ctx.Output(framework::GradVarName("X")); if (!x_grad) { return; } x_grad->set_lod(x->lod()); auto lod = x->lod(); const size_t level = lod.size() - 1; x_grad->mutable_data(ctx.GetPlace()); SequenceSoftmaxGradFunctor seq_softmax_grad_functor; seq_softmax_grad_functor(ctx.template device_context(), *out_grad, *out, lod[level], x_grad); } }; } // namespace operators } // namespace paddle