sequence_pool_op.h 1.7 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

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
16
#include <string>
17

Y
Yi Wang 已提交
18 19
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
20
#include "paddle/phi/kernels/funcs/math_function.h"
21
#include "paddle/phi/kernels/funcs/sequence_pooling.h"
22 23 24 25

namespace paddle {
namespace operators {

Q
QI JUN 已提交
26
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
27
class SequencePoolGradKernel : public framework::OpKernel<T> {
28 29
 public:
  void Compute(const framework::ExecutionContext& context) const override {
柠檬味~ 已提交
30 31 32
    auto* out_g =
        context.Input<phi::DenseTensor>(framework::GradVarName("Out"));
    auto* in_g = context.Output<phi::DenseTensor>(framework::GradVarName("X"));
D
dzhwinter 已提交
33
    std::string pooltype = context.Attr<std::string>("pooltype");
34
    const phi::DenseTensor* index = nullptr;
35
    if (pooltype == "MAX") {
36
      index = context.Input<phi::DenseTensor>("MaxIndex");
37
    }
D
dzhwinter 已提交
38
    in_g->mutable_data<T>(context.GetPlace());
39
    phi::funcs::SequencePoolGradFunctor<DeviceContext, T> pool;
40 41 42 43 44
    pool(context.template device_context<DeviceContext>(),
         pooltype,
         *out_g,
         in_g,
         index);
45 46 47 48 49
  }
};

}  // namespace operators
}  // namespace paddle