d-smirnov commented on a change in pull request #9704:
URL: https://github.com/apache/tvm/pull/9704#discussion_r774522526



##########
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:
       1. This is an overhead added by refs/smartpointer implementation TVM 
has. 
   Simple test:
   ```
   TEST(Deref, MapPtr) {
       using namespace tvm;
       srand(0);
       constexpr auto len = 8192'000;
       runtime::Array<Integer> arr1;
       for( int i = 0; i < len; ++i ) { 
           int key = rand() % 1024;
           arr1.push_back( Integer(key) );
       }   
   #if 1
       {   
           std::unordered_map<const IntImmNode*, Integer> map1;
           for( auto &it: arr1 ) { 
               const IntImmNode *v = it.as<IntImmNode>();
               int key = rand() % 1024;
               map1[v] = key;
           }
       }   
   #endif
   #if 0
       {
           runtime::Map<const Integer, Integer> map1;
           for( const auto &it: arr1 ) {
               const Integer&  v = it;
               int key = rand() % 1024;
               map1.Set(v, key);
           }
       }
   #endif
   }
   
   ```
   ### Results
   
   - Without both maps (just Array): 10431 ms
   - Pointers (first #if/#endif block): 39650 ms
   - Refs (second #if/#endif block): 62837 ms
   
   Profile from Pointers:
   ```
       1 Flat profile:
       2 
       3 Each sample counts as 0.01 seconds.
       4   %   cumulative   self              self     total           
       5  time   seconds   seconds    calls   s/call   s/call  name    
       6  15.01      0.98     0.98       22     0.04     0.06  
std::_Hashtable<tvm::IntImmNode const*, std::pair<tvm::IntImmNode const* const, 
tvm::Integer>, std::allocator<std::pair<tvm::IntImmNode const* const, 
tvm::Integer> >, std::__detail::_Select1st, std::equal_to<tvm::IntImmNode 
const*>, std::hash<tvm::IntImmNode const*>, std::__detail::_Mod_range_hashing, 
std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, 
std::__detail::_Hashtable_traits<false, false, true> >::_M_rehash_aux(unsigned 
long, std::integral_constant<bool, true>)
       7   9.95      1.63     0.65 73751517     0.00     0.00  
tvm::runtime::Object::DecRef()
       8   7.50      2.12     0.49 57379220     0.00     0.00  
tvm::runtime::Object::IncRef()
       9   7.35      2.60     0.48  8192000     0.00     0.00  
std::_Hashtable<tvm::IntImmNode const*, std::pair<tvm::IntImmNode const* const, 
tvm::Integer>, std::allocator<std::pair<tvm::IntImmNode const* const, 
tvm::Integer> >, std::__detail::_Select1st, std::equal_to<tvm::IntImmNode 
const*>, std::hash<tvm::IntImmNode const*>, std::__detail::_Mod_range_hashing, 
std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, 
std::__detail::_Hashtable_traits<false, false, true> 
>::_M_find_before_node(unsigned long, tvm::IntImmNode const* const&, unsigned 
long) const
      10   2.83      2.79     0.19 90132705     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::ObjectPtr(tvm::runtime::ObjectPtr<tvm::runtime::Object>
 const&)
      11   2.60      2.96     0.17 196691087     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::reset()
      12   2.37      3.11     0.16 180301469     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::~ObjectPtr()
      13   2.14      3.25     0.14 40991606     0.00     0.00  
std::enable_if<std::__and_<std::__not_<std::__is_tuple_like<tvm::runtime::Object*>
 >, std::is_move_constructible<tvm::runtime::Object*>, 
std::is_move_assignable<tvm::runtime::Object*> >::value, void>::type 
std::swap<tvm::runtime::Object*>(tvm::runtime::Object*&, tvm::runtime::Object*&)
      14   1.53      3.35     0.10 98331824     0.00     0.00  
tvm::runtime::ObjectRef::~ObjectRef()
      15   1.38      3.44     0.09 31274752     0.00     0.00  
std::__detail::_Mod_range_hashing::operator()(unsigned long, unsigned long) 
const
      16   1.30      3.53     0.09 90134239     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::ObjectPtr(tvm::runtime::Object*)
      17   1.07      3.60     0.07 40991606     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::swap(tvm::runtime::ObjectPtr<tvm::runtime::Object>&)
   ```
   
   Profile from Refs:
   ```
       1 Flat profile:
       2 
       3 Each sample counts as 0.01 seconds.
       4   %   cumulative   self              self     total           
       5  time   seconds   seconds    calls   s/call   s/call  name    
       6  18.52      2.82     2.82 139054448     0.00     0.00  
tvm::runtime::Object::DecRef()
       7  15.56      5.19     2.37 122682151     0.00     0.00  
tvm::runtime::Object::IncRef()
       8  10.57      6.80     1.61 57720991     0.00     0.00  bool 
tvm::runtime::Object::IsInstance<tvm::runtime::StringObj>() const
       9   6.96      7.86     1.06 114637021     0.00     0.00  
tvm::runtime::DenseMapNode::ListNode::IsEmpty() const
      10   5.15      8.65     0.79 32524464     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::operator==(tvm::runtime::ObjectPtr<tvm::runtime::Object>
 const&) const
      11   3.84      9.23     0.59 141855755     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::ObjectPtr(tvm::runtime::ObjectPtr<tvm::runtime::Object>&&)
      12   3.41      9.75     0.52 369380990     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::reset()
      13   2.36     10.11     0.36 179895045     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::ObjectPtr(tvm::runtime::ObjectPtr<tvm::runtime::Object>
 const&)
      14   2.20     10.45     0.34 352991372     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::~ObjectPtr()
      15   1.94     10.74     0.30 179896579     0.00     0.00  
tvm::runtime::ObjectPtr<tvm::runtime::Object>::ObjectPtr(tvm::runtime::Object*)
      16   1.58     10.98     0.24 33747989     0.00     0.00  
tvm::runtime::DenseMapNode::ListNode::MoveToNext(tvm::runtime::DenseMapNode 
const*)
      17   1.25     11.17     0.19 230294812     0.00     0.00  
tvm::runtime::ObjectRef::~ObjectRef()
   ```
   
   2. Usage of const Ref& in Map is prohibited by template expansion rules.




-- 
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: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to