Tenncor
layer.hpp
Go to the documentation of this file.
1 
9 #include "estd/estd.hpp"
10 
11 #include "tag/tag.hpp"
12 
13 #include "eteq/constant.hpp"
14 #include "eteq/variable.hpp"
15 
16 #include "eteq/generated/pyapi.hpp"
17 
18 #ifndef LAYR_LAYER_HPP
19 #define LAYR_LAYER_HPP
20 
21 namespace layr
22 {
23 
25 const std::string layers_key_prefix = "layer_";
26 
28 const char llabel_sep = ':';
29 
32 void validate_label (const std::string& label);
33 
35 struct LayerId final
36 {
37  LayerId (void) = default;
38 
39  LayerId (std::string label) : label_(label) {}
40 
41  LayerId (std::string type, std::string label, size_t index) :
42  type_(type), label_(label), index_(index) {}
43 
46  std::string to_string (std::string label) const
47  {
48  return fmts::sprintf("%s%c%s%c%s%c%d",
49  label.c_str(), llabel_sep,
50  type_.c_str(), llabel_sep,
51  label_.c_str(), llabel_sep,
52  index_);
53  }
54 
56  std::string type_;
57 
59  std::string label_;
60 
62  size_t index_ = 0;
63 };
64 
66 using LayerIdsT = std::vector<LayerId>;
67 
69 std::string layer_label_fmt (std::string label, LayerId subid);
70 
72 std::unordered_map<std::string,LayerIdsT> unpack_labels (
73  const std::vector<std::string>& labels);
74 
76 struct LayerTag final : public tag::iTag
77 {
78  LayerTag (std::string layer_type, std::string name) :
79  reps_({{layer_type, {name}}}) {}
80 
82  size_t tag_id (void) const override
83  {
84  return tag_id_;
85  }
86 
88  void absorb (tag::TagptrT&& other) override
89  {
90  LayersT& oreps =
91  static_cast<LayerTag*>(other.get())->reps_;
92  for (auto& reppair : oreps)
93  {
94  auto& names = reps_[reppair.first];
95  names.insert(reppair.second.begin(), reppair.second.end());
96  }
97  }
98 
100  tag::TagRepsT get_tags (void) const override
101  {
102  tag::TagRepsT out;
103  for (auto& layer : reps_)
104  {
105  out.emplace(layer.first, std::vector<std::string>(
106  layer.second.begin(), layer.second.end()));
107  }
108  return out;
109  }
110 
111 private:
112  using LayersT = std::map<std::string,std::unordered_set<std::string>>;
113 
115 
116  static size_t tag_id_;
117 };
118 
121 struct iLayer
122 {
123  virtual ~iLayer (void) = default;
124 
126  iLayer* clone (std::string label_prefix = "") const
127  {
128  return this->clone_impl(label_prefix);
129  }
130 
132  virtual size_t get_ninput (void) const = 0;
133 
135  virtual size_t get_noutput (void) const = 0;
136 
138  virtual std::string get_ltype (void) const = 0;
139 
141  virtual std::string get_label (void) const = 0;
142 
144  virtual teq::TensptrsT get_contents (void) const = 0;
145 
147  virtual NodeptrT connect (NodeptrT input) const = 0;
148 
149 protected:
150  virtual iLayer* clone_impl (const std::string& label_prefix) const = 0;
151 
152  void tag (teq::TensptrT tensor, LayerId subs) const;
153 
154  void recursive_tag (teq::TensptrT root,
155  teq::TensSetT ignores, LayerId subs) const;
156 };
157 
159 using LayerptrT = std::shared_ptr<iLayer>;
160 
165 {
166  virtual ~iLayerBuilder (void) = default;
167 
169  virtual void set_tensor (teq::TensptrT tens, std::string target) = 0;
170 
172  virtual void set_sublayer (LayerptrT layer) = 0;
173 
175  virtual LayerptrT build (void) const = 0;
176 };
177 
179 using LBuilderptrT = std::shared_ptr<iLayerBuilder>;
180 
182 using LayerBuildF = std::function<LBuilderptrT(std::string)>;
183 
186 struct LayerRegistry final
187 {
188  LayerRegistry (tag::TagRegistry& registry = tag::get_reg()) : tag_reg_(registry) {}
189 
191  void layer_tag (teq::TensrefT tens, std::string layer_type, std::string name)
192  {
193  tag_reg_.add_tag(tens, tag::TagptrT(new LayerTag(layer_type, name)));
194  }
195 
198  std::string register_tagr (std::string key, LayerBuildF builder)
199  {
200  lbuilders_.emplace(key, builder); // todo: remove tagr since it's not used
201 
202  return tag_reg_.register_tagr(key,
203  [this, key](teq::TensrefT ref, std::string label)
204  {
205  this->layer_tag(ref, key, label);
206  });
207  }
208 
210  LayerBuildF get_builder (std::string layer_type)
211  {
212  return estd::must_getf(lbuilders_, layer_type,
213  "failed to find registered layer `%s`", layer_type.c_str());
214  }
215 
218  {
219  return tag_reg_;
220  }
221 
222 private:
223  std::unordered_map<std::string,LayerBuildF> lbuilders_;
224 
226 };
227 
230 
233 void recursive_layer_tag (teq::TensptrT tens, std::string layer_type,
234  std::string name, teq::TensSetT stops,
235  LayerRegistry& registry = get_layer_reg());
236 
239 LayerptrT load_layer (std::istream& ins, teq::TensptrsT& roots,
240  std::string ltype, std::string label,
241  LayerRegistry& registry = get_layer_reg());
242 
245 bool save_layer (std::ostream& outs, const iLayer& layer, teq::TensptrsT roots,
246  LayerRegistry& registry = get_layer_reg());
247 
248 }
249 
250 #endif // LAYR_LAYER_HPP
void recursive_layer_tag(teq::TensptrT tens, std::string layer_type, std::string name, teq::TensSetT stops, LayerRegistry &registry=get_layer_reg())
virtual NodeptrT connect(NodeptrT input) const =0
Return the root of the graph that connects input with internal tensors.
LayerId(void)=default
std::unordered_set< teq::iTensor * > TensSetT
Hash set of raw tensor pointers.
Definition: itensor.hpp:63
virtual ~iLayerBuilder(void)=default
void validate_label(const std::string &label)
std::unordered_map< std::string, LayerIdsT > unpack_labels(const std::vector< std::string > &labels)
Return raw labels mapped to sublayers given a vector of formatted labels.
std::string layer_label_fmt(std::string label, LayerId subid)
Return formatted raw label with associated sublayer.
iLayer * clone(std::string label_prefix="") const
Return deep copy of this layer with prefixed label.
Definition: layer.hpp:126
LayerRegistry & get_layer_reg(void)
Return global layer registry reference.
Definition: tag.hpp:25
const char llabel_sep
Layer label separator to divide each element in the LayerId.
Definition: layer.hpp:28
virtual size_t get_ninput(void) const =0
Return input value of the expected input (first dimension)
Definition: layer.hpp:186
Registry for associating tensors to tag collectives.
Definition: tag.hpp:165
std::string to_string(std::string label) const
Definition: layer.hpp:46
virtual std::string get_label(void) const =0
Return the raw layer label.
std::vector< LayerId > LayerIdsT
Vector of sublayer ids.
Definition: layer.hpp:66
std::string register_tagr(std::string key, LayerBuildF builder)
Definition: layer.hpp:198
void recursive_tag(teq::TensptrT root, teq::TensSetT ignores, LayerId subs) const
Definition: conv.hpp:16
virtual teq::TensptrsT get_contents(void) const =0
Return all internal tensors representing the layer.
Tag implementation specifically for contents of layers.
Definition: layer.hpp:76
LayersT reps_
Definition: layer.hpp:114
LayerId(std::string label)
Definition: layer.hpp:39
std::shared_ptr< iLayerBuilder > LBuilderptrT
Layer builder smart pointer.
Definition: layer.hpp:179
std::unique_ptr< iTag > TagptrT
Unique pointer of tag.
Definition: tag.hpp:40
size_t tag_id(void) const override
Implementation of iTag.
Definition: layer.hpp:82
std::map< std::string, std::vector< std::string > > TagRepsT
Map tag key to a series of labels.
Definition: tag.hpp:21
tag::TagRegistry & tag_reg_
Definition: layer.hpp:225
std::unordered_map< std::string, LayerBuildF > lbuilders_
Definition: layer.hpp:223
virtual LayerptrT build(void) const =0
Return the layer built to contain set tensors and sublayers.
virtual size_t get_noutput(void) const =0
Return output value of the expected output (first dimension)
virtual void set_tensor(teq::TensptrT tens, std::string target)=0
Set internal tensors that make up the output layer.
std::string register_tagr(std::string tag_key, TagrF tagr)
Definition: tag.hpp:232
const std::string layers_key_prefix
String prefixed to every layer key.
Definition: layer.hpp:25
Definition: layer.hpp:164
LayerRegistry(tag::TagRegistry &registry=tag::get_reg())
Definition: layer.hpp:188
void absorb(tag::TagptrT &&other) override
Implementation of iTag.
Definition: layer.hpp:88
virtual std::string get_ltype(void) const =0
Return the layer type which is also the tag key of tagged contents.
std::map< std::string, std::unordered_set< std::string > > LayersT
Definition: layer.hpp:112
LayerId(std::string type, std::string label, size_t index)
Definition: layer.hpp:41
std::string label_
Sublayer label.
Definition: layer.hpp:59
void tag(teq::TensptrT tensor, LayerId subs) const
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
std::vector< TensptrT > TensptrsT
Vector of tensor smart pointers.
Definition: itensor.hpp:60
Sublayer type, label, and index encapsulation.
Definition: layer.hpp:35
std::function< LBuilderptrT(std::string)> LayerBuildF
Function that takes layer type and returns associated layer builder.
Definition: layer.hpp:182
TagRegistry & get_reg(void)
Return reference to global tag registry.
LayerTag(std::string layer_type, std::string name)
Definition: layer.hpp:78
LayerptrT load_layer(std::istream &ins, teq::TensptrsT &roots, std::string ltype, std::string label, LayerRegistry &registry=get_layer_reg())
tag::TagRegistry & get_tag_registry(void)
Return wrapped tag registry refence.
Definition: layer.hpp:217
NElemT index(Shape shape, CoordT coord)
bool save_layer(std::ostream &outs, const iLayer &layer, teq::TensptrsT roots, LayerRegistry &registry=get_layer_reg())
void add_tag(teq::TensrefT tens, TagptrT tag)
Add tag to collective referenced by tens.
Definition: tag.hpp:168
std::shared_ptr< iNode< T > > NodeptrT
Smart pointer of node.
Definition: inode.hpp:63
std::string type_
Sublayer type.
Definition: layer.hpp:56
void layer_tag(teq::TensrefT tens, std::string layer_type, std::string name)
Tag tens reference with layer type and label.
Definition: layer.hpp:191
tag::TagRepsT get_tags(void) const override
Implementation of iTag.
Definition: layer.hpp:100
LayerBuildF get_builder(std::string layer_type)
Return builder associated with layer type.
Definition: layer.hpp:210
virtual void set_sublayer(LayerptrT layer)=0
Set internal sublayers that make up the output layer.
std::weak_ptr< iTensor > TensrefT
Tensor weak pointers.
Definition: itensor.hpp:54
std::shared_ptr< iLayer > LayerptrT
Smart pointer of layer.
Definition: layer.hpp:159
size_t index_
Sublayer index.
Definition: layer.hpp:62
virtual ~iLayer(void)=default
virtual iLayer * clone_impl(const std::string &label_prefix) const =0
static size_t tag_id_
Definition: layer.hpp:116
Definition: layer.hpp:121