net_op.cc 3.0 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
std::vector<std::string> NetOp::OutputVars(bool has_intermediate) const {
Y
Yu Yang 已提交
71 72 73 74 75 76
  std::vector<std::string> all;
  for (auto& pair : this->outputs_) {
    for (auto& var_name : pair.second) {
      all.push_back(var_name);
    }
  }
77
  if (has_intermediate) {
Y
Yu Yang 已提交
78
    return all;
79 80 81 82 83 84 85 86 87 88
  }
  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 已提交
89 90
NetOp::NetOp(const std::string& type, const framework::VariableNameMap& inputs,
             const framework::VariableNameMap& outputs,
Y
Yu Yang 已提交
91
             const framework::AttributeMap& attrs)
Y
Yu Yang 已提交
92
    : framework::OperatorBase(type, inputs, outputs, attrs) {}
Y
Yu Yang 已提交
93

Y
Yu Yang 已提交
94
std::unique_ptr<framework::OperatorBase> NetOp::Clone() const {
Y
Yu Yang 已提交
95 96 97
  PADDLE_ENFORCE(
      add_op_done_,
      "Must clone a sealed NetOp, invoke Net::CompleteAddOp before clone");
Y
Yu Yang 已提交
98
  return std::unique_ptr<OperatorBase>(new NetOp(*this));
Y
Yu Yang 已提交
99 100
}

Y
Yan Chunwei 已提交
101
}  // namespace operators
S
Superjom 已提交
102
}  // namespace paddle