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 5.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 : memory_base_map_) {
  23. if (FreeMemory(it.second.memory_addr_, device_id) != ge::SUCCESS) {
  24. GELOGW("Initialize: FreeMemory failed");
  25. }
  26. }
  27. memory_base_map_.clear();
  28. return SUCCESS;
  29. }
  30. void MemoryAllocator::Finalize(uint32_t device_id) {
  31. GELOGI("MemoryAllocator::Finalize");
  32. // free memory
  33. for (auto &it : memory_base_map_) {
  34. if (FreeMemory(it.second.memory_addr_, device_id) != ge::SUCCESS) {
  35. GELOGW("Finalize: FreeMemory failed");
  36. }
  37. }
  38. memory_base_map_.clear();
  39. }
  40. uint8_t *MemoryAllocator::MallocMemory(const string &purpose, size_t memory_size, uint32_t device_id) const {
  41. uint8_t *memory_addr = nullptr;
  42. if (rtMalloc(reinterpret_cast<void **>(&memory_addr), memory_size, memory_type_) != RT_ERROR_NONE) {
  43. REPORT_CALL_ERROR("E19999", "Call rtMalloc fail, purpose:%s, size:%zu, device_id:%u",
  44. purpose.c_str(), memory_size, device_id);
  45. GELOGE(ge::INTERNAL_ERROR,
  46. "MemoryAllocator::MallocMemory device_id = %u,"
  47. " size= %lu",
  48. device_id, memory_size);
  49. return nullptr;
  50. }
  51. GELOGI("MemoryAllocator::MallocMemory device_id = %u, size= %lu", device_id, memory_size);
  52. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), memory_size)
  53. return memory_addr;
  54. }
  55. Status MemoryAllocator::FreeMemory(uint8_t *memory_addr, uint32_t device_id) const {
  56. GELOGI("MemoryAllocator::FreeMemory device_id = %u", device_id);
  57. auto rtRet = rtFree(memory_addr);
  58. if (rtRet != RT_ERROR_NONE) {
  59. REPORT_CALL_ERROR("E19999", "Call rtFree fail, device_id:%u", device_id);
  60. GELOGE(rtRet, "MemoryAllocator::MallocMemory device_id = %u", device_id);
  61. return RT_ERROR_TO_GE_STATUS(rtRet);
  62. }
  63. memory_addr = nullptr;
  64. return ge::SUCCESS;
  65. }
  66. uint8_t *MemoryAllocator::MallocMemory(const string &purpose, const string &memory_key, size_t memory_size,
  67. uint32_t device_id) {
  68. auto it = memory_base_map_.find(memory_key);
  69. if (it != memory_base_map_.end()) {
  70. it->second.memory_used_num_++;
  71. return it->second.memory_addr_;
  72. }
  73. uint8_t *memory_addr = MallocMemory(purpose, memory_size, device_id);
  74. if (memory_addr == nullptr) {
  75. REPORT_CALL_ERROR("E19999", "Malloc Memory fail, purpose:%s, memory_key:%s, memory_size:%zu, device_id:%u",
  76. purpose.c_str(), memory_key.c_str(), memory_size, device_id);
  77. GELOGE(ge::INTERNAL_ERROR,
  78. "MemoryAllocator::MallocMemory failed,"
  79. " memory_key[%s], size = %lu.",
  80. memory_key.c_str(), memory_size);
  81. return nullptr;
  82. }
  83. MemoryInfo memory_info(memory_addr, memory_size);
  84. memory_info.memory_used_num_++;
  85. memory_base_map_[memory_key] = memory_info;
  86. mem_malloced_ = true;
  87. return memory_addr;
  88. }
  89. Status MemoryAllocator::FreeMemory(const string &memory_key, uint32_t device_id) {
  90. auto it = memory_base_map_.find(memory_key);
  91. if (it == memory_base_map_.end()) {
  92. if (mem_malloced_) {
  93. GELOGW(
  94. "MemoryAllocator::FreeMemory failed,"
  95. " memory_key[%s] was not exist, device_id = %u.",
  96. memory_key.c_str(), device_id);
  97. }
  98. return ge::INTERNAL_ERROR;
  99. }
  100. if (it->second.memory_used_num_ > 1) {
  101. GELOGW("MemoryAllocator::FreeMemory memory_key[%s] should not be released, reference count %d", memory_key.c_str(),
  102. it->second.memory_used_num_);
  103. // reference count greater than 1 represnt that static memory is used by
  104. // someone else, reference count decrement
  105. it->second.memory_used_num_--;
  106. return ge::SUCCESS;
  107. }
  108. if (FreeMemory(it->second.memory_addr_, device_id) != ge::SUCCESS) {
  109. REPORT_CALL_ERROR("E19999", "Free Memory fail, memory_key:%s, device_id:%u",
  110. memory_key.c_str(), device_id);
  111. GELOGE(ge::INTERNAL_ERROR,
  112. "MemoryAllocator::FreeMemory rtFree failed,"
  113. " memory_key[%s]",
  114. memory_key.c_str());
  115. return ge::INTERNAL_ERROR;
  116. }
  117. GELOGI("MemoryAllocator::FreeMemory device_id = %u", device_id);
  118. memory_base_map_.erase(it);
  119. return ge::SUCCESS;
  120. }
  121. uint8_t *MemoryAllocator::GetMemoryAddr(const string &memory_key, uint32_t device_id) {
  122. auto it = memory_base_map_.find(memory_key);
  123. if (it == memory_base_map_.end()) {
  124. GELOGW(
  125. "MemoryAllocator::GetMemoryAddr failed,"
  126. " memory_key[%s] was not exist, device_id = %u.",
  127. memory_key.c_str(), device_id);
  128. return nullptr;
  129. }
  130. return it->second.memory_addr_;
  131. }
  132. } // namespace ge

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