net_op.cc 2.4 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.
*/

Y
Yan Chunwei 已提交
17
#include "paddle/operators/net_op.h"
Y
Yu Yang 已提交
18
#include <set>
D
dongzhihong 已提交
19
#include "paddle/framework/op_registry.h"
S
Superjom 已提交
20 21

namespace paddle {
Y
Yan Chunwei 已提交
22
namespace operators {
S
Superjom 已提交
23

Y
Yu Yang 已提交
24
void NetOp::CompleteAddOp(bool calc) {
Y
Yu Yang 已提交
25 26
  add_op_done_ = true;
  if (!calc) return;
Y
Yu Yang 已提交
27 28 29
  std::set<std::string> input_set;
  std::set<std::string> output_set;
  std::set<std::string> temp_output;
S
Superjom 已提交
30
  for (auto& op : ops_) {
Q
Qiao Longfei 已提交
31
    for (auto& ipt : op->inputs_) {
Y
Yu Yang 已提交
32 33 34 35 36 37
      for (auto& var_name : ipt.second) {
        if (!Contains(output_set, var_name)) {  // Not other op's output
          input_set.insert(var_name);
        } else {
          temp_output.insert(var_name);
        }
Q
Qiao Longfei 已提交
38 39 40 41
      }
    }

    for (auto& opt : op->outputs_) {
Y
Yu Yang 已提交
42 43 44
      for (auto& var_name : opt.second) {
        output_set.insert(var_name);
      }
Q
Qiao Longfei 已提交
45
    }
S
Superjom 已提交
46
  }
Y
Yu Yang 已提交
47 48 49 50 51 52
  auto& inputs = inputs_["all"];
  inputs.reserve(input_set.size());
  std::copy(input_set.begin(), input_set.end(), std::back_inserter(inputs));
  auto& outputs = outputs_["all"];
  outputs.reserve(output_set.size());
  std::copy(output_set.begin(), output_set.end(), std::back_inserter(outputs));
Y
Yu Yang 已提交
53

Y
Yu Yang 已提交
54
  //! TODO figure out how to generate temporary_index in Network.
Q
Qiao Longfei 已提交
55 56
  std::vector<int> tmp_index;
  tmp_index.reserve(temp_output.size());
Y
Yu Yang 已提交
57
  int output_len = static_cast<int>(outputs.size());
Y
Yu Yang 已提交
58
  for (int i = 0; i < output_len; ++i) {
Y
Yu Yang 已提交
59
    if (Contains(temp_output, outputs[i])) {
Y
Yu Yang 已提交
60
      tmp_index.push_back(i);
Q
Qiao Longfei 已提交
61
    }
D
dongzhihong 已提交
62
  }
Q
Qiao Longfei 已提交
63 64

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

Y
Yu Yang 已提交
67
std::string NetOp::DebugString() const {
68
  std::ostringstream os;
Y
Yu Yang 已提交
69
  os << OperatorBase::DebugString() << std::endl;
70
  for (auto& op : ops_) {
Y
Yu Yang 已提交
71 72 73 74
    std::istringstream is(op->DebugString());
    for (std::string line; std::getline(is, line);) {
      os << "    " << line << std::endl;
    }
75 76 77 78
  }
  return os.str();
}

Y
Yu Yang 已提交
79 80
bool NetOp::IsNetOp() const { return true; }

Y
Yan Chunwei 已提交
81
}  // namespace operators
S
Superjom 已提交
82
}  // namespace paddle