net_op.cc 3.2 KB
Newer Older
1
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// 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.
L
liaogang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/net_op.h"
Y
Yu Yang 已提交
16
#include <set>
Y
Yi Wang 已提交
17
#include "paddle/fluid/framework/op_registry.h"
S
Superjom 已提交
18 19

namespace paddle {
Y
Yan Chunwei 已提交
20
namespace operators {
S
Superjom 已提交
21

22 23
const char NetOp::kAll[] = "all";

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
  std::set<std::string> input_set;
  std::set<std::string> output_set;
S
Superjom 已提交
29
  for (auto& op : ops_) {
Q
qiaolongfei 已提交
30
    for (auto& ipt : op->Inputs()) {
Y
Yu Yang 已提交
31
      for (auto& var_name : ipt.second) {
Q
qijun 已提交
32 33 34 35
        // 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)) {
36
          intermediate_outputs_.insert(var_name);
Q
qijun 已提交
37 38
        } else {
          input_set.insert(var_name);
Y
Yu Yang 已提交
39
        }
Q
Qiao Longfei 已提交
40 41 42
      }
    }

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

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

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

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

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

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