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_RELAX_DATAFLOW_PATTERN_H_
25 #define TVM_RELAX_DATAFLOW_PATTERN_H_
26 
27 #include <tvm/ffi/container/array.h>
28 #include <tvm/ffi/optional.h>
29 #include <tvm/ffi/reflection/registry.h>
30 #include <tvm/ir/expr.h>
31 #include <tvm/ir/with_context.h>
32 #include <tvm/relax/expr.h>
33 #include <tvm/relax/type.h>
34 
35 #include <cstdint>
36 #include <functional>
37 #include <map>
38 #include <memory>
39 #include <string>
40 #include <tuple>
41 #include <utility>
42 #include <vector>
43 
44 namespace tvm {
45 
46 namespace arith {
47 class AnalyzerObj;
48 class Analyzer;
49 } // namespace arith
50 
51 namespace relax {
52 
53 class PatternSeq;
54 class CallPattern;
55 class OrPattern;
56 class AndPattern;
57 class NotPattern;
58 class ShapePattern;
59 class TypePattern;
60 class DataTypePattern;
61 class AttrPattern;
62 class SameShapeConstraint;
63 
72 TVM_DLL PatternSeq UsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index = -1);
74 TVM_DLL PatternSeq operator^(const PatternSeq& lhs, const PatternSeq& rhs);
75 
84 TVM_DLL PatternSeq OnlyUsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index = -1);
86 TVM_DLL PatternSeq operator>>(const PatternSeq& lhs, const PatternSeq& rhs);
87 
92 class DFPatternNode : public ffi::Object {
93  public:
94  static constexpr const uint32_t _type_child_slots = 21;
95  TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.DFPattern", DFPatternNode, ffi::Object);
96 };
97 
102 class DFPattern : public ffi::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 ffi::Map<ffi::String, Any>& attrs) const;
118  TVM_DLL TypePattern HasType(const Type& ty) const;
120  TVM_DLL DataTypePattern HasDtype(DLDataType dtype) const;
122  TVM_DLL DataTypePattern HasDtype(const std::string& dtype) const;
124  TVM_DLL ShapePattern HasShape(const ffi::Array<PrimExpr>& shape) const;
126  TVM_DLL SameShapeConstraint HasSameShapeAs(const DFPattern& other) const;
128  TVM_DLL DFPattern dup() const;
129 
131  TVM_DLL operator PatternSeq() const;
132 
134 };
135 
137 struct PairCons {
139  enum Type {
143  int index = -1;
151  TVM_DLL explicit PairCons(Type t, int index = -1) : type(t), index(index) {}
152 
153  bool operator==(const PairCons& other) const {
154  return type == other.type && index == other.index;
155  }
156 };
157 
165 class DFConstraintNode : public ffi::Object {
166  public:
168  virtual ffi::Array<DFPattern> GetDependentPatterns() const = 0;
169 
197  virtual std::tuple<PrimExpr, bool> AsCondition(
198  std::function<ffi::Optional<Var>(const DFPatternNode*)> match_state) const = 0;
199 
200  static constexpr const uint32_t _type_child_slots = 1;
201  TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.DFConstraint", DFConstraintNode, ffi::Object);
202 };
203 
204 class DFConstraint : public ffi::ObjectRef {
205  public:
207 };
208 
213 class PatternSeqNode final : public ffi::Object {
214  public:
215  tvm::ffi::Array<DFPattern> patterns;
216  std::vector<PairCons> pair_constraints;
218  static void RegisterReflection() {
219  namespace refl = tvm::ffi::reflection;
220  refl::ObjectDef<PatternSeqNode>().def_ro("patterns", &PatternSeqNode::patterns);
221  }
222  TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.PatternSeq", PatternSeqNode, ffi::Object);
223 };
224 
229 class PatternSeq final : public ffi::ObjectRef {
230  public:
231  TVM_DLL explicit PatternSeq(DFPattern init_pattern);
232  TVM_DLL explicit PatternSeq(tvm::ffi::Array<DFPattern> patterns, bool only_used_by = false);
233 
234  PatternSeq UsedBy(PatternSeq other, int index = -1) const;
235  PatternSeq OnlyUsedBy(PatternSeq other, int index = -1) const;
236 
238  PatternSeq dup() const;
239 
240  // friend functions
241  friend PatternSeq UsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index);
242  friend PatternSeq OnlyUsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index);
243 
245 };
246 
251 class PatternContextNode : public ffi::Object {
252  public:
254  enum ExternUse {
258 
259  // src node -> <dst node, constraint type> constraints.
260  // Dst nodes are kept in a vector to keep them ordered.
261  std::map<DFPattern, std::vector<std::pair<DFPattern, std::vector<PairCons>>>> edge_constraints;
262 
263  // Underlying DFPattern nodes which the edge constraints may reference
264  // Kept as a separate vector of patterns to process constraints in a fixed order.
265  std::vector<DFPattern> src_ordered;
266 
267  // Non-edge constraints
268  std::vector<DFConstraint> validation_constraints;
269  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.PatternContext", PatternContextNode, ffi::Object);
270 };
271 
276 class PatternContext : public ffi::ObjectRef {
277  public:
278  explicit PatternContext(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
279  TVM_DLL explicit PatternContext(ffi::ObjectPtr<ffi::Object> n) : ffi::ObjectRef(n) {}
280  TVM_DLL explicit PatternContext(bool incremental = false);
281 
283  TVM_FFI_ICHECK(get() != nullptr);
284  return static_cast<const PatternContextNode*>(get());
285  }
286 
288  TVM_FFI_ICHECK(get() != nullptr);
289  return static_cast<PatternContextNode*>(get_mutable());
290  }
291 
299  void add_constraint(DFPattern producer, DFPattern consumer, PairCons cons) {
300  auto& pairs = (*this)->edge_constraints[producer];
301  auto it = std::find_if(pairs.begin(), pairs.end(),
302  [consumer](auto p) { return p.first == consumer; });
303  if (it == pairs.end()) {
304  pairs.emplace_back(consumer, std::vector{cons});
305  } else {
306  auto& vec = it->second;
307  TVM_FFI_ICHECK(std::find(vec.cbegin(), vec.cend(), cons) == vec.cend())
308  << "Constraint already exists";
309  vec.push_back(cons);
310  }
311 
312  auto& patterns = (*this)->src_ordered;
313  if (std::find(patterns.begin(), patterns.end(), producer) == patterns.end()) {
314  patterns.push_back(producer);
315  }
316  }
317 
323  void add_constraint(DFConstraint constraint) {
324  (*this)->validation_constraints.push_back(constraint);
325  }
326 
328  TVM_DLL static ffi::Optional<PatternContext> Current();
329 
331  TVM_DLL void EnterWithScope() const;
333  TVM_DLL void ExitWithScope() const;
334 
335  private:
336  friend class With<PatternContext>;
337 };
338 
344  public:
347  static void RegisterReflection() {
348  namespace refl = tvm::ffi::reflection;
349  refl::ObjectDef<ExprPatternNode>().def_ro("expr", &ExprPatternNode::expr);
350  }
352 };
353 
358 class ExprPattern : public DFPattern {
359  public:
360  TVM_DLL explicit ExprPattern(Expr expr);
362 };
363 
370  public:
371  ffi::String name;
372  const ffi::String& name_hint() const { return name; }
373 
374  static void RegisterReflection() {
375  namespace refl = tvm::ffi::reflection;
376  refl::ObjectDef<VarPatternNode>().def_ro("name", &VarPatternNode::name);
377  }
378 
379  static constexpr const uint32_t _type_child_slots = 1;
381 };
382 
387 class VarPattern : public DFPattern {
388  public:
394  TVM_DLL VarPattern(ffi::String name_hint);
396 };
397 
403  public:
404  static void RegisterReflection() {
405  namespace refl = tvm::ffi::reflection;
406  refl::ObjectDef<DataflowVarPatternNode>();
407  }
410 };
411 
417  public:
419  TVM_DLL DataflowVarPattern(ffi::String name_hint);
421 };
422 
428  public:
430  DFPatternNode);
431 };
432 
437 class GlobalVarPattern : public DFPattern {
438  public:
439  TVM_DLL GlobalVarPattern(ffi::String name_hint);
441 };
442 
448  public:
449  static void RegisterReflection() {
450  namespace refl = tvm::ffi::reflection;
451  refl::ObjectDef<ConstantPatternNode>();
452  }
454  DFPatternNode);
455 };
456 
461 class ConstantPattern : public DFPattern {
462  public:
464 };
465 
471  public:
478  tvm::ffi::Array<DFPattern> args;
487  // Todo(relax-team): Dataflow pattern for Type, and match ty_args
488 
489  static void RegisterReflection() {
490  namespace refl = tvm::ffi::reflection;
491  refl::ObjectDef<CallPatternNode>()
492  .def_ro("op", &CallPatternNode::op)
493  .def_ro("args", &CallPatternNode::args);
494  }
496 };
497 
498 class CallPattern : public DFPattern {
499  public:
500  TVM_DLL CallPattern(DFPattern op, ffi::Array<DFPattern> args, bool varg_default_wildcard = false);
502 };
503 
510  public:
511  ffi::Array<PrimExpr> fields;
513  static void RegisterReflection() {
514  namespace refl = tvm::ffi::reflection;
515  refl::ObjectDef<PrimArrPatternNode>().def_ro("fields", &PrimArrPatternNode::fields);
516  }
518 };
519 
524 class PrimArrPattern : public DFPattern {
525  public:
526  TVM_DLL PrimArrPattern(ffi::Array<PrimExpr> arr);
528 };
529 
536  public:
537  tvm::ffi::Array<DFPattern> params;
546  static void RegisterReflection() {
547  namespace refl = tvm::ffi::reflection;
548  refl::ObjectDef<FunctionPatternNode>()
549  .def_ro("params", &FunctionPatternNode::params)
550  .def_ro("body", &FunctionPatternNode::body);
551  }
553  DFPatternNode);
554 };
555 
560 class FunctionPattern : public DFPattern {
561  public:
567  TVM_DLL FunctionPattern(tvm::ffi::Array<DFPattern> params, DFPattern body);
568 
570 };
571 
577  public:
578  tvm::ffi::Array<DFPattern> fields;
580  static void RegisterReflection() {
581  namespace refl = tvm::ffi::reflection;
582  refl::ObjectDef<TuplePatternNode>().def_ro("fields", &TuplePatternNode::fields);
583  }
585 };
586 
591 class TuplePattern : public DFPattern {
592  public:
593  TVM_DLL explicit TuplePattern(tvm::ffi::Array<DFPattern> fields);
595 };
596 
602  public:
603  tvm::ffi::Array<DFPattern> fields;
605  static void RegisterReflection() {
606  namespace refl = tvm::ffi::reflection;
607  refl::ObjectDef<UnorderedTuplePatternNode>().def_ro("fields",
609  }
611  DFPatternNode);
612 };
613 
619  public:
620  TVM_DLL explicit UnorderedTuplePattern(tvm::ffi::Array<DFPattern> fields);
623 };
624 
631  public:
633  int index;
635  static void RegisterReflection() {
636  namespace refl = tvm::ffi::reflection;
637  refl::ObjectDef<TupleGetItemPatternNode>()
638  .def_ro("tuple", &TupleGetItemPatternNode::tuple)
639  .def_ro("index", &TupleGetItemPatternNode::index);
640  }
642  DFPatternNode);
643 };
644 
650  public:
651  TVM_DLL TupleGetItemPattern(DFPattern tuple, int index);
654 };
655 
661  public:
665  static void RegisterReflection() {
666  namespace refl = tvm::ffi::reflection;
667  refl::ObjectDef<AndPatternNode>()
668  .def_ro("left", &AndPatternNode::left)
669  .def_ro("right", &AndPatternNode::right);
670  }
672 };
673 
678 class AndPattern : public DFPattern {
679  public:
680  TVM_DLL AndPattern(DFPattern lhs, DFPattern rhs);
682 };
683 
688 class OrPatternNode : public DFPatternNode {
689  public:
693  static void RegisterReflection() {
694  namespace refl = tvm::ffi::reflection;
695  refl::ObjectDef<OrPatternNode>()
696  .def_ro("left", &OrPatternNode::left)
697  .def_ro("right", &OrPatternNode::right);
698  }
700 };
701 
706 class OrPattern : public DFPattern {
707  public:
708  TVM_DLL OrPattern(DFPattern left, DFPattern right);
710 };
711 
717  public:
720  static void RegisterReflection() {
721  namespace refl = tvm::ffi::reflection;
722  refl::ObjectDef<NotPatternNode>().def_ro("reject", &NotPatternNode::reject);
723  }
725 };
726 
731 class NotPattern : public DFPattern {
732  public:
733  TVM_DLL NotPattern(DFPattern reject);
735 };
736 
742  public:
743  static void RegisterReflection() {
744  namespace refl = tvm::ffi::reflection;
745  refl::ObjectDef<WildcardPatternNode>();
746  }
748  DFPatternNode);
749 };
750 
755 class WildcardPattern : public DFPattern {
756  public:
758  explicit WildcardPattern(ffi::ObjectPtr<WildcardPatternNode> data)
759  : DFPattern(ffi::UnsafeInit{}) {
760  TVM_FFI_ICHECK(data != nullptr);
761  data_ = std::move(data);
762  }
763 
764  // Declaring WildcardPattern declared as non-nullable avoids the
765  // default zero-parameter constructor for ffi::ObjectRef with `data_ =
766  // nullptr`. This allows a zero-parameter constructor to be
767  // declared here, to create a valid wildcard instance.
768 
770 };
771 
777  public:
781  static void RegisterReflection() {
782  namespace refl = tvm::ffi::reflection;
783  refl::ObjectDef<TypePatternNode>()
784  .def_ro("pattern", &TypePatternNode::pattern)
785  .def_ro("ty", &TypePatternNode::ty);
786  }
788 };
789 
790 class TypePattern : public DFPattern {
791  public:
792  TVM_DLL TypePattern(DFPattern pattern, Type ty);
794 };
795 
801  public:
803  ffi::Array<PrimExpr> shape;
805  static void RegisterReflection() {
806  namespace refl = tvm::ffi::reflection;
807  refl::ObjectDef<ShapePatternNode>()
808  .def_ro("pattern", &ShapePatternNode::pattern)
809  .def_ro("shape", &ShapePatternNode::shape);
810  }
812 };
813 
818 class ShapePattern : public DFPattern {
819  public:
820  TVM_DLL ShapePattern(DFPattern pattern, ffi::Array<PrimExpr> type);
822 };
823 
829  public:
830  ffi::Array<DFPattern> args;
832  ffi::Array<DFPattern> GetDependentPatterns() const override { return args; }
833 
834  std::tuple<PrimExpr, bool> AsCondition(
835  std::function<ffi::Optional<Var>(const DFPatternNode*)> match_state) const override;
836 
837  static void RegisterReflection() {
838  namespace refl = tvm::ffi::reflection;
839  refl::ObjectDef<SameShapeConstraintNode>().def_ro("args", &SameShapeConstraintNode::args);
840  }
843 };
844 
850  public:
851  TVM_DLL SameShapeConstraint(ffi::Array<DFPattern> args);
854 };
855 
861  public:
863  DLDataType dtype;
865  static void RegisterReflection() {
866  namespace refl = tvm::ffi::reflection;
867  refl::ObjectDef<DataTypePatternNode>()
868  .def_ro("pattern", &DataTypePatternNode::pattern)
869  .def_ro("dtype", &DataTypePatternNode::dtype);
870  }
872  DFPatternNode);
873 };
874 
879 class DataTypePattern : public DFPattern {
880  public:
881  TVM_DLL DataTypePattern(DFPattern pattern, DLDataType dtype);
883 };
884 
890  public:
894  static void RegisterReflection() {
895  namespace refl = tvm::ffi::reflection;
896  refl::ObjectDef<AttrPatternNode>()
897  .def_ro("pattern", &AttrPatternNode::pattern)
898  .def_ro("attrs", &AttrPatternNode::attrs);
899  }
901 };
902 
907 class AttrPattern : public DFPattern {
908  public:
909  TVM_DLL AttrPattern(DFPattern pattern, DictAttrs attrs);
911 };
912 
919  public:
920  ffi::String global_symbol_;
923  const ffi::String& global_symbol() const { return global_symbol_; }
924 
925  static void RegisterReflection() {
926  namespace refl = tvm::ffi::reflection;
927  refl::ObjectDef<ExternFuncPatternNode>().def_ro("global_symbol",
929  }
931  DFPatternNode);
932 };
933 
938 class ExternFuncPattern : public DFPattern {
939  public:
940  TVM_DLL ExternFuncPattern(ffi::String global_symbol);
942 };
943 
945 VarPattern IsVar(const ffi::String& name);
951 ExprPattern IsExpr(const Expr& expr);
953 ExprPattern IsOp(const ffi::String& op_name);
955 // Todo(relax-team): Dataflow pattern for Type, and match out_ty
956 CallPattern IsCallTIR(const ffi::String& name, ffi::Optional<TuplePattern> args = std::nullopt);
958 CallPattern IsCallTIR(const ffi::String& name, TuplePattern var_args);
960 CallPattern IsCallDPSPacked(const ffi::String& name,
961  ffi::Optional<TuplePattern> args = std::nullopt);
963 CallPattern IsCallDPSPacked(const ffi::String& name, TuplePattern var_args);
965 DFPattern IsTuple(const ffi::Array<DFPattern>& fields, bool unordered = false);
967 TupleGetItemPattern IsTupleGetItem(const DFPattern tuple, int index = -1);
968 
970 template <typename... Args>
971 CallPattern DFPattern::operator()(Args&&... args) const {
972  return CallPattern(ffi::GetRef<DFPattern>(this->get()),
973  ffi::Array<DFPattern>({std::forward<Args>(args)...}));
974 }
975 
976 } // namespace relax
977 } // namespace tvm
978 #endif // TVM_RELAX_DATAFLOW_PATTERN_H_
Managed reference to DictAttrsNode.
Definition: attrs.h:102
Managed reference to ExprNode.
Definition: base_expr.h:311
Managed reference to TypeNode.
Definition: base_expr.h:77
static Type Missing()
Sentinel for a type that has not been populated yet.
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition: with_context.h:59
Match a conjunction of other patterns.
Definition: dataflow_pattern.h:660
DFPattern left
Definition: dataflow_pattern.h:662
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.AndPattern", AndPatternNode, DFPatternNode)
DFPattern right
Definition: dataflow_pattern.h:663
static void RegisterReflection()
Definition: dataflow_pattern.h:665
Managed reference to AndPatternNode.
Definition: dataflow_pattern.h:678
AndPattern(DFPattern lhs, DFPattern rhs)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(AndPattern, DFPattern, AndPatternNode)
A pattern that asserting a root pattern has certain attributes.
Definition: dataflow_pattern.h:889
DictAttrs attrs
Definition: dataflow_pattern.h:892
DFPattern pattern
Definition: dataflow_pattern.h:891
static void RegisterReflection()
Definition: dataflow_pattern.h:894
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.AttrPattern", AttrPatternNode, DFPatternNode)
Managed reference to AttrPatternNode.
Definition: dataflow_pattern.h:907
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(AttrPattern, DFPattern, AttrPatternNode)
AttrPattern(DFPattern pattern, DictAttrs attrs)
A pattern to match a callable node in Relax.
Definition: dataflow_pattern.h:470
bool varg_default_wildcard
Definition: dataflow_pattern.h:485
static void RegisterReflection()
Definition: dataflow_pattern.h:489
tvm::ffi::Array< DFPattern > args
Definition: dataflow_pattern.h:478
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.CallPattern", CallPatternNode, DFPatternNode)
DFPattern op
Definition: dataflow_pattern.h:477
Definition: dataflow_pattern.h:498
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(CallPattern, DFPattern, CallPatternNode)
CallPattern(DFPattern op, ffi::Array< DFPattern > args, bool varg_default_wildcard=false)
A Pattern to Match a Relax Constant.
Definition: dataflow_pattern.h:447
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.ConstantPattern", ConstantPatternNode, DFPatternNode)
static void RegisterReflection()
Definition: dataflow_pattern.h:449
Managed reference to a ConstantPattern.
Definition: dataflow_pattern.h:461
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ConstantPattern, DFPattern, ConstantPatternNode)
Additional constraints on the graph.
Definition: dataflow_pattern.h:165
TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.DFConstraint", DFConstraintNode, ffi::Object)
virtual std::tuple< PrimExpr, bool > AsCondition(std::function< ffi::Optional< Var >(const DFPatternNode *)> match_state) const =0
Convert the constraint to a PrimExpr.
virtual ffi::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:200
Definition: dataflow_pattern.h:204
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DFConstraint, ffi::ObjectRef, DFConstraintNode)
Base type of all dataflow patterns.
Definition: dataflow_pattern.h:92
static constexpr const uint32_t _type_child_slots
Definition: dataflow_pattern.h:94
TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.DFPattern", DFPatternNode, ffi::Object)
Managed reference to dataflow patterns.
Definition: dataflow_pattern.h:102
CallPattern operator()(const std::vector< DFPattern > &args) const
Syntatic Sugar for creating a CallPattern.
SameShapeConstraint HasSameShapeAs(const DFPattern &other) const
Syntatic Sugar for creating a ShapePattern.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DFPattern, ffi::ObjectRef, DFPatternNode)
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:971
TypePattern HasType(const Type &ty) const
Syntatic Sugar for creating a TypePattern.
AttrPattern HasAttr(const ffi::Map< ffi::String, Any > &attrs) const
Syntatic Sugar for creating an AttrPattern.
ShapePattern HasShape(const ffi::Array< PrimExpr > &shape) const
Syntatic Sugar for creating a ShapePattern.
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.
DataTypePattern HasDtype(const std::string &dtype) const
Syntatic Sugar for creating a DataTypePattern with a data type's name.
DataTypePattern HasDtype(DLDataType dtype) const
Syntatic Sugar for creating a DataTypePattern with a dtype.
A pattern that asserting a root pattern has a certain data type.
Definition: dataflow_pattern.h:860
DFPattern pattern
Definition: dataflow_pattern.h:862
DLDataType dtype
Definition: dataflow_pattern.h:863
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.DataTypePattern", DataTypePatternNode, DFPatternNode)
static void RegisterReflection()
Definition: dataflow_pattern.h:865
Managed reference to DataTypePatternNode.
Definition: dataflow_pattern.h:879
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DataTypePattern, DFPattern, DataTypePatternNode)
DataTypePattern(DFPattern pattern, DLDataType dtype)
A Pattern to Match a Relax Dataflow Variable.
Definition: dataflow_pattern.h:402
static void RegisterReflection()
Definition: dataflow_pattern.h:404
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.DataflowVarPattern", DataflowVarPatternNode, VarPatternNode)
Managed reference to a DataflowVarPattern.
Definition: dataflow_pattern.h:416
DataflowVarPattern(ffi::String name_hint)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DataflowVarPattern, DFPattern, DataflowVarPatternNode)
Pattern for Relax Expression.
Definition: dataflow_pattern.h:343
Expr expr
Definition: dataflow_pattern.h:345
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.ExprPattern", ExprPatternNode, DFPatternNode)
static void RegisterReflection()
Definition: dataflow_pattern.h:347
Managed reference to an ExprPattern.
Definition: dataflow_pattern.h:358
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ExprPattern, DFPattern, ExprPatternNode)
A pattern of external function.
Definition: dataflow_pattern.h:918
const ffi::String & global_symbol() const
The external function name.
Definition: dataflow_pattern.h:923
static void RegisterReflection()
Definition: dataflow_pattern.h:925
ffi::String global_symbol_
Definition: dataflow_pattern.h:920
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.ExternFuncPattern", ExternFuncPatternNode, DFPatternNode)
Managed reference to ExternFuncPatternNode.
Definition: dataflow_pattern.h:938
ExternFuncPattern(ffi::String global_symbol)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ExternFuncPattern, DFPattern, ExternFuncPatternNode)
A pattern to match a Relax Function.
Definition: dataflow_pattern.h:535
static void RegisterReflection()
Definition: dataflow_pattern.h:546
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.FunctionPattern", FunctionPatternNode, DFPatternNode)
DFPattern body
Definition: dataflow_pattern.h:544
tvm::ffi::Array< DFPattern > params
Definition: dataflow_pattern.h:537
Managed reference to FunctionPatternNode.
Definition: dataflow_pattern.h:560
FunctionPattern(tvm::ffi::Array< DFPattern > params, DFPattern body)
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(FunctionPattern, DFPattern, FunctionPatternNode)
A Pattern to Match a Relax Global Variable.
Definition: dataflow_pattern.h:427
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.GlobalVarPattern", GlobalVarPatternNode, DFPatternNode)
Managed reference to a GlobalVarPattern.
Definition: dataflow_pattern.h:437
GlobalVarPattern(ffi::String name_hint)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(GlobalVarPattern, DFPattern, GlobalVarPatternNode)
Pattern for rejecting a certain pattern.
Definition: dataflow_pattern.h:716
DFPattern reject
Definition: dataflow_pattern.h:718
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.NotPattern", NotPatternNode, DFPatternNode)
static void RegisterReflection()
Definition: dataflow_pattern.h:720
Managed reference to NotPatternNode.
Definition: dataflow_pattern.h:731
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(NotPattern, DFPattern, NotPatternNode)
NotPattern(DFPattern reject)
Match a disjunction of other patterns.
Definition: dataflow_pattern.h:688
DFPattern left
Definition: dataflow_pattern.h:690
DFPattern right
Definition: dataflow_pattern.h:691
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.OrPattern", OrPatternNode, DFPatternNode)
static void RegisterReflection()
Definition: dataflow_pattern.h:693
Managed reference to OrPatternNode.
Definition: dataflow_pattern.h:706
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(OrPattern, DFPattern, OrPatternNode)
OrPattern(DFPattern left, DFPattern right)
A context to manage the graph-level pattern matching.
Definition: dataflow_pattern.h:251
ExternUse
Constrainting matched graph with assertion to external uses.
Definition: dataflow_pattern.h:254
@ kMustNot
Definition: dataflow_pattern.h:256
@ kMay
Definition: dataflow_pattern.h:255
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.PatternContext", PatternContextNode, ffi::Object)
std::vector< DFConstraint > validation_constraints
Definition: dataflow_pattern.h:268
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:261
std::vector< DFPattern > src_ordered
Definition: dataflow_pattern.h:265
Managed reference to a pattern context.
Definition: dataflow_pattern.h:276
void add_constraint(DFConstraint constraint)
Add a validation constraint.
Definition: dataflow_pattern.h:323
void ExitWithScope() const
The RAII-like exit of a constraint context scope.
static ffi::Optional< PatternContext > Current()
Get the constraint context object on the top of the stack.
PatternContextNode * operator->()
Definition: dataflow_pattern.h:287
void add_constraint(DFPattern producer, DFPattern consumer, PairCons cons)
Build an edge constraint between two patterns (producer and consumer).
Definition: dataflow_pattern.h:299
const PatternContextNode * operator->() const
Definition: dataflow_pattern.h:282
PatternContext(ffi::ObjectPtr< ffi::Object > n)
Definition: dataflow_pattern.h:279
PatternContext(bool incremental=false)
PatternContext(ffi::UnsafeInit tag)
Definition: dataflow_pattern.h:278
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:213
static void RegisterReflection()
Definition: dataflow_pattern.h:218
TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.PatternSeq", PatternSeqNode, ffi::Object)
std::vector< PairCons > pair_constraints
Definition: dataflow_pattern.h:216
tvm::ffi::Array< DFPattern > patterns
Definition: dataflow_pattern.h:215
Managed reference to pattern sequences.
Definition: dataflow_pattern.h:229
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PatternSeq, ffi::ObjectRef, PatternSeqNode)
PatternSeq OnlyUsedBy(PatternSeq other, int index=-1) const
PatternSeq UsedBy(PatternSeq other, int index=-1) const
PatternSeq(tvm::ffi::Array< DFPattern > patterns, bool only_used_by=false)
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:509
ffi::Array< PrimExpr > fields
Definition: dataflow_pattern.h:511
static void RegisterReflection()
Definition: dataflow_pattern.h:513
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.PrimArrPattern", PrimArrPatternNode, DFPatternNode)
Managed reference to a PrimArrPattern.
Definition: dataflow_pattern.h:524
PrimArrPattern(ffi::Array< PrimExpr > arr)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PrimArrPattern, DFPattern, PrimArrPatternNode)
A pattern that asserting multiple root patterns have the same shape.
Definition: dataflow_pattern.h:828
std::tuple< PrimExpr, bool > AsCondition(std::function< ffi::Optional< Var >(const DFPatternNode *)> match_state) const override
Convert the constraint to a PrimExpr.
ffi::Array< DFPattern > GetDependentPatterns() const override
Return the patterns on which the constraint depends.
Definition: dataflow_pattern.h:832
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.SameShapeConstraint", SameShapeConstraintNode, DFConstraintNode)
static void RegisterReflection()
Definition: dataflow_pattern.h:837
ffi::Array< DFPattern > args
Definition: dataflow_pattern.h:830
Managed reference to SameShapePatternNode.
Definition: dataflow_pattern.h:849
SameShapeConstraint(ffi::Array< DFPattern > args)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(SameShapeConstraint, DFConstraint, SameShapeConstraintNode)
A pattern that asserting a root pattern has a certain shape.
Definition: dataflow_pattern.h:800
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.ShapePattern", ShapePatternNode, DFPatternNode)
ffi::Array< PrimExpr > shape
Definition: dataflow_pattern.h:803
DFPattern pattern
Definition: dataflow_pattern.h:802
static void RegisterReflection()
Definition: dataflow_pattern.h:805
Managed reference to ShapePatternNode.
Definition: dataflow_pattern.h:818
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ShapePattern, DFPattern, ShapePatternNode)
ShapePattern(DFPattern pattern, ffi::Array< PrimExpr > type)
A pattern to match n'th indexing to a tuple.
Definition: dataflow_pattern.h:630
static void RegisterReflection()
Definition: dataflow_pattern.h:635
DFPattern tuple
Definition: dataflow_pattern.h:632
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.TupleGetItemPattern", TupleGetItemPatternNode, DFPatternNode)
int index
Definition: dataflow_pattern.h:633
Managed reference to TupleGetItemPatternNode.
Definition: dataflow_pattern.h:649
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TupleGetItemPattern, DFPattern, TupleGetItemPatternNode)
TupleGetItemPattern(DFPattern tuple, int index)
Pattern to match a tuple of ordered expressions.
Definition: dataflow_pattern.h:576
tvm::ffi::Array< DFPattern > fields
Definition: dataflow_pattern.h:578
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.TuplePattern", TuplePatternNode, DFPatternNode)
static void RegisterReflection()
Definition: dataflow_pattern.h:580
Managed reference to TuplePatternNode.
Definition: dataflow_pattern.h:591
TuplePattern(tvm::ffi::Array< DFPattern > fields)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TuplePattern, DFPattern, TuplePatternNode)
Pattern for matching a certain type.
Definition: dataflow_pattern.h:776
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.TypePattern", TypePatternNode, DFPatternNode)
Type ty
Definition: dataflow_pattern.h:779
DFPattern pattern
Definition: dataflow_pattern.h:778
static void RegisterReflection()
Definition: dataflow_pattern.h:781
Definition: dataflow_pattern.h:790
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TypePattern, DFPattern, TypePatternNode)
TypePattern(DFPattern pattern, Type ty)
A pattern to match multiple expressions unorderedly.
Definition: dataflow_pattern.h:601
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.UnorderedTuplePattern", UnorderedTuplePatternNode, DFPatternNode)
tvm::ffi::Array< DFPattern > fields
Definition: dataflow_pattern.h:603
static void RegisterReflection()
Definition: dataflow_pattern.h:605
Managed reference to UnorderedTuplePatternNode.
Definition: dataflow_pattern.h:618
UnorderedTuplePattern(tvm::ffi::Array< DFPattern > fields)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(UnorderedTuplePattern, DFPattern, UnorderedTuplePatternNode)
A Pattern to Match a Relax Variable.
Definition: dataflow_pattern.h:369
static constexpr const uint32_t _type_child_slots
Definition: dataflow_pattern.h:379
ffi::String name
Definition: dataflow_pattern.h:371
const ffi::String & name_hint() const
Definition: dataflow_pattern.h:372
TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.VarPattern", VarPatternNode, DFPatternNode)
static void RegisterReflection()
Definition: dataflow_pattern.h:374
Managed reference to a VarPattern.
Definition: dataflow_pattern.h:387
VarPattern(ffi::String name_hint)
Create a pattern matching by variable name.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(VarPattern, DFPattern, VarPatternNode)
Wildcard Pattern is a pattern that can match anything.
Definition: dataflow_pattern.h:741
static void RegisterReflection()
Definition: dataflow_pattern.h:743
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.WildcardPattern", WildcardPatternNode, DFPatternNode)
Managed reference to WildcardPatternNode.
Definition: dataflow_pattern.h:755
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(WildcardPattern, DFPattern, WildcardPatternNode)
WildcardPattern(ffi::ObjectPtr< WildcardPatternNode > data)
Definition: dataflow_pattern.h:758
Base expr nodes in TVM.
PatternSeq operator^(const PatternSeq &lhs, const PatternSeq &rhs)
Syntax sugar of UsedBy(lhs, rhs, -1).
ExprPattern IsOp(const ffi::String &op_name)
Syntatic Sugar for creating a ExprPattern base on an Op.
DFPattern IsTuple(const ffi::Array< DFPattern > &fields, bool unordered=false)
Syntatic Sugar for creating TuplePattern or UnorderedTuplePattern (unordered=true)
CallPattern IsCallTIR(const ffi::String &name, ffi::Optional< TuplePattern > args=std::nullopt)
Syntatic Sugar for call_tir (return a tensor)
CallPattern IsCallDPSPacked(const ffi::String &name, ffi::Optional< TuplePattern > args=std::nullopt)
Syntatic Sugar for call_dps_packed (return a tensor)
WildcardPattern Wildcard()
Syntatic Sugar for creating a WildcardPattern.
VarPattern IsVar(const ffi::String &name)
Syntatic Sugar for creating a VarPattern with a name.
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.
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:2010
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
Relax types, including the richer dependent Relax type nodes.
Constraint of a DFPattern edge (producer -> consumer) in graph-level matching.
Definition: dataflow_pattern.h:137
int index
Definition: dataflow_pattern.h:143
bool operator==(const PairCons &other) const
Definition: dataflow_pattern.h:153
enum tvm::relax::PairCons::Type type
Type
Constraint types of the edge.
Definition: dataflow_pattern.h:139
@ kOnlyUsedBy
Definition: dataflow_pattern.h:141
@ kUsedBy
Definition: dataflow_pattern.h:140
PairCons(Type t, int index=-1)
Construct a new PairCons object.
Definition: dataflow_pattern.h:151
RAII wrapper function to enter and exit a context object similar to python's with syntax.