Skip to content

Commit c80da03

Browse files
authored
[USMP] Adding support for U1 usecase for constant pools (#10189)
* [TIR.Constant] U1 usecase Constants are now aggregated into one struct and initialized in default_lib0.c file Change-Id: I34d61f8139c8a92c06944fe990ba892a660476fd Unit test fixed Change-Id: I436e7b6d6b3064b3f8bbfbb048d4296b63a6b69c * Refactored Addressed: * PoolInfo splitted to WorkspacePoolInfo and ConstantPoolInfo * workspace_byte_alignment moved to ExecutorCodegenMetadata * getModuleAlignment -> GetModuleAlignment * GenerateInternalWorkspaceBuffers refactored * reverted format change of src/tir/transforms/legalize_packed_calls.cc * addressed comments for src/tir/usmp/analysis/extract_buffer_info.cc * removed commented code from include/tvm/tir/usmp/utils.h Change-Id: I7d1b32884b0e5992e2e00c7838c85e425d9c25fd * more unit test fixes Change-Id: I573a05fa1cb4037ae83691f7dff2c2724b1d7700 * More refactoring and unit test fixes Added ConstantMemoryPools Change-Id: If1e391c631575980564bca790ba33748c82d907f * bugfix Change-Id: Iacc7a9d734a505dfa0d8d32d23ea3f57e6de8582 * refactoring. added constant_alignment added constant_alignment unit tests updated Change-Id: I378193cb9e675e352c61d96ff4e09655090053e1 * unit-test bugix Change-Id: Ia4411d59c4a376c01326fed366cdb196a432899e * unit test fix Change-Id: Ia2077bdeb1d2c6c9827eeef90ab410ae31b8c4a4 * Added support for c++ runtime * refactored * renamed pools and consts renamed pools and consts to workspace_pools and constant_pools * addressed upstream comments * addressed upstream comments-2 * addressed upstream comments-3
1 parent 910624a commit c80da03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1671
-468
lines changed

include/tvm/ir/memory_pools.h

Lines changed: 180 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@
2727
#include <tvm/runtime/registry.h>
2828
#include <tvm/target/target.h>
2929

30+
struct TVMConstantInfo;
3031
namespace tvm {
3132

3233
/*!
3334
* \brief Describes a pool of memory accessible by one or more targets.
3435
*/
3536
struct PoolInfoNode : public Object {
37+
public:
3638
/*! \brief The name of the memory pool */
3739
String pool_name;
3840
/*! \brief The expected size hint to be used by the allocator.
3941
* The size_hint_bytes is set to kUnrestrictedPoolSizeHint
4042
* to indicate the pool is not size restricted.
4143
*/
4244
Integer size_hint_bytes;
43-
/*! \brief The accessibility from each Target */
44-
Map<Target, String> target_access; // 'rw' or 'ro'
4545
/*! \brief The clock frequency of the memory in Hz */
4646
Integer clock_frequency_hz;
4747
/*! \brief The read bandwidth in bytes/cycle */
@@ -60,10 +60,12 @@ struct PoolInfoNode : public Object {
6060
*/
6161
bool is_internal = false;
6262

63+
/*! \brief The targets linked to the pool */
64+
Array<Target> targets;
65+
6366
void VisitAttrs(tvm::AttrVisitor* v) {
6467
v->Visit("pool_name", &pool_name);
6568
v->Visit("size_hint_bytes", &size_hint_bytes);
66-
v->Visit("target_access", &target_access);
6769
v->Visit("clock_frequency_hz", &clock_frequency_hz);
6870
v->Visit("read_bandwidth_bytes_per_cycle", &read_bandwidth_bytes_per_cycle);
6971
v->Visit("write_bandwidth_bytes_per_cycle", &write_bandwidth_bytes_per_cycle);
@@ -75,8 +77,6 @@ struct PoolInfoNode : public Object {
7577

7678
bool SEqualReduce(const PoolInfoNode* other, SEqualReducer equal) const {
7779
return equal(pool_name, other->pool_name) && equal(size_hint_bytes, other->size_hint_bytes) &&
78-
equal(target_access, other->target_access) &&
79-
equal(target_access, other->target_access) &&
8080
equal(clock_frequency_hz, other->clock_frequency_hz) &&
8181
equal(read_bandwidth_bytes_per_cycle, other->read_bandwidth_bytes_per_cycle) &&
8282
equal(write_bandwidth_bytes_per_cycle, other->write_bandwidth_bytes_per_cycle) &&
@@ -89,7 +89,6 @@ struct PoolInfoNode : public Object {
8989
void SHashReduce(SHashReducer hash_reduce) const {
9090
hash_reduce(pool_name);
9191
hash_reduce(size_hint_bytes);
92-
hash_reduce(target_access);
9392
hash_reduce(clock_frequency_hz);
9493
hash_reduce(read_bandwidth_bytes_per_cycle);
9594
hash_reduce(write_bandwidth_bytes_per_cycle);
@@ -100,7 +99,7 @@ struct PoolInfoNode : public Object {
10099
}
101100

102101
static constexpr const char* _type_key = "ir.PoolInfo";
103-
TVM_DECLARE_FINAL_OBJECT_INFO(PoolInfoNode, Object);
102+
TVM_DECLARE_BASE_OBJECT_INFO(PoolInfoNode, Object);
104103
};
105104

106105
/*!
@@ -129,18 +128,166 @@ static const int kUnknownReadBandwidth = -1;
129128
/*! \brief The write bandwidth is not known */
130129
static const int kUnknownWriteBandwidth = -1;
131130

131+
/*! \brief Base class for WorkspacePoolInfo and ConstantPoolInfo */
132132
class PoolInfo : public ObjectRef {
133-
public:
134-
TVM_DLL PoolInfo(String pool_name, Map<Target, String> target_access,
135-
Integer size_hint_bytes = kUnrestrictedPoolSizeHint,
133+
protected:
134+
TVM_DLL PoolInfo(String pool_name, Integer size_hint_bytes = kUnrestrictedPoolSizeHint,
136135
Integer clock_frequency_hz = kUnknownClockFrequency,
137136
Integer read_bandwidth_bytes_per_cycle = kUnknownReadBandwidth,
138137
Integer write_bandwidth_bytes_per_cycle = kUnknownWriteBandwidth,
139138
Integer read_latency_cycles = 0, Integer write_latency_cycles = 0,
140139
Map<Target, Integer> target_burst_bytes = {}, Bool is_internal = Bool(false));
141-
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(PoolInfo, ObjectRef, PoolInfoNode);
140+
141+
public:
142+
TVM_DEFINE_OBJECT_REF_METHODS(PoolInfo, ObjectRef, PoolInfoNode);
143+
};
144+
145+
/*!
146+
* \brief Describes a pool of memory properties
147+
*/
148+
struct PoolInfoPropertiesNode : public Object {
149+
/*! \brief The expected size hint to be used by the allocator.
150+
* The size_hint_bytes is set to kUnrestrictedPoolSizeHint
151+
* to indicate the pool is not size restricted.
152+
*/
153+
Integer size_hint_bytes = kUnrestrictedPoolSizeHint;
154+
/*! \brief The clock frequency of the memory in Hz */
155+
Integer clock_frequency_hz = kUnknownClockFrequency;
156+
/*! \brief The read bandwidth in bytes/cycle */
157+
Integer read_bandwidth_bytes_per_cycle = kUnknownReadBandwidth;
158+
/*! \brief The write bandwidth in bytes/cycle */
159+
Integer write_bandwidth_bytes_per_cycle = kUnknownWriteBandwidth;
160+
/*! \brief The read latency in cycles */
161+
Integer read_latency_cycles = 0;
162+
/*! \brief The write latency in cycles */
163+
Integer write_latency_cycles = 0;
164+
/*! \brief The burst length in bytes for each Target */
165+
Map<Target, Integer> target_burst_bytes{};
166+
/*! \brief Whether pool is internally generated.
167+
* The internal pools will be generated as part of
168+
* the entry point code generation of the executor
169+
*/
170+
bool is_internal = false;
171+
172+
void VisitAttrs(tvm::AttrVisitor* v) {
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+
183+
bool SEqualReduce(const PoolInfoPropertiesNode* other, SEqualReducer equal) const {
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) &&
190+
equal(target_burst_bytes, other->target_burst_bytes) &&
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";
206+
TVM_DECLARE_FINAL_OBJECT_INFO(PoolInfoPropertiesNode, Object);
142207
};
143208

209+
class PoolInfoProperties : public ObjectRef {
210+
public:
211+
TVM_DLL PoolInfoProperties(Integer size_hint_bytes,
212+
Integer clock_frequency_hz = kUnknownClockFrequency,
213+
Integer read_bandwidth_bytes_per_cycle = kUnknownReadBandwidth,
214+
Integer write_bandwidth_bytes_per_cycle = kUnknownWriteBandwidth,
215+
Integer read_latency_cycles = 0, Integer write_latency_cycles = 0,
216+
Map<Target, Integer> target_burst_bytes = {},
217+
Bool is_internal = Bool(false));
218+
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(PoolInfoProperties, ObjectRef, PoolInfoPropertiesNode);
219+
};
220+
221+
/* \brief Represents RW memory area */
222+
struct WorkspacePoolInfoNode : public PoolInfoNode {
223+
static constexpr const char* _type_key = "ir.WorkspacePoolInfo";
224+
TVM_DECLARE_FINAL_OBJECT_INFO(WorkspacePoolInfoNode, PoolInfoNode);
225+
};
226+
227+
class WorkspacePoolInfo : public PoolInfo {
228+
public:
229+
TVM_DLL WorkspacePoolInfo(
230+
String pool_name, Array<Target> targets,
231+
PoolInfoProperties properties = PoolInfoProperties(kUnrestrictedPoolSizeHint));
232+
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(WorkspacePoolInfo, PoolInfo, WorkspacePoolInfoNode);
233+
};
234+
235+
/*
236+
* \brief The ConstantInfoNode contains numeric literal in RO pool
237+
* Used to initialise RO memory in ConstantPoolInfo
238+
*/
239+
struct ConstantInfoNode : public Object {
240+
String name_hint;
241+
Integer byte_offset;
242+
runtime::NDArray data;
243+
244+
void VisitAttrs(tvm::AttrVisitor* v) {
245+
v->Visit("name_hint", &name_hint);
246+
v->Visit("byte_offset", &byte_offset);
247+
v->Visit("data", &data);
248+
}
249+
250+
bool SEqualReduce(const ConstantInfoNode* other, SEqualReducer equal) const {
251+
return equal(name_hint, other->name_hint) && equal(byte_offset, other->byte_offset) &&
252+
equal(data, other->data);
253+
}
254+
255+
void SHashReduce(SHashReducer hash_reduce) const {
256+
hash_reduce(name_hint);
257+
hash_reduce(byte_offset);
258+
hash_reduce(data);
259+
}
260+
261+
static constexpr const char* _type_key = "ir.ConstantInfo";
262+
static constexpr bool _type_has_method_sequal_reduce = true;
263+
static constexpr bool _type_has_method_shash_reduce = true;
264+
TVM_DECLARE_FINAL_OBJECT_INFO(ConstantInfoNode, Object);
265+
};
266+
267+
class ConstantInfo : public ObjectRef {
268+
public:
269+
TVM_DLL ConstantInfo(const struct ::TVMConstantInfo* data);
270+
ConstantInfo(String name, Integer byte_offset, runtime::NDArray data);
271+
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ConstantInfo, ObjectRef, ConstantInfoNode);
272+
};
273+
274+
/* \brief ConstantPoolInfoNode represents an RO memory area initialized with
275+
* data from constant_info_array */
276+
struct ConstantPoolInfoNode : public PoolInfoNode {
277+
Array<ConstantInfo> constant_info_array;
278+
static constexpr const char* _type_key = "ir.ConstantPoolInfo";
279+
TVM_DECLARE_FINAL_OBJECT_INFO(ConstantPoolInfoNode, PoolInfoNode);
280+
};
281+
282+
class ConstantPoolInfo : public PoolInfo {
283+
public:
284+
TVM_DLL ConstantPoolInfo(
285+
String pool_name, Array<Target> targets, Array<ConstantInfo> constant_info_array,
286+
PoolInfoProperties properties = PoolInfoProperties(kUnrestrictedPoolSizeHint));
287+
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ConstantPoolInfo, PoolInfo, ConstantPoolInfoNode);
288+
};
289+
290+
/* \brief A container for WorkspacePoolInfo objects */
144291
struct WorkspaceMemoryPoolsNode : public Object {
145292
Array<PoolInfo> pools;
146293

@@ -162,6 +309,28 @@ class WorkspaceMemoryPools : public ObjectRef {
162309
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(WorkspaceMemoryPools, ObjectRef, WorkspaceMemoryPoolsNode);
163310
};
164311

312+
/* \brief A container for ConstantPoolInfo objects */
313+
struct ConstantMemoryPoolsNode : public Object {
314+
Array<ConstantPoolInfo> pools;
315+
316+
void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("pools", &pools); }
317+
318+
bool SEqualReduce(const ConstantMemoryPoolsNode* other, SEqualReducer equal) const {
319+
return equal(pools, other->pools);
320+
}
321+
322+
void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(pools); }
323+
324+
static constexpr const char* _type_key = "ir.ConstantMemoryPools";
325+
TVM_DECLARE_FINAL_OBJECT_INFO(ConstantMemoryPoolsNode, Object);
326+
};
327+
328+
class ConstantMemoryPools : public ObjectRef {
329+
public:
330+
TVM_DLL ConstantMemoryPools(Array<ConstantPoolInfo> pools);
331+
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ConstantMemoryPools, ObjectRef, ConstantMemoryPoolsNode);
332+
};
333+
165334
} // namespace tvm
166335

167336
#endif // TVM_IR_MEMORY_POOLS_H_

include/tvm/ir/module.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,15 @@ constexpr const char* kRuntime = "runtime";
506506
*/
507507
constexpr const char* kWorkspaceMemoryPools = "workspace_memory_pools";
508508

509+
/*!
510+
* \brief constant memory pools of the module
511+
*
512+
* Type: ConstantMemoryPools
513+
*
514+
* \sa tvm::ConstantMemoryPools
515+
*/
516+
constexpr const char* kConstantMemoryPools = "constant_memory_pools";
517+
509518
/*
510519
* \brief Module attribute for tir constants
511520
*/

include/tvm/runtime/metadata.h

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#ifndef TVM_RUNTIME_METADATA_H_
2525
#define TVM_RUNTIME_METADATA_H_
2626

27+
#include <dmlc/memory_io.h>
2728
#include <tvm/runtime/c_runtime_api.h>
2829
#include <tvm/runtime/metadata_base.h>
2930
#include <tvm/runtime/metadata_types.h>
@@ -45,16 +46,10 @@ namespace metadata {
4546
* Should be populated into the `version` field of all TVMMetadata.
4647
*/
4748
static const constexpr int64_t kMetadataVersion = TVM_METADATA_VERSION;
48-
} // namespace metadata
49-
} // namespace runtime
50-
} // namespace tvm
51-
52-
namespace tvm {
53-
namespace runtime {
54-
namespace metadata {
5549

5650
class Metadata;
5751
class TensorInfo;
52+
class ConstantInfoMetadata;
5853

5954
class MetadataNode : public MetadataBaseNode {
6055
public:
@@ -66,10 +61,12 @@ class MetadataNode : public MetadataBaseNode {
6661
ArrayAccessor<struct TVMTensorInfo, TensorInfo> inputs();
6762
inline int64_t num_outputs() const { return data_->num_outputs; }
6863
ArrayAccessor<struct TVMTensorInfo, TensorInfo> outputs();
69-
inline int64_t num_pools() const { return data_->num_pools; }
70-
ArrayAccessor<struct TVMTensorInfo, TensorInfo> pools();
64+
inline int64_t num_workspace_pools() const { return data_->num_workspace_pools; }
65+
ArrayAccessor<struct TVMTensorInfo, TensorInfo> workspace_pools();
7166
inline ::tvm::runtime::String mod_name() const { return ::tvm::runtime::String(data_->mod_name); }
7267
const struct ::TVMMetadata* data() const { return data_; }
68+
ArrayAccessor<struct TVMConstantInfo, ConstantInfoMetadata> constant_pools();
69+
inline int64_t num_constant_pools() const { return data_->num_constant_pools; }
7370
TVM_DECLARE_FINAL_OBJECT_INFO(MetadataNode, MetadataBaseNode);
7471

7572
private:
@@ -107,6 +104,37 @@ class TensorInfo : public MetadataBase {
107104
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TensorInfo, MetadataBase, TensorInfoNode);
108105
};
109106

107+
class ConstantInfoMetadataNode : public MetadataBaseNode {
108+
public:
109+
explicit ConstantInfoMetadataNode(const struct ::TVMConstantInfo* data) : data_{data} {}
110+
// This name should match TVMConstantInfo after processing
111+
static constexpr const char* _type_key = "metadata.ConstantInfoNode";
112+
const char* get_c_struct_name() const override;
113+
inline ::tvm::runtime::String name_hint() const {
114+
return ::tvm::runtime::String(data_->name_hint);
115+
}
116+
inline size_t byte_offset() const { return data_->byte_offset; }
117+
inline ::tvm::runtime::NDArray data() const {
118+
::tvm::runtime::NDArray ndarray;
119+
if (data_->data_len) {
120+
dmlc::MemoryFixedSizeStream bytes(const_cast<void*>(data_->data_bytes), data_->data_len);
121+
ndarray.Load(&bytes);
122+
}
123+
return ndarray;
124+
}
125+
TVM_DECLARE_FINAL_OBJECT_INFO(ConstantInfoMetadataNode, MetadataBaseNode);
126+
127+
protected:
128+
const struct ::TVMConstantInfo* data_;
129+
};
130+
131+
class ConstantInfoMetadata : public MetadataBase {
132+
public:
133+
explicit ConstantInfoMetadata(const struct ::TVMConstantInfo* data);
134+
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(ConstantInfoMetadata, MetadataBase,
135+
ConstantInfoMetadataNode);
136+
};
137+
110138
} // namespace metadata
111139
} // namespace runtime
112140
} // namespace tvm

0 commit comments

Comments
 (0)