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.

test_ffts_plus.cc 6.1 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "init/gelib.h"
  18. #include "opskernel_manager/ops_kernel_builder_manager.h"
  19. #include "external/ge/ge_api.h"
  20. #include "ge_running_env/ge_running_env_faker.h"
  21. #include "ge_graph_dsl/graph_dsl.h"
  22. #include "ge_running_env/fake_compound_engine.h"
  23. #include "ge_running_env/fake_op.h"
  24. #include "easy_graph/layout/graph_layout.h"
  25. #include "easy_graph/layout/engines/graph_easy/graph_easy_option.h"
  26. #include "easy_graph/layout/engines/graph_easy/graph_easy_executor.h"
  27. #include "ge_graph_dsl/assert/graph_assert.h"
  28. using namespace std;
  29. using namespace ge;
  30. namespace {
  31. bool IfNodeExist(const ComputeGraphPtr &graph, std::function<bool(const NodePtr &)> filter,
  32. bool direct_node_flag = true) {
  33. for (const auto &node : graph->GetNodes(direct_node_flag)) {
  34. if (filter(node)) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. void GetSubgraphsWithFilter(const ComputeGraphPtr &graph, std::function<bool(const ComputeGraphPtr &)> filter,
  41. std::vector<ComputeGraphPtr> &subgraphs) {
  42. for (const auto &subgraph : graph->GetAllSubgraphs()) {
  43. if (filter(subgraph)) {
  44. subgraphs.emplace_back(subgraph);
  45. }
  46. }
  47. }
  48. bool IsAllNodeMatch(const ComputeGraphPtr &graph, std::function<bool(const NodePtr &)> filter) {
  49. for (const auto &node : graph->GetAllNodes()) {
  50. if (!filter(node)) {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. }
  57. class TestFftsPlus : public testing::Test {
  58. protected:
  59. GeRunningEnvFaker ge_env;
  60. EG_NS::GraphEasyExecutor executor;
  61. void SetUp() {
  62. EG_NS::GraphLayout::GetInstance().Config(executor, nullptr);
  63. ge_env.InstallDefault()
  64. .Install(FakeCompoundEngine("ffts_plus", {"AIcoreEngine", "DNN_VM_AICPU"}).KernelInfoStore("ffts_plus"))
  65. .Install(FakeOp(GETNEXT).InfoStoreAndBuilder("AicpuLib"))
  66. .Install(FakeOp(HCOMREDUCE).InfoStoreAndBuilder("HcclLib"));
  67. }
  68. void TearDown() {}
  69. };
  70. /*
  71. * g1
  72. *
  73. * ┌──────────┐ (0,1) ┌────────┐ (0,0) ┌────────┐
  74. * │ const │ ───────> │ less │ ───────> │ reduce │
  75. * └──────────┘ └────────┘ └────────┘
  76. * ∧
  77. * │ (0,0)
  78. * │
  79. * ┌──────────┐ (0,0) ┌────────┐ (0,1) ┌────────┐
  80. * │ get_next │ ───────> │ add │ <─────── │ data1 │
  81. * └──────────┘ └────────┘ └────────┘
  82. *
  83. */
  84. TEST_F(TestFftsPlus, test_ffts_plus) {
  85. auto tensor = std::make_shared<GeTensor>();
  86. uint32_t value = 0;
  87. tensor->SetData((uint8_t *)&value, sizeof(uint32_t));
  88. DEF_GRAPH(g1) {
  89. CHAIN(NODE("get_next", GETNEXT)->NODE("add", ADD));
  90. CHAIN(NODE("data1", DATA)->NODE("add")->NODE("less", LESS)->NODE("reduce", HCOMREDUCE));
  91. CHAIN(NODE("const", OP_CFG(CONSTANTOP).Attr("value", tensor))->Node("less"));
  92. };
  93. auto graph = ToGeGraph(g1);
  94. // new session & add graph
  95. map<AscendString, AscendString> options;
  96. Session session(options);
  97. auto ret = session.AddGraph(1, graph, options);
  98. EXPECT_EQ(ret, SUCCESS);
  99. // build input tensor
  100. std::vector<InputTensorInfo> inputs;
  101. // build_graph through session
  102. ret = session.BuildGraph(1, inputs);
  103. EXPECT_EQ(ret, SUCCESS);
  104. CHECK_GRAPH(PreRunAfterBuild) {
  105. // node exist
  106. ASSERT_FALSE(IfNodeExist(graph, [](const NodePtr &node) { return node->GetName() == "get_next"; }));
  107. ASSERT_FALSE(IfNodeExist(graph, [](const NodePtr &node) { return node->GetName() == "add"; }));
  108. ASSERT_FALSE(IfNodeExist(graph, [](const NodePtr &node) { return node->GetName() == "less"; }));
  109. ASSERT_TRUE(IfNodeExist(graph, [](const NodePtr &node) { return node->GetType() == PARTITIONEDCALL; }));
  110. // subgraph exit
  111. ASSERT_EQ(graph->GetAllSubgraphs().size(), 1);
  112. std::vector<ComputeGraphPtr> subgraphs;
  113. GetSubgraphsWithFilter(graph,
  114. [](const ComputeGraphPtr &graph) {
  115. const auto &parent_node = graph->GetParentNode();
  116. if ((parent_node == nullptr) || (parent_node->GetOpDesc() == nullptr)) {
  117. return false;
  118. }
  119. return parent_node->GetOpDesc()->HasAttr(ATTR_NAME_FFTS_PLUS_SUB_GRAPH); },
  120. subgraphs);
  121. ASSERT_EQ(subgraphs.size(), 1);
  122. // subgraph node check
  123. const auto &subgraph = subgraphs[0];
  124. ASSERT_TRUE(subgraph != nullptr);
  125. ASSERT_TRUE(IsAllNodeMatch(subgraph,
  126. [](const NodePtr &node) {
  127. return node->GetOpDesc()->HasAttr(ATTR_NAME_THREAD_SCOPE_ID);
  128. }));
  129. const auto &parent_node = subgraph->GetParentNode();
  130. ASSERT_TRUE(parent_node != nullptr);
  131. ASSERT_TRUE(parent_node->GetOpDesc() != nullptr);
  132. int64_t stream_id = parent_node->GetOpDesc()->GetStreamId();
  133. ASSERT_TRUE(IsAllNodeMatch(subgraph,
  134. [stream_id](const NodePtr &node) {
  135. return node->GetOpDesc()->GetStreamId() == stream_id;
  136. }));
  137. };
  138. }

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