DLPrimitives
net.hpp
1 #pragma once
2 #include <dlprim/operator.hpp>
3 #include <map>
4 #include <vector>
5 #include <list>
6 
7 namespace dlprim {
8  class SharedResource;
9  class ModelBase;
10 
14  class Net {
15  public:
16 
20  Net(Context &ctx);
21 
22  ~Net() {}
23  Net const &operator=(Net const &) = delete;
24  Net(Net const &) = delete;
25 
26  Net &operator=(Net &&) = default;
27  Net(Net &&) = default;
28 
32  void load_model(ModelBase &model);
36  void load_from_json(json::value const &v);
40  void load_from_json_file(std::string const &name);
41 
45  void add_input_tensor(std::string const &name,TensorSpecs const &ts,bool requires_gradient=false);
49  void mark_output_tensor(std::string const &name);
54  void set_loss_weight(std::string const &name,float loss_weight=1.0);
55 
59  void load_parameters(std::istream &s,bool allow_missing=false);
63  void load_parameters(std::string const &name,bool allow_missing=false);
67  void load_parameters(ModelBase &model,bool allow_missing=false);
68 
72  void save_parameters(std::string const &fname);
73 
77  void load_parameters_from_hdf5(std::string const &fname,bool allow_missing=false);
81  void save_parameters_to_hdf5(std::string const &fname);
82 
87  {
88  return keep_intermediate_tensors_;
89  }
94  void keep_intermediate_tensors(bool keep)
95  {
96  keep_intermediate_tensors_ = keep;
97  }
98 
106  void add_operator( std::unique_ptr<Operator> op,
107  std::string const &name,
108  std::vector<std::string> const &inputs,
109  std::vector<std::string> const &outputs,
110  std::vector<std::string> const &parameters = std::vector<std::string>(),
111  bool frozen=false);
112 
117  void mode(CalculationsMode mode);
118 
121  {
122  return mode_;
123  }
124 
129  std::shared_ptr<SharedResource> shared_resource()
130  {
131  return shared_resource_;
132  }
133 
137  void setup();
141  void reshape();
142 
147 
148  void copy_parameters_to_device();
149  void copy_parameters_to_host();
150  void clear_memory();
151 
156  std::map<std::string,Tensor> &tensors()
157  {
158  return tensors_;
159  }
164  std::map<std::string,Tensor> &tensor_diffs()
165  {
166  return tensors_diff_;
167  }
168 
172  std::map<std::string,Tensor> &params()
173  {
174  return parameters_;
175  }
179  std::map<std::string,Tensor> &param_diffs()
180  {
181  return parameters_diff_;
182  }
183 
185  Tensor &tensor(std::string const &name)
186  {
187  auto p=tensors_.find(name);
188  if(p == tensors_.end())
189  throw ValidationError("Unknown tensor name:" + name);
190  return p->second;
191  }
193  Tensor &tensor_diff(std::string const &name)
194  {
195  auto p=tensors_diff_.find(name);
196  if(p == tensors_diff_.end())
197  throw ValidationError("Unknown tensor name:" + name);
198  return p->second;
199  }
200 
202  Tensor &param(std::string const &name)
203  {
204  auto p=parameters_.find(name);
205  if(p == parameters_.end())
206  throw ValidationError("Unknown parameter name:" + name);
207  return p->second;
208  }
210  Tensor &param_diff(std::string const &name)
211  {
212  auto p=parameters_diff_.find(name);
213  if(p == parameters_diff_.end())
214  throw ValidationError("Unknown parameter name:" + name);
215  return p->second;
216  }
217 
218  void forward(ExecutionContext const &ectx,bool sync=false);
219  void backward(ExecutionContext const &ectx,bool sync=false);
220 
222  std::vector<std::string> const &input_names()
223  {
224  return inputs_;
225  }
226 
228  std::vector<std::string> const &output_names()
229  {
230  return outputs_;
231  }
232 
234  Tensor &input(unsigned id)
235  {
236  if(id >= inputs_.size())
237  throw ValidationError("Invalid input id");
238  return tensor(inputs_[id]);
239  }
240 
242  Tensor &output(unsigned id)
243  {
244  if(id >= outputs_.size())
245  throw ValidationError("Invalid output id");
246  return tensor(outputs_[id]);
247  }
248 
249  private:
250  struct Connection {
251  std::string name;
252  std::unique_ptr<Operator> op;
253 
254  std::vector<std::string> parameter_names;
255  std::vector<std::string> input_names;
256  std::vector<Tensor> input_tensors;
257  std::vector<TensorSpecs> input_specs;
258 
259  std::vector<std::string> output_names;
260  std::vector<Tensor> output_tensors;
261  std::vector<TensorSpecs> output_specs;
262 
263  std::vector<TensorSpecs> parameter_specs;
264  std::vector<Tensor> parameters;
265 
266  std::vector<TensorAndGradient> in_grad;
267  std::vector<TensorAndGradient> out_grad;
268  std::vector<TensorAndGradient> param_grad;
269  size_t ws_size;
270  int gradient_flags;
271  bool frozen;
272  };
273 
274  void setup_ws();
275  void mark_backpropagating_edges();
276  void allocate_tensors();
277  void allocate_aliases();
278  bool is_loss(std::string const &name);
279  void allocate_optimized_chunks(bool forward_only);
280  void tensor_use_list(std::vector<std::list<std::string> > &start,
281  std::vector<std::list<std::string> > &stop);
282  void allocate_chunks();
283  void load_header(std::istream &f,json::value &v);
284 
285 
286  Context ctx_;
287  std::map<std::string,TensorSpecs> tensor_specs_;
288  std::map<std::string,TensorSpecs> parameter_specs_;
289 
290  std::vector<Connection> connections_;
291  std::map<std::string,unsigned> connections_index_;
292 
293  Tensor workspace_;
294  std::map<std::string,std::string> alias_sources_;
295  std::map<std::string,Tensor> tensors_;
296  std::map<std::string,Tensor> tensors_diff_;
297 
298  std::map<std::string,Tensor> parameters_;
299  std::map<std::string,Tensor> parameters_diff_;
300  std::map<std::string,float> loss_weights_;
301  std::vector<std::string> inputs_;
302  std::vector<std::string> outputs_;
303  std::vector<Tensor> memory_;
304 
305  std::shared_ptr<SharedResource> shared_resource_;
306 
307  CalculationsMode mode_;
308  bool keep_intermediate_tensors_;
309  };
310 };
void save_parameters_to_hdf5(std::string const &fname)
Save all network parameters to a file.
std::map< std::string, Tensor > & tensors()
Get all intermediate tensors (operators inputs/outputs). Note if keep_intermediate_tensors() == false...
Definition: net.hpp:156
void load_parameters_from_hdf5(std::string const &fname, bool allow_missing=false)
Copy all parameters for hdf5 file format. allow_missing - ignore if parameter is not defined...
void mark_output_tensor(std::string const &name)
Make a tensor as an output tensor (will be preserverd) by name.
Definition of Tensor without actual memory/object.
Definition: tensor.hpp:11
Tensor & input(unsigned id)
Get input tensors by their numeric id - according to the order in input_names()
Definition: net.hpp:234
void keep_intermediate_tensors(bool keep)
Set if to keed intermediate results for debugging. Default is false - optimise memory use and reuse i...
Definition: net.hpp:94
std::vector< std::string > const & input_names()
Get of all network inputs.
Definition: net.hpp:222
void add_input_tensor(std::string const &name, TensorSpecs const &ts, bool requires_gradient=false)
Define network input.
CalculationsMode mode() const
get currenct calcuations mode
Definition: net.hpp:120
void setup()
Allocate all tensors and prepare network data.
void initialize_parameters(ExecutionContext const &e)
Initialize all parameters for training.
Tensor & output(unsigned id)
Get output tensors by their numeric id - according to the order in output_names() ...
Definition: net.hpp:242
void save_parameters(std::string const &fname)
Save network parameters to DLP format.
CalculationsMode
Operation mode of layers - inference of training.
Definition: definitions.hpp:283
void load_from_json_file(std::string const &name)
Configure network graph from json.
void load_parameters(std::istream &s, bool allow_missing=false)
Load parameters from binary stream DLP format (file) s must be seekable.
void reshape()
reshape all tensors in network after reshaping the input data
Tensor & tensor(std::string const &name)
Get tensor by name, throws ValidationError if does not exist.
Definition: net.hpp:185
This is main object that represent the pair of OpenCL platform and device all other objects use it...
Definition: context.hpp:302
void load_model(ModelBase &model)
Load from external model, shortcut to load_from_json(mode.network()) + setup() + load_parameters(mode...
void load_from_json(json::value const &v)
Configure network graph from json.
std::shared_ptr< SharedResource > shared_resource()
Get SharedResource object that can be shared between all operators in same network allowing operator ...
Definition: net.hpp:129
This class is central representation of json objects.
Definition: json.hpp:652
Tensor & tensor_diff(std::string const &name)
Get tensor gradient by name, throws ValidationError if does not exist.
Definition: net.hpp:193
bool keep_intermediate_tensors() const
True if all intermediate results are kept.
Definition: net.hpp:86
Tensor & param(std::string const &name)
Get parameter by name, throws ValidationError if does not exist.
Definition: net.hpp:202
std::map< std::string, Tensor > & tensor_diffs()
Get all intermediate tensor (operators inputs/outputs) gradient. Note if keep_intermediate_tensors() ...
Definition: net.hpp:164
Thrown in case of invalid parameters.
Definition: definitions.hpp:46
Base class used for loading non-native model formats to dlprimitives.
Definition: model.hpp:11
Net(Context &ctx)
Create an empty network object for a context.
Major object used for inference.
Definition: net.hpp:14
Mane namespace.
Definition: context.hpp:9
Tensor & param_diff(std::string const &name)
Get parameter gradient by name, throws ValidationError if does not exist.
Definition: net.hpp:210
Central Data Contrainer - Tensor.
Definition: tensor.hpp:99
std::vector< std::string > const & output_names()
Get of all network outputs.
Definition: net.hpp:228
std::map< std::string, Tensor > & param_diffs()
All operator parameters gradients trainable and not trainable.
Definition: net.hpp:179
void set_loss_weight(std::string const &name, float loss_weight=1.0)
Make a tensor as an output loss tensor, regardless of its name (by default a name starting with "loss...
std::map< std::string, Tensor > & params()
All operator parameters trainable and not trainable.
Definition: net.hpp:172
void add_operator(std::unique_ptr< Operator > op, std::string const &name, std::vector< std::string > const &inputs, std::vector< std::string > const &outputs, std::vector< std::string > const &parameters=std::vector< std::string >(), bool frozen=false)
Add an operator op to the network. name should be unique.
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121