|
|
@ -14,6 +14,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "paddle/framework/net_proto.pb.h"
|
|
|
|
|
|
|
|
#include "paddle/framework/op_proto.pb.h"
|
|
|
|
#include "paddle/framework/scope.h"
|
|
|
|
#include "paddle/framework/scope.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
namespace paddle {
|
|
|
@ -27,31 +29,11 @@ typedef int OpIndex;
|
|
|
|
* keep updating if the concepts related are implemented.
|
|
|
|
* keep updating if the concepts related are implemented.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// Operator's runtime context.
|
|
|
|
|
|
|
|
struct OpContext {
|
|
|
|
|
|
|
|
int dev_id;
|
|
|
|
|
|
|
|
DevType dev_type{kCPU};
|
|
|
|
|
|
|
|
enum DevType { kCPU, kGPU };
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Proto definitions, use `struct`s for simpility.
|
|
|
|
|
|
|
|
struct VarDesc {
|
|
|
|
|
|
|
|
std::string type;
|
|
|
|
|
|
|
|
std::vector<int> dims;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OpDesc {
|
|
|
|
|
|
|
|
std::string type;
|
|
|
|
|
|
|
|
std::vector<VarDesc> inputs;
|
|
|
|
|
|
|
|
std::vector<VarDesc> outputs;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct struct NetDesc {
|
|
|
|
|
|
|
|
std::vector<OpDesc> ops;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class Operator {
|
|
|
|
class Operator {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
Operator(const OpDesc &def) {}
|
|
|
|
Operator(const OpDesc &def) {}
|
|
|
|
Error InferShape() {}
|
|
|
|
bool InferShape() {}
|
|
|
|
Error Run() {}
|
|
|
|
bool Run() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -73,7 +55,7 @@ class Net {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Infer shapes of all inputs and outputs of operators.
|
|
|
|
* @brief Infer shapes of all inputs and outputs of operators.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
virtual Error InferShape(Scope *scope) override;
|
|
|
|
virtual bool InferShape(Scope *scope) override;
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Run the network.
|
|
|
|
* @brief Run the network.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -82,8 +64,8 @@ class Net {
|
|
|
|
* environment for ops. `begin` and `end` specify the scope of `ops_` to run,
|
|
|
|
* environment for ops. `begin` and `end` specify the scope of `ops_` to run,
|
|
|
|
* If no positive indexes are provided, all operators in `ops_` will run.
|
|
|
|
* If no positive indexes are provided, all operators in `ops_` will run.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
virtual Error Run(Scope *scope, OpContext *context, OpIndex begin = -1,
|
|
|
|
virtual bool Run(Scope *scope, OpContext *context, OpIndex begin = -1,
|
|
|
|
OpIndex end = -1) const = 0;
|
|
|
|
OpIndex end = -1) const = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Add an Operator according to `def`.
|
|
|
|
* @brief Add an Operator according to `def`.
|
|
|
@ -93,12 +75,12 @@ class Net {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Add optimizer operators acctording to `attrs`.
|
|
|
|
* @brief Add optimizer operators acctording to `attrs`.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
virtual Error AddOptimizerOps(const OptAttrs &attrs) = 0;
|
|
|
|
virtual bool AddOptimizerOps(const OptAttrs &attrs) = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Add backward operators.
|
|
|
|
* @brief Add backward operators.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
virtual Error AddBackwardOps() = 0;
|
|
|
|
virtual bool AddBackwardOps() = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Create a network.
|
|
|
|
* @brief Create a network.
|
|
|
@ -126,7 +108,7 @@ class PlainNet : public Net {
|
|
|
|
* Infer all the operators' input and output varialbes' shapes, will be called
|
|
|
|
* Infer all the operators' input and output varialbes' shapes, will be called
|
|
|
|
* before every mini-batch
|
|
|
|
* before every mini-batch
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
virtual Error InferShape(Scope *scope) override;
|
|
|
|
virtual bool InferShape(Scope *scope) override;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Run the network.
|
|
|
|
* @brief Run the network.
|
|
|
@ -135,8 +117,8 @@ class PlainNet : public Net {
|
|
|
|
* scope will be used instead. If no OpContext is provicded, default context
|
|
|
|
* scope will be used instead. If no OpContext is provicded, default context
|
|
|
|
* will be used.
|
|
|
|
* will be used.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
virtual Error Run(Scope *scope = nullptr, OpContext *context = nullptr,
|
|
|
|
virtual bool Run(Scope *scope = nullptr, OpContext *context = nullptr,
|
|
|
|
OpIndex begin = -1, OpIndex end = -1) const override;
|
|
|
|
OpIndex begin = -1, OpIndex end = -1) const override;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Add an operator to this network.
|
|
|
|
* @brief Add an operator to this network.
|
|
|
@ -146,12 +128,12 @@ class PlainNet : public Net {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Add all optimizer operators related into the network.
|
|
|
|
* @brief Add all optimizer operators related into the network.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
virtual Error AddOptimizerOps(const OptAttrs &attrs) override;
|
|
|
|
virtual bool AddOptimizerOps(const OptAttrs &attrs) override;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Add all backward operators related into the network.
|
|
|
|
* @brief Add all backward operators related into the network.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
virtual Error AddBackwardOps() override;
|
|
|
|
virtual bool AddBackwardOps() override;
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -159,7 +141,7 @@ class PlainNet : public Net {
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Create operators accordding to `def`, will be called by the constructor.
|
|
|
|
* Create operators accordding to `def`, will be called by the constructor.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
Error BuildNet(const NetDesc &def);
|
|
|
|
bool BuildNet(const NetDesc &def);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Add an operator into this network.
|
|
|
|
* @brief Add an operator into this network.
|
|
|
|