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 13 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. if (is_single_op) {
  41. GE_CHK_STATUS_RET(HybridModelBuilder(*this).BuildForSingleOp(), "Failed to build hybrid model.");
  42. } else {
  43. GE_CHK_STATUS_RET(HybridModelBuilder(*this).Build(), "Failed to build hybrid model.");
  44. }
  45. GELOGD("HybridModel initialized successfully.");
  46. return SUCCESS;
  47. }
  48. TensorValue* HybridModel::GetVariable(const string &name) const {
  49. auto it = variable_tensors_.find(name);
  50. if (it == variable_tensors_.end()) {
  51. GELOGD("Failed to get variable tensor. var name = [%s]", name.c_str());
  52. return nullptr;
  53. }
  54. GELOGD("Got variable tensor. var name = [%s], tensor = %s", name.c_str(), it->second->DebugString().c_str());
  55. return it->second.get();
  56. }
  57. NodePtr HybridModel::GetVariableNode(const string &name) const {
  58. auto it = device_variable_nodes_.find(name);
  59. if (it != device_variable_nodes_.end()) {
  60. return it->second;
  61. }
  62. auto host_find = host_variable_nodes_.find(name);
  63. if (host_find != host_variable_nodes_.end()) {
  64. return host_find->second;
  65. }
  66. GELOGD("Failed to get variable node by name = [%s]", name.c_str());
  67. return nullptr;
  68. }
  69. const std::vector<domi::TaskDef> *HybridModel::GetTaskDefs(const NodePtr &node) const {
  70. auto it = task_defs_.find(node);
  71. if (it == task_defs_.end()) {
  72. return nullptr;
  73. }
  74. return &it->second;
  75. }
  76. NodeItem *HybridModel::MutableNodeItem(const NodePtr &node) {
  77. auto it = node_items_.find(node);
  78. if (it == node_items_.end()) {
  79. return nullptr;
  80. }
  81. return it->second.get();
  82. }
  83. const NodeItem *HybridModel::GetNodeItem(const NodePtr &node) const {
  84. auto it = node_items_.find(node);
  85. if (it == node_items_.end()) {
  86. return nullptr;
  87. }
  88. return it->second.get();
  89. }
  90. GeModelPtr HybridModel::GetGeModel(const NodePtr &node) const {
  91. auto it = known_shape_sub_models_.find(node);
  92. if (it == known_shape_sub_models_.end()) {
  93. GELOGE(INTERNAL_ERROR, "[%s] Failed to get GeModel for subgraph node.", node->GetName().c_str());
  94. return nullptr;
  95. }
  96. return it->second;
  97. }
  98. const GraphItem* HybridModel::GetRootGraphItem() const {
  99. return root_graph_item_.get();
  100. }
  101. const GraphItem *HybridModel::GetSubgraphItem(const std::string &graph_name) const {
  102. GELOGD("To find subgraph item by name = %s", graph_name.c_str());
  103. auto it = subgraph_items_.find(graph_name);
  104. if (it == subgraph_items_.end()) {
  105. GELOGD("Subgraph item not found by node = %s", graph_name.c_str());
  106. return nullptr;
  107. }
  108. return it->second.get();
  109. }
  110. const GraphItem *HybridModel::GetSubgraphItem(const ComputeGraphPtr &subgraph) const {
  111. if (subgraph == nullptr) {
  112. GELOGE(PARAM_INVALID, "subgraph is nullptr");
  113. return nullptr;
  114. }
  115. auto subgraph_name = subgraph->GetName();
  116. return GetSubgraphItem(subgraph_name);
  117. }
  118. const string &HybridModel::GetModelName() const {
  119. return model_name_;
  120. }
  121. Status HybridModel::GetDynamicBatchInfo(std::vector<std::vector<int64_t>> &batch_info, int32_t &dynamic_type) {
  122. // dynamic shape do not need dynamic batch
  123. batch_info = {};
  124. dynamic_type = -1;
  125. return SUCCESS;
  126. }
  127. void HybridModel::GetUserDesignateShapeOrder(std::vector<std::string> &user_input_shape_order) {
  128. // dynamic shape do not need dynamic batch
  129. user_input_shape_order = {};
  130. }
  131. void HybridModel::GetModelAttr(std::vector<std::string> &dynamic_output_shape_info) {
  132. dynamic_output_shape_info = {};
  133. }
  134. Status HybridModel::GetInputOutputDescInfo(vector<InputOutputDescInfo> &input_desc,
  135. vector<InputOutputDescInfo> &output_desc,
  136. std::vector<uint32_t> &input_formats,
  137. std::vector<uint32_t> &output_formats) {
  138. auto node_item_list = root_graph_item_->GetInputNodes();
  139. if (node_item_list.empty()) {
  140. GELOGE(FAILED, "node item list is empty!");
  141. return FAILED;
  142. }
  143. GE_CHECK_NOTNULL(node_item_list[0]->node);
  144. GE_CHECK_NOTNULL(node_item_list[0]->node->GetOpDesc());
  145. if (node_item_list[0]->node->GetOpDesc()->GetInputsSize() != 1) {
  146. GELOGE(FAILED, "input size of op is not 1!");
  147. return FAILED;
  148. }
  149. GE_CHK_STATUS_RET(GetInputDescInfo(input_desc, input_formats), "get input desc info failed");
  150. GE_CHK_STATUS_RET(GetOutputDescInfo(output_desc, output_formats), "get ouput desc info failed");
  151. return SUCCESS;
  152. }
  153. void HybridModel::SetInputDimsAndShapeRangesInfo(const vector<int64_t> &model_input_dims,
  154. std::vector<std::pair<int64_t, int64_t>> &shape_ranges,
  155. InputOutputDescInfo &input) {
  156. for (auto model_input_dim : model_input_dims) {
  157. input.shape_info.dims.push_back(model_input_dim);
  158. }
  159. input.shape_info.shape_ranges = shape_ranges;
  160. return;
  161. }
  162. void HybridModel::CreateInputDimsInfo(const OpDescPtr &op_desc, InputOutputDescInfo &input) {
  163. std::vector<std::pair<int64_t,int64_t>> shape_ranges;
  164. if (is_new_model_desc_ && op_desc->HasAttr(ATTR_NAME_INPUT_DIMS)) {
  165. // When static aipp is set, need to get the model input dims which processed by aipp
  166. vector<int64_t> model_input_dims;
  167. (void)AttrUtils::GetListInt(op_desc, ATTR_NAME_INPUT_DIMS, model_input_dims);
  168. SetInputDimsAndShapeRangesInfo(model_input_dims, shape_ranges, input);
  169. return;
  170. }
  171. // judge if this data is linked dynamic aipp first, multiply batch has been considered
  172. if (op_desc->HasAttr("_dynamic_aipp_input_dims")) {
  173. vector<int64_t> dynamic_aipp_input_dims;
  174. (void)AttrUtils::GetListInt(op_desc, "_dynamic_aipp_input_dims", dynamic_aipp_input_dims);
  175. SetInputDimsAndShapeRangesInfo(dynamic_aipp_input_dims, shape_ranges, input);
  176. return;
  177. } else {
  178. vector<int64_t> input_dims = op_desc->GetInputDescPtr(0)->GetShape().GetDims();
  179. op_desc->GetInputDescPtr(0)->GetShapeRange(shape_ranges);
  180. SetInputDimsAndShapeRangesInfo(input_dims, shape_ranges, input);
  181. return;
  182. }
  183. }
  184. Status HybridModel::GetInputDescInfo(vector<InputOutputDescInfo> &input_desc, std::vector<uint32_t> &formats) {
  185. auto node_item_list = root_graph_item_->GetInputNodes();
  186. for (auto &node_item : node_item_list) {
  187. InputOutputDescInfo input;
  188. GE_CHECK_NOTNULL(node_item->node);
  189. auto op_desc = node_item->node->GetOpDesc();
  190. GE_CHECK_NOTNULL(op_desc);
  191. GE_CHECK_NOTNULL(op_desc->GetInputDescPtr(0));
  192. Format format = op_desc->GetInputDescPtr(0)->GetFormat();
  193. input.data_type = op_desc->GetInputDescPtr(0)->GetDataType();
  194. input.name = op_desc->GetName();
  195. int64_t input_size = 0;
  196. GE_CHK_STATUS_RET(TensorUtils::GetSize(*op_desc->GetInputDescPtr(0), input_size), "get input size failed.");
  197. // support dynamic shape
  198. if (input_size < 0) {
  199. GELOGD("dynamic shape scene, input size is unknown. "
  200. "format=%d, data_type=%d, input_size=%ld",
  201. format, input.data_type, input_size);
  202. input_size = kMemSizeUnknownShape; // -1
  203. }
  204. // not support dynamic shape input for now, so input_size here will be not less than zero.
  205. input.size = input_size;
  206. CreateInputDimsInfo(op_desc, input);
  207. formats.push_back(format);
  208. input_desc.push_back(input);
  209. }
  210. is_new_model_desc_ = false;
  211. return SUCCESS;
  212. }
  213. void HybridModel::CreateOutput(ConstGeTensorDescPtr &output_desc,
  214. InputOutputDescInfo &output_desc_info, uint32_t &format_result) {
  215. GE_IF_BOOL_EXEC(output_desc == nullptr, GELOGE(FAILED, "output desc ptr is nullptr"); return );
  216. Format format = output_desc->GetFormat();
  217. GeShape shape = output_desc->GetShape();
  218. std::vector<std::pair<int64_t,int64_t>> shape_ranges;
  219. output_desc->GetShapeRange(shape_ranges);
  220. DataType data_type = output_desc->GetDataType();
  221. format_result = format;
  222. if (format == FORMAT_FRACTAL_Z) { // FraczToHWCK
  223. int64_t k = shape.GetDim(0); // 0: first dim
  224. int64_t c = shape.GetDim(1); // 1: second dim
  225. int64_t h = shape.GetDim(2); // 2: third dim
  226. int64_t w = shape.GetDim(3); // 3: forth dim
  227. output_desc_info.shape_info.dims.push_back(h);
  228. output_desc_info.shape_info.dims.push_back(w);
  229. output_desc_info.shape_info.dims.push_back(c);
  230. output_desc_info.shape_info.dims.push_back(k);
  231. if (shape_ranges.size() == 4) { // 4 dims
  232. output_desc_info.shape_info.shape_ranges.push_back(shape_ranges[2]); // h:2
  233. output_desc_info.shape_info.shape_ranges.push_back(shape_ranges[3]); // w:3
  234. output_desc_info.shape_info.shape_ranges.push_back(shape_ranges[1]); // c:1
  235. output_desc_info.shape_info.shape_ranges.push_back(shape_ranges[0]); // k:0
  236. }
  237. format_result = FORMAT_HWCN;
  238. } else {
  239. for (size_t j = 0; j < shape.GetDimNum(); j++) {
  240. output_desc_info.shape_info.dims.push_back(shape.GetDim(j));
  241. }
  242. output_desc_info.shape_info.shape_ranges = shape_ranges;
  243. }
  244. int64_t tensor_size = 0;
  245. (void)TensorUtils::CalcTensorMemSize(shape, format, data_type, tensor_size);
  246. output_desc_info.size = static_cast<uint64_t>(tensor_size);
  247. output_desc_info.data_type = output_desc->GetDataType();
  248. }
  249. Status HybridModel::GetOutputDescInfo(vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &formats) {
  250. std::vector<ConstGeTensorDescPtr> output_desc_list;
  251. // output_desc_list contains vaild input desc
  252. GE_CHK_STATUS_RET(root_graph_item_->GetOutputDescList(output_desc_list), "get output desc info failed");
  253. vector<std::string> out_node_names;
  254. (void)ge::AttrUtils::GetListStr(ge_root_model_->GetRootGraph(), ATTR_MODEL_OUT_NODES_NAME, out_node_names);
  255. GE_CHECK_NOTNULL(root_graph_item_->GetOutputNode());
  256. auto op_desc = root_graph_item_->GetOutputNode()->op_desc;
  257. GE_CHECK_NOTNULL(op_desc);
  258. auto out_size = static_cast<uint32_t>(op_desc->GetInputsSize());
  259. GE_CHK_BOOL_RET_STATUS(out_size == output_desc_list.size(),
  260. FAILED, "output size[%u] not match output_desc_list size[%zu]", out_size, output_desc_list.size());
  261. for (uint32_t index = 0; index < out_size; ++index) {
  262. string output_name;
  263. std::vector<std::string> src_name = op_desc->GetSrcName();
  264. std::vector<int64_t> src_index = op_desc->GetSrcIndex();
  265. if (out_size == out_node_names.size()) {
  266. bool contains_colon = out_node_names[index].find(":") != std::string::npos;
  267. output_name = contains_colon ? out_node_names[index] : out_node_names[index] +
  268. ":" + std::to_string(src_index[index]);
  269. } else {
  270. output_name = std::string("output_") + std::to_string(index) + "_" + src_name[index] +
  271. "_" + std::to_string(src_index[index]);
  272. }
  273. InputOutputDescInfo output_desc_info;
  274. output_desc_info.name = output_name;
  275. uint32_t format_result;
  276. CreateOutput(output_desc_list[index], output_desc_info, format_result);
  277. output_desc.push_back(output_desc_info);
  278. formats.push_back(format_result);
  279. }
  280. return SUCCESS;
  281. }
  282. TensorValue *HybridModel::GetConstant(const NodePtr &node) const {
  283. if (node == nullptr) {
  284. GELOGE(PARAM_INVALID, "Param is null");
  285. return nullptr;
  286. }
  287. auto it = constant_tensors_.find(node);
  288. if (it == constant_tensors_.end()) {
  289. GELOGD("constant not found, node name = [%s]", node->GetName().c_str());
  290. return nullptr;
  291. }
  292. GELOGD("Got constant tensor, node name = [%s], tensor = %s",
  293. node->GetName().c_str(),
  294. it->second->DebugString().c_str());
  295. return it->second.get();
  296. }
  297. TensorValue *HybridModel::GetTensor(const NodePtr &node) const {
  298. if (node == nullptr) {
  299. GELOGE(PARAM_INVALID, "Param is null");
  300. return nullptr;
  301. }
  302. if (node->GetType() == CONSTANT) {
  303. return GetConstant(node);
  304. }
  305. return GetVariable(node->GetName());
  306. }
  307. } // namespace hybrid
  308. } // namespace ge

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