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.0 kB

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

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