net.h 2.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

Q
Qiao Longfei 已提交
17 18
#include <paddle/framework/op_desc.pb.h>
#include <paddle/framework/operator.h>
D
dongzhihong 已提交
19 20
#include "paddle/framework/net_proto.pb.h"
#include "paddle/framework/op_proto.pb.h"
Q
Qiao Longfei 已提交
21
#include "paddle/framework/op_registry.h"
S
Superjom 已提交
22
#include "paddle/framework/scope.h"
D
dongzhihong 已提交
23
#include "paddle/platform/device_context.h"
S
Superjom 已提交
24 25 26 27

namespace paddle {
namespace framework {
/**
Q
Qiao Longfei 已提交
28 29 30
 * @brief Network is also a type of Operator
 *
 * It will manage the operators it has.
S
Superjom 已提交
31
 *
Q
Qiao Longfei 已提交
32
 * Network is the container and controller of a set of operators.
S
Superjom 已提交
33 34 35 36 37

 * 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 已提交
38
 * This is the base class of network, all the networks should implement the APIs
S
Superjom 已提交
39 40
 * it defines.
 */
Q
Qiao Longfei 已提交
41
class Net : public OperatorBase {
S
Superjom 已提交
42
 public:
Q
Qiao Longfei 已提交
43 44
  virtual void AddOp(const OperatorPtr& op) = 0;
  virtual void CompleteAddOp() = 0;
S
Superjom 已提交
45 46
};

Q
Qiao Longfei 已提交
47 48
using NetPtr = std::shared_ptr<Net>;

S
Superjom 已提交
49 50 51 52 53 54 55 56 57
/**
 * @brief a basic implementation of Net.
 *
 * PlainNet is a very simple Net, it create a list of operators, and run them
 * sequentially following the order they added.
 */
class PlainNet : public Net {
 public:
  /**
Q
Qiao Longfei 已提交
58
   * Infer all the operators' input and output variables' shapes, will be called
S
Superjom 已提交
59 60
   * before every mini-batch
   */
Q
Qiao Longfei 已提交
61 62 63 64 65
  void InferShape(const ScopePtr& scope) const override {
    for (auto& op : ops_) {
      op->InferShape(scope);
    }
  }
S
Superjom 已提交
66 67 68 69 70 71 72 73

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

  /**
Q
Qiao Longfei 已提交
82
   * @brief Add an operator by ptr
S
Superjom 已提交
83
   */
Q
Qiao Longfei 已提交
84 85 86 87
  void AddOp(const OperatorPtr& op) override {
    PADDLE_ENFORCE(!add_op_done_, "Cannot AddOp when this network is sealed");
    ops_.push_back(op);
  }
S
Superjom 已提交
88

Q
Qiao Longfei 已提交
89
  void CompleteAddOp() override;
S
Superjom 已提交
90

91 92
  std::string DebugString() const override;

Q
Qiao Longfei 已提交
93
  std::vector<OperatorPtr> ops_;
S
Superjom 已提交
94 95

 private:
Q
Qiao Longfei 已提交
96 97 98 99 100 101
  bool add_op_done_{false};

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

}  // namespace framework
}  // namespace paddle