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

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