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

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