recurrent_op.h 5.3 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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/framework/operator.h"
Y
Yan Chunwei 已提交
18
#include "paddle/operators/net_op.h"
19
#include "paddle/operators/rnn/recurrent_op_utils.h"
Y
Yan Chunwei 已提交
20 21 22 23 24

namespace paddle {
namespace operators {

// The sequence format in RecurrentOp is Tensor<seq_len, batch_size, dim> now.
S
superjom 已提交
25
// TODO(Superjom)
Y
Yan Chunwei 已提交
26 27 28 29 30 31 32
// 1. No-padding computing for sequences with indifinite length in one batch.
// 2. Hierarchical RNN for sequence with sub-sequence.
// 3. Internal Memory.
// 4. More Complex RNN architecture, such as Gated Feedback RNN.
//    Refer to: https://arxiv.org/pdf/1502.02367.pdf

class RecurrentAlgorithm {
33
 public:
Y
Yi Wang 已提交
34 35
  void Run(const framework::Scope& scope,
           const platform::DeviceContext& dev_ctx) const;
Y
Yan Chunwei 已提交
36

Y
Yu Yang 已提交
37 38
  void Init(rnn::Argument* arg,
            std::unique_ptr<framework::OperatorBase>* stepnet) {
Y
Yan Chunwei 已提交
39 40 41 42
    PADDLE_ENFORCE_NOT_NULL(stepnet, "stepnet should be set before.");
    arg_ = arg;
    stepnet_ = stepnet;
  }
Y
Yan Chunwei 已提交
43

44
 protected:
Y
Yan Chunwei 已提交
45 46 47 48 49 50
  /*
   * The step scopes will be stored in the father scope as a variable.
   *
   * NOTE the scopes are reused in both the forward and backward, so just
   * create once and expand its size if more steps need.
   */
Y
Yi Wang 已提交
51
  void CreateScopes(const framework::Scope& scope) const;
Y
Yan Chunwei 已提交
52

Y
Yi Wang 已提交
53 54 55 56
  const std::vector<framework::Scope*>& GetStepScopes(
      const framework::Scope& scope) const {
    return *scope.FindVar(arg_->step_scopes)
                ->GetMutable<std::vector<framework::Scope*>>();
Y
Yan Chunwei 已提交
57 58
  }

Q
qiaolongfei 已提交
59
  void InitMemories(framework::Scope* step_scopes) const;
Y
Yan Chunwei 已提交
60

61
 private:
Y
Yu Yang 已提交
62
  std::unique_ptr<framework::OperatorBase>* stepnet_;
Y
Yan Chunwei 已提交
63
  rnn::Argument* arg_;
Y
Yan Chunwei 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77
  mutable size_t seq_len_;
};

class RecurrentGradientAlgorithm {
  /**
   * RNN's backward alogorithm.
   *
   * To accelerate the development of RecurrentGradientOp, we decouple RNN's
   * algorithm and `OperatorBase`'s implementation, the former contains the core
   * implementation of a RNN, and will keep stable even if the framework changes
   * a
   * lot, and the latter is a wrapper acts like an dapter for it to make RNN an
   * operator.
   */
78
 public:
Y
Yu Yang 已提交
79 80
  void Init(rnn::Argument* arg,
            std::unique_ptr<framework::OperatorBase>* stepnet) {
Y
Yan Chunwei 已提交
81 82 83 84
    PADDLE_ENFORCE_NOT_NULL(stepnet, "stepnet should be set before.");
    arg_ = std::move(arg);
    stepnet_ = stepnet;
  }
Y
Yan Chunwei 已提交
85

Y
Yi Wang 已提交
86 87
  void Run(const framework::Scope& scope,
           const platform::DeviceContext& dev_ctx) const;
Y
Yan Chunwei 已提交
88

Q
qiaolongfei 已提交
89
  void LinkBootMemoryGradients(framework::Scope* step_scopes) const;
Q
qiaolongfei 已提交
90

91
 protected:
Y
Yi Wang 已提交
92 93 94 95
  inline const std::vector<framework::Scope*>& GetStepScopes(
      const framework::Scope& scope) const {
    return *scope.FindVar(arg_->step_scopes)
                ->GetMutable<std::vector<framework::Scope*>>();
Y
Yan Chunwei 已提交
96 97
  }

98
 private:
Y
Yan Chunwei 已提交
99
  rnn::Argument* arg_;
Y
Yan Chunwei 已提交
100
  mutable size_t seq_len_;
Y
Yu Yang 已提交
101
  std::unique_ptr<framework::OperatorBase>* stepnet_;
Y
Yan Chunwei 已提交
102 103
};

Y
Yu Yang 已提交
104
class RecurrentOp : public framework::OperatorBase {
105
 public:
Y
Yu Yang 已提交
106 107 108
  RecurrentOp(const std::string& type, const framework::VariableNameMap& inputs,
              const framework::VariableNameMap& outputs,
              const framework::AttributeMap& attrs);
Y
Yu Yang 已提交
109 110 111 112 113 114 115

  RecurrentOp(const RecurrentOp& o)
      : framework::OperatorBase(
            static_cast<const framework::OperatorBase&>(o)) {
    // TODO(yuyang18): Implement copy ctor well.
    PADDLE_THROW("Not implemented");
  }
Y
Yan Chunwei 已提交
116

Y
Yi Wang 已提交
117
  void Run(const framework::Scope& scope,
L
liaogang 已提交
118
           const platform::DeviceContext& dev_ctx) const override {
Y
Yan Chunwei 已提交
119 120 121
    alg_.Run(scope, dev_ctx);
  }

Y
Yu Yang 已提交
122 123 124
  void set_stepnet(std::unique_ptr<OperatorBase> net) {
    stepnet_ = std::move(net);
  }
Q
qiaolongfei 已提交
125

Y
Yu Yang 已提交
126
  const OperatorBase& stepnet() const { return *stepnet_; }
Y
Yan Chunwei 已提交
127

Y
Yan Chunwei 已提交
128 129
  static const rnn::ArgumentName kArgName;

130
 private:
Y
Yan Chunwei 已提交
131
  RecurrentAlgorithm alg_;
Y
Yan Chunwei 已提交
132
  rnn::Argument arg_;
Y
Yu Yang 已提交
133
  std::unique_ptr<OperatorBase> stepnet_;
Y
Yan Chunwei 已提交
134 135
};

Y
Yu Yang 已提交
136
class RecurrentGradientOp : public framework::OperatorBase {
137
 public:
Y
Yu Yang 已提交
138 139 140
  RecurrentGradientOp(const std::string& type,
                      const framework::VariableNameMap& inputs,
                      const framework::VariableNameMap& outputs,
Y
Yu Yang 已提交
141
                      const framework::AttributeMap& attrs);
Y
Yan Chunwei 已提交
142

Y
Yu Yang 已提交
143 144 145 146 147 148 149
  RecurrentGradientOp(const RecurrentGradientOp& o)
      : framework::OperatorBase(
            static_cast<const framework::OperatorBase&>(o)) {
    // TODO(yuyang18): Implement Copy ctor.
    PADDLE_THROW("Not Implemented");
  }

Y
Yi Wang 已提交
150
  void Run(const framework::Scope& scope,
L
liaogang 已提交
151
           const platform::DeviceContext& dev_ctx) const override {
Y
Yan Chunwei 已提交
152 153 154 155 156
    alg_.Run(scope, dev_ctx);
  }

  static const rnn::ArgumentName kArgName;

S
superjom 已提交
157 158 159
  /*
   * set a stepnet that is created according to a RecurrentOp's stepnet.
   */
Y
Yu Yang 已提交
160 161 162 163
  void set_stepnet(std::unique_ptr<OperatorBase> net) {
    stepnet_ = std::move(net);
  }
  const OperatorBase& stepnet() const { return *stepnet_; }
Y
Yan Chunwei 已提交
164

165
 private:
Y
Yan Chunwei 已提交
166
  RecurrentGradientAlgorithm alg_;
Y
Yu Yang 已提交
167
  std::unique_ptr<OperatorBase> stepnet_;
Y
Yan Chunwei 已提交
168
  rnn::Argument arg_;
Y
Yan Chunwei 已提交
169 170 171 172
};

}  // namespace operators
}  // namespace paddle