net_op.cc 3.1 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
      for (auto& var_name : ipt.second) {
Q
qijun 已提交
34 35 36 37
        // If input variable has been in output set, then it will be
        // added into intermediate_outputs_. Otherwise, it will be
        // added into input set.
        if (Contains(output_set, var_name)) {
38
          intermediate_outputs_.insert(var_name);
Q
qijun 已提交
39 40
        } else {
          input_set.insert(var_name);
Y
Yu Yang 已提交
41
        }
Q
Qiao Longfei 已提交
42 43 44
      }
    }

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

59
std::string NetOp::DebugStringEx(const framework::Scope* scope) const {
60
  std::ostringstream os;
61
  os << OperatorBase::DebugStringEx(scope) << std::endl;
62
  for (auto& op : ops_) {
63
    std::istringstream is(op->DebugStringEx(scope));
Y
Yu Yang 已提交
64 65 66
    for (std::string line; std::getline(is, line);) {
      os << "    " << line << std::endl;
    }
67 68 69 70
  }
  return os.str();
}

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

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

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

Y
Yan Chunwei 已提交
104
}  // namespace operators
S
Superjom 已提交
105
}  // namespace paddle