tvm
frame.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 #ifndef TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_
20 #define TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_
21 
23 #include <tvm/relax/expr.h>
27 
28 namespace tvm {
29 namespace script {
30 namespace ir_builder {
31 namespace relax {
32 
35  public:
37 
38  static constexpr const char* _type_key = "script.ir_builder.relax.RelaxFrame";
40 };
41 
42 class RelaxFrame : public IRBuilderFrame {
43  public:
45 
46  protected:
47  RelaxFrame() = default;
48 };
49 
54  public:
59 
62  v->Visit("binding_blocks", &binding_blocks);
63  v->Visit("output", &output);
64  }
65 
66  static constexpr const char* _type_key = "script.ir_builder.relax.SeqExprFrame";
68 
69  public:
70  void EnterWithScope() override;
71  void ExitWithScope() override;
72 };
73 
74 class SeqExprFrame : public RelaxFrame {
75  public:
77 };
78 
81  public:
108 
111  v->Visit("name", &name);
112  v->Visit("params", &params);
113  v->Visit("ret_struct_info", &ret_struct_info);
114  v->Visit("is_pure", &is_pure);
115  v->Visit("attrs", &attrs);
116  v->Visit("binding_blocks", &binding_blocks);
117  v->Visit("output", &output);
118  // `block_builder` is not visited.
119  }
120 
121  static constexpr const char* _type_key = "script.ir_builder.relax.FunctionFrame";
123 
124  public:
125  void EnterWithScope() final;
126  void ExitWithScope() final;
127 };
128 
129 class FunctionFrame : public SeqExprFrame {
130  public:
132 };
133 
136  public:
152 
155  v->Visit("is_dataflow", &is_dataflow);
156  v->Visit("emitted_vars", &emitted_vars);
157  v->Visit("output_vars", &output_vars);
158  // `block_ended` is not visited.
159  }
160 
161  static constexpr const char* _type_key = "script.ir_builder.relax.BlockFrame";
163 
164  public:
165  void EnterWithScope() final;
166  void ExitWithScope() final;
167 };
168 
169 class BlockFrame : public RelaxFrame {
170  public:
172 };
173 
179 class IfFrameNode : public RelaxFrameNode {
180  public:
191 
194  v->Visit("condition", &condition);
195  v->Visit("then_expr", &then_expr);
196  v->Visit("else_expr", &else_expr);
197  v->Visit("var", &var);
198  v->Visit("var_name", &var_name);
199  }
200 
201  static constexpr const char* _type_key = "script.ir_builder.relax.IfFrame";
203 
204  public:
209  void EnterWithScope() final;
214  void ExitWithScope() final;
215 };
216 
222 class IfFrame : public RelaxFrame {
223  public:
225 };
226 
233  public:
234  static constexpr const char* _type_key = "script.ir_builder.relax.ThenFrame";
236 
237  public:
242  void EnterWithScope() final;
247  void ExitWithScope() final;
248 };
249 
255 class ThenFrame : public SeqExprFrame {
256  public:
258 };
259 
266  public:
267  static constexpr const char* _type_key = "script.ir_builder.relax.ElseFrame";
269 
270  public:
275  void EnterWithScope() final;
280  void ExitWithScope() final;
281 };
282 
288 class ElseFrame : public SeqExprFrame {
289  public:
291 };
292 
293 } // namespace relax
294 } // namespace ir_builder
295 } // namespace script
296 } // namespace tvm
297 
298 #endif // TVM_SCRIPT_IR_BUILDER_RELAX_FRAME_H_
The utility for constructing Relax binding blocks.
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 RelayExprNode.
Definition: expr.h:442
Definition: block_builder.h:264
Definition: expr.h:422
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
void VisitAttrs(tvm::AttrVisitor *v)
Definition: base.h:69
Managed reference to an IRBuilderFrameNode.
Definition: base.h:100
The ir_builder frame for relax binding blocks.
Definition: frame.h:135
TVM_DECLARE_FINAL_OBJECT_INFO(BlockFrameNode, RelaxFrameNode)
void EnterWithScope() final
The method called when entering RAII scope.
bool is_dataflow
The flag that indicates whether the block is a dataflow block.
Definition: frame.h:138
Array< tvm::relax::Var > output_vars
The output vars of the dataflow block.
Definition: frame.h:151
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:153
Array< tvm::relax::Var > emitted_vars
The variables emitted in this block.
Definition: frame.h:140
bool block_ended
A boolean indicating if the dataflow block is ended of construction. If it is true,...
Definition: frame.h:146
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(BlockFrame, RelaxFrame, BlockFrameNode)
A frame that represents else.
Definition: frame.h:265
TVM_DECLARE_FINAL_OBJECT_INFO(ElseFrameNode, SeqExprFrameNode)
void EnterWithScope() final
The method called when entering RAII scope.
Managed reference to ElseFrameNode.
Definition: frame.h:288
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ElseFrame, SeqExprFrame, ElseFrameNode)
The ir_builder frame for the relax function.
Definition: frame.h:80
tvm::relax::BlockBuilder block_builder
The block builder to create Relax function.
Definition: frame.h:107
Optional< String > name
The function name.
Definition: frame.h:87
void EnterWithScope() final
The method called when entering RAII scope.
Map< String, ObjectRef > attrs
The function attributes.
Definition: frame.h:105
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:109
void ExitWithScope() final
The method called when exiting RAII scope.
Optional< Bool > is_private
Whether the function is annotated as private.
Definition: frame.h:103
static constexpr const char * _type_key
Definition: frame.h:121
Optional< tvm::relax::StructInfo > ret_struct_info
The function return struct info.
Definition: frame.h:99
Optional< Bool > is_pure
Whether the function is annotated as pure.
Definition: frame.h:101
TVM_DECLARE_FINAL_OBJECT_INFO(FunctionFrameNode, SeqExprFrameNode)
Array< tvm::relax::Var > params
The function params.
Definition: frame.h:89
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(FunctionFrame, SeqExprFrame, FunctionFrameNode)
A frame that represents if statement.
Definition: frame.h:179
void EnterWithScope() final
The method called when entering RAII scope.
TVM_DECLARE_FINAL_OBJECT_INFO(IfFrameNode, RelaxFrameNode)
tvm::relax::Var var
The Binding var.
Definition: frame.h:188
tvm::relax::Expr condition
The condition of the if statement.
Definition: frame.h:182
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:192
String var_name
The binding var name.
Definition: frame.h:190
Optional< tvm::relax::Expr > then_expr
The Bindings in the true branch.
Definition: frame.h:184
Optional< tvm::relax::Expr > else_expr
The Bindings in the false branch.
Definition: frame.h:186
Managed reference to IfFrameNode.
Definition: frame.h:222
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IfFrame, RelaxFrame, IfFrameNode)
The base ir_builder frame for the relax dialect.
Definition: frame.h:34
static constexpr const char * _type_key
Definition: frame.h:38
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:36
TVM_DECLARE_BASE_OBJECT_INFO(RelaxFrameNode, IRBuilderFrameNode)
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(RelaxFrame, IRBuilderFrame, RelaxFrameNode)
The base ir_builder frame for frames with SeqExpr i.e. Functions, If branches.
Definition: frame.h:53
TVM_DECLARE_BASE_OBJECT_INFO(SeqExprFrameNode, RelaxFrameNode)
void EnterWithScope() override
The method called when entering RAII scope.
void ExitWithScope() override
The method called when exiting RAII scope.
Array< tvm::relax::BindingBlock > binding_blocks
The binding blocks inside the frame.
Definition: frame.h:56
Optional< tvm::relax::Expr > output
The frame output expr. NullOpt when undefined.
Definition: frame.h:58
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:60
static constexpr const char * _type_key
Definition: frame.h:66
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(SeqExprFrame, RelaxFrame, SeqExprFrameNode)
A frame that represents then.
Definition: frame.h:232
void EnterWithScope() final
The method called when entering RAII scope.
TVM_DECLARE_FINAL_OBJECT_INFO(ThenFrameNode, SeqExprFrameNode)
Managed reference to ThenFrameNode.
Definition: frame.h:255
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ThenFrame, SeqExprFrame, ThenFrameNode)
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36