net_op.h 4.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
S
Superjom 已提交
2

Q
Qiao Longfei 已提交
3 4 5
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
S
Superjom 已提交
6

Q
Qiao Longfei 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
S
Superjom 已提交
8

Q
Qiao Longfei 已提交
9 10 11 12 13
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. */
S
Superjom 已提交
14 15 16

#pragma once

Y
Yu Yang 已提交
17
#include <set>
Y
Yi Wang 已提交
18 19
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/op_registry.h"
S
Superjom 已提交
20 21

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

S
Superjom 已提交
24
/**
Q
Qiao Longfei 已提交
25 26 27
 * @brief Network is also a type of Operator
 *
 * It will manage the operators it has.
S
Superjom 已提交
28
 *
Q
Qiao Longfei 已提交
29
 * Network is the container and controller of a set of operators.
S
Superjom 已提交
30 31 32 33 34

 * A network object knows all Operators belonging to this network. Variables,
 * which are inputs and outputs of these operators, are created and managed by a
 * hierarchy of Scope objects.
 *
Q
Qiao Longfei 已提交
35
 * This is the base class of network, all the networks should implement the APIs
S
Superjom 已提交
36 37
 * it defines.
 */
Y
Yan Chunwei 已提交
38
class NetOp : public framework::OperatorBase {
39
 public:
40
  static const char kAll[];
Y
Yiqun Liu 已提交
41 42 43 44
  NetOp()
      : framework::OperatorBase("plain_net", framework::VariableNameMap{},
                                framework::VariableNameMap{},
                                framework::AttributeMap{}) {}
Y
Yu Yang 已提交
45 46 47 48

  NetOp(const std::string& type, const framework::VariableNameMap& inputs,
        const framework::VariableNameMap& outputs,
        const framework::AttributeMap& attrs);
49

Y
Yu Yang 已提交
50
  NetOp(const NetOp& o) : framework::OperatorBase(o.type_, {}, {}, o.attrs_) {
Y
Yu Yang 已提交
51
    this->ops_.reserve(o.ops_.size());
Y
Yu Yang 已提交
52 53 54 55 56
    std::transform(
        o.ops_.begin(), o.ops_.end(), std::back_inserter(this->ops_),
        [](const std::unique_ptr<framework::OperatorBase>& op) {
          return std::unique_ptr<framework::OperatorBase>(op->Clone());
        });
Y
Yu Yang 已提交
57 58 59
    this->CompleteAddOp();
  }

60 61 62 63 64 65 66 67
  bool SupportGPU() const override {
    for (auto& op : ops_) {
      if (!op->SupportGPU()) {
        return false;
      }
    }
    return true;
  }
S
Superjom 已提交
68

Y
Yu Yang 已提交
69
  void AppendOp(const framework::OperatorBase& op) { AppendOp(op.Clone()); }
Y
Yu Yang 已提交
70

S
Superjom 已提交
71
  /**
Q
Qiao Longfei 已提交
72
   * @brief Add an operator by ptr
S
Superjom 已提交
73
   */
Y
Yu Yang 已提交
74 75 76
  void AppendOp(std::unique_ptr<framework::OperatorBase> op) {
    PADDLE_ENFORCE(!add_op_done_,
                   "Cannot AppendOp when this network is sealed");
Y
Yan Chunwei 已提交
77
    PADDLE_ENFORCE_NOT_NULL(op, "Cannot Insert Null op");
Y
Yu Yang 已提交
78
    ops_.push_back(std::move(op));
Y
Yu Yang 已提交
79 80
  }

Y
Yu Yang 已提交
81
  void InsertOp(size_t pos, std::unique_ptr<framework::OperatorBase> op) {
Y
Yu Yang 已提交
82 83
    PADDLE_ENFORCE(!add_op_done_,
                   "Cannot InsertOp when this network is sealed");
Y
Yan Chunwei 已提交
84 85
    PADDLE_ENFORCE_NOT_NULL(op, "Cannot Insert Null op");
    PADDLE_ENFORCE_LE(pos, ops_.size(), "Out of range");
Y
Yu Yang 已提交
86
    ops_.insert(ops_.begin() + pos, std::move(op));
Y
Yu Yang 已提交
87 88 89 90
  }

  void InsertOp(size_t pos, const framework::OperatorBase& op) {
    InsertOp(pos, op.Clone());
Y
Yu Yang 已提交
91 92
  }

Y
Yu Yang 已提交
93
  void CompleteAddOp(bool calculate = true);
S
Superjom 已提交
94

95 96
  std::string DebugStringEx(
      const framework::Scope* scope = nullptr) const override;
97

Y
Yu Yang 已提交
98
  bool IsNetOp() const override;
99
  std::vector<std::string> OutputVars(bool has_intermediate) const override;
Y
Yu Yang 已提交
100 101

  std::unique_ptr<framework::OperatorBase> Clone() const override;
Y
Yu Yang 已提交
102

Y
Yu Yang 已提交
103
  std::vector<std::unique_ptr<framework::OperatorBase>> ops_;
S
Superjom 已提交
104

105
 private:
106 107 108 109 110 111 112 113 114 115 116 117 118 119
  /**
   * @brief Run the network.
   *
   * Run all the operators with the `scope`, if no scope is provided, default
   * scope will be used instead. If no OpContext is provicded, default context
   * will be used.
   */
  void RunImpl(const framework::Scope& scope,
               const platform::Place& place) const override {
    for (auto& op : ops_) {
      op->Run(scope, place);
    }
  }

Q
Qiao Longfei 已提交
120
  bool add_op_done_{false};
121
  std::set<std::string> intermediate_outputs_;
Q
Qiao Longfei 已提交
122 123 124 125 126

  template <typename T, typename KeyType>
  static bool Contains(T container, KeyType key) {
    return container.find(key) != container.end();
  }
S
Superjom 已提交
127 128
};

Y
Yan Chunwei 已提交
129
}  // namespace operators
S
Superjom 已提交
130
}  // namespace paddle