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.

hybrid_davinci_model.cc 7.8 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <memory>
  17. #include "hybrid_davinci_model.h"
  18. #include "hybrid/model/hybrid_model.h"
  19. #include "hybrid/executor/hybrid_model_async_executor.h"
  20. #include "hybrid/node_executor/node_executor.h"
  21. #include "graph/manager/graph_manager_utils.h"
  22. namespace ge {
  23. namespace hybrid {
  24. class HybridDavinciModel::Impl {
  25. public:
  26. explicit Impl(GeRootModelPtr ge_model) : model_(std::move(ge_model)), executor_(&model_) {
  27. }
  28. ~Impl() {
  29. NodeExecutorManager::GetInstance().FinalizeExecutors();
  30. }
  31. Status Init() {
  32. GE_CHK_STATUS_RET(NodeExecutorManager::GetInstance().EnsureInitialized(), "Failed to initialize executors");
  33. GE_CHK_STATUS_RET(model_.Init(), "Failed to init model.")
  34. GE_CHK_STATUS_RET(executor_.Init(), "Failed to init model executor.")
  35. return SUCCESS;
  36. }
  37. Status Execute(const std::vector<DataBuffer> &inputs,
  38. const std::vector<GeTensorDesc> &input_desc,
  39. std::vector<DataBuffer> &outputs,
  40. std::vector<GeTensorDesc> &output_desc,
  41. rtStream_t stream) {
  42. return executor_.Execute(inputs, input_desc, outputs, output_desc);
  43. }
  44. Status Execute(const vector<GeTensor> &inputs, vector<GeTensor> &outputs) {
  45. return executor_.Execute(inputs, outputs);
  46. }
  47. Status ModelRunStart() {
  48. return executor_.Start(listener_);
  49. }
  50. Status ModelRunStop() {
  51. return executor_.Stop();
  52. }
  53. Status EnqueueData(const std::shared_ptr<InputDataWrapper> &data) {
  54. return executor_.EnqueueData(data);
  55. }
  56. void SetListener(const shared_ptr<ModelListener> &listener) {
  57. listener_ = listener;
  58. }
  59. void SetModelId(uint32_t model_id) {
  60. executor_.SetModelId(model_id);
  61. model_.SetModelId(model_id);
  62. }
  63. void SetDeviceId(uint32_t device_id) {
  64. model_.SetDeviceId(device_id);
  65. executor_.SetDeviceId(device_id);
  66. }
  67. void SetOmName(const string &model_name) {
  68. model_.SetOmName(model_name);
  69. }
  70. uint64_t GetSessionId() {
  71. return model_.GetSessionId();
  72. }
  73. Status GetDynamicBatchInfo(std::vector<std::vector<int64_t>> &batch_info, int32_t &dynamic_type) {
  74. return model_.GetDynamicBatchInfo(batch_info, dynamic_type);
  75. }
  76. void GetUserDesignateShapeOrder(std::vector<std::string> &user_input_shape_order) {
  77. model_.GetUserDesignateShapeOrder(user_input_shape_order);
  78. }
  79. void GetModelAttr(std::vector<std::string> &dynamic_output_shape_info) {
  80. model_.GetModelAttr(dynamic_output_shape_info);
  81. }
  82. Status GetInputOutputDescInfo(vector<InputOutputDescInfo> &input_desc,
  83. vector<InputOutputDescInfo> &output_desc,
  84. std::vector<uint32_t> &input_formats,
  85. std::vector<uint32_t> &output_formats) {
  86. return model_.GetInputOutputDescInfo(input_desc, output_desc, input_formats, output_formats);
  87. }
  88. void SetModelDescVersion(bool is_new_model_desc) {
  89. model_.SetModelDescVersion(is_new_model_desc);
  90. }
  91. uint32_t GetDataInputerSize() { return executor_.GetDataInputerSize(); }
  92. bool GetRunningFlag() const { return executor_.GetRunningFlag(); }
  93. Status SetRunAsyncListenerCallback(const RunAsyncCallback &callback) {
  94. auto listener = dynamic_cast<RunAsyncListener *>(listener_.get());
  95. GE_CHECK_NOTNULL(listener);
  96. listener->SetCallback(callback);
  97. return SUCCESS;
  98. }
  99. private:
  100. std::shared_ptr<ModelListener> listener_;
  101. HybridModel model_;
  102. HybridModelAsyncExecutor executor_;
  103. };
  104. HybridDavinciModel::~HybridDavinciModel() {
  105. delete impl_;
  106. }
  107. std::unique_ptr<HybridDavinciModel> HybridDavinciModel::Create(const GeRootModelPtr &ge_root_model) {
  108. auto instance = std::unique_ptr<HybridDavinciModel>(new (std::nothrow)HybridDavinciModel());
  109. if (instance != nullptr) {
  110. instance->impl_ = new (std::nothrow) HybridDavinciModel::Impl(ge_root_model);
  111. if (instance->impl_ != nullptr) {
  112. return instance;
  113. }
  114. }
  115. return nullptr;
  116. }
  117. Status HybridDavinciModel::Init() {
  118. GE_CHECK_NOTNULL(impl_);
  119. return impl_->Init();
  120. }
  121. Status HybridDavinciModel::Execute(const std::vector<DataBuffer> &inputs,
  122. const std::vector<GeTensorDesc> &input_desc,
  123. std::vector<DataBuffer> &outputs,
  124. std::vector<GeTensorDesc> &output_desc, rtStream_t stream) {
  125. GE_CHECK_NOTNULL(impl_);
  126. return impl_->Execute(inputs, input_desc, outputs, output_desc, stream);
  127. }
  128. Status HybridDavinciModel::Execute(const vector<GeTensor> &inputs, vector<GeTensor> &outputs) {
  129. GE_CHECK_NOTNULL(impl_);
  130. return impl_->Execute(inputs, outputs);
  131. }
  132. Status HybridDavinciModel::ModelRunStart() {
  133. GE_CHECK_NOTNULL(impl_);
  134. return impl_->ModelRunStart();
  135. }
  136. Status HybridDavinciModel::ModelRunStop() {
  137. GE_CHECK_NOTNULL(impl_);
  138. return impl_->ModelRunStop();
  139. }
  140. Status HybridDavinciModel::EnqueueData(const shared_ptr<InputDataWrapper> &data) {
  141. GE_CHECK_NOTNULL(impl_);
  142. return impl_->EnqueueData(data);
  143. }
  144. void HybridDavinciModel::SetListener(const shared_ptr<ModelListener> &listener) {
  145. if (impl_ != nullptr) {
  146. impl_->SetListener(listener);
  147. }
  148. }
  149. void HybridDavinciModel::SetModelId(uint32_t model_id) {
  150. if (impl_ != nullptr) {
  151. impl_->SetModelId(model_id);
  152. }
  153. }
  154. void HybridDavinciModel::SetDeviceId(uint32_t device_id) {
  155. if (impl_ != nullptr) {
  156. impl_->SetDeviceId(device_id);
  157. }
  158. }
  159. void HybridDavinciModel::SetOmName(const string &om_name) {
  160. if (impl_ != nullptr) {
  161. impl_->SetOmName(om_name);
  162. }
  163. }
  164. Status HybridDavinciModel::GetDynamicBatchInfo(std::vector<std::vector<int64_t>> &batch_info, int32_t &dynamic_type) {
  165. GE_CHECK_NOTNULL(impl_);
  166. return impl_->GetDynamicBatchInfo(batch_info, dynamic_type);
  167. }
  168. void HybridDavinciModel::GetUserDesignateShapeOrder(std::vector<std::string> &user_input_shape_order) {
  169. if (impl_ != nullptr) {
  170. impl_->GetUserDesignateShapeOrder(user_input_shape_order);
  171. }
  172. }
  173. void HybridDavinciModel::GetModelAttr(std::vector<std::string> &dynamic_output_shape_info) {
  174. if (impl_ != nullptr) {
  175. impl_->GetModelAttr(dynamic_output_shape_info);
  176. }
  177. }
  178. Status HybridDavinciModel::GetInputOutputDescInfo(vector<InputOutputDescInfo> &input_desc,
  179. vector<InputOutputDescInfo> &output_desc,
  180. std::vector<uint32_t> &input_formats,
  181. std::vector<uint32_t> &output_formats) {
  182. GE_CHECK_NOTNULL(impl_);
  183. return impl_->GetInputOutputDescInfo(input_desc, output_desc, input_formats, output_formats);
  184. }
  185. void HybridDavinciModel::SetModelDescVersion(bool is_new_model_desc) {
  186. if (impl_ != nullptr) {
  187. impl_->SetModelDescVersion(is_new_model_desc);
  188. }
  189. }
  190. uint64_t HybridDavinciModel::GetSessionId() {
  191. GE_CHECK_NOTNULL(impl_);
  192. return impl_->GetSessionId();
  193. }
  194. uint32_t HybridDavinciModel::GetDataInputerSize() {
  195. GE_CHECK_NOTNULL(impl_);
  196. return impl_->GetDataInputerSize();
  197. }
  198. bool HybridDavinciModel::GetRunningFlag() const { return impl_->GetRunningFlag(); }
  199. Status HybridDavinciModel::SetRunAsyncListenerCallback(const RunAsyncCallback &callback) {
  200. return impl_->SetRunAsyncListenerCallback(callback);
  201. }
  202. } // namespace hybrid
  203. } // namespace ge

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