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.h 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifndef GE_GRAPH_MANAGER_SESSION_SCOPE_MEM_ALLOCATOR_H_
  17. #define GE_GRAPH_MANAGER_SESSION_SCOPE_MEM_ALLOCATOR_H_
  18. #include <iostream>
  19. #include <map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. #include <vector>
  24. #include <unordered_map>
  25. #include <functional>
  26. #include "framework/common/ge_inner_error_codes.h"
  27. #include "graph/node.h"
  28. #include "graph/manager/block_memory.h"
  29. #include "runtime/mem.h"
  30. #include "graph/manager/graph_mem_allocator.h"
  31. namespace ge {
  32. class SessionScopeMemoryInfo {
  33. public:
  34. SessionScopeMemoryInfo(size_t size, const std::shared_ptr<uint8_t> &ptr) : size(size), ptr(ptr) {}
  35. SessionScopeMemoryInfo() = delete;
  36. virtual ~SessionScopeMemoryInfo() = default;
  37. SessionScopeMemoryInfo(const SessionScopeMemoryInfo &other) {
  38. if (&other == this) {
  39. return;
  40. }
  41. size = other.size;
  42. ptr = other.ptr;
  43. };
  44. SessionScopeMemoryInfo &operator=(const SessionScopeMemoryInfo &other) {
  45. if (&other == this) {
  46. return *this;
  47. }
  48. size = other.size;
  49. ptr = other.ptr;
  50. return *this;
  51. };
  52. private:
  53. size_t size = 0;
  54. std::shared_ptr<uint8_t> ptr = nullptr;
  55. };
  56. class SessionScopeMemAllocator {
  57. public:
  58. explicit SessionScopeMemAllocator(rtMemType_t memory_type);
  59. SessionScopeMemAllocator(const SessionScopeMemAllocator &) = delete;
  60. SessionScopeMemAllocator &operator=(const SessionScopeMemAllocator &) = delete;
  61. virtual ~SessionScopeMemAllocator() = default;
  62. ///
  63. /// @ingroup ge_graph
  64. /// @brief caching allocator init
  65. /// @param [in] device id
  66. /// @return Status of init
  67. ///
  68. Status Initialize(uint32_t device_id = 0);
  69. ///
  70. /// @ingroup ge_graph
  71. /// @brief memory allocator finalize, release all memory
  72. /// @return void
  73. ///
  74. void Finalize(uint32_t device_id = 0);
  75. ///
  76. /// @ingroup ge_graph
  77. /// @brief malloc memory
  78. /// @param [in] size memory size
  79. /// @param [in] session_id session id
  80. /// @param [in] device id
  81. /// @return memory address
  82. ///
  83. uint8_t *Malloc(size_t size, uint64_t session_id, uint32_t device_id = 0);
  84. ///
  85. /// @ingroup ge_graph
  86. /// @brief free memory
  87. /// @param [in] session_id session id
  88. /// @param [in] device_id device id
  89. /// @return Status result of function
  90. ///
  91. Status Free(uint64_t session_id, uint32_t device_id = 0);
  92. private:
  93. void FreeAllMemory();
  94. private:
  95. rtMemType_t memory_type_;
  96. // device memory allocator
  97. MemoryAllocator *memory_allocator_;
  98. // lock around all operations
  99. mutable std::recursive_mutex mutex_;
  100. // allocated blocks by memory pointer
  101. std::unordered_map<uint64_t, std::vector<SessionScopeMemoryInfo>> allocated_memory_;
  102. };
  103. } // namespace ge
  104. #endif // GE_GRAPH_MANAGER_SESSION_SCOPE_MEM_ALLOCATOR_H_

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