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

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 "paddle/framework/framework.pb.h"
Q
Qiao Longfei 已提交
18
#include "paddle/framework/op_registry.h"
S
Superjom 已提交
19 20

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

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

 * 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 已提交
34
 * This is the base class of network, all the networks should implement the APIs
S
Superjom 已提交
35 36
 * it defines.
 */
Y
Yan Chunwei 已提交
37
class NetOp : public framework::OperatorBase {
38
 public:
39
  static const char kAll[];
Y
Yu Yang 已提交
40 41 42
  NetOp() : framework::OperatorBase("plain_net", {}, {}, {}) {}
  NetOp(const std::string& type, const VarNameMap& inputs,
        const VarNameMap& outputs, const framework::AttributeMap& attrs);
43

Y
Yu Yang 已提交
44 45 46 47 48 49 50 51 52 53 54 55
  NetOp(const NetOp& o)
      : framework::OperatorBase(
            static_cast<const framework::OperatorBase&>(o)) {
    this->ops_.reserve(o.ops_.size());
    std::transform(o.ops_.begin(), o.ops_.end(), std::back_inserter(this->ops_),
                   [](const std::shared_ptr<OperatorBase>& op)
                       -> std::shared_ptr<OperatorBase> {
                         return std::shared_ptr<OperatorBase>(op->Clone());
                       });
    this->CompleteAddOp();
  }

S
Superjom 已提交
56
  /**
Q
Qiao Longfei 已提交
57
   * Infer all the operators' input and output variables' shapes, will be called
S
Superjom 已提交
58 59
   * before every mini-batch
   */
Y
Yan Chunwei 已提交
60
  void InferShape(const framework::Scope& scope) const override {
Q
Qiao Longfei 已提交
61 62 63 64
    for (auto& op : ops_) {
      op->InferShape(scope);
    }
  }
S
Superjom 已提交
65 66 67 68 69 70 71 72

  /**
   * @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.
   */
Y
Yan Chunwei 已提交
73
  void Run(const framework::Scope& scope,
Q
Qiao Longfei 已提交
74 75 76 77 78
           const platform::DeviceContext& dev_ctx) const override {
    for (auto& op : ops_) {
      op->Run(scope, dev_ctx);
    }
  }
S
Superjom 已提交
79

80 81 82 83 84 85 86 87
  bool SupportGPU() const override {
    for (auto& op : ops_) {
      if (!op->SupportGPU()) {
        return false;
      }
    }
    return true;
  }
S
Superjom 已提交
88 89

  /**
Q
Qiao Longfei 已提交
90
   * @brief Add an operator by ptr
S
Superjom 已提交
91
   */
Y
Yu Yang 已提交
92
  void AddOp(const std::shared_ptr<OperatorBase>& op) {
Q
Qiao Longfei 已提交
93
    PADDLE_ENFORCE(!add_op_done_, "Cannot AddOp when this network is sealed");
Y
Yan Chunwei 已提交
94
    PADDLE_ENFORCE_NOT_NULL(op, "Cannot Insert Null op");
Q
Qiao Longfei 已提交
95 96
    ops_.push_back(op);
  }
S
Superjom 已提交
97

Y
Yu Yang 已提交
98 99 100
  void InsertOp(size_t pos, const std::shared_ptr<OperatorBase>& op) {
    PADDLE_ENFORCE(!add_op_done_,
                   "Cannot InsertOp when this network is sealed");
Y
Yan Chunwei 已提交
101 102
    PADDLE_ENFORCE_NOT_NULL(op, "Cannot Insert Null op");
    PADDLE_ENFORCE_LE(pos, ops_.size(), "Out of range");
Y
Yu Yang 已提交
103 104 105
    ops_.insert(ops_.begin() + pos, op);
  }

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

108 109
  std::string DebugString() const override;

Y
Yu Yang 已提交
110
  bool IsNetOp() const override;
111
  std::vector<std::string> OutputVars(bool has_intermediate) const override;
Y
Yu Yang 已提交
112 113

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

Y
Yu Yang 已提交
115
  std::vector<std::shared_ptr<OperatorBase>> ops_;
S
Superjom 已提交
116

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

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

Y
Yan Chunwei 已提交
127
}  // namespace operators
S
Superjom 已提交
128
}  // namespace paddle