tvm
Loading...
Searching...
No Matches
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/base.h>
28#include <tvm/ffi/container/array.h>
29#include <tvm/ffi/optional.h>
30#include <tvm/ffi/reflection/registry.h>
31#include <tvm/ir/expr.h>
32#include <tvm/ir/with_context.h>
33#include <tvm/relax/expr.h>
34#include <tvm/relax/type.h>
35
36#include <cstdint>
37#include <functional>
38#include <map>
39#include <memory>
40#include <string>
41#include <tuple>
42#include <utility>
43#include <vector>
44
45namespace tvm {
46
47namespace sym {
48class AnalyzerObj;
49class Analyzer;
50} // namespace sym
51
52namespace relax {
53
54class PatternSeq;
55class CallPattern;
56class OrPattern;
57class AndPattern;
58class NotPattern;
59class ShapePattern;
60class TypePattern;
61class DataTypePattern;
62class AttrPattern;
63class SameShapeConstraint;
64
73TVM_DLL PatternSeq UsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index = -1);
75TVM_DLL PatternSeq operator^(const PatternSeq& lhs, const PatternSeq& rhs);
76
85TVM_DLL PatternSeq OnlyUsedBy(const PatternSeq& lhs, const PatternSeq& rhs, int index = -1);
87TVM_DLL PatternSeq operator>>(const PatternSeq& lhs, const PatternSeq& rhs);
88
93class DFPatternObj : public ffi::Object {
94 public:
95 static constexpr const uint32_t _type_child_slots = 21;
96 TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.DFPattern", DFPatternObj, ffi::Object);
97};
98
103class DFPattern : public ffi::ObjectRef {
104 public:
106 template <typename... Args>
107 CallPattern operator()(Args&&... args) const;
109 TVM_DLL CallPattern operator()(const std::vector<DFPattern>& args) const;
111 TVM_DLL OrPattern operator|(const DFPattern& other) const;
113 TVM_DLL AndPattern operator&(const DFPattern& other) const;
115 TVM_DLL NotPattern operator~() const;
117 TVM_DLL AttrPattern HasAttr(const ffi::Map<ffi::String, Any>& attrs) const;
119 TVM_DLL TypePattern HasType(const Type& ty) const;
121 TVM_DLL DataTypePattern HasDtype(DLDataType dtype) const;
123 TVM_DLL DataTypePattern HasDtype(const std::string& dtype) const;
125 TVM_DLL ShapePattern HasShape(const ffi::Array<PrimExpr>& shape) const;
127 TVM_DLL SameShapeConstraint HasSameShapeAs(const DFPattern& other) const;
129 TVM_DLL DFPattern dup() const;
130
132 TVM_DLL operator PatternSeq() const;
133
135};
136
138struct PairCons {
144 int index = -1;
152 TVM_DLL explicit PairCons(Type t, int index = -1) : type(t), index(index) {}
153
154 bool operator==(const PairCons& other) const {
155 return type == other.type && index == other.index;
156 }
157};
158
166class DFConstraintObj : public ffi::Object {
167 public:
169 virtual ffi::Array<DFPattern> GetDependentPatterns() const = 0;
170
198 virtual std::tuple<PrimExpr, bool> AsCondition(
199 std::function<ffi::Optional<Var>(const DFPatternObj*)> match_state) const = 0;
200
201 static constexpr const uint32_t _type_child_slots = 1;
202 TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.DFConstraint", DFConstraintObj, ffi::Object);
203};
204
205class DFConstraint : public ffi::ObjectRef {
206 public:
208};
209
214class PatternSeqObj final : public ffi::Object {
215 public:
216 tvm::ffi::Array<DFPattern> patterns;
217 std::vector<PairCons> pair_constraints;
219 static void RegisterReflection() {
220 namespace refl = tvm::ffi::reflection;
221 refl::ObjectDef<PatternSeqObj>().def_ro("patterns", &PatternSeqObj::patterns);
222 }
223 TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.PatternSeq", PatternSeqObj, ffi::Object);
224};
225
230class PatternSeq final : public ffi::ObjectRef {
231 public:
232 TVM_DLL explicit PatternSeq(DFPattern init_pattern);
233 TVM_DLL explicit PatternSeq(tvm::ffi::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
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
252class PatternContextObj : public ffi::Object {
253 public:
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 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.PatternContext", PatternContextObj, ffi::Object);
271};
272
277class PatternContext : public ffi::ObjectRef {
278 public:
279 explicit PatternContext(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
280 static constexpr bool _type_is_nullable = false;
281 TVM_DLL explicit PatternContext(bool incremental = false);
282
284 TVM_FFI_ICHECK(get() != nullptr);
285 return static_cast<const PatternContextObj*>(get());
286 }
287
289 TVM_FFI_ICHECK(get() != nullptr);
290 return static_cast<PatternContextObj*>(get_mutable());
291 }
292
300 void add_constraint(DFPattern producer, DFPattern consumer, PairCons cons) {
301 auto& pairs = (*this)->edge_constraints[producer];
302 auto it = std::find_if(pairs.begin(), pairs.end(),
303 [consumer](auto p) { return p.first == consumer; });
304 if (it == pairs.end()) {
305 pairs.emplace_back(consumer, std::vector{cons});
306 } else {
307 auto& vec = it->second;
308 TVM_FFI_ICHECK(std::find(vec.cbegin(), vec.cend(), cons) == vec.cend())
309 << "Constraint already exists";
310 vec.push_back(cons);
311 }
312
313 auto& patterns = (*this)->src_ordered;
314 if (std::find(patterns.begin(), patterns.end(), producer) == patterns.end()) {
315 patterns.push_back(producer);
316 }
317 }
318
324 void add_constraint(DFConstraint constraint) {
325 (*this)->validation_constraints.push_back(constraint);
326 }
327
329 TVM_DLL static ffi::Optional<PatternContext> Current();
330
332 TVM_DLL void EnterWithScope() const;
334 TVM_DLL void ExitWithScope() const;
335
336 private:
337 friend class With<PatternContext>;
338};
339
345 public:
346 explicit ExprPatternObj(Expr expr) : expr(std::move(expr)) {}
347 explicit ExprPatternObj(ffi::UnsafeInit) : expr(ffi::UnsafeInit{}) {}
348
351 static void RegisterReflection() {
352 namespace refl = tvm::ffi::reflection;
353 refl::ObjectDef<ExprPatternObj>().def_ro("expr", &ExprPatternObj::expr);
354 }
356};
357
367
374 public:
375 ffi::String name;
376 const ffi::String& name_hint() const { return name; }
377
378 static void RegisterReflection() {
379 namespace refl = tvm::ffi::reflection;
380 refl::ObjectDef<VarPatternObj>().def_ro("name", &VarPatternObj::name);
381 }
382
383 static constexpr const uint32_t _type_child_slots = 1;
385};
386
401
407 public:
408 static void RegisterReflection() {
409 namespace refl = tvm::ffi::reflection;
410 refl::ObjectDef<DataflowVarPatternObj>();
411 }
414};
415
427
433 public:
436};
437
447
453 public:
454 static void RegisterReflection() {
455 namespace refl = tvm::ffi::reflection;
456 refl::ObjectDef<ConstantPatternObj>();
457 }
459};
460
466 public:
467 explicit ConstantPattern(ffi::UnsafeInit tag, ffi::ObjectPtr<ConstantPatternObj> n)
468 : DFPattern(tag) {
469 data_ = std::move(n);
470 }
472};
473
479 public:
480 explicit CallPatternObj(DFPattern op) : op(std::move(op)) {}
481 explicit CallPatternObj(ffi::UnsafeInit) : op(ffi::UnsafeInit{}) {}
482
489 tvm::ffi::Array<DFPattern> args;
498 // Todo(relax-team): Dataflow pattern for Type, and match ty_args
499
500 static void RegisterReflection() {
501 namespace refl = tvm::ffi::reflection;
502 refl::ObjectDef<CallPatternObj>()
503 .def_ro("op", &CallPatternObj::op)
504 .def_ro("args", &CallPatternObj::args);
505 }
507};
508
509class CallPattern : public DFPattern {
510 public:
511 TVM_DLL CallPattern(DFPattern op, ffi::Array<DFPattern> args, bool varg_default_wildcard = false);
513};
514
521 public:
522 ffi::Array<PrimExpr> fields;
524 static void RegisterReflection() {
525 namespace refl = tvm::ffi::reflection;
526 refl::ObjectDef<PrimArrPatternObj>().def_ro("fields", &PrimArrPatternObj::fields);
527 }
529};
530
540
547 public:
548 explicit FunctionPatternObj(DFPattern body) : body(std::move(body)) {}
549 explicit FunctionPatternObj(ffi::UnsafeInit) : body(ffi::UnsafeInit{}) {}
550
551 tvm::ffi::Array<DFPattern> params;
560 static void RegisterReflection() {
561 namespace refl = tvm::ffi::reflection;
562 refl::ObjectDef<FunctionPatternObj>()
563 .def_ro("params", &FunctionPatternObj::params)
564 .def_ro("body", &FunctionPatternObj::body);
565 }
567};
568
574 public:
580 TVM_DLL FunctionPattern(tvm::ffi::Array<DFPattern> params, DFPattern body);
581
583};
584
590 public:
591 tvm::ffi::Array<DFPattern> fields;
593 static void RegisterReflection() {
594 namespace refl = tvm::ffi::reflection;
595 refl::ObjectDef<TuplePatternObj>().def_ro("fields", &TuplePatternObj::fields);
596 }
598};
599
604class TuplePattern : public DFPattern {
605 public:
606 TVM_DLL explicit TuplePattern(tvm::ffi::Array<DFPattern> fields);
608};
609
615 public:
616 tvm::ffi::Array<DFPattern> fields;
618 static void RegisterReflection() {
619 namespace refl = tvm::ffi::reflection;
620 refl::ObjectDef<UnorderedTuplePatternObj>().def_ro("fields", &UnorderedTuplePatternObj::fields);
621 }
624};
625
636
643 public:
645 explicit TupleGetItemPatternObj(ffi::UnsafeInit) : tuple(ffi::UnsafeInit{}) {}
646
648 int index;
650 static void RegisterReflection() {
651 namespace refl = tvm::ffi::reflection;
652 refl::ObjectDef<TupleGetItemPatternObj>()
653 .def_ro("tuple", &TupleGetItemPatternObj::tuple)
654 .def_ro("index", &TupleGetItemPatternObj::index);
655 }
658};
659
670
676 public:
678 : left(std::move(left)), right(std::move(right)) {}
679 explicit AndPatternObj(ffi::UnsafeInit) : left(ffi::UnsafeInit{}), right(ffi::UnsafeInit{}) {}
680
684 static void RegisterReflection() {
685 namespace refl = tvm::ffi::reflection;
686 refl::ObjectDef<AndPatternObj>()
687 .def_ro("left", &AndPatternObj::left)
688 .def_ro("right", &AndPatternObj::right);
689 }
691};
692
702
708 public:
710 : left(std::move(left)), right(std::move(right)) {}
711 explicit OrPatternObj(ffi::UnsafeInit) : left(ffi::UnsafeInit{}), right(ffi::UnsafeInit{}) {}
712
716 static void RegisterReflection() {
717 namespace refl = tvm::ffi::reflection;
718 refl::ObjectDef<OrPatternObj>()
719 .def_ro("left", &OrPatternObj::left)
720 .def_ro("right", &OrPatternObj::right);
721 }
723};
724
734
740 public:
741 explicit NotPatternObj(DFPattern reject) : reject(std::move(reject)) {}
742 explicit NotPatternObj(ffi::UnsafeInit) : reject(ffi::UnsafeInit{}) {}
743
746 static void RegisterReflection() {
747 namespace refl = tvm::ffi::reflection;
748 refl::ObjectDef<NotPatternObj>().def_ro("reject", &NotPatternObj::reject);
749 }
751};
752
762
768 public:
769 static void RegisterReflection() {
770 namespace refl = tvm::ffi::reflection;
771 refl::ObjectDef<WildcardPatternObj>();
772 }
774};
775
781 public:
783 explicit WildcardPattern(ffi::ObjectPtr<WildcardPatternObj> data) : DFPattern(ffi::UnsafeInit{}) {
784 TVM_FFI_ICHECK(data != nullptr);
785 data_ = std::move(data);
786 }
787
788 // Declaring WildcardPattern declared as non-nullable avoids the
789 // default zero-parameter constructor for ffi::ObjectRef with `data_ =
790 // nullptr`. This allows a zero-parameter constructor to be
791 // declared here, to create a valid wildcard instance.
792
794};
795
801 public:
802 explicit TypePatternObj(DFPattern pattern) : pattern(std::move(pattern)) {}
803 explicit TypePatternObj(ffi::UnsafeInit) : pattern(ffi::UnsafeInit{}) {}
804
808 static void RegisterReflection() {
809 namespace refl = tvm::ffi::reflection;
810 refl::ObjectDef<TypePatternObj>()
811 .def_ro("pattern", &TypePatternObj::pattern)
812 .def_ro("ty", &TypePatternObj::ty);
813 }
815};
816
822
828 public:
830 explicit ShapePatternObj(ffi::UnsafeInit) : pattern(ffi::UnsafeInit{}) {}
831
833 ffi::Array<PrimExpr> shape;
835 static void RegisterReflection() {
836 namespace refl = tvm::ffi::reflection;
837 refl::ObjectDef<ShapePatternObj>()
838 .def_ro("pattern", &ShapePatternObj::pattern)
839 .def_ro("shape", &ShapePatternObj::shape);
840 }
842};
843
848class ShapePattern : public DFPattern {
849 public:
850 TVM_DLL ShapePattern(DFPattern pattern, ffi::Array<PrimExpr> type);
852};
853
859 public:
860 ffi::Array<DFPattern> args;
862 ffi::Array<DFPattern> GetDependentPatterns() const override { return args; }
863
864 std::tuple<PrimExpr, bool> AsCondition(
865 std::function<ffi::Optional<Var>(const DFPatternObj*)> match_state) const override;
866
867 static void RegisterReflection() {
868 namespace refl = tvm::ffi::reflection;
869 refl::ObjectDef<SameShapeConstraintObj>().def_ro("args", &SameShapeConstraintObj::args);
870 }
873};
874
885
891 public:
893 explicit DataTypePatternObj(ffi::UnsafeInit) : pattern(ffi::UnsafeInit{}) {}
894
896 DLDataType dtype;
898 static void RegisterReflection() {
899 namespace refl = tvm::ffi::reflection;
900 refl::ObjectDef<DataTypePatternObj>()
901 .def_ro("pattern", &DataTypePatternObj::pattern)
902 .def_ro("dtype", &DataTypePatternObj::dtype);
903 }
905};
906
916
922 public:
923 explicit AttrPatternObj(DFPattern pattern) : pattern(std::move(pattern)) {}
924 explicit AttrPatternObj(ffi::UnsafeInit) : pattern(ffi::UnsafeInit{}) {}
925
929 static void RegisterReflection() {
930 namespace refl = tvm::ffi::reflection;
931 refl::ObjectDef<AttrPatternObj>()
932 .def_ro("pattern", &AttrPatternObj::pattern)
933 .def_ro("attrs", &AttrPatternObj::attrs);
934 }
936};
937
947
954 public:
955 ffi::String global_symbol_;
958 const ffi::String& global_symbol() const { return global_symbol_; }
959
960 static void RegisterReflection() {
961 namespace refl = tvm::ffi::reflection;
962 refl::ObjectDef<ExternFuncPatternObj>().def_ro("global_symbol",
964 }
967};
968
978
980VarPattern IsVar(const ffi::String& name);
988ExprPattern IsOp(const ffi::String& op_name);
990// Todo(relax-team): Dataflow pattern for Type, and match out_ty
991CallPattern IsCallTIR(const ffi::String& name, ffi::Optional<TuplePattern> args = std::nullopt);
993CallPattern IsCallTIR(const ffi::String& name, TuplePattern var_args);
995CallPattern IsCallDPSPacked(const ffi::String& name,
996 ffi::Optional<TuplePattern> args = std::nullopt);
998CallPattern IsCallDPSPacked(const ffi::String& name, TuplePattern var_args);
1000DFPattern IsTuple(const ffi::Array<DFPattern>& fields, bool unordered = false);
1002TupleGetItemPattern IsTupleGetItem(const DFPattern tuple, int index = -1);
1003
1005template <typename... Args>
1006CallPattern DFPattern::operator()(Args&&... args) const {
1007 return CallPattern(ffi::GetRef<DFPattern>(this->get()),
1008 ffi::Array<DFPattern>({std::forward<Args>(args)...}));
1009}
1010
1011} // namespace relax
1012} // namespace tvm
1013#endif // TVM_RELAX_DATAFLOW_PATTERN_H_
Base expr nodes in TVM.
Managed reference to DictAttrsObj.
Definition attrs.h:108
Managed reference to ExprObj.
Definition expr.h:101
Managed reference to TypeObj.
Definition type.h:75
static TVM_TIRX_DLL Type Missing()
Construct a MissingType for type information not yet populated.
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:58
Match a conjunction of other patterns.
Definition dataflow_pattern.h:675
AndPatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:679
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.AndPattern", AndPatternObj, DFPatternObj)
static void RegisterReflection()
Definition dataflow_pattern.h:684
DFPattern right
Definition dataflow_pattern.h:682
AndPatternObj(DFPattern left, DFPattern right)
Definition dataflow_pattern.h:677
DFPattern left
Definition dataflow_pattern.h:681
Managed reference to AndPatternObj.
Definition dataflow_pattern.h:697
AndPattern(DFPattern lhs, DFPattern rhs)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AndPattern, DFPattern, AndPatternObj)
A pattern that asserting a root pattern has certain attributes.
Definition dataflow_pattern.h:921
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.AttrPattern", AttrPatternObj, DFPatternObj)
AttrPatternObj(DFPattern pattern)
Definition dataflow_pattern.h:923
DictAttrs attrs
Definition dataflow_pattern.h:927
static void RegisterReflection()
Definition dataflow_pattern.h:929
DFPattern pattern
Definition dataflow_pattern.h:926
AttrPatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:924
Managed reference to AttrPatternObj.
Definition dataflow_pattern.h:942
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AttrPattern, DFPattern, AttrPatternObj)
AttrPattern(DFPattern pattern, DictAttrs attrs)
A pattern to match a callable node in Relax.
Definition dataflow_pattern.h:478
static void RegisterReflection()
Definition dataflow_pattern.h:500
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.CallPattern", CallPatternObj, DFPatternObj)
bool varg_default_wildcard
Definition dataflow_pattern.h:496
tvm::ffi::Array< DFPattern > args
Definition dataflow_pattern.h:489
CallPatternObj(DFPattern op)
Definition dataflow_pattern.h:480
CallPatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:481
DFPattern op
Definition dataflow_pattern.h:488
Definition dataflow_pattern.h:509
CallPattern(DFPattern op, ffi::Array< DFPattern > args, bool varg_default_wildcard=false)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(CallPattern, DFPattern, CallPatternObj)
A Pattern to Match a Relax GenericConst.
Definition dataflow_pattern.h:452
static void RegisterReflection()
Definition dataflow_pattern.h:454
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.ConstantPattern", ConstantPatternObj, DFPatternObj)
Managed reference to a ConstantPattern.
Definition dataflow_pattern.h:465
ConstantPattern(ffi::UnsafeInit tag, ffi::ObjectPtr< ConstantPatternObj > n)
Definition dataflow_pattern.h:467
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ConstantPattern, DFPattern, ConstantPatternObj)
Additional constraints on the graph.
Definition dataflow_pattern.h:166
static constexpr const uint32_t _type_child_slots
Definition dataflow_pattern.h:201
TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.DFConstraint", DFConstraintObj, ffi::Object)
virtual std::tuple< PrimExpr, bool > AsCondition(std::function< ffi::Optional< Var >(const DFPatternObj *)> 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.
Definition dataflow_pattern.h:205
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DFConstraint, ffi::ObjectRef, DFConstraintObj)
Base type of all dataflow patterns.
Definition dataflow_pattern.h:93
static constexpr const uint32_t _type_child_slots
Definition dataflow_pattern.h:95
TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.DFPattern", DFPatternObj, ffi::Object)
Managed reference to dataflow patterns.
Definition dataflow_pattern.h:103
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.
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:1006
TypePattern HasType(const Type &ty) const
Syntatic Sugar for creating a TypePattern.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DFPattern, ffi::ObjectRef, DFPatternObj)
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:890
static void RegisterReflection()
Definition dataflow_pattern.h:898
DFPattern pattern
Definition dataflow_pattern.h:895
DataTypePatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:893
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.DataTypePattern", DataTypePatternObj, DFPatternObj)
DataTypePatternObj(DFPattern pattern)
Definition dataflow_pattern.h:892
DLDataType dtype
Definition dataflow_pattern.h:896
Managed reference to DataTypePatternObj.
Definition dataflow_pattern.h:911
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DataTypePattern, DFPattern, DataTypePatternObj)
DataTypePattern(DFPattern pattern, DLDataType dtype)
A Pattern to Match a Relax Dataflow Variable.
Definition dataflow_pattern.h:406
static void RegisterReflection()
Definition dataflow_pattern.h:408
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.DataflowVarPattern", DataflowVarPatternObj, VarPatternObj)
Managed reference to a DataflowVarPattern.
Definition dataflow_pattern.h:420
DataflowVarPattern(ffi::String name_hint)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DataflowVarPattern, DFPattern, DataflowVarPatternObj)
Pattern for Relax Expression.
Definition dataflow_pattern.h:344
ExprPatternObj(Expr expr)
Definition dataflow_pattern.h:346
ExprPatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:347
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.ExprPattern", ExprPatternObj, DFPatternObj)
Expr expr
Definition dataflow_pattern.h:349
static void RegisterReflection()
Definition dataflow_pattern.h:351
Managed reference to an ExprPattern.
Definition dataflow_pattern.h:362
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ExprPattern, DFPattern, ExprPatternObj)
A pattern of external function.
Definition dataflow_pattern.h:953
const ffi::String & global_symbol() const
The external function name.
Definition dataflow_pattern.h:958
ffi::String global_symbol_
Definition dataflow_pattern.h:955
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.ExternFuncPattern", ExternFuncPatternObj, DFPatternObj)
static void RegisterReflection()
Definition dataflow_pattern.h:960
Managed reference to ExternFuncPatternObj.
Definition dataflow_pattern.h:973
ExternFuncPattern(ffi::String global_symbol)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ExternFuncPattern, DFPattern, ExternFuncPatternObj)
A pattern to match a Relax Function.
Definition dataflow_pattern.h:546
tvm::ffi::Array< DFPattern > params
Definition dataflow_pattern.h:551
DFPattern body
Definition dataflow_pattern.h:558
FunctionPatternObj(DFPattern body)
Definition dataflow_pattern.h:548
static void RegisterReflection()
Definition dataflow_pattern.h:560
FunctionPatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:549
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.FunctionPattern", FunctionPatternObj, DFPatternObj)
Managed reference to FunctionPatternObj.
Definition dataflow_pattern.h:573
FunctionPattern(tvm::ffi::Array< DFPattern > params, DFPattern body)
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(FunctionPattern, DFPattern, FunctionPatternObj)
A Pattern to Match a Relax Global Variable.
Definition dataflow_pattern.h:432
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.GlobalVarPattern", GlobalVarPatternObj, DFPatternObj)
Managed reference to a GlobalVarPattern.
Definition dataflow_pattern.h:442
GlobalVarPattern(ffi::String name_hint)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(GlobalVarPattern, DFPattern, GlobalVarPatternObj)
Pattern for rejecting a certain pattern.
Definition dataflow_pattern.h:739
DFPattern reject
Definition dataflow_pattern.h:744
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.NotPattern", NotPatternObj, DFPatternObj)
static void RegisterReflection()
Definition dataflow_pattern.h:746
NotPatternObj(DFPattern reject)
Definition dataflow_pattern.h:741
NotPatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:742
Managed reference to NotPatternObj.
Definition dataflow_pattern.h:757
NotPattern(DFPattern reject)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(NotPattern, DFPattern, NotPatternObj)
Match a disjunction of other patterns.
Definition dataflow_pattern.h:707
DFPattern right
Definition dataflow_pattern.h:714
OrPatternObj(DFPattern left, DFPattern right)
Definition dataflow_pattern.h:709
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.OrPattern", OrPatternObj, DFPatternObj)
OrPatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:711
static void RegisterReflection()
Definition dataflow_pattern.h:716
DFPattern left
Definition dataflow_pattern.h:713
Managed reference to OrPatternObj.
Definition dataflow_pattern.h:729
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(OrPattern, DFPattern, OrPatternObj)
OrPattern(DFPattern left, DFPattern right)
A context to manage the graph-level pattern matching.
Definition dataflow_pattern.h:252
std::vector< DFPattern > src_ordered
Definition dataflow_pattern.h:266
ExternUse
Constrainting matched graph with assertion to external uses.
Definition dataflow_pattern.h:255
@ kMay
Definition dataflow_pattern.h:256
@ kMustNot
Definition dataflow_pattern.h:257
enum tvm::relax::PatternContextObj::ExternUse allow_extern_use
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.PatternContext", PatternContextObj, ffi::Object)
std::map< DFPattern, std::vector< std::pair< DFPattern, std::vector< PairCons > > > > edge_constraints
Definition dataflow_pattern.h:262
std::vector< DFConstraint > validation_constraints
Definition dataflow_pattern.h:269
Managed reference to a pattern context.
Definition dataflow_pattern.h:277
void add_constraint(DFConstraint constraint)
Add a validation constraint.
Definition dataflow_pattern.h:324
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.
void add_constraint(DFPattern producer, DFPattern consumer, PairCons cons)
Build an edge constraint between two patterns (producer and consumer).
Definition dataflow_pattern.h:300
PatternContextObj * operator->()
Definition dataflow_pattern.h:288
const PatternContextObj * operator->() const
Definition dataflow_pattern.h:283
PatternContext(bool incremental=false)
static constexpr bool _type_is_nullable
Definition dataflow_pattern.h:280
PatternContext(ffi::UnsafeInit tag)
Definition dataflow_pattern.h:279
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:214
std::vector< PairCons > pair_constraints
Definition dataflow_pattern.h:217
static void RegisterReflection()
Definition dataflow_pattern.h:219
tvm::ffi::Array< DFPattern > patterns
Definition dataflow_pattern.h:216
TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.PatternSeq", PatternSeqObj, ffi::Object)
Managed reference to pattern sequences.
Definition dataflow_pattern.h:230
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.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PatternSeq, ffi::ObjectRef, PatternSeqObj)
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:520
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.PrimArrPattern", PrimArrPatternObj, DFPatternObj)
static void RegisterReflection()
Definition dataflow_pattern.h:524
ffi::Array< PrimExpr > fields
Definition dataflow_pattern.h:522
Managed reference to a PrimArrPattern.
Definition dataflow_pattern.h:535
PrimArrPattern(ffi::Array< PrimExpr > arr)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PrimArrPattern, DFPattern, PrimArrPatternObj)
A pattern that asserting multiple root patterns have the same shape.
Definition dataflow_pattern.h:858
std::tuple< PrimExpr, bool > AsCondition(std::function< ffi::Optional< Var >(const DFPatternObj *)> 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:862
ffi::Array< DFPattern > args
Definition dataflow_pattern.h:860
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.SameShapeConstraint", SameShapeConstraintObj, DFConstraintObj)
static void RegisterReflection()
Definition dataflow_pattern.h:867
Managed reference to SameShapePatternNode.
Definition dataflow_pattern.h:879
SameShapeConstraint(ffi::Array< DFPattern > args)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SameShapeConstraint, DFConstraint, SameShapeConstraintObj)
A pattern that asserting a root pattern has a certain shape.
Definition dataflow_pattern.h:827
ffi::Array< PrimExpr > shape
Definition dataflow_pattern.h:833
ShapePatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:830
ShapePatternObj(DFPattern pattern)
Definition dataflow_pattern.h:829
static void RegisterReflection()
Definition dataflow_pattern.h:835
DFPattern pattern
Definition dataflow_pattern.h:832
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.ShapePattern", ShapePatternObj, DFPatternObj)
Managed reference to ShapePatternObj.
Definition dataflow_pattern.h:848
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ShapePattern, DFPattern, ShapePatternObj)
ShapePattern(DFPattern pattern, ffi::Array< PrimExpr > type)
A pattern to match n'th indexing to a tuple.
Definition dataflow_pattern.h:642
TupleGetItemPatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:645
int index
Definition dataflow_pattern.h:648
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.TupleGetItemPattern", TupleGetItemPatternObj, DFPatternObj)
static void RegisterReflection()
Definition dataflow_pattern.h:650
TupleGetItemPatternObj(DFPattern tuple)
Definition dataflow_pattern.h:644
DFPattern tuple
Definition dataflow_pattern.h:647
Managed reference to TupleGetItemPatternObj.
Definition dataflow_pattern.h:664
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TupleGetItemPattern, DFPattern, TupleGetItemPatternObj)
TupleGetItemPattern(DFPattern tuple, int index)
Pattern to match a tuple of ordered expressions.
Definition dataflow_pattern.h:589
tvm::ffi::Array< DFPattern > fields
Definition dataflow_pattern.h:591
static void RegisterReflection()
Definition dataflow_pattern.h:593
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.TuplePattern", TuplePatternObj, DFPatternObj)
Managed reference to TuplePatternObj.
Definition dataflow_pattern.h:604
TuplePattern(tvm::ffi::Array< DFPattern > fields)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TuplePattern, DFPattern, TuplePatternObj)
Pattern for matching a certain type.
Definition dataflow_pattern.h:800
TypePatternObj(DFPattern pattern)
Definition dataflow_pattern.h:802
static void RegisterReflection()
Definition dataflow_pattern.h:808
TypePatternObj(ffi::UnsafeInit)
Definition dataflow_pattern.h:803
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.TypePattern", TypePatternObj, DFPatternObj)
Type ty
Definition dataflow_pattern.h:806
DFPattern pattern
Definition dataflow_pattern.h:805
Definition dataflow_pattern.h:817
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TypePattern, DFPattern, TypePatternObj)
TypePattern(DFPattern pattern, Type ty)
A pattern to match multiple expressions unorderedly.
Definition dataflow_pattern.h:614
static void RegisterReflection()
Definition dataflow_pattern.h:618
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.UnorderedTuplePattern", UnorderedTuplePatternObj, DFPatternObj)
tvm::ffi::Array< DFPattern > fields
Definition dataflow_pattern.h:616
Managed reference to UnorderedTuplePatternObj.
Definition dataflow_pattern.h:630
UnorderedTuplePattern(tvm::ffi::Array< DFPattern > fields)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(UnorderedTuplePattern, DFPattern, UnorderedTuplePatternObj)
A Pattern to Match a Relax Variable.
Definition dataflow_pattern.h:373
ffi::String name
Definition dataflow_pattern.h:375
static void RegisterReflection()
Definition dataflow_pattern.h:378
const ffi::String & name_hint() const
Definition dataflow_pattern.h:376
static constexpr const uint32_t _type_child_slots
Definition dataflow_pattern.h:383
TVM_FFI_DECLARE_OBJECT_INFO("relax.dpl.VarPattern", VarPatternObj, DFPatternObj)
Managed reference to a VarPattern.
Definition dataflow_pattern.h:391
VarPattern(ffi::String name_hint)
Create a pattern matching by variable name.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(VarPattern, DFPattern, VarPatternObj)
Wildcard Pattern is a pattern that can match anything.
Definition dataflow_pattern.h:767
static void RegisterReflection()
Definition dataflow_pattern.h:769
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.dpl.WildcardPattern", WildcardPatternObj, DFPatternObj)
Managed reference to WildcardPatternObj.
Definition dataflow_pattern.h:780
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(WildcardPattern, DFPattern, WildcardPatternObj)
WildcardPattern(ffi::ObjectPtr< WildcardPatternObj > data)
Definition dataflow_pattern.h:783
Coordinated symbolic analyses over an explicitly synchronized premise environment.
Definition analyzer.h:556
Managed reference to AnalyzerObj.
Definition analyzer.h:750
Parent compiler symbol visibility.
Relax types, including the richer dependent Relax type nodes.
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.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Constraint of a DFPattern edge (producer -> consumer) in graph-level matching.
Definition dataflow_pattern.h:138
int index
Definition dataflow_pattern.h:144
bool operator==(const PairCons &other) const
Definition dataflow_pattern.h:154
enum tvm::relax::PairCons::Type type
Type
Constraint types of the edge.
Definition dataflow_pattern.h:140
@ kOnlyUsedBy
Definition dataflow_pattern.h:142
@ kUsedBy
Definition dataflow_pattern.h:141
PairCons(Type t, int index=-1)
Construct a new PairCons object.
Definition dataflow_pattern.h:152
RAII wrapper function to enter and exit a context object similar to python's with syntax.