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.

new_op_test_utils.h 16 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #ifndef OME_REBUILD_OME_OP_TEST_UTILS_H
  17. #define OME_REBUILD_OME_OP_TEST_UTILS_H
  18. #include <gtest/gtest.h>
  19. #include <memory>
  20. #include <utility>
  21. #include "common/fmk_types.h"
  22. #include "common/helper/model_helper.h"
  23. #include "common/op/attr_value_util.h"
  24. #include "common/properties_manager.h"
  25. #include "common/types.h"
  26. #include "executor/ge_executor.h"
  27. #include "graph/buffer.h"
  28. #include "graph/debug/ge_attr_define.h"
  29. #include "graph/ge_attr_value.h"
  30. #include "graph/model_serialize.h"
  31. #include "graph/utils/graph_utils.h"
  32. #include "graph/utils/op_desc_utils.h"
  33. #include "graph/utils/tensor_utils.h"
  34. #include "proto/ge_ir.pb.h"
  35. #define protected public
  36. #define private public
  37. #include "graph/compute_graph.h"
  38. #include "graph/debug/ge_attr_define.h"
  39. #include "graph/load/new_model_manager/davinci_model.h"
  40. #include "graph/node.h"
  41. #include "graph/op_desc.h"
  42. #include "graph/utils/attr_utils.h"
  43. #include "graph/utils/graph_utils.h"
  44. #include "graph/utils/op_desc_utils.h"
  45. #include "graph/utils/tensor_utils.h"
  46. #undef protected
  47. #undef private
  48. using namespace ge;
  49. class GlobalModelData {
  50. public:
  51. GlobalModelData() {}
  52. ~GlobalModelData() {
  53. if (data_.model_data != nullptr) {
  54. delete[](uint8_t *) data_.model_data;
  55. data_.model_data = nullptr;
  56. }
  57. }
  58. ge::ModelData data_;
  59. };
  60. static GlobalModelData g_model_data;
  61. class OmeTestOpUtils {
  62. public:
  63. static void InitModel(std::shared_ptr<ge::DavinciModel> davinciModel) { InitModel(*davinciModel); }
  64. static ge::NodePtr GenNodeFromOpDesc(ge::OpDescPtr op_desc) {
  65. if (!op_desc) {
  66. return nullptr;
  67. }
  68. auto g = std::make_shared<ge::ComputeGraph>("g");
  69. return g->AddNode(std::move(op_desc));
  70. }
  71. static void AddInputOutputToTaskModel(std::shared_ptr<ge::Model> model,
  72. std::shared_ptr<domi::ModelTaskDef> model_task_def) {
  73. uint32_t stream_num111 = model_task_def->stream_num();
  74. uint32_t weights_num = model_task_def->weight_size();
  75. uint32_t mem_num = model_task_def->memory_size();
  76. int64_t memory_size = 0;
  77. int64_t weight_size = 0;
  78. (void)ge::AttrUtils::GetInt(model.get(), ATTR_MODEL_MEMORY_SIZE, memory_size);
  79. (void)ge::AttrUtils::GetInt(model.get(), ATTR_MODEL_WEIGHT_SIZE, weight_size);
  80. // Save memory_size/weight_size/stream_num/event_num to proto
  81. model_task_def->set_memory_size(memory_size);
  82. model_task_def->set_weight_size(weight_size);
  83. int64_t stream_num = 0;
  84. (void)ge::AttrUtils::GetInt(model.get(), ATTR_MODEL_STREAM_NUM, stream_num);
  85. model_task_def->set_stream_num(stream_num);
  86. ge::ComputeGraphPtr graph = ge::GraphUtils::GetComputeGraph(model->GetGraph());
  87. vector<ConstOpDescPtr> op_desc_ptrs;
  88. for (const auto &node_ptr : graph->GetAllNodes()) {
  89. if (node_ptr->GetType() == DATA_TYPE || node_ptr->GetType() == ANN_DATA_TYPE) {
  90. op_desc_ptrs.push_back(node_ptr->GetOpDesc());
  91. continue;
  92. }
  93. for (auto tensor_desc : node_ptr->GetOpDesc()->GetAllOutputsDescPtr()) {
  94. bool is_output = false;
  95. ge::TensorUtils::GetOutputTensor(*tensor_desc, is_output);
  96. if (is_output) {
  97. // output Op and add to array
  98. op_desc_ptrs.push_back(node_ptr->GetOpDesc());
  99. break;
  100. }
  101. }
  102. }
  103. // save multi OpDescPtr to attr
  104. ge::ModelSerialize model_serialize;
  105. for (auto op_desc_ptr : op_desc_ptrs) {
  106. ge::Buffer buffer = model_serialize.SerializeOpDesc(op_desc_ptr);
  107. model_task_def->add_op(string(reinterpret_cast<const char *>(buffer.GetData()), buffer.GetSize()));
  108. }
  109. int64_t run_mode = -1;
  110. for (auto node_ptr : graph->GetAllNodes()) {
  111. // TE CUSTOM op need to init
  112. if (ge::AttrUtils::GetInt(node_ptr->GetOpDesc(), ATTR_NAME_IMPLY_TYPE, run_mode) &&
  113. run_mode != (uint32_t)domi::ImplyType::BUILDIN && run_mode != (uint32_t)domi::ImplyType::INVALID) {
  114. (*(model_task_def->mutable_attr()))["contain_custom"] = "1";
  115. break;
  116. }
  117. }
  118. }
  119. static void LoadStandardModelDataLocal(ge::ModelData &data) {
  120. static const std::string STANDARD_MODEL_DATA_PATH =
  121. "llt/framework/domi/ut/ome/test/data/standard_partition_model.txt";
  122. ge::proto::ModelDef model_def;
  123. ReadProtoFromText(STANDARD_MODEL_DATA_PATH.c_str(), &model_def);
  124. data.model_len = model_def.ByteSizeLong();
  125. data.model_data = new uint8_t[data.model_len];
  126. model_def.SerializePartialToArray(data.model_data, data.model_len);
  127. }
  128. static void InitModel(ge::DavinciModel &davinciModel) {
  129. ge::ModelData data;
  130. LoadStandardModelDataLocal(data);
  131. std::shared_ptr<ge::Model> model_ = std::make_shared<ge::Model>();
  132. ge::Model::Load((uint8_t *)data.model_data, data.model_len, *model_);
  133. GeModelPtr ge_model;
  134. ModelHelper::TransModelToGeModel(model_, ge_model);
  135. davinciModel.Assign(ge_model);
  136. if (data.model_data != nullptr) {
  137. delete[](uint8_t *) data.model_data;
  138. }
  139. }
  140. static void InitEmptyModel(ge::DavinciModel &davinciModel) {
  141. auto model = std::make_shared<ge::Model>();
  142. ge::AttrUtils::SetInt(model, ATTR_MODEL_MEMORY_SIZE, 81000000);
  143. ge::AttrUtils::SetInt(model, ATTR_MODEL_WEIGHT_SIZE, 4100000);
  144. ge::AttrUtils::SetInt(model, ATTR_MODEL_STREAM_NUM, 1);
  145. ge::AttrUtils::SetInt(model, ATTR_MODEL_EVENT_NUM, 1);
  146. ge::AttrUtils::SetInt(model, MODEL_ATTR_TASK_GEN_BASE_ADDR, 0x123);
  147. ge::AttrUtils::SetInt(model, MODEL_ATTR_TASK_GEN_WEIGHT_ADDR, 0x456);
  148. ge::AttrUtils::SetInt(model, ATTR_MODEL_BATCH_NUM, 1);
  149. // ge::AttrUtils::SetStr(model, ATTR_MODEL_TARGET_TYPE, "MINI"); // domi::MINI
  150. auto compute_graph = std::make_shared<ge::ComputeGraph>("graph");
  151. ge::GeAttrValue::BYTES buffer(4100000, 0);
  152. ge::AttrUtils::SetBytes(compute_graph, "weights_data", buffer);
  153. auto graph = ge::GraphUtils::CreateGraphFromComputeGraph(compute_graph);
  154. model->SetGraph(graph);
  155. GeModelPtr ge_model;
  156. ModelHelper::TransModelToGeModel(model, ge_model);
  157. davinciModel.Assign(ge_model);
  158. }
  159. static void InitModelWithoutMem(ge::DavinciModel &davinciModel) { InitModel(davinciModel); }
  160. static Status ModelLoadStub(const uint8_t *data, size_t len, ge::Model &model) {
  161. auto compute_graph = std::make_shared<ge::ComputeGraph>("graph");
  162. auto graph = ge::GraphUtils::CreateGraphFromComputeGraph(compute_graph);
  163. model.SetGraph(graph);
  164. return SUCCESS;
  165. }
  166. static void InitDefaultTensorDesc(ge::GeTensorDesc &tensor_desc) {}
  167. static void AddInputDesc(ge::OpDescPtr op_desc, vector<int64_t> shape, ge::Format format, ge::DataType dataType,
  168. int64_t dataSize = 0) {
  169. ge::GeTensorDesc tensor_desc(ge::GeShape(shape), format, dataType);
  170. InitDefaultTensorDesc(tensor_desc);
  171. ge::TensorUtils::SetSize(tensor_desc, dataSize);
  172. op_desc->AddInputDesc(tensor_desc);
  173. }
  174. static void AddOutputDesc(ge::OpDescPtr op_desc, vector<int64_t> shape, ge::Format format, ge::DataType dataType,
  175. int64_t dataSize = 0) {
  176. ge::GeTensorDesc tensor_desc(ge::GeShape(shape), format, dataType);
  177. InitDefaultTensorDesc(tensor_desc);
  178. ge::TensorUtils::SetSize(tensor_desc, dataSize);
  179. op_desc->AddOutputDesc(tensor_desc);
  180. }
  181. static void AddWeight(ge::NodePtr node_ptr, uint8_t *data, size_t dataLen, vector<int64_t> shape = {},
  182. ge::Format format = ge::FORMAT_NCHW, ge::DataType dataType = ge::DT_FLOAT) {
  183. ge::GeTensorDesc tensor_desc(ge::GeShape(shape), format, dataType);
  184. vector<ge::GeTensorPtr> weigths = ge::OpDescUtils::MutableWeights(node_ptr);
  185. weigths.push_back(std::make_shared<ge::GeTensor>(tensor_desc, data, dataLen));
  186. ge::OpDescUtils::SetWeights(node_ptr, weigths);
  187. }
  188. static ge::OpDescPtr CreateOpDesc() {
  189. auto op_desc = std::make_shared<ge::OpDesc>();
  190. return op_desc;
  191. }
  192. };
  193. class OmeTestOpDescBuilder {
  194. public:
  195. OmeTestOpDescBuilder(ge::OpDescPtr orgOpDesc = nullptr) : orgOpDesc_(orgOpDesc) {
  196. if (orgOpDesc_) {
  197. streamId_ = orgOpDesc_->GetStreamId();
  198. }
  199. }
  200. OmeTestOpDescBuilder &SetStreamId(int64_t streamId) {
  201. streamId_ = streamId;
  202. return *this;
  203. }
  204. OmeTestOpDescBuilder &SetWorkspace(vector<int64_t> workspace) {
  205. workspace_ = workspace;
  206. return *this;
  207. }
  208. OmeTestOpDescBuilder &SetWorkspaceBytes(vector<int64_t> workspaceBytes) {
  209. workspaceBytes_ = workspaceBytes;
  210. return *this;
  211. }
  212. OmeTestOpDescBuilder &SetType(const string &type) {
  213. type_ = type;
  214. return *this;
  215. }
  216. OmeTestOpDescBuilder &SetName(const string &name) {
  217. name_ = name;
  218. return *this;
  219. }
  220. OmeTestOpDescBuilder &SetInputs(vector<int64_t> inputs) {
  221. inputsDataOffeset_ = inputs;
  222. return *this;
  223. }
  224. OmeTestOpDescBuilder &AddInput(int64_t input) {
  225. inputsDataOffeset_.push_back(input);
  226. return *this;
  227. }
  228. OmeTestOpDescBuilder &SetOutputs(vector<int64_t> outputs) {
  229. outputsDataOffeset_ = outputs;
  230. return *this;
  231. }
  232. OmeTestOpDescBuilder &AddOutput(int64_t output) {
  233. outputsDataOffeset_.push_back(output);
  234. return *this;
  235. }
  236. OmeTestOpDescBuilder &SetEventId(int64_t eventId) {
  237. eventId_ = eventId;
  238. return *this;
  239. }
  240. OmeTestOpDescBuilder &Setscopeid(int64_t scopeid) {
  241. scopeid_ = scopeid;
  242. return *this;
  243. }
  244. ge::GeTensorDesc &AddInputDesc(vector<int64_t> shape, ge::Format format, ge::DataType dataType,
  245. int64_t dataSize = 0) {
  246. ge::GeTensorDesc tensor_desc(ge::GeShape(shape), format, dataType);
  247. OmeTestOpUtils::InitDefaultTensorDesc(tensor_desc);
  248. ge::TensorUtils::SetSize(tensor_desc, dataSize);
  249. inputTensorDescs.push_back(tensor_desc);
  250. return inputTensorDescs.back();
  251. }
  252. ge::GeTensorDesc &AddInputDesc(vector<int64_t> shape, ge::Format format, ge::DataType dataType, int64_t realdimcnt,
  253. int64_t dataSize) {
  254. ge::GeTensorDesc tensor_desc(ge::GeShape(shape), format, dataType);
  255. OmeTestOpUtils::InitDefaultTensorDesc(tensor_desc);
  256. ge::TensorUtils::SetSize(tensor_desc, dataSize);
  257. ge::TensorUtils::SetRealDimCnt(tensor_desc, realdimcnt);
  258. inputTensorDescs.push_back(tensor_desc);
  259. return inputTensorDescs.back();
  260. }
  261. ge::GeTensorDesc &AddOutputDesc(vector<int64_t> shape, ge::Format format, ge::DataType dataType,
  262. int64_t dataSize = 0) {
  263. ge::GeTensorDesc tensor_desc(ge::GeShape(shape), format, dataType);
  264. OmeTestOpUtils::InitDefaultTensorDesc(tensor_desc);
  265. ge::TensorUtils::SetSize(tensor_desc, dataSize);
  266. outputTensorDescs.push_back(tensor_desc);
  267. return outputTensorDescs.back();
  268. }
  269. ge::GeTensorDesc &AddOutputDesc(vector<int64_t> shape, ge::Format format, ge::DataType dataType, int64_t realdimcnt,
  270. int64_t dataSize) {
  271. ge::GeTensorDesc tensor_desc(ge::GeShape(shape), format, dataType);
  272. OmeTestOpUtils::InitDefaultTensorDesc(tensor_desc);
  273. ge::TensorUtils::SetSize(tensor_desc, dataSize);
  274. ge::TensorUtils::SetRealDimCnt(tensor_desc, realdimcnt);
  275. outputTensorDescs.push_back(tensor_desc);
  276. return outputTensorDescs.back();
  277. }
  278. ge::GeTensorPtr AddWeight(uint8_t *data, size_t dataLen, vector<int64_t> shape = {},
  279. ge::Format format = ge::FORMAT_NCHW, ge::DataType dataType = ge::DT_FLOAT) {
  280. ge::GeTensorDesc tensor_desc(ge::GeShape(shape), format, dataType);
  281. weights_.emplace_back(std::make_shared<ge::GeTensor>(tensor_desc, data, dataLen));
  282. return weights_.back();
  283. }
  284. ge::NodePtr Finish() {
  285. ge::OpDescPtr op_desc;
  286. if (orgOpDesc_) {
  287. op_desc = orgOpDesc_;
  288. } else {
  289. op_desc = OmeTestOpUtils::CreateOpDesc(); // std::make_shared<ge::OpDesc>(name_, type_);
  290. }
  291. if (!type_.empty()) {
  292. op_desc->SetType(type_);
  293. }
  294. if (!name_.empty()) {
  295. op_desc->SetName(name_);
  296. }
  297. op_desc->SetStreamId(streamId_);
  298. ge::AttrUtils::SetInt(op_desc, "id", 1);
  299. if (eventId_ != -1) {
  300. ge::AttrUtils::SetInt(op_desc, SEND_ATTR_EVENT_ID, eventId_);
  301. }
  302. if (scopeid_ != -1) {
  303. ge::AttrUtils::SetInt(op_desc, "fusion_scope", scopeid_);
  304. }
  305. // ge::AttrUtils::SetInt(op_desc, ATTR_NAME_STREAM_ID, streamId_);
  306. // if(!inputsDataOffeset_.empty())
  307. {
  308. vector<int64_t> inputs;
  309. inputs = op_desc->GetInputOffset();
  310. inputs.insert(inputs.end(), inputsDataOffeset_.begin(), inputsDataOffeset_.end());
  311. op_desc->SetInputOffset(inputs);
  312. }
  313. // if(!outputsDataOffeset_.empty())
  314. {
  315. vector<int64_t> outputs;
  316. outputs = op_desc->GetOutputOffset();
  317. outputs.insert(outputs.end(), outputsDataOffeset_.begin(), outputsDataOffeset_.end());
  318. op_desc->SetOutputOffset(outputs);
  319. }
  320. // if(!workspace_.empty())
  321. {
  322. vector<int64_t> workspace = op_desc->GetWorkspace();
  323. workspace.insert(workspace.end(), workspace_.begin(), workspace_.end());
  324. op_desc->SetWorkspace(workspace);
  325. }
  326. // if(!workspaceBytes_.empty())
  327. {
  328. vector<int64_t> workspaceBytes;
  329. workspaceBytes = op_desc->GetWorkspaceBytes();
  330. workspaceBytes.insert(workspaceBytes.end(), workspaceBytes_.begin(), workspaceBytes_.end());
  331. op_desc->SetWorkspaceBytes(workspaceBytes);
  332. }
  333. for (auto &tensor_desc : inputTensorDescs) {
  334. op_desc->AddInputDesc(tensor_desc);
  335. }
  336. for (auto &tensor_desc : outputTensorDescs) {
  337. op_desc->AddOutputDesc(tensor_desc);
  338. }
  339. static std::shared_ptr<ge::ComputeGraph> graph;
  340. // clear graph
  341. graph = std::make_shared<ge::ComputeGraph>("g");
  342. ge::NodePtr node_op = graph->AddNode(op_desc);
  343. // for(int i=0; i < inputTensorDescs.size(); i++)
  344. for (int i = 0; i < op_desc->GetInputsSize(); i++) {
  345. ge::OpDescPtr src_op_desc = std::make_shared<ge::OpDesc>();
  346. ge::GeTensorDesc src_out_desc;
  347. src_op_desc->AddOutputDesc(src_out_desc);
  348. ge::NodePtr src_node = graph->AddNode(src_op_desc);
  349. if (nullptr == src_node) {
  350. GELOGE(ge::FAILED, "Finish: nullptr == src_node");
  351. }
  352. Status res = ge::GraphUtils::AddEdge(src_node->GetOutDataAnchor(0), node_op->GetInDataAnchor(i));
  353. if (SUCCESS != res) {
  354. GELOGE(ge::FAILED, "Finish: GraphUtils::AddEdge failed");
  355. }
  356. }
  357. {
  358. vector<ge::GeTensorPtr> weights;
  359. weights = ge::OpDescUtils::MutableWeights(node_op);
  360. weights.insert(weights.end(), weights_.begin(), weights_.end());
  361. ge::OpDescUtils::SetWeights(node_op, weights);
  362. }
  363. *this = OmeTestOpDescBuilder(op_desc); // clear up
  364. return node_op;
  365. }
  366. private:
  367. ge::OpDescPtr orgOpDesc_;
  368. int64_t streamId_ = 0;
  369. string type_;
  370. string name_;
  371. vector<int64_t> inputsDataOffeset_; // input
  372. vector<int64_t> outputsDataOffeset_; // output
  373. vector<ge::GeTensorDesc> inputTensorDescs;
  374. vector<ge::GeTensorDesc> outputTensorDescs;
  375. vector<int64_t> workspace_;
  376. vector<int64_t> workspaceBytes_;
  377. vector<ge::GeTensorPtr> weights_;
  378. int64_t eventId_ = -1;
  379. int64_t scopeid_ = -1;
  380. };
  381. #endif // OME_REBUILD_OME_OP_TEST_UTILS_H

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