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.

session_scope_mem_allocator.cc 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/session_scope_mem_allocator.h"
  17. #include <set>
  18. #include <string>
  19. #include <utility>
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/manager/graph_mem_manager.h"
  22. namespace ge {
  23. SessionScopeMemAllocator::SessionScopeMemAllocator(rtMemType_t memory_type)
  24. : memory_type_(memory_type), memory_allocator_(nullptr) {}
  25. Status SessionScopeMemAllocator::Initialize(uint32_t device_id) {
  26. GELOGI("Device id %u", device_id);
  27. // when redo Initialize free old memory
  28. FreeAllMemory();
  29. std::lock_guard<std::recursive_mutex> lock(mutex_);
  30. memory_allocator_ = &MemManager::Instance().MemInstance(memory_type_);
  31. if (memory_allocator_ == nullptr) {
  32. return ACL_ERROR_GE_INTERNAL_ERROR;
  33. }
  34. return ge::SUCCESS;
  35. }
  36. void SessionScopeMemAllocator::Finalize(uint32_t device_id) {
  37. GELOGI("Device id %u", device_id);
  38. FreeAllMemory();
  39. }
  40. uint8_t *SessionScopeMemAllocator::Malloc(size_t size, uint64_t session_id, uint32_t device_id) {
  41. GELOGI("Start malloc memory, size:%zu, session id:%lu device id:%u", size, session_id, device_id);
  42. const std::string purpose = "Memory for session scope.";
  43. auto ptr = memory_allocator_->MallocMemory(purpose, size, device_id);
  44. if (ptr == nullptr) {
  45. GELOGE(ge::FAILED, "Malloc failed, no enough memory for size:%zu, session_id:%lu device_id:%u", size,
  46. session_id, device_id);
  47. return nullptr;
  48. }
  49. std::lock_guard<std::recursive_mutex> lock(mutex_);
  50. std::shared_ptr<uint8_t> mem_ptr(ptr, [&](uint8_t *p) { (void)memory_allocator_->FreeMemory(p); });
  51. allocated_memory_[session_id].emplace_back(size, mem_ptr);
  52. return ptr;
  53. }
  54. Status SessionScopeMemAllocator::Free(uint64_t session_id, uint32_t device_id) {
  55. GELOGI("Free session:%lu memory, device id:%u.", session_id, device_id);
  56. std::lock_guard<std::recursive_mutex> lock(mutex_);
  57. auto it = allocated_memory_.find(session_id);
  58. if (it == allocated_memory_.end()) {
  59. REPORT_INNER_ERROR("E19999", "Param memory not allocated before, session_id:%lu device_id:%u, check invalid",
  60. session_id, device_id);
  61. GELOGE(PARAM_INVALID, "Invalid session_id");
  62. return ge::PARAM_INVALID;
  63. }
  64. allocated_memory_.erase(it);
  65. return ge::SUCCESS;
  66. }
  67. void SessionScopeMemAllocator::FreeAllMemory() {
  68. GELOGI("Free all memory");
  69. std::lock_guard<std::recursive_mutex> lock(mutex_);
  70. for (auto &session_mem : allocated_memory_) {
  71. session_mem.second.clear();
  72. }
  73. allocated_memory_.clear();
  74. }
  75. } // namespace ge

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