manupa-arm commented on a change in pull request #9704:
URL: https://github.com/apache/tvm/pull/9704#discussion_r772590797
##########
File path: src/tir/usmp/algo/greedy.cc
##########
@@ -39,118 +39,14 @@
#include <tvm/tir/builtin.h>
#include <tvm/tir/function.h>
#include <tvm/tir/stmt_functor.h>
+#include <tvm/tir/usmp/algo/greedy.h>
#include <tvm/tir/usmp/utils.h>
namespace tvm {
namespace tir {
namespace usmp {
namespace algo {
-/*!
- * \brief This is the base class for Greedy Algorithms where the sorting
- * is specialized in the extended classes based on the greedy criteria.
- */
-class GreedyBase {
Review comment:
I think we should only move the declaration of the class to the header
and we should keep the definition here.
##########
File path: src/tir/usmp/algo/hill_climb.cc
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/usmp/algo/hill_climb.cc
+ * \brief Implement greedy by size memory planning algorithm
+ */
+#include <tvm/arith/analyzer.h>
+#include <tvm/runtime/device_api.h>
+#include <tvm/tir/builtin.h>
+#include <tvm/tir/function.h>
+#include <tvm/tir/stmt_functor.h>
+#include <tvm/tir/usmp/algo/greedy.h>
+#include <tvm/tir/usmp/utils.h>
+
+#include <algorithm>
+#include <numeric>
+#include <sstream>
+
+namespace tvm {
+namespace tir {
+namespace usmp {
+namespace algo {
+
+/*
+ * Simulated annealing / Hill climb
+ *
+ * Works by continiously invoking 'greedy-by-size' allocation,
+ * assessing the result, and introducing permutations to the allocation
+ * order which hopefully will led to more 'compact' memory allocation.
+ */
+class HillClimbAllocator : public GreedyBase {
+ private:
+ size_t memory_pressure_ = 0;
+
+ public:
+ explicit HillClimbAllocator(size_t memory_pressure)
+ : GreedyBase(), memory_pressure_(memory_pressure) {}
+
+ protected:
+ using alloc_map_t = std::unordered_map<const BufferInfoNode*,
PoolAllocation>;
+
+ /*
+ * Initial sorting routine
+ */
+ void sort_vector(std::vector<BufferInfo>* buffer_info_vec) {
+ std::sort(buffer_info_vec->begin(), buffer_info_vec->end(),
+ [](const BufferInfo& a, const BufferInfo& b) {
+ if (a->size_bytes->value == b->size_bytes->value) {
+ if (a->conflicts.size() == b->conflicts.size()) {
+ auto a_name_hash =
std::hash<std::string>{}(a->name_hint->data);
+ auto b_name_hash =
std::hash<std::string>{}(b->name_hint->data);
+ return a_name_hash > b_name_hash;
+ } else {
+ return a->conflicts.size() > b->conflicts.size();
+ }
+ }
+ return a->size_bytes->value > b->size_bytes->value;
+ });
+ }
+
+ /*
+ * HillClimb's version of greedy allocation
+ * \param buffer_info_vec - buffers in specific order for allocation
+ */
+ alloc_map_t greedy(const std::vector<BufferInfo>& buffer_info_vec) {
+ alloc_map_t pool_allocations(buffer_info_vec.size());
+ for (const auto& buf_info : buffer_info_vec) {
+ std::unordered_map<PoolInfo, size_t, ObjectPtrHash, ObjectPtrEqual>
pool_offset_candidates;
+ for (const auto& pool_info : buf_info->pool_candidates) {
+ if (IsValidPlacement(pool_info, 0, buf_info->size_bytes->value)) {
+ pool_offset_candidates[pool_info] = 0;
+ }
+ }
+
+ std::vector<const BufferInfoNode*> buf_conf;
+ for (const auto& conflict_buf_info_obj : buf_info->conflicts) {
+ const BufferInfoNode* conflict_buf_info =
conflict_buf_info_obj.as<BufferInfoNode>();
+ if (pool_allocations.end() !=
pool_allocations.find(conflict_buf_info)) {
+ buf_conf.push_back(conflict_buf_info);
+ }
+ }
+
+ // extra sorting for pool offsets
+ std::sort(buf_conf.begin(), buf_conf.end(),
+ [&pool_allocations](const auto* a, const auto* b) {
+ return pool_allocations[a]->byte_offset->value <
+ pool_allocations[b]->byte_offset->value;
+ });
+
+ for (const auto* conflict_buf_info : buf_conf) {
Review comment:
This part looks very similiar to Would it be possible to use
https://github.com/apache/tvm/blob/0274b951ca02244ac07c666d843bb87f5dbc29c7/src/tir/usmp/algo/greedy.cc#L112,
Would we be able to re-use that code ?
##########
File path: src/tir/usmp/algo/hill_climb.cc
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/usmp/algo/hill_climb.cc
+ * \brief Implement greedy by size memory planning algorithm
+ */
+#include <tvm/arith/analyzer.h>
+#include <tvm/runtime/device_api.h>
+#include <tvm/tir/builtin.h>
+#include <tvm/tir/function.h>
+#include <tvm/tir/stmt_functor.h>
+#include <tvm/tir/usmp/algo/greedy.h>
+#include <tvm/tir/usmp/utils.h>
+
+#include <algorithm>
+#include <numeric>
+#include <sstream>
+
+namespace tvm {
+namespace tir {
+namespace usmp {
+namespace algo {
+
+/*
+ * Simulated annealing / Hill climb
+ *
+ * Works by continiously invoking 'greedy-by-size' allocation,
+ * assessing the result, and introducing permutations to the allocation
+ * order which hopefully will led to more 'compact' memory allocation.
+ */
+class HillClimbAllocator : public GreedyBase {
+ private:
+ size_t memory_pressure_ = 0;
+
+ public:
+ explicit HillClimbAllocator(size_t memory_pressure)
+ : GreedyBase(), memory_pressure_(memory_pressure) {}
+
+ protected:
+ using alloc_map_t = std::unordered_map<const BufferInfoNode*,
PoolAllocation>;
+
+ /*
+ * Initial sorting routine
+ */
+ void sort_vector(std::vector<BufferInfo>* buffer_info_vec) {
+ std::sort(buffer_info_vec->begin(), buffer_info_vec->end(),
+ [](const BufferInfo& a, const BufferInfo& b) {
+ if (a->size_bytes->value == b->size_bytes->value) {
+ if (a->conflicts.size() == b->conflicts.size()) {
+ auto a_name_hash =
std::hash<std::string>{}(a->name_hint->data);
+ auto b_name_hash =
std::hash<std::string>{}(b->name_hint->data);
+ return a_name_hash > b_name_hash;
+ } else {
+ return a->conflicts.size() > b->conflicts.size();
+ }
+ }
+ return a->size_bytes->value > b->size_bytes->value;
+ });
+ }
+
+ /*
+ * HillClimb's version of greedy allocation
Review comment:
It is better if explain the difference versus the original greedy as
there will be two implementations of this in the codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]