Skip to content

Commit 26fffb0

Browse files
committed
[TIR][USMP] Greedy algorithms for USMP
This commit implements greedy algorithms for USMP based on size and number of liveness conflicts. * This includes a slight fix for buffer info extraction where non-linear network buffers owned by the main function should not show sporadic liveness. Change-Id: I957d543e75b3b0bcf5fc1fbc7870705c875c7a03
1 parent e0ca1d9 commit 26fffb0

File tree

5 files changed

+746
-441
lines changed

5 files changed

+746
-441
lines changed

src/tir/usmp/algo/greedy.cc

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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/analysis/usmp/algo/greedy_by_size.cc
22+
* \brief This source contains greedy algorithms for planning
23+
* memory for USMP. There are two algorithms present here :
24+
* 1) greedy_by_size and 2) greedy_by_conflicts.
25+
*
26+
* greedy_by_size : this algorithm prioritizes placing the
27+
* largest size buffer to the given pools. The BufferInfo objects
28+
* are sorted based on the size and placed on each pool adhering
29+
* to size_hint constraint.
30+
*
31+
* greedy_by_conflicts : this algorithm prioritizes placing the
32+
* the most liveness conflicted buffer to the given pools. The
33+
* BufferInfo objects are sorted based on the number of conflicts
34+
* and placed on each pool adhering to size_hint constraint.
35+
*/
36+
37+
#include <tvm/arith/analyzer.h>
38+
#include <tvm/runtime/device_api.h>
39+
#include <tvm/tir/builtin.h>
40+
#include <tvm/tir/function.h>
41+
#include <tvm/tir/stmt_functor.h>
42+
#include <tvm/tir/usmp/utils.h>
43+
44+
namespace tvm {
45+
namespace tir {
46+
namespace usmp {
47+
namespace algo {
48+
49+
/*!
50+
* \brief This is the base class for Greedy Algorithms where the sorting
51+
* is specialized in the extended classes based on the greedy criteria.
52+
*/
53+
class GreedyBase {
54+
public:
55+
GreedyBase() {}
56+
/*!
57+
* \brief This function should be implemented by the extended classes to sort the BufferInfo
58+
* objects based on a criteria and then calling PostSortAllocation.
59+
*/
60+
virtual Map<BufferInfo, PoolAllocation> PlanMemory(const Array<BufferInfo>& buffer_info_arr) = 0;
61+
62+
protected:
63+
/*!
64+
* \brief Rounds up the offset to satisfy the alignement requirement
65+
*/
66+
size_t round_up_to_byte_alignment(const size_t& non_aligned_byte_offset,
67+
const int& byte_alignment) {
68+
return ((non_aligned_byte_offset + byte_alignment - 1) / byte_alignment) * byte_alignment;
69+
}
70+
71+
/*!
72+
* \brief A helper function check whether a offset is valid given the constraints
73+
*/
74+
bool IsValidPlacement(const PoolInfo& candidate_pool, const size_t& next_offset,
75+
const size_t& size_bytes) {
76+
if (candidate_pool->size_hint_bytes == -1) {
77+
// this means pool is not bounded
78+
return true;
79+
}
80+
auto pool_size = static_cast<size_t>(candidate_pool->size_hint_bytes->value);
81+
auto max_address = next_offset + size_bytes;
82+
if (max_address <= pool_size) {
83+
return true;
84+
}
85+
return false;
86+
}
87+
88+
/*!
89+
* \brief Selects a pool for placement in the given set of ordered pool candidates
90+
*/
91+
PoolInfo SelectPlacementPool(
92+
const Array<PoolInfo>& pool_candidates,
93+
const std::unordered_map<PoolInfo, size_t, ObjectPtrHash, ObjectPtrEqual>& pool_offsets) {
94+
// Here the pool candidates are ordered when it is consumed by the algorithm.
95+
// This could be from order the user has specified. However, schedulers are
96+
// welcome to change the order for performance reasons.
97+
for (const auto& pool_info : pool_candidates) {
98+
if (pool_offsets.count(pool_info)) {
99+
return pool_info;
100+
}
101+
}
102+
ICHECK(false) << "TVM USMP Internal Error: no candidate have been selected!";
103+
return PoolInfo();
104+
}
105+
106+
/*!
107+
* \brief This is the base allocation function that works on sorted BufferInfo objects based
108+
* on the greedy heuristic. The sorting algorithm has to be called before calling this.
109+
*/
110+
Map<BufferInfo, PoolAllocation> PostSortAllocation(
111+
const std::vector<BufferInfo>& buffer_info_vec) {
112+
Map<BufferInfo, PoolAllocation> pool_allocations;
113+
for (const auto& buf_info : buffer_info_vec) {
114+
std::unordered_map<PoolInfo, size_t, ObjectPtrHash, ObjectPtrEqual> pool_offset_candidates;
115+
for (const auto& pool_info : buf_info->pool_candidates) {
116+
// Mark pool candidates that satisfy the size constraints.
117+
if (IsValidPlacement(pool_info, 0, buf_info->size_bytes->value)) {
118+
pool_offset_candidates[pool_info] = 0;
119+
}
120+
}
121+
122+
for (const auto& conflict_buf_info_obj : buf_info->conflicts) {
123+
auto conflict_buf_info = Downcast<BufferInfo>(conflict_buf_info_obj);
124+
size_t next_offset = 0;
125+
// We only look at already allocated BufferInfo in-terms of conflicts.
126+
if (pool_allocations.count(conflict_buf_info)) {
127+
auto pool_allocation = pool_allocations[conflict_buf_info];
128+
next_offset = pool_allocation->byte_offset + conflict_buf_info->size_bytes;
129+
next_offset =
130+
round_up_to_byte_alignment(next_offset, conflict_buf_info->alignment->value);
131+
// Checks whether the next offset in the same pool as the conflicting BufferInfo is valid.
132+
if (IsValidPlacement(pool_allocation->pool_info, next_offset,
133+
buf_info->size_bytes->value)) {
134+
// There could be multiple conflicting BufferInfo in the same pool.
135+
// Thus, we need to make sure we pick the largest offset of them all.
136+
if (next_offset > pool_offset_candidates[pool_allocation->pool_info]) {
137+
pool_offset_candidates[pool_allocation->pool_info] = next_offset;
138+
}
139+
} else {
140+
pool_offset_candidates.erase(pool_allocation->pool_info);
141+
}
142+
}
143+
}
144+
auto selected_pool = SelectPlacementPool(buf_info->pool_candidates, pool_offset_candidates);
145+
pool_allocations.Set(
146+
buf_info, PoolAllocation(selected_pool, Integer(pool_offset_candidates[selected_pool])));
147+
}
148+
return pool_allocations;
149+
}
150+
};
151+
152+
/*!
153+
* \brief This class implements Greedy by the size of BufferInfo
154+
* greedy algorithm. Please refer to main documentation of the file
155+
* for more details.
156+
*/
157+
class GreedySize : public GreedyBase {
158+
public:
159+
GreedySize() {}
160+
Map<BufferInfo, PoolAllocation> PlanMemory(const Array<BufferInfo>& buffer_info_arr) {
161+
std::vector<BufferInfo> buffer_info_vec;
162+
Map<BufferInfo, PoolAllocation> pool_allocations;
163+
for (const auto& buffer_info : buffer_info_arr) {
164+
buffer_info_vec.push_back(std::move(buffer_info));
165+
}
166+
std::sort(buffer_info_vec.begin(), buffer_info_vec.end(),
167+
[](const BufferInfo& a, const BufferInfo& b) {
168+
if (a->size_bytes->value == b->size_bytes->value) {
169+
if (a->conflicts.size() == b->conflicts.size()) {
170+
auto a_name_hash = std::hash<std::string>{}(a->name_hint->data);
171+
auto b_name_hash = std::hash<std::string>{}(b->name_hint->data);
172+
return a_name_hash > b_name_hash;
173+
} else {
174+
return a->conflicts.size() > b->conflicts.size();
175+
}
176+
}
177+
return a->size_bytes > b->size_bytes;
178+
});
179+
return PostSortAllocation(buffer_info_vec);
180+
}
181+
};
182+
183+
/*!
184+
* \brief This class implements Greedy by the number of conflicts of
185+
* BufferInfo greedy algorithm. Please refer to main documentation
186+
* of the file for more details.
187+
*/
188+
class GreedyConflicts : public GreedyBase {
189+
public:
190+
GreedyConflicts() {}
191+
Map<BufferInfo, PoolAllocation> PlanMemory(const Array<BufferInfo>& buffer_info_arr) {
192+
std::vector<BufferInfo> buffer_info_vec;
193+
Map<BufferInfo, PoolAllocation> pool_allocations;
194+
for (const auto& buffer_info : buffer_info_arr) {
195+
buffer_info_vec.push_back(std::move(buffer_info));
196+
}
197+
std::sort(buffer_info_vec.begin(), buffer_info_vec.end(),
198+
[](const BufferInfo& a, const BufferInfo& b) {
199+
if (a->conflicts.size() == b->conflicts.size()) {
200+
if (a->size_bytes->value == b->size_bytes->value) {
201+
auto a_name_hash = std::hash<std::string>{}(a->name_hint->data);
202+
auto b_name_hash = std::hash<std::string>{}(b->name_hint->data);
203+
return a_name_hash > b_name_hash;
204+
} else {
205+
return a->size_bytes->value > b->size_bytes->value;
206+
}
207+
}
208+
return a->conflicts.size() > b->conflicts.size();
209+
});
210+
return PostSortAllocation(buffer_info_vec);
211+
}
212+
};
213+
214+
Map<BufferInfo, PoolAllocation> GreedyBySize(const Array<BufferInfo>& buffer_info_arr) {
215+
return GreedySize().PlanMemory(buffer_info_arr);
216+
}
217+
218+
Map<BufferInfo, PoolAllocation> GreedyByConflicts(const Array<BufferInfo>& buffer_info_arr) {
219+
return GreedyConflicts().PlanMemory(buffer_info_arr);
220+
}
221+
222+
TVM_REGISTER_GLOBAL("tir.usmp.algo.greedy_by_size")
223+
.set_body_typed([](Array<BufferInfo> buffer_info_arr) {
224+
return GreedyBySize(buffer_info_arr);
225+
});
226+
227+
TVM_REGISTER_GLOBAL("tir.usmp.algo.greedy_by_conflicts")
228+
.set_body_typed([](Array<BufferInfo> buffer_info_arr) {
229+
return GreedyByConflicts(buffer_info_arr);
230+
});
231+
232+
} // namespace algo
233+
} // namespace usmp
234+
} // namespace tir
235+
} // namespace tvm

src/tir/usmp/algo/greedy_by_size.cc

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)