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.

util_insert_aipp_op.cc 31 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
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
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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 "graph/preprocess/insert_op/util_insert_aipp_op.h"
  17. #include <fstream>
  18. #include <utility>
  19. #include "common/dynamic_aipp.h"
  20. #include "common/formats/utils/formats_trans_utils.h"
  21. #include "common/ge/ge_util.h"
  22. #include "common/op/ge_op_utils.h"
  23. #include "common/util.h"
  24. #include "common/util/error_manager/error_manager.h"
  25. #include "framework/common/debug/ge_log.h"
  26. #include "framework/common/debug/log.h"
  27. #include "framework/common/ge_inner_error_codes.h"
  28. #include "framework/omg/omg_inner_types.h"
  29. #include "graph/debug/ge_attr_define.h"
  30. #include "graph/preprocess/insert_op/ge_aipp_op.h"
  31. #include "graph/utils/attr_utils.h"
  32. #include "graph/utils/graph_utils.h"
  33. #include "graph/utils/op_desc_utils.h"
  34. #include "graph/utils/tensor_utils.h"
  35. #include "graph/utils/type_utils.h"
  36. using domi::AippOpParams;
  37. namespace ge {
  38. namespace {
  39. const char *const kMbatchSwitchnName = "mbatch-switch-name";
  40. const int64_t kFormatAgnosticSwitch = 1;
  41. const int64_t kFormatDependInputIndex = 1;
  42. } // namespace
  43. static void ConvertShape2Nhwc(Format &format, vector<int64_t> &shape_vec) {
  44. if ((format == FORMAT_NHWC) || (shape_vec.size() != static_cast<size_t>(NORMAL_TENSOR_SIZE))) {
  45. return;
  46. }
  47. if (format != FORMAT_NCHW) {
  48. GELOGW("The format is not NCHW, current format is %s.", TypeUtils::FormatToSerialString(format).c_str());
  49. return;
  50. }
  51. vector<int64_t> shape_vec_tmp;
  52. shape_vec.swap(shape_vec_tmp);
  53. shape_vec.push_back(shape_vec_tmp[NCHW_DIM_N]);
  54. shape_vec.push_back(shape_vec_tmp[NCHW_DIM_H]);
  55. shape_vec.push_back(shape_vec_tmp[NCHW_DIM_W]);
  56. shape_vec.push_back(shape_vec_tmp[NCHW_DIM_C]);
  57. return;
  58. }
  59. Status InsertNewOpUtil::Init() {
  60. insert_op_conf_.reset((new (std::nothrow) domi::InsertNewOps()));
  61. GE_CHECK_NOTNULL(insert_op_conf_);
  62. return SUCCESS;
  63. }
  64. Status InsertNewOpUtil::Parse(const char *conf_path) {
  65. if (conf_path == nullptr || *conf_path == '\0') {
  66. return SUCCESS;
  67. }
  68. GE_CHK_BOOL_RET_STATUS(ReadProtoFromText(conf_path, insert_op_conf_.get()), FAILED, "Read AIPP conf file error: %s",
  69. conf_path);
  70. GE_CHK_STATUS_RET(CheckPositionNotRepeat(), "Check insert position of op failed");
  71. for (int i = 0; i < insert_op_conf_->aipp_op_size(); i++) {
  72. domi::AippOpParams *aipp_op_params = insert_op_conf_->mutable_aipp_op(i);
  73. std::unique_ptr<AippOp> aipp_op(new (std::nothrow) AippOp());
  74. GE_CHECK_NOTNULL(aipp_op);
  75. GE_CHK_STATUS_RET(aipp_op->Init(aipp_op_params), "Aipp op init failed.");
  76. insert_ops_.push_back(std::move(aipp_op));
  77. }
  78. for (auto &dynamic_op : insert_ops_) {
  79. GE_CHECK_NOTNULL(dynamic_op);
  80. GE_CHK_STATUS_RET(dynamic_op->ValidateParams(), "Validate insert_op config file failed");
  81. GE_CHK_STATUS_RET(dynamic_op->SetDefaultParams(), "Set default value of insert_op failed");
  82. }
  83. return SUCCESS;
  84. }
  85. Status InsertNewOpUtil::InsertAippOps(ComputeGraphPtr &graph, std::string &aippConfigPath) {
  86. GE_CHECK_NOTNULL(graph);
  87. for (uint32_t index = 0; index < insert_ops_.size(); ++index) {
  88. GE_CHK_STATUS_RET(insert_ops_[index]->InsertAippToGraph(graph, aippConfigPath, index), "insert op to graph failed");
  89. }
  90. GE_CHK_STATUS_RET(CheckGraph(graph), "after inserting all ops, check graph failed");
  91. GE_CHK_STATUS_RET(graph->TopologicalSorting(), "after insert dynamic op, sort graph failed");
  92. ClearNewOps();
  93. return SUCCESS;
  94. }
  95. void InsertNewOpUtil::ClearNewOps() {
  96. if (insert_op_conf_ != nullptr) {
  97. insert_op_conf_->Clear();
  98. insert_ops_.clear();
  99. }
  100. }
  101. Status InsertNewOpUtil::CheckPositionNotRepeat() {
  102. for (int i = 0; i < insert_op_conf_->aipp_op_size(); i++) {
  103. const domi::AippOpParams *item = insert_op_conf_->mutable_aipp_op(i);
  104. for (int j = i + 1; j < insert_op_conf_->aipp_op_size(); j++) {
  105. const domi::AippOpParams *another_item = insert_op_conf_->mutable_aipp_op(j);
  106. GE_IF_BOOL_EXEC(item->related_input_rank() == another_item->related_input_rank(),
  107. string errormsg = "Can not insert aipp to the same postion! Please ensure related_input_rank"
  108. " param is different in different aipp config.";
  109. ErrorManager::GetInstance().ATCReportErrMessage("E10043", {"reason"}, {errormsg});
  110. GELOGE(PARAM_INVALID,
  111. "Can not insert aipp op to the same postion! Please ensure related_input_rank param "
  112. "is different in different aipp config.");
  113. return PARAM_INVALID;);
  114. }
  115. }
  116. return SUCCESS;
  117. }
  118. Status InsertNewOpUtil::CheckGraph(const ComputeGraphPtr &graph) {
  119. GE_CHECK_NOTNULL(graph);
  120. domi::AippOpParams::AippMode aippMode = domi::AippOpParams::undefined;
  121. for (const auto &node : graph->GetDirectNode()) {
  122. if (node->GetType() != DATA) {
  123. continue;
  124. }
  125. size_t next_nodes_cnt = 0;
  126. std::vector<NodePtr> aippNodes;
  127. for (const auto &anchor : node->GetAllOutDataAnchors()) {
  128. for (const auto &inAnchor : anchor->GetPeerInDataAnchors()) {
  129. const std::string &nodeType = inAnchor->GetOwnerNode()->GetType();
  130. next_nodes_cnt++;
  131. if (nodeType == AIPP) {
  132. aippNodes.push_back(inAnchor->GetOwnerNode());
  133. continue;
  134. }
  135. }
  136. }
  137. GE_CHK_BOOL_RET_STATUS((aippNodes.size() == 0) || (aippNodes.size() == next_nodes_cnt), PARAM_INVALID,
  138. "Can not config part of outputs of Data node to support AIPP, config all "
  139. "of the outputs of Data to support AIPP, or config none of them");
  140. std::unique_ptr<domi::AippOpParams> aippParams(new (std::nothrow) domi::AippOpParams());
  141. GE_CHECK_NOTNULL(aippParams);
  142. GE_IF_BOOL_EXEC(
  143. aippNodes.size() > 1, for (decltype(aippNodes)::size_type i = 1; i < aippNodes.size(); i++) {
  144. std::unique_ptr<domi::AippOpParams> currAippParam(new (std::nothrow) domi::AippOpParams());
  145. GE_CHECK_NOTNULL(currAippParam);
  146. GE_CHK_STATUS(GetAippParams(currAippParam, aippNodes[i]));
  147. if (aippMode == domi::AippOpParams::static_) {
  148. GE_CHK_BOOL_RET_STATUS(aippParams->input_format() == currAippParam->input_format(), PARAM_INVALID,
  149. "The input_format of all aipp_ops after one Data should be the same");
  150. GE_CHK_BOOL_RET_STATUS(aippParams->src_image_size_w() == currAippParam->src_image_size_w(), PARAM_INVALID,
  151. "The src_image_size_w of all aipp_ops after one Data should be the same");
  152. GE_CHK_BOOL_RET_STATUS(aippParams->src_image_size_h() == currAippParam->src_image_size_h(), PARAM_INVALID,
  153. "The src_image_size_h of all aipp_ops after one Data should be the same");
  154. } else {
  155. GE_CHK_BOOL_RET_STATUS(aippParams->max_src_image_size() == currAippParam->max_src_image_size(),
  156. PARAM_INVALID,
  157. "The max_src_image_size of all aipp_ops after one Data should be the same");
  158. }
  159. });
  160. }
  161. return SUCCESS;
  162. }
  163. Status InsertNewOpUtil::GetAippParams(const std::unique_ptr<domi::AippOpParams> &aippParams, const NodePtr &aipp_node) {
  164. GE_CHECK_NOTNULL(aipp_node);
  165. ge::GeAttrValue::NAMED_ATTRS aipp_attr;
  166. const OpDescPtr tmpOpPtr = aipp_node->GetOpDesc();
  167. GE_CHECK_NOTNULL(tmpOpPtr);
  168. GE_CHK_BOOL_RET_STATUS(AttrUtils::GetNamedAttrs(tmpOpPtr, ATTR_NAME_AIPP, aipp_attr), FAILED,
  169. "Aipp node should contain param aipp!");
  170. GE_CHK_STATUS_RET(OpUtils::ConvertAippParams(aipp_attr, aippParams.get()), "get aipp params failed");
  171. return SUCCESS;
  172. }
  173. Status InsertNewOpUtil::AddFormatAgnosticAttrToSwitchn(const NodePtr &aipp_node) {
  174. GE_CHECK_NOTNULL(aipp_node);
  175. auto next_nodes = aipp_node->GetOutDataNodes();
  176. for (const auto next_node : next_nodes) {
  177. GE_CHECK_NOTNULL(next_node);
  178. auto op_desc = next_node->GetOpDesc();
  179. GE_CHECK_NOTNULL(op_desc);
  180. if (op_desc->GetType() == SWITCHN) {
  181. GELOGI("Find switchn node [%s] after aipp [%s]", op_desc->GetName().c_str(), aipp_node->GetName().c_str());
  182. (void)AttrUtils::SetInt(op_desc, "_format_agnostic", kFormatAgnosticSwitch);
  183. (void)AttrUtils::SetListInt(op_desc, "_format_agnostic_except_input",
  184. std::vector<int64_t>({kFormatDependInputIndex}));
  185. }
  186. }
  187. return SUCCESS;
  188. }
  189. Status InsertNewOpUtil::UpdateDataNodeByAipp(const ComputeGraphPtr &graph) {
  190. std::map<std::string, NodePtr> switchn_names_to_data;
  191. std::set<NodePtr> updated_switchn;
  192. NodePtr multbatch_case;
  193. for (auto &node : graph->GetDirectNode()) {
  194. if (node->GetType() == DATA) {
  195. std::string switchn_name;
  196. if (AttrUtils::GetStr(node->GetOpDesc(), kMbatchSwitchnName, switchn_name)) {
  197. switchn_names_to_data[switchn_name] = node;
  198. }
  199. }
  200. if (node->GetType() == AIPP) {
  201. GE_RETURN_IF_ERROR(UpdatePrevNodeByAipp(node, updated_switchn));
  202. // In dynamic batch/HW and dynamic aipp scend, switchn should be set format agnostic, otherwise transdata maybe
  203. // inserted between aipp and switchn which introduce performance and memory increase problem.
  204. GE_RETURN_IF_ERROR(AddFormatAgnosticAttrToSwitchn(node));
  205. }
  206. if (node->GetType() == CASE && node->GetOpDesc()->HasAttr(ATTR_NAME_BATCH_NUM)) {
  207. multbatch_case = node;
  208. }
  209. }
  210. for (auto &switchn : updated_switchn) {
  211. auto data_iter = switchn_names_to_data.find(switchn->GetName());
  212. if (data_iter == switchn_names_to_data.end()) {
  213. GELOGE(INTERNAL_ERROR, "Failed to find relative data node by switchn %s", switchn->GetName().c_str());
  214. return INTERNAL_ERROR;
  215. }
  216. GE_RETURN_IF_ERROR(UpdateDataBySwitchN(switchn, data_iter->second));
  217. }
  218. if (multbatch_case != nullptr) {
  219. GE_RETURN_IF_ERROR(UpdateCaseNode(graph, multbatch_case));
  220. }
  221. return SUCCESS;
  222. }
  223. Status InsertNewOpUtil::FindMaxSizeNode(const ComputeGraphPtr &graph, const NodePtr &case_node,
  224. map<uint32_t, int64_t> &max_sizes,
  225. map<uint32_t, GeTensorDescPtr> &aipp_inputs) {
  226. const auto &func_desc = case_node->GetOpDesc();
  227. for (const auto &name : func_desc->GetSubgraphInstanceNames()) {
  228. const auto &subgraph = graph->GetSubgraph(name);
  229. if (subgraph == nullptr) {
  230. GELOGE(GE_GRAPH_EMPTY_SUBGRAPH, "Subgraph not found, name: %s", name.c_str());
  231. return GE_GRAPH_EMPTY_SUBGRAPH;
  232. }
  233. std::set<NodePtr> updated_switchn; // fix interface
  234. for (auto &node : subgraph->GetDirectNode()) {
  235. if (node->GetType() == AIPP) {
  236. GE_RETURN_IF_ERROR(UpdatePrevNodeByAipp(node, updated_switchn));
  237. int64_t size = 0;
  238. auto in_data_anchor = node->GetInDataAnchor(0);
  239. GE_CHECK_NOTNULL(in_data_anchor);
  240. auto peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  241. GE_CHECK_NOTNULL(peer_out_anchor);
  242. const auto &src_node = peer_out_anchor->GetOwnerNode();
  243. const auto &src_op = src_node->GetOpDesc();
  244. GE_CHECK_NOTNULL(src_op);
  245. uint32_t parent_index = 0;
  246. if (!AttrUtils::GetInt(src_op, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  247. GELOGE(FAILED, "Parent index not found, name: %s", src_op->GetName().c_str());
  248. return FAILED;
  249. }
  250. auto aipp_op_desc = node->GetOpDesc();
  251. GE_CHECK_NOTNULL(aipp_op_desc);
  252. auto input = aipp_op_desc->MutableInputDesc(0);
  253. GE_CHECK_NOTNULL(input);
  254. if (TensorUtils::GetSize(*input, size) == GRAPH_SUCCESS) {
  255. if (max_sizes[parent_index] < size) {
  256. max_sizes[parent_index] = size;
  257. aipp_inputs[parent_index] = input;
  258. }
  259. }
  260. }
  261. }
  262. }
  263. return SUCCESS;
  264. }
  265. Status InsertNewOpUtil::UpdateCaseNode(const ComputeGraphPtr &graph, const NodePtr &case_node) {
  266. const auto &func_desc = case_node->GetOpDesc();
  267. map<uint32_t, int64_t> max_sizes;
  268. map<uint32_t, GeTensorDescPtr> aipp_inputs;
  269. GE_RETURN_IF_ERROR(FindMaxSizeNode(graph, case_node, max_sizes, aipp_inputs));
  270. for (const auto &item : aipp_inputs) {
  271. uint32_t parent_index = item.first;
  272. const GeTensorDescPtr &aipp_input = item.second;
  273. GE_CHECK_NOTNULL(aipp_input);
  274. const GeTensorDescPtr &input_desc = func_desc->MutableInputDesc(parent_index);
  275. GE_CHECK_NOTNULL(input_desc);
  276. input_desc->SetDataType(aipp_input->GetDataType());
  277. input_desc->SetOriginDataType(aipp_input->GetOriginDataType());
  278. input_desc->SetShape(aipp_input->GetShape());
  279. input_desc->SetOriginShape(aipp_input->GetShape());
  280. input_desc->SetFormat(aipp_input->GetFormat());
  281. input_desc->SetOriginFormat(aipp_input->GetFormat());
  282. ge::TensorUtils::SetSize(*input_desc, max_sizes[item.first]);
  283. const auto &in_anchor = case_node->GetInDataAnchor(parent_index);
  284. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  285. const auto &data = out_anchor->GetOwnerNode();
  286. auto data_opdesc = data->GetOpDesc();
  287. GE_CHECK_NOTNULL(data_opdesc);
  288. Format old_format = data_opdesc->MutableOutputDesc(0)->GetFormat();
  289. auto ret = data_opdesc->UpdateOutputDesc(0, *input_desc);
  290. if (ret != GRAPH_SUCCESS) {
  291. GELOGE(INTERNAL_ERROR, "Failed to update data %s output using case %s", data->GetName().c_str(),
  292. case_node->GetName().c_str());
  293. return INTERNAL_ERROR;
  294. }
  295. ret = data_opdesc->UpdateInputDesc(0, *input_desc);
  296. if (ret != GRAPH_SUCCESS) {
  297. GELOGE(INTERNAL_ERROR, "Failed to update data %s input using case %s", data->GetName().c_str(),
  298. case_node->GetName().c_str());
  299. return INTERNAL_ERROR;
  300. }
  301. // Update attr _mbatch_origin_input_dims for data when it is linked to aipp
  302. UpdateMultiBatchInputDims(data_opdesc, old_format);
  303. }
  304. return SUCCESS;
  305. }
  306. Status InsertNewOpUtil::UpdatePrevNodeByAipp(NodePtr &node, std::set<NodePtr> &switchns) {
  307. GELOGI("Start to update prev node size by aipp %s.", node->GetName().c_str());
  308. auto aipp_op_desc = node->GetOpDesc();
  309. GE_CHECK_NOTNULL(aipp_op_desc);
  310. auto aipp_input = aipp_op_desc->MutableInputDesc(0);
  311. GE_CHECK_NOTNULL(aipp_input);
  312. int64_t size = 0;
  313. graphStatus graph_ret = ge::TensorUtils::GetSize(*aipp_input, size);
  314. if (graph_ret != GRAPH_SUCCESS) {
  315. GELOGE(FAILED, "UpdateOutputDesc fail, graph_ret:%d", graph_ret);
  316. return FAILED;
  317. }
  318. GELOGI("Get input size [%ld] from aipp [%s].", size, aipp_op_desc->GetName().c_str());
  319. if (size == 0) {
  320. GELOGE(FAILED, "Can not get size from aipp [%s]", aipp_op_desc->GetName().c_str());
  321. return FAILED;
  322. }
  323. (void)AttrUtils::SetInt(aipp_input, ATTR_NAME_INPUT_ORIGIN_SIZE, size);
  324. auto in_data_anchor = node->GetInDataAnchor(0);
  325. GE_CHECK_NOTNULL(in_data_anchor);
  326. auto peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  327. GE_CHECK_NOTNULL(peer_out_anchor);
  328. const auto &src_node = peer_out_anchor->GetOwnerNode();
  329. const auto &src_op = src_node->GetOpDesc();
  330. GE_CHECK_NOTNULL(src_op);
  331. // if the type of src_node is SwitchN, the input of it may be updated to a size not the max one
  332. // the correct size will be updated in function `UpdateDataBySwitchN`
  333. DataType aipp_dt = aipp_input->GetDataType();
  334. aipp_input->SetOriginDataType(aipp_dt);
  335. DataType aipp_origni_dt = aipp_input->GetOriginDataType();
  336. GeShape aipp_shape = aipp_input->GetShape();
  337. Format aipp_format = aipp_input->GetFormat();
  338. GELOGI("Aipp [%s] input datatype is %s, origin datatype is %s, input shape is %s", aipp_op_desc->GetName().c_str(),
  339. TypeUtils::DataTypeToSerialString(aipp_dt).c_str(), TypeUtils::DataTypeToSerialString(aipp_origni_dt).c_str(),
  340. ge::formats::ShapeToString(aipp_shape.GetDims()).c_str());
  341. const GeTensorDescPtr &input = src_op->MutableInputDesc(0);
  342. GE_CHECK_NOTNULL(input);
  343. input->SetDataType(aipp_dt);
  344. input->SetOriginDataType(aipp_origni_dt);
  345. input->SetShape(aipp_shape);
  346. input->SetOriginShape(aipp_shape);
  347. input->SetFormat(aipp_format);
  348. input->SetOriginFormat(aipp_format);
  349. ge::TensorUtils::SetSize(*input, size);
  350. const GeTensorDescPtr &output = src_op->MutableOutputDesc(peer_out_anchor->GetIdx());
  351. GE_CHECK_NOTNULL(output);
  352. output->SetDataType(aipp_dt);
  353. output->SetOriginDataType(aipp_origni_dt);
  354. output->SetShape(aipp_shape);
  355. output->SetOriginShape(aipp_shape);
  356. output->SetFormat(aipp_format);
  357. output->SetOriginFormat(aipp_format);
  358. ge::TensorUtils::SetSize(*output, size);
  359. if (src_node->GetType() == SWITCHN) {
  360. switchns.insert(src_node);
  361. }
  362. GELOGI("Set node %s output %d size %ld by aipp.", src_node->GetName().c_str(), peer_out_anchor->GetIdx(), size);
  363. return SUCCESS;
  364. }
  365. Status InsertNewOpUtil::UpdateDataBySwitchN(const NodePtr &switchn, const NodePtr &data) {
  366. size_t max_index = switchn->GetOpDesc()->GetOutputsSize();
  367. int64_t max_size = 0;
  368. for (size_t i = 0; i < switchn->GetOpDesc()->GetOutputsSize(); ++i) {
  369. int64_t size = 0;
  370. auto output_desc = switchn->GetOpDesc()->MutableOutputDesc(i);
  371. GE_CHECK_NOTNULL(output_desc);
  372. if (TensorUtils::GetSize(*output_desc, size) == GRAPH_SUCCESS) {
  373. if (max_size < size) {
  374. max_size = size;
  375. max_index = i;
  376. }
  377. }
  378. }
  379. if (max_index >= switchn->GetOpDesc()->GetOutputsSize()) {
  380. GELOGE(INTERNAL_ERROR, "No max size found from switchn node %s", switchn->GetName().c_str());
  381. return INTERNAL_ERROR;
  382. }
  383. auto output_desc = switchn->GetOpDesc()->MutableOutputDesc(max_index);
  384. auto input_desc = switchn->GetOpDesc()->MutableInputDesc(0);
  385. GE_CHECK_NOTNULL(input_desc);
  386. input_desc->SetDataType(output_desc->GetDataType());
  387. input_desc->SetOriginDataType(output_desc->GetOriginDataType());
  388. input_desc->SetShape(output_desc->GetShape());
  389. input_desc->SetOriginShape(output_desc->GetOriginShape());
  390. input_desc->SetFormat(output_desc->GetFormat());
  391. input_desc->SetOriginFormat(output_desc->GetOriginFormat());
  392. TensorUtils::SetSize(*input_desc, max_size);
  393. auto data_opdesc = data->GetOpDesc();
  394. GE_CHECK_NOTNULL(data_opdesc);
  395. Format old_format = data_opdesc->MutableOutputDesc(0)->GetFormat();
  396. auto ret = data_opdesc->UpdateOutputDesc(0, *input_desc);
  397. if (ret != GRAPH_SUCCESS) {
  398. GELOGE(INTERNAL_ERROR, "Failed to update data %s output using switchn %s", data->GetName().c_str(),
  399. switchn->GetName().c_str());
  400. return INTERNAL_ERROR;
  401. }
  402. ret = data_opdesc->UpdateInputDesc(0, *input_desc);
  403. if (ret != GRAPH_SUCCESS) {
  404. GELOGE(INTERNAL_ERROR, "Failed to update data %s input using switchn %s", data->GetName().c_str(),
  405. switchn->GetName().c_str());
  406. return INTERNAL_ERROR;
  407. }
  408. // Update attr _mbatch_origin_input_dims for data when it is linked to aipp
  409. UpdateMultiBatchInputDims(data_opdesc, old_format);
  410. return SUCCESS;
  411. }
  412. void InsertNewOpUtil::UpdateMultiBatchInputDims(const OpDescPtr &data_opdesc, Format &old_format) {
  413. if (!data_opdesc->HasAttr(ATTR_MBATCH_ORIGIN_INPUT_DIMS)) {
  414. GELOGW("Failed to acquire _mbatch_origin_input_dims attr from node [%s]", data_opdesc->GetName().c_str());
  415. return;
  416. }
  417. auto new_data_dims = data_opdesc->GetOutputDesc(0).GetShape().GetDims();
  418. vector<int64_t> origin_input_dims;
  419. (void)AttrUtils::GetListInt(data_opdesc, ATTR_MBATCH_ORIGIN_INPUT_DIMS, origin_input_dims);
  420. // Convert origin_input_dims to NHWC because data format is set to NHWC when it is linked to aipp.
  421. ConvertShape2Nhwc(old_format, origin_input_dims);
  422. if (new_data_dims.size() != origin_input_dims.size()) {
  423. return;
  424. }
  425. for (size_t i = 0; i < origin_input_dims.size(); ++i) {
  426. // Need to update shape when aipp has crop function because H,W is different, ignore -1.
  427. if (origin_input_dims[i] > 0) {
  428. origin_input_dims[i] = new_data_dims[i];
  429. }
  430. }
  431. (void)AttrUtils::SetListInt(data_opdesc, ATTR_MBATCH_ORIGIN_INPUT_DIMS, origin_input_dims);
  432. return;
  433. }
  434. Status InsertNewOpUtil::GetDataRelatedNode(NodePtr &node, std::map<NodePtr, std::set<NodePtr>> &data_next_node_map) {
  435. GELOGI("Start to get data and next node %s.", node->GetName().c_str());
  436. OpDescPtr data_op = node->GetOpDesc();
  437. GE_CHECK_NOTNULL(data_op);
  438. if (!data_op->HasAttr(ATTR_NAME_AIPP)) {
  439. GELOGI("there is not AIPP info for Data: %s.", data_op->GetName().c_str());
  440. return SUCCESS;
  441. }
  442. std::unique_ptr<domi::AippOpParams> aipp_params(new (std::nothrow) domi::AippOpParams());
  443. ge::GeAttrValue::NAMED_ATTRS aipp_attr;
  444. GE_CHK_BOOL_RET_STATUS(AttrUtils::GetNamedAttrs(data_op, ATTR_NAME_AIPP, aipp_attr), GE_AIPP_NOT_EXIST,
  445. "Data node do not contain param aipp!");
  446. GE_CHK_STATUS_RET(OpUtils::ConvertAippParams(aipp_attr, aipp_params.get()), "get aipp params failed");
  447. for (auto out_data_anchor : node->GetAllOutDataAnchors()) {
  448. GE_CHECK_NOTNULL(out_data_anchor);
  449. auto peer_in_anchors = out_data_anchor->GetPeerInDataAnchors();
  450. for (auto peer_in_data_anchor : peer_in_anchors) {
  451. GE_CHECK_NOTNULL(peer_in_data_anchor);
  452. const auto &dst_node = peer_in_data_anchor->GetOwnerNode();
  453. const auto &dst_op = dst_node->GetOpDesc();
  454. GE_CHECK_NOTNULL(dst_op);
  455. if (dst_op->GetType() == AIPP || dst_op->GetType() == SWITCHN || dst_op->GetType() == CASE) {
  456. auto data_iter = data_next_node_map.find(node);
  457. if (data_iter == data_next_node_map.end()) {
  458. std::set<NodePtr> next_node_set;
  459. next_node_set.insert(dst_node);
  460. data_next_node_map[node] = next_node_set;
  461. } else {
  462. if (data_next_node_map[node].find(dst_node) == data_next_node_map[node].end()) {
  463. data_next_node_map[node].insert(dst_node);
  464. }
  465. }
  466. }
  467. }
  468. }
  469. return SUCCESS;
  470. }
  471. Status InsertNewOpUtil::GetAllAipps(const NodePtr &data_node, const NodePtr &node, std::vector<NodePtr> &aipps) {
  472. GE_CHECK_NOTNULL(node);
  473. OpDescPtr op = node->GetOpDesc();
  474. GE_CHECK_NOTNULL(op);
  475. GELOGI("Get all aipp node from this node %s.", op->GetName().c_str());
  476. if (op->GetType() == AIPP) {
  477. aipps.emplace_back(node);
  478. } else if (op->GetType() == SWITCHN) {
  479. for (auto out_data_anchor : node->GetAllOutDataAnchors()) {
  480. GE_CHECK_NOTNULL(out_data_anchor);
  481. auto peer_in_anchors = out_data_anchor->GetPeerInDataAnchors();
  482. if (peer_in_anchors.size() > 0) {
  483. auto peer_in_anchor = peer_in_anchors.at(0);
  484. GE_CHECK_NOTNULL(peer_in_anchor);
  485. auto dst_aipp_node = peer_in_anchor->GetOwnerNode();
  486. if (dst_aipp_node->GetType() == AIPP) {
  487. aipps.emplace_back(dst_aipp_node);
  488. }
  489. }
  490. }
  491. } else if (op->GetType() == CASE) {
  492. const ComputeGraphPtr &graph = node->GetOwnerComputeGraph();
  493. for (const auto &name : op->GetSubgraphInstanceNames()) {
  494. const auto &subgraph = graph->GetSubgraph(name);
  495. if (subgraph == nullptr) {
  496. GELOGE(GE_GRAPH_EMPTY_SUBGRAPH, "Subgraph not found, name: %s", name.c_str());
  497. return GE_GRAPH_EMPTY_SUBGRAPH;
  498. }
  499. for (auto &subgraph_node : subgraph->GetDirectNode()) {
  500. if (subgraph_node->GetType() == AIPP) {
  501. auto src_node = subgraph_node->GetInDataNodes().at(0);
  502. const auto &src_op = src_node->GetOpDesc();
  503. GE_CHECK_NOTNULL(src_op);
  504. uint32_t parent_index = 0;
  505. if (!AttrUtils::GetInt(src_op, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  506. GELOGE(FAILED, "Parent index not found, name: %s", src_op->GetName().c_str());
  507. return FAILED;
  508. }
  509. auto data = node->GetInDataNodes().at(parent_index);
  510. if (data->GetName() == data_node->GetName()) {
  511. aipps.emplace_back(subgraph_node);
  512. }
  513. }
  514. }
  515. }
  516. }
  517. return SUCCESS;
  518. }
  519. Status InsertNewOpUtil::RecordAIPPInfoToData(const ComputeGraphPtr &graph) {
  520. GELOGI("Start to record aipp info to Data.");
  521. std::map<NodePtr, std::set<NodePtr>> data_next_node_map;
  522. for (auto &node : graph->GetDirectNode()) {
  523. if (node->GetType() == DATA) {
  524. GE_RETURN_IF_ERROR(GetDataRelatedNode(node, data_next_node_map));
  525. }
  526. }
  527. for (auto it : data_next_node_map) {
  528. std::vector<std::string> input_dims;
  529. std::vector<std::string> output_dims;
  530. auto data_node = it.first;
  531. auto data_op_desc = data_node->GetOpDesc();
  532. GE_CHECK_NOTNULL(data_op_desc);
  533. std::set<NodePtr> aipps_or_switchs_or_case = it.second;
  534. if (aipps_or_switchs_or_case.size() != 1) {
  535. GELOGW("The number of successors swith or aipp of data is more than 1");
  536. continue;
  537. }
  538. std::vector<NodePtr> aipps;
  539. GE_RETURN_IF_ERROR(GetAllAipps(data_node, *aipps_or_switchs_or_case.begin(), aipps));
  540. GELOGI("RecordAIPPInfoToData: Data: name[%s], type[%s], batch size[%u]", data_node->GetName().c_str(),
  541. data_node->GetType().c_str(), aipps.size());
  542. for (auto aipp_it : aipps) {
  543. string input;
  544. string output;
  545. GetInputOutputInfo(data_node, aipp_it, input, output);
  546. input_dims.emplace_back(input);
  547. output_dims.emplace_back(output);
  548. // When static aipp is set, need to get the model input dims which processed by aipp
  549. GE_RETURN_IF_ERROR(SetModelInputDims(data_node, aipp_it));
  550. }
  551. // if _all_origin_gears_inputs is set, use its value directly.
  552. if (data_op_desc->HasAttr("_all_origin_gears_inputs")) {
  553. std::vector<std::string> input_dims_str;
  554. (void)AttrUtils::GetListStr(data_op_desc, "_all_origin_gears_inputs", input_dims_str);
  555. (void)AttrUtils::SetListStr(data_op_desc, ATTR_NAME_AIPP_INPUTS, input_dims_str);
  556. if ((input_dims_str.size() > output_dims.size()) && !output_dims.empty()) {
  557. // make sure output and input counts is equal, appears in dynamic aipp and dynamic shape/batch scene.
  558. std::vector<std::string> output_dims_str{input_dims_str.size(), output_dims[0]};
  559. (void)AttrUtils::SetListStr(data_op_desc, ATTR_NAME_AIPP_OUTPUTS, output_dims_str);
  560. } else {
  561. (void)AttrUtils::SetListStr(data_op_desc, ATTR_NAME_AIPP_OUTPUTS, output_dims);
  562. }
  563. } else {
  564. (void)AttrUtils::SetListStr(data_op_desc, ATTR_NAME_AIPP_INPUTS, input_dims);
  565. (void)AttrUtils::SetListStr(data_op_desc, ATTR_NAME_AIPP_OUTPUTS, output_dims);
  566. }
  567. }
  568. return SUCCESS;
  569. }
  570. Status InsertNewOpUtil::GetInputOutputInfo(NodePtr &data_node, NodePtr &aipp_node, std::string &input,
  571. std::string &output) {
  572. GE_CHECK_NOTNULL(data_node);
  573. GE_CHECK_NOTNULL(aipp_node);
  574. OpDescPtr data_op = data_node->GetOpDesc();
  575. GE_CHECK_NOTNULL(data_op);
  576. OpDescPtr aipp_op = aipp_node->GetOpDesc();
  577. GE_CHECK_NOTNULL(aipp_op);
  578. // aipp node's original output shape equals to original model data's shape
  579. ConstGeTensorDescPtr output_desc = aipp_op->GetOutputDescPtr(0);
  580. Format orig_format = output_desc->GetOriginFormat();
  581. DataType orig_data_type = output_desc->GetOriginDataType();
  582. std::string tensor_name = data_op->GetName();
  583. size_t dim_num = output_desc->GetOriginShape().GetDimNum();
  584. int64_t tensor_size = 0;
  585. (void)TensorUtils::CalcTensorMemSize(output_desc->GetOriginShape(), orig_format, orig_data_type, tensor_size);
  586. int64_t input_size = tensor_size;
  587. input = TypeUtils::FormatToSerialString(orig_format) + ":" + TypeUtils::DataTypeToSerialString(orig_data_type) + ":" +
  588. tensor_name + ":" + std::to_string(input_size) + ":" + std::to_string(dim_num) + ":" +
  589. formats::JoinToString(output_desc->GetOriginShape().GetDims());
  590. Format format = output_desc->GetFormat();
  591. DataType data_type = output_desc->GetDataType();
  592. std::string output_name = aipp_op->GetOutputNameByIndex(0);
  593. size_t output_dim_num = output_desc->GetShape().GetDimNum();
  594. (void)TensorUtils::CalcTensorMemSize(output_desc->GetShape(), output_desc->GetFormat(), output_desc->GetDataType(),
  595. tensor_size);
  596. int64_t output_size = tensor_size;
  597. output = TypeUtils::FormatToSerialString(format) + ":" + TypeUtils::DataTypeToSerialString(data_type) + ":" +
  598. output_name + ":" + std::to_string(output_size) + ":" + std::to_string(output_dim_num) + ":" +
  599. formats::JoinToString(output_desc->GetShape().GetDims());
  600. GELOGI("GetInputOutputInfo: get data[%s] node related aipp[%s] node info, input[%s], output[%s].",
  601. data_node->GetName().c_str(), aipp_node->GetName().c_str(), input.c_str(), output.c_str());
  602. return SUCCESS;
  603. }
  604. Status InsertNewOpUtil::SetModelInputDims(NodePtr &data_node, NodePtr &aipp_node) {
  605. GE_CHECK_NOTNULL(data_node);
  606. GE_CHECK_NOTNULL(aipp_node);
  607. OpDescPtr data_opdesc = data_node->GetOpDesc();
  608. GE_CHECK_NOTNULL(data_opdesc);
  609. OpDescPtr aipp_opdesc = aipp_node->GetOpDesc();
  610. GE_CHECK_NOTNULL(aipp_opdesc);
  611. // In dynamic bacth/hw scenario, the new model input dims only need be set once
  612. if (data_node->GetOpDesc()->HasAttr(ATTR_NAME_INPUT_DIMS)) {
  613. GELOGD("Data %s already has attribute %s", data_node->GetOpDesc()->GetName().c_str(), ATTR_NAME_INPUT_DIMS.c_str());
  614. return SUCCESS;
  615. }
  616. vector<int64_t> model_input_dims;
  617. vector<int64_t> origin_input_dims;
  618. if (AttrUtils::GetListInt(aipp_opdesc, ATTR_NAME_INPUT_DIMS, model_input_dims) && !model_input_dims.empty()) {
  619. // When dynamic bacth/hw is set, N or HW need to be set to -1
  620. if (AttrUtils::GetListInt(data_opdesc, ATTR_MBATCH_ORIGIN_INPUT_DIMS, origin_input_dims) &&
  621. !origin_input_dims.empty()) {
  622. GELOGI("In dynamic bacth/hw scenario, N or HW need to be set to -1. model_input_dims: %s, origin_input_dims: %s",
  623. formats::JoinToString(model_input_dims).c_str(), formats::JoinToString(origin_input_dims).c_str());
  624. for (size_t i = 0; i < origin_input_dims.size(); ++i) {
  625. // N or HW need to be set to -1
  626. if (origin_input_dims[i] < 0) {
  627. model_input_dims[i] = origin_input_dims[i];
  628. }
  629. }
  630. }
  631. GELOGD("After set N or H/W to -1, the model input dims: %s.", formats::JoinToString(model_input_dims).c_str());
  632. if (!AttrUtils::SetListInt(data_opdesc, ATTR_NAME_INPUT_DIMS, model_input_dims)) {
  633. GELOGE(FAILED, "SetListInt of %s failed.", ATTR_NAME_INPUT_DIMS.c_str());
  634. return FAILED;
  635. }
  636. }
  637. return SUCCESS;
  638. }
  639. } // namespace ge

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