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.7 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. //
  106. // std::string fp_point;
  107. // std::string bp_point;
  108. // ProfilingManager::Instance().GetFpBpPoint(fp_point, bp_point);
  109. // EXPECT_EQ(fp_point, "Data_0");
  110. // EXPECT_EQ(bp_point, "addn");
  111. //}
  112. //TEST_F(UtestGeProfilinganager, get_fp_bp_point_empty) {
  113. // // fp bp empty
  114. // map<std::string, string> options_map = {
  115. // { OPTION_EXEC_PROFILING_OPTIONS,
  116. // R"({"result_path":"/data/profiling","training_trace":"on","task_trace":"on","aicpu_trace":"on","ai_core_metrics":"ResourceConflictRatio"})"}};
  117. // GEThreadLocalContext &context = GetThreadLocalContext();
  118. // context.SetGraphOption(options_map);
  119. // std::string fp_point = "fp";
  120. // std::string bp_point = "bp";
  121. // ProfilingManager::Instance().bp_point_ = "";
  122. // ProfilingManager::Instance().fp_point_ = "";
  123. // ProfilingManager::Instance().GetFpBpPoint(fp_point, bp_point);
  124. // EXPECT_EQ(fp_point, "");
  125. // EXPECT_EQ(bp_point, "");
  126. //}
  127. TEST_F(UtestGeProfilinganager, set_step_info_success) {
  128. uint64_t index_id = 0;
  129. auto stream = (rtStream_t)0x1;
  130. Status ret = ProfSetStepInfo(index_id, 0, stream);
  131. EXPECT_EQ(ret, ge::SUCCESS);
  132. ret = ProfSetStepInfo(index_id, 1, stream);
  133. EXPECT_EQ(ret, ge::SUCCESS);
  134. }
  135. TEST_F(UtestGeProfilinganager, set_step_info_failed) {
  136. uint64_t index_id = 0;
  137. auto stream = (rtStream_t)0x1;
  138. Status ret = ProfSetStepInfo(index_id, 1, stream);
  139. EXPECT_EQ(ret, ge::FAILED);
  140. }
  141. TEST_F(UtestGeProfilinganager, get_device_from_graph) {
  142. GraphId graph_id = 1;
  143. uint32_t device_id = 0;
  144. GraphManager graph_manager;
  145. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  146. graph_manager.AddGraphNode(graph_id, graph_node);
  147. graph_manager.SetAddGraphCondition(graph_id, 2);
  148. Graph graph("test_graph");
  149. CreateGraph(graph);
  150. std::map<std::string, std::string> options;
  151. OmgContext context;
  152. Status ret = graph_manager.AddGraph(graph_id, graph, options, context);
  153. EXPECT_EQ(ret, ge::SUCCESS);
  154. ret = ProfGetDeviceFormGraphId(graph_id, device_id);
  155. EXPECT_EQ(ret, ge::SUCCESS);
  156. }
  157. TEST_F(UtestGeProfilinganager, handle_subscribe_info) {
  158. ProfCommandHandleType prof_type = kProfCommandhandleModelSubscribe;
  159. ProfCommandHandleData prof_data;
  160. prof_data.profSwitch = 0;
  161. prof_data.modelId = 1;
  162. domi::GetContext().train_flag = true;
  163. auto prof_ptr = std::make_shared<ProfCommandHandleData>(prof_data);
  164. Status ret = ProfCommandHandle(prof_type, static_cast<void *>(prof_ptr.get()), sizeof(prof_data));
  165. EXPECT_EQ(ret, ge::SUCCESS);
  166. }
  167. TEST_F(UtestGeProfilinganager, handle_unsubscribe_info) {
  168. ProfCommandHandleType prof_type = kProfCommandhandleModelUnsubscribe;
  169. ProfCommandHandleData prof_data;
  170. prof_data.profSwitch = 0;
  171. prof_data.modelId = 1;
  172. domi::GetContext().train_flag = true;
  173. auto &profiling_manager = ge::ProfilingManager::Instance();
  174. profiling_manager.SetSubscribeInfo(0, 1, true);
  175. auto prof_ptr = std::make_shared<ProfCommandHandleData>(prof_data);
  176. Status ret = ProfCommandHandle(prof_type, static_cast<void *>(prof_ptr.get()), sizeof(prof_data));
  177. profiling_manager.CleanSubscribeInfo();
  178. }
  179. TEST_F(UtestGeProfilinganager, set_subscribe_info) {
  180. auto &profiling_manager = ge::ProfilingManager::Instance();
  181. profiling_manager.SetSubscribeInfo(0, 1, true);
  182. const auto &subInfo = profiling_manager.GetSubscribeInfo();
  183. EXPECT_EQ(subInfo.prof_switch, 0);
  184. EXPECT_EQ(subInfo.graph_id, 1);
  185. EXPECT_EQ(subInfo.is_subscribe, true);
  186. }
  187. TEST_F(UtestGeProfilinganager, clean_subscribe_info) {
  188. auto &profiling_manager = ge::ProfilingManager::Instance();
  189. profiling_manager.CleanSubscribeInfo();
  190. const auto &subInfo = profiling_manager.GetSubscribeInfo();
  191. EXPECT_EQ(subInfo.prof_switch, 0);
  192. EXPECT_EQ(subInfo.graph_id, 0);
  193. EXPECT_EQ(subInfo.is_subscribe, false);
  194. }
  195. TEST_F(UtestGeProfilinganager, get_model_id_success) {
  196. auto &profiling_manager = ge::ProfilingManager::Instance();
  197. profiling_manager.SetGraphIdToModelMap(0, 1);
  198. uint32_t model_id = 0;
  199. Status ret = profiling_manager.GetModelIdFromGraph(0, model_id);
  200. EXPECT_EQ(ret, ge::SUCCESS);
  201. }
  202. TEST_F(UtestGeProfilinganager, get_model_id_failed) {
  203. auto &profiling_manager = ge::ProfilingManager::Instance();
  204. profiling_manager.SetGraphIdToModelMap(0, 1);
  205. uint32_t model_id = 0;
  206. Status ret = profiling_manager.GetModelIdFromGraph(10, model_id);
  207. EXPECT_EQ(ret, ge::FAILED);
  208. }
  209. TEST_F(UtestGeProfilinganager, get_device_id_success) {
  210. auto &profiling_manager = ge::ProfilingManager::Instance();
  211. profiling_manager.SetGraphIdToDeviceMap(0, 1);
  212. uint32_t device_id = 0;
  213. Status ret = profiling_manager.GetDeviceIdFromGraph(0, device_id);
  214. EXPECT_EQ(ret, ge::SUCCESS);
  215. }
  216. TEST_F(UtestGeProfilinganager, get_device_id_failed) {
  217. auto &profiling_manager = ge::ProfilingManager::Instance();
  218. profiling_manager.SetGraphIdToDeviceMap(0, 1);
  219. uint32_t device_id = 0;
  220. Status ret = profiling_manager.GetDeviceIdFromGraph(10, device_id);
  221. EXPECT_EQ(ret, ge::FAILED);
  222. }

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