tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
utils.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 
25 #ifndef TVM_TIR_USMP_UTILS_H_
26 #define TVM_TIR_USMP_UTILS_H_
27 
28 #include <tvm/ir/expr.h>
29 #include <tvm/ir/memory_pools.h>
30 #include <tvm/runtime/device_api.h>
31 #include <tvm/target/target.h>
32 #include <tvm/tir/stmt.h>
33 
34 namespace tvm {
35 
39 constexpr const char* kUSMPEnableOption = "tir.usmp.enable";
43 constexpr const char* kUSMPAlgorithmOption = "tir.usmp.algorithm";
47 constexpr const char* kUSMPUseWorkspaceIO = "tir.usmp.use_workspace_io";
52 constexpr const char* kUSMPCustomAlgorithmOption = "tir.usmp.custom_algorithm";
53 
54 namespace tir {
55 namespace usmp {
60 enum class BufferInfoKind { kIntermediate = 0, kInput = 1, kOutput = 2 };
61 
71 struct BufferInfoNode : public Object {
84 
86  v->Visit("name_hint", &name_hint);
87  v->Visit("size_bytes", &size_bytes);
88  v->Visit("pool_candidates", &pool_candidates);
89  v->Visit("alignment", &alignment);
90  v->Visit("conflicts", &conflicts);
91  v->Visit("kind", &kind);
92  }
93 
94  bool SEqualReduce(const BufferInfoNode* other, SEqualReducer equal) const {
95  return equal(name_hint, other->name_hint) && equal(size_bytes, other->size_bytes) &&
97  equal(conflicts, other->conflicts) && equal(kind, other->kind);
98  }
99 
100  void SHashReduce(SHashReducer hash_reduce) const {
101  hash_reduce(name_hint);
102  hash_reduce(size_bytes);
103  hash_reduce(alignment);
104  hash_reduce(conflicts);
105  hash_reduce(pool_candidates);
106  hash_reduce(kind);
107  }
113  TVM_DLL void SetConflicts(Array<ObjectRef> conflicting_buffer_info_objs);
114 
115  static constexpr const char* _type_key = "tir.usmp.BufferInfo";
117 };
118 
119 class BufferInfo : public ObjectRef {
120  public:
121  TVM_DLL BufferInfo(String name_hint, Integer size_bytes, Array<PoolInfo> pool_candidates,
125 };
126 
143 
145  v->Visit("buffer_info_stmts", &buffer_info_stmts);
146  v->Visit("memory_pressure", &memory_pressure);
147  }
148 
150  return equal(buffer_info_stmts, other->buffer_info_stmts) &&
152  }
153 
154  void SHashReduce(SHashReducer hash_reduce) const {
155  hash_reduce(buffer_info_stmts);
156  hash_reduce(memory_pressure);
157  }
158 };
159 
161  public:
162  TVM_DLL BufferInfoAnalysis(Map<BufferInfo, tir::Stmt> buffer_info_stmts, Integer memory_pressure);
164 };
165 
169 struct PoolAllocationNode : public Object {
174 
176  v->Visit("pool_info", &pool_info);
177  v->Visit("byte_offset", &byte_offset);
178  }
179 
181  return equal(pool_info, other->pool_info) && equal(byte_offset, other->byte_offset);
182  }
183 
184  void SHashReduce(SHashReducer hash_reduce) const {
185  hash_reduce(pool_info);
186  hash_reduce(byte_offset);
187  }
188 
189  static constexpr const char* _type_key = "tir.usmp.PoolAllocation";
191 };
192 
193 class PoolAllocation : public ObjectRef {
194  public:
195  TVM_DLL PoolAllocation(PoolInfo pool_info, Integer byte_offset);
197 };
198 
202 struct AllocatedPoolInfoNode : public Object {
209 
211  v->Visit("pool_info", &pool_info);
212  v->Visit("allocated_size", &allocated_size);
213  v->Visit("pool_var_idx", &pool_var_idx);
214  }
215 
217  return equal(pool_info, other->pool_info) && equal(allocated_size, other->allocated_size) &&
219  }
220 
221  void SHashReduce(SHashReducer hash_reduce) const {
222  hash_reduce(pool_info);
223  hash_reduce(allocated_size);
224  hash_reduce(pool_var_idx);
225  }
226 
227  static constexpr const char* _type_key = "ir.AllocatedPoolInfo";
229 };
230 
231 class AllocatedPoolInfo : public ObjectRef {
232  public:
233  TVM_DLL AllocatedPoolInfo(PoolInfo pool_info, Integer allocated_size,
234  Integer pool_var_idx = Integer());
236 };
237 
244 
251 
257 static constexpr const char* kPoolCandidatesAllocateAttr = "candidate_memory_pools";
258 
263 static constexpr const char* kInputTensorAllocate = "input_tensor";
264 
269 static constexpr const char* kOutputTensorAllocate = "output_tensor";
270 
277 
284 
292  const Map<BufferInfo, Stmt>& buffer_info_to_stmt,
293  const Map<BufferInfo, PoolAllocation>& buffer_info_to_pool_allocation);
294 
303  const Map<BufferInfo, PoolAllocation>& buffer_info_to_pool_allocation);
304 
305 } // namespace usmp
306 } // namespace tir
307 
308 namespace attr {
313 static constexpr const char* kPoolArgs = "pool_args";
314 
319 static constexpr const char* kIOTensorPoolAllocations = "io_tensor_pool_allocations";
320 
321 } // namespace attr
322 
323 } // namespace tvm
324 
325 #endif // TVM_TIR_USMP_UTILS_H_
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 class to IRModuleNode.
Definition: module.h:348
Container of constant int that adds more constructors.
Definition: expr.h:622
Base class for WorkspacePoolInfo and ConstantPoolInfo.
Definition: memory_pools.h:133
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:124
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:110
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
Base class of all object reference.
Definition: object.h:515
base class of all object containers.
Definition: object.h:167
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
Allocate a buffer that can be used in body.
Definition: stmt.h:537
Allocate a buffer that can be used in body.
Definition: stmt.h:455
Definition: utils.h:231
AllocatedPoolInfo(PoolInfo pool_info, Integer allocated_size, Integer pool_var_idx=Integer())
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(AllocatedPoolInfo, ObjectRef, AllocatedPoolInfoNode)
Definition: utils.h:160
BufferInfoAnalysis(Map< BufferInfo, tir::Stmt > buffer_info_stmts, Integer memory_pressure)
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(BufferInfoAnalysis, ObjectRef, BufferInfoAnalysisNode)
Definition: utils.h:119
BufferInfo(String name_hint, Integer size_bytes, Array< PoolInfo > pool_candidates, Integer alignment=runtime::kDefaultWorkspaceAlignment, BufferInfoKind kind=BufferInfoKind::kIntermediate)
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(BufferInfo, ObjectRef, BufferInfoNode)
Definition: utils.h:193
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(PoolAllocation, ObjectRef, PoolAllocationNode)
PoolAllocation(PoolInfo pool_info, Integer byte_offset)
Abstract device memory management API.
Base expr nodes in TVM.
The object definition for relay.build argument type of memory pools.
constexpr int kDefaultWorkspaceAlignment
Number of bytes each allocation must align to by default in the workspace buffer to service intermedi...
Definition: device_api.h:73
Array< BufferInfo > ConvertToArrayOfBufferInfo(const Map< BufferInfo, Stmt > &buffer_info_map)
Convert the IR-bound BufferInfo map to an array of BufferInfo.
Integer CalculateModuleWorkspaceSize(const IRModule &mod)
Calculate workspace required to execute a IRModule with main expressed in TIR.
Map< Stmt, PoolAllocation > AssignStmtPoolAllocations(const Map< BufferInfo, Stmt > &buffer_info_to_stmt, const Map< BufferInfo, PoolAllocation > &buffer_info_to_pool_allocation)
Joins the Stmt nodes with PoolAllocation objects.
Integer CalculateExtentsSize(const AllocateNode *op)
Calculate the size of the extents in bytes.
BufferInfoKind
A special kind to distinguish between I/O tensors to the model and intermediate tensors of the model.
Definition: utils.h:60
Map< String, PoolAllocation > GetIOPoolAllocations(const Map< BufferInfo, PoolAllocation > &buffer_info_to_pool_allocation)
Obtains I/O tensor names to their PoolAllocation objects.
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
constexpr const char * kUSMPUseWorkspaceIO
PassContext option to enable placing I/O tensors in the workspace.
Definition: utils.h:47
constexpr const char * kUSMPCustomAlgorithmOption
PassContext option to specify a custom memory planning algorithm in USMP. The algorithm should be pro...
Definition: utils.h:52
constexpr const char * kUSMPAlgorithmOption
PassContext option to select the memory planning algorithm in USMP.
Definition: utils.h:43
constexpr const char * kUSMPEnableOption
PassContext option to enable the USMP.
Definition: utils.h:39
TIR statements.
This object contains information post-allocation for PoolInfo objects.
Definition: utils.h:202
Optional< Integer > pool_var_idx
An optional associated pool Var index of PrimFunc params.
Definition: utils.h:208
void SHashReduce(SHashReducer hash_reduce) const
Definition: utils.h:221
TVM_DECLARE_FINAL_OBJECT_INFO(AllocatedPoolInfoNode, Object)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: utils.h:210
bool SEqualReduce(const AllocatedPoolInfoNode *other, SEqualReducer equal) const
Definition: utils.h:216
static constexpr const char * _type_key
Definition: utils.h:227
Integer allocated_size
The allocated size into this pool.
Definition: utils.h:206
PoolInfo pool_info
The assigned PoolInfo object.
Definition: utils.h:204
This is a composite node that is produced by extract_buffer_info analysis pass that contains useful g...
Definition: utils.h:132
bool SEqualReduce(const BufferInfoAnalysisNode *other, SEqualReducer equal) const
Definition: utils.h:149
Map< BufferInfo, tir::Stmt > buffer_info_stmts
The BufferInfo object and its associated TIR statement.
Definition: utils.h:134
void VisitAttrs(tvm::AttrVisitor *v)
Definition: utils.h:144
void SHashReduce(SHashReducer hash_reduce) const
Definition: utils.h:154
Integer memory_pressure
This represent maximum amount of memory being used at any point of time in the inference....
Definition: utils.h:142
Describes an abstract memory buffer that will get allocated inside a pool. The actual memory buffer i...
Definition: utils.h:71
Integer size_bytes
The size in terms of bytes.
Definition: utils.h:75
TVM_DECLARE_FINAL_OBJECT_INFO(BufferInfoNode, Object)
static constexpr const char * _type_key
Definition: utils.h:115
BufferInfoKind kind
Whether BufferInfo object retains info about IO tensors or intermediaries.
Definition: utils.h:83
Array< PoolInfo > pool_candidates
The pool candidates that this buffer can get pooled to.
Definition: utils.h:77
Integer alignment
The byte alignment required for buffers that will placed within the pool.
Definition: utils.h:79
Array< ObjectRef > conflicts
The liveness conflicting other buffer info objects.
Definition: utils.h:81
void SetConflicts(Array< ObjectRef > conflicting_buffer_info_objs)
Set the liveness conflicts of this BufferInfo.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: utils.h:85
String name_hint
The name of the buffer var.
Definition: utils.h:73
bool SEqualReduce(const BufferInfoNode *other, SEqualReducer equal) const
Definition: utils.h:94
void SHashReduce(SHashReducer hash_reduce) const
Definition: utils.h:100
The pool allocation produced after the USMP algorithm.
Definition: utils.h:169
static constexpr const char * _type_key
Definition: utils.h:189
void VisitAttrs(tvm::AttrVisitor *v)
Definition: utils.h:175
void SHashReduce(SHashReducer hash_reduce) const
Definition: utils.h:184
Integer byte_offset
The byte offset within the pool.
Definition: utils.h:173
TVM_DECLARE_FINAL_OBJECT_INFO(PoolAllocationNode, Object)
bool SEqualReduce(const PoolAllocationNode *other, SEqualReducer equal) const
Definition: utils.h:180
PoolInfo pool_info
The assigned WorkspacePoolInfo or ConstantPoolInfo object.
Definition: utils.h:171
Compilation target object.