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_RELAX_DATAFLOW_PATTERN_H_
25 #define TVM_RELAX_DATAFLOW_PATTERN_H_
26 
27 #include <tvm/ir/expr.h>
28 #include <tvm/relax/expr.h>
29 #include <tvm/relax/type.h>
32 #include <tvm/support/with.h>
33 
34 #include <cstdint>
35 #include <functional>
36 #include <map>
37 #include <memory>
38 #include <string>
39 #include <tuple>
40 #include <utility>
41 #include <vector>
42 
43 namespace tvm {
44 
45 namespace arith {
46 class Analyzer;
47 }
48 
49 namespace relax {
50 
51 class PatternSeq;
52 class CallPattern;
53 class OrPattern;
54 class AndPattern;
55 class NotPattern;
56 class ShapePattern;
57 class StructInfoPattern;
58 class TypePattern;
59 class DataTypePattern;
60 class AttrPattern;
61 class SameShapeConstraint;
62 
71 TVM_DLL PatternSeq UsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index = -1);
73 TVM_DLL PatternSeq operator^(const PatternSeq& lhs, const PatternSeq& rhs);
74 
83 TVM_DLL PatternSeq OnlyUsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index = -1);
85 TVM_DLL PatternSeq operator>>(const PatternSeq& lhs, const PatternSeq& rhs);
86 
91 class DFPatternNode : public Object {
92  public:
93  static constexpr const char* _type_key = "DFPatternNode";
94  static constexpr const uint32_t _type_child_slots = 21;
96 };
97 
102 class DFPattern : public ObjectRef {
103  public:
105  template <typename... Args>
106  CallPattern operator()(Args&&... args) const;
108  TVM_DLL CallPattern operator()(const std::vector<DFPattern>& args) const;
110  TVM_DLL OrPattern operator|(const DFPattern& other) const;
112  TVM_DLL AndPattern operator&(const DFPattern& other) const;
114  TVM_DLL NotPattern operator~() const;
116  TVM_DLL AttrPattern HasAttr(const Map<String, ObjectRef>& attrs) const;
118  TVM_DLL StructInfoPattern HasStructInfo(const StructInfo& struct_info) const;
120  TVM_DLL TypePattern HasType(const Type& type) const;
122  TVM_DLL DataTypePattern HasDtype(const DataType& dtype) const;
124  TVM_DLL DataTypePattern HasDtype(const std::string& dtype) const;
128  TVM_DLL SameShapeConstraint HasSameShapeAs(const DFPattern& other) const;
130  TVM_DLL DFPattern dup() const;
131 
133  TVM_DLL operator PatternSeq() const;
134 
136 };
137 
139 struct PairCons {
141  enum Type {
145  int index = -1;
153  TVM_DLL explicit PairCons(Type t, int index = -1) : type(t), index(index) {}
154 
155  bool operator==(const PairCons& other) const {
156  return type == other.type && index == other.index;
157  }
158 };
159 
167 class DFConstraintNode : public Object {
168  public:
171 
199  virtual std::tuple<PrimExpr, bool> AsPrimExpr(
200  std::function<Optional<Var>(const DFPatternNode*)> match_state) const = 0;
201 
202  static constexpr const char* _type_key = "DFConstraintNode";
203  static constexpr const uint32_t _type_child_slots = 1;
205 };
206 
207 class DFConstraint : public ObjectRef {
208  public:
210 };
211 
216 class PatternSeqNode final : public Object {
217  public:
219  std::vector<PairCons> pair_constraints;
221  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("patterns", &patterns); }
222  static constexpr const char* _type_key = "relax.dpl.PatternSeq";
224 };
225 
230 class PatternSeq final : public ObjectRef {
231  public:
232  TVM_DLL explicit PatternSeq(DFPattern init_pattern);
233  TVM_DLL explicit PatternSeq(tvm::Array<DFPattern> patterns, bool only_used_by = false);
234 
235  PatternSeq UsedBy(PatternSeq other, int index = -1) const;
236  PatternSeq OnlyUsedBy(PatternSeq other, int index = -1) const;
237 
239  PatternSeq dup() const;
240 
241  // friend functions
242  friend PatternSeq UsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index);
243  friend PatternSeq OnlyUsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index);
244 
246 };
247 
252 class PatternContextNode : public Object {
253  public:
255  enum ExternUse {
259 
260  // src node -> <dst node, constraint type> constraints.
261  // Dst nodes are kept in a vector to keep them ordered.
262  std::map<DFPattern, std::vector<std::pair<DFPattern, std::vector<PairCons>>>> edge_constraints;
263 
264  // Underlying DFPattern nodes which the edge constraints may reference
265  // Kept as a separate vector of patterns to process constraints in a fixed order.
266  std::vector<DFPattern> src_ordered;
267 
268  // Non-edge constraints
269  std::vector<DFConstraint> validation_constraints;
270 
271  static constexpr const char* _type_key = "relax.dpl.PatternContext";
273 };
274 
279 class PatternContext : public ObjectRef {
280  public:
281  TVM_DLL explicit PatternContext(ObjectPtr<Object> n) : ObjectRef(n) {}
282  TVM_DLL explicit PatternContext(bool incremental = false);
283 
285  ICHECK(get() != nullptr);
286  return static_cast<const PatternContextNode*>(get());
287  }
288 
290  ICHECK(get() != nullptr);
291  return static_cast<PatternContextNode*>(get_mutable());
292  }
293 
301  void add_constraint(DFPattern producer, DFPattern consumer, PairCons cons) {
302  auto& pairs = (*this)->edge_constraints[producer];
303  auto it = std::find_if(pairs.begin(), pairs.end(),
304  [consumer](auto p) { return p.first == consumer; });
305  if (it == pairs.end()) {
306  pairs.emplace_back(consumer, std::vector{cons});
307  } else {
308  auto& vec = it->second;
309  ICHECK(std::find(vec.cbegin(), vec.cend(), cons) == vec.cend())
310  << "Constraint already exists";
311  vec.push_back(cons);
312  }
313 
314  auto& patterns = (*this)->src_ordered;
315  if (std::find(patterns.begin(), patterns.end(), producer) == patterns.end()) {
316  patterns.push_back(producer);
317  }
318  }
319 
325  void add_constraint(DFConstraint constraint) {
326  (*this)->validation_constraints.push_back(constraint);
327  }
328 
331 
333  TVM_DLL void EnterWithScope() const;
335  TVM_DLL void ExitWithScope() const;
336 
337  private:
338  friend class With<PatternContext>;
339 };
340 
346  public:
349  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("expr", &expr); }
350 
351  static constexpr const char* _type_key = "relax.dpl.ExprPattern";
353 };
354 
359 class ExprPattern : public DFPattern {
360  public:
361  TVM_DLL explicit ExprPattern(Expr expr);
363 };
364 
371  public:
373  const String& name_hint() const { return name; }
374  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("name", &name); }
375 
376  static constexpr const char* _type_key = "relax.dpl.VarPattern";
377  static constexpr const uint32_t _type_child_slots = 1;
379 };
380 
385 class VarPattern : public DFPattern {
386  public:
392  TVM_DLL VarPattern(String name_hint);
394 };
395 
401  public:
402  static constexpr const char* _type_key = "relax.dpl.DataflowVarPattern";
404 };
405 
411  public:
413  TVM_DLL DataflowVarPattern(String name_hint);
415 };
416 
422  public:
423  static constexpr const char* _type_key = "relax.dpl.GlobalVarPattern";
425 };
426 
431 class GlobalVarPattern : public DFPattern {
432  public:
433  TVM_DLL GlobalVarPattern(String name_hint);
435 };
436 
442  public:
444 
445  static constexpr const char* _type_key = "relax.dpl.ConstantPattern";
447 };
448 
453 class ConstantPattern : public DFPattern {
454  public:
456 };
457 
463  public:
479  // Todo(relax-team): Dataflow pattern for StructInfo, and match sinfo_args
480 
482  v->Visit("op", &op);
483  v->Visit("args", &args);
484  }
485 
486  static constexpr const char* _type_key = "relax.dpl.CallPattern";
488 };
489 
490 class CallPattern : public DFPattern {
491  public:
492  TVM_DLL CallPattern(DFPattern op, Array<DFPattern> args, bool varg_default_wildcard = false);
494 };
495 
502  public:
504  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("fields", &fields); }
505  static constexpr const char* _type_key = "relax.dpl.PrimArrPattern";
507 };
508 
513 class PrimArrPattern : public DFPattern {
514  public:
517 };
518 
525  public:
536  v->Visit("params", &params);
537  v->Visit("body", &body);
538  }
539 
540  static constexpr const char* _type_key = "relax.dpl.FunctionPattern";
542 };
543 
548 class FunctionPattern : public DFPattern {
549  public:
556 
558 };
559 
565  public:
568  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("fields", &fields); }
569 
570  static constexpr const char* _type_key = "relax.dpl.TuplePattern";
572 };
573 
578 class TuplePattern : public DFPattern {
579  public:
580  TVM_DLL explicit TuplePattern(tvm::Array<DFPattern> fields);
582 };
583 
589  public:
592  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("fields", &fields); }
593 
594  static constexpr const char* _type_key = "relax.dpl.UnorderedTuplePattern";
596 };
597 
603  public:
606 };
607 
614  public:
616  int index;
619  v->Visit("tuple", &tuple);
620  v->Visit("index", &index);
621  }
622 
623  static constexpr const char* _type_key = "relax.dpl.TupleGetItemPattern";
625 };
626 
632  public:
633  TVM_DLL TupleGetItemPattern(DFPattern tuple, int index);
635 };
636 
642  public:
647  v->Visit("left", &left);
648  v->Visit("right", &right);
649  }
650 
651  static constexpr const char* _type_key = "relax.dpl.AndPattern";
653 };
654 
659 class AndPattern : public DFPattern {
660  public:
661  TVM_DLL AndPattern(DFPattern lhs, DFPattern rhs);
663 };
664 
669 class OrPatternNode : public DFPatternNode {
670  public:
675  v->Visit("left", &left);
676  v->Visit("right", &right);
677  }
678 
679  static constexpr const char* _type_key = "relax.dpl.OrPattern";
681 };
682 
687 class OrPattern : public DFPattern {
688  public:
689  TVM_DLL OrPattern(DFPattern left, DFPattern right);
691 };
692 
698  public:
701  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("reject", &reject); }
702 
703  static constexpr const char* _type_key = "relax.dpl.NotPattern";
705 };
706 
711 class NotPattern : public DFPattern {
712  public:
713  TVM_DLL NotPattern(DFPattern reject);
715 };
716 
722  public:
724 
725  static constexpr const char* _type_key = "relax.dpl.WildcardPattern";
727 };
728 
733 class WildcardPattern : public DFPattern {
734  public:
736 
737  // Declaring WildcardPattern declared as non-nullable avoids the
738  // default zero-parameter constructor for ObjectRef with `data_ =
739  // nullptr`. This allows a zero-parameter constructor to be
740  // declared here, to create a valid wildcard instance.
741 
743 };
744 
750  public:
755  v->Visit("pattern", &pattern);
756  v->Visit("type", &type);
757  }
758 
759  static constexpr const char* _type_key = "relax.dpl.TypePattern";
761 };
762 
767 class TypePattern : public DFPattern {
768  public:
769  TVM_DLL TypePattern(DFPattern pattern, Type type);
771 };
772 
778  public:
783  v->Visit("pattern", &pattern);
784  v->Visit("struct_info", &struct_info);
785  }
786 
787  static constexpr const char* _type_key = "relax.dpl.StructInfoPattern";
789 };
790 
791 class StructInfoPattern : public DFPattern {
792  public:
793  TVM_DLL StructInfoPattern(DFPattern pattern, StructInfo struct_info);
795 };
796 
802  public:
807  v->Visit("pattern", &pattern);
808  v->Visit("shape", &shape);
809  }
810 
811  static constexpr const char* _type_key = "relax.dpl.ShapePattern";
813 };
814 
819 class ShapePattern : public DFPattern {
820  public:
821  TVM_DLL ShapePattern(DFPattern pattern, Array<PrimExpr> type);
823 };
824 
830  public:
833  Array<DFPattern> GetDependentPatterns() const override { return args; }
834 
835  std::tuple<PrimExpr, bool> AsPrimExpr(
836  std::function<Optional<Var>(const DFPatternNode*)> match_state) const override;
837 
838  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("args", &args); }
839 
840  static constexpr const char* _type_key = "relax.dpl.SameShapeConstraint";
842 };
843 
849  public:
852 };
853 
859  public:
864  v->Visit("pattern", &pattern);
865  v->Visit("dtype", &dtype);
866  }
867 
868  static constexpr const char* _type_key = "relax.dpl.DataTypePattern";
870 };
871 
876 class DataTypePattern : public DFPattern {
877  public:
878  TVM_DLL DataTypePattern(DFPattern pattern, DataType dtype);
880 };
881 
887  public:
892  v->Visit("pattern", &pattern);
893  v->Visit("attrs", &attrs);
894  }
895 
896  static constexpr const char* _type_key = "relax.dpl.AttrPattern";
898 };
899 
904 class AttrPattern : public DFPattern {
905  public:
906  TVM_DLL AttrPattern(DFPattern pattern, DictAttrs attrs);
908 };
909 
916  public:
920  const String& global_symbol() const { return global_symbol_; }
921  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("global_symbol", &global_symbol_); }
922 
923  static constexpr const char* _type_key = "relax.dpl.ExternFuncPattern";
925 };
926 
931 class ExternFuncPattern : public DFPattern {
932  public:
933  TVM_DLL ExternFuncPattern(String global_symbol);
935 };
936 
938 VarPattern IsVar(const String& name);
944 ExprPattern IsExpr(const Expr& expr);
946 ExprPattern IsOp(const String& op_name);
948 // Todo(relax-team): Dataflow pattern for StructInfo, and match out_sinfo
951 CallPattern IsCallTIR(const String& name, TuplePattern var_args);
957 DFPattern IsTuple(const Array<DFPattern>& fields, bool unordered = false);
959 TupleGetItemPattern IsTupleGetItem(const DFPattern tuple, int index = -1);
960 
962 template <typename... Args>
963 CallPattern DFPattern::operator()(Args&&... args) const {
964  return CallPattern(GetRef<DFPattern>(this->get()),
965  Array<DFPattern>({std::forward<Args>(args)...}));
966 }
967 
968 } // namespace relax
969 } // namespace tvm
970 #endif // TVM_RELAX_DATAFLOW_PATTERN_H_
Runtime Array container types.
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 RelaxExprNode.
Definition: expr.h:405
Managed reference to TypeNode.
Definition: type.h:93
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition: with.h:58
Match a conjunction of other patterns.
Definition: dataflow_pattern.h:641
DFPattern left
Definition: dataflow_pattern.h:643
DFPattern right
Definition: dataflow_pattern.h:644
static constexpr const char * _type_key
Definition: dataflow_pattern.h:651
TVM_DECLARE_FINAL_OBJECT_INFO(AndPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:646
Managed reference to AndPatternNode.
Definition: dataflow_pattern.h:659
AndPattern(DFPattern lhs, DFPattern rhs)
TVM_DEFINE_OBJECT_REF_METHODS(AndPattern, DFPattern, AndPatternNode)
A pattern that asserting a root pattern has certain attributes.
Definition: dataflow_pattern.h:886
DictAttrs attrs
Definition: dataflow_pattern.h:889
DFPattern pattern
Definition: dataflow_pattern.h:888
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:891
TVM_DECLARE_FINAL_OBJECT_INFO(AttrPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:896
Managed reference to AttrPatternNode.
Definition: dataflow_pattern.h:904
TVM_DEFINE_OBJECT_REF_METHODS(AttrPattern, DFPattern, AttrPatternNode)
AttrPattern(DFPattern pattern, DictAttrs attrs)
A pattern to match a callable node in Relax.
Definition: dataflow_pattern.h:462
TVM_DECLARE_FINAL_OBJECT_INFO(CallPatternNode, DFPatternNode)
bool varg_default_wildcard
Definition: dataflow_pattern.h:477
static constexpr const char * _type_key
Definition: dataflow_pattern.h:486
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:481
tvm::Array< DFPattern > args
Definition: dataflow_pattern.h:470
DFPattern op
Definition: dataflow_pattern.h:469
Definition: dataflow_pattern.h:490
CallPattern(DFPattern op, Array< DFPattern > args, bool varg_default_wildcard=false)
TVM_DEFINE_OBJECT_REF_METHODS(CallPattern, DFPattern, CallPatternNode)
A Pattern to Match a Relax Constant.
Definition: dataflow_pattern.h:441
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:443
TVM_DECLARE_FINAL_OBJECT_INFO(ConstantPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:445
Managed reference to a ConstantPattern.
Definition: dataflow_pattern.h:453
TVM_DEFINE_OBJECT_REF_METHODS(ConstantPattern, DFPattern, ConstantPatternNode)
Additional constraints on the graph.
Definition: dataflow_pattern.h:167
TVM_DECLARE_BASE_OBJECT_INFO(DFConstraintNode, Object)
virtual Array< DFPattern > GetDependentPatterns() const =0
Return the patterns on which the constraint depends.
static constexpr const uint32_t _type_child_slots
Definition: dataflow_pattern.h:203
virtual std::tuple< PrimExpr, bool > AsPrimExpr(std::function< Optional< Var >(const DFPatternNode *)> match_state) const =0
Convert the constraint to a PrimExpr.
static constexpr const char * _type_key
Definition: dataflow_pattern.h:202
Definition: dataflow_pattern.h:207
TVM_DEFINE_OBJECT_REF_METHODS(DFConstraint, ObjectRef, DFConstraintNode)
Base type of all dataflow patterns.
Definition: dataflow_pattern.h:91
static constexpr const uint32_t _type_child_slots
Definition: dataflow_pattern.h:94
TVM_DECLARE_BASE_OBJECT_INFO(DFPatternNode, Object)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:93
Managed reference to dataflow patterns.
Definition: dataflow_pattern.h:102
CallPattern operator()(const std::vector< DFPattern > &args) const
Syntatic Sugar for creating a CallPattern.
ShapePattern HasShape(const Array< PrimExpr > &shape) const
Syntatic Sugar for creating a ShapePattern.
DataTypePattern HasDtype(const DataType &dtype) const
Syntatic Sugar for creating a DataTypePattern with a DataType.
SameShapeConstraint HasSameShapeAs(const DFPattern &other) const
Syntatic Sugar for creating a ShapePattern.
OrPattern operator|(const DFPattern &other) const
Syntatic Sugar for creating an OrPattern.
CallPattern operator()(Args &&... args) const
Syntatic Sugar for creating a CallPattern.
Definition: dataflow_pattern.h:963
TVM_DEFINE_OBJECT_REF_METHODS(DFPattern, ObjectRef, DFPatternNode)
TypePattern HasType(const Type &type) const
Syntatic Sugar for creating a TypePattern.
StructInfoPattern HasStructInfo(const StructInfo &struct_info) const
Syntatic Sugar for creating a StructInfoPattern.
AndPattern operator&(const DFPattern &other) const
Syntatic Sugar for creating an AndPattern.
DFPattern dup() const
Syntatic Sugar for duplicating the current pattern.
NotPattern operator~() const
Syntatic Sugar for creating a NotPattern.
AttrPattern HasAttr(const Map< String, ObjectRef > &attrs) const
Syntatic Sugar for creating an AttrPattern.
DataTypePattern HasDtype(const std::string &dtype) const
Syntatic Sugar for creating a DataTypePattern with a data type's name.
A pattern that asserting a root pattern has a certain data type.
Definition: dataflow_pattern.h:858
DFPattern pattern
Definition: dataflow_pattern.h:860
TVM_DECLARE_FINAL_OBJECT_INFO(DataTypePatternNode, DFPatternNode)
DataType dtype
Definition: dataflow_pattern.h:861
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:863
static constexpr const char * _type_key
Definition: dataflow_pattern.h:868
Managed reference to DataTypePatternNode.
Definition: dataflow_pattern.h:876
DataTypePattern(DFPattern pattern, DataType dtype)
TVM_DEFINE_OBJECT_REF_METHODS(DataTypePattern, DFPattern, DataTypePatternNode)
A Pattern to Match a Relax Dataflow Variable.
Definition: dataflow_pattern.h:400
TVM_DECLARE_FINAL_OBJECT_INFO(DataflowVarPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:402
Managed reference to a DataflowVarPattern.
Definition: dataflow_pattern.h:410
DataflowVarPattern(String name_hint)
TVM_DEFINE_OBJECT_REF_METHODS(DataflowVarPattern, DFPattern, DataflowVarPatternNode)
Pattern for Relax Expression.
Definition: dataflow_pattern.h:345
Expr expr
Definition: dataflow_pattern.h:347
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:349
TVM_DECLARE_FINAL_OBJECT_INFO(ExprPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:351
Managed reference to an ExprPattern.
Definition: dataflow_pattern.h:359
TVM_DEFINE_OBJECT_REF_METHODS(ExprPattern, DFPattern, ExprPatternNode)
A pattern of external function.
Definition: dataflow_pattern.h:915
const String & global_symbol() const
The external function name.
Definition: dataflow_pattern.h:920
String global_symbol_
Definition: dataflow_pattern.h:917
TVM_DECLARE_FINAL_OBJECT_INFO(ExternFuncPatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:921
static constexpr const char * _type_key
Definition: dataflow_pattern.h:923
Managed reference to ExternFuncPatternNode.
Definition: dataflow_pattern.h:931
ExternFuncPattern(String global_symbol)
TVM_DEFINE_OBJECT_REF_METHODS(ExternFuncPattern, DFPattern, ExternFuncPatternNode)
A pattern to match a Relax Function.
Definition: dataflow_pattern.h:524
DFPattern body
Definition: dataflow_pattern.h:533
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:535
static constexpr const char * _type_key
Definition: dataflow_pattern.h:540
TVM_DECLARE_FINAL_OBJECT_INFO(FunctionPatternNode, DFPatternNode)
tvm::Array< DFPattern > params
Definition: dataflow_pattern.h:526
Managed reference to FunctionPatternNode.
Definition: dataflow_pattern.h:548
FunctionPattern(tvm::Array< DFPattern > params, DFPattern body)
Constructor.
TVM_DEFINE_OBJECT_REF_METHODS(FunctionPattern, DFPattern, FunctionPatternNode)
A Pattern to Match a Relax Global Variable.
Definition: dataflow_pattern.h:421
TVM_DECLARE_FINAL_OBJECT_INFO(GlobalVarPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:423
Managed reference to a GlobalVarPattern.
Definition: dataflow_pattern.h:431
GlobalVarPattern(String name_hint)
TVM_DEFINE_OBJECT_REF_METHODS(GlobalVarPattern, DFPattern, GlobalVarPatternNode)
Pattern for rejecting a certain pattern.
Definition: dataflow_pattern.h:697
DFPattern reject
Definition: dataflow_pattern.h:699
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:701
TVM_DECLARE_FINAL_OBJECT_INFO(NotPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:703
Managed reference to NotPatternNode.
Definition: dataflow_pattern.h:711
NotPattern(DFPattern reject)
TVM_DEFINE_OBJECT_REF_METHODS(NotPattern, DFPattern, NotPatternNode)
Match a disjunction of other patterns.
Definition: dataflow_pattern.h:669
DFPattern left
Definition: dataflow_pattern.h:671
DFPattern right
Definition: dataflow_pattern.h:672
TVM_DECLARE_FINAL_OBJECT_INFO(OrPatternNode, DFPatternNode)
static constexpr const char * _type_key
Definition: dataflow_pattern.h:679
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:674
Managed reference to OrPatternNode.
Definition: dataflow_pattern.h:687
TVM_DEFINE_OBJECT_REF_METHODS(OrPattern, DFPattern, OrPatternNode)
OrPattern(DFPattern left, DFPattern right)
A context to manage the graph-level pattern matching.
Definition: dataflow_pattern.h:252
ExternUse
Constrainting matched graph with assertion to external uses.
Definition: dataflow_pattern.h:255
@ kMustNot
Definition: dataflow_pattern.h:257
@ kMay
Definition: dataflow_pattern.h:256
TVM_DECLARE_FINAL_OBJECT_INFO(PatternContextNode, Object)
std::vector< DFConstraint > validation_constraints
Definition: dataflow_pattern.h:269
static constexpr const char * _type_key
Definition: dataflow_pattern.h:271
enum tvm::relax::PatternContextNode::ExternUse allow_extern_use
std::map< DFPattern, std::vector< std::pair< DFPattern, std::vector< PairCons > > > > edge_constraints
Definition: dataflow_pattern.h:262
std::vector< DFPattern > src_ordered
Definition: dataflow_pattern.h:266
Managed reference to a pattern context.
Definition: dataflow_pattern.h:279
void add_constraint(DFConstraint constraint)
Add a validation constraint.
Definition: dataflow_pattern.h:325
void ExitWithScope() const
The RAII-like exit of a constraint context scope.
PatternContextNode * operator->()
Definition: dataflow_pattern.h:289
static Optional< PatternContext > Current()
Get the constraint context object on the top of the stack.
void add_constraint(DFPattern producer, DFPattern consumer, PairCons cons)
Build an edge constraint between two patterns (producer and consumer).
Definition: dataflow_pattern.h:301
const PatternContextNode * operator->() const
Definition: dataflow_pattern.h:284
PatternContext(bool incremental=false)
PatternContext(ObjectPtr< Object > n)
Definition: dataflow_pattern.h:281
void EnterWithScope() const
The RAII-like entry of a constraint context scope.
A sequence of DFPatterns that the previous DFPattern is connected to the next one.
Definition: dataflow_pattern.h:216
TVM_DECLARE_BASE_OBJECT_INFO(PatternSeqNode, Object)
tvm::Array< DFPattern > patterns
Definition: dataflow_pattern.h:218
static constexpr const char * _type_key
Definition: dataflow_pattern.h:222
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:221
std::vector< PairCons > pair_constraints
Definition: dataflow_pattern.h:219
Managed reference to pattern sequences.
Definition: dataflow_pattern.h:230
PatternSeq(tvm::Array< DFPattern > patterns, bool only_used_by=false)
PatternSeq OnlyUsedBy(PatternSeq other, int index=-1) const
TVM_DEFINE_OBJECT_REF_METHODS(PatternSeq, ObjectRef, PatternSeqNode)
PatternSeq UsedBy(PatternSeq other, int index=-1) const
friend PatternSeq UsedBy(const PatternSeq &lhs, const PatternSeq &rhs, int index)
Create used-by relationship between lhs[-1] and rhs[0], with [*lhs, *rhs] returned.
PatternSeq(DFPattern init_pattern)
friend PatternSeq OnlyUsedBy(const PatternSeq &lhs, const PatternSeq &rhs, int index)
Create only-used-by relationship between lhs[-1] and rhs[0], with [*lhs, *rhs] returned.
PatternSeq dup() const
Syntatic Sugar for duplicating the current pattern sequence.
A pattern to match an array of PrimExpr.
Definition: dataflow_pattern.h:501
TVM_DECLARE_FINAL_OBJECT_INFO(PrimArrPatternNode, DFPatternNode)
Array< PrimExpr > fields
Definition: dataflow_pattern.h:503
static constexpr const char * _type_key
Definition: dataflow_pattern.h:505
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:504
Managed reference to a PrimArrPattern.
Definition: dataflow_pattern.h:513
TVM_DEFINE_OBJECT_REF_METHODS(PrimArrPattern, DFPattern, PrimArrPatternNode)
PrimArrPattern(Array< PrimExpr > arr)
A pattern that asserting multiple root patterns have the same shape.
Definition: dataflow_pattern.h:829
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:838
Array< DFPattern > args
Definition: dataflow_pattern.h:831
TVM_DECLARE_FINAL_OBJECT_INFO(SameShapeConstraintNode, DFConstraintNode)
std::tuple< PrimExpr, bool > AsPrimExpr(std::function< Optional< Var >(const DFPatternNode *)> match_state) const override
Convert the constraint to a PrimExpr.
static constexpr const char * _type_key
Definition: dataflow_pattern.h:840
Array< DFPattern > GetDependentPatterns() const override
Return the patterns on which the constraint depends.
Definition: dataflow_pattern.h:833
Managed reference to SameShapePatternNode.
Definition: dataflow_pattern.h:848
TVM_DEFINE_OBJECT_REF_METHODS(SameShapeConstraint, DFConstraint, SameShapeConstraintNode)
SameShapeConstraint(Array< DFPattern > args)
A pattern that asserting a root pattern has a certain shape.
Definition: dataflow_pattern.h:801
static constexpr const char * _type_key
Definition: dataflow_pattern.h:811
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:806
TVM_DECLARE_FINAL_OBJECT_INFO(ShapePatternNode, DFPatternNode)
DFPattern pattern
Definition: dataflow_pattern.h:803
Array< PrimExpr > shape
Definition: dataflow_pattern.h:804
Managed reference to ShapePatternNode.
Definition: dataflow_pattern.h:819
TVM_DEFINE_OBJECT_REF_METHODS(ShapePattern, DFPattern, ShapePatternNode)
ShapePattern(DFPattern pattern, Array< PrimExpr > type)
Pattern for matching a certain struct info.
Definition: dataflow_pattern.h:777
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:782
StructInfo struct_info
Definition: dataflow_pattern.h:780
static constexpr const char * _type_key
Definition: dataflow_pattern.h:787
TVM_DECLARE_FINAL_OBJECT_INFO(StructInfoPatternNode, DFPatternNode)
DFPattern pattern
Definition: dataflow_pattern.h:779
Definition: dataflow_pattern.h:791
TVM_DEFINE_OBJECT_REF_METHODS(StructInfoPattern, DFPattern, StructInfoPatternNode)
StructInfoPattern(DFPattern pattern, StructInfo struct_info)
Managed reference to StructInfoNode.
Definition: expr.h:130
A pattern to match n'th indexing to a tuple.
Definition: dataflow_pattern.h:613
static constexpr const char * _type_key
Definition: dataflow_pattern.h:623
TVM_DECLARE_FINAL_OBJECT_INFO(TupleGetItemPatternNode, DFPatternNode)
DFPattern tuple
Definition: dataflow_pattern.h:615
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:618
int index
Definition: dataflow_pattern.h:616
Managed reference to TupleGetItemPatternNode.
Definition: dataflow_pattern.h:631
TVM_DEFINE_OBJECT_REF_METHODS(TupleGetItemPattern, DFPattern, TupleGetItemPatternNode)
TupleGetItemPattern(DFPattern tuple, int index)
Pattern to match a tuple of ordered expressions.
Definition: dataflow_pattern.h:564
tvm::Array< DFPattern > fields
Definition: dataflow_pattern.h:566
TVM_DECLARE_FINAL_OBJECT_INFO(TuplePatternNode, DFPatternNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:568
static constexpr const char * _type_key
Definition: dataflow_pattern.h:570
Managed reference to TuplePatternNode.
Definition: dataflow_pattern.h:578
TVM_DEFINE_OBJECT_REF_METHODS(TuplePattern, DFPattern, TuplePatternNode)
TuplePattern(tvm::Array< DFPattern > fields)
Pattern for matching a certain type.
Definition: dataflow_pattern.h:749
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:754
TVM_DECLARE_FINAL_OBJECT_INFO(TypePatternNode, DFPatternNode)
DFPattern pattern
Definition: dataflow_pattern.h:751
Type type
Definition: dataflow_pattern.h:752
static constexpr const char * _type_key
Definition: dataflow_pattern.h:759
Managed reference to TypePatternNode.
Definition: dataflow_pattern.h:767
TVM_DEFINE_OBJECT_REF_METHODS(TypePattern, DFPattern, TypePatternNode)
TypePattern(DFPattern pattern, Type type)
A pattern to match multiple expressions unorderedly.
Definition: dataflow_pattern.h:588
tvm::Array< DFPattern > fields
Definition: dataflow_pattern.h:590
static constexpr const char * _type_key
Definition: dataflow_pattern.h:594
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:592
TVM_DECLARE_FINAL_OBJECT_INFO(UnorderedTuplePatternNode, DFPatternNode)
Managed reference to UnorderedTuplePatternNode.
Definition: dataflow_pattern.h:602
UnorderedTuplePattern(tvm::Array< DFPattern > fields)
TVM_DEFINE_OBJECT_REF_METHODS(UnorderedTuplePattern, DFPattern, UnorderedTuplePatternNode)
A Pattern to Match a Relax Variable.
Definition: dataflow_pattern.h:370
static constexpr const uint32_t _type_child_slots
Definition: dataflow_pattern.h:377
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:374
String name
Definition: dataflow_pattern.h:372
const String & name_hint() const
Definition: dataflow_pattern.h:373
static constexpr const char * _type_key
Definition: dataflow_pattern.h:376
TVM_DECLARE_BASE_OBJECT_INFO(VarPatternNode, DFPatternNode)
Managed reference to a VarPattern.
Definition: dataflow_pattern.h:385
TVM_DEFINE_OBJECT_REF_METHODS(VarPattern, DFPattern, VarPatternNode)
VarPattern(String name_hint)
Create a pattern matching by variable name.
Wildcard Pattern is a pattern that can match anything.
Definition: dataflow_pattern.h:721
static constexpr const char * _type_key
Definition: dataflow_pattern.h:725
void VisitAttrs(tvm::AttrVisitor *v)
Definition: dataflow_pattern.h:723
TVM_DECLARE_FINAL_OBJECT_INFO(WildcardPatternNode, DFPatternNode)
Managed reference to WildcardPatternNode.
Definition: dataflow_pattern.h:733
TVM_DEFINE_NOTNULLABLE_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:43
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
A custom smart pointer for Object.
Definition: object.h:363
Base class of all object reference.
Definition: object.h:520
const Object * get() const
Definition: object.h:555
Object * get_mutable() const
Definition: object.h:608
base class of all object containers.
Definition: object.h:172
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:97
Base expr nodes in TVM.
CallPattern IsCallTIR(const String &name, Optional< TuplePattern > args=NullOpt)
Syntatic Sugar for call_tir (return a tensor)
PatternSeq operator^(const PatternSeq &lhs, const PatternSeq &rhs)
Syntax sugar of UsedBy(lhs, rhs, -1).
CallPattern IsCallDPSPacked(const String &name, Optional< TuplePattern > args=NullOpt)
Syntatic Sugar for call_dps_packed (return a tensor)
ExprPattern IsOp(const String &op_name)
Syntatic Sugar for creating a ExprPattern base on an Op.
DFPattern IsTuple(const Array< DFPattern > &fields, bool unordered=false)
Syntatic Sugar for creating TuplePattern or UnorderedTuplePattern (unordered=true)
WildcardPattern Wildcard()
Syntatic Sugar for creating a WildcardPattern.
ConstantPattern IsConst()
Syntatic Sugar for creating a ConstantPattern.
PatternSeq UsedBy(const PatternSeq &lhs, const PatternSeq &rhs, int index=-1)
Create used-by relationship between lhs[-1] and rhs[0], with [*lhs, *rhs] returned.
PatternSeq OnlyUsedBy(const PatternSeq &lhs, const PatternSeq &rhs, int index=-1)
Create only-used-by relationship between lhs[-1] and rhs[0], with [*lhs, *rhs] returned.
ExprPattern IsExpr(const Expr &expr)
Syntatic Sugar for creating a ExprPattern.
PatternSeq operator>>(const PatternSeq &lhs, const PatternSeq &rhs)
Syntax sugar of OnlyUsedBy(lhs, rhs, -1).
TupleGetItemPattern IsTupleGetItem(const DFPattern tuple, int index=-1)
Syntatic Sugar for creating a TupleGetItemPattern.
VarPattern IsVar(const String &name)
Syntatic Sugar for creating a VarPattern with a name.
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
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
constexpr runtime::NullOptType NullOpt
Definition: optional.h:169
Runtime Optional container types.
Relax Types.
Constraint of a DFPattern edge (producer -> consumer) in graph-level matching.
Definition: dataflow_pattern.h:139
int index
Definition: dataflow_pattern.h:145
bool operator==(const PairCons &other) const
Definition: dataflow_pattern.h:155
enum tvm::relax::PairCons::Type type
Type
Constraint types of the edge.
Definition: dataflow_pattern.h:141
@ kOnlyUsedBy
Definition: dataflow_pattern.h:143
@ kUsedBy
Definition: dataflow_pattern.h:142
PairCons(Type t, int index=-1)
Construct a new PairCons object.
Definition: dataflow_pattern.h:153
RAII wrapper function to enter and exit a context object similar to python's with syntax.