24 #ifndef TVM_RELAY_DATAFLOW_PATTERN_H_
25 #define TVM_RELAY_DATAFLOW_PATTERN_H_
31 #include <unordered_map>
44 static constexpr
const char*
_type_key =
"DFPatternNode";
92 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.ExprPattern";
125 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.VarPattern";
144 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.ConstantPattern";
174 v->Visit(
"args", &
args);
177 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.CallPattern";
204 v->Visit(
"params", &
params);
205 v->Visit(
"body", &
body);
208 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.FunctionPattern";
240 v->Visit(
"var", &
var);
241 v->Visit(
"value", &
value);
242 v->Visit(
"body", &
body);
245 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.LetPattern";
275 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.TuplePattern";
295 v->Visit(
"tuple", &
tuple);
296 v->Visit(
"index", &
index);
299 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.TupleGetItemPattern";
308 v->Visit(
"cond", &
cond);
313 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.IfPattern";
341 v->Visit(
"left", &
left);
342 v->Visit(
"right", &
right);
345 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.AltPattern";
369 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.WildcardPattern";
396 v->Visit(
"type", &
type);
399 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.TypePattern";
425 v->Visit(
"shape", &
shape);
428 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.ShapePattern";
454 v->Visit(
"dtype", &
dtype);
457 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.DataTypePattern";
483 v->Visit(
"attrs", &
attrs);
486 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.AttrPattern";
515 v->Visit(
"parent", &
parent);
516 v->Visit(
"path", &
path);
517 v->Visit(
"child", &
child);
520 static constexpr
const char*
_type_key =
"relay.dataflow_pattern.DominatorPattern";
570 std::stringstream string_stream{}, tmp_stream{};
573 string_stream <<
"Main pattern:" << std::endl;
574 string_stream << printer.string_stream.str();
575 string_stream << std::endl;
576 string_stream <<
"Auxiliary patterns:";
577 for (
const DFPattern& pat : printer.auxiliary_patterns) {
578 string_stream << std::endl;
579 string_stream << printer.memo_[pat].second;
581 os << string_stream.str();
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Managed reference to DictAttrsNode.
Definition: attrs.h:227
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:64
Managed reference to RelayExprNode.
Definition: expr.h:442
A printer class to print the AST/IR nodes.
Definition: repr_printer.h:34
std::ostream & stream
The output stream.
Definition: repr_printer.h:37
Managed reference to TypeNode.
Definition: type.h:93
Pattern for Alternate Expressions.
Definition: dataflow_pattern.h:333
DFPattern right
The right optional pattern.
Definition: dataflow_pattern.h:338
DFPattern left
The left optional pattern.
Definition: dataflow_pattern.h:336
TVM_DECLARE_FINAL_OBJECT_INFO(AltPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:340
static constexpr const char * _type_key
Definition: dataflow_pattern.h:345
A pattern which matches either of two patterns.
Definition: dataflow_pattern.h:352
TVM_DEFINE_OBJECT_REF_METHODS(AltPattern, DFPattern, AltPatternNode)
AltPattern(DFPattern left, DFPattern right)
Pattern for Attributes.
Definition: dataflow_pattern.h:474
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:477
DictAttrs attrs
The attribute to match.
Definition: dataflow_pattern.h:479
static constexpr const char * _type_key
Definition: dataflow_pattern.h:486
TVM_DECLARE_FINAL_OBJECT_INFO(AttrPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:481
A pattern which matches attributes in another pattern.
Definition: dataflow_pattern.h:493
AttrPattern(DFPattern pattern, DictAttrs attrs)
TVM_DEFINE_OBJECT_REF_METHODS(AttrPattern, DFPattern, AttrPatternNode)
CallPattern container.
Definition: dataflow_pattern.h:159
TVM_DECLARE_FINAL_OBJECT_INFO(CallPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:177
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:172
tvm::Array< relay::DFPattern > args
The arguments(inputs) of the call.
Definition: dataflow_pattern.h:170
DFPattern op
The operator(function) being invoked.
Definition: dataflow_pattern.h:167
Definition: dataflow_pattern.h:181
CallPattern(DFPattern op, Array< DFPattern > args)
TVM_DEFINE_OBJECT_REF_METHODS(CallPattern, DFPattern, CallPatternNode)
Container for Constant.
Definition: dataflow_pattern.h:140
static constexpr const char * _type_key
Definition: dataflow_pattern.h:144
TVM_DECLARE_FINAL_OBJECT_INFO(ConstantPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:142
Definition: dataflow_pattern.h:148
TVM_DEFINE_OBJECT_REF_METHODS(ConstantPattern, DFPattern, ConstantPatternNode)
Base type of all dataflow patterns.
Definition: dataflow_pattern.h:42
TVM_DECLARE_BASE_OBJECT_INFO(DFPatternNode, Object)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:44
A printer class to print pattern.
Definition: dataflow_pattern.h:549
std::vector< DFPattern > auxiliary_patterns
Subpatterns that are encountered more than once during printing. If a subpattern has already printed,...
Definition: dataflow_pattern.h:559
std::stringstream string_stream
Definition: dataflow_pattern.h:551
DFPatternPrinter(std::ostream &stream)
Definition: dataflow_pattern.h:561
std::unordered_map< DFPattern, std::pair< size_t, std::string >, ObjectPtrHash, ObjectPtrEqual > memo_
Definition: dataflow_pattern.h:554
void Print(const ObjectRef &node)
Managed reference to dataflow patterns.
Definition: dataflow_pattern.h:52
DFPattern operator*(const DFPattern &other) const
Syntatic Sugar for creating a CallPattern with a "multiply" op.
DFPattern HasType(const Type &type) const
Syntatic Sugar for creating a TypePattern.
DFPattern HasDtype(const DataType &dtype) const
Syntatic Sugar for creating a DataTypePattern with a DataType.
TVM_DEFINE_OBJECT_REF_METHODS(DFPattern, ObjectRef, DFPatternNode)
DFPattern operator()(const std::vector< DFPattern > &args) const
Syntatic Sugar for creating a CallPattern.
DFPattern operator+(const DFPattern &other) const
Syntatic Sugar for creating a CallPattern with an "add" op.
DFPattern Optional(const std::function< DFPattern(const DFPattern &)> &func) const
Syntatic Sugar for creating an Optional Pattern.
DFPattern HasShape(const Array< PrimExpr > shape) const
Syntatic Sugar for creating a ShapePattern.
DFPattern operator||(const DFPattern &other) const
Syntatic Sugar for creating an AltPattern.
DFPattern operator/(const DFPattern &other) const
Syntatic Sugar for creating a CallPattern with a "divide" op.
DFPattern operator-(const DFPattern &other) const
Syntatic Sugar for creating a CallPattern with a "subtract" op.
DFPattern HasAttr(const Map< String, ObjectRef > &attrs) const
Syntatic Sugar for creating an AttrPattern.
DFPattern HasDtype(const std::string &dtype) const
Syntatic Sugar for creating a DataTypePattern with a data type's name.
Pattern for Types.
Definition: dataflow_pattern.h:445
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:452
DataType dtype
The type to match.
Definition: dataflow_pattern.h:450
TVM_DECLARE_FINAL_OBJECT_INFO(DataTypePatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:457
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:448
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:464
DataTypePattern(DFPattern pattern, DataType dtype)
TVM_DEFINE_OBJECT_REF_METHODS(DataTypePattern, DFPattern, DataTypePatternNode)
Dominated Graph Pattern Pattern for fuzzy subgraphs where all outputs of the parent are used finally ...
Definition: dataflow_pattern.h:505
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:514
static constexpr const char * _type_key
Definition: dataflow_pattern.h:520
DFPattern path
The path.
Definition: dataflow_pattern.h:510
TVM_DECLARE_FINAL_OBJECT_INFO(DominatorPatternNode, DFPatternNode)
DFPattern parent
The parent.
Definition: dataflow_pattern.h:508
DFPattern child
The child.
Definition: dataflow_pattern.h:512
A pattern which matches a variable length dominator path.
Definition: dataflow_pattern.h:527
TVM_DEFINE_OBJECT_REF_METHODS(DominatorPattern, DFPattern, DominatorPatternNode)
DominatorPattern(DFPattern parent, DFPattern path, DFPattern child)
Pattern for Relay Expression.
Definition: dataflow_pattern.h:85
Expr expr
The expression to match.
Definition: dataflow_pattern.h:88
TVM_DECLARE_FINAL_OBJECT_INFO(ExprPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:92
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:90
A pattern which matches a literal expression.
Definition: dataflow_pattern.h:102
TVM_DEFINE_OBJECT_REF_METHODS(ExprPattern, DFPattern, ExprPatternNode)
Relay Function container.
Definition: dataflow_pattern.h:191
static constexpr const char * _type_key
Definition: dataflow_pattern.h:208
tvm::Array< DFPattern > params
Function parameters.
Definition: dataflow_pattern.h:194
TVM_DECLARE_FINAL_OBJECT_INFO(FunctionPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:203
DFPattern body
The expression which represents the computation of the function, the expression may reference the par...
Definition: dataflow_pattern.h:201
Managed reference to FunctionNode.
Definition: dataflow_pattern.h:216
FunctionPattern(tvm::Array< DFPattern > params, DFPattern body)
Constructor.
TVM_DEFINE_OBJECT_REF_COW_METHOD(FunctionPatternNode)
TVM_DEFINE_OBJECT_REF_METHODS(FunctionPattern, DFPattern, FunctionPatternNode)
Definition: dataflow_pattern.h:303
TVM_DECLARE_FINAL_OBJECT_INFO(IfPatternNode, DFPatternNode)
DFPattern cond
Definition: dataflow_pattern.h:305
static constexpr const char * _type_key
Definition: dataflow_pattern.h:313
DFPattern true_branch
Definition: dataflow_pattern.h:305
DFPattern false_branch
Definition: dataflow_pattern.h:305
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:307
Definition: dataflow_pattern.h:317
IfPattern(DFPattern cond, DFPattern then_clause, DFPattern else_clause)
TVM_DEFINE_OBJECT_REF_METHODS(IfPattern, DFPattern, IfPatternNode)
A binding of a sub-network.
Definition: dataflow_pattern.h:230
DFPattern body
The body of the let binding.
Definition: dataflow_pattern.h:237
static constexpr const char * _type_key
Definition: dataflow_pattern.h:245
TVM_DECLARE_FINAL_OBJECT_INFO(LetPatternNode, DFPatternNode)
DFPattern value
The value we bind var to.
Definition: dataflow_pattern.h:235
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:239
DFPattern var
The variable we bind to.
Definition: dataflow_pattern.h:233
Let binding that binds a local var.
Definition: dataflow_pattern.h:252
TVM_DEFINE_OBJECT_REF_METHODS(LetPattern, DFPattern, LetPatternNode)
LetPattern(DFPattern var, DFPattern value, DFPattern body)
The constructor.
Pattern for Shapes.
Definition: dataflow_pattern.h:416
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:423
TVM_DECLARE_FINAL_OBJECT_INFO(ShapePatternNode, DFPatternNode)
Array< PrimExpr > shape
The type to match.
Definition: dataflow_pattern.h:421
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:419
static constexpr const char * _type_key
Definition: dataflow_pattern.h:428
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:435
TVM_DEFINE_OBJECT_REF_METHODS(ShapePattern, DFPattern, ShapePatternNode)
ShapePattern(DFPattern pattern, Array< PrimExpr > type)
Definition: dataflow_pattern.h:287
static constexpr const char * _type_key
Definition: dataflow_pattern.h:299
DFPattern tuple
The tuple Expression.
Definition: dataflow_pattern.h:290
TVM_DECLARE_FINAL_OBJECT_INFO(TupleGetItemPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:294
int index
which value to get
Definition: dataflow_pattern.h:292
Definition: dataflow_pattern.h:323
TVM_DEFINE_OBJECT_REF_METHODS(TupleGetItemPattern, DFPattern, TupleGetItemPatternNode)
TupleGetItemPattern(DFPattern tuple, int index)
Tuple container.
Definition: dataflow_pattern.h:268
static constexpr const char * _type_key
Definition: dataflow_pattern.h:275
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:273
TVM_DECLARE_FINAL_OBJECT_INFO(TuplePatternNode, DFPatternNode)
tvm::Array< DFPattern > fields
the fields of the tuple
Definition: dataflow_pattern.h:271
Definition: dataflow_pattern.h:279
TuplePattern(tvm::Array< DFPattern > fields)
TVM_DEFINE_OBJECT_REF_METHODS(TuplePattern, DFPattern, TuplePatternNode)
Pattern for Types.
Definition: dataflow_pattern.h:387
TVM_DECLARE_FINAL_OBJECT_INFO(TypePatternNode, DFPatternNode)
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:390
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:394
static constexpr const char * _type_key
Definition: dataflow_pattern.h:399
Type type
The type to match.
Definition: dataflow_pattern.h:392
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:406
TypePattern(DFPattern pattern, Type type)
TVM_DEFINE_OBJECT_REF_METHODS(TypePattern, DFPattern, TypePatternNode)
Container for Var.
Definition: dataflow_pattern.h:113
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:123
String name
The name of the Var (optional).
Definition: dataflow_pattern.h:118
static constexpr const char * _type_key
Definition: dataflow_pattern.h:125
const String & name_hint() const
Definition: dataflow_pattern.h:121
TVM_DECLARE_FINAL_OBJECT_INFO(VarPatternNode, DFPatternNode)
Definition: dataflow_pattern.h:129
VarPattern(String name_hint)
TVM_DEFINE_OBJECT_REF_METHODS(VarPattern, DFPattern, VarPatternNode)
Wildcard Pattern.
Definition: dataflow_pattern.h:361
static constexpr const char * _type_key
Definition: dataflow_pattern.h:369
TVM_DECLARE_FINAL_OBJECT_INFO(WildcardPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:363
Optional< DFPattern > pattern
If the wildcard is redirected, then pattern is not nullptr, and the wildcard redirects to the pattern...
Definition: dataflow_pattern.h:367
A pattern which matches anything.
Definition: dataflow_pattern.h:376
TVM_DEFINE_OBJECT_REF_METHODS(WildcardPattern, DFPattern, WildcardPatternNode)
void redirect_to(DFPattern pat) const
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Runtime primitive data type.
Definition: data_type.h:43
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
DFPattern IsTuple(const Array< DFPattern > &fields)
Syntatic Sugar for creating a TuplePattern.
String PrettyPrint(const ObjectRef &node)
Pretty print a node for debug purposes.
DFPattern IsConstant()
Syntatic Sugar for creating a ConstantPattern.
DFPattern IsTupleGetItem(const DFPattern tuple, int index=-1)
Syntatic Sugar for creating a TupleGetItemPattern.
std::ostream & operator<<(std::ostream &os, const DFPattern &n)
Definition: dataflow_pattern.h:568
DFPattern IsWildcard()
Syntatic Sugar for creating a WildcardPattern.
DFPattern IsExpr(const Expr &expr)
Syntatic Sugar for creating a ExprPattern.
DFPattern IsOp(const String &op_name)
Syntatic Sugar for creating a ExprPattern base on an Op.
DFPattern IsVar(const String &name)
Syntatic Sugar for creating a VarPattern with a name.
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1913
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Relay expression language.
ObjectRef equal functor.
Definition: object.h:665
ObjectRef hash functor.
Definition: object.h:655