net.cc 2.5 KB
Newer Older
L
liaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
  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.
*/

S
Superjom 已提交
17
#include "paddle/framework/net.h"
D
dongzhihong 已提交
18
#include "paddle/framework/op_registry.h"
S
Superjom 已提交
19 20 21 22

namespace paddle {
namespace framework {

D
dongzhihong 已提交
23
std::shared_ptr<PlainNet> AddBackwardOp(std::shared_ptr<PlainNet> ForwardOps) {
D
dongzhihong 已提交
24
  auto grad_ops = std::make_shared<PlainNet>();
D
dongzhihong 已提交
25 26 27 28 29 30 31 32
  for (auto& op : ForwardOps->ops_) {
    auto op_grad = OpRegistry::CreateGradOp(op);
    grad_ops->AddOp(op_grad);
  }
  grad_ops->CompleteAddOp();
  return grad_ops;
}

Y
Yu Yang 已提交
33 34 35
void PlainNet::CompleteAddOp(bool calc) {
  add_op_done_ = true;
  if (!calc) return;
Q
Qiao Longfei 已提交
36 37 38
  std::unordered_set<std::string> input_set;
  std::unordered_set<std::string> output_set;
  std::unordered_set<std::string> temp_output;
S
Superjom 已提交
39
  for (auto& op : ops_) {
Q
Qiao Longfei 已提交
40 41 42 43 44 45 46 47 48 49 50
    for (auto& ipt : op->inputs_) {
      if (!Contains(output_set, ipt)) {  // Not other op's output
        input_set.insert(ipt);
      } else {
        temp_output.insert(ipt);
      }
    }

    for (auto& opt : op->outputs_) {
      output_set.insert(opt);
    }
S
Superjom 已提交
51
  }
Y
Yu Yang 已提交
52

Q
Qiao Longfei 已提交
53 54
  inputs_.reserve(input_set.size());
  std::copy(input_set.begin(), input_set.end(), std::back_inserter(inputs_));
Y
Yu Yang 已提交
55
  std::sort(inputs_.begin(), inputs_.end());
Q
Qiao Longfei 已提交
56 57

  outputs_.reserve(output_set.size());
Y
Yu Yang 已提交
58 59 60
  std::copy(output_set.begin(), output_set.end(), std::back_inserter(outputs_));
  std::sort(outputs_.begin(), outputs_.end());

Q
Qiao Longfei 已提交
61 62
  std::vector<int> tmp_index;
  tmp_index.reserve(temp_output.size());
Y
Yu Yang 已提交
63 64 65 66
  int output_len = static_cast<int>(outputs_.size());
  for (int i = 0; i < output_len; ++i) {
    if (Contains(temp_output, outputs_[i])) {
      tmp_index.push_back(i);
Q
Qiao Longfei 已提交
67
    }
D
dongzhihong 已提交
68
  }
Q
Qiao Longfei 已提交
69 70

  attrs_["temporary_index"] = tmp_index;
D
dongzhihong 已提交
71
}
Q
Qiao Longfei 已提交
72

73 74
std::string PlainNet::DebugString() const {
  std::ostringstream os;
Y
Yu Yang 已提交
75
  os << OperatorBase::DebugString() << std::endl;
76
  for (auto& op : ops_) {
Y
Yu Yang 已提交
77 78 79 80
    std::istringstream is(op->DebugString());
    for (std::string line; std::getline(is, line);) {
      os << "    " << line << std::endl;
    }
81 82 83 84
  }
  return os.str();
}

S
Superjom 已提交
85 86
}  // namespace framework
}  // namespace paddle