Apache Singa
A General Distributed Deep Learning Library
layer.h
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef SINGA_MODEL_LAYER_H_
20 #define SINGA_MODEL_LAYER_H_
21 
22 #include <vector>
23 #include <string>
24 #include <stack>
25 #include <utility>
26 #include <memory>
27 #include "singa/core/tensor.h"
28 #include "singa/proto/model.pb.h"
29 #include "singa/utils/factory.h"
30 
31 namespace singa {
32 
33 typedef vector<size_t> Shape;
37 class Layer {
38  public:
39  Layer() = default;
40 
43  void Setup(const Shape& in_shape, const string& proto_str) {
44  LayerConf conf;
45  conf.ParseFromString(proto_str);
46  this->Setup(in_shape, conf);
47  }
48 
50  void Setup(const vector<Shape>& in_shapes, const string& proto_str) {
51  LayerConf conf;
52  conf.ParseFromString(proto_str);
53  this->Setup(in_shapes, conf);
54  }
55 
56 
57  // ============= Following Functions could be override =====================
59  virtual ~Layer() {};
60 
63  virtual const std::string layer_type() const { return "Unknown"; }
64 
72  virtual void Setup(const Shape& in_sample, const LayerConf& conf) {
73  name_ = conf.name();
74  // TODO(wangwei) load param values from checkpoint files.
75  }
76 
78  virtual void Setup(const vector<Shape>& in_samples, const LayerConf& conf) {
79  name_ = conf.name();
80  // TODO(wangwei) load param values from checkpoint files.
81  }
82 
84  virtual const Shape GetOutputSampleShape() const {
85  LOG(FATAL) << "Pls override this function";
86  return vector<size_t>{};
87  }
90  virtual const Shape GetOutputSampleShape(int k) {
91  LOG(FATAL) << "Pls override this function";
92  return vector<size_t>{};
93  }
94 
103  virtual const Tensor Forward(int flag, const Tensor& input) {
104  LOG(FATAL) << "Not implemented";
105  Tensor t;
106  return t;
107  }
108 
114  virtual const vector<Tensor> Forward(int flag, const vector<Tensor>& inputs) {
115  vector<Tensor> ret;
116  if (inputs.size() == 1) ret.push_back(Forward(flag, inputs.at(0)));
117 
118  LOG(FATAL) << "Not implemented";
119  return ret;
120  }
121 
128  // layer has no parameters.
132  virtual const std::pair<Tensor, vector<Tensor>> Backward(int flag,
133  const Tensor& grad) {
134  LOG(FATAL) << "Not implemented!";
135  Tensor t;
136  return std::make_pair(t, vector<Tensor>{});
137  }
138 
141  virtual const std::pair<vector<Tensor>, vector<Tensor>> Backward(
142  int flag, const vector<Tensor>& grads) {
143  vector<Tensor> input_grad, param_grad;
144  if (grads.size() == 1u) {
145  auto ret = Backward(flag, grads.at(0));
146  input_grad.push_back(ret.first);
147  param_grad = ret.second;
148  } else {
149  LOG(FATAL) << "Not implemented";
150  }
151  return std::make_pair(input_grad, param_grad);
152  }
153 
156  // virtual Layer* Clone(std::shared_ptr<Device> device);
159  virtual void ToDevice(std::shared_ptr<Device> device) {
160  }
161 
163  virtual void AsType(DataType dtype) {
164  }
165 
167  virtual void ToProto(LayerConf* conf) const {
168  //conf->set_name(name_);
169  //for (const auto& spec : param_specs_) {
170  // ParamSpec* p = conf->add_param();
171  // p->CopyFrom(spec);
172  //}
173  // TODO(wangwei) add param values into conf;
174  }
175 
176  // ========================================================================
177 
180  std::string ToProtoStr() const {
181  LayerConf conf;
182  ToProto(&conf);
183  string str;
184  conf.SerializeToString(&str);
185  return str;
186  }
189  const vector<ParamSpec> param_specs() { return param_specs_; }
190 
192  const ParamSpec& param_specs(size_t i) {
193  CHECK_LT(i, param_specs_.size());
194  return param_specs_.at(i);
195  }
196 
198  virtual const vector<Tensor> param_values() {
199  return vector<Tensor>{};
200  }
201 
203  const vector<string> param_names() {
204  vector<string> pname;
205  for (const auto& spec : param_specs_) pname.push_back(spec.name());
206  return pname;
207  }
208 
210  const string& param_name(size_t i) {
211  CHECK_LT(i, param_specs_.size());
212  return param_specs_.at(i).name();
213  }
214 
217  const std::string name() const { return name_; }
218 
219  protected:
220  std::string name_;
221  vector<ParamSpec> param_specs_;
222 };
223 
235 #define RegisterLayerClass(Name, SubLayer) \
236  static Registra<Layer, SubLayer> Name##SubLayer(#Name);
237 
238 inline std::shared_ptr<Layer> CreateLayer(const std::string type) {
239  std::shared_ptr<Layer> layer(Factory<Layer>::Create(type));
240  return layer;
241 }
242 
243 inline const std::vector<std::string> GetRegisteredLayers() {
244  vector<std::string> ret;
245  for (const string type : Factory<Layer>::GetIDs()) {
246  auto layer = CreateLayer(type);
247  ret.push_back("Register type: " + type);
248  }
249  return ret;
250 }
251 } // namespace singa
252 #endif // SINGA_MODEL_LAYER_H_
const string & param_name(size_t i)
Return the &#39;i&#39;-th parameter name.
Definition: layer.h:210
const std::string name() const
Each layer instance would optionally have a name.
Definition: layer.h:217
const vector< string > param_names()
Return names of all parmaeters.
Definition: layer.h:203
virtual void ToProto(LayerConf *conf) const
Serialize the layer info (including params) into a LayerConf proto message.
Definition: layer.h:167
virtual const std::string layer_type() const
Each layer sub-class would optionaly have a type name.
Definition: layer.h:63
void Setup(const Shape &in_shape, const string &proto_str)
Set meta data fields from a string representing a proto message.
Definition: layer.h:43
const ParamSpec & param_specs(size_t i)
Return the i-th ParamSpec.
Definition: layer.h:192
virtual const Shape GetOutputSampleShape() const
Return the shape of the generated Tensor without the batchsize dimension.
Definition: layer.h:84
Factory template to generate class (or a sub-class) object based on id.
Definition: factory.h:45
virtual const std::pair< Tensor, vector< Tensor > > Backward(int flag, const Tensor &grad)
Compute gradients of this layer.
Definition: layer.h:132
std::string ToProtoStr() const
Serialize the layer info, including params_, into a string representing a LayerParameter message...
Definition: layer.h:180
virtual ~Layer()
Destruct objects created by this layer.
Definition: layer.h:59
virtual void ToDevice(std::shared_ptr< Device > device)
Clone the layer to the given device.
Definition: layer.h:159
virtual void Setup(const Shape &in_sample, const LayerConf &conf)
Set meta data fields configured in &#39;conf&#39; (a proto message).
Definition: layer.h:72
virtual const Shape GetOutputSampleShape(int k)
Return the shape of the k-th generated tensor without the batchsize dimension.
Definition: layer.h:90
virtual const vector< Tensor > param_values()
Return pointers to parameter Tensor s.
Definition: layer.h:198
virtual void Setup(const vector< Shape > &in_samples, const LayerConf &conf)
Used for layers that have multiple input tensors, e.g., concatenate layer.
Definition: layer.h:78
const vector< ParamSpec > param_specs()
Return specs/configuration of all parameter instances of this layer.
Definition: layer.h:189
A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU)...
Definition: tensor.h:56
virtual const vector< Tensor > Forward(int flag, const vector< Tensor > &inputs)
Do feature transformation for the given &#39;input&#39; tensor (denoted as x).
Definition: layer.h:114
void Setup(const vector< Shape > &in_shapes, const string &proto_str)
&#39;in_shapes&#39; is the shape of the input feature for one sample
Definition: layer.h:50
virtual void AsType(DataType dtype)
Set the data type of Tensor in this layer.
Definition: layer.h:163
The base layer class.
Definition: layer.h:37
virtual const Tensor Forward(int flag, const Tensor &input)
Do feature transformation for the given &#39;input&#39; tensor (denoted as x).
Definition: layer.h:103
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48
virtual const std::pair< vector< Tensor >, vector< Tensor > > Backward(int flag, const vector< Tensor > &grads)
Definition: layer.h:141