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

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

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