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_api_unittest.cc 6.2 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Copyright 2021 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 <vector>
  18. #include <string>
  19. #include <map>
  20. #define protected public
  21. #define private public
  22. #include "common/ge/ge_util.h"
  23. #include "proto/ge_ir.pb.h"
  24. #include "inc/external/ge/ge_api.h"
  25. #include "session/session_manager.h"
  26. using namespace std;
  27. namespace ge {
  28. class UtestGeApi : public testing::Test {
  29. protected:
  30. void SetUp() override {}
  31. void TearDown() override {}
  32. };
  33. TEST_F(UtestGeApi, run_graph_with_stream) {
  34. vector<Tensor> inputs;
  35. vector<Tensor> outputs;
  36. std::map<std::string, std::string> options;
  37. Session session(options);
  38. auto ret = session.RunGraphWithStreamAsync(10, nullptr, inputs, outputs);
  39. ASSERT_NE(ret, SUCCESS);
  40. SessionManager session_manager;
  41. session_manager.init_flag_ = true;
  42. ret = session_manager.RunGraphWithStreamAsync(10, 10, nullptr, inputs, outputs);
  43. ASSERT_NE(ret, SUCCESS);
  44. InnerSession inner_session(1, options);
  45. inner_session.init_flag_ = true;
  46. ret = inner_session.RunGraphWithStreamAsync(10, nullptr, inputs, outputs);
  47. ASSERT_NE(ret, SUCCESS);
  48. }
  49. TEST_F(UtestGeApi, build_graph_success) {
  50. vector<Tensor> inputs;
  51. std::map<std::string, std::string> options;
  52. Session session(options);
  53. auto ret = session.BuildGraph(1, inputs);
  54. ASSERT_NE(ret, SUCCESS);
  55. }
  56. TEST_F(UtestGeApi, ge_initialize_modify_mixlist) {
  57. std::map<std::string, std::string> options = {
  58. {ge::MODIFY_MIXLIST, "/mixlist.json"}
  59. };
  60. auto ret = GEInitialize(options);
  61. ASSERT_NE(ret, SUCCESS);
  62. }
  63. TEST_F(UtestGeApi, ge_not_initialized) {
  64. EXPECT_EQ(GEFinalize(), SUCCESS);
  65. std::map<std::string, std::string> options;
  66. std::map<AscendString, AscendString> ascend_options;
  67. Session session(options);
  68. GraphId graph_id = 1;
  69. const auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  70. Graph graph = GraphUtils::CreateGraphFromComputeGraph(compute_graph);
  71. EXPECT_EQ(session.AddGraph(graph_id, graph), FAILED);
  72. EXPECT_EQ(session.AddGraph(graph_id, graph, ascend_options), FAILED);
  73. EXPECT_EQ(session.AddGraphWithCopy(graph_id, graph), FAILED);
  74. EXPECT_EQ(session.AddGraphWithCopy(graph_id, graph, ascend_options), FAILED);
  75. vector<Tensor> inputs;
  76. vector<InputTensorInfo> tensors;
  77. EXPECT_EQ(session.BuildGraph(graph_id, inputs), FAILED);
  78. EXPECT_EQ(session.BuildGraph(graph_id, tensors), FAILED);
  79. vector<Tensor> outputs;
  80. EXPECT_EQ(session.RunGraph(graph_id, inputs, outputs), FAILED);
  81. EXPECT_EQ(session.RunGraphWithStreamAsync(graph_id, nullptr, inputs, outputs), FAILED);
  82. EXPECT_EQ(session.RunGraphAsync(graph_id, inputs, nullptr), FAILED);
  83. vector<string> var_inputs;
  84. EXPECT_EQ(session.GetVariables(var_inputs, outputs), FAILED);
  85. vector<AscendString> var_names;
  86. EXPECT_EQ(session.GetVariables(var_names, outputs), FAILED);
  87. std::string key;
  88. pCallBackFunc ge_callback;
  89. EXPECT_EQ(session.RegisterCallBackFunc(key, ge_callback), FAILED);
  90. session::pCallBackFunc session_callback;
  91. EXPECT_EQ(session.RegisterCallBackFunc(key.c_str(), session_callback), FAILED);
  92. EXPECT_FALSE(session.IsGraphNeedRebuild(graph_id));
  93. EXPECT_EQ(session.RemoveGraph(graph_id), FAILED);
  94. EXPECT_EQ(GEFinalize(), SUCCESS);
  95. }
  96. TEST_F(UtestGeApi, ge_session_ascend_string) {
  97. std::map<AscendString, AscendString> options;
  98. EXPECT_EQ(GEInitialize(options), SUCCESS);
  99. Session session(options);
  100. GraphId graph_id = 1;
  101. const auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  102. EXPECT_EQ(session.AddGraph(graph_id, GraphUtils::CreateGraphFromComputeGraph(compute_graph)), SUCCESS);
  103. EXPECT_TRUE(session.IsGraphNeedRebuild(graph_id));
  104. EXPECT_EQ(session.RemoveGraph(graph_id), SUCCESS);
  105. EXPECT_EQ(GEFinalize(), SUCCESS);
  106. }
  107. TEST_F(UtestGeApi, ge_session_test) {
  108. std::map<std::string, std::string> options;
  109. EXPECT_EQ(GEInitialize(options), SUCCESS);
  110. std::map<AscendString, AscendString> ascend_options;
  111. Session session(options);
  112. GraphId graph_id = 1;
  113. const auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  114. Graph graph = GraphUtils::CreateGraphFromComputeGraph(compute_graph);
  115. EXPECT_EQ(session.AddGraph(graph_id, graph), SUCCESS);
  116. EXPECT_EQ(session.AddGraph(graph_id, graph, ascend_options), SUCCESS);
  117. EXPECT_EQ(session.AddGraphWithCopy(graph_id, graph), FAILED);
  118. EXPECT_EQ(session.AddGraphWithCopy(graph_id, graph, ascend_options), FAILED);
  119. vector<Tensor> inputs;
  120. vector<InputTensorInfo> tensors;
  121. EXPECT_EQ(session.BuildGraph(graph_id, inputs), FAILED);
  122. EXPECT_EQ(session.BuildGraph(graph_id, tensors), FAILED);
  123. vector<Tensor> outputs;
  124. EXPECT_EQ(session.RunGraph(graph_id, inputs, outputs), FAILED);
  125. EXPECT_EQ(session.RunGraphWithStreamAsync(graph_id, nullptr, inputs, outputs), FAILED);
  126. EXPECT_EQ(session.RunGraphAsync(graph_id, inputs, nullptr), SUCCESS); // Push to queue.
  127. vector<string> var_inputs;
  128. EXPECT_EQ(session.GetVariables(var_inputs, outputs), FAILED);
  129. vector<AscendString> var_names;
  130. EXPECT_EQ(session.GetVariables(var_names, outputs), FAILED);
  131. std::string key;
  132. pCallBackFunc ge_callback;
  133. EXPECT_EQ(session.RegisterCallBackFunc(key, ge_callback), SUCCESS);
  134. session::pCallBackFunc session_callback;
  135. EXPECT_EQ(session.RegisterCallBackFunc(key.c_str(), session_callback), SUCCESS);
  136. EXPECT_TRUE(session.IsGraphNeedRebuild(graph_id));
  137. EXPECT_EQ(session.RemoveGraph(graph_id), SUCCESS);
  138. EXPECT_EQ(GEFinalize(), SUCCESS);
  139. }
  140. } // namespace ge

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