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.

known_node_executor_unittest.cc 5.6 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright 2019-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 <gmock/gmock.h>
  18. #include <vector>
  19. #include <memory>
  20. #define protected public
  21. #define private public
  22. #include "hybrid/node_executor/compiledsubgraph/known_node_executor.h"
  23. #include "common/dump/dump_manager.h"
  24. #undef private
  25. #undef protected
  26. #include "graph/manager/graph_mem_allocator.h"
  27. #include "../graph/passes/graph_builder_utils.h"
  28. #include "../inc/graph/utils/graph_utils.h"
  29. using namespace std;
  30. using namespace testing;
  31. using namespace ge;
  32. using namespace hybrid;
  33. class UnknownNodeExecutorTest : public testing::Test {
  34. protected:
  35. void SetUp() {}
  36. void TearDown() {}
  37. };
  38. namespace {
  39. class KnownNodeTaskMock : public KnownNodeTask {
  40. public:
  41. KnownNodeTaskMock(std::shared_ptr<DavinciModel> davinci_model): KnownNodeTask(davinci_model) {};
  42. ~KnownNodeTaskMock() override = default;
  43. MOCK_METHOD2(DoInitDavinciModel, Status(void *, size_t));
  44. };
  45. }
  46. static ge::OpDescPtr CreateOpDesc(string name = "", string type = "") {
  47. auto op_desc = std::make_shared<ge::OpDesc>(name, type);
  48. op_desc->SetStreamId(0);
  49. op_desc->SetId(0);
  50. op_desc->SetWorkspace({});
  51. ;
  52. op_desc->SetWorkspaceBytes({});
  53. op_desc->SetInputOffset({});
  54. op_desc->SetOutputOffset({});
  55. ge::AttrUtils::SetStr(op_desc, ge::TVM_ATTR_NAME_MAGIC, "RT_DEV_BINARY_MAGIC_ELF_AIVEC");
  56. bool support_dynamic = true;
  57. ge::AttrUtils::GetBool(op_desc, "support_dynamicshape", support_dynamic);
  58. return op_desc;
  59. }
  60. static ComputeGraphPtr BuildDataDirectConnectGraph() {
  61. const char *kRefIndex = "_parent_node_index";
  62. ge::ut::GraphBuilder builder("subgraph");
  63. auto data = builder.AddNode("Data", "Data", 1, 1);
  64. auto netoutput = builder.AddNode("NetOutput", "NetOutput", 1, 1);
  65. (void)AttrUtils::SetInt(netoutput->GetOpDesc()->MutableInputDesc(0), kRefIndex, 0);
  66. builder.AddDataEdge(data, 0, netoutput, 0);
  67. return builder.GetGraph();
  68. }
  69. TEST_F(UnknownNodeExecutorTest, test_init_davinci_model) {
  70. auto davinci_model = std::make_shared<DavinciModel>(0, nullptr);
  71. davinci_model->SetDeviceId(0);
  72. davinci_model->SetKnownNode(true);
  73. auto ge_model = make_shared<GeModel>();
  74. AttrUtils::SetInt(ge_model, ATTR_MODEL_VAR_SIZE, 0);
  75. AttrUtils::SetInt(ge_model, ATTR_MODEL_MEMORY_SIZE, 1024);
  76. davinci_model->Assign(ge_model);
  77. HybridModel model(nullptr);
  78. KnownNodeTaskMock mock(davinci_model);
  79. DumpProperties dump_properties;
  80. dump_properties.enable_dump_ = "1";
  81. DumpManager::GetInstance().AddDumpProperties(model.GetSessionId(), dump_properties);
  82. EXPECT_CALL(mock, DoInitDavinciModel).WillRepeatedly(::testing::Return(SUCCESS));
  83. ASSERT_EQ(mock.InitDavinciModel(model, model.GetModelWeight("subgraph")), SUCCESS);
  84. int32_t buffer[8];
  85. model.weight_buffer_map_.emplace("subgraph", TensorBuffer::Create(buffer, sizeof(buffer)));
  86. ASSERT_EQ(mock.InitDavinciModel(model, model.GetModelWeight("subgraph")), SUCCESS);
  87. }
  88. TEST_F(UnknownNodeExecutorTest, TestParseAttrForAllocatingOutputs) {
  89. ut::GraphBuilder builder("test-graph");
  90. auto data_node = builder.AddNode("Data0", DATA, 1, 1);
  91. auto netoutput_node = builder.AddNode("NodeOutput", NETOUTPUT, 2, 2);
  92. builder.AddDataEdge(data_node, 0, netoutput_node, 0);
  93. auto const_node = builder.AddNode("Const0", CONSTANT, 0, 1);
  94. builder.AddDataEdge(const_node, 0, netoutput_node, 1);
  95. auto graph = builder.GetGraph();
  96. ut::GraphBuilder builder2("root-graph");
  97. auto partitioned_call = builder2.AddNode("Node0", PARTITIONEDCALL, 1, 2);
  98. NodeItem node_item(partitioned_call);
  99. ASSERT_EQ(KnownNodeExecutor::ParseAttrForAllocatingOutputs(node_item, *graph), SUCCESS);
  100. ASSERT_EQ(node_item.ref_outputs.size(), 1);
  101. ASSERT_EQ(node_item.ref_outputs[1], const_node);
  102. ASSERT_EQ(node_item.reuse_inputs.size(), 1);
  103. ASSERT_EQ(node_item.reuse_inputs[0], 0);
  104. }
  105. TEST_F(UnknownNodeExecutorTest, TestSetGlobalStep) {
  106. OpDescPtr op_desc = CreateOpDesc("PartitionedCall", "PartitionedCall");
  107. auto root_graph = make_shared<ComputeGraph>("root_graph");
  108. auto node = root_graph->AddNode(op_desc);
  109. node->SetOwnerComputeGraph(root_graph);
  110. auto sub_graph = BuildDataDirectConnectGraph();
  111. sub_graph->SetParentGraph(root_graph);
  112. sub_graph->SetParentNode(node);
  113. node->GetOpDesc()->AddSubgraphName("subgraph");
  114. node->GetOpDesc()->SetSubgraphInstanceName(0, "subgraph");
  115. root_graph->AddSubgraph("subgraph", sub_graph);
  116. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(root_graph);
  117. HybridModel hybrid_model(ge_root_model);
  118. auto *step_id = new int64_t[1];
  119. step_id[0] = 520;
  120. std::unique_ptr<TensorValue> tensor_value;
  121. tensor_value.reset(new(std::nothrow)TensorValue((void*)step_id, sizeof(step_id)));
  122. hybrid_model.variable_tensors_.insert({"ge_global_step", std::move(tensor_value)});
  123. KnownNodeExecutor known_node_executor;
  124. std::shared_ptr<DavinciModel> davinci_model = MakeShared<DavinciModel>(0, nullptr);
  125. known_node_executor.SettingDaviciModel(hybrid_model, node, davinci_model);
  126. EXPECT_EQ(*(static_cast<int64_t*>(davinci_model->global_step_addr_)), 520);
  127. }

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