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.

ge_profiling_manager_unittest.cc 9.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <bits/stdc++.h>
  17. #include <dirent.h>
  18. #include <gtest/gtest.h>
  19. #include <fstream>
  20. #include <map>
  21. #include <string>
  22. #include "graph/load/model_manager/davinci_model.h"
  23. #define protected public
  24. #define private public
  25. #include "common/profiling/profiling_manager.h"
  26. #include "graph/ge_local_context.h"
  27. #include "inc/framework/common/profiling/ge_profiling.h"
  28. #include "graph/manager/graph_manager.h"
  29. #include "graph/ops_stub.h"
  30. #include "inc/framework/omg/omg_inner_types.h"
  31. using namespace ge;
  32. using namespace std;
  33. class UtestGeProfilinganager : public testing::Test {
  34. protected:
  35. void SetUp() override {}
  36. void TearDown() override {}
  37. };
  38. int32_t ReporterCallback(uint32_t moduleId, uint32_t type, void *data, uint32_t len) {
  39. return -1;
  40. }
  41. void CreateGraph(Graph &graph) {
  42. TensorDesc desc(ge::Shape({1, 3, 224, 224}));
  43. uint32_t size = desc.GetShape().GetShapeSize();
  44. desc.SetSize(size);
  45. auto data = op::Data("Data").set_attr_index(0);
  46. data.update_input_desc_data(desc);
  47. data.update_output_desc_out(desc);
  48. auto flatten = op::Flatten("Flatten").set_input_x(data, data.name_out_out());
  49. std::vector<Operator> inputs{data};
  50. std::vector<Operator> outputs{flatten};
  51. std::vector<Operator> targets{flatten};
  52. // Graph graph("test_graph");
  53. graph.SetInputs(inputs).SetOutputs(outputs).SetTargets(targets);
  54. }
  55. TEST_F(UtestGeProfilinganager, init_success) {
  56. setenv("PROFILING_MODE", "true", true);
  57. Options options;
  58. options.device_id = 0;
  59. options.job_id = "0";
  60. options.profiling_mode = "1";
  61. options.profiling_options = R"({"result_path":"/data/profiling","training_trace":"on","task_trace":"on","aicpu_trace":"on","fp_point":"Data_0","bp_point":"addn","ai_core_metrics":"ResourceConflictRatio"})";
  62. struct MsprofGeOptions prof_conf = {{ 0 }};
  63. Status ret = ProfilingManager::Instance().InitFromOptions(options.profiling_mode, options.profiling_options, options.job_id, prof_conf);
  64. EXPECT_EQ(ret, ge::SUCCESS);
  65. }
  66. TEST_F(UtestGeProfilinganager, initialize) {
  67. setenv("PROFILING_MODE", "true", true);
  68. const std::map<string, string> options({
  69. {OPTION_EXEC_JOB_ID, "0"}, {OPTION_EXEC_PROFILING_MODE, "1"}, {OPTION_EXEC_PROFILING_OPTIONS, R"({"result_path":"/data/profiling","training_trace":"on","task_trace":"on","aicpu_trace":"on","fp_point":"Data_0","bp_point":"addn","ai_core_metrics":"ResourceConflictRatio"})"}
  70. });
  71. ProfilingManager::Instance().Initialize(options);
  72. EXPECT_FALSE(ProfilingManager::Instance().is_execute_profiling_);
  73. }
  74. TEST_F(UtestGeProfilinganager, ParseOptions) {
  75. setenv("PROFILING_MODE", "true", true);
  76. Options options;
  77. options.device_id = 0;
  78. options.job_id = "0";
  79. options.profiling_mode = "1";
  80. options.profiling_options = R"({"result_path":"/data/profiling","training_trace":"on","task_trace":"on","aicpu_trace":"on","fp_point":"Data_0","bp_point":"addn","ai_core_metrics":"ResourceConflictRatio"})";
  81. struct MsprofGeOptions prof_conf = {{ 0 }};
  82. Status ret = ProfilingManager::Instance().ParseOptions(options.profiling_options);
  83. EXPECT_EQ(ret, ge::SUCCESS);
  84. EXPECT_EQ(ProfilingManager::Instance().is_training_trace_, true);
  85. EXPECT_EQ(ProfilingManager::Instance().fp_point_, "Data_0");
  86. EXPECT_EQ(ProfilingManager::Instance().bp_point_, "addn");
  87. }
  88. TEST_F(UtestGeProfilinganager, plungin_init_) {
  89. ProfilingManager::Instance().prof_cb_.msprofReporterCallback = ReporterCallback;
  90. Status ret = ProfilingManager::Instance().PluginInit();
  91. EXPECT_EQ(ret, INTERNAL_ERROR);
  92. ProfilingManager::Instance().prof_cb_.msprofReporterCallback = nullptr;
  93. }
  94. TEST_F(UtestGeProfilinganager, report_data_) {
  95. std::string data = "ge is better than tensorflow.";
  96. std::string tag_name = "fmk";
  97. ProfilingManager::Instance().ReportData(0, data, tag_name);
  98. }
  99. TEST_F(UtestGeProfilinganager, get_fp_bp_point_) {
  100. map<std::string, string> options_map = {
  101. {OPTION_EXEC_PROFILING_OPTIONS,
  102. R"({"result_path":"/data/profiling","training_trace":"on","task_trace":"on","aicpu_trace":"on","fp_point":"Data_0","bp_point":"addn","ai_core_metrics":"ResourceConflictRatio"})"}};
  103. GEThreadLocalContext &context = GetThreadLocalContext();
  104. context.SetGraphOption(options_map);
  105. std::string fp_point;
  106. std::string bp_point;
  107. ProfilingManager::Instance().GetFpBpPoint(fp_point, bp_point);
  108. EXPECT_EQ(fp_point, "Data_0");
  109. EXPECT_EQ(bp_point, "addn");
  110. }
  111. TEST_F(UtestGeProfilinganager, get_fp_bp_point_empty) {
  112. // fp bp empty
  113. map<std::string, string> options_map = {
  114. { OPTION_EXEC_PROFILING_OPTIONS,
  115. R"({"result_path":"/data/profiling","training_trace":"on","task_trace":"on","aicpu_trace":"on","ai_core_metrics":"ResourceConflictRatio"})"}};
  116. GEThreadLocalContext &context = GetThreadLocalContext();
  117. context.SetGraphOption(options_map);
  118. std::string fp_point = "fp";
  119. std::string bp_point = "bp";
  120. ProfilingManager::Instance().bp_point_ = "";
  121. ProfilingManager::Instance().fp_point_ = "";
  122. ProfilingManager::Instance().GetFpBpPoint(fp_point, bp_point);
  123. EXPECT_EQ(fp_point, "");
  124. EXPECT_EQ(bp_point, "");
  125. }
  126. TEST_F(UtestGeProfilinganager, set_step_info_success) {
  127. uint64_t index_id = 0;
  128. auto stream = (rtStream_t)0x1;
  129. Status ret = ProfSetStepInfo(index_id, 0, stream);
  130. EXPECT_EQ(ret, ge::SUCCESS);
  131. ret = ProfSetStepInfo(index_id, 1, stream);
  132. EXPECT_EQ(ret, ge::SUCCESS);
  133. }
  134. TEST_F(UtestGeProfilinganager, set_step_info_failed) {
  135. uint64_t index_id = 0;
  136. auto stream = (rtStream_t)0x1;
  137. Status ret = ProfSetStepInfo(index_id, 1, stream);
  138. EXPECT_EQ(ret, ge::FAILED);
  139. }
  140. TEST_F(UtestGeProfilinganager, get_device_from_graph) {
  141. GraphId graph_id = 1;
  142. uint32_t device_id = 0;
  143. GraphManager graph_manager;
  144. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  145. graph_manager.AddGraphNode(graph_id, graph_node);
  146. graph_manager.SetAddGraphCondition(graph_id, 2);
  147. Graph graph("test_graph");
  148. CreateGraph(graph);
  149. std::map<std::string, std::string> options;
  150. OmgContext context;
  151. Status ret = graph_manager.AddGraph(graph_id, graph, options, context);
  152. EXPECT_EQ(ret, ge::SUCCESS);
  153. ret = ProfGetDeviceFormGraphId(graph_id, device_id);
  154. EXPECT_EQ(ret, ge::SUCCESS);
  155. }
  156. TEST_F(UtestGeProfilinganager, handle_subscribe_info) {
  157. ProfCommandHandleType prof_type = kProfCommandhandleModelSubscribe;
  158. ProfCommandHandleData prof_data;
  159. prof_data.profSwitch = 0;
  160. prof_data.modelId = 1;
  161. domi::GetContext().train_flag = true;
  162. auto prof_ptr = std::make_shared<ProfCommandHandleData>(prof_data);
  163. Status ret = ProfCommandHandle(prof_type, static_cast<void *>(prof_ptr.get()), sizeof(prof_data));
  164. EXPECT_EQ(ret, ge::SUCCESS);
  165. }
  166. TEST_F(UtestGeProfilinganager, handle_unsubscribe_info) {
  167. ProfCommandHandleType prof_type = kProfCommandhandleModelUnsubscribe;
  168. ProfCommandHandleData prof_data;
  169. prof_data.profSwitch = 0;
  170. prof_data.modelId = 1;
  171. domi::GetContext().train_flag = true;
  172. auto &profiling_manager = ge::ProfilingManager::Instance();
  173. profiling_manager.SetSubscribeInfo(0, 1, true);
  174. auto prof_ptr = std::make_shared<ProfCommandHandleData>(prof_data);
  175. Status ret = ProfCommandHandle(prof_type, static_cast<void *>(prof_ptr.get()), sizeof(prof_data));
  176. profiling_manager.CleanSubscribeInfo();
  177. }
  178. TEST_F(UtestGeProfilinganager, set_subscribe_info) {
  179. auto &profiling_manager = ge::ProfilingManager::Instance();
  180. profiling_manager.SetSubscribeInfo(0, 1, true);
  181. const auto &subInfo = profiling_manager.GetSubscribeInfo();
  182. EXPECT_EQ(subInfo.prof_switch, 0);
  183. EXPECT_EQ(subInfo.graph_id, 1);
  184. EXPECT_EQ(subInfo.is_subscribe, true);
  185. }
  186. TEST_F(UtestGeProfilinganager, clean_subscribe_info) {
  187. auto &profiling_manager = ge::ProfilingManager::Instance();
  188. profiling_manager.CleanSubscribeInfo();
  189. const auto &subInfo = profiling_manager.GetSubscribeInfo();
  190. EXPECT_EQ(subInfo.prof_switch, 0);
  191. EXPECT_EQ(subInfo.graph_id, 0);
  192. EXPECT_EQ(subInfo.is_subscribe, false);
  193. }
  194. TEST_F(UtestGeProfilinganager, get_model_id_success) {
  195. auto &profiling_manager = ge::ProfilingManager::Instance();
  196. profiling_manager.SetGraphIdToModelMap(0, 1);
  197. uint32_t model_id = 0;
  198. Status ret = profiling_manager.GetModelIdFromGraph(0, model_id);
  199. EXPECT_EQ(ret, ge::SUCCESS);
  200. }
  201. TEST_F(UtestGeProfilinganager, get_model_id_failed) {
  202. auto &profiling_manager = ge::ProfilingManager::Instance();
  203. profiling_manager.SetGraphIdToModelMap(0, 1);
  204. uint32_t model_id = 0;
  205. Status ret = profiling_manager.GetModelIdFromGraph(10, model_id);
  206. EXPECT_EQ(ret, ge::FAILED);
  207. }
  208. TEST_F(UtestGeProfilinganager, get_device_id_success) {
  209. auto &profiling_manager = ge::ProfilingManager::Instance();
  210. profiling_manager.SetGraphIdToDeviceMap(0, 1);
  211. uint32_t device_id = 0;
  212. Status ret = profiling_manager.GetDeviceIdFromGraph(0, device_id);
  213. EXPECT_EQ(ret, ge::SUCCESS);
  214. }
  215. TEST_F(UtestGeProfilinganager, get_device_id_failed) {
  216. auto &profiling_manager = ge::ProfilingManager::Instance();
  217. profiling_manager.SetGraphIdToDeviceMap(0, 1);
  218. uint32_t device_id = 0;
  219. Status ret = profiling_manager.GetDeviceIdFromGraph(10, device_id);
  220. EXPECT_EQ(ret, ge::FAILED);
  221. }

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