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.

model_runner.cc 5.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "ge_runtime/model_runner.h"
  17. #include "./runtime_model.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "common/ge_inner_error_codes.h"
  20. #include "common/ge/ge_util.h"
  21. #include "ge_runtime/davinci_model.h"
  22. #include "graph/op_desc.h"
  23. namespace ge {
  24. namespace model_runner {
  25. using RuntimeModelPtr = std::shared_ptr<RuntimeModel>;
  26. using DavinciModelPtr = std::shared_ptr<DavinciModel>;
  27. ModelRunner &ModelRunner::Instance() {
  28. static ModelRunner instance; // Guaranteed to be destroyed.
  29. return instance;
  30. }
  31. bool ModelRunner::LoadDavinciModel(uint32_t device_id, uint64_t session_id, uint32_t model_id,
  32. std::shared_ptr<DavinciModel> davinci_model,
  33. std::shared_ptr<ModelListener> listener) {
  34. std::shared_ptr<RuntimeModel> model = MakeShared<RuntimeModel>();
  35. if (model == nullptr) {
  36. return false;
  37. }
  38. bool status = model->Load(device_id, session_id, davinci_model);
  39. if (!status) {
  40. return false;
  41. }
  42. runtime_models_[model_id] = model;
  43. return true;
  44. }
  45. bool ModelRunner::LoadModelComplete(uint32_t model_id) {
  46. auto model_iter = runtime_models_.find(model_id);
  47. if (model_iter == runtime_models_.end()) {
  48. GELOGE(PARAM_INVALID, "Model id %u not found.", model_id);
  49. return false;
  50. }
  51. return model_iter->second->LoadComplete();
  52. }
  53. const std::vector<uint32_t> &ModelRunner::GetTaskIdList(uint32_t model_id) const {
  54. auto model_iter = runtime_models_.find(model_id);
  55. if (model_iter == runtime_models_.end()) {
  56. GELOGE(PARAM_INVALID, "Model id %u not found.", model_id);
  57. static const std::vector<uint32_t> empty_ret;
  58. return empty_ret;
  59. }
  60. return model_iter->second->GetTaskIdList();
  61. }
  62. const std::vector<uint32_t> &ModelRunner::GetStreamIdList(uint32_t model_id) const {
  63. auto model_iter = runtime_models_.find(model_id);
  64. if (model_iter == runtime_models_.end()) {
  65. GELOGE(PARAM_INVALID, "Model id %u not found.", model_id);
  66. static const std::vector<uint32_t> empty_ret;
  67. return empty_ret;
  68. }
  69. return model_iter->second->GetStreamIdList();
  70. }
  71. const std::map<std::string, std::shared_ptr<RuntimeInfo>> &ModelRunner::GetRuntimeInfoMap(uint32_t model_id) const {
  72. auto model_iter = runtime_models_.find(model_id);
  73. if (model_iter == runtime_models_.end()) {
  74. GELOGW("Model id %u not found.", model_id);
  75. static const std::map<std::string, std::shared_ptr<RuntimeInfo>> empty_ret;
  76. return empty_ret;
  77. }
  78. return model_iter->second->GetRuntimeInfoMap();
  79. }
  80. bool ModelRunner::UnloadModel(uint32_t model_id) {
  81. auto iter = runtime_models_.find(model_id);
  82. if (iter != runtime_models_.end()) {
  83. (void)runtime_models_.erase(iter);
  84. return true;
  85. }
  86. return false;
  87. }
  88. bool ModelRunner::RunModel(uint32_t model_id, const InputData &input_data, OutputData *output_data) {
  89. if (output_data == nullptr) {
  90. GELOGW("Output data point is null.");
  91. }
  92. auto model_iter = runtime_models_.find(model_id);
  93. if (model_iter == runtime_models_.end()) {
  94. GELOGE(PARAM_INVALID, "Model id %u not found.", model_id);
  95. return false;
  96. }
  97. bool status = model_iter->second->CopyInputData(input_data);
  98. if (!status) {
  99. GELOGE(FAILED, "Copy input data fail.");
  100. return false;
  101. }
  102. status = model_iter->second->Run();
  103. if (!status) {
  104. GELOGE(FAILED, "Run model fail.");
  105. return false;
  106. }
  107. return true;
  108. }
  109. bool ModelRunner::GetInputOutputDescInfo(uint32_t model_id, bool zero_copy,
  110. std::vector<InputOutputDescInfo> *input_desc,
  111. std::vector<InputOutputDescInfo> *output_desc,
  112. std::vector<uint32_t> *input_format, std::vector<uint32_t> *output_format) {
  113. if (runtime_models_.find(model_id) == runtime_models_.end()) {
  114. GELOGE(PARAM_INVALID, "Model id %u not found.", model_id);
  115. return false;
  116. }
  117. auto model = runtime_models_[model_id];
  118. if (input_desc == nullptr || output_desc == nullptr) {
  119. GELOGE(PARAM_INVALID, "input_desc or output_desc is null.");
  120. return false;
  121. }
  122. bool status = model->GetInputOutputDescInfo(zero_copy, input_desc, output_desc, input_format, output_format);
  123. if (!status) {
  124. GELOGE(FAILED, "Get input output desc info fail.");
  125. return false;
  126. }
  127. return true;
  128. }
  129. } // namespace model_runner
  130. } // namespace ge

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