Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
connection_layer.h
1 /************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 #ifndef SINGA_NEURALNET_CONNECTION_LAYER_H_
23 #define SINGA_NEURALNET_CONNECTION_LAYER_H_
24 
25 #include <vector>
26 #include "neuralnet/layer.h"
27 
32 namespace singa {
33 class BridgeLayer : virtual public ConnectionLayer {
34  public:
35  void set_ready(bool a) {
36  ready_ = a;
37  }
38  bool ready() const {
39  return ready_;
40  }
41  virtual bool is_bridgesrclayer() const {
42  return false;
43  }
44  virtual bool is_bridgedstlayer() const {
45  return false;
46  }
47 
48  protected:
50  bool ready_;
51 };
52 
57 class BridgeDstLayer : public BridgeLayer {
58  public:
59  void Setup(const LayerProto& proto, int npartitions) override;
60  void ComputeFeature(int flag, Metric* perf) override {
61  // reset ready_ for next iteration.
62  ready_ = false;
63  }
64  void ComputeGradient(int flag, Metric* perf) override {}
65  bool is_bridgedstlayer() const {
66  return true;
67  }
68 };
69 
74 class BridgeSrcLayer : public BridgeLayer {
75  public:
76  void ComputeFeature(int flag, Metric* perf) override {}
77  void ComputeGradient(int flag, Metric* perf) override {
78  ready_ = false;
79  }
80  const Blob<float>& data(const Layer* from) const override {
81  return srclayers_[0]->data(this);
82  }
83  Blob<float>* mutable_data(const Layer* from) override {
84  return srclayers_[0]->mutable_data(this);
85  }
86  const Blob<float>& grad(const Layer* from) const override {
87  return srclayers_[0]->grad(this);
88  }
89  Blob<float>* mutable_grad(const Layer* from) override {
90  return srclayers_[0]->mutable_grad(this);
91  }
92  bool is_bridgesrclayer() const override {
93  return true;
94  }
95 };
96 
97 
105  public:
106  void Setup(const LayerProto& proto, int npartitions) override;
107  void ComputeFeature(int flag, Metric* perf) override;
108  void ComputeGradient(int flag, Metric* perf) override;
109 };
110 
117 class SliceLayer : public ConnectionLayer {
118  public:
119  void Setup(const LayerProto& proto, int npartitions) override;
120  void ComputeFeature(int flag, Metric *perf) override;
121  void ComputeGradient(int flag, Metric* perf) override;
122 
123  private:
124  std::vector<Blob<float>> datavec_;
125  std::vector<Blob<float>> gradvec_;
126  int slice_dim_;
127  int slice_num_;
128 };
129 
137 class SplitLayer : public ConnectionLayer {
138  public:
139  void Setup(const LayerProto& proto, int npartitions) override;
140  void ComputeFeature(int flag, Metric* perf) override;
141  void ComputeGradient(int flag, Metric* perf) override;
142 
143  protected:
144  Blob<float> grads_;
145 };
146 
147 } // namespace singa
148 
149 #endif // SINGA_NEURALNET_CONNECTION_LAYER_H_
void ComputeFeature(int flag, Metric *perf) override
Compute features of this layer based on connected layers.
void ComputeFeature(int flag, Metric *perf) override
Compute features of this layer based on connected layers.
Definition: connection_layer.h:60
void ComputeFeature(int flag, Metric *perf) override
Compute features of this layer based on connected layers.
Definition: connection_layer.h:76
void ComputeFeature(int flag, Metric *perf) override
Compute features of this layer based on connected layers.
void Setup(const LayerProto &proto, int npartitions) override
Setup layer properties.
void ComputeFeature(int flag, Metric *perf) override
Compute features of this layer based on connected layers.
void ComputeGradient(int flag, Metric *perf) override
Compute gradients for parameters and connected layers.
Definition: connection_layer.h:64
Base layer for connecting layers when neural net is partitioned.
Definition: layer.h:193
Blob< float > * mutable_grad(const Layer *from) override
Definition: connection_layer.h:89
void ComputeGradient(int flag, Metric *perf) override
Compute gradients for parameters and connected layers.
Definition: connection_layer.h:77
bool ready_
< true if received grad from BridgeDstLayer
Definition: connection_layer.h:50
void Setup(const LayerProto &proto, int npartitions) override
Setup layer properties.
const Blob< float > & data(const Layer *from) const override
Definition: connection_layer.h:80
void Setup(const LayerProto &proto, int npartitions) override
Setup layer properties.
Definition: connection_layer.h:33
Base layer class.
Definition: layer.h:44
Connect a single (src) layer with multiple (dst) layers.
Definition: connection_layer.h:117
void ComputeGradient(int flag, Metric *perf) override
Compute gradients for parameters and connected layers.
void Setup(const LayerProto &proto, int npartitions) override
Setup layer properties.
Connect multiple (src) layers with a single (dst) layer.
Definition: connection_layer.h:104
void ComputeGradient(int flag, Metric *perf) override
Compute gradients for parameters and connected layers.
For recv data from layer on other threads which may resident on other nodes due to layer/data partito...
Definition: connection_layer.h:57
Performance mtrics.
Definition: common.h:85
Connect a single (src) layer with multiple dst layers.
Definition: connection_layer.h:137
void ComputeGradient(int flag, Metric *perf) override
Compute gradients for parameters and connected layers.
For sending data to layer on other threads which may resident on other nodes due to layer/data partit...
Definition: connection_layer.h:74