Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
base_layer.h
1 #ifndef INCLUDE_BASE_LAYER_H_
2 #define INCLUDE_BASE_LAYER_H_
3 
4 #include <vector>
5 #include <string>
6 #include <map>
7 #include <functional>
8 #include <utility>
9 #include <memory>
10 #include <chrono>
11 #include <algorithm>
12 #include <thread>
13 
14 #include "proto/model.pb.h"
15 #include "utils/param.h"
16 #include "utils/common.h"
17 #include "utils/blob.h"
18 
19 using std::vector;
20 using std::shared_ptr;
21 using std::make_shared;
22 using std::string;
23 using std::map;
24 
25 namespace singa{
26 
27 class Layer;
28 typedef shared_ptr<Layer> SLayer;
37 class Layer {
38  public:
39  Layer(){}
40  virtual ~Layer(){}
49  virtual void Init(const LayerProto &proto);
54  void Init(const Layer& other, const vector<int>& shape);
64  virtual void ToProto(LayerProto *layer_proto, bool copyData);
73  virtual void Setup(const LayerProto& proto,
74  const vector<SLayer>& srclayers)=0;
78  virtual void Setup();
87  virtual void SetupAfterPartition(const LayerProto& proto,
88  const vector<int> &shape,
89  const vector<SLayer>& srclayers)=0;
94  virtual void SetupAfterPartition();
100  virtual vector<shared_ptr<Param>> GetParams(){
101  return vector<shared_ptr<Param>>();
102  }
112  virtual void ComputeFeature(bool training, const vector<SLayer>& srclayers)=0;
116  virtual void ComputeFeature(bool training);
125  virtual void ComputeGradient(const vector<SLayer>& srclayers)=0;
129  virtual void ComputeGradient();
136  virtual int partition_dimension() const {
137  int ret=0;
138  if(partition_type()==kLayerPartition)
139  ret= 1;
140  else if(partition_type()==kNone)
141  ret= -1;
142  return ret;
143  }
144 
153  virtual ConnectionType connection_type(int k) const {
154  CHECK_LT(k, srclayers_.size());
155  return kOneToOne;
156  }
160  virtual PartitionType partition_type() const {
161  return layer_proto_.partition_type();
162  }
166  virtual void set_partitionid(int id){
167  layer_proto_.set_partitionid(id);
168  }
169  virtual int partitionid() const {
170  return layer_proto_.partitionid();
171  }
172  virtual void set_name(string name){
173  name_=name;
174  layer_proto_.set_name(name);
175  }
176  virtual const string type() const {
177  return layer_proto_.type();
178  }
182  const std::string &name() const {
183  return layer_proto_.name();
184  }
191  const std::string &datablob() const {
192  return layer_proto_.datablob();
193  }
194  const vector<int>& shape(const Layer* layer) const{
195  return data(layer).shape();
196  }
197 
201  virtual const Blob<float>& data(const Layer* from) const {
202  return data_;
203  }
204  virtual Blob<float>* mutable_data(const Layer* from){
205  return &data_;
206  }
207 
208  virtual const Blob<float>& grad(const Layer* from) const {
209  return grad_;
210  }
214  virtual Blob<float>* mutable_grad(const Layer* from) {
215  return &grad_;
216  }
217 
221  virtual const vector< SLayer> srclayers() const {
222  return srclayers_;
223  }
227  virtual const vector<SLayer> dstlayers() const {
228  return dstlayers_;
229  }
230 
231  virtual const int srclayers_size() const {
232  return srclayers_.size();
233  }
234  virtual const int dstlayers_size() const {
235  return dstlayers_.size();
236  }
237  virtual void ClearDstLayers() {
238  dstlayers_.clear();
239  }
240  virtual void ClearSrcLayers() {
241  srclayers_.clear();
242  }
243 
244  virtual void AddSrcLayer(SLayer src){
245  srclayers_.push_back(src);
246  }
247  virtual void AddDstLayer(SLayer dst){
248  dstlayers_.push_back(dst);
249  }
250 
251  virtual bool is_datalayer() const {
252  return false;
253  }
254  virtual bool is_parserlayer() const {
255  return false;
256  }
257  virtual bool is_losslayer() const {
258  return false;
259  }
260  virtual bool is_bridgesrclayer() const {
261  return false;
262  }
263  virtual bool is_bridgedstlayer() const {
264  return false;
265  }
266 protected:
267  string name_;
268  Blob<float> data_, grad_;
269  LayerProto layer_proto_;
270  vector<SLayer> srclayers_, dstlayers_;
271 };
272 
277 class BridgeSrcLayer: public Layer {
278  public:
279  using Layer::Setup;
280  using Layer::ComputeFeature;
282 
283  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
284  virtual void SetupAfterPartition();
285  virtual void SetupAfterPartition(const LayerProto& proto,
286  const vector<int> &shape,
287  const vector<SLayer>& srclayers){}
288 
289  virtual void ComputeFeature(bool training, const vector<SLayer>& srclayers);
290  virtual void ComputeGradient(const vector<SLayer>& srclayers);
291  virtual const Blob<float>& data(const Layer* from) const {
292  return srclayers_[0]->data(this);
293  }
294  virtual Blob<float>* mutable_data(const Layer* from){
295  return srclayers_[0]->mutable_data(this);
296  }
297 
298  virtual const Blob<float>& grad(const Layer* from) const {
299  return srclayers_[0]->grad(this);
300  }
301  virtual Blob<float>* mutable_grad(const Layer* from) {
302  return srclayers_[0]->mutable_grad(this);
303  }
304  int dst_partition() const;
305  virtual bool is_bridgesrclayer() const {
306  return true;
307  }
308  virtual void set_ready(bool a) {
309  ready_=a;
310  }
311  virtual bool ready() const {
312  return ready_;
313  }
314  protected:
315  bool ready_;
316 };
321 class BridgeDstLayer: public Layer {
322  public:
323  using Layer::Setup;
324  using Layer::ComputeFeature;
326 
327  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
328  virtual void SetupAfterPartition();
329  virtual void SetupAfterPartition(const LayerProto& proto,
330  const vector<int> &shape,
331  const vector<SLayer>& srclayers){}
332 
333  virtual void ComputeFeature(bool training, const vector<SLayer>& srclayers){
334  ready_=false;
335  }
336  virtual void ComputeGradient(const vector<SLayer>& srclayers){}
337  virtual bool is_bridgedstlayer() const {
338  return true;
339  }
340  virtual void set_ready(bool a) {
341  ready_=a;
342  }
343  virtual bool ready() const {
344  return ready_;
345  }
346  protected:
347  bool ready_;
348 };
349 
353 class ConcateLayer: public Layer {
354  public:
355  using Layer::Setup;
356  using Layer::ComputeFeature;
358 
359  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
360  virtual void SetupAfterPartition();
361  virtual void SetupAfterPartition(const LayerProto& proto,
362  const vector<int> &shape,
363  const vector<SLayer>& srclayers){}
364 
365  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
366  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
367 };
368 
369 
375 class DataLayer: public Layer{
376  public:
377  using Layer::Setup;
378  using Layer::ComputeFeature;
380 
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 {
384  return true;
385  }
386  virtual void ComputeGradient(const vector<SLayer>& srclayers){};
387  virtual const vector<Record>& records() const {
388  return records_;
389  }
390  virtual void Setup(){
391  vector<SLayer> dummy;
392  Setup(layer_proto_,dummy);
393  has_setup_=true;
394  }
395  virtual void SetupAfterPartition(const LayerProto& proto,
396  const vector<int> &shape,
397  const vector<SLayer>& srclayers){}
398 
399  virtual void SetupAfterPartition(){
400  if(!has_setup_)
401  Setup();
402  }
403  virtual PartitionType partition_type () const {
404  return kNone;
405  }
406 
407  virtual int batchsize() const {
408  return layer_proto_.data_param().batchsize();
409  }
410  virtual const Record& sample() const {
411  return sample_;
412  }
413 
414  virtual Blob<float>* mutable_data(const Layer* layer) {
415  return nullptr;
416  }
417  virtual Blob<float>* mutable_grad(const Layer* layer) {
418  return nullptr;
419  }
420  protected:
421  bool has_setup_;
422  int random_skip_, batchsize_;
423  Record sample_;
424  vector<Record> records_;
425 };
426 
434 class PrefetchLayer : public Layer {
435  public:
436  using Layer::Setup;
437  using Layer::ComputeFeature;
440 
441  virtual ~PrefetchLayer();
442  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
443  virtual void ComputeFeature(bool training, const vector<SLayer>& srclayers);
444  virtual void ComputeGradient(const vector<SLayer>& srclayers){};
445  virtual void SetupAfterPartition(const LayerProto& proto,
446  const vector<int> &shape,
447  const vector<SLayer>& srclayers){}
448 
449  virtual const Blob<float>& data(const Layer* from) const ;
450  virtual Blob<float>* mutable_data(const Layer* layer) ;
451 
452  virtual Blob<float>* mutable_grad(const Layer* layer){
453  return nullptr;
454  }
455  virtual const Blob<float>& grad(const Layer* from) const {
456  CHECK(false)<<"Loss layer has not gradient blob";
457  return grad_;
458  }
459  virtual PartitionType partition_type () const {
460  return kNone;
461  }
462 
463  void Prefetch(bool training);
464  protected:
465  vector<shared_ptr<Layer>> sublayers_;
466  map<string, Blob<float>> datablobs_;
467  std::thread thread_;
468 };
469 
473 class SliceLayer: public Layer {
474  public:
475  using Layer::Setup;
476  using Layer::ComputeFeature;
478 
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);
482  virtual void SetupAfterPartition();
483  virtual void SetupAfterPartition(const LayerProto& proto,
484  const vector<int> &shape,
485  const vector<SLayer>& srclayers){}
486 
487  virtual const Blob<float>& data(const Layer* layer) const;
488  virtual const Blob<float>& grad(const Layer* layer) const;
489  virtual Blob<float>* mutable_data(const Layer* layer);
490  virtual Blob<float>* mutable_grad(const Layer* layer);
491  protected:
492  int SliceID(const Layer* layer) const;
493  vector<Blob<float>> datavec_, gradvec_;
494  int slice_dim_, slice_num_;
495 };
496 
501 class SplitLayer: public Layer {
502  public:
503  using Layer::Setup;
504  using Layer::ComputeFeature;
506 
507  virtual void Setup(const LayerProto& proto, const vector<SLayer>& srclayers);
508  virtual void SetupAfterPartition();
509  virtual void SetupAfterPartition(const LayerProto& proto,
510  const vector<int> &shape,
511  const vector<SLayer>& srclayers){}
512 
513  virtual void ComputeFeature(bool training, const vector<shared_ptr<Layer>>& srclayers);
514  virtual void ComputeGradient(const vector<shared_ptr<Layer>>& srclayers);
515 };
516 
520 class LossLayer: public Layer{
521  public:
522  using Layer::Setup;
524 
525  virtual void Setup(const LayerProto& proto,
526  const vector<SLayer>& srclayers)=0;
527  virtual void SetupAfterPartition(const LayerProto& proto,
528  const vector<int> &shape,
529  const vector<SLayer>& srclayers)=0;
530 
531  virtual Blob<float>* mutable_grad(const Layer* layer){
532  return nullptr;
533  }
534  virtual const Blob<float>& grad(const Layer* from) const {
535  CHECK(false)<<"Loss layer has not gradient blob";
536  return grad_;
537  }
538  virtual bool is_losslayer() const {
539  return true;
540  }
541  virtual const Blob<float>& metric() const {
542  return metric_;
543  }
544  protected:
545  Blob<float> metric_;
546 };
547 
551 class ParserLayer: public Layer {
552  public:
553  using Layer::Setup;
555  using Layer::ComputeFeature;
557 
558  virtual void Setup(const LayerProto& proto,
559  const vector<SLayer>& srclayers)=0;
565  virtual void ParseRecords(bool training, const vector<Record>& records,
566  Blob<float>* blob)=0;
567 
568  virtual bool is_parserlayer() const {
569  return true;
570  }
571 
572  virtual void ComputeFeature(bool training, const vector<SLayer>& srclayers);
576  virtual void ComputeGradient(const vector<SLayer>& srclayers){};
577  virtual void Setup(){
578  Setup(layer_proto_,srclayers_);
579  has_setup_=true;
580  }
581  virtual void SetupAfterPartition(){
582  if(!has_setup_)
583  Setup();
584  }
585  virtual void SetupAfterPartition(const LayerProto& proto,
586  const vector<int> &shape,
587  const vector<SLayer>& srclayers){}
588 
589  virtual PartitionType partition_type () const{
590  return kNone;
591  }
592  virtual Blob<float>* mutable_grad(const Layer* layer) {
593  return nullptr;
594  }
595  virtual const Blob<float>& grad(const Layer* from) const {
596  CHECK(false)<<"Parser layer has not gradient blob";
597  return grad_;
598  }
599 
600  private:
601  bool has_setup_;
602 };
603 } // singa
604 
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