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.

single_op_task_unittest.cc 8.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <gtest/gtest.h>
  17. #include <vector>
  18. #include "graph/load/model_manager/model_utils.h"
  19. #include "graph/utils/graph_utils.h"
  20. #include "runtime/rt.h"
  21. #define protected public
  22. #define private public
  23. #include "single_op/single_op_model.h"
  24. #include "single_op/task/tbe_task_builder.h"
  25. #include "single_op/task/op_task.h"
  26. #include "single_op/task/tbe_task_builder.h"
  27. #include "external/register/op_tiling_registry.h"
  28. #undef private
  29. #undef protected
  30. using namespace std;
  31. using namespace testing;
  32. using namespace ge;
  33. using namespace optiling;
  34. class UtestSingleOpTask : public testing::Test {
  35. protected:
  36. void SetUp() {}
  37. void TearDown() {}
  38. };
  39. TEST_F(UtestSingleOpTask, test_build_kernel_task) {
  40. string model_data_str = "123456789";
  41. SingleOpModel model("model", model_data_str.c_str(), model_data_str.size());
  42. model.input_offset_list_.push_back(0);
  43. model.input_sizes_.push_back(16);
  44. model.output_offset_list_.push_back(0);
  45. model.output_sizes_.push_back(16);
  46. auto graph = make_shared<ComputeGraph>("graph");
  47. auto op_desc = make_shared<OpDesc>("Add", "Add");
  48. AttrUtils::SetStr(op_desc, TVM_ATTR_NAME_MAGIC, "RT_DEV_BINARY_MAGIC_ELF");
  49. std::vector<char> kernelBin;
  50. TBEKernelPtr tbe_kernel = std::make_shared<ge::OpKernelBin>("name/Add", std::move(kernelBin));
  51. op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel);
  52. std::string kernel_name("kernel/Add");
  53. AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name);
  54. vector<int64_t> shape{16, 16};
  55. GeShape ge_shape(shape);
  56. GeTensorDesc desc(ge_shape);
  57. op_desc->AddInputDesc(desc);
  58. op_desc->AddOutputDesc(desc);
  59. auto node = graph->AddNode(op_desc);
  60. std::mutex stream_mu_;
  61. rtStream_t stream_ = nullptr;
  62. StreamResource stream_resource(0);
  63. SingleOp single_op(&stream_resource, &stream_mu_, stream_);
  64. domi::TaskDef task_def;
  65. task_def.set_type(RT_MODEL_TASK_ALL_KERNEL);
  66. domi::KernelDefWithHandle *kernel_with_handle = task_def.mutable_kernel_with_handle();
  67. kernel_with_handle->set_original_kernel_key("");
  68. kernel_with_handle->set_node_info("");
  69. kernel_with_handle->set_block_dim(32);
  70. kernel_with_handle->set_args_size(64);
  71. string args(64, '1');
  72. kernel_with_handle->set_args(args.data(), 64);
  73. domi::KernelContext *context = kernel_with_handle->mutable_context();
  74. context->set_op_index(1);
  75. context->set_kernel_type(2); // ccKernelType::TE
  76. uint16_t args_offset[9] = {0};
  77. context->set_args_offset(args_offset, 9 * sizeof(uint16_t));
  78. model.op_list_[1] = node;
  79. TbeOpTask task_tmp;
  80. TbeOpTask *task = &task_tmp;
  81. ASSERT_EQ(model.BuildKernelTask(task_def, &task), SUCCESS);
  82. ge::DataBuffer data_buffer;
  83. vector<GeTensorDesc> input_desc;
  84. vector<DataBuffer> input_buffers = { data_buffer };
  85. vector<GeTensorDesc> output_desc;
  86. vector<DataBuffer> output_buffers = { data_buffer };
  87. task->node_ = node;
  88. OpTilingFunc op_tiling_func = [](const TeOpParas &, const OpCompileInfo &, OpRunInfo &) -> bool {return true;};
  89. OpTilingRegistryInterf("Add", op_tiling_func);
  90. ge::AttrUtils::SetStr(op_desc, "compile_info_key", "op_compile_info_key");
  91. ge::AttrUtils::SetStr(op_desc, "compile_info_json", "op_compile_info_json");
  92. char c = '0';
  93. char* buffer = &c;
  94. task->tiling_buffer_ = buffer;
  95. task->max_tiling_size_ = 64;
  96. task->tiling_data_ = "tiling_data";
  97. task->arg_size_ = 64;
  98. task->args_.reset(new (std::nothrow) uint8_t[sizeof(void *) * 3]);
  99. ASSERT_EQ(task->LaunchKernel(input_desc, input_buffers, output_desc, output_buffers, stream_), SUCCESS);
  100. char *handle = "00";
  101. task->SetHandle(handle);
  102. ASSERT_EQ(task->LaunchKernel(input_desc, input_buffers, output_desc, output_buffers, stream_), SUCCESS);
  103. }
  104. TEST_F(UtestSingleOpTask, test_update_ioaddr) {
  105. auto graph = make_shared<ComputeGraph>("graph");
  106. auto op_desc = make_shared<OpDesc>("Add", "Add");
  107. GeTensorDesc desc;
  108. op_desc->AddInputDesc(desc);
  109. op_desc->AddInputDesc(desc);
  110. op_desc->AddOutputDesc(desc);
  111. vector<bool> is_input_const = { true, false };
  112. op_desc->SetIsInputConst(is_input_const);
  113. auto node = graph->AddNode(op_desc);
  114. TbeOpTask task;
  115. task.op_desc_ = op_desc;
  116. task.node_ = node;
  117. ASSERT_EQ(task.SetArgIndex(), SUCCESS);
  118. task.arg_size_ = sizeof(void *) * 4;
  119. task.args_.reset(new (std::nothrow) uint8_t[task.arg_size_]);
  120. task.arg_index_ = {0};
  121. task.input_num_ = 2;
  122. task.output_num_ = 1;
  123. vector<void *> args;
  124. vector<DataBuffer> inputs;
  125. vector<DataBuffer> outputs;
  126. ASSERT_EQ(task.UpdateIoAddr(inputs, outputs), ACL_ERROR_GE_PARAM_INVALID);
  127. ge::DataBuffer data_buffer;
  128. inputs = { data_buffer };
  129. outputs = { data_buffer };
  130. ASSERT_EQ(task.UpdateIoAddr(inputs, outputs), SUCCESS);
  131. task.tiling_buffer_ = (void *)0x0001;
  132. task.workspaces_ = { (void *)0x0002 };
  133. ASSERT_EQ(task.UpdateTilingArgs(nullptr), SUCCESS);
  134. task.tiling_buffer_ = nullptr;
  135. }
  136. TEST_F(UtestSingleOpTask, test_atomic_exec) {
  137. auto graph = make_shared<ComputeGraph>("graph");
  138. auto op_desc = make_shared<OpDesc>("Add", "Add");
  139. GeTensorDesc desc;
  140. op_desc->AddInputDesc(desc);
  141. op_desc->AddOutputDesc(desc);
  142. auto node = graph->AddNode(op_desc);
  143. AtomicAddrCleanOpTask task;
  144. task.op_desc_ = op_desc;
  145. task.node_ = node;
  146. vector<DataBuffer> inputs;
  147. vector<DataBuffer> outputs;
  148. std::vector<int64_t> atomic_output_indices;
  149. ge::AttrUtils::SetListInt(op_desc, ATOMIC_ATTR_OUTPUT_INDEX, atomic_output_indices);
  150. ASSERT_EQ(task.InitAtomicAddrCleanIndices(), INTERNAL_ERROR);
  151. atomic_output_indices = { 0 };
  152. ge::AttrUtils::SetListInt(op_desc, ATOMIC_ATTR_OUTPUT_INDEX, atomic_output_indices);
  153. ASSERT_EQ(task.InitAtomicAddrCleanIndices(), INTERNAL_ERROR);
  154. task.arg_size_ = sizeof(void *) * 2;
  155. task.args_.reset(new (std::nothrow) uint8_t[task.arg_size_]);
  156. ASSERT_EQ(task.InitAtomicAddrCleanIndices(), SUCCESS);
  157. ASSERT_EQ(task.UpdateIoAddr(inputs, outputs), ACL_ERROR_GE_PARAM_INVALID);
  158. ge::DataBuffer data_buffer;
  159. outputs = { data_buffer };
  160. ASSERT_EQ(task.UpdateIoAddr(inputs, outputs), SUCCESS);
  161. task.tiling_buffer_ = (void *)0x0001;
  162. ASSERT_EQ(task.UpdateTilingArgs(nullptr), SUCCESS);
  163. task.tiling_buffer_ = nullptr;
  164. optiling::utils::OpRunInfo run_info(0, true, 0);
  165. task.CalcTilingInfo(run_info);
  166. }
  167. TEST_F(UtestSingleOpTask, test_aicpu_task_update_io_addr) {
  168. AiCpuCCTask task;
  169. task.num_inputs_ = 2;
  170. task.num_outputs_ = 1;
  171. task.input_is_const_ = {true, false};
  172. int total_addr = 3;
  173. uint32_t* addrs[total_addr] = {nullptr, nullptr, nullptr};
  174. task.io_addr_ = reinterpret_cast<uintptr_t*>(addrs);
  175. task.io_addr_num_ = total_addr;
  176. {
  177. vector<DataBuffer> inputs(1, DataBuffer());
  178. vector<DataBuffer> outputs(1, DataBuffer());
  179. auto ret = task.UpdateIoAddr(inputs, outputs);
  180. ASSERT_EQ(ret, SUCCESS);
  181. ASSERT_EQ(addrs[0], nullptr);
  182. ASSERT_EQ(addrs[1], nullptr);
  183. ASSERT_EQ(addrs[2], nullptr);
  184. }
  185. {
  186. uint32_t data_buf[2];
  187. vector<DataBuffer> inputs{DataBuffer(&data_buf[0], 4, false)};
  188. vector<DataBuffer> outputs{DataBuffer(&data_buf[1], 4, false)};
  189. auto ret = task.UpdateIoAddr(inputs, outputs);
  190. ASSERT_EQ(ret, SUCCESS);
  191. ASSERT_EQ(addrs[0], nullptr);
  192. ASSERT_EQ(addrs[1], &data_buf[0]);
  193. ASSERT_EQ(addrs[2], &data_buf[1]);
  194. }
  195. {
  196. uint32_t data_buf[2];
  197. vector<DataBuffer> inputs{DataBuffer(nullptr, 4, false)};
  198. vector<DataBuffer> outputs{DataBuffer(&data_buf[1], 4, false)};
  199. auto ret = task.UpdateIoAddr(inputs, outputs);
  200. ASSERT_EQ(ret, PARAM_INVALID);
  201. }
  202. {
  203. uint32_t data_buf[2];
  204. vector<DataBuffer> inputs{DataBuffer(&data_buf[0], 4, false)};
  205. vector<DataBuffer> outputs{DataBuffer(nullptr, 4, false)};
  206. auto ret = task.UpdateIoAddr(inputs, outputs);
  207. ASSERT_EQ(ret, PARAM_INVALID);
  208. }
  209. }

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