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.

hybrid_model.cc 18 kB

5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 "hybrid_model.h"
  17. #include <vector>
  18. #include "graph/debug/ge_attr_define.h"
  19. #include "graph/load/model_manager/model_utils.h"
  20. #include "graph/utils/graph_utils.h"
  21. #include "graph/utils/node_utils.h"
  22. #include "graph/utils/tensor_utils.h"
  23. #include "graph/utils/type_utils.h"
  24. #include "hybrid/common/npu_memory_allocator.h"
  25. #include "hybrid/model/hybrid_model_builder.h"
  26. #include "hybrid/node_executor/node_executor.h"
  27. #include "common/op/ge_op_utils.h"
  28. namespace ge {
  29. namespace hybrid {
  30. namespace {
  31. const int64_t kMemSizeUnknownShape = -1; // Unknown shape mem size
  32. }
  33. HybridModel::HybridModel(GeRootModelPtr ge_model) : ge_root_model_(std::move(ge_model)) {
  34. }
  35. HybridModel::~HybridModel() {
  36. GELOGD("[%s] HybridModel destroyed.", model_name_.c_str());
  37. }
  38. Status HybridModel::Init(bool is_single_op) {
  39. GELOGD("Start to init hybrid model.");
  40. is_single_op_ = is_single_op;
  41. if (is_single_op) {
  42. GE_CHK_STATUS_RET(HybridModelBuilder(*this).BuildForSingleOp(), "[Build][HybridModel] for SingleOp failed.");
  43. } else {
  44. GE_CHK_STATUS_RET(HybridModelBuilder(*this).Build(), "[Build][HybridModel] failed.");
  45. }
  46. SaveSpecifyAttrValues();
  47. GELOGD("HybridModel initialized successfully.");
  48. return SUCCESS;
  49. }
  50. TensorValue *HybridModel::GetVariable(const string &name) const {
  51. auto it = variable_tensors_.find(name);
  52. if (it == variable_tensors_.end()) {
  53. GELOGD("Failed to get variable tensor. var name = [%s]", name.c_str());
  54. return nullptr;
  55. }
  56. GELOGD("Got variable tensor. var name = [%s], tensor = %s", name.c_str(), it->second->DebugString().c_str());
  57. return it->second.get();
  58. }
  59. NodePtr HybridModel::GetVariableNode(const string &name) const {
  60. auto it = device_variable_nodes_.find(name);
  61. if (it != device_variable_nodes_.end()) {
  62. return it->second;
  63. }
  64. auto host_find = host_variable_nodes_.find(name);
  65. if (host_find != host_variable_nodes_.end()) {
  66. return host_find->second;
  67. }
  68. GELOGD("Failed to get variable node by name = [%s]", name.c_str());
  69. return nullptr;
  70. }
  71. const std::vector<domi::TaskDef> *HybridModel::GetTaskDefs(const NodePtr &node) const {
  72. auto it = task_defs_.find(node);
  73. if (it == task_defs_.end()) {
  74. return nullptr;
  75. }
  76. return &it->second;
  77. }
  78. NodeItem *HybridModel::MutableNodeItem(const NodePtr &node) {
  79. auto it = node_items_.find(node);
  80. if (it == node_items_.end()) {
  81. return nullptr;
  82. }
  83. return it->second.get();
  84. }
  85. const NodeItem *HybridModel::GetNodeItem(const NodePtr &node) const {
  86. auto it = node_items_.find(node);
  87. if (it == node_items_.end()) {
  88. return nullptr;
  89. }
  90. return it->second.get();
  91. }
  92. GeModelPtr HybridModel::GetGeModel(const NodePtr &node) const {
  93. auto it = known_shape_sub_models_.find(node);
  94. if (it == known_shape_sub_models_.end()) {
  95. GELOGE(INTERNAL_ERROR, "[Check][Param:node][%s] Failed to get GeModel for subgraph node,"
  96. "because node not in known_shape_sub_models_.", node->GetName().c_str());
  97. REPORT_INNER_ERROR("E19999", "%s Failed to get GeModel for subgraph node,"
  98. "because node not in known_shape_sub_models_.", node->GetName().c_str());
  99. return nullptr;
  100. }
  101. return it->second;
  102. }
  103. const GraphItem *HybridModel::GetRootGraphItem() const {
  104. return root_graph_item_.get();
  105. }
  106. const GraphItem *HybridModel::GetSubgraphItem(const std::string &graph_name) const {
  107. GELOGD("To find subgraph item by name = %s", graph_name.c_str());
  108. auto it = subgraph_items_.find(graph_name);
  109. if (it == subgraph_items_.end()) {
  110. GELOGD("Subgraph item not found by node = %s", graph_name.c_str());
  111. return nullptr;
  112. }
  113. return it->second.get();
  114. }
  115. const GraphItem *HybridModel::GetSubgraphItem(const ComputeGraphPtr &subgraph) const {
  116. if (subgraph == nullptr) {
  117. REPORT_INNER_ERROR("E19999", "Input param subgraph is nullptr, Graph:%s",
  118. root_graph_item_->GetName().c_str());
  119. GELOGE(PARAM_INVALID, "[Check][Param]subgraph is nullptr. graph:%s",
  120. root_graph_item_->GetName().c_str());
  121. return nullptr;
  122. }
  123. auto subgraph_name = subgraph->GetName();
  124. return GetSubgraphItem(subgraph_name);
  125. }
  126. const string &HybridModel::GetModelName() const {
  127. return model_name_;
  128. }
  129. Status HybridModel::GetDynamicBatchInfo(std::vector<std::vector<int64_t>> &batch_info, int32_t &dynamic_type) {
  130. // dynamic shape do not need dynamic batch
  131. batch_info = {};
  132. dynamic_type = -1;
  133. return SUCCESS;
  134. }
  135. void HybridModel::GetUserDesignateShapeOrder(std::vector<std::string> &user_input_shape_order) {
  136. // dynamic shape do not need dynamic batch
  137. user_input_shape_order = {};
  138. }
  139. void HybridModel::GetModelAttr(std::vector<std::string> &dynamic_output_shape_info) {
  140. dynamic_output_shape_info = {};
  141. }
  142. Status HybridModel::GetInputOutputDescInfo(vector<InputOutputDescInfo> &input_desc,
  143. vector<InputOutputDescInfo> &output_desc,
  144. std::vector<uint32_t> &input_formats,
  145. std::vector<uint32_t> &output_formats) {
  146. auto node_item_list = root_graph_item_->GetInputNodes();
  147. if (node_item_list.empty()) {
  148. REPORT_INNER_ERROR("E19999", "node item list is empty!, graph:%s",
  149. root_graph_item_->GetName().c_str());
  150. GELOGE(FAILED, "[Get][InputNodes]node item list is empty!, graph:%s",
  151. root_graph_item_->GetName().c_str());
  152. return FAILED;
  153. }
  154. GE_CHECK_NOTNULL(node_item_list[0]->node);
  155. GE_CHECK_NOTNULL(node_item_list[0]->node->GetOpDesc());
  156. if (node_item_list[0]->node->GetOpDesc()->GetInputsSize() != 1) {
  157. REPORT_INNER_ERROR("E19999", "Input size of op is not 1, op:%s, type:%s",
  158. node_item_list[0]->node->GetName().c_str(),
  159. node_item_list[0]->node->GetType().c_str());
  160. GELOGE(FAILED, "[Check][Size]input size of op is not 1! op:%s, type:%s",
  161. node_item_list[0]->node->GetName().c_str(),
  162. node_item_list[0]->node->GetType().c_str());
  163. return FAILED;
  164. }
  165. GE_CHK_STATUS_RET(GetInputDescInfo(input_desc, input_formats), "[Get][InputDescInfo] failed.");
  166. GE_CHK_STATUS_RET(GetOutputDescInfo(output_desc, output_formats), "[Get][OutputDescInfo] failed.");
  167. return SUCCESS;
  168. }
  169. void HybridModel::SetInputDimsAndShapeRangesInfo(const vector<int64_t> &model_input_dims,
  170. std::vector<std::pair<int64_t, int64_t>> &shape_ranges,
  171. InputOutputDescInfo &input) {
  172. for (auto model_input_dim : model_input_dims) {
  173. input.shape_info.dims.push_back(model_input_dim);
  174. }
  175. input.shape_info.shape_ranges = shape_ranges;
  176. return;
  177. }
  178. void HybridModel::CreateInputDimsInfo(const OpDescPtr &op_desc, InputOutputDescInfo &input) {
  179. std::vector<std::pair<int64_t,int64_t>> shape_ranges;
  180. if (is_new_model_desc_ && op_desc->HasAttr(ATTR_NAME_INPUT_DIMS)) {
  181. // When static aipp is set, need to get the model input dims which processed by aipp
  182. vector<int64_t> model_input_dims;
  183. (void)AttrUtils::GetListInt(op_desc, ATTR_NAME_INPUT_DIMS, model_input_dims);
  184. SetInputDimsAndShapeRangesInfo(model_input_dims, shape_ranges, input);
  185. return;
  186. }
  187. // judge if this data is linked dynamic aipp first, multiply batch has been considered
  188. if (op_desc->HasAttr("_dynamic_aipp_input_dims")) {
  189. vector<int64_t> dynamic_aipp_input_dims;
  190. (void)AttrUtils::GetListInt(op_desc, "_dynamic_aipp_input_dims", dynamic_aipp_input_dims);
  191. SetInputDimsAndShapeRangesInfo(dynamic_aipp_input_dims, shape_ranges, input);
  192. return;
  193. } else {
  194. vector<int64_t> input_dims = op_desc->GetInputDescPtr(0)->GetShape().GetDims();
  195. op_desc->GetInputDescPtr(0)->GetShapeRange(shape_ranges);
  196. SetInputDimsAndShapeRangesInfo(input_dims, shape_ranges, input);
  197. return;
  198. }
  199. }
  200. Status HybridModel::GetInputDescInfo(vector<InputOutputDescInfo> &input_desc, std::vector<uint32_t> &formats) {
  201. auto node_item_list = root_graph_item_->GetInputNodes();
  202. for (auto &node_item : node_item_list) {
  203. InputOutputDescInfo input;
  204. GE_CHECK_NOTNULL(node_item->node);
  205. auto op_desc = node_item->node->GetOpDesc();
  206. GE_CHECK_NOTNULL(op_desc);
  207. GE_CHECK_NOTNULL(op_desc->GetInputDescPtr(0));
  208. Format format = op_desc->GetInputDescPtr(0)->GetFormat();
  209. DataType data_type = op_desc->GetInputDescPtr(0)->GetDataType();
  210. input.data_type = static_cast<uint32_t>(data_type);
  211. input.name = op_desc->GetName();
  212. GeShape shape = op_desc->GetInputDescPtr(0)->GetShape();
  213. int64_t tensor_size = 0;
  214. if (TensorUtils::CalcTensorMemSize(shape, format, data_type, tensor_size) != GRAPH_SUCCESS) {
  215. GELOGE(FAILED, "[Calculate][TensorMemSize] failed input0 desc in node:%s."
  216. "shape:%s, format:%s, datatype:%s.", op_desc->GetName().c_str(),
  217. shape.ToString().c_str(), TypeUtils::FormatToSerialString(format).c_str(),
  218. TypeUtils::DataTypeToSerialString(data_type).c_str());
  219. REPORT_CALL_ERROR("E19999", "CalcTensorMemSize failed for input0 desc in node:%s,"
  220. "shape:%s, format:%s, datatype:%s", op_desc->GetName().c_str(),
  221. shape.ToString().c_str(), TypeUtils::FormatToSerialString(format).c_str(),
  222. TypeUtils::DataTypeToSerialString(data_type).c_str());
  223. return FAILED;
  224. }
  225. if (tensor_size == kMemSizeUnknownShape) {
  226. tensor_size = 0;
  227. }
  228. input.size = static_cast<uint64_t>(tensor_size);
  229. CreateInputDimsInfo(op_desc, input);
  230. formats.push_back(format);
  231. input_desc.push_back(input);
  232. }
  233. is_new_model_desc_ = false;
  234. return SUCCESS;
  235. }
  236. void HybridModel::CreateOutput(ConstGeTensorDescPtr &output_desc,
  237. InputOutputDescInfo &output_desc_info, uint32_t &format_result) {
  238. GE_IF_BOOL_EXEC(output_desc == nullptr,
  239. REPORT_INNER_ERROR("E19999", "param output_desc is nullptr, check invalid.");
  240. GELOGE(FAILED, "[Check][Param:output_desc]output desc ptr is nullptr");
  241. return );
  242. Format format = output_desc->GetFormat();
  243. GeShape shape = output_desc->GetShape();
  244. std::vector<std::pair<int64_t,int64_t>> shape_ranges;
  245. output_desc->GetShapeRange(shape_ranges);
  246. DataType data_type = output_desc->GetDataType();
  247. format_result = format;
  248. if (format == FORMAT_FRACTAL_Z) { // FraczToHWCK
  249. int64_t k = shape.GetDim(0); // 0: first dim
  250. int64_t c = shape.GetDim(1); // 1: second dim
  251. int64_t h = shape.GetDim(2); // 2: third dim
  252. int64_t w = shape.GetDim(3); // 3: forth dim
  253. output_desc_info.shape_info.dims.push_back(h);
  254. output_desc_info.shape_info.dims.push_back(w);
  255. output_desc_info.shape_info.dims.push_back(c);
  256. output_desc_info.shape_info.dims.push_back(k);
  257. if (shape_ranges.size() == 4) { // 4 dims
  258. output_desc_info.shape_info.shape_ranges.push_back(shape_ranges[2]); // h:2
  259. output_desc_info.shape_info.shape_ranges.push_back(shape_ranges[3]); // w:3
  260. output_desc_info.shape_info.shape_ranges.push_back(shape_ranges[1]); // c:1
  261. output_desc_info.shape_info.shape_ranges.push_back(shape_ranges[0]); // k:0
  262. }
  263. format_result = FORMAT_HWCN;
  264. } else {
  265. for (size_t j = 0; j < shape.GetDimNum(); j++) {
  266. output_desc_info.shape_info.dims.push_back(shape.GetDim(j));
  267. }
  268. output_desc_info.shape_info.shape_ranges = shape_ranges;
  269. }
  270. int64_t tensor_size = 0;
  271. (void)TensorUtils::CalcTensorMemSize(shape, format, data_type, tensor_size);
  272. if (tensor_size == kMemSizeUnknownShape) {
  273. tensor_size = 0;
  274. }
  275. output_desc_info.size = static_cast<uint64_t>(tensor_size);
  276. output_desc_info.data_type = output_desc->GetDataType();
  277. }
  278. Status HybridModel::GetOutputDescInfo(vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &formats) {
  279. std::vector<ConstGeTensorDescPtr> output_desc_list;
  280. // output_desc_list contains vaild input desc
  281. GE_CHK_STATUS_RET(root_graph_item_->GetOutputDescList(output_desc_list),
  282. "[Invoke][GetOutputDescList]get output desc info failed, Graph:%s",
  283. root_graph_item_->GetName().c_str());
  284. vector<std::string> out_node_names;
  285. (void)ge::AttrUtils::GetListStr(ge_root_model_->GetRootGraph(), ATTR_MODEL_OUT_NODES_NAME, out_node_names);
  286. GE_CHECK_NOTNULL(root_graph_item_->GetOutputNode());
  287. auto op_desc = root_graph_item_->GetOutputNode()->op_desc;
  288. GE_CHECK_NOTNULL(op_desc);
  289. auto out_size = static_cast<uint32_t>(op_desc->GetInputsSize());
  290. GE_IF_BOOL_EXEC(out_size != output_desc_list.size(),
  291. REPORT_INNER_ERROR("E19999", "output size[%u] not match output_desc_list size[%zu]",
  292. out_size, output_desc_list.size());
  293. GELOGE(FAILED, "[Check][Size]output size[%u] not match output_desc_list size[%zu]",
  294. out_size, output_desc_list.size());
  295. return FAILED;);
  296. for (uint32_t index = 0; index < out_size; ++index) {
  297. string output_name;
  298. std::vector<std::string> src_name = op_desc->GetSrcName();
  299. std::vector<int64_t> src_index = op_desc->GetSrcIndex();
  300. if (out_size == out_node_names.size()) {
  301. bool contains_colon = out_node_names[index].find(":") != std::string::npos;
  302. output_name = contains_colon ? out_node_names[index] : out_node_names[index] +
  303. ":" + std::to_string(src_index[index]);
  304. } else {
  305. output_name = std::string("output_") + std::to_string(index) + "_" + src_name[index] +
  306. "_" + std::to_string(src_index[index]);
  307. }
  308. InputOutputDescInfo output_desc_info;
  309. output_desc_info.name = output_name;
  310. uint32_t format_result;
  311. CreateOutput(output_desc_list[index], output_desc_info, format_result);
  312. output_desc.push_back(output_desc_info);
  313. formats.push_back(format_result);
  314. }
  315. return SUCCESS;
  316. }
  317. TensorValue *HybridModel::GetConstant(const NodePtr &node) const {
  318. if (node == nullptr) {
  319. GELOGE(PARAM_INVALID, "[Check][Param:node]node is null.");
  320. REPORT_INNER_ERROR("E19999", "param node is null, check invalid.");
  321. return nullptr;
  322. }
  323. auto it = constant_tensors_.find(node);
  324. if (it == constant_tensors_.end()) {
  325. GELOGD("constant not found, node name = [%s]", node->GetName().c_str());
  326. return nullptr;
  327. }
  328. GELOGD("Got constant tensor, node name = [%s], tensor = %s",
  329. node->GetName().c_str(),
  330. it->second->DebugString().c_str());
  331. return it->second.get();
  332. }
  333. TensorValue *HybridModel::GetTensor(const NodePtr &node) const {
  334. if (node == nullptr) {
  335. GELOGE(PARAM_INVALID, "[Check][Param:node]node is null.");
  336. REPORT_INNER_ERROR("E19999", "param node is null, check invalid.");
  337. return nullptr;
  338. }
  339. if (node->GetType() == CONSTANT) {
  340. return GetConstant(node);
  341. }
  342. return GetVariable(node->GetName());
  343. }
  344. const map<int64_t, std::vector<std::pair<int, Tensor>>> &HybridModel::GetHostTensors() const {
  345. return host_tensors_;
  346. }
  347. void *HybridModel::GetGlobalStep() const {
  348. if (global_step_ == nullptr) {
  349. return nullptr;
  350. }
  351. return global_step_->GetData();
  352. }
  353. TensorBuffer *HybridModel::GetModelWeight(const string &subgraph_name) const {
  354. auto it = weight_buffer_map_.find(subgraph_name);
  355. if (it == weight_buffer_map_.end()) {
  356. GELOGD("Model weight not found, subgraph name = %s", subgraph_name.c_str());
  357. return nullptr;
  358. }
  359. return it->second.get();
  360. }
  361. // save specify attr values of op, such as ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES
  362. // it will save more attr values in the future
  363. void HybridModel::SaveSpecifyAttrValues() {
  364. for (const auto &node : root_graph_->GetAllNodes()) {
  365. if (node == nullptr) {
  366. continue;
  367. }
  368. auto op_desc = node->GetOpDesc();
  369. if (op_desc == nullptr) {
  370. continue;
  371. }
  372. std::vector<std::string> value;
  373. if (AttrUtils::GetListStr(op_desc, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, value)) {
  374. std::map<std::string, std::vector<std::string>> attr_name_to_value;
  375. attr_name_to_value[ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES] = value;
  376. op_name_to_attrs_[op_desc->GetName()] = attr_name_to_value;
  377. GELOGD("Get op:%s attr:%s success.", op_desc->GetName().c_str(), ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES.c_str());
  378. }
  379. }
  380. return;
  381. }
  382. Status HybridModel::GetOpAttr(const std::string &op_name, const std::string &attr_name,
  383. std::string &attr_value) const {
  384. auto itr = op_name_to_attrs_.find(op_name);
  385. if (itr == op_name_to_attrs_.end()) {
  386. GELOGW("Did not save op:%s attr", op_name.c_str());
  387. return SUCCESS;
  388. }
  389. auto attr_itr = itr->second.find(attr_name);
  390. if (attr_itr == itr->second.end()) {
  391. GELOGW("Did not save attr:%s of op:%s", attr_name.c_str(), op_name.c_str());
  392. return SUCCESS;
  393. }
  394. for (const auto &name : attr_itr->second) {
  395. attr_value += "[" + std::to_string(name.size()) + "]" + name;
  396. }
  397. GELOGD("Get attr:%s of op:%s success, attr value:%s", attr_name.c_str(), op_name.c_str(), attr_value.c_str());
  398. return SUCCESS;
  399. }
  400. } // namespace hybrid
  401. } // namespace ge

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