1 #ifndef INCLUDE_BASE_LAYER_H_
2 #define INCLUDE_BASE_LAYER_H_
14 #include "proto/model.pb.h"
15 #include "utils/param.h"
16 #include "utils/common.h"
17 #include "utils/blob.h"
20 using std::shared_ptr;
21 using std::make_shared;
28 typedef shared_ptr<Layer> SLayer;
54 void Init(
const Layer& other,
const vector<int>& shape);
88 const vector<int> &shape,
89 const vector<SLayer>& srclayers)=0;
101 return vector<shared_ptr<Param>>();
112 virtual void ComputeFeature(
bool training,
const vector<SLayer>& srclayers)=0;
154 CHECK_LT(k, srclayers_.size());
161 return layer_proto_.partition_type();
167 layer_proto_.set_partitionid(
id);
169 virtual int partitionid()
const {
170 return layer_proto_.partitionid();
172 virtual void set_name(
string name){
174 layer_proto_.set_name(name);
176 virtual const string type()
const {
177 return layer_proto_.type();
182 const std::string &
name()
const {
183 return layer_proto_.name();
192 return layer_proto_.datablob();
194 const vector<int>& shape(
const Layer* layer)
const{
195 return data(layer).shape();
208 virtual const Blob<float>& grad(
const Layer* from)
const {
231 virtual const int srclayers_size()
const {
232 return srclayers_.size();
234 virtual const int dstlayers_size()
const {
235 return dstlayers_.size();
237 virtual void ClearDstLayers() {
240 virtual void ClearSrcLayers() {
244 virtual void AddSrcLayer(SLayer src){
245 srclayers_.push_back(src);
247 virtual void AddDstLayer(SLayer dst){
248 dstlayers_.push_back(dst);
251 virtual bool is_datalayer()
const {
254 virtual bool is_parserlayer()
const {
257 virtual bool is_losslayer()
const {
260 virtual bool is_bridgesrclayer()
const {
263 virtual bool is_bridgedstlayer()
const {
269 LayerProto layer_proto_;
270 vector<SLayer> srclayers_, dstlayers_;
283 virtual void Setup(
const LayerProto& proto,
const vector<SLayer>& srclayers);
286 const vector<int> &shape,
287 const vector<SLayer>& srclayers){}
289 virtual void ComputeFeature(
bool training,
const vector<SLayer>& srclayers);
292 return srclayers_[0]->data(
this);
295 return srclayers_[0]->mutable_data(
this);
298 virtual const Blob<float>& grad(
const Layer* from)
const {
299 return srclayers_[0]->grad(
this);
302 return srclayers_[0]->mutable_grad(
this);
304 int dst_partition()
const;
305 virtual bool is_bridgesrclayer()
const {
308 virtual void set_ready(
bool a) {
311 virtual bool ready()
const {
327 virtual void Setup(
const LayerProto& proto,
const vector<SLayer>& srclayers);
330 const vector<int> &shape,
331 const vector<SLayer>& srclayers){}
337 virtual bool is_bridgedstlayer()
const {
340 virtual void set_ready(
bool a) {
343 virtual bool ready()
const {
359 virtual void Setup(
const LayerProto& proto,
const vector<SLayer>& srclayers);
362 const vector<int> &shape,
363 const vector<SLayer>& srclayers){}
365 virtual void ComputeFeature(
bool training,
const vector<shared_ptr<Layer>>& srclayers);
366 virtual void ComputeGradient(
const vector<shared_ptr<Layer>>& srclayers);
381 virtual void ComputeFeature(
bool training,
const vector<SLayer>& srclayers)=0;
382 virtual void Setup(
const LayerProto& proto,
const vector<SLayer>& srclayers)=0;
383 virtual bool is_datalayer()
const {
387 virtual const vector<Record>& records()
const {
391 vector<SLayer> dummy;
392 Setup(layer_proto_,dummy);
396 const vector<int> &shape,
397 const vector<SLayer>& srclayers){}
407 virtual int batchsize()
const {
408 return layer_proto_.data_param().batchsize();
410 virtual const Record& sample()
const {
414 virtual Blob<float>* mutable_data(
const Layer* layer) {
422 int random_skip_, batchsize_;
424 vector<Record> records_;
442 virtual void Setup(
const LayerProto& proto,
const vector<SLayer>& srclayers);
443 virtual void ComputeFeature(
bool training,
const vector<SLayer>& srclayers);
446 const vector<int> &shape,
447 const vector<SLayer>& srclayers){}
456 CHECK(
false)<<
"Loss layer has not gradient blob";
463 void Prefetch(
bool training);
465 vector<shared_ptr<Layer>> sublayers_;
466 map<string, Blob<float>> datablobs_;
479 virtual void ComputeFeature(
bool training,
const vector<shared_ptr<Layer>>& srclayers);
480 virtual void ComputeGradient(
const vector<shared_ptr<Layer>>& srclayers);
481 virtual void Setup(
const LayerProto& proto,
const vector<SLayer>& srclayers);
484 const vector<int> &shape,
485 const vector<SLayer>& srclayers){}
492 int SliceID(
const Layer* layer)
const;
493 vector<Blob<float>> datavec_, gradvec_;
494 int slice_dim_, slice_num_;
507 virtual void Setup(
const LayerProto& proto,
const vector<SLayer>& srclayers);
510 const vector<int> &shape,
511 const vector<SLayer>& srclayers){}
513 virtual void ComputeFeature(
bool training,
const vector<shared_ptr<Layer>>& srclayers);
514 virtual void ComputeGradient(
const vector<shared_ptr<Layer>>& srclayers);
526 const vector<SLayer>& srclayers)=0;
528 const vector<int> &shape,
529 const vector<SLayer>& srclayers)=0;
535 CHECK(
false)<<
"Loss layer has not gradient blob";
538 virtual bool is_losslayer()
const {
559 const vector<SLayer>& srclayers)=0;
565 virtual void ParseRecords(
bool training,
const vector<Record>& records,
568 virtual bool is_parserlayer()
const {
572 virtual void ComputeFeature(
bool training,
const vector<SLayer>& srclayers);
578 Setup(layer_proto_,srclayers_);
586 const vector<int> &shape,
587 const vector<SLayer>& srclayers){}
596 CHECK(
false)<<
"Parser layer has not gradient blob";
605 #endif // INCLUDE_BASE_LAYER_H_
virtual const Blob< float > & data(const Layer *layer) const
parse the input records into Blobs.
Definition: base_layer.h:551
virtual void ComputeGradient()
Compute gradients for parameters and connecting layers.
Definition: model.pb.h:1128
virtual void set_partitionid(int id)
partition id is the ID of the layer in the original layer.
Definition: base_layer.h:166
virtual void SetupAfterPartition()
Setup the layer properties except shape.
virtual void ComputeFeature(bool training, const vector< SLayer > &srclayers)
Compute features of this layer based on connected layers.
Definition: base_layer.h:333
virtual void ComputeFeature(bool training, const vector< SLayer > &srclayers)
Compute features of this layer based on connected layers.
virtual void SetupAfterPartition(const LayerProto &proto, const vector< int > &shape, const vector< SLayer > &srclayers)
Setup the layer properties except shape.
Definition: base_layer.h:361
virtual void SetupAfterPartition(const LayerProto &proto, const vector< int > &shape, const vector< SLayer > &srclayers)
Setup the layer properties except shape.
Definition: base_layer.h:509
virtual PartitionType partition_type() const
Definition: base_layer.h:403
virtual void ToProto(LayerProto *layer_proto, bool copyData)
TODO(wangsheng) Marshal layer properties and data into google protobuf object (i.e., snapshot).
virtual void ParseRecords(bool training, const vector< Record > &records, Blob< float > *blob)=0
Parse records from DataLayer into blob.
virtual void Init(const LayerProto &proto)
Layer initialization.
virtual const vector< SLayer > srclayers() const
return LayerS that connected to this layer
Definition: base_layer.h:221
virtual const vector< SLayer > dstlayers() const
return LayerS that this layer connected to
Definition: base_layer.h:227
virtual const Blob< float > & data(const Layer *from) const
Definition: base_layer.h:291
virtual vector< shared_ptr< Param > > GetParams()
Layers that have paramters must overload this function.
Definition: base_layer.h:100
virtual Blob< float > * mutable_grad(const Layer *from)
Definition: base_layer.h:301
virtual void ComputeFeature(bool training, const vector< SLayer > &srclayers)=0
Compute features of this layer based on connected layers.
const std::string & name() const
Return name of this layer.
Definition: base_layer.h:182
virtual void Setup()
Setup layer properties.
virtual ConnectionType connection_type(int k) const
Return connection type between two layers.
Definition: base_layer.h:153
Base layer for reading records from local Shard, HDFS, lmdb, etc.
Definition: base_layer.h:375
virtual int partition_dimension() const
Decide on which dimension to do the partitioning.
Definition: base_layer.h:136
const std::string & datablob() const
Definition: base_layer.h:191
virtual void ComputeGradient(const vector< SLayer > &srclayers)
Dummy function.
Definition: base_layer.h:576
Loss layer to calculate loss and other metrics, e.g., precison.
Definition: base_layer.h:520
virtual void ComputeFeature(bool training, const vector< SLayer > &srclayers)=0
Compute features of this layer based on connected layers.
virtual void SetupAfterPartition()
Setup the layer properties except shape.
virtual Blob< float > * mutable_grad(const Layer *layer)
Definition: base_layer.h:452
virtual void ComputeFeature(bool training, const vector< SLayer > &srclayers)
Compute features of this layer based on connected layers.
virtual PartitionType partition_type() const
Definition: base_layer.h:459
virtual void SetupAfterPartition()
Setup the layer properties except shape.
virtual Blob< float > * mutable_grad(const Layer *layer)
Definition: base_layer.h:417
virtual void Setup()
Setup layer properties.
Definition: base_layer.h:577
virtual void SetupAfterPartition(const LayerProto &proto, const vector< int > &shape, const vector< SLayer > &srclayers)
Setup the layer properties except shape.
Definition: base_layer.h:329
virtual const Blob< float > & data(const Layer *from) const
virtual void SetupAfterPartition()
Setup the layer properties except shape.
Definition: base_layer.h:581
Layer for prefetching data records and parsing them.
Definition: base_layer.h:434
virtual void SetupAfterPartition(const LayerProto &proto, const vector< int > &shape, const vector< SLayer > &srclayers)
Setup the layer properties except shape.
Definition: base_layer.h:483
virtual PartitionType partition_type() const
Definition: base_layer.h:589
virtual PartitionType partition_type() const
Definition: base_layer.h:160
virtual Blob< float > * mutable_grad(const Layer *layer)
Definition: base_layer.h:592
virtual Blob< float > * mutable_grad(const Layer *layer)
virtual void SetupAfterPartition()
Setup the layer properties except shape.
Base layer class.
Definition: base_layer.h:37
virtual void SetupAfterPartition()
Setup the layer properties except shape.
Definition: base_layer.h:399
virtual void SetupAfterPartition(const LayerProto &proto, const vector< int > &shape, const vector< SLayer > &srclayers)
Setup the layer properties except shape.
Definition: base_layer.h:445
Slice the source layer into multiple dst layers on one dimension.
Definition: base_layer.h:473
virtual void Setup()
Setup layer properties.
Definition: base_layer.h:390
virtual Blob< float > * mutable_grad(const Layer *layer)
Definition: base_layer.h:531
Definition: model.pb.h:3042
virtual Blob< float > * mutable_grad(const Layer *from)
Definition: base_layer.h:214
Concate src layers on one dimension.
Definition: base_layer.h:353
virtual void SetupAfterPartition(const LayerProto &proto, const vector< int > &shape, const vector< SLayer > &srclayers)
Setup the layer properties except shape.
Definition: base_layer.h:395
For recv data from layer on other threads which may resident on other nodes due to layer/data partito...
Definition: base_layer.h:321
virtual void SetupAfterPartition(const LayerProto &proto, const vector< int > &shape, const vector< SLayer > &srclayers)
Setup the layer properties except shape.
Definition: base_layer.h:285
virtual void ComputeGradient(const vector< SLayer > &srclayers)
Compute gradients for parameters and connecting layers.
Definition: base_layer.h:444
Replciate this layer into multiple dst layers TODO change name to ReplicateLayer. ...
Definition: base_layer.h:501
virtual void SetupAfterPartition()
Setup the layer properties except shape.
virtual void SetupAfterPartition()
Setup the layer properties except shape.
virtual void ComputeFeature(bool training, const vector< SLayer > &srclayers)
Compute features of this layer based on connected layers.
For sending data to layer on other threads which may resident on other nodes due to layer/data partit...
Definition: base_layer.h:277
virtual void ComputeGradient(const vector< SLayer > &srclayers)
Compute gradients for parameters and connecting layers.
Definition: base_layer.h:386
virtual void ComputeGradient(const vector< SLayer > &srclayers)
Compute gradients for parameters and connecting layers.
Definition: base_layer.h:336
virtual const Blob< float > & data(const Layer *from) const
Definition: base_layer.h:201
virtual void SetupAfterPartition(const LayerProto &proto, const vector< int > &shape, const vector< SLayer > &srclayers)
Setup the layer properties except shape.
Definition: base_layer.h:585