tvm
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);
55  DFPattern operator+(const DFPattern& other);
57  DFPattern operator-(const DFPattern& other);
59  DFPattern operator*(const DFPattern& other);
61  DFPattern operator/(const DFPattern& other);
63  DFPattern operator||(const DFPattern& other);
65  DFPattern HasAttr(const Map<String, ObjectRef>& attrs);
67  DFPattern HasType(const Type& type);
69  DFPattern HasDtype(const DataType& dtype);
71  DFPattern HasDtype(const std::string& dtype);
73  DFPattern HasShape(const Array<PrimExpr> shape);
74 
76 };
77 
82  public:
85 
86  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("expr", &expr); }
87 
88  static constexpr const char* _type_key = "relay.dataflow_pattern.ExprPattern";
90 };
91 
98 class ExprPattern : public DFPattern {
99  public:
100  TVM_DLL explicit ExprPattern(Expr expr);
102 };
103 
107 class VarPattern;
110  public:
115 
117  const String& name_hint() const { return name; }
118 
119  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("name", &name); }
120 
121  static constexpr const char* _type_key = "relay.dataflow_pattern.VarPattern";
123 };
124 
125 class VarPattern : public DFPattern {
126  public:
127  TVM_DLL VarPattern(String name_hint);
129 };
130 
134 class ConstantPattern;
137  public:
139 
140  static constexpr const char* _type_key = "relay.dataflow_pattern.ConstantPattern";
142 };
143 
144 class ConstantPattern : public DFPattern {
145  public:
147 };
148 
153 class CallPattern;
156  public:
164 
167 
169  v->Visit("op", &op);
170  v->Visit("args", &args);
171  }
172 
173  static constexpr const char* _type_key = "relay.dataflow_pattern.CallPattern";
175 };
176 
177 class CallPattern : public DFPattern {
178  public:
179  TVM_DLL CallPattern(DFPattern op, Array<DFPattern> args);
181 };
182 
188  public:
198 
200  v->Visit("params", &params);
201  v->Visit("body", &body);
202  }
203 
204  static constexpr const char* _type_key = "relay.dataflow_pattern.FunctionPattern";
206 };
207 
212 class FunctionPattern : public DFPattern {
213  public:
219  TVM_DLL FunctionPattern(tvm::Array<DFPattern> params, DFPattern body);
220 
223 };
224 
227  public:
234 
236  v->Visit("var", &var);
237  v->Visit("value", &value);
238  v->Visit("body", &body);
239  }
240 
241  static constexpr const char* _type_key = "relay.dataflow_pattern.LetPattern";
243 };
244 
248 class LetPattern : public DFPattern {
249  public:
256  TVM_DLL LetPattern(DFPattern var, DFPattern value, DFPattern body);
257 
259 };
260 
262 class TuplePattern;
265  public:
268 
269  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("fields", &fields); }
270 
271  static constexpr const char* _type_key = "relay.dataflow_pattern.TuplePattern";
273 };
274 
275 class TuplePattern : public DFPattern {
276  public:
277  TVM_DLL explicit TuplePattern(tvm::Array<DFPattern> fields);
279 };
280 
282 class TupleGetItemPattern;
284  public:
288  int index;
289 
291  v->Visit("tuple", &tuple);
292  v->Visit("index", &index);
293  }
294 
295  static constexpr const char* _type_key = "relay.dataflow_pattern.TupleGetItemPattern";
297 };
298 
299 class IfPatternNode : public DFPatternNode {
300  public:
301  DFPattern cond, true_branch, false_branch;
302 
304  v->Visit("cond", &cond);
305  v->Visit("true_branch", &true_branch);
306  v->Visit("false_branch", &false_branch);
307  }
308 
309  static constexpr const char* _type_key = "relay.dataflow_pattern.IfPattern";
311 };
312 
313 class IfPattern : public DFPattern {
314  public:
315  TVM_DLL IfPattern(DFPattern cond, DFPattern then_clause, DFPattern else_clause);
317 };
318 
320  public:
321  TVM_DLL TupleGetItemPattern(DFPattern tuple, int index);
323 };
324 
325 class AltPattern;
330  public:
335 
337  v->Visit("left", &left);
338  v->Visit("right", &right);
339  }
340 
341  static constexpr const char* _type_key = "relay.dataflow_pattern.AltPattern";
343 };
344 
348 class AltPattern : public DFPattern {
349  public:
350  TVM_DLL AltPattern(DFPattern left, DFPattern right);
352 };
353 
358  public:
360 
361  static constexpr const char* _type_key = "relay.dataflow_pattern.WildcardPattern";
363 };
364 
368 class WildcardPattern : public DFPattern {
369  public:
371 };
372 
373 class TypePattern;
378  public:
383 
385  v->Visit("pattern", &pattern);
386  v->Visit("type", &type);
387  }
388 
389  static constexpr const char* _type_key = "relay.dataflow_pattern.TypePattern";
391 };
392 
396 class TypePattern : public DFPattern {
397  public:
398  TVM_DLL TypePattern(DFPattern pattern, Type type);
400 };
401 
402 class ShapePattern;
407  public:
412 
414  v->Visit("pattern", &pattern);
415  v->Visit("shape", &shape);
416  }
417 
418  static constexpr const char* _type_key = "relay.dataflow_pattern.ShapePattern";
420 };
421 
425 class ShapePattern : public DFPattern {
426  public:
427  TVM_DLL ShapePattern(DFPattern pattern, Array<PrimExpr> type);
429 };
430 
431 class DataTypePattern;
436  public:
441 
443  v->Visit("pattern", &pattern);
444  v->Visit("dtype", &dtype);
445  }
446 
447  static constexpr const char* _type_key = "relay.dataflow_pattern.DataTypePattern";
449 };
450 
454 class DataTypePattern : public DFPattern {
455  public:
456  TVM_DLL DataTypePattern(DFPattern pattern, DataType dtype);
458 };
459 
460 class AttrPattern;
465  public:
470 
472  v->Visit("pattern", &pattern);
473  v->Visit("attrs", &attrs);
474  }
475 
476  static constexpr const char* _type_key = "relay.dataflow_pattern.AttrPattern";
478 };
479 
483 class AttrPattern : public DFPattern {
484  public:
485  TVM_DLL AttrPattern(DFPattern pattern, DictAttrs attrs);
487 };
488 
489 class DominatorPattern;
496  public:
503 
505  v->Visit("parent", &parent);
506  v->Visit("path", &path);
507  v->Visit("child", &child);
508  }
509 
510  static constexpr const char* _type_key = "relay.dataflow_pattern.DominatorPattern";
512 };
513 
517 class DominatorPattern : public DFPattern {
518  public:
519  TVM_DLL DominatorPattern(DFPattern parent, DFPattern path, DFPattern child);
521 };
522 
524 DFPattern IsVar(const String& name);
530 DFPattern IsExpr(const Expr& expr);
532 DFPattern IsOp(const String& op_name);
534 DFPattern IsTuple(const Array<DFPattern>& fields);
536 DFPattern IsTupleGetItem(const DFPattern tuple, int index = -1);
537 
538 } // namespace relay
539 } // namespace tvm
540 #endif // TVM_RELAY_DATAFLOW_PATTERN_H_
Definition: dataflow_pattern.h:125
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:235
DFPattern IsExpr(const Expr &expr)
Syntatic Sugar for creating a ExprPattern.
DFPattern path
The path.
Definition: dataflow_pattern.h:500
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:504
A pattern which matches a literal expression.
Definition: dataflow_pattern.h:98
DFPattern var
The variable we bind to.
Definition: dataflow_pattern.h:229
Pattern for Types.
Definition: dataflow_pattern.h:435
int index
which value to get
Definition: dataflow_pattern.h:288
Bool operator||(const Bool &a, bool b)
Definition: expr.h:337
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:336
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:409
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:380
Relay expression language.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:138
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
Pattern for Relay Expression.
Definition: dataflow_pattern.h:81
Definition: dataflow_pattern.h:299
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:384
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:359
Definition: dataflow_pattern.h:319
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:86
Definition: dataflow_pattern.h:144
DFPattern IsConstant()
Syntatic Sugar for creating a ConstantPattern.
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:438
A binding of a sub-network.
Definition: dataflow_pattern.h:226
Dominated Graph Pattern Pattern for fuzzy subgraphs where all outputs of the parent are used finally ...
Definition: dataflow_pattern.h:495
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:425
DataType dtype
The type to match.
Definition: dataflow_pattern.h:440
Managed reference to DictAttrsNode.
Definition: attrs.h:227
base class of all object containers.
Definition: object.h:165
TVM_DECLARE_BASE_OBJECT_INFO(DFPatternNode, Object)
PrimExpr operator-(PrimExpr a, PrimExpr b)
subtraction operator
DictAttrs attrs
The attribute to match.
Definition: dataflow_pattern.h:469
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:413
Definition: dataflow_pattern.h:283
Pattern for Types.
Definition: dataflow_pattern.h:377
DFPattern tuple
The tuple Expression.
Definition: dataflow_pattern.h:286
A pattern which matches either of two patterns.
Definition: dataflow_pattern.h:348
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
DFPattern value
The value we bind var to.
Definition: dataflow_pattern.h:231
Base type of all dataflow patterns.
Definition: dataflow_pattern.h:40
Pattern for Alternate Expressions.
Definition: dataflow_pattern.h:329
Managed reference to FunctionNode.
Definition: dataflow_pattern.h:212
String name
The name of the Var (optional).
Definition: dataflow_pattern.h:114
Runtime primitive data type.
Definition: data_type.h:41
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:119
DFPattern left
The left optional pattern.
Definition: dataflow_pattern.h:332
Pattern for Attributes.
Definition: dataflow_pattern.h:464
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:396
A pattern which matches attributes in another pattern.
Definition: dataflow_pattern.h:483
A pattern which matches a type in another pattern.
Definition: dataflow_pattern.h:454
Relay typed AST nodes.
DFPattern right
The right optional pattern.
Definition: dataflow_pattern.h:334
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:168
CallPattern container.
Definition: dataflow_pattern.h:155
DFPattern body
The body of the let binding.
Definition: dataflow_pattern.h:233
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:303
DFPattern IsTuple(const Array< DFPattern > &fields)
Syntatic Sugar for creating a TuplePattern.
Reference to string objects.
Definition: string.h:129
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:199
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:1608
Managed reference to RelayExprNode.
Definition: expr.h:177
Container for Constant.
Definition: dataflow_pattern.h:136
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:706
tvm::Array< DFPattern > fields
the fields of the tuple
Definition: dataflow_pattern.h:267
Managed reference to dataflow patterns.
Definition: dataflow_pattern.h:50
Definition: dataflow_pattern.h:313
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:290
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
Base class of all object reference.
Definition: object.h:504
#define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName)
Define CopyOnWrite function in an ObjectRef.
Definition: object.h:778
Expr expr
The expression to match.
Definition: dataflow_pattern.h:84
const String & name_hint() const
Definition: dataflow_pattern.h:117
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:442
DFPattern IsWildcard()
Syntatic Sugar for creating a WildcardPattern.
A pattern which matches anything.
Definition: dataflow_pattern.h:368
PrimExpr operator*(PrimExpr a, PrimExpr b)
multiplication operator
Container for Var.
Definition: dataflow_pattern.h:109
Tuple container.
Definition: dataflow_pattern.h:264
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:664
Definition: dataflow_pattern.h:177
Pattern for Shapes.
Definition: dataflow_pattern.h:406
Type type
The type to match.
Definition: dataflow_pattern.h:382
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:269
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1235
DFPattern pattern
The pattern.
Definition: dataflow_pattern.h:467
PrimExpr operator/(PrimExpr a, PrimExpr b)
division operator
Managed reference to TypeNode.
Definition: type.h:93
DFPattern IsTupleGetItem(const DFPattern tuple, int index=-1)
Syntatic Sugar for creating a TupleGetItemPattern.
DFPattern true_branch
Definition: dataflow_pattern.h:301
A pattern which matches a variable length dominator path.
Definition: dataflow_pattern.h:517
Array< PrimExpr > shape
The type to match.
Definition: dataflow_pattern.h:411
Definition: dataflow_pattern.h:275
DFPattern op
The operator(function) being invoked.
Definition: dataflow_pattern.h:163
Let binding that binds a local var.
Definition: dataflow_pattern.h:248
DFPattern IsOp(const String &op_name)
Syntatic Sugar for creating a ExprPattern base on an Op.
Relay Function container.
Definition: dataflow_pattern.h:187
DFPattern child
The child.
Definition: dataflow_pattern.h:502
Wildcard Pattern.
Definition: dataflow_pattern.h:357
tvm::Array< relay::DFPattern > args
The arguments(inputs) of the call.
Definition: dataflow_pattern.h:166
PrimExpr operator+(PrimExpr a, PrimExpr b)
add operator
DFPattern parent
The parent.
Definition: dataflow_pattern.h:498
static constexpr const char * _type_key
Definition: dataflow_pattern.h:42
DFPattern IsVar(const String &name)
Syntatic Sugar for creating a VarPattern with a name.
DFPattern body
The expression which represents the computation of the function, the expression may reference the par...
Definition: dataflow_pattern.h:197
tvm::Array< DFPattern > params
Function parameters.
Definition: dataflow_pattern.h:190
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:471