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

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