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

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 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
4 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/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. void StreamResource::SetStream(rtStream_t stream) {
  55. stream_ = stream;
  56. }
  57. uint8_t *StreamResource::DoMallocMemory(const std::string &purpose,
  58. size_t size,
  59. size_t &max_allocated,
  60. std::vector<uint8_t *> &allocated) {
  61. if (size <= max_allocated && !allocated.empty()) {
  62. GELOGD("reuse last memory");
  63. return allocated.back();
  64. }
  65. uint8_t *buffer = nullptr;
  66. auto ret = rtMalloc(reinterpret_cast<void **>(&buffer), size, RT_MEMORY_HBM);
  67. if (ret != RT_ERROR_NONE) {
  68. GELOGE(RT_FAILED, "rtMalloc failed, size = %zu, ret = %d", size, ret);
  69. return nullptr;
  70. }
  71. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), size)
  72. ret = rtMemset(buffer, size, 0U, size);
  73. if (ret != RT_ERROR_NONE) {
  74. GELOGE(RT_FAILED, "rtMemset failed, ret = %d", ret);
  75. auto rt_ret = rtFree(buffer);
  76. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "rtFree failed"));
  77. return nullptr;
  78. }
  79. GELOGD("Malloc new memory succeeded. size = %zu", size);
  80. max_allocated = size;
  81. allocated.emplace_back(buffer);
  82. return buffer;
  83. }
  84. uint8_t *StreamResource::MallocMemory(const std::string &purpose, size_t size) {
  85. GELOGD("To Malloc memory, size = %zu", size);
  86. uint8_t *buffer = DoMallocMemory(purpose, size, max_memory_size_, memory_list_);
  87. return buffer;
  88. }
  89. uint8_t *StreamResource::MallocWeight(const std::string &purpose, size_t size) {
  90. GELOGD("To Malloc weight, size = %zu", size);
  91. uint8_t *buffer = nullptr;
  92. auto ret = rtMalloc(reinterpret_cast<void **>(&buffer), size, RT_MEMORY_HBM);
  93. if (ret != RT_ERROR_NONE) {
  94. GELOGE(RT_FAILED, "rtMalloc failed, size = %zu, ret = %d", size, ret);
  95. return nullptr;
  96. }
  97. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, purpose.c_str(), size)
  98. weight_list_.emplace_back(buffer);
  99. return buffer;
  100. }
  101. Status StreamResource::BuildDynamicOperator(const string &model_name,
  102. const ModelData &model_data,
  103. DynamicSingleOp **single_op) {
  104. std::lock_guard<std::mutex> lk(mu_);
  105. auto it = dynamic_op_map_.find(model_data.model_data);
  106. if (it != dynamic_op_map_.end()) {
  107. *single_op = it->second.get();
  108. return SUCCESS;
  109. }
  110. SingleOpModel model(model_name, model_data.model_data, model_data.model_len);
  111. auto ret = model.Init();
  112. if (ret != SUCCESS) {
  113. GELOGE(ret, "Init model failed. model = %s, ret = %u", model_name.c_str(), ret);
  114. return ret;
  115. }
  116. auto new_op = std::unique_ptr<DynamicSingleOp>(new(std::nothrow) DynamicSingleOp(resource_id_, &stream_mu_, stream_));
  117. GE_CHECK_NOTNULL(new_op);
  118. GELOGI("To build operator: %s", model_name.c_str());
  119. GE_CHK_STATUS_RET(model.BuildDynamicOp(*new_op), "Build op failed. op = %s, ret = %u", model_name.c_str(), ret);
  120. *single_op = new_op.get();
  121. dynamic_op_map_[model_data.model_data] = std::move(new_op);
  122. return SUCCESS;
  123. }
  124. Status StreamResource::BuildOperator(const string &model_name, const ModelData &model_data, SingleOp **single_op) {
  125. std::lock_guard<std::mutex> lk(mu_);
  126. auto it = op_map_.find(model_data.model_data);
  127. if (it != op_map_.end()) {
  128. *single_op = it->second.get();
  129. return SUCCESS;
  130. }
  131. SingleOpModel model(model_name, model_data.model_data, model_data.model_len);
  132. auto ret = model.Init();
  133. if (ret != SUCCESS) {
  134. GELOGE(ret, "Init model failed. model = %s, ret = %u", model_name.c_str(), ret);
  135. return ret;
  136. }
  137. auto new_op = std::unique_ptr<SingleOp>(new(std::nothrow) SingleOp(&stream_mu_, stream_));
  138. if (new_op == nullptr) {
  139. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "new SingleOp failed");
  140. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  141. }
  142. GELOGI("To build operator: %s", model_name.c_str());
  143. GE_CHK_STATUS_RET(model.BuildOp(*this, *new_op), "Build op failed. op = %s, ret = %u", model_name.c_str(), ret);
  144. *single_op = new_op.get();
  145. op_map_[model_data.model_data] = std::move(new_op);
  146. return SUCCESS;
  147. }
  148. } // namespace ge

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