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 6.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, "rtFree 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, "rtFree failed"));
  35. }
  36. }
  37. }
  38. SingleOp *StreamResource::GetOperator(const void *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 void *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 failed, size = %zu, ret = %d", size, ret);
  84. return nullptr;
  85. }
  86. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), size)
  87. ret = rtMemset(buffer, size, 0U, size);
  88. if (ret != RT_ERROR_NONE) {
  89. GELOGE(RT_FAILED, "rtMemset failed, ret = %d", ret);
  90. auto rt_ret = rtFree(buffer);
  91. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "rtFree failed"));
  92. return nullptr;
  93. }
  94. GELOGD("Malloc new memory succeeded. size = %zu", size);
  95. max_allocated = size;
  96. allocated.emplace_back(buffer);
  97. return buffer;
  98. }
  99. uint8_t *StreamResource::MallocMemory(const std::string &purpose, size_t size, bool holding_lock) {
  100. GELOGD("To Malloc memory, size = %zu", size);
  101. if (holding_lock) {
  102. return DoMallocMemory(purpose, size, max_memory_size_, memory_list_);
  103. } else {
  104. std::lock_guard<std::mutex> lk(stream_mu_);
  105. return DoMallocMemory(purpose, size, max_memory_size_, memory_list_);
  106. }
  107. }
  108. uint8_t *StreamResource::MallocWeight(const std::string &purpose, size_t size) {
  109. GELOGD("To Malloc weight, size = %zu", size);
  110. uint8_t *buffer = nullptr;
  111. auto ret = rtMalloc(reinterpret_cast<void **>(&buffer), size, RT_MEMORY_HBM);
  112. if (ret != RT_ERROR_NONE) {
  113. GELOGE(RT_FAILED, "rtMalloc failed, size = %zu, ret = %d", size, ret);
  114. return nullptr;
  115. }
  116. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), size)
  117. weight_list_.emplace_back(buffer);
  118. return buffer;
  119. }
  120. Status StreamResource::BuildDynamicOperator(const string &model_name,
  121. const ModelData &model_data,
  122. DynamicSingleOp **single_op) {
  123. std::lock_guard<std::mutex> lk(mu_);
  124. auto it = dynamic_op_map_.find(model_data.model_data);
  125. if (it != dynamic_op_map_.end()) {
  126. *single_op = it->second.get();
  127. return SUCCESS;
  128. }
  129. SingleOpModel model(model_name, model_data.model_data, model_data.model_len);
  130. auto ret = model.Init();
  131. if (ret != SUCCESS) {
  132. GELOGE(ret, "Init model failed. model = %s, ret = %u", model_name.c_str(), ret);
  133. return ret;
  134. }
  135. auto new_op = std::unique_ptr<DynamicSingleOp>(new(std::nothrow) DynamicSingleOp(resource_id_, &stream_mu_, stream_));
  136. GE_CHECK_NOTNULL(new_op);
  137. GELOGI("To build operator: %s", model_name.c_str());
  138. GE_CHK_STATUS_RET(model.BuildDynamicOp(*this, *new_op),
  139. "Build op failed. op = %s, ret = %u", model_name.c_str(), ret);
  140. *single_op = new_op.get();
  141. dynamic_op_map_[model_data.model_data] = std::move(new_op);
  142. return SUCCESS;
  143. }
  144. Status StreamResource::BuildOperator(const string &model_name, const ModelData &model_data, SingleOp **single_op) {
  145. std::lock_guard<std::mutex> lk(mu_);
  146. auto it = op_map_.find(model_data.model_data);
  147. if (it != op_map_.end()) {
  148. *single_op = it->second.get();
  149. return SUCCESS;
  150. }
  151. SingleOpModel model(model_name, model_data.model_data, model_data.model_len);
  152. auto ret = model.Init();
  153. if (ret != SUCCESS) {
  154. GELOGE(ret, "Init model failed. model = %s, ret = %u", model_name.c_str(), ret);
  155. return ret;
  156. }
  157. auto new_op = std::unique_ptr<SingleOp>(new(std::nothrow) SingleOp(this, &stream_mu_, stream_));
  158. if (new_op == nullptr) {
  159. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "new SingleOp failed");
  160. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  161. }
  162. GELOGI("To build operator: %s", model_name.c_str());
  163. GE_CHK_STATUS_RET(model.BuildOp(*this, *new_op), "Build op failed. op = %s, ret = %u", model_name.c_str(), ret);
  164. *single_op = new_op.get();
  165. op_map_[model_data.model_data] = std::move(new_op);
  166. return SUCCESS;
  167. }
  168. const uint8_t *StreamResource::GetMemoryBase() const {
  169. if (memory_list_.empty()) {
  170. return nullptr;
  171. }
  172. return memory_list_.back();
  173. }
  174. } // namespace ge

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