You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

graph_mem_allocator.cc 6.4 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "graph/manager/graph_mem_allocator.h"
  17. #include <string>
  18. namespace ge {
  19. Status MemoryAllocator::Initialize(uint32_t device_id) {
  20. GELOGI("MemoryAllocator::Initialize");
  21. // when redo Initialize free memory
  22. for (auto &it_map : deviceid_2_memory_bases_map_) {
  23. for (auto &it : it_map.second) {
  24. if (FreeMemory(it.second.memory_addr_, device_id) != ge::SUCCESS) {
  25. GELOGW("Initialize: FreeMemory failed");
  26. }
  27. }
  28. it_map.second.clear();
  29. }
  30. deviceid_2_memory_bases_map_.clear();
  31. return SUCCESS;
  32. }
  33. void MemoryAllocator::Finalize(uint32_t device_id) {
  34. GELOGI("MemoryAllocator::Finalize");
  35. // free memory
  36. for (auto &it_map : deviceid_2_memory_bases_map_) {
  37. for (auto &it : it_map.second) {
  38. if (FreeMemory(it.second.memory_addr_, device_id) != ge::SUCCESS) {
  39. GELOGW("Finalize: FreeMemory failed");
  40. }
  41. }
  42. it_map.second.clear();
  43. }
  44. deviceid_2_memory_bases_map_.clear();
  45. }
  46. uint8_t *MemoryAllocator::MallocMemory(const string &purpose, size_t memory_size, uint32_t device_id) const {
  47. uint8_t *memory_addr = nullptr;
  48. if (rtMalloc(reinterpret_cast<void **>(&memory_addr), memory_size, memory_type_) != RT_ERROR_NONE) {
  49. REPORT_CALL_ERROR("E19999", "Call rtMalloc fail, purpose:%s, size:%zu, device_id:%u",
  50. purpose.c_str(), memory_size, device_id);
  51. GELOGE(ge::INTERNAL_ERROR, "[Malloc][Memory] failed, device_id = %u, size= %lu",
  52. device_id, memory_size);
  53. return nullptr;
  54. }
  55. GELOGI("MemoryAllocator::MallocMemory device_id = %u, size= %lu", device_id, memory_size);
  56. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), memory_size)
  57. return memory_addr;
  58. }
  59. Status MemoryAllocator::FreeMemory(uint8_t *memory_addr, uint32_t device_id) const {
  60. GELOGI("MemoryAllocator::FreeMemory device_id = %u", device_id);
  61. auto rtRet = rtFree(memory_addr);
  62. if (rtRet != RT_ERROR_NONE) {
  63. REPORT_CALL_ERROR("E19999", "Call rtFree fail, device_id:%u", device_id);
  64. GELOGE(rtRet, "[Call][RtFree] failed, device_id = %u", device_id);
  65. return RT_ERROR_TO_GE_STATUS(rtRet);
  66. }
  67. memory_addr = nullptr;
  68. return ge::SUCCESS;
  69. }
  70. uint8_t *MemoryAllocator::MallocMemory(const string &purpose, const string &memory_key, size_t memory_size,
  71. uint32_t device_id) {
  72. map<string, MemoryInfo> memory_base_map;
  73. auto it_map = deviceid_2_memory_bases_map_.find(device_id);
  74. if (it_map != deviceid_2_memory_bases_map_.end()) {
  75. memory_base_map = it_map->second;
  76. auto it = it_map->second.find(memory_key);
  77. if (it != it_map->second.end()) {
  78. it->second.memory_used_num_++;
  79. return it->second.memory_addr_;
  80. }
  81. }
  82. uint8_t *memory_addr = MallocMemory(purpose, memory_size, device_id);
  83. if (memory_addr == nullptr) {
  84. REPORT_CALL_ERROR("E19999", "Malloc Memory fail, purpose:%s, memory_key:%s, memory_size:%zu, device_id:%u",
  85. purpose.c_str(), memory_key.c_str(), memory_size, device_id);
  86. GELOGE(ge::INTERNAL_ERROR, "[Malloc][Memory] failed, memory_key[%s], size = %lu, device_id:%u.",
  87. memory_key.c_str(), memory_size, device_id);
  88. return nullptr;
  89. }
  90. MemoryInfo memory_info(memory_addr, memory_size, device_id);
  91. memory_info.memory_used_num_++;
  92. memory_base_map[memory_key] = memory_info;
  93. deviceid_2_memory_bases_map_[device_id] = memory_base_map;
  94. mem_malloced_ = true;
  95. return memory_addr;
  96. }
  97. Status MemoryAllocator::FreeMemory(const string &memory_key, uint32_t device_id) {
  98. auto it_map = deviceid_2_memory_bases_map_.find(device_id);
  99. if (it_map == deviceid_2_memory_bases_map_.end()){
  100. if (mem_malloced_) {
  101. GELOGW(
  102. "MemoryAllocator::FreeMemory failed,"
  103. " memory_key[%s] was not exist, device_id = %u.",
  104. memory_key.c_str(), device_id);
  105. }
  106. return ge::INTERNAL_ERROR;
  107. }
  108. auto it = it_map->second.find(memory_key);
  109. if (it == it_map->second.end()) {
  110. if (mem_malloced_) {
  111. GELOGW(
  112. "MemoryAllocator::FreeMemory failed,"
  113. " memory_key[%s] was not exist, device_id = %u.",
  114. memory_key.c_str(), device_id);
  115. }
  116. return ge::INTERNAL_ERROR;
  117. }
  118. if (it->second.memory_used_num_ > 1) {
  119. GELOGW("MemoryAllocator::FreeMemory memory_key[%s] should not be released, reference count %d", memory_key.c_str(),
  120. it->second.memory_used_num_);
  121. // reference count greater than 1 represnt that static memory is used by
  122. // someone else, reference count decrement
  123. it->second.memory_used_num_--;
  124. return ge::SUCCESS;
  125. }
  126. if (FreeMemory(it->second.memory_addr_, device_id) != ge::SUCCESS) {
  127. REPORT_CALL_ERROR("E19999", "Free Memory fail, memory_key:%s, device_id:%u",
  128. memory_key.c_str(), device_id);
  129. GELOGE(ge::INTERNAL_ERROR, "[Free][Memory] failed, memory_key[%s], device_id:%u",
  130. memory_key.c_str(), device_id);
  131. return ge::INTERNAL_ERROR;
  132. }
  133. GELOGI("MemoryAllocator::FreeMemory device_id = %u", device_id);
  134. it_map->second.erase(it);
  135. return ge::SUCCESS;
  136. }
  137. uint8_t *MemoryAllocator::GetMemoryAddr(const string &memory_key, uint32_t device_id) {
  138. auto it_map = deviceid_2_memory_bases_map_.find(device_id);
  139. if (it_map == deviceid_2_memory_bases_map_.end()) {
  140. GELOGW(
  141. "MemoryAllocator::GetMemoryAddr failed,"
  142. " memory_key[%s] was not exist, device_id = %u.",
  143. memory_key.c_str(), device_id);
  144. return nullptr;
  145. }
  146. auto it = it_map->second.find(memory_key);
  147. if (it == it_map->second.end()) {
  148. GELOGW(
  149. "MemoryAllocator::GetMemoryAddr failed,"
  150. " memory_key[%s] was not exist, device_id = %u.",
  151. memory_key.c_str(), device_id);
  152. return nullptr;
  153. }
  154. return it->second.memory_addr_;
  155. }
  156. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示