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

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

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