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>
29 #include <tvm/relax/expr.h>
30 #include <tvm/relax/utils.h>
31 #include <tvm/runtime/base.h>
32 
33 namespace tvm {
34 namespace relax {
35 
65 class BlockBuilderNode : public ffi::Object {
66  public:
67  //-------------------------------
68  // Global Context management
69  //-------------------------------
76 
83  virtual IRModule GetContextIRModule() const = 0;
84 
96  virtual IRModule Finalize() = 0;
97 
106  virtual GlobalVar AddFunction(const BaseFunc& func, ffi::String func_name_hint) = 0;
107 
113  virtual void UpdateFunction(const GlobalVar& gv, BaseFunc function) = 0;
114 
115  //-------------------------------
116  // Scope management
117  //-------------------------------
124  virtual ffi::Optional<Expr> LookupBinding(const Var& var) = 0;
125 
140  virtual void BeginScope(ffi::Optional<ffi::Array<Var>> params) = 0;
141 
155  virtual void BeginInnerScope() = 0;
156 
169  virtual void AddDefinitionToScope(Var var) = 0;
170 
172  virtual void EndScope() = 0;
173 
175  virtual void BeginDataflowBlock() = 0;
176 
178  virtual void BeginBindingBlock() = 0;
183  virtual BindingBlock EndBlock() = 0;
184 
189  virtual bool CurrentBlockIsDataFlow() = 0;
190 
200  virtual Var Emit(Expr expr, ffi::String name_hint = "") = 0;
201 
209  virtual Var EmitMatchCast(Expr value, Type ty, ffi::String name_hint = "") = 0;
210 
217  virtual Var EmitOutput(Expr output, ffi::String name_hint = "") = 0;
218 
226  virtual void EmitNormalized(Binding normalized_binding) = 0;
227 
236  virtual Expr Normalize(const Expr& expr) = 0;
237 
245  virtual Expr NormalizeArgument(const Expr& expr) = 0;
246 
252 
253  static constexpr const bool _type_mutable = true;
254  TVM_FFI_DECLARE_OBJECT_INFO("relax.BlockBuilder", BlockBuilderNode, ffi::Object);
255 };
256 
257 class BlockBuilder : public ffi::ObjectRef {
258  public:
270  TVM_DLL static BlockBuilder Create(ffi::Optional<IRModule> ctx_mod);
271 
296  explicit DisableOperatorSpecificNormalizationForTVMScript() = default; // NOLINT(*)
297  };
311  TVM_DLL static BlockBuilder Create(ffi::Optional<IRModule> ctx_mod,
313 
315 };
316 
317 } // namespace relax
318 } // namespace tvm
319 
320 #endif // TVM_RELAX_BLOCK_BUILDER_H_
Algebra expression simplifications.
Managed reference to BaseFuncNode.
Definition: function.h:250
Managed reference to ExprNode.
Definition: base_expr.h:311
Managed reference to GlobalVarNode.
Definition: expr.h:303
Managed reference class to IRModuleNode.
Definition: module.h:255
Managed reference to TypeNode.
Definition: base_expr.h:77
Managed reference class to UniqueNameSupplyNode.
Definition: unique_name_supply.h:115
Managed reference to AnalyzerObj.
Definition: analyzer.h:913
Definition: expr.h:403
Definition: expr.h:307
A builder to build Relax binding blocks.
Definition: block_builder.h:65
virtual void BeginDataflowBlock()=0
Begin to build a DataflowBlock.
virtual IRModule GetContextIRModule() const =0
Get the context IRModule in this builder.
virtual arith::Analyzer GetAnalyzer()=0
Get the analyzer of the BlockBuilder.
virtual Var Emit(Expr expr, ffi::String name_hint="")=0
Emits an Expr, and returns the variable it is bound to.
virtual ffi::Optional< Expr > LookupBinding(const Var &var)=0
Lookup the binding value that var binds to in the current emitted sequences.
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 GlobalVar AddFunction(const BaseFunc &func, ffi::String func_name_hint)=0
Add a Relax function or a TIR PrimFunc to internal context module.
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.
TVM_FFI_DECLARE_OBJECT_INFO("relax.BlockBuilder", BlockBuilderNode, ffi::Object)
virtual void EndScope()=0
End the previously defined scope.
virtual UniqueNameSupply name_supply()=0
Get the unique name supply for generating unique names.
virtual void BeginScope(ffi::Optional< ffi::Array< Var >> params)=0
Begin a new scope, with optional parameters that are visible within the scope.
virtual Var EmitMatchCast(Expr value, Type ty, ffi::String name_hint="")=0
Emit a MatchCast.
virtual void UpdateFunction(const GlobalVar &gv, BaseFunc function)=0
Update a Relax function or a TIR PrimFunc in the internal context module.
virtual Var EmitOutput(Expr output, ffi::String name_hint="")=0
Generate an output for the current dataflow block.
virtual void AddDefinitionToScope(Var var)=0
Append a definition to the current scope.
virtual bool CurrentBlockIsDataFlow()=0
Check if the block being built is DataflowBlock or not.
virtual BindingBlock EndBlock()=0
End building a BindingBlock.
virtual void BeginBindingBlock()=0
Begin to build a BindingBlock.
static constexpr const bool _type_mutable
Definition: block_builder.h:253
virtual Expr NormalizeArgument(const Expr &expr)=0
Normalize argument to a call or another IRNode.
Definition: block_builder.h:257
static BlockBuilder Create(ffi::Optional< IRModule > ctx_mod, DisableOperatorSpecificNormalizationForTVMScript tag)
Create a BlockBuilder.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(BlockBuilder, ffi::ObjectRef, BlockBuilderNode)
static BlockBuilder Create(ffi::Optional< IRModule > ctx_mod)
Create a BlockBuilder.
Definition: expr.h:155
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
Utility classes and functions for working with the Relax IR.
A marker struct to disable FNormalize.
Definition: block_builder.h:295
UniqueNameSupply that can be used to generate unique variable names.