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.

single_op_manager.cc 6.4 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "single_op/single_op_manager.h"
  17. #include <mutex>
  18. #include <string>
  19. #include "graph/manager/graph_mem_allocator.h"
  20. #include "graph/manager/graph_caching_allocator.h"
  21. namespace ge {
  22. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY SingleOpManager::~SingleOpManager() {
  23. for (auto &it : stream_resources_) {
  24. delete it.second;
  25. it.second = nullptr;
  26. }
  27. }
  28. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status SingleOpManager::GetOpFromModel(const std::string &model_name,
  29. const ModelData &model_data,
  30. void *stream,
  31. SingleOp **single_op,
  32. const uint64_t model_id) {
  33. GELOGI("GetOpFromModel in. model name = %s, model id = %lu", model_name.c_str(), model_id);
  34. if (single_op == nullptr) {
  35. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "[Check][Param:single_op] is null.");
  36. REPORT_INPUT_ERROR("E10412", std::vector<std::string>({"inputparam"}), std::vector<std::string>({"single_op"}));
  37. return ACL_ERROR_GE_INTERNAL_ERROR;
  38. }
  39. uintptr_t resource_id = 0;
  40. GE_CHK_STATUS_RET(GetResourceId(stream, resource_id));
  41. StreamResource *res = GetResource(resource_id, stream);
  42. if (res == nullptr) {
  43. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[Get][Resource] failed.");
  44. REPORT_CALL_ERROR("E19999", "GetOpFromModel fail because GetResource return nullptr.");
  45. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  46. }
  47. SingleOp *op = res->GetOperator(model_id);
  48. if (op != nullptr) {
  49. GELOGD("Got operator from stream cache");
  50. *single_op = op;
  51. return SUCCESS;
  52. }
  53. return res->BuildOperator(model_data, single_op, model_id);
  54. }
  55. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status SingleOpManager::ReleaseResource(void *stream) {
  56. auto resource_id = reinterpret_cast<uintptr_t>(stream);
  57. GELOGI("ReleaseResource in. resource id = 0x%lx", static_cast<uint64_t>(resource_id));
  58. std::lock_guard<std::mutex> lock(mutex_);
  59. auto it = stream_resources_.find(resource_id);
  60. if (it == stream_resources_.end()) {
  61. MemManager::Instance().CachingInstance(RT_MEMORY_HBM).TryFreeBlocks();
  62. return SUCCESS;
  63. }
  64. delete it->second;
  65. it->second = nullptr;
  66. (void)stream_resources_.erase(it);
  67. MemManager::Instance().CachingInstance(RT_MEMORY_HBM).TryFreeBlocks();
  68. return SUCCESS;
  69. }
  70. StreamResource *SingleOpManager::GetResource(uintptr_t resource_id, rtStream_t stream) {
  71. std::lock_guard<std::mutex> lock(mutex_);
  72. auto it = stream_resources_.find(resource_id);
  73. StreamResource *res = nullptr;
  74. if (it == stream_resources_.end()) {
  75. res = new(std::nothrow) StreamResource(resource_id);
  76. if (res != nullptr) {
  77. if (res->Init() != SUCCESS) {
  78. GELOGE(FAILED, "[Malloc][Memory]Failed to malloc device buffer.");
  79. delete res;
  80. return nullptr;
  81. }
  82. res->SetStream(stream);
  83. stream_resources_.emplace(resource_id, res);
  84. }
  85. } else {
  86. res = it->second;
  87. }
  88. return res;
  89. }
  90. StreamResource *SingleOpManager::TryGetResource(uintptr_t resource_id) {
  91. std::lock_guard<std::mutex> lock(mutex_);
  92. auto it = stream_resources_.find(resource_id);
  93. if (it == stream_resources_.end()) {
  94. return nullptr;
  95. }
  96. return it->second;
  97. }
  98. Status SingleOpManager::GetDynamicOpFromModel(const string &model_name,
  99. const ModelData &model_data,
  100. void *stream,
  101. DynamicSingleOp **single_op,
  102. const uint64_t model_id) {
  103. GELOGI("GetOpFromModel in. model name = %s, model id = %lu", model_name.c_str(), model_id);
  104. if (!tiling_func_registered_) {
  105. RegisterTilingFunc();
  106. }
  107. GE_CHECK_NOTNULL(single_op);
  108. uintptr_t resource_id = 0;
  109. GE_CHK_STATUS_RET(GetResourceId(stream, resource_id));
  110. StreamResource *res = GetResource(resource_id, stream);
  111. if (res == nullptr) {
  112. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[Get][Resource] failed.");
  113. REPORT_CALL_ERROR("E19999", "GetDynamicOpFromModel fail because GetResource return nullptr.");
  114. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  115. }
  116. DynamicSingleOp *op = res->GetDynamicOperator(model_id);
  117. if (op != nullptr) {
  118. GELOGD("Got operator from stream cache");
  119. *single_op = op;
  120. return SUCCESS;
  121. }
  122. return res->BuildDynamicOperator(model_data, single_op, model_id);
  123. }
  124. void SingleOpManager::RegisterTilingFunc() {
  125. std::lock_guard<std::mutex> lk(mutex_);
  126. if (tiling_func_registered_) {
  127. return;
  128. }
  129. op_tiling_manager_.LoadSo();
  130. tiling_func_registered_ = true;
  131. }
  132. Status SingleOpManager::GetResourceId(rtStream_t stream, uintptr_t &resource_id) {
  133. // runtime uses NULL to denote a default stream for each device
  134. if (stream == nullptr) {
  135. // get current context
  136. rtContext_t rt_cur_ctx = nullptr;
  137. auto rt_err = rtCtxGetCurrent(&rt_cur_ctx);
  138. if (rt_err != RT_ERROR_NONE) {
  139. GELOGE(rt_err, "[Get][CurrentContext] failed, runtime result is %d", static_cast<int>(rt_err));
  140. REPORT_CALL_ERROR("E19999",
  141. "GetResourceId failed because rtCtxGetCurrent result is %d", static_cast<int>(rt_err));
  142. return RT_ERROR_TO_GE_STATUS(rt_err);
  143. }
  144. // use current context as resource key instead
  145. GELOGI("use context as resource key instead when default stream");
  146. resource_id = reinterpret_cast<uintptr_t>(rt_cur_ctx);
  147. } else {
  148. GELOGI("use stream as resource key instead when create stream");
  149. resource_id = reinterpret_cast<uintptr_t>(stream);
  150. }
  151. return SUCCESS;
  152. }
  153. } // namespace ge

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