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

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

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