net_op.cc 2.6 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

24 25
const char NetOp::kAll[] = "all";

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

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

Y
Yu Yang 已提交
56
std::string NetOp::DebugString() const {
57
  std::ostringstream os;
Y
Yu Yang 已提交
58
  os << OperatorBase::DebugString() << std::endl;
59
  for (auto& op : ops_) {
Y
Yu Yang 已提交
60 61 62 63
    std::istringstream is(op->DebugString());
    for (std::string line; std::getline(is, line);) {
      os << "    " << line << std::endl;
    }
64 65 66 67
  }
  return os.str();
}

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

70 71 72 73 74 75 76 77 78 79 80 81 82 83
std::vector<std::string> NetOp::OutputVars(bool has_intermediate) const {
  if (has_intermediate) {
    return this->outputs_.at(kAll);
  }
  auto& all = this->outputs_.at(kAll);
  std::vector<std::string> ret_val;
  for (auto& each : all) {
    if (!Contains(intermediate_outputs_, each)) {
      ret_val.push_back(each);
    }
  }
  return ret_val;
}

Y
Yu Yang 已提交
84 85 86 87 88 89
NetOp::NetOp(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 已提交
90
}  // namespace operators
S
Superjom 已提交
91
}  // namespace paddle