recurrent_op.h 5.7 KB
Newer Older
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"
18
#include "paddle/operators/net_op.h"
19
#include "paddle/operators/rnn/recurrent_op_utils.h"
20 21 22 23 24

namespace paddle {
namespace operators {

// The sequence format in RecurrentOp is Tensor<seq_len, batch_size, dim> now.
25
// TODO(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;
36

37 38
  void Init(rnn::Argument* arg,
            std::unique_ptr<framework::OperatorBase>* stepnet) {
39 40 41 42
    PADDLE_ENFORCE_NOT_NULL(stepnet, "stepnet should be set before.");
    arg_ = arg;
    stepnet_ = stepnet;
  }
43 44 45 46

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

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

Y
Yi Wang 已提交
58 59 60 61
  const std::vector<framework::Scope*>& GetStepScopes(
      const framework::Scope& scope) const {
    return *scope.FindVar(arg_->step_scopes)
                ->GetMutable<std::vector<framework::Scope*>>();
62 63
  }

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

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

Y
Yi Wang 已提交
91 92
  void Run(const framework::Scope& scope,
           const platform::DeviceContext& dev_ctx) const;
93

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

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

102
 protected:
Y
Yi Wang 已提交
103 104 105 106
  inline const std::vector<framework::Scope*>& GetStepScopes(
      const framework::Scope& scope) const {
    return *scope.FindVar(arg_->step_scopes)
                ->GetMutable<std::vector<framework::Scope*>>();
107 108
  }

109
 private:
110
  rnn::Argument* arg_;
111
  mutable size_t seq_len_;
112
  std::unique_ptr<framework::OperatorBase>* stepnet_;
113 114
};

115
class RecurrentOp : public framework::OperatorBase {
116
 public:
117 118
  RecurrentOp(const std::string& type, const VarNameMap& inputs,
              const VarNameMap& outputs, const framework::AttributeMap& attrs);
119 120 121 122 123 124 125

  RecurrentOp(const RecurrentOp& o)
      : framework::OperatorBase(
            static_cast<const framework::OperatorBase&>(o)) {
    // TODO(yuyang18): Implement copy ctor well.
    PADDLE_THROW("Not implemented");
  }
126
  /**
127 128
   * InferShape must be called before Run.
   */
Y
Yi Wang 已提交
129 130 131
  void InferShape(const framework::Scope& scope) const override {
    alg_.InferShape(scope);
  }
132

Y
Yi Wang 已提交
133
  void Run(const framework::Scope& scope,
134
           const platform::DeviceContext& dev_ctx) const override {
135 136 137
    alg_.Run(scope, dev_ctx);
  }

138 139 140 141
  void set_stepnet(std::unique_ptr<OperatorBase> net) {
    stepnet_ = std::move(net);
  }
  const OperatorBase& stepnet() const { return *stepnet_; }
142

143 144
  static const rnn::ArgumentName kArgName;

145
 private:
146
  RecurrentAlgorithm alg_;
147
  rnn::Argument arg_;
148
  std::unique_ptr<OperatorBase> stepnet_;
149 150
};

151
class RecurrentGradientOp : public framework::OperatorBase {
152
 public:
153 154 155
  RecurrentGradientOp(const std::string& type, const VarNameMap& inputs,
                      const VarNameMap& outputs,
                      const framework::AttributeMap& attrs);
156

157 158 159 160 161 162 163
  RecurrentGradientOp(const RecurrentGradientOp& o)
      : framework::OperatorBase(
            static_cast<const framework::OperatorBase&>(o)) {
    // TODO(yuyang18): Implement Copy ctor.
    PADDLE_THROW("Not Implemented");
  }

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

Y
Yi Wang 已提交
171
  void Run(const framework::Scope& scope,
172
           const platform::DeviceContext& dev_ctx) const override {
173 174 175 176 177
    alg_.Run(scope, dev_ctx);
  }

  static const rnn::ArgumentName kArgName;

178 179 180 181
  void set_stepnet(std::unique_ptr<OperatorBase> net) {
    stepnet_ = std::move(net);
  }
  const OperatorBase& stepnet() const { return *stepnet_; }
182

183
 private:
184
  RecurrentGradientAlgorithm alg_;
185
  std::unique_ptr<OperatorBase> stepnet_;
186
  rnn::Argument arg_;
187 188 189 190
};

}  // namespace operators
}  // namespace paddle
新手
引导
客服 返回
顶部