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

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

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