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

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