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.

tbe_handle_store.cc 4.5 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright 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 "tbe_handle_store.h"
  17. #include <limits>
  18. #include "common/ge_inner_error_codes.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "runtime/kernel.h"
  21. namespace ge {
  22. void TbeHandleInfo::used_inc(uint32_t num) {
  23. if (used_ > std::numeric_limits<uint32_t>::max() - num) {
  24. REPORT_INNER_ERROR("E19999", "Used:%u reach numeric max", used_);
  25. GELOGE(INTERNAL_ERROR, "Used[%u] reach numeric max.", used_);
  26. return;
  27. }
  28. used_ += num;
  29. }
  30. void TbeHandleInfo::used_dec(uint32_t num) {
  31. if (used_ < std::numeric_limits<uint32_t>::min() + num) {
  32. REPORT_INNER_ERROR("E19999", "Used:%u reach numeric min", used_);
  33. GELOGE(INTERNAL_ERROR, "Used[%u] reach numeric min.", used_);
  34. return;
  35. }
  36. used_ -= num;
  37. }
  38. uint32_t TbeHandleInfo::used_num() const {
  39. return used_;
  40. }
  41. void *TbeHandleInfo::handle() const {
  42. return handle_;
  43. }
  44. TBEHandleStore &TBEHandleStore::GetInstance() {
  45. static TBEHandleStore instance;
  46. return instance;
  47. }
  48. ///
  49. /// @ingroup ge
  50. /// @brief Find Registered TBE handle by name.
  51. /// @param [in] name: TBE handle name to find.
  52. /// @param [out] handle: handle names record.
  53. /// @return true: found / false: not found.
  54. ///
  55. bool TBEHandleStore::FindTBEHandle(const std::string &name, void *&handle) {
  56. std::lock_guard<std::mutex> lock(mutex_);
  57. auto it = kernels_.find(name);
  58. if (it == kernels_.end()) {
  59. return false;
  60. } else {
  61. TbeHandleInfo &info = it->second;
  62. handle = info.handle();
  63. return true;
  64. }
  65. }
  66. ///
  67. /// @ingroup ge
  68. /// @brief Store registered TBE handle info.
  69. /// @param [in] name: TBE handle name to store.
  70. /// @param [in] handle: TBE handle addr to store.
  71. /// @param [in] kernel: TBE kernel bin to store.
  72. /// @return NA
  73. ///
  74. void TBEHandleStore::StoreTBEHandle(const std::string &name, void *handle,
  75. std::shared_ptr<OpKernelBin> &kernel) {
  76. std::lock_guard<std::mutex> lock(mutex_);
  77. auto it = kernels_.find(name);
  78. if (it == kernels_.end()) {
  79. TbeHandleInfo info(handle, kernel);
  80. info.used_inc();
  81. kernels_.emplace(name, info);
  82. } else {
  83. TbeHandleInfo &info = it->second;
  84. info.used_inc();
  85. }
  86. }
  87. ///
  88. /// @ingroup ge
  89. /// @brief Increase reference of registered TBE handle info.
  90. /// @param [in] name: handle name increase reference.
  91. /// @return NA
  92. ///
  93. void TBEHandleStore::ReferTBEHandle(const std::string &name) {
  94. std::lock_guard<std::mutex> lock(mutex_);
  95. auto it = kernels_.find(name);
  96. if (it == kernels_.end()) {
  97. REPORT_INNER_ERROR("E19999", "Kernel:%s not found in stored check invalid",
  98. name.c_str());
  99. GELOGE(INTERNAL_ERROR, "Kernel[%s] not found in stored.", name.c_str());
  100. return;
  101. }
  102. TbeHandleInfo &info = it->second;
  103. info.used_inc();
  104. }
  105. ///
  106. /// @ingroup ge
  107. /// @brief Erase TBE registered handle record.
  108. /// @param [in] names: handle names erase.
  109. /// @return NA
  110. ///
  111. void TBEHandleStore::EraseTBEHandle(const std::map<std::string, uint32_t> &names) {
  112. std::lock_guard<std::mutex> lock(mutex_);
  113. for (auto &item : names) {
  114. auto it = kernels_.find(item.first);
  115. if (it == kernels_.end()) {
  116. REPORT_INNER_ERROR("E19999", "Kernel:%s not found in stored check invalid",
  117. item.first.c_str());
  118. GELOGE(INTERNAL_ERROR, "Kernel[%s] not found in stored.", item.first.c_str());
  119. continue;
  120. }
  121. TbeHandleInfo &info = it->second;
  122. if (info.used_num() > item.second) {
  123. info.used_dec(item.second);
  124. } else {
  125. rtError_t rt_ret = rtDevBinaryUnRegister(info.handle());
  126. if (rt_ret != RT_ERROR_NONE) {
  127. REPORT_INNER_ERROR("E19999", "Call rtDevBinaryUnRegister failed for Kernel:%s fail, ret:0x%X",
  128. item.first.c_str(), rt_ret);
  129. GELOGE(INTERNAL_ERROR, "Kernel[%s] UnRegister handle fail:%u.", item.first.c_str(), rt_ret);
  130. }
  131. kernels_.erase(it);
  132. }
  133. }
  134. }
  135. } // namespace ge

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