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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #define protected public
  19. #define private public
  20. #include "graph/execute/graph_execute.h"
  21. #include "graph/load/model_manager/model_manager.h"
  22. #include "graph/load/model_manager/davinci_model.h"
  23. #undef private
  24. #undef public
  25. #include <pthread.h>
  26. #include <algorithm>
  27. #include <future>
  28. #include <set>
  29. #include <sstream>
  30. #include <string>
  31. #include <thread>
  32. #include <future>
  33. using namespace std;
  34. using namespace testing;
  35. using namespace ge;
  36. using namespace domi;
  37. namespace ge {
  38. namespace {
  39. const uint32_t kInvalidModelId = UINT32_MAX;
  40. }
  41. class UtestGraphExecuteTest : public testing::Test {
  42. protected:
  43. void SetUp() {}
  44. void TearDown() {}
  45. };
  46. TEST_F(UtestGraphExecuteTest, get_execute_model_id_invalid) {
  47. GraphExecutor executor;
  48. ComputeGraphPtr graph = MakeShared<ComputeGraph>("test");
  49. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(graph);
  50. auto model_id = executor.GetExecuteModelId(ge_root_model);
  51. EXPECT_EQ(model_id, kInvalidModelId);
  52. }
  53. TEST_F(UtestGraphExecuteTest, get_execute_model_id_1) {
  54. GraphExecutor executor;
  55. ComputeGraphPtr graph = MakeShared<ComputeGraph>("test");
  56. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(graph);
  57. auto model_manager = ModelManager::GetInstance();
  58. shared_ptr<DavinciModel> davinci_model1 = MakeShared<DavinciModel>(1, nullptr);
  59. davinci_model1->SetId(1);
  60. model_manager->InsertModel(1, davinci_model1);
  61. ge_root_model->SetModelId(1);
  62. auto model_id = executor.GetExecuteModelId(ge_root_model);
  63. EXPECT_EQ(model_id, 1);
  64. }
  65. TEST_F(UtestGraphExecuteTest, get_execute_model_id_2) {
  66. GraphExecutor executor;
  67. ComputeGraphPtr graph = MakeShared<ComputeGraph>("test");
  68. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(graph);
  69. auto model_manager = ModelManager::GetInstance();
  70. // model1 with 2 load
  71. shared_ptr<DavinciModel> davinci_model1 = MakeShared<DavinciModel>(1, nullptr);
  72. davinci_model1->SetId(1);
  73. davinci_model1->data_inputer_ = new DataInputer();
  74. auto data = MakeShared<InputDataWrapper>();
  75. davinci_model1->data_inputer_->Push(data);
  76. davinci_model1->data_inputer_->Push(data);
  77. model_manager->InsertModel(1, davinci_model1);
  78. // model 2 with 3 load
  79. shared_ptr<DavinciModel> davinci_model2 = MakeShared<DavinciModel>(1, nullptr);
  80. davinci_model2->SetId(2);
  81. davinci_model2->data_inputer_ = new DataInputer();
  82. davinci_model2->data_inputer_->Push(data);
  83. davinci_model2->data_inputer_->Push(data);
  84. davinci_model2->data_inputer_->Push(data);
  85. model_manager->InsertModel(2, davinci_model2);
  86. // model 3 witH 1 load
  87. shared_ptr<DavinciModel> davinci_model3 = MakeShared<DavinciModel>(1, nullptr);
  88. davinci_model3->SetId(3);
  89. davinci_model3->data_inputer_ = new DataInputer();
  90. davinci_model3->data_inputer_->Push(data);
  91. model_manager->InsertModel(3, davinci_model3);
  92. ge_root_model->SetModelId(1);
  93. ge_root_model->SetModelId(2);
  94. ge_root_model->SetModelId(3);
  95. auto model_id = executor.GetExecuteModelId(ge_root_model);
  96. // model 3 is picked for having least loads
  97. EXPECT_EQ(model_id, 3);
  98. }
  99. TEST_F(UtestGraphExecuteTest, test_set_callback) {
  100. GraphExecutor executor;
  101. ComputeGraphPtr graph = MakeShared<ComputeGraph>("test");
  102. // is_unknown_shape_graph_ = false
  103. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(graph);
  104. RunAsyncCallback callback = [](Status, std::vector<ge::Tensor> &) {};
  105. auto model_manager = ModelManager::GetInstance();
  106. auto listener = MakeShared<RunAsyncListener>();
  107. shared_ptr<DavinciModel> davinci_model1 = MakeShared<DavinciModel>(1, listener);
  108. davinci_model1->SetId(1);
  109. model_manager->InsertModel(1, davinci_model1);
  110. auto status = executor.SetCallback(1, ge_root_model, callback);
  111. EXPECT_EQ(status, SUCCESS);
  112. }
  113. } // namespace ge

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