12 #ifndef ETEQ_PARSE_HPP 13 #define ETEQ_PARSE_HPP 20 std::vector<double> arr;
21 for (
auto it = list->
head_;
nullptr != it; it = it->
next_)
23 arr.push_back(it->val_);
35 std::vector<double> clist =
vectorize(list);
39 std::copy(clist.begin(), clist.end(), carr.begin());
40 out = std::make_shared<CoordMap>(carr,
false);
79 "cannot find any id `%s` in conversion",
any_id_.c_str());
82 logs::fatal(
"context value is not any");
84 return *(val.begin());
106 logs::fatal(
"created a rule argument with null argument");
124 template <
typename T>
135 for (
auto& arg :
args_)
142 auto tens = arg.arg_->build(ctx, childshape);
145 arg.shaper_, arg.coorder_));
153 std::vector<std::string>
args;
156 std::back_inserter(
args),
159 return arg.arg_->to_string();
161 return opcode_.
name_ + fmts::sprintf(
"(%s)", fmts::join(
",",
173 template <
typename T>
187 for (
auto& arg :
args_)
194 args.push_back(arg.arg_->build(ctx, childshape));
201 args.insert(
args.end(), varargs.begin(), varargs.end());
204 outs.reserve(
args.size());
205 std::transform(
args.begin(),
args.end(),
206 std::back_inserter(outs),
213 return tenncor::sum(outs)->get_tensor();
215 return tenncor::prod(outs)->get_tensor();
221 std::vector<std::string>
args;
224 std::back_inserter(
args),
227 return arg.arg_->to_string();
233 return fmts::sprintf(
"group:%s(%s)",
group_.c_str(),
234 fmts::join(
",",
args.begin(),
args.end()).c_str());
248 template <
typename T>
257 if (
auto f = dynamic_cast<teq::iOperableFunc*>(tens))
260 T* data = (T*) f->data();
273 logs::fatal(
"cannot make builder with null subgraph");
279 out = std::make_shared<ScalarConvr<T>>(sg->val_.scalar_);
283 std::string symbol(sg->val_.any_);
284 if (
false == estd::has(ctx.
symbols_, symbol))
286 logs::fatalf(
"undeclared symbol `%s`", symbol.c_str());
288 out = std::make_shared<AnyConvr<T>>(symbol);
293 ::Branch* branch = sg->val_.branch_;
294 if (
nullptr == branch)
296 logs::fatal(
"subgraph ended at nullptr branch");
307 std::string label(branch->
label_);
311 if (variadic.size() > 0 &&
false == estd::has(ctx.
symbols_, variadic))
313 logs::warnf(
"unknown variadic %s", variadic.c_str());
316 out = std::make_shared<GroupConvr<T>>(
317 label,
args, variadic);
321 out = std::make_shared<FuncConvr<T>>(label,
args);
326 logs::fatalf(
"unknown subgraph node type %d", sg->type_);
339 std::vector<double> slist =
vectorize(list);
340 if (slist.size() > 0)
342 out = std::make_shared<teq::CoordMap>(
350 if (
index < slist.size())
352 m[i][j] = slist[
index];
369 template <
typename T>
377 template <
typename T>
386 #endif // ETEQ_PARSE_HPP std::array< CDimT, rank_cap > CoordT
Definition: shape.hpp:56
teq::CoordptrT coorderize(::NumList *list) const override
Implementation of iConverterBuilder.
Definition: parse.hpp:362
std::unordered_set< std::string > symbols_
Set of declared ANY typed rule node id.
Definition: parse.hpp:22
std::string to_string(void) const override
Implementation of iConverter.
Definition: parse.hpp:59
NodeptrT< T > make_constant_scalar(T scalar, teq::Shape shape)
Return constant node given scalar and shape.
Definition: constant.hpp:146
args
Definition: csv_to_png.py:105
std::vector< BuilderArg > BuilderArgsT
Vector of FuncArg.
Definition: parse.hpp:121
Implementation of optimization converter that represents and builds subgraphs of specific types...
Definition: parse.hpp:174
std::set< teq::TensptrT > CtxValT
Set of tensors that potentially matches some id.
Definition: candidate.hpp:23
FuncConvr(std::string op, BuilderArgsT args)
Definition: parse.hpp:127
teq::TensptrT build(const opt::ContexT &ctx, teq::Shape outshape) const override
Implementation of iConverter.
Definition: parse.hpp:131
Encoding of operation.
Definition: ifunctor.hpp:18
NodeptrT< T > make_functor(teq::Opcode opcode, ArgsT< T > args)
Return functor node given opcode and node arguments.
Definition: functor.hpp:278
GroupConvr(std::string group, BuilderArgsT args, std::string variadic)
Definition: parse.hpp:176
Implementation of optimization converter that represents and builds functors.
Definition: parse.hpp:125
Definition: constant.hpp:17
std::string to_string(void) const override
Implementation of iConverter.
Definition: parse.hpp:88
static CoordptrT coorderize(::NumList *list)
Definition: parse.hpp:28
Argument for a branching node.
Definition: def.h:54
opt::ConvptrT arg_
Optimization IR node.
Definition: parse.hpp:111
struct NumNode * head_
Head of the linked list.
Definition: list.h:29
int is_group_
Definition: def.h:19
Eigen node version of teq::FuncArg.
Definition: funcarg.hpp:22
std::shared_ptr< iCoordMap > CoordptrT
Type of iCoordMap smartpointer.
Definition: coord.hpp:106
teq::TensptrT build(const opt::ContexT &ctx, teq::Shape outshape) const override
Implementation of iConverter.
Definition: parse.hpp:183
std::string name_
String representation of operation.
Definition: ifunctor.hpp:21
struct NumNode * next_
Next node in list (null denoting end of list)
Definition: list.h:19
Decimal linked list.
Definition: list.h:26
const RankT mat_dim
Number of rows and columns for the homogeneous matrix.
Definition: matops.hpp:22
Branching node.
Definition: def.h:33
Interface for TEQ extensions to construct conversion rules.
Definition: parse.hpp:30
std::vector< NodeptrT< T > > NodesT
Vector of nodes.
Definition: inode.hpp:67
AnyConvr(std::string any_id)
Definition: parse.hpp:72
std::shared_ptr< CoordMap > CoordptrT
Type of iCoordMap smartpointer.
Definition: coord.hpp:64
struct NumList * shaper_
Shape mapping meta-data.
Definition: def.h:60
struct PtrList * args_
Definition: def.h:20
Encapsulation of all conversion rules.
Definition: optimize.hpp:23
CoordptrT coorder_
Argument coordinate transformation data.
Definition: parse.hpp:117
Branching (Functor or Group) node.
Definition: def.h:17
opt::ConvptrT build(const ::Subgraph *sg, const opt::RulesContext &ctx) const override
Implementation of iConverterBuilder.
Definition: parse.hpp:268
std::map< std::string, CtxValT > ContexT
Map of rule graph leaf identifiers to corresponding matches.
Definition: candidate.hpp:26
struct NumList * coorder_
Coordinate mapping meta-data.
Definition: def.h:63
FuncArg equivalent for optimizer's IR of functor.
Definition: parse.hpp:98
static std::vector< double > vectorize(::NumList *list)
Definition: parse.hpp:18
teq::CoordptrT shaper_
Argument shaper.
Definition: parse.hpp:114
BuilderArg(opt::ConvptrT arg, teq::CoordptrT shaper, CoordptrT coorder)
Definition: parse.hpp:100
teq::CoordptrT shaperize(::NumList *list) const override
Implementation of iConverterBuilder.
Definition: parse.hpp:332
opt::CstConvertF build_cconv(void) const override
Implementation of iConverterBuilder.
Definition: parse.hpp:252
Rule tree leaf that represents any real node.
Definition: def.h:31
std::shared_ptr< iConverter > ConvptrT
Smart pointer of converter.
Definition: iconverter.hpp:36
double scalar_
Scalar represented.
Definition: parse.hpp:65
std::string to_string(void) const override
Implementation of iConverter.
Definition: parse.hpp:151
char variadic_[32]
Definition: def.h:22
OptCtx parse(std::string content, const iConverterBuilder &builder)
Return all parsed optimization rules of string content.
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
std::function< teq::TensptrT(teq::iTensor *)> CstConvertF
Definition: optimize.hpp:20
std::string to_string(teq::CoordptrT c)
Return brief hashable string representation of coordinate mapper.
std::vector< TensptrT > TensptrsT
Vector of tensor smart pointers.
Definition: itensor.hpp:60
Global statements shared between all rule statements.
Definition: parse.hpp:19
std::vector< FuncArg< T > > ArgsT
Type of typed functor arguments.
Definition: funcarg.hpp:84
opt::OptCtx parse(std::string content)
Return optimization rules tied to ETEQ Builder specified in content.
Definition: parse.hpp:370
teq::Opcode opcode_
Operator being represented.
Definition: parse.hpp:166
teq::TensptrT build(const opt::ContexT &ctx, teq::Shape outshape) const override
Implementation of iConverter.
Definition: parse.hpp:52
Optimization builder's implementation for building ETEQ nodes.
Definition: parse.hpp:249
OptCtx parse_file(std::string filename, const iConverterBuilder &builder)
Return all parsed optimization rules of a file.
Interface of traversible and differentiable nodes with shape information.
Definition: itensor.hpp:36
std::string any_id_
String id of the any node.
Definition: parse.hpp:94
NElemT index(Shape shape, CoordT coord)
BuilderArgsT args_
Arguments of the functor.
Definition: parse.hpp:169
Implementation of optimization converter that represents any node.
Definition: parse.hpp:70
Implementation of optimization converter that represents and builds scalar constants.
Definition: parse.hpp:47
std::string group_
Group name.
Definition: parse.hpp:238
Converter interface for building TEQ graphs.
Definition: iconverter.hpp:22
struct PtrNode * head_
Head of the linked list.
Definition: list.h:62
ScalarConvr(double scalar)
Definition: parse.hpp:49
opt::OptCtx parse_file(std::string filename)
Return optimization rules tied to ETEQ Builder specified in file.
Definition: parse.hpp:378
char label_[32]
Definition: def.h:21
NodeptrT< T > make_constant(T *data, teq::Shape shape)
Return constant node given raw array and shape.
Definition: constant.hpp:157
Shape apply_shaper(const CoordptrT &shaper, Shape inshape)
struct Subgraph * subgraph_
Subgraph node.
Definition: def.h:57
double[mat_dim][mat_dim] MatrixT
Coordinate transformation matrix (using homogeneous)
Definition: matops.hpp:28
std::string variadic_
Variadic Any id.
Definition: parse.hpp:244
struct PtrNode * next_
Next node in list (null denoting end of list)
Definition: list.h:52
Definitive scalar constant.
Definition: def.h:29
#define TO_NODE(tens)
Macro for converting tensor to node.
Definition: inode.hpp:106
std::string to_string(void) const override
Implementation of iConverter.
Definition: parse.hpp:219
bool is_identity(iCoordMap *coorder)
Checks if the coord mapper is an identity mapper.
teq::TensptrT build(const opt::ContexT &ctx, teq::Shape outshape) const override
Implementation of iConverter.
Definition: parse.hpp:75
BuilderArgsT args_
Arguments of the group.
Definition: parse.hpp:241