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

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