Tenncor
ivoter.hpp
Go to the documentation of this file.
1 
9 extern "C" {
10 #include "opt/parse/def.h"
11 }
12 
13 #include "opt/stats.hpp"
14 #include "opt/candidate.hpp"
15 
16 #ifndef OPT_IVOTER_HPP
17 #define OPT_IVOTER_HPP
18 
19 namespace opt
20 {
21 
23 struct VoterArg final
24 {
25  VoterArg (std::string label,
26  teq::CoordptrT shaper,
27  teq::CoordptrT coorder,
28  SUBGRAPH_TYPE type) :
29  label_(label),
30  shaper_(shaper),
31  coorder_(coorder),
32  type_(type) {}
33 
35  bool match (CtxsT& ctxs, const CandArg& arg) const
36  {
37  // match arg.shaper_ and arg.coorder_
38  if (false == is_equal(arg.shaper_, shaper_) ||
39  false == is_equal(arg.coorder_, coorder_))
40  {
41  return false;
42  }
43  switch (type_)
44  {
46  // look for scalar candidate in arg.candidates
47  return estd::has(arg.candidates_, Symbol{
48  CAND_TYPE::SCALAR,
49  label_,
50  });
52  {
53  // look for intermediate candidate in arg.candidates
54  auto it = arg.candidates_.find(Symbol{
56  label_,
57  });
58  if (arg.candidates_.end() == it)
59  {
60  return false;
61  }
62  auto& cand_ctxs = it->second;
63  CtxsT matching_ctxs;
64  if (ctxs.empty())
65  {
66  matching_ctxs = cand_ctxs;
67  }
68  for (const ContexT& ctx : ctxs)
69  {
70  for (ContexT cand_tx : cand_ctxs)
71  {
72  // if the cand_tx map and ctx have a
73  // non-conflicting intersection, mark as matched
74  if (std::all_of(ctx.begin(), ctx.end(),
75  [&](const std::pair<std::string,CtxValT>& ctxpair)
76  {
77  auto ait = cand_tx.find(ctxpair.first);
78  if (cand_tx.end() == ait)
79  {
80  return true;
81  }
82  auto& ctens = ctxpair.second;
83  auto& cand_ctens = ait->second;
84  return std::equal(ctens.begin(), ctens.end(),
85  cand_ctens.begin());
86  }))
87  {
88  // merge
89  cand_tx.insert(ctx.begin(), ctx.end());
90  matching_ctxs.emplace(cand_tx);
91  }
92  }
93  }
94  bool has_match = matching_ctxs.size() > 0;
95  if (has_match)
96  {
97  ctxs = matching_ctxs;
98  }
99  return has_match;
100  }
102  {
103  CtxsT matching_ctxs;
104  if (ctxs.empty())
105  {
106  matching_ctxs.emplace(ContexT{{label_, {arg.tensor_}}});
107  }
108  for (ContexT ctx : ctxs)
109  {
110  auto it = ctx.find(label_);
111  if (ctx.end() == it || estd::has(it->second, arg.tensor_))
112  {
113  // matching ANY
114  ctx.emplace(label_, CtxValT{arg.tensor_});
115  matching_ctxs.emplace(ctx);
116  }
117  }
118  bool has_match = matching_ctxs.size() > 0;
119  if (has_match)
120  {
121  ctxs = matching_ctxs;
122  }
123  return has_match;
124  }
125  }
126  return true;
127  }
128 
130  std::string label_;
131 
134 
137 
140 };
141 
143 using VoterArgsT = std::vector<VoterArg>;
144 
146 struct SegVArgs
147 {
148  size_t size (void) const
149  {
150  return scalars_.size() + branches_.size() + anys_.size();
151  }
152 
155 
158 
161 };
162 
164 void sort_vargs (VoterArgsT& args);
165 
167 struct OrdrHasher final
168 {
170  size_t operator() (const VoterArgsT& args) const
171  {
172  size_t seed = 0;
173  hash_combine(seed, args);
174  return seed;
175  }
176 
178  void hash_combine (size_t& seed, const VoterArgsT& args) const
179  {
180  for (const VoterArg& arg : args)
181  {
182  std::tuple<std::string,std::string,std::string,size_t>
183  hash_target = {
184  arg.label_,
185  to_string(arg.shaper_),
186  to_string(arg.coorder_),
187  arg.type_,
188  };
189  boost::hash_combine(seed, hash_target);
190  }
191  }
192 };
193 
195 inline bool operator == (const VoterArgsT& lhs, const VoterArgsT& rhs)
196 {
197  return lhs.size() == rhs.size() &&
198  std::equal(lhs.begin(), lhs.end(), rhs.begin(),
199  [](const VoterArg& l, const VoterArg& r)
200  {
201  return l.label_ == r.label_ &&
202  is_equal(l.shaper_, r.shaper_) &&
203  is_equal(l.coorder_, r.coorder_) &&
204  l.type_ == r.type_;
205  });
206 }
207 
209 struct CommHasher final
210 {
212  size_t operator() (const SegVArgs& args) const
213  {
214  size_t seed = 0;
215  hasher_.hash_combine(seed, args.scalars_);
216  hasher_.hash_combine(seed, args.branches_);
217  hasher_.hash_combine(seed, args.anys_);
218  return seed;
219  }
220 
223 };
224 
226 inline bool operator == (const SegVArgs& lhs, const SegVArgs& rhs)
227 {
228  return lhs.scalars_ == rhs.scalars_ &&
229  lhs.branches_ == rhs.branches_ &&
230  lhs.anys_ == rhs.anys_;
231 }
232 
234 struct iVoter
235 {
236  virtual ~iVoter (void) = default;
237 
240  virtual void emplace (VoterArgsT args, Symbol cand) = 0;
241 
243  virtual CandsT inspect (const CandArgsT& args) const = 0;
244 };
245 
247 using VotptrT = std::shared_ptr<iVoter>;
248 
250 struct VoterPool
251 {
253  std::unordered_set<std::string> immutables_;
254 
256  std::unordered_map<std::string,VotptrT> branches_;
257 };
258 
259 }
260 
261 #endif // OPT_IVOTER_HPP
bool is_equal(teq::CoordptrT a, teq::CoordptrT b)
Return true if a is equal to b.
CandsT candidates_
Potential rules contexts that can match subgraph of tensor_.
Definition: candidate.hpp:85
args
Definition: csv_to_png.py:105
virtual ~iVoter(void)=default
size_t operator()(const SegVArgs &args) const
Return hash of arguments.
Definition: ivoter.hpp:212
std::set< teq::TensptrT > CtxValT
Set of tensors that potentially matches some id.
Definition: candidate.hpp:23
teq::CoordptrT shaper_
Real shaper in the argument.
Definition: candidate.hpp:88
virtual CandsT inspect(const CandArgsT &args) const =0
Return virtual node graph candidates given candidate arguments.
VoterArgsT branches_
Branch-typed arguments (functors/groups)
Definition: ivoter.hpp:157
std::unordered_map< std::string, VotptrT > branches_
Map voter identifier to associated branch voters.
Definition: ivoter.hpp:256
std::shared_ptr< iCoordMap > CoordptrT
Type of iCoordMap smartpointer.
Definition: coord.hpp:106
teq::CoordptrT coorder_
Real coorder in the argument.
Definition: candidate.hpp:91
Definition: candidate.hpp:19
VoterArgsT anys_
Any-typed leaf arguments.
Definition: ivoter.hpp:160
Branching node.
Definition: def.h:33
teq::CoordptrT shaper_
Converted shape mapper.
Definition: ivoter.hpp:133
std::unordered_set< ContexT, boost::hash< ContexT > > CtxsT
Set of contexts that serve as a candidates of a conversion rule.
Definition: candidate.hpp:29
std::shared_ptr< iVoter > VotptrT
Smart pointer of rule tree.
Definition: ivoter.hpp:247
std::vector< CandArg > CandArgsT
Vector of candidate arguments.
Definition: candidate.hpp:95
std::string label_
Argument node identifier.
Definition: ivoter.hpp:130
VoterArg(std::string label, teq::CoordptrT shaper, teq::CoordptrT coorder, SUBGRAPH_TYPE type)
Definition: ivoter.hpp:25
Variadic/communtative branch voter arguments.
Definition: ivoter.hpp:146
void hash_combine(size_t &seed, const VoterArgsT &args) const
Apply boost::hash_combine for each argument.
Definition: ivoter.hpp:178
std::map< std::string, CtxValT > ContexT
Map of rule graph leaf identifiers to corresponding matches.
Definition: candidate.hpp:26
SUBGRAPH_TYPE type_
Subgraph type of the argument.
Definition: ivoter.hpp:139
Hash voter arguments while preserving order of arguments.
Definition: ivoter.hpp:167
Intermediate conversion.
Definition: candidate.hpp:39
Rule tree leaf that represents any real node.
Definition: def.h:31
std::unordered_map< Symbol, CtxsT, SymbolHash > CandsT
Map of convers symbols to its potential candidate conversion rules.
Definition: candidate.hpp:76
Encapsulation of match output argument.
Definition: candidate.hpp:79
teq::TensptrT tensor_
Real tensor of the argument.
Definition: candidate.hpp:82
VoterArgsT scalars_
Scalar-typed arguments.
Definition: ivoter.hpp:154
void sort_vargs(VoterArgsT &args)
Normalize voter arguments to facilitate matching.
size_t operator()(const VoterArgsT &args) const
Return hash of arguments.
Definition: ivoter.hpp:170
std::string to_string(teq::CoordptrT c)
Return brief hashable string representation of coordinate mapper.
Rule tree node that identify and selects matching candidates.
Definition: ivoter.hpp:234
SUBGRAPH_TYPE
Rule tree node type.
Definition: def.h:26
OrdrHasher hasher_
Internal ordered hasher used against normalized commutative args.
Definition: ivoter.hpp:222
std::unordered_set< std::string > immutables_
Set of immutable ids under rule tree.
Definition: ivoter.hpp:253
Hash variadic/commutative arguments that ignores order.
Definition: ivoter.hpp:209
Generic representation of a conversion rule.
Definition: candidate.hpp:45
size_t size(void) const
Definition: ivoter.hpp:148
std::vector< VoterArg > VoterArgsT
Vector of voter arguments for branching nodes.
Definition: ivoter.hpp:143
teq::CoordptrT coorder_
Converted coordinate mapping meta-structure.
Definition: ivoter.hpp:136
virtual void emplace(VoterArgsT args, Symbol cand)=0
Definitive scalar constant.
Definition: def.h:29
Parsed representation of a rule tree.
Definition: ivoter.hpp:250
bool operator==(const Symbol &lhs, const Symbol &rhs)
Compare equality of Symbols.
Definition: candidate.hpp:70
Argument voter for functors.
Definition: ivoter.hpp:23
bool match(CtxsT &ctxs, const CandArg &arg) const
Return true if arg matches this only add to ctxs if matches.
Definition: ivoter.hpp:35