From 651f9a93896a8b6f0d5df89c2ffb343507f7a0fd Mon Sep 17 00:00:00 2001 From: Superjom Date: Wed, 21 Jun 2017 18:22:51 +0800 Subject: [PATCH 1/2] add net_design doc --- doc/design/refactor_discuess/net_design.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 doc/design/refactor_discuess/net_design.md diff --git a/doc/design/refactor_discuess/net_design.md b/doc/design/refactor_discuess/net_design.md new file mode 100644 index 0000000000000..9d7c1724fe888 --- /dev/null +++ b/doc/design/refactor_discuess/net_design.md @@ -0,0 +1,14 @@ +# Overfiew + +整体设计上参考 caffe2的SimpleNet,即Net 是一组 operator 的集合,包含了operator相关的操作,比如Create, RunOps, Delete 等。 + +Net 有如下一些特性: + +- Net 管理其拥有的operator +- Net 本身不占用任何Variable资源 +- Net 的调用方式类似 Functor,即 `output = net(inputs, scope)` ,其中 + - scope提供net执行所需要的全局Variable + - 输入一个或多个variable, `inputs` + - 返回一个variable, `output` + +# API From d6f2abc8ed84e4c8748e8ed2164feaffa784fee4 Mon Sep 17 00:00:00 2001 From: Superjom Date: Wed, 21 Jun 2017 20:20:14 +0800 Subject: [PATCH 2/2] net design in en --- doc/design/refactor_discuess/net_design.md | 69 +++++++++++++++++++--- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/doc/design/refactor_discuess/net_design.md b/doc/design/refactor_discuess/net_design.md index 9d7c1724fe888..86031cc0ae244 100644 --- a/doc/design/refactor_discuess/net_design.md +++ b/doc/design/refactor_discuess/net_design.md @@ -1,14 +1,65 @@ -# Overfiew +# Network Design -整体设计上参考 caffe2的SimpleNet,即Net 是一组 operator 的集合,包含了operator相关的操作,比如Create, RunOps, Delete 等。 +`Network` is the container and controller of a set of operators in a network, users can use `Network.addOp` to add operators into a network, +and use `Network.runOps` to run all the operators in the network. -Net 有如下一些特性: +The `Network` will -- Net 管理其拥有的operator -- Net 本身不占用任何Variable资源 -- Net 的调用方式类似 Functor,即 `output = net(inputs, scope)` ,其中 - - scope提供net执行所需要的全局Variable - - 输入一个或多个variable, `inputs` - - 返回一个variable, `output` +- manage all the operators contained in the network. +- not own any `Variable`. # API + +To make the `Network` extendibe, a base class is defined like this + +```c++ +// The minimum a network should be implemented. +class BaseNetwork { +public: + BaseNetwork(const NetDef& def, Scope *scope); + + // run all the operators and return success(true) or not. + virtual bool Run() = 0; + +protected: + // the input variables feed into the network. + std::vector external_inputs_; + // the corresponding output variables the network will write. + std::vector external_outputs_; + // scope which contains all the global variables visiable to this network. + Scope *scope_; +}; +``` + +A simple implemention is as followed: + +```c++ +class Network : public BaseNetwork { +public: + + // Create an empty network. + Network(const std::string& name, Scope *scope); + + // NetDef is the definition of a network, in some occasion, operators are created + // dynamically by user one by one; but in some other occasion such as LSTM, all + // the operators in the networks should be created during the construction + // of the network. So a `NetDef` is provided to make the `Network` create a + // network with all the operators described in `def`. + Network(const std::string& name, const NetDef& def, Scope *scope); + + // add a operator which is identified as `type` and has attributes described + // in `attr`. + bool AddOp(const std::string &type, const OprAttr& attr); + + // run all operators in oprs_ sequentially. + virtual bool Run() override; + +protected: + // to make the network's structure more human-readable, each network will + // has a `name` + std::string name_; + // the operations are owned by `Network`. + std::vector> oprs_; +}; +``` +