|
| 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 | + |
| 20 | +/*! |
| 21 | + * \file tir/usmp/utils.h |
| 22 | + * \brief Utilities for Unified Static Memory Planner |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef TVM_TIR_USMP_UTILS_H_ |
| 26 | +#define TVM_TIR_USMP_UTILS_H_ |
| 27 | + |
| 28 | +#include <tvm/ir/expr.h> |
| 29 | +#include <tvm/target/target.h> |
| 30 | +#include <tvm/tir/stmt.h> |
| 31 | + |
| 32 | +namespace tvm { |
| 33 | +namespace tir { |
| 34 | +namespace usmp { |
| 35 | + |
| 36 | +/*! |
| 37 | + * \brief The string parameter to indicate read and write access to a pool |
| 38 | + * This needs to be kept in sync with PoolInfo.READ_WRITE_ACCESS in |
| 39 | + * python/tvm/tir/usmp/utils.py |
| 40 | + */ |
| 41 | +static constexpr const char* kTargetPoolReadWriteAccess = "rw"; |
| 42 | +/*! |
| 43 | + * \brief The string parameter to indicate read only access to a pool |
| 44 | + * This needs to be kept in sync with PoolInfo.READ_ONLY_ACCESS in |
| 45 | + * python/tvm/tir/usmp/utils.py |
| 46 | + */ |
| 47 | +static constexpr const char* kTargetPoolReadOnlyAccess = "ro"; |
| 48 | + |
| 49 | +/*! |
| 50 | + * \brief Describes a pool of memory accessible by one or more targets. |
| 51 | + */ |
| 52 | +struct PoolInfoNode : public Object { |
| 53 | + /*! \brief The name of the memory pool */ |
| 54 | + String pool_name; |
| 55 | + /*! \brief The expected size hint to be used by the allocator. |
| 56 | + * The size_hint_bytes is defaulted to kUnrestrictedPoolSizeHint |
| 57 | + * to indicate the pool is not size restricted. |
| 58 | + */ |
| 59 | + Integer size_hint_bytes; |
| 60 | + /*! \brief The accessibility from each Target*/ |
| 61 | + Map<Target, String> target_access; // 'rw' or 'ro' |
| 62 | + |
| 63 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 64 | + v->Visit("pool_name", &pool_name); |
| 65 | + v->Visit("size_hint_bytes", &size_hint_bytes); |
| 66 | + v->Visit("target_access", &target_access); |
| 67 | + } |
| 68 | + |
| 69 | + bool SEqualReduce(const PoolInfoNode* other, SEqualReducer equal) const { |
| 70 | + return equal(pool_name, other->pool_name) && equal(size_hint_bytes, other->size_hint_bytes) && |
| 71 | + equal(target_access, other->target_access); |
| 72 | + } |
| 73 | + |
| 74 | + void SHashReduce(SHashReducer hash_reduce) const { |
| 75 | + hash_reduce(pool_name); |
| 76 | + hash_reduce(size_hint_bytes); |
| 77 | + hash_reduce(target_access); |
| 78 | + } |
| 79 | + |
| 80 | + static constexpr const char* _type_key = "tir.usmp.PoolInfo"; |
| 81 | + TVM_DECLARE_FINAL_OBJECT_INFO(PoolInfoNode, Object); |
| 82 | +}; |
| 83 | + |
| 84 | +/*! |
| 85 | + * \brief The PoolSize is unrestricted for the memory planner |
| 86 | + */ |
| 87 | +static const int kUnrestrictedPoolSizeHint = -1; |
| 88 | + |
| 89 | +class PoolInfo : public ObjectRef { |
| 90 | + public: |
| 91 | + TVM_DLL PoolInfo(String pool_name, Map<Target, String> target_access, |
| 92 | + Integer size_hint_bytes = kUnrestrictedPoolSizeHint); |
| 93 | + TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(PoolInfo, ObjectRef, PoolInfoNode); |
| 94 | +}; |
| 95 | + |
| 96 | +/*! |
| 97 | + * \brief Describes an abstract memory buffer that will get allocated inside a pool. |
| 98 | + * The actual memory buffer in represented by PoolAllocationNode after static memory planning. |
| 99 | + * |
| 100 | + * See also for relay-level counterparts: |
| 101 | + * relay::StorageToken (graph_plan_memory.cc) |
| 102 | + * relay::backend::StorageInfoNode (relay/backend/utils.h) |
| 103 | + * Region (python/tvm/relay/transform/memory_plan.py) |
| 104 | + */ |
| 105 | +struct BufferInfoNode : public Object { |
| 106 | + /*! \brief The name of the buffer var */ |
| 107 | + String name_hint; |
| 108 | + /*! \brief The size in terms of bytes */ |
| 109 | + Integer size_bytes; |
| 110 | + /*! \brief The pool candidates that this buffer can get pooled to*/ |
| 111 | + Array<PoolInfo> pool_candidates; |
| 112 | + /*! \brief The byte alignment required for buffers that will placed within the pool */ |
| 113 | + Integer alignment; |
| 114 | + /*! \brief The liveness conflicting other buffer info objects */ |
| 115 | + Array<ObjectRef> conflicts; |
| 116 | + |
| 117 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 118 | + v->Visit("name_hint", &name_hint); |
| 119 | + v->Visit("size_bytes", &size_bytes); |
| 120 | + v->Visit("pool_candidates", &pool_candidates); |
| 121 | + v->Visit("alignment", &alignment); |
| 122 | + v->Visit("conflicts", &conflicts); |
| 123 | + } |
| 124 | + |
| 125 | + bool SEqualReduce(const BufferInfoNode* other, SEqualReducer equal) const { |
| 126 | + return equal(name_hint, other->name_hint) && equal(size_bytes, other->size_bytes) && |
| 127 | + equal(pool_candidates, other->pool_candidates) && equal(alignment, other->alignment) && |
| 128 | + equal(conflicts, other->conflicts); |
| 129 | + } |
| 130 | + |
| 131 | + void SHashReduce(SHashReducer hash_reduce) const { |
| 132 | + hash_reduce(name_hint); |
| 133 | + hash_reduce(size_bytes); |
| 134 | + hash_reduce(alignment); |
| 135 | + hash_reduce(conflicts); |
| 136 | + hash_reduce(pool_candidates); |
| 137 | + } |
| 138 | + /*! |
| 139 | + * \brief Set the liveness conflicts of this BufferInfo |
| 140 | + * |
| 141 | + * \param conflicting_buffer_info_objs An array of BufferInfo that conflicts in liveness |
| 142 | + */ |
| 143 | + TVM_DLL void SetConflicts(Array<ObjectRef> conflicting_buffer_info_objs); |
| 144 | + |
| 145 | + static constexpr const char* _type_key = "tir.usmp.BufferInfo"; |
| 146 | + TVM_DECLARE_FINAL_OBJECT_INFO(BufferInfoNode, Object); |
| 147 | +}; |
| 148 | + |
| 149 | +class BufferInfo : public ObjectRef { |
| 150 | + public: |
| 151 | + TVM_DLL BufferInfo(String name_hint, Integer size_bytes, Array<PoolInfo> pool_candidates, |
| 152 | + Integer alignment = runtime::kDefaultWorkspaceAlignment); |
| 153 | + TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(BufferInfo, ObjectRef, BufferInfoNode); |
| 154 | +}; |
| 155 | + |
| 156 | +/*! |
| 157 | + * \brief The pool allocation produced after the USMP algorithm |
| 158 | + */ |
| 159 | +struct PoolAllocationNode : public Object { |
| 160 | + /*! \brief The assigned PoolInfo object */ |
| 161 | + PoolInfo pool_info; |
| 162 | + /*! \brief The byte offset where the tensor is supposed to be placed within the pool*/ |
| 163 | + Integer byte_offset; |
| 164 | + |
| 165 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 166 | + v->Visit("pool_info", &pool_info); |
| 167 | + v->Visit("byte_offset", &byte_offset); |
| 168 | + } |
| 169 | + |
| 170 | + bool SEqualReduce(const PoolAllocationNode* other, SEqualReducer equal) const { |
| 171 | + return equal(pool_info, other->pool_info) && equal(byte_offset, other->byte_offset); |
| 172 | + } |
| 173 | + |
| 174 | + void SHashReduce(SHashReducer hash_reduce) const { |
| 175 | + hash_reduce(pool_info); |
| 176 | + hash_reduce(byte_offset); |
| 177 | + } |
| 178 | + |
| 179 | + static constexpr const char* _type_key = "tir.usmp.PoolAllocation"; |
| 180 | + TVM_DECLARE_FINAL_OBJECT_INFO(PoolAllocationNode, Object); |
| 181 | +}; |
| 182 | + |
| 183 | +class PoolAllocation : public ObjectRef { |
| 184 | + public: |
| 185 | + TVM_DLL PoolAllocation(PoolInfo pool_info, Integer byte_offset); |
| 186 | + TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(PoolAllocation, ObjectRef, PoolAllocationNode); |
| 187 | +}; |
| 188 | + |
| 189 | +/*! |
| 190 | + * \brief Convert the IR-bound BufferInfo map to an array of BufferInfo |
| 191 | + * |
| 192 | + * \param buffer_info_map IR-bound BufferInfo map |
| 193 | + */ |
| 194 | +Array<BufferInfo> CreateArrayBufferInfo(const Map<Stmt, BufferInfo>& buffer_info_map); |
| 195 | + |
| 196 | +/*! |
| 197 | + * \brief The allocate node attribute to indicate candidate memory pools. |
| 198 | + * This needs to be kept in sync with CANDIDATE_MEMORY_POOL_ATTR in |
| 199 | + * python/tvm/tir/usmp/utils.py. |
| 200 | + */ |
| 201 | +static constexpr const char* kPoolCandidatesAllocateAttr = "candidate_memory_pools"; |
| 202 | + |
| 203 | +/*! |
| 204 | + * \brief Calculate the size of the extents in bytes |
| 205 | + * |
| 206 | + * \param op the allocate node |
| 207 | + */ |
| 208 | +Integer CalculateExtentsSize(const AllocateNode* op); |
| 209 | + |
| 210 | +} // namespace usmp |
| 211 | +} // namespace tir |
| 212 | +} // namespace tvm |
| 213 | + |
| 214 | +#endif // TVM_TIR_USMP_UTILS_H_ |
0 commit comments