You need to sign in or sign up before continuing.
recurrent_op.cc 9.8 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/operators/recurrent_op.h"
Y
Yan Chunwei 已提交
16 17 18 19 20

#include <cstring>
#include <sstream>

#include "paddle/framework/op_registry.h"
Y
Yan Chunwei 已提交
21
#include "paddle/operators/net_op.h"
Y
Yan Chunwei 已提交
22 23 24 25

namespace paddle {
namespace operators {

D
dongzhihong 已提交
26 27 28 29
using Scope = framework::Scope;
using Variable = framework::Variable;
using Tensor = framework::Tensor;

Y
Yu Yang 已提交
30 31
void RecurrentAlgorithm::InferShape(const Scope& scope) const {
  seq_len_ = scope.FindVar((arg_->inlinks[0]).external)
Y
Yan Chunwei 已提交
32 33 34
                 ->GetMutable<Tensor>()
                 ->dims()[0];
  CreateScopes(scope);
35
  auto step_scopes = GetStepScopes(scope);
36 37
  rnn::SegmentInputs(step_scopes, arg_->inlinks, seq_len_,
                     true /*infer_shape_mode*/);
D
dangqingqing 已提交
38
  InitMemories(step_scopes[0], true /*infer_shape_mode*/);
Y
Yu Yang 已提交
39
  Variable* net = scope.FindVar(arg_->step_net);
Y
Yan Chunwei 已提交
40
  PADDLE_ENFORCE(net != nullptr, "failed to get step net");
Y
Yan Chunwei 已提交
41

Y
Yan Chunwei 已提交
42 43
  for (size_t i = 0; i < seq_len_; i++) {
    if (i > 0) {
44 45
      rnn::LinkMemories(step_scopes, arg_->memories, i, -1,
                        true /*infer_shape_mode*/);
Y
Yan Chunwei 已提交
46
    }
Y
Yu Yang 已提交
47
    net->GetMutable<NetOp>()->InferShape(*step_scopes[i]);
Y
Yan Chunwei 已提交
48
  }
49 50
  rnn::ConcatOutputs(step_scopes, arg_->outlinks, seq_len_,
                     true /*infer_shape_mode*/);
Y
Yan Chunwei 已提交
51 52
}

Y
Yu Yang 已提交
53
void RecurrentAlgorithm::Run(const Scope& scope,
Y
Yan Chunwei 已提交
54 55
                             const platform::DeviceContext& dev_ctx) const {
  auto step_scopes = GetStepScopes(scope);
56 57
  rnn::SegmentInputs(step_scopes, arg_->inlinks, seq_len_,
                     false /*infer_shape_mode*/);
D
dangqingqing 已提交
58
  InitMemories(step_scopes[0], false /*infer_shape_mode*/);
Y
Yu Yang 已提交
59
  Variable* net = scope.FindVar(arg_->step_net);
D
dangqingqing 已提交
60

Y
Yan Chunwei 已提交
61
  for (size_t step_id = 0; step_id < seq_len_; step_id++) {
Y
Yan Chunwei 已提交
62
    // create output alias variables
Y
Yan Chunwei 已提交
63
    if (step_id > 0) {
64 65
      rnn::LinkMemories(step_scopes, arg_->memories, step_id, -1,
                        false /*infer_shape_mode*/);
Y
Yan Chunwei 已提交
66
    }
Y
Yu Yang 已提交
67
    net->GetMutable<NetOp>()->Run(*step_scopes[step_id], dev_ctx);
Y
Yan Chunwei 已提交
68
  }
69 70
  rnn::ConcatOutputs(step_scopes, arg_->outlinks, seq_len_,
                     false /*infer_shape_mode*/);
Y
Yan Chunwei 已提交
71 72
}

Y
Yu Yang 已提交
73
void RecurrentAlgorithm::CreateScopes(const Scope& scope) const {
Y
Yan Chunwei 已提交
74
  // TODO(superjom) Only two scopes are needed for inference, this case will be
Y
Yan Chunwei 已提交
75
  // supported later.
Y
Yan Chunwei 已提交
76 77 78 79 80 81 82 83 84
  auto step_scopes_var = scope.FindVar(arg_->step_scopes);
  PADDLE_ENFORCE(step_scopes_var != nullptr, "");
  auto step_scopes = step_scopes_var->GetMutable<std::vector<Scope*>>();

  // Now all variables in scope must be created outside of op.
  auto net_var = scope.FindVar(arg_->step_net);
  PADDLE_ENFORCE(net_var != nullptr, "no stepnet called %s in scope",
                 arg_->step_net);
  auto net_op = net_var->GetMutable<NetOp>();
Q
qiaolongfei 已提交
85
  PADDLE_ENFORCE(!net_op->Outputs().empty(), "net_op has no outputs");
Y
Yan Chunwei 已提交
86 87 88

  if (seq_len_ > step_scopes->size()) {
    for (size_t i = step_scopes->size(); i < seq_len_; ++i) {
Y
Yu Yang 已提交
89
      auto& step_scope = scope.NewScope();
Y
Yan Chunwei 已提交
90

Y
Yan Chunwei 已提交
91
      // create step net's temp inputs
Q
qiaolongfei 已提交
92
      for (auto& input : net_op->Inputs()) {
93
        // the weight are located in parent scope
Y
Yu Yang 已提交
94 95 96 97 98
        for (auto& var_name : input.second) {
          if (!step_scope.FindVar(var_name)) {
            step_scope.NewVar(var_name)->GetMutable<Tensor>();
          }
        }
Y
Yan Chunwei 已提交
99
      }
Y
Yan Chunwei 已提交
100
      // create stepnet's outputs
Q
qiaolongfei 已提交
101
      for (const auto& output : net_op->Outputs()) {
Y
Yu Yang 已提交
102 103 104
        for (auto& var_name : output.second) {
          step_scope.NewVar(var_name);
        }
Y
Yan Chunwei 已提交
105
      }
Y
Yu Yang 已提交
106
      step_scopes->emplace_back(&step_scope);
Y
Yan Chunwei 已提交
107 108 109 110
    }
  }
}

D
dangqingqing 已提交
111
void RecurrentAlgorithm::InitMemories(Scope* step_scope,
D
dangqingqing 已提交
112
                                      bool infer_shape_mode) const {
Y
Yan Chunwei 已提交
113
  for (auto& attr : arg_->memories) {
114
    Tensor* pre_mem = step_scope->NewVar(attr.pre_var)->GetMutable<Tensor>();
Y
Yu Yang 已提交
115
    PADDLE_ENFORCE(step_scope->FindVar(attr.boot_var) != nullptr,
116
                   "memory [%s]'s boot variable [%s] not exists", attr.var,
Y
Yan Chunwei 已提交
117
                   attr.boot_var);
118
    Tensor* boot_mem = step_scope->FindVar(attr.boot_var)->GetMutable<Tensor>();
D
dangqingqing 已提交
119
    if (infer_shape_mode) {
120
      pre_mem->Resize(boot_mem->dims());
Y
Yan Chunwei 已提交
121
      PADDLE_ENFORCE_EQ(pre_mem->dims().size(), 2);
122 123 124
    } else {
      pre_mem->ShareDataWith<float>(*boot_mem);
    }
Y
Yan Chunwei 已提交
125 126 127
  }
}

128 129 130 131 132 133 134 135 136
const rnn::ArgumentName RecurrentOp::kArgName{
    "step_net", "step_scopes",  "inlinks",
    "outlinks", "inlink_alias", "outlink_alias",
    "memories", "pre_memories", "boot_memories"};

const rnn::ArgumentName RecurrentGradientOp::kArgName{
    "step_net",    "step_scopes",  "outlink@grad",
    "inlink@grad", "inlink_alias", "outlink_alias",
    "memories",    "pre_memories", "boot_memories@grad"};
Y
Yan Chunwei 已提交
137

Y
Yu Yang 已提交
138 139 140 141 142
RecurrentOp::RecurrentOp(const std::string& type,
                         const framework::OperatorBase::VarNameMap& inputs,
                         const framework::OperatorBase::VarNameMap& outputs,
                         const framework::AttributeMap& attrs)
    : OperatorBase(type, inputs, outputs, attrs) {
Y
Yan Chunwei 已提交
143 144 145 146 147
  std::unique_ptr<rnn::Argument> arg(new rnn::Argument());
  rnn::InitArgument(kArgName, arg.get(), *this);
  alg_.Init(std::move(arg));
}

D
dongzhihong 已提交
148 149
class RecurrentAlgorithmProtoAndCheckerMaker
    : public framework::OpProtoAndCheckerMaker {
150
 public:
D
dongzhihong 已提交
151 152
  RecurrentAlgorithmProtoAndCheckerMaker(framework::OpProto* proto,
                                         framework::OpAttrChecker* op_checker)
Y
Yan Chunwei 已提交
153 154 155
      : OpProtoAndCheckerMaker(proto, op_checker) {
    const auto& name = RecurrentOp::kArgName;
    // inputs and outputs stored in proto
D
dangqingqing 已提交
156 157
    AddInput(name.inlinks,
             "the inputs that need to be segmented for each step.")
Y
Yu Yang 已提交
158
        .AsDuplicable();
Y
Yu Yang 已提交
159
    AddInput(name.boot_memories, "variables to initialize memories.")
Y
Yu Yang 已提交
160
        .AsDuplicable();
Y
Yan Chunwei 已提交
161 162
    AddInput(name.step_net, "network shared by all steps.");

D
dangqingqing 已提交
163
    AddOutput(name.outlinks, "the outputs that need to concated for all steps.")
Y
Yu Yang 已提交
164
        .AsDuplicable();
Y
Yan Chunwei 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178
    AddOutput(name.step_scopes, "step scopes");

    // Attributes stored in AttributeMap
    AddAttr<std::vector<std::string>>(name.inlink_alias, "alias of inlinks");
    AddAttr<std::vector<std::string>>(name.outlink_alias, "alias of outlinks");
    AddAttr<std::vector<std::string>>(name.pre_memories,
                                      "names of pre-memories");
    AddAttr<std::vector<std::string>>(name.memories, "names of memories");

    AddComment("This is a recurrent group operator.");
  }
};

void RecurrentGradientAlgorithm::Run(
Y
Yu Yang 已提交
179
    const Scope& scope, const platform::DeviceContext& dev_ctx) const {
Y
Yan Chunwei 已提交
180
  auto step_scopes = GetStepScopes(scope);
181 182
  rnn::SegmentInputs(step_scopes, arg_->inlinks, seq_len_,
                     false /*infer_shape_mode*/);
Y
Yu Yang 已提交
183
  Variable* net = scope.FindVar(arg_->step_net);
Y
Yan Chunwei 已提交
184 185 186
  PADDLE_ENFORCE(net != nullptr, "failed to get step net");
  for (int step_id = seq_len_ - 1; step_id >= 0; --step_id) {
    if (static_cast<size_t>(step_id) != seq_len_ - 1) {
187 188
      rnn::LinkMemories(step_scopes, arg_->memories, step_id, 1,
                        false /*infer_shape_mode*/);
Y
Yan Chunwei 已提交
189
    }
Y
Yu Yang 已提交
190
    net->GetMutable<NetOp>()->Run(*step_scopes[step_id], dev_ctx);
Y
Yan Chunwei 已提交
191
  }
192
  LinkBootMemoryGradients(step_scopes[0], false);
193 194
  rnn::ConcatOutputs(step_scopes, arg_->outlinks, seq_len_,
                     false /*infer_shape_mode*/);
Y
Yan Chunwei 已提交
195 196 197
}

void RecurrentGradientAlgorithm::LinkBootMemoryGradients(
D
dangqingqing 已提交
198
    Scope* step_scope, bool infer_shape_mode) const {
Y
Yan Chunwei 已提交
199
  for (auto& attr : arg_->memories) {
D
dangqingqing 已提交
200
    PADDLE_ENFORCE(step_scope->FindVar(attr.var) != nullptr,
201
                   "memory variable [%s] does not exists", attr.var);
Y
Yu Yang 已提交
202
    PADDLE_ENFORCE(step_scope->FindVar(attr.boot_var) != nullptr,
203
                   "boot variable [%s] does not exists", attr.boot_var);
D
dangqingqing 已提交
204
    Tensor* mem_grad = step_scope->NewVar(attr.var)->GetMutable<Tensor>();
Y
Yan Chunwei 已提交
205
    Tensor* boot_mem_grad =
206
        step_scope->NewVar(attr.boot_var)->GetMutable<Tensor>();
D
dangqingqing 已提交
207
    if (infer_shape_mode) {
208 209 210 211
      boot_mem_grad->Resize(mem_grad->dims());
    } else {
      boot_mem_grad->ShareDataWith<float>(*mem_grad);
    }
Y
Yan Chunwei 已提交
212 213 214
  }
}

Y
Yu Yang 已提交
215 216
void RecurrentGradientAlgorithm::InferShape(const Scope& scope) const {
  seq_len_ = scope.FindVar((arg_->inlinks[0]).external)
Y
Yan Chunwei 已提交
217 218 219
                 ->GetMutable<Tensor>()
                 ->dims()[0];
  auto step_scopes = GetStepScopes(scope);
220 221
  rnn::SegmentInputs(step_scopes, arg_->inlinks, seq_len_,
                     true /*infer_shape_mode*/);
Y
Yu Yang 已提交
222
  Variable* net = scope.FindVar(arg_->step_net);
Y
Yan Chunwei 已提交
223 224 225
  PADDLE_ENFORCE(net != nullptr, "failed to get step net");
  for (int step_id = seq_len_ - 1; step_id >= 0; --step_id) {
    if (static_cast<size_t>(step_id) != seq_len_ - 1) {
226 227
      rnn::LinkMemories(step_scopes, arg_->memories, step_id, 1,
                        true /*infer_shape_mode*/);
Y
Yan Chunwei 已提交
228
    }
Y
Yu Yang 已提交
229
    net->GetMutable<NetOp>()->InferShape(*step_scopes[step_id]);
Y
Yan Chunwei 已提交
230
  }
231 232
  rnn::ConcatOutputs(step_scopes, arg_->outlinks, seq_len_,
                     true /*infer_shape_mode*/);
D
dangqingqing 已提交
233
  LinkBootMemoryGradients(step_scopes[0], true /*infer_shape_mode*/);
Y
Yan Chunwei 已提交
234 235
}

Y
Yu Yang 已提交
236 237 238 239 240
RecurrentGradientOp::RecurrentGradientOp(
    const std::string& type, const framework::OperatorBase::VarNameMap& inputs,
    const framework::OperatorBase::VarNameMap& outputs,
    const framework::AttributeMap& attrs)
    : OperatorBase(type, inputs, outputs, attrs) {
Y
Yan Chunwei 已提交
241 242 243 244 245 246 247 248
  std::unique_ptr<rnn::Argument> arg(new rnn::Argument());
  rnn::InitArgument(kArgName, arg.get(), *this);
  alg_.Init(std::move(arg));
}

}  // namespace operators
}  // namespace paddle

F
fengjiayi 已提交
249 250 251
REGISTER_OP_WITHOUT_GRADIENT(
    recurrent_op, paddle::operators::RecurrentOp,
    paddle::operators::RecurrentAlgorithmProtoAndCheckerMaker);