tvm
block_builder.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_BLOCK_BUILDER_H_
25 #define TVM_RELAX_BLOCK_BUILDER_H_
26 
27 #include <tvm/arith/analyzer.h>
28 #include <tvm/ir/name_supply.h>
29 #include <tvm/relax/expr.h>
30 #include <tvm/relax/utils.h>
31 #include <tvm/runtime/object.h>
32 
33 namespace tvm {
34 namespace relax {
35 
65 class BlockBuilderNode : public Object {
66  public:
67  //-------------------------------
68  // Global Context management
69  //-------------------------------
75  virtual NameSupply name_supply() = 0;
76 
83  virtual IRModule GetContextIRModule() const = 0;
84 
96  virtual IRModule Finalize() = 0;
97 
106  virtual GlobalVar AddFunction(const BaseFunc& func, String func_name_hint) = 0;
107 
113  virtual void UpdateFunction(const GlobalVar& gv, BaseFunc function) = 0;
114 
119  [[noreturn]] virtual void ReportFatal(const Diagnostic& diagnostic) = 0;
120 
121  //-------------------------------
122  // Scope management
123  //-------------------------------
130  virtual Optional<Expr> LookupBinding(const Var& var) = 0;
131 
146  virtual void BeginScope(Optional<Array<Var>> params) = 0;
147 
161  virtual void BeginInnerScope() = 0;
162 
175  virtual void AddDefinitionToScope(Var var) = 0;
176 
178  virtual void EndScope() = 0;
179 
181  virtual void BeginDataflowBlock() = 0;
182 
184  virtual void BeginBindingBlock() = 0;
189  virtual BindingBlock EndBlock() = 0;
190 
195  virtual bool CurrentBlockIsDataFlow() = 0;
196 
206  virtual Var Emit(Expr expr, String name_hint = "") = 0;
207 
215  virtual Var EmitMatchCast(Expr value, StructInfo struct_info, String name_hint = "") = 0;
216 
223  virtual Var EmitOutput(Expr output, String name_hint = "") = 0;
224 
232  virtual void EmitNormalized(Binding normalized_binding) = 0;
233 
242  virtual Expr Normalize(const Expr& expr) = 0;
243 
251  virtual Expr NormalizeArgument(const Expr& expr) = 0;
252 
257  virtual arith::Analyzer* GetAnalyzer() = 0;
258 
259  static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
260  static constexpr const char* _type_key = "relax.BlockBuilder";
262 };
263 
264 class BlockBuilder : public ObjectRef {
265  public:
277  TVM_DLL static BlockBuilder Create(Optional<IRModule> ctx_mod);
278 
303  explicit DisableOperatorSpecificNormalizationForTVMScript() = default; // NOLINT(*)
304  };
318  TVM_DLL static BlockBuilder Create(Optional<IRModule> ctx_mod,
320 
322 };
323 
324 } // namespace relax
325 } // namespace tvm
326 
327 #endif // TVM_RELAX_BLOCK_BUILDER_H_
Algebra expression simplifications.
Managed reference to BaseFuncNode.
Definition: function.h:230
Definition: diagnostic.h:86
Managed reference to GlobalVarNode.
Definition: expr.h:487
Managed reference class to IRModuleNode.
Definition: module.h:366
Managed reference class to NameSupplyNode.
Definition: name_supply.h:112
Managed reference to RelayExprNode.
Definition: expr.h:442
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:629
Definition: expr.h:784
Definition: expr.h:681
A builder to build Relax binding blocks.
Definition: block_builder.h:65
virtual NameSupply name_supply()=0
Get the name supply for generating unique names.
virtual void BeginDataflowBlock()=0
Begin to build a DataflowBlock.
virtual IRModule GetContextIRModule() const =0
Get the context IRModule in this builder.
virtual void BeginInnerScope()=0
Begin a new scope, which inherits visible parameters from its parent scope.
virtual void EmitNormalized(Binding normalized_binding)=0
Emit a binding that is already normalized.
virtual void ReportFatal(const Diagnostic &diagnostic)=0
Report an error during transformation construction.
static constexpr const char * _type_key
Definition: block_builder.h:260
virtual IRModule Finalize()=0
Finalize the building process and return the result IRModule. Possibly rename GlobalVars in the IRMod...
virtual Expr Normalize(const Expr &expr)=0
Convert an expression to normal form, and try to eagerly infer types and shapes.
virtual void BeginScope(Optional< Array< Var >> params)=0
Begin a new scope, with optional parameters that are visible within the scope.
virtual void EndScope()=0
End the previously defined scope.
virtual void UpdateFunction(const GlobalVar &gv, BaseFunc function)=0
Update a Relax function or a TIR PrimFunc in the internal context module.
virtual Var EmitMatchCast(Expr value, StructInfo struct_info, String name_hint="")=0
Emit a MatchCast.
virtual arith::Analyzer * GetAnalyzer()=0
Get the analyzer of the BlockBuilder.
virtual void AddDefinitionToScope(Var var)=0
Append a definition to the current scope.
virtual Var EmitOutput(Expr output, String name_hint="")=0
Generate an output for the current dataflow block.
TVM_DECLARE_BASE_OBJECT_INFO(BlockBuilderNode, Object)
virtual bool CurrentBlockIsDataFlow()=0
Check if the block being built is DataflowBlock or not.
static constexpr const uint32_t _type_index
Definition: block_builder.h:259
virtual GlobalVar AddFunction(const BaseFunc &func, String func_name_hint)=0
Add a Relax function or a TIR PrimFunc to internal context module.
virtual BindingBlock EndBlock()=0
End building a BindingBlock.
virtual void BeginBindingBlock()=0
Begin to build a BindingBlock.
virtual Optional< Expr > LookupBinding(const Var &var)=0
Lookup the binding value that var binds to in the current emitted sequences.
virtual Var Emit(Expr expr, String name_hint="")=0
Emits an Expr, and returns the variable it is bound to.
virtual Expr NormalizeArgument(const Expr &expr)=0
Normalize argument to a call or another IRNode.
Definition: block_builder.h:264
static BlockBuilder Create(Optional< IRModule > ctx_mod, DisableOperatorSpecificNormalizationForTVMScript tag)
Create a BlockBuilder.
static BlockBuilder Create(Optional< IRModule > ctx_mod)
Create a BlockBuilder.
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(BlockBuilder, ObjectRef, BlockBuilderNode)
Managed reference to StructInfoNode.
Definition: expr.h:129
Definition: expr.h:422
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
NameSupply that can be used to generate unique variable names.
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
A managed object in the TVM runtime.
Utility classes and functions for working with the Relax IR.
A marker struct to disable FNormalize.
Definition: block_builder.h:302
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:84