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.

stream_resource.cc 7.3 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/stream_resource.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include "framework/common/debug/log.h"
  19. #include "runtime/rt.h"
  20. #include "single_op/single_op_model.h"
  21. namespace ge {
  22. StreamResource::StreamResource(uintptr_t resource_id) : resource_id_(resource_id) {
  23. }
  24. StreamResource::~StreamResource() {
  25. for (auto mem : memory_list_) {
  26. if (mem != nullptr) {
  27. auto rt_ret = rtFree(mem);
  28. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "[Free][Rt] failed."));
  29. }
  30. }
  31. for (auto weight : weight_list_) {
  32. if (weight != nullptr) {
  33. auto rt_ret = rtFree(weight);
  34. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "[Free][Rt] failed."));
  35. }
  36. }
  37. }
  38. SingleOp *StreamResource::GetOperator(const uint64_t key) {
  39. std::lock_guard<std::mutex> lk(mu_);
  40. auto it = op_map_.find(key);
  41. if (it == op_map_.end()) {
  42. return nullptr;
  43. }
  44. return it->second.get();
  45. }
  46. DynamicSingleOp *StreamResource::GetDynamicOperator(const uint64_t key) {
  47. std::lock_guard<std::mutex> lk(mu_);
  48. auto it = dynamic_op_map_.find(key);
  49. if (it == dynamic_op_map_.end()) {
  50. return nullptr;
  51. }
  52. return it->second.get();
  53. }
  54. rtStream_t StreamResource::GetStream() const {
  55. return stream_;
  56. }
  57. void StreamResource::SetStream(rtStream_t stream) {
  58. stream_ = stream;
  59. }
  60. uint8_t *StreamResource::DoMallocMemory(const std::string &purpose,
  61. size_t size,
  62. size_t &max_allocated,
  63. std::vector<uint8_t *> &allocated) {
  64. if (size == 0) {
  65. GELOGD("Mem size == 0");
  66. return nullptr;
  67. }
  68. if (size <= max_allocated && !allocated.empty()) {
  69. GELOGD("reuse last memory");
  70. return allocated.back();
  71. }
  72. if (!allocated.empty()) {
  73. uint8_t *current_buffer = allocated.back();
  74. allocated.pop_back();
  75. if (rtStreamSynchronize(stream_) != RT_ERROR_NONE) {
  76. GELOGW("Failed to invoke rtStreamSynchronize");
  77. }
  78. (void) rtFree(current_buffer);
  79. }
  80. uint8_t *buffer = nullptr;
  81. auto ret = rtMalloc(reinterpret_cast<void **>(&buffer), size, RT_MEMORY_HBM);
  82. if (ret != RT_ERROR_NONE) {
  83. GELOGE(RT_FAILED, "[RtMalloc][Memory] failed, size = %zu, ret = %d", size, ret);
  84. REPORT_INNER_ERROR("E19999", "rtMalloc failed, size = %zu, ret = %d, when %s.", size, ret, __FUNCTION__);
  85. return nullptr;
  86. }
  87. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), size)
  88. ret = rtMemset(buffer, size, 0U, size);
  89. if (ret != RT_ERROR_NONE) {
  90. GELOGE(RT_FAILED, "[RtMemset][Memory] failed, ret = %d", ret);
  91. REPORT_INNER_ERROR("E19999", "rtMemset failed, ret = %d, when %s.", ret, __FUNCTION__);
  92. auto rt_ret = rtFree(buffer);
  93. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "[RtFree][Memory] failed"));
  94. return nullptr;
  95. }
  96. GELOGD("Malloc new memory succeeded. size = %zu", size);
  97. max_allocated = size;
  98. allocated.emplace_back(buffer);
  99. return buffer;
  100. }
  101. uint8_t *StreamResource::MallocMemory(const std::string &purpose, size_t size, bool holding_lock) {
  102. GELOGD("To Malloc memory, size = %zu", size);
  103. if (holding_lock) {
  104. return DoMallocMemory(purpose, size, max_memory_size_, memory_list_);
  105. } else {
  106. std::lock_guard<std::mutex> lk(stream_mu_);
  107. return DoMallocMemory(purpose, size, max_memory_size_, memory_list_);
  108. }
  109. }
  110. uint8_t *StreamResource::MallocWeight(const std::string &purpose, size_t size) {
  111. GELOGD("To Malloc weight, size = %zu", size);
  112. uint8_t *buffer = nullptr;
  113. auto ret = rtMalloc(reinterpret_cast<void **>(&buffer), size, RT_MEMORY_HBM);
  114. if (ret != RT_ERROR_NONE) {
  115. GELOGE(RT_FAILED, "[RtMalloc][Memory] failed, size = %zu, ret = %d", size, ret);
  116. REPORT_INNER_ERROR("E19999", "rtMalloc failed, size = %zu, ret = %d when %s.",
  117. size, ret, __FUNCTION__);
  118. return nullptr;
  119. }
  120. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), size)
  121. weight_list_.emplace_back(buffer);
  122. return buffer;
  123. }
  124. Status StreamResource::BuildDynamicOperator(const ModelData &model_data,
  125. DynamicSingleOp **single_op,
  126. const uint64_t model_id) {
  127. const string &model_name = std::to_string(model_id);
  128. std::lock_guard<std::mutex> lk(mu_);
  129. auto it = dynamic_op_map_.find(model_id);
  130. if (it != dynamic_op_map_.end()) {
  131. *single_op = it->second.get();
  132. return SUCCESS;
  133. }
  134. SingleOpModel model(model_name, model_data.model_data, model_data.model_len);
  135. auto ret = model.Init();
  136. if (ret != SUCCESS) {
  137. GELOGE(ret, "[Init][SingleOpModel] failed. model = %s, ret = %u", model_name.c_str(), ret);
  138. REPORT_CALL_ERROR("E19999", "SingleOpModel init failed, model = %s, ret = %u", model_name.c_str(), ret);
  139. return ret;
  140. }
  141. auto new_op = std::unique_ptr<DynamicSingleOp>(new(std::nothrow) DynamicSingleOp(resource_id_, &stream_mu_, stream_));
  142. GE_CHECK_NOTNULL(new_op);
  143. GELOGI("To build operator: %s", model_name.c_str());
  144. GE_CHK_STATUS_RET(model.BuildDynamicOp(*this, *new_op),
  145. "[Build][DynamicOp]failed. op = %s, ret = %u", model_name.c_str(), ret);
  146. *single_op = new_op.get();
  147. dynamic_op_map_[model_id] = std::move(new_op);
  148. return SUCCESS;
  149. }
  150. Status StreamResource::BuildOperator(const ModelData &model_data, SingleOp **single_op, const uint64_t model_id) {
  151. const string &model_name = std::to_string(model_id);
  152. std::lock_guard<std::mutex> lk(mu_);
  153. auto it = op_map_.find(model_id);
  154. if (it != op_map_.end()) {
  155. *single_op = it->second.get();
  156. return SUCCESS;
  157. }
  158. SingleOpModel model(model_name, model_data.model_data, model_data.model_len);
  159. auto ret = model.Init();
  160. if (ret != SUCCESS) {
  161. GELOGE(ret, "[Init][SingleOpModel] failed. model = %s, ret = %u", model_name.c_str(), ret);
  162. REPORT_CALL_ERROR("E19999", "SingleOpModel init failed, model = %s, ret = %u", model_name.c_str(), ret);
  163. return ret;
  164. }
  165. auto new_op = std::unique_ptr<SingleOp>(new(std::nothrow) SingleOp(this, &stream_mu_, stream_));
  166. if (new_op == nullptr) {
  167. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[New][SingleOp] failed.");
  168. REPORT_INNER_ERROR("E19999", "new SingleOp failed when %s.", __FUNCTION__);
  169. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  170. }
  171. GELOGI("To build operator: %s", model_name.c_str());
  172. GE_CHK_STATUS_RET(model.BuildOp(*this, *new_op), "[Build][Op] failed. op = %s, ret = %u", model_name.c_str(), ret);
  173. *single_op = new_op.get();
  174. op_map_[model_id] = std::move(new_op);
  175. return SUCCESS;
  176. }
  177. const uint8_t *StreamResource::GetMemoryBase() const {
  178. if (memory_list_.empty()) {
  179. return nullptr;
  180. }
  181. return memory_list_.back();
  182. }
  183. } // namespace ge

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