tvm
memory_pools.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_IR_MEMORY_POOLS_H_
25 #define TVM_IR_MEMORY_POOLS_H_
26 
27 #include <tvm/runtime/registry.h>
28 #include <tvm/target/target.h>
29 
30 struct TVMConstantInfo;
31 namespace tvm {
32 
36 struct PoolInfoNode : public Object {
37  public:
61  bool is_internal = false;
62 
65 
67  v->Visit("pool_name", &pool_name);
68  v->Visit("size_hint_bytes", &size_hint_bytes);
69  v->Visit("clock_frequency_hz", &clock_frequency_hz);
70  v->Visit("read_bandwidth_bytes_per_cycle", &read_bandwidth_bytes_per_cycle);
71  v->Visit("write_bandwidth_bytes_per_cycle", &write_bandwidth_bytes_per_cycle);
72  v->Visit("read_latency_cycles", &read_latency_cycles);
73  v->Visit("write_latency_cycles", &write_latency_cycles);
74  v->Visit("target_burst_bytes", &target_burst_bytes);
75  v->Visit("is_internal", &is_internal);
76  }
77 
78  bool SEqualReduce(const PoolInfoNode* other, SEqualReducer equal) const {
79  return equal(pool_name, other->pool_name) && equal(size_hint_bytes, other->size_hint_bytes) &&
80  equal(clock_frequency_hz, other->clock_frequency_hz) &&
81  equal(read_bandwidth_bytes_per_cycle, other->read_bandwidth_bytes_per_cycle) &&
82  equal(write_bandwidth_bytes_per_cycle, other->write_bandwidth_bytes_per_cycle) &&
83  equal(read_latency_cycles, other->read_latency_cycles) &&
84  equal(write_latency_cycles, other->write_latency_cycles) &&
85  equal(target_burst_bytes, other->target_burst_bytes) &&
86  equal(is_internal, other->is_internal);
87  }
88 
89  void SHashReduce(SHashReducer hash_reduce) const {
90  hash_reduce(pool_name);
91  hash_reduce(size_hint_bytes);
92  hash_reduce(clock_frequency_hz);
93  hash_reduce(read_bandwidth_bytes_per_cycle);
94  hash_reduce(write_bandwidth_bytes_per_cycle);
95  hash_reduce(read_latency_cycles);
96  hash_reduce(write_latency_cycles);
97  hash_reduce(target_burst_bytes);
98  hash_reduce(is_internal);
99  }
100 
101  static constexpr const char* _type_key = "ir.PoolInfo";
103 };
104 
110 static constexpr const char* kTargetPoolReadWriteAccess = "rw";
111 
117 static constexpr const char* kTargetPoolReadOnlyAccess = "ro";
118 
120 static const int kUnrestrictedPoolSizeHint = -1;
121 
123 static const int kUnknownClockFrequency = -1;
124 
126 static const int kUnknownReadBandwidth = -1;
127 
129 static const int kUnknownWriteBandwidth = -1;
130 
132 class PoolInfo : public ObjectRef {
133  protected:
134  TVM_DLL PoolInfo(String pool_name, Integer size_hint_bytes = kUnrestrictedPoolSizeHint,
135  Integer clock_frequency_hz = kUnknownClockFrequency,
136  Integer read_bandwidth_bytes_per_cycle = kUnknownReadBandwidth,
137  Integer write_bandwidth_bytes_per_cycle = kUnknownWriteBandwidth,
140 
141  public:
143 };
144 
153  Integer size_hint_bytes = kUnrestrictedPoolSizeHint;
155  Integer clock_frequency_hz = kUnknownClockFrequency;
157  Integer read_bandwidth_bytes_per_cycle = kUnknownReadBandwidth;
159  Integer write_bandwidth_bytes_per_cycle = kUnknownWriteBandwidth;
170  bool is_internal = false;
171 
173  v->Visit("size_hint_bytes", &size_hint_bytes);
174  v->Visit("clock_frequency_hz", &clock_frequency_hz);
175  v->Visit("read_bandwidth_bytes_per_cycle", &read_bandwidth_bytes_per_cycle);
176  v->Visit("write_bandwidth_bytes_per_cycle", &write_bandwidth_bytes_per_cycle);
177  v->Visit("read_latency_cycles", &read_latency_cycles);
178  v->Visit("write_latency_cycles", &write_latency_cycles);
179  v->Visit("target_burst_bytes", &target_burst_bytes);
180  v->Visit("is_internal", &is_internal);
181  }
182 
184  return equal(size_hint_bytes, other->size_hint_bytes) &&
185  equal(clock_frequency_hz, other->clock_frequency_hz) &&
186  equal(read_bandwidth_bytes_per_cycle, other->read_bandwidth_bytes_per_cycle) &&
187  equal(write_bandwidth_bytes_per_cycle, other->write_bandwidth_bytes_per_cycle) &&
188  equal(read_latency_cycles, other->read_latency_cycles) &&
189  equal(write_latency_cycles, other->write_latency_cycles) &&
191  equal(is_internal, other->is_internal);
192  }
193 
194  void SHashReduce(SHashReducer hash_reduce) const {
195  hash_reduce(size_hint_bytes);
196  hash_reduce(clock_frequency_hz);
197  hash_reduce(read_bandwidth_bytes_per_cycle);
198  hash_reduce(write_bandwidth_bytes_per_cycle);
199  hash_reduce(read_latency_cycles);
200  hash_reduce(write_latency_cycles);
201  hash_reduce(target_burst_bytes);
202  hash_reduce(is_internal);
203  }
204 
205  static constexpr const char* _type_key = "ir.PoolInfoProperties";
207 };
208 
210  public:
212  Integer clock_frequency_hz = kUnknownClockFrequency,
213  Integer read_bandwidth_bytes_per_cycle = kUnknownReadBandwidth,
214  Integer write_bandwidth_bytes_per_cycle = kUnknownWriteBandwidth,
217  Bool is_internal = Bool(false));
219 };
220 
221 /* \brief Represents RW memory area */
224 
226  return PoolInfoNode::SEqualReduce(other, equal);
227  }
228 
229  void SHashReduce(SHashReducer hash_reduce) const { PoolInfoNode::SHashReduce(hash_reduce); }
230 
231  static constexpr const char* _type_key = "ir.WorkspacePoolInfo";
233 };
234 
235 class WorkspacePoolInfo : public PoolInfo {
236  public:
237  TVM_DLL WorkspacePoolInfo(
239  PoolInfoProperties properties = PoolInfoProperties(kUnrestrictedPoolSizeHint));
241 };
242 
243 /*
244  * \brief The ConstantInfoNode contains numeric literal in RO pool
245  * Used to initialise RO memory in ConstantPoolInfo
246  */
247 struct ConstantInfoNode : public Object {
251 
253  v->Visit("name_hint", &name_hint);
254  v->Visit("byte_offset", &byte_offset);
255  v->Visit("data", &data);
256  }
257 
258  bool SEqualReduce(const ConstantInfoNode* other, SEqualReducer equal) const {
259  return equal(name_hint, other->name_hint) && equal(byte_offset, other->byte_offset) &&
260  equal(data, other->data);
261  }
262 
263  void SHashReduce(SHashReducer hash_reduce) const {
264  hash_reduce(name_hint);
265  hash_reduce(byte_offset);
266  hash_reduce(data);
267  }
268 
269  static constexpr const char* _type_key = "ir.ConstantInfo";
270  static constexpr bool _type_has_method_sequal_reduce = true;
271  static constexpr bool _type_has_method_shash_reduce = true;
273 };
274 
275 class ConstantInfo : public ObjectRef {
276  public:
277  TVM_DLL ConstantInfo(const struct ::TVMConstantInfo* data);
278  ConstantInfo(String name, Integer byte_offset, runtime::NDArray data);
280 };
281 
282 /* \brief ConstantPoolInfoNode represents an RO memory area initialized with
283  * data from constant_info_array */
286 
289  v->Visit("constant_info_array", &constant_info_array);
290  }
291 
293  return PoolInfoNode::SEqualReduce(other, equal) &&
294  equal(constant_info_array, other->constant_info_array);
295  }
296 
297  void SHashReduce(SHashReducer hash_reduce) const {
298  PoolInfoNode::SHashReduce(hash_reduce);
299  hash_reduce(constant_info_array);
300  }
301 
302  static constexpr const char* _type_key = "ir.ConstantPoolInfo";
304 };
305 
306 class ConstantPoolInfo : public PoolInfo {
307  public:
308  TVM_DLL ConstantPoolInfo(
310  PoolInfoProperties properties = PoolInfoProperties(kUnrestrictedPoolSizeHint));
312 };
313 
314 /* \brief A container for WorkspacePoolInfo objects */
317 
318  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("pools", &pools); }
319 
321  return equal(pools, other->pools);
322  }
323 
324  void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(pools); }
325 
326  static constexpr const char* _type_key = "ir.WorkspaceMemoryPools";
328 };
329 
331  public:
332  TVM_DLL WorkspaceMemoryPools(Array<PoolInfo> pools);
334 };
335 
336 /* \brief A container for ConstantPoolInfo objects */
339 
340  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("pools", &pools); }
341 
343  return equal(pools, other->pools);
344  }
345 
346  void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(pools); }
347 
348  static constexpr const char* _type_key = "ir.ConstantMemoryPools";
350 };
351 
353  public:
356 };
357 
358 } // namespace tvm
359 
360 #endif // TVM_IR_MEMORY_POOLS_H_
Definition: memory_pools.h:330
Integer read_latency_cycles
The read latency in cycles.
Definition: memory_pools.h:161
Boolean constant.
Definition: expr.h:369
Definition: memory_pools.h:235
void SHashReduce(SHashReducer hash_reduce) const
Definition: memory_pools.h:324
String name_hint
Definition: memory_pools.h:248
TVM_DECLARE_BASE_OBJECT_INFO(PoolInfoNode, Object)
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:102
void VisitAttrs(tvm::AttrVisitor *v)
Definition: memory_pools.h:318
Integer write_bandwidth_bytes_per_cycle
The write bandwidth in bytes/cycle.
Definition: memory_pools.h:50
void VisitAttrs(tvm::AttrVisitor *v)
Definition: memory_pools.h:252
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:102
void VisitAttrs(tvm::AttrVisitor *v)
Definition: memory_pools.h:287
void VisitAttrs(tvm::AttrVisitor *v)
Definition: memory_pools.h:172
Integer size_hint_bytes
The expected size hint to be used by the allocator. The size_hint_bytes is set to kUnrestrictedPoolSi...
Definition: memory_pools.h:153
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Integer write_latency_cycles
The write latency in cycles.
Definition: memory_pools.h:163
Base class for WorkspacePoolInfo and ConstantPoolInfo.
Definition: memory_pools.h:132
bool is_internal
Whether pool is internally generated. The internal pools will be generated as part of the entry point...
Definition: memory_pools.h:61
Integer write_bandwidth_bytes_per_cycle
The write bandwidth in bytes/cycle.
Definition: memory_pools.h:159
Definition: memory_pools.h:275
Integer read_latency_cycles
The read latency in cycles.
Definition: memory_pools.h:52
Integer size_hint_bytes
The expected size hint to be used by the allocator. The size_hint_bytes is set to kUnrestrictedPoolSi...
Definition: memory_pools.h:44
Integer read_bandwidth_bytes_per_cycle
The read bandwidth in bytes/cycle.
Definition: memory_pools.h:48
base class of all object containers.
Definition: object.h:167
Map< Target, Integer > target_burst_bytes
The burst length in bytes for each Target.
Definition: memory_pools.h:56
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:59
Map< Target, Integer > target_burst_bytes
The burst length in bytes for each Target.
Definition: memory_pools.h:165
Integer clock_frequency_hz
The clock frequency of the memory in Hz.
Definition: memory_pools.h:46
#define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:744
bool SEqualReduce(const WorkspaceMemoryPoolsNode *other, SEqualReducer equal) const
Definition: memory_pools.h:320
static constexpr const char * _type_key
Definition: memory_pools.h:101
Array< Target > targets
The targets linked to the pool.
Definition: memory_pools.h:64
Definition: memory_pools.h:247
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
Array< ConstantPoolInfo > pools
Definition: memory_pools.h:338
void VisitAttrs(tvm::AttrVisitor *v)
Definition: memory_pools.h:66
bool is_internal
Whether pool is internally generated. The internal pools will be generated as part of the entry point...
Definition: memory_pools.h:170
Array< PoolInfo > pools
Definition: memory_pools.h:316
bool SEqualReduce(const ConstantPoolInfoNode *other, SEqualReducer equal) const
Definition: memory_pools.h:292
void VisitAttrs(tvm::AttrVisitor *v)
Definition: memory_pools.h:223
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
String pool_name
The name of the memory pool.
Definition: memory_pools.h:39
Reference to string objects.
Definition: string.h:124
bool SEqualReduce(const PoolInfoPropertiesNode *other, SEqualReducer equal) const
Definition: memory_pools.h:183
void SHashReduce(SHashReducer hash_reduce) const
Definition: memory_pools.h:89
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:713
Integer write_latency_cycles
The write latency in cycles.
Definition: memory_pools.h:54
Definition: memory_pools.h:352
runtime::NDArray data
Definition: memory_pools.h:250
Base class of all object reference.
Definition: object.h:511
void SHashReduce(SHashReducer hash_reduce) const
Definition: memory_pools.h:263
void SHashReduce(SHashReducer hash_reduce) const
Definition: memory_pools.h:194
Definition: memory_pools.h:306
Integer clock_frequency_hz
The clock frequency of the memory in Hz.
Definition: memory_pools.h:155
Describes one constant argument to run_model.
Definition: metadata_types.h:94
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
Integer byte_offset
Definition: memory_pools.h:249
bool SEqualReduce(const WorkspacePoolInfoNode *other, SEqualReducer equal) const
Definition: memory_pools.h:225
Integer read_bandwidth_bytes_per_cycle
The read bandwidth in bytes/cycle.
Definition: memory_pools.h:157
bool SEqualReduce(const PoolInfoNode *other, SEqualReducer equal) const
Definition: memory_pools.h:78
Compilation target object.
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1268
void SHashReduce(SHashReducer hash_reduce) const
Definition: memory_pools.h:229
static constexpr bool _type_has_method_shash_reduce
Definition: object.h:234
bool SEqualReduce(const ConstantInfoNode *other, SEqualReducer equal) const
Definition: memory_pools.h:258
Definition: memory_pools.h:209
void VisitAttrs(tvm::AttrVisitor *v)
Definition: memory_pools.h:340
Definition: memory_pools.h:284
Definition: memory_pools.h:337
Describes a pool of memory accessible by one or more targets.
Definition: memory_pools.h:36
bool SEqualReduce(const ConstantMemoryPoolsNode *other, SEqualReducer equal) const
Definition: memory_pools.h:342
Describes a pool of memory properties.
Definition: memory_pools.h:148
void SHashReduce(SHashReducer hash_reduce) const
Definition: memory_pools.h:346
Definition: memory_pools.h:315
static constexpr bool _type_has_method_sequal_reduce
Definition: object.h:233
Array< ConstantInfo > constant_info_array
Definition: memory_pools.h:285
void SHashReduce(SHashReducer hash_reduce) const
Definition: memory_pools.h:297
This file defines the TVM global function registry.
Definition: memory_pools.h:222
Container of constant int that adds more constructors.
Definition: expr.h:404