tvm
struct_info_functor.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_STRUCT_INFO_FUNCTOR_H_
25 #define TVM_RELAX_STRUCT_INFO_FUNCTOR_H_
26 
27 #include <tvm/node/functor.h>
29 #include <tvm/relax/struct_info.h>
30 
31 #include <utility>
32 
33 namespace tvm {
34 namespace relax {
35 
36 template <typename FStructInfo>
38 
39 // functions to be overriden.
40 #define STRUCT_INFO_FUNCTOR_DEFAULT \
41  { \
42  return VisitStructInfoDefault_(op, std::forward<Args>(args)...); \
43  }
44 
45 #define TVM_STRUCT_INFO_FUNCTOR_DISPATCH(OP) \
46  vtable.template set_dispatch<OP>([](const ObjectRef& n, TSelf* self, Args... args) { \
47  return self->VisitStructInfo_(static_cast<const OP*>(n.get()), std::forward<Args>(args)...); \
48  });
49 
50 template <typename R, typename... Args>
51 class StructInfoFunctor<R(const StructInfo& n, Args...)> {
52  private:
53  using TSelf = StructInfoFunctor<R(const StructInfo& n, Args...)>;
54  using FStructInfo = tvm::NodeFunctor<R(const ObjectRef& n, TSelf* self, Args...)>;
55 
56  public:
58  using result_type = R;
60  virtual ~StructInfoFunctor() {}
67  R operator()(const StructInfo& n, Args... args) {
68  return VisitStructInfo(n, std::forward<Args>(args)...);
69  }
76  virtual R VisitStructInfo(const StructInfo& n, Args... args) {
77  TVM_FFI_ICHECK(n.defined());
78  static FStructInfo vtable = InitVTable();
79  return vtable(n, this, std::forward<Args>(args)...);
80  }
81  // Functions that can be overriden by subclass
83  Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
84  virtual R VisitStructInfo_(const PrimStructInfoNode* op,
85  Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
87  Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
89  Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
91  Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
93  Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
94  virtual R VisitStructInfo_(const FuncStructInfoNode* op,
95  Args... args) STRUCT_INFO_FUNCTOR_DEFAULT;
96  virtual R VisitStructInfoDefault_(const Object* op, Args...) {
97  TVM_FFI_THROW(InternalError) << "Do not have a default for " << op->GetTypeKey();
98  throw; // unreachable, written to stop compiler warning
99  }
100 
101  private:
102  // initialize the vtable.
103  static FStructInfo InitVTable() {
104  FStructInfo vtable;
105  // Set dispatch
113  vtable.Finalize();
114  return vtable;
115  }
116 };
117 
118 #undef TVM_STRUCT_INFO_FUNCTOR_DISPATCH
119 
123 class TVM_DLL StructInfoVisitor : public StructInfoFunctor<void(const StructInfo& n)> {
124  public:
125  void VisitStructInfo_(const ObjectStructInfoNode* op) override;
126  void VisitStructInfo_(const PrimStructInfoNode* op) override;
127  void VisitStructInfo_(const ShapeStructInfoNode* op) override;
128  void VisitStructInfo_(const TensorStructInfoNode* op) override;
130  void VisitStructInfo_(const TupleStructInfoNode* op) override;
131  void VisitStructInfo_(const FuncStructInfoNode* op) override;
132 
133  protected:
134  // two functions to override when visit expr fields in struct info.
135  virtual void VisitStructInfoExprField(const Expr& expr) {}
136  virtual void VisitStructInfoExprField(const PrimExpr& expr) {}
137 };
138 
142 class TVM_DLL StructInfoMutator : public StructInfoFunctor<StructInfo(const StructInfo& n)> {
143  public:
151 
152  protected:
153  // two functions to override when visit expr fields in struct info.
154  virtual Expr VisitStructInfoExprField(const Expr& expr) { return expr; }
155  virtual PrimExpr VisitStructInfoExprField(const PrimExpr& expr) { return expr; }
156 };
157 
158 } // namespace relax
159 } // namespace tvm
160 #endif // TVM_RELAX_STRUCT_INFO_FUNCTOR_H_
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:65
Reference to PrimExprNode.
Definition: expr.h:126
Managed reference to RelaxExprNode.
Definition: expr.h:441
Structure information about function.
Definition: struct_info.h:263
Opaque object.
Definition: struct_info.h:39
Primitive value.
Definition: struct_info.h:62
StructInfo of shape value.
Definition: struct_info.h:97
virtual R VisitStructInfo_(const FuncStructInfoNode *op, Args... args)
Definition: struct_info_functor.h:94
virtual R VisitStructInfo_(const TensorStructInfoNode *op, Args... args)
Definition: struct_info_functor.h:88
virtual R VisitStructInfo_(const PrimStructInfoNode *op, Args... args)
Definition: struct_info_functor.h:84
virtual R VisitStructInfoDefault_(const Object *op, Args...)
Definition: struct_info_functor.h:96
virtual R VisitStructInfo_(const ShapeStructInfoNode *op, Args... args)
Definition: struct_info_functor.h:86
virtual R VisitStructInfo(const StructInfo &n, Args... args)
The functor call.
Definition: struct_info_functor.h:76
virtual R VisitStructInfo_(const TupleStructInfoNode *op, Args... args)
Definition: struct_info_functor.h:92
virtual R VisitStructInfo_(const ObjectStructInfoNode *op, Args... args)
Definition: struct_info_functor.h:82
virtual ~StructInfoFunctor()
virtual destructor
Definition: struct_info_functor.h:60
virtual R VisitStructInfo_(const distributed::DTensorStructInfoNode *op, Args... args)
Definition: struct_info_functor.h:90
R result_type
the result type of this functor
Definition: struct_info_functor.h:58
R operator()(const StructInfo &n, Args... args)
Same as call.
Definition: struct_info_functor.h:67
Definition: struct_info_functor.h:37
StructInfoMutator that mutates struct info.
Definition: struct_info_functor.h:142
StructInfo VisitStructInfo_(const TupleStructInfoNode *op) override
StructInfo VisitStructInfo_(const FuncStructInfoNode *op) override
StructInfo VisitStructInfo_(const ObjectStructInfoNode *op) override
StructInfo VisitStructInfo_(const ShapeStructInfoNode *op) override
StructInfo VisitStructInfo_(const PrimStructInfoNode *op) override
virtual Expr VisitStructInfoExprField(const Expr &expr)
Definition: struct_info_functor.h:154
StructInfo VisitStructInfo_(const distributed::DTensorStructInfoNode *op) override
virtual PrimExpr VisitStructInfoExprField(const PrimExpr &expr)
Definition: struct_info_functor.h:155
StructInfo VisitStructInfo_(const TensorStructInfoNode *op) override
A struct info visitor.
Definition: struct_info_functor.h:123
void VisitStructInfo_(const FuncStructInfoNode *op) override
virtual void VisitStructInfoExprField(const PrimExpr &expr)
Definition: struct_info_functor.h:136
void VisitStructInfo_(const TupleStructInfoNode *op) override
void VisitStructInfo_(const ObjectStructInfoNode *op) override
virtual void VisitStructInfoExprField(const Expr &expr)
Definition: struct_info_functor.h:135
void VisitStructInfo_(const PrimStructInfoNode *op) override
void VisitStructInfo_(const distributed::DTensorStructInfoNode *op) override
void VisitStructInfo_(const ShapeStructInfoNode *op) override
void VisitStructInfo_(const TensorStructInfoNode *op) override
Managed reference to StructInfoNode.
Definition: expr.h:132
StructInfo of Tensor.
Definition: struct_info.h:144
StructInfo of Tuple.
Definition: struct_info.h:221
StructInfo of DTensor (Distributed Tensor).
Definition: struct_info.h:116
Struct info for DTensor (Distributed Tensor)
Defines the Functor data structures.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
#define STRUCT_INFO_FUNCTOR_DEFAULT
Definition: struct_info_functor.h:40
#define TVM_STRUCT_INFO_FUNCTOR_DISPATCH(OP)
Definition: struct_info_functor.h:45