recurrent_op.h 5.6 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.
L
liaogang 已提交
25
// TODO(Yan Chunwei):
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
  void Init(rnn::Argument* arg, framework::OperatorBase* stepnet) {
Y
Yan Chunwei 已提交
38 39 40 41
    PADDLE_ENFORCE_NOT_NULL(stepnet, "stepnet should be set before.");
    arg_ = arg;
    stepnet_ = stepnet;
  }
Y
Yan Chunwei 已提交
42 43 44 45

  /**
   * InferShape must be called before Run.
   */
Y
Yi Wang 已提交
46
  void InferShape(const framework::Scope& scope) const;
Y
Yan Chunwei 已提交
47

48
 protected:
Y
Yan Chunwei 已提交
49 50 51 52 53 54
  /*
   * 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 已提交
55
  void CreateScopes(const framework::Scope& scope) const;
Y
Yan Chunwei 已提交
56

Y
Yi Wang 已提交
57 58 59 60
  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 已提交
61 62
  }

Y
Yi Wang 已提交
63
  void InitMemories(framework::Scope* step_scopes, bool infer_shape_mode) const;
Y
Yan Chunwei 已提交
64

65
 private:
Y
Yu Yang 已提交
66
  framework::OperatorBase* stepnet_;
Y
Yan Chunwei 已提交
67
  rnn::Argument* arg_;
Y
Yan Chunwei 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
  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.
   */
82
 public:
Y
Yu Yang 已提交
83
  void Init(rnn::Argument* arg, framework::OperatorBase* stepnet) {
Y
Yan Chunwei 已提交
84 85 86 87
    PADDLE_ENFORCE_NOT_NULL(stepnet, "stepnet should be set before.");
    arg_ = std::move(arg);
    stepnet_ = stepnet;
  }
Y
Yan Chunwei 已提交
88

Y
Yi Wang 已提交
89 90
  void Run(const framework::Scope& scope,
           const platform::DeviceContext& dev_ctx) const;
Y
Yan Chunwei 已提交
91

Y
Yi Wang 已提交
92 93
  void LinkBootMemoryGradients(framework::Scope* step_scopes,
                               bool infer_shape_mode) const;
Y
Yan Chunwei 已提交
94 95 96 97

  /**
   * InferShape must be called before Run.
   */
Y
Yi Wang 已提交
98
  void InferShape(const framework::Scope& scope) const;
Y
Yan Chunwei 已提交
99

100
 protected:
Y
Yi Wang 已提交
101 102 103 104
  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 已提交
105 106
  }

107
 private:
Y
Yan Chunwei 已提交
108
  rnn::Argument* arg_;
Y
Yan Chunwei 已提交
109
  mutable size_t seq_len_;
Y
Yu Yang 已提交
110
  framework::OperatorBase* stepnet_;
Y
Yan Chunwei 已提交
111 112
};

Y
Yu Yang 已提交
113
class RecurrentOp : public framework::OperatorBase {
114
 public:
Y
Yu Yang 已提交
115 116
  RecurrentOp(const std::string& type, const VarNameMap& inputs,
              const VarNameMap& outputs, const framework::AttributeMap& attrs);
Y
Yu Yang 已提交
117 118 119 120 121 122 123

  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 已提交
124
  /**
Y
Yu Yang 已提交
125 126
   * InferShape must be called before Run.
   */
Y
Yi Wang 已提交
127 128 129
  void InferShape(const framework::Scope& scope) const override {
    alg_.InferShape(scope);
  }
Y
Yan Chunwei 已提交
130

Y
Yi Wang 已提交
131
  void Run(const framework::Scope& scope,
L
liaogang 已提交
132
           const platform::DeviceContext& dev_ctx) const override {
Y
Yan Chunwei 已提交
133 134 135
    alg_.Run(scope, dev_ctx);
  }

Y
Yu Yang 已提交
136 137 138 139
  void set_stepnet(std::unique_ptr<OperatorBase> net) {
    stepnet_ = std::move(net);
  }
  const OperatorBase& stepnet() const { return *stepnet_; }
Y
Yan Chunwei 已提交
140

Y
Yan Chunwei 已提交
141 142
  static const rnn::ArgumentName kArgName;

143
 private:
Y
Yan Chunwei 已提交
144
  RecurrentAlgorithm alg_;
Y
Yan Chunwei 已提交
145
  rnn::Argument arg_;
Y
Yu Yang 已提交
146
  std::unique_ptr<OperatorBase> stepnet_;
Y
Yan Chunwei 已提交
147 148
};

Y
Yu Yang 已提交
149
class RecurrentGradientOp : public framework::OperatorBase {
150
 public:
Y
Yu Yang 已提交
151 152 153
  RecurrentGradientOp(const std::string& type, const VarNameMap& inputs,
                      const VarNameMap& outputs,
                      const framework::AttributeMap& attrs);
Y
Yan Chunwei 已提交
154

Y
Yu Yang 已提交
155 156 157 158 159 160 161
  RecurrentGradientOp(const RecurrentGradientOp& o)
      : framework::OperatorBase(
            static_cast<const framework::OperatorBase&>(o)) {
    // TODO(yuyang18): Implement Copy ctor.
    PADDLE_THROW("Not Implemented");
  }

Y
Yan Chunwei 已提交
162 163 164
  /**
   * InferShape must be called before Run.
   */
Y
Yi Wang 已提交
165 166 167
  void InferShape(const framework::Scope& scope) const override {
    alg_.InferShape(scope);
  }
Y
Yan Chunwei 已提交
168

Y
Yi Wang 已提交
169
  void Run(const framework::Scope& scope,
L
liaogang 已提交
170
           const platform::DeviceContext& dev_ctx) const override {
Y
Yan Chunwei 已提交
171 172 173 174 175
    alg_.Run(scope, dev_ctx);
  }

  static const rnn::ArgumentName kArgName;

Y
Yu Yang 已提交
176 177 178 179
  void set_stepnet(std::unique_ptr<OperatorBase> net) {
    stepnet_ = std::move(net);
  }
  const OperatorBase& stepnet() const { return *stepnet_; }
Y
Yan Chunwei 已提交
180

181
 private:
Y
Yan Chunwei 已提交
182
  RecurrentGradientAlgorithm alg_;
Y
Yu Yang 已提交
183
  std::unique_ptr<OperatorBase> stepnet_;
Y
Yan Chunwei 已提交
184
  rnn::Argument arg_;
Y
Yan Chunwei 已提交
185 186 187 188
};

}  // namespace operators
}  // namespace paddle