tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
dataflow_pattern.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_RELAY_DATAFLOW_PATTERN_H_
25 #define TVM_RELAY_DATAFLOW_PATTERN_H_
26 
27 #include <tvm/relay/expr.h>
28 #include <tvm/relay/type.h>
29 
30 #include <string>
31 #include <vector>
32 
33 namespace tvm {
34 namespace relay {
35 
40 class DFPatternNode : public Object {
41  public:
42  static constexpr const char* _type_key = "DFPatternNode";
44 };
45 
50 class DFPattern : public ObjectRef {
51  public:
53  DFPattern operator()(const std::vector<DFPattern>& args) const;
55  DFPattern operator+(const DFPattern& other) const;
57  DFPattern operator-(const DFPattern& other) const;
59  DFPattern operator*(const DFPattern& other) const;
61  DFPattern operator/(const DFPattern& other) const;
63  DFPattern operator||(const DFPattern& other) const;
65  DFPattern Optional(const std::function<DFPattern(const DFPattern&)>& func) const;
69  DFPattern HasType(const Type& type) const;
71  DFPattern HasDtype(const DataType& dtype) const;
73  DFPattern HasDtype(const std::string& dtype) const;
76 
78 };
79 
84  public:
87 
88  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("expr", &expr); }
89 
90  static constexpr const char* _type_key = "relay.dataflow_pattern.ExprPattern";
92 };
93 
100 class ExprPattern : public DFPattern {
101  public:
102  TVM_DLL explicit ExprPattern(Expr expr);
104 };
105 
109 class VarPattern;
112  public:
117 
119  const String& name_hint() const { return name; }
120 
121  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("name", &name); }
122 
123  static constexpr const char* _type_key = "relay.dataflow_pattern.VarPattern";
125 };
126 
127 class VarPattern : public DFPattern {
128  public:
129  TVM_DLL VarPattern(String name_hint);
131 };
132 
136 class ConstantPattern;
139  public:
141 
142  static constexpr const char* _type_key = "relay.dataflow_pattern.ConstantPattern";
144 };
145 
146 class ConstantPattern : public DFPattern {
147  public:
149 };
150 
155 class CallPattern;
158  public:
166 
169 
171  v->Visit("op", &op);
172  v->Visit("args", &args);
173  }
174 
175  static constexpr const char* _type_key = "relay.dataflow_pattern.CallPattern";
177 };
178 
179 class CallPattern : public DFPattern {
180  public:
183 };
184 
190  public:
200 
202  v->Visit("params", &params);
203  v->Visit("body", &body);
204  }
205 
206  static constexpr const char* _type_key = "relay.dataflow_pattern.FunctionPattern";
208 };
209 
214 class FunctionPattern : public DFPattern {
215  public:
222 
225 };
226 
229  public:
236 
238  v->Visit("var", &var);
239  v->Visit("value", &value);
240  v->Visit("body", &body);
241  }
242 
243  static constexpr const char* _type_key = "relay.dataflow_pattern.LetPattern";
245 };
246 
250 class LetPattern : public DFPattern {
251  public:
258  TVM_DLL LetPattern(DFPattern var, DFPattern value, DFPattern body);
259 
261 };
262 
264 class TuplePattern;
267  public:
270 
271  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("fields", &fields); }
272 
273  static constexpr const char* _type_key = "relay.dataflow_pattern.TuplePattern";
275 };
276 
277 class TuplePattern : public DFPattern {
278  public:
279  TVM_DLL explicit TuplePattern(tvm::Array<DFPattern> fields);
281 };
282 
284 class TupleGetItemPattern;
286  public:
290  int index;
291 
293  v->Visit("tuple", &tuple);
294  v->Visit("index", &index);
295  }
296 
297  static constexpr const char* _type_key = "relay.dataflow_pattern.TupleGetItemPattern";
299 };
300 
301 class IfPatternNode : public DFPatternNode {
302  public:
304 
306  v->Visit("cond", &cond);
307  v->Visit("true_branch", &true_branch);
308  v->Visit("false_branch", &false_branch);
309  }
310 
311  static constexpr const char* _type_key = "relay.dataflow_pattern.IfPattern";
313 };
314 
315 class IfPattern : public DFPattern {
316  public:
317  TVM_DLL IfPattern(DFPattern cond, DFPattern then_clause, DFPattern else_clause);
319 };
320 
322  public:
323  TVM_DLL TupleGetItemPattern(DFPattern tuple, int index);
325 };
326 
327 class AltPattern;
332  public:
337 
339  v->Visit("left", &left);
340  v->Visit("right", &right);
341  }
342 
343  static constexpr const char* _type_key = "relay.dataflow_pattern.AltPattern";
345 };
346 
350 class AltPattern : public DFPattern {
351  public:
352  TVM_DLL AltPattern(DFPattern left, DFPattern right);
354 };
355 
360  public:
362 
363  static constexpr const char* _type_key = "relay.dataflow_pattern.WildcardPattern";
365 };
366 
370 class WildcardPattern : public DFPattern {
371  public:
373 };
374 
375 class TypePattern;
380  public:
385 
387  v->Visit("pattern", &pattern);
388  v->Visit("type", &type);
389  }
390 
391  static constexpr const char* _type_key = "relay.dataflow_pattern.TypePattern";
393 };
394 
398 class TypePattern : public DFPattern {
399  public:
400  TVM_DLL TypePattern(DFPattern pattern, Type type);
402 };
403 
404 class ShapePattern;
409  public:
414 
416  v->Visit("pattern", &pattern);
417  v->Visit("shape", &shape);
418  }
419 
420  static constexpr const char* _type_key = "relay.dataflow_pattern.ShapePattern";
422 };
423 
427 class ShapePattern : public DFPattern {
428  public:
429  TVM_DLL ShapePattern(DFPattern pattern, Array<PrimExpr> type);
431 };
432 
433 class DataTypePattern;
438  public:
443 
445  v->Visit("pattern", &pattern);
446  v->Visit("dtype", &dtype);
447  }
448 
449  static constexpr const char* _type_key = "relay.dataflow_pattern.DataTypePattern";
451 };
452 
456 class DataTypePattern : public DFPattern {
457  public:
458  TVM_DLL DataTypePattern(DFPattern pattern, DataType dtype);
460 };
461 
462 class AttrPattern;
467  public:
472 
474  v->Visit("pattern", &pattern);
475  v->Visit("attrs", &attrs);
476  }
477 
478  static constexpr const char* _type_key = "relay.dataflow_pattern.AttrPattern";
480 };
481 
485 class AttrPattern : public DFPattern {
486  public:
487  TVM_DLL AttrPattern(DFPattern pattern, DictAttrs attrs);
489 };
490 
491 class DominatorPattern;
498  public:
505 
507  v->Visit("parent", &parent);
508  v->Visit("path", &path);
509  v->Visit("child", &child);
510  }
511 
512  static constexpr const char* _type_key = "relay.dataflow_pattern.DominatorPattern";
514 };
515 
519 class DominatorPattern : public DFPattern {
520  public:
521  TVM_DLL DominatorPattern(DFPattern parent, DFPattern path, DFPattern child);
523 };
524 
526 DFPattern IsVar(const String& name);
532 DFPattern IsExpr(const Expr& expr);
534 DFPattern IsOp(const String& op_name);
538 DFPattern IsTupleGetItem(const DFPattern tuple, int index = -1);
539 
540 } // namespace relay
541 } // namespace tvm
542 #endif // TVM_RELAY_DATAFLOW_PATTERN_H_
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
Managed reference to RelayExprNode.
Definition: expr.h:433
Managed reference to TypeNode.
Definition: type.h:93
Pattern for Alternate Expressions.
Definition: dataflow_pattern.h:331
DFPattern right
The right optional pattern.
Definition: dataflow_pattern.h:336
DFPattern left
The left optional pattern.
Definition: dataflow_pattern.h:334
TVM_DECLARE_FINAL_OBJECT_INFO(AltPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:338
static constexpr const char * _type_key
Definition: dataflow_pattern.h:343
A pattern which matches either of two patterns.
Definition: dataflow_pattern.h:350
TVM_DEFINE_OBJECT_REF_METHODS(AltPattern, DFPattern, AltPatternNode)
AltPattern(DFPattern left, DFPattern right)
Pattern for Attributes.
Definition: dataflow_pattern.h:466
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:469
DictAttrs attrs
The attribute to match.
Definition: dataflow_pattern.h:471
static constexpr const char * _type_key
Definition: dataflow_pattern.h:478
TVM_DECLARE_FINAL_OBJECT_INFO(AttrPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:473
A pattern which matches attributes in another pattern.
Definition: dataflow_pattern.h:485
AttrPattern(DFPattern pattern, DictAttrs attrs)
TVM_DEFINE_OBJECT_REF_METHODS(AttrPattern, DFPattern, AttrPatternNode)
CallPattern container.
Definition: dataflow_pattern.h:157
TVM_DECLARE_FINAL_OBJECT_INFO(CallPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:175
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:170
tvm::Array< relay::DFPattern > args
The arguments(inputs) of the call.
Definition: dataflow_pattern.h:168
DFPattern op
The operator(function) being invoked.
Definition: dataflow_pattern.h:165
Definition: dataflow_pattern.h:179
CallPattern(DFPattern op, Array< DFPattern > args)
TVM_DEFINE_OBJECT_REF_METHODS(CallPattern, DFPattern, CallPatternNode)
Container for Constant.
Definition: dataflow_pattern.h:138
static constexpr const char * _type_key
Definition: dataflow_pattern.h:142
TVM_DECLARE_FINAL_OBJECT_INFO(ConstantPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:140
Definition: dataflow_pattern.h:146
TVM_DEFINE_OBJECT_REF_METHODS(ConstantPattern, DFPattern, ConstantPatternNode)
Base type of all dataflow patterns.
Definition: dataflow_pattern.h:40
TVM_DECLARE_BASE_OBJECT_INFO(DFPatternNode, Object)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:42
Managed reference to dataflow patterns.
Definition: dataflow_pattern.h:50
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:437
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:444
DataType dtype
The type to match.
Definition: dataflow_pattern.h:442
TVM_DECLARE_FINAL_OBJECT_INFO(DataTypePatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:449
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:440
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:456
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:497
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:506
static constexpr const char * _type_key
Definition: dataflow_pattern.h:512
DFPattern path
The path.
Definition: dataflow_pattern.h:502
TVM_DECLARE_FINAL_OBJECT_INFO(DominatorPatternNode, DFPatternNode)
DFPattern parent
The parent.
Definition: dataflow_pattern.h:500
DFPattern child
The child.
Definition: dataflow_pattern.h:504
A pattern which matches a variable length dominator path.
Definition: dataflow_pattern.h:519
TVM_DEFINE_OBJECT_REF_METHODS(DominatorPattern, DFPattern, DominatorPatternNode)
DominatorPattern(DFPattern parent, DFPattern path, DFPattern child)
Pattern for Relay Expression.
Definition: dataflow_pattern.h:83
Expr expr
The expression to match.
Definition: dataflow_pattern.h:86
TVM_DECLARE_FINAL_OBJECT_INFO(ExprPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:90
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:88
A pattern which matches a literal expression.
Definition: dataflow_pattern.h:100
TVM_DEFINE_OBJECT_REF_METHODS(ExprPattern, DFPattern, ExprPatternNode)
Relay Function container.
Definition: dataflow_pattern.h:189
static constexpr const char * _type_key
Definition: dataflow_pattern.h:206
tvm::Array< DFPattern > params
Function parameters.
Definition: dataflow_pattern.h:192
TVM_DECLARE_FINAL_OBJECT_INFO(FunctionPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:201
DFPattern body
The expression which represents the computation of the function, the expression may reference the par...
Definition: dataflow_pattern.h:199
Managed reference to FunctionNode.
Definition: dataflow_pattern.h:214
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:301
TVM_DECLARE_FINAL_OBJECT_INFO(IfPatternNode, DFPatternNode)
DFPattern cond
Definition: dataflow_pattern.h:303
static constexpr const char * _type_key
Definition: dataflow_pattern.h:311
DFPattern true_branch
Definition: dataflow_pattern.h:303
DFPattern false_branch
Definition: dataflow_pattern.h:303
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:305
Definition: dataflow_pattern.h:315
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:228
DFPattern body
The body of the let binding.
Definition: dataflow_pattern.h:235
static constexpr const char * _type_key
Definition: dataflow_pattern.h:243
TVM_DECLARE_FINAL_OBJECT_INFO(LetPatternNode, DFPatternNode)
DFPattern value
The value we bind var to.
Definition: dataflow_pattern.h:233
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:237
DFPattern var
The variable we bind to.
Definition: dataflow_pattern.h:231
Let binding that binds a local var.
Definition: dataflow_pattern.h:250
TVM_DEFINE_OBJECT_REF_METHODS(LetPattern, DFPattern, LetPatternNode)
LetPattern(DFPattern var, DFPattern value, DFPattern body)
The constructor.
Pattern for Shapes.
Definition: dataflow_pattern.h:408
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:415
TVM_DECLARE_FINAL_OBJECT_INFO(ShapePatternNode, DFPatternNode)
Array< PrimExpr > shape
The type to match.
Definition: dataflow_pattern.h:413
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:411
static constexpr const char * _type_key
Definition: dataflow_pattern.h:420
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:427
TVM_DEFINE_OBJECT_REF_METHODS(ShapePattern, DFPattern, ShapePatternNode)
ShapePattern(DFPattern pattern, Array< PrimExpr > type)
Definition: dataflow_pattern.h:285
static constexpr const char * _type_key
Definition: dataflow_pattern.h:297
DFPattern tuple
The tuple Expression.
Definition: dataflow_pattern.h:288
TVM_DECLARE_FINAL_OBJECT_INFO(TupleGetItemPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:292
int index
which value to get
Definition: dataflow_pattern.h:290
Definition: dataflow_pattern.h:321
TVM_DEFINE_OBJECT_REF_METHODS(TupleGetItemPattern, DFPattern, TupleGetItemPatternNode)
TupleGetItemPattern(DFPattern tuple, int index)
Tuple container.
Definition: dataflow_pattern.h:266
static constexpr const char * _type_key
Definition: dataflow_pattern.h:273
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:271
TVM_DECLARE_FINAL_OBJECT_INFO(TuplePatternNode, DFPatternNode)
tvm::Array< DFPattern > fields
the fields of the tuple
Definition: dataflow_pattern.h:269
Definition: dataflow_pattern.h:277
TuplePattern(tvm::Array< DFPattern > fields)
TVM_DEFINE_OBJECT_REF_METHODS(TuplePattern, DFPattern, TuplePatternNode)
Pattern for Types.
Definition: dataflow_pattern.h:379
TVM_DECLARE_FINAL_OBJECT_INFO(TypePatternNode, DFPatternNode)
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:382
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:386
static constexpr const char * _type_key
Definition: dataflow_pattern.h:391
Type type
The type to match.
Definition: dataflow_pattern.h:384
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:398
TypePattern(DFPattern pattern, Type type)
TVM_DEFINE_OBJECT_REF_METHODS(TypePattern, DFPattern, TypePatternNode)
Container for Var.
Definition: dataflow_pattern.h:111
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:121
String name
The name of the Var (optional).
Definition: dataflow_pattern.h:116
static constexpr const char * _type_key
Definition: dataflow_pattern.h:123
const String & name_hint() const
Definition: dataflow_pattern.h:119
TVM_DECLARE_FINAL_OBJECT_INFO(VarPatternNode, DFPatternNode)
Definition: dataflow_pattern.h:127
VarPattern(String name_hint)
TVM_DEFINE_OBJECT_REF_METHODS(VarPattern, DFPattern, VarPatternNode)
Wildcard Pattern.
Definition: dataflow_pattern.h:359
static constexpr const char * _type_key
Definition: dataflow_pattern.h:363
TVM_DECLARE_FINAL_OBJECT_INFO(WildcardPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:361
A pattern which matches anything.
Definition: dataflow_pattern.h:370
TVM_DEFINE_OBJECT_REF_METHODS(WildcardPattern, DFPattern, WildcardPatternNode)
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Runtime primitive data type.
Definition: data_type.h:42
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:515
base class of all object containers.
Definition: object.h:167
Reference to string objects.
Definition: string.h:98
DFPattern IsTuple(const Array< DFPattern > &fields)
Syntatic Sugar for creating a TuplePattern.
DFPattern IsConstant()
Syntatic Sugar for creating a ConstantPattern.
DFPattern IsTupleGetItem(const DFPattern tuple, int index=-1)
Syntatic Sugar for creating a TupleGetItemPattern.
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:1763
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Relay expression language.
Relay typed AST nodes.