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.

graph_execute_unittest.cc 5.9 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <gtest/gtest.h>
  17. #include <memory>
  18. #include "common/profiling/profiling_manager.h"
  19. #define protected public
  20. #define private public
  21. #include "graph/execute/graph_execute.h"
  22. #include "graph/load/model_manager/model_manager.h"
  23. #include "graph/load/model_manager/davinci_model.h"
  24. #undef private
  25. #undef public
  26. #include <pthread.h>
  27. #include <algorithm>
  28. #include <future>
  29. #include <set>
  30. #include <sstream>
  31. #include <string>
  32. #include <thread>
  33. #include <future>
  34. using namespace std;
  35. using namespace testing;
  36. using namespace ge;
  37. using namespace domi;
  38. namespace ge {
  39. namespace {
  40. const uint32_t kInvalidModelId = UINT32_MAX;
  41. }
  42. class UtestGraphExecuteTest : public testing::Test {
  43. protected:
  44. void SetUp() {}
  45. void TearDown() {}
  46. };
  47. TEST_F(UtestGraphExecuteTest, get_execute_model_id_invalid) {
  48. GraphExecutor executor;
  49. ComputeGraphPtr graph = MakeShared<ComputeGraph>("test");
  50. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(graph);
  51. auto model_id = executor.GetExecuteModelId(ge_root_model);
  52. EXPECT_EQ(model_id, kInvalidModelId);
  53. }
  54. TEST_F(UtestGraphExecuteTest, get_execute_model_id_1) {
  55. GraphExecutor executor;
  56. ComputeGraphPtr graph = MakeShared<ComputeGraph>("test");
  57. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(graph);
  58. auto model_manager = ModelManager::GetInstance();
  59. shared_ptr<DavinciModel> davinci_model1 = MakeShared<DavinciModel>(1, nullptr);
  60. davinci_model1->SetId(1);
  61. model_manager->InsertModel(1, davinci_model1);
  62. ge_root_model->SetModelId(1);
  63. auto model_id = executor.GetExecuteModelId(ge_root_model);
  64. EXPECT_EQ(model_id, 1);
  65. }
  66. TEST_F(UtestGraphExecuteTest, get_execute_model_id_2) {
  67. GraphExecutor executor;
  68. ComputeGraphPtr graph = MakeShared<ComputeGraph>("test");
  69. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(graph);
  70. auto model_manager = ModelManager::GetInstance();
  71. // model1 with 2 load
  72. shared_ptr<DavinciModel> davinci_model1 = MakeShared<DavinciModel>(1, nullptr);
  73. davinci_model1->SetId(1);
  74. davinci_model1->data_inputer_ = new DataInputer();
  75. auto data = MakeShared<InputDataWrapper>();
  76. davinci_model1->data_inputer_->Push(data);
  77. davinci_model1->data_inputer_->Push(data);
  78. model_manager->InsertModel(1, davinci_model1);
  79. // model 2 with 3 load
  80. shared_ptr<DavinciModel> davinci_model2 = MakeShared<DavinciModel>(1, nullptr);
  81. davinci_model2->SetId(2);
  82. davinci_model2->data_inputer_ = new DataInputer();
  83. davinci_model2->data_inputer_->Push(data);
  84. davinci_model2->data_inputer_->Push(data);
  85. davinci_model2->data_inputer_->Push(data);
  86. model_manager->InsertModel(2, davinci_model2);
  87. // model 3 witH 1 load
  88. shared_ptr<DavinciModel> davinci_model3 = MakeShared<DavinciModel>(1, nullptr);
  89. davinci_model3->SetId(3);
  90. davinci_model3->data_inputer_ = new DataInputer();
  91. davinci_model3->data_inputer_->Push(data);
  92. model_manager->InsertModel(3, davinci_model3);
  93. ge_root_model->SetModelId(1);
  94. ge_root_model->SetModelId(2);
  95. ge_root_model->SetModelId(3);
  96. auto model_id = executor.GetExecuteModelId(ge_root_model);
  97. // model 3 is picked for having least loads
  98. EXPECT_EQ(model_id, 3);
  99. }
  100. TEST_F(UtestGraphExecuteTest, test_set_callback) {
  101. GraphExecutor executor;
  102. ComputeGraphPtr graph = MakeShared<ComputeGraph>("test");
  103. // is_unknown_shape_graph_ = false
  104. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(graph);
  105. RunAsyncCallback callback = [](Status, std::vector<ge::Tensor> &) {};
  106. auto model_manager = ModelManager::GetInstance();
  107. auto listener = MakeShared<RunAsyncListener>();
  108. shared_ptr<DavinciModel> davinci_model1 = MakeShared<DavinciModel>(1, listener);
  109. davinci_model1->SetId(1);
  110. model_manager->InsertModel(1, davinci_model1);
  111. auto status = executor.SetCallback(1, ge_root_model, callback);
  112. EXPECT_EQ(status, SUCCESS);
  113. }
  114. TEST_F(UtestGraphExecuteTest, test_without_subscribe) {
  115. GraphExecutor executor;
  116. auto ret = executor.ModelSubscribe(1);
  117. EXPECT_EQ(ret, SUCCESS);
  118. }
  119. TEST_F(UtestGraphExecuteTest, test_with_subscribe_failed1) {
  120. GraphExecutor executor;
  121. uint32_t graph_id = 1;
  122. auto &profiling_manager = ProfilingManager::Instance();
  123. profiling_manager.SetSubscribeInfo(0, 1, true);
  124. auto ret = executor.ModelSubscribe(graph_id);
  125. profiling_manager.CleanSubscribeInfo();
  126. EXPECT_NE(ret, SUCCESS);
  127. }
  128. TEST_F(UtestGraphExecuteTest, test_with_subscribe_failed2) {
  129. GraphExecutor executor;
  130. uint32_t graph_id = 1;
  131. uint32_t model_id = 1;
  132. auto &profiling_manager = ProfilingManager::Instance();
  133. profiling_manager.SetSubscribeInfo(0, 1, true);
  134. profiling_manager.SetGraphIdToModelMap(2, model_id);
  135. auto ret = executor.ModelSubscribe(graph_id);
  136. profiling_manager.CleanSubscribeInfo();
  137. EXPECT_NE(ret, SUCCESS);
  138. }
  139. TEST_F(UtestGraphExecuteTest, test_with_subscribe_success) {
  140. GraphExecutor executor;
  141. uint32_t graph_id = 1;
  142. uint32_t model_id = 1;
  143. GraphNodePtr graph_node = std::make_shared<GraphNode>(graph_id);
  144. DavinciModel model(model_id, nullptr);
  145. auto &profiling_manager = ProfilingManager::Instance();
  146. profiling_manager.SetSubscribeInfo(0, 1, true);
  147. profiling_manager.SetGraphIdToModelMap(graph_id, model_id);
  148. auto ret = executor.ModelSubscribe(graph_id);
  149. profiling_manager.CleanSubscribeInfo();
  150. EXPECT_EQ(ret, SUCCESS);
  151. }
  152. } // namespace ge

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