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.

ge_aipp_op.cc 43 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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/ge_aipp_op.h"
  17. #include <memory>
  18. #include <set>
  19. #include <string>
  20. #include <utility>
  21. #include <vector>
  22. #include "base_insert_op.h"
  23. #include "common/dynamic_aipp.h"
  24. #include "common/ge/ge_util.h"
  25. #include "common/util.h"
  26. #include "common/util/error_manager/error_manager.h"
  27. #include "external/graph/operator_factory.h"
  28. #include "framework/common/debug/ge_log.h"
  29. #include "framework/common/ge_inner_error_codes.h"
  30. #include "framework/common/op/ge_op_utils.h"
  31. #include "framework/common/types.h"
  32. #include "framework/omg/omg_inner_types.h"
  33. #include "graph/debug/ge_attr_define.h"
  34. #include "graph/optimize/common/params.h"
  35. #include "graph/utils/graph_utils.h"
  36. #include "graph/utils/node_utils.h"
  37. #include "graph/utils/op_desc_utils.h"
  38. #include "graph/utils/tensor_utils.h"
  39. #include "graph/utils/type_utils.h"
  40. #include "proto/insert_op.pb.h"
  41. #include "graph/common/local_context.h"
  42. #define SAVE_AIPP_ATTR(KEY, SAVE_TYPE) \
  43. do { \
  44. (void)aipp_attrs.SetAttr(#KEY, GeAttrValue::CreateFrom<SAVE_TYPE>(aipp_params_->KEY())); \
  45. } while (0)
  46. #define SAVE_AIPP_ATTR_LIST(KEY, SAVE_TYPE) \
  47. do { \
  48. if (aipp_params_->KEY##_size() > 0) { \
  49. (void)aipp_attrs.SetAttr(#KEY, GeAttrValue::CreateFrom<SAVE_TYPE>(aipp_params_->KEY(0))); \
  50. } \
  51. } while (0)
  52. #define AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(expr, _status, errormsg) \
  53. do { \
  54. bool b = (expr); \
  55. if (!b) { \
  56. GELOGE(_status, errormsg); \
  57. ErrorManager::GetInstance().ATCReportErrMessage("E10043", {"reason"}, {errormsg}); \
  58. return _status; \
  59. } \
  60. } while (0)
  61. namespace {
  62. const int32_t DEFAULT_MATRIX_R0C0_YUV2RGB = 298;
  63. const int32_t DEFAULT_MATRIX_R0C1_YUV2RGB = 0;
  64. const int32_t DEFAULT_MATRIX_R0C2_YUV2RGB = 409;
  65. const int32_t DEFAULT_MATRIX_R1C0_YUV2RGB = 298;
  66. const int32_t DEFAULT_MATRIX_R1C1_YUV2RGB = -100;
  67. const int32_t DEFAULT_MATRIX_R1C2_YUV2RGB = -208;
  68. const int32_t DEFAULT_MATRIX_R2C0_YUV2RGB = 298;
  69. const int32_t DEFAULT_MATRIX_R2C1_YUV2RGB = 516;
  70. const int32_t DEFAULT_MATRIX_R2C2_YUV2RGB = 0;
  71. const int32_t DEFAULT_MATRIX_R0C0_RGB2YUV = 66;
  72. const int32_t DEFAULT_MATRIX_R0C1_RGB2YUV = 129;
  73. const int32_t DEFAULT_MATRIX_R0C2_RGB2YUV = 25;
  74. const int32_t DEFAULT_MATRIX_R1C0_RGB2YUV = -38;
  75. const int32_t DEFAULT_MATRIX_R1C1_RGB2YUV = -74;
  76. const int32_t DEFAULT_MATRIX_R1C2_RGB2YUV = 112;
  77. const int32_t DEFAULT_MATRIX_R2C0_RGB2YUV = 112;
  78. const int32_t DEFAULT_MATRIX_R2C1_RGB2YUV = -94;
  79. const int32_t DEFAULT_MATRIX_R2C2_RGB2YUV = -18;
  80. const int32_t DEFAULT_OUTPUT_BIAS_0 = 16;
  81. const int32_t DEFAULT_OUTPUT_BIAS_1 = 128;
  82. const int32_t DEFAULT_OUTPUT_BIAS_2 = 128;
  83. const int32_t DEFAULT_INPUT_BIAS_0 = 16;
  84. const int32_t DEFAULT_INPUT_BIAS_1 = 128;
  85. const int32_t DEFAULT_INPUT_BIAS_2 = 128;
  86. const float DEFAULT_VAR_RECI_CHN = 1.0;
  87. } // namespace
  88. namespace ge {
  89. namespace {
  90. const char *const kMbatchSwitchnName = "mbatch-switch-name";
  91. const char *const kAippConfigPath = "aipp_config_path";
  92. const char *const kCurrentAippIndex = "current_aipp_index";
  93. const char *const kDynamicAippData = "ascend_dynamic_aipp_data";
  94. const uint64_t kMinTransferShape = 3;
  95. const int kAippImageInputIndex = 0;
  96. const int kAippParamsInputIndex = 1;
  97. const int kAippDataOutputIndex = 0;
  98. const int64_t kDynamicDim = -1;
  99. // the `format` must one NCHW or NHWC
  100. Status GetDataDimN(const ge::NodePtr &data_node, ge::Format format, int64_t &batch) {
  101. auto output_desc = NodeUtils::GetOutputDesc(*data_node, 0);
  102. auto shape = output_desc.GetShape().GetDims();
  103. if (shape.size() == kMinTransferShape) {
  104. batch = 1;
  105. return SUCCESS;
  106. }
  107. if (shape.size() == DIM_DEFAULT_SIZE) {
  108. switch (format) {
  109. case FORMAT_NCHW:
  110. batch = shape[NCHW_DIM_N];
  111. return SUCCESS;
  112. case FORMAT_NHWC:
  113. batch = shape[NHWC_DIM_N];
  114. return SUCCESS;
  115. default:
  116. GELOGE(PARAM_INVALID, "Not support data format: %s", TypeUtils::FormatToSerialString(format).c_str());
  117. return PARAM_INVALID;
  118. }
  119. }
  120. string errormsg = "its shape size must be in range[3,4] which dynamic aipp is linked, "
  121. "maybe this input is not suitable for dynamic aipp";
  122. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  123. {data_node->GetName() + " shape size",
  124. to_string(shape.size()), errormsg});
  125. GELOGE(PARAM_INVALID, "The shape size of this node [%s] which linked dynamic aipp must be in range[3, 4], but is %zu",
  126. data_node->GetName().c_str(), shape.size());
  127. return PARAM_INVALID;
  128. }
  129. // the batch_count must be more than 0
  130. int64_t CalcMaxSize(int64_t batch_count) {
  131. batch_count--;
  132. if (batch_count > 0) {
  133. if (INT64_MAX / batch_count < static_cast<int64_t>(sizeof(kAippDynamicBatchPara))) {
  134. return -1;
  135. }
  136. }
  137. int64_t size = batch_count * sizeof(kAippDynamicBatchPara);
  138. if (INT64_MAX - static_cast<int64_t>(sizeof(kAippDynamicPara)) < size) {
  139. return -1;
  140. }
  141. return size + sizeof(kAippDynamicPara);
  142. }
  143. Format GetAndCheckFormat() {
  144. switch (GetLocalOmgContext().format) {
  145. case domi::DOMI_TENSOR_NCHW:
  146. return FORMAT_NCHW;
  147. case domi::DOMI_TENSOR_NHWC:
  148. return FORMAT_NHWC;
  149. default:
  150. GELOGE(PARAM_INVALID, "Unexpected format found %d", static_cast<int>(GetLocalOmgContext().format));
  151. return FORMAT_ND;
  152. }
  153. }
  154. } // namespace
  155. Status AippOp::Init(domi::AippOpParams *aipp_params) {
  156. aipp_params_ = new (std::nothrow) domi::AippOpParams();
  157. if (aipp_params_ == nullptr) {
  158. return FAILED;
  159. }
  160. aipp_params_->CopyFrom(*aipp_params);
  161. return SUCCESS;
  162. }
  163. AippOp::~AippOp() {
  164. if (aipp_params_ != nullptr) {
  165. delete aipp_params_;
  166. aipp_params_ = nullptr;
  167. }
  168. }
  169. Status AippOp::InsertAippToGraph(ComputeGraphPtr &graph, std::string &aippConfigPath, const uint32_t index) {
  170. GE_CHECK_NOTNULL(graph);
  171. NodePtr target_input = nullptr;
  172. std::vector<std::pair<OutDataAnchorPtr, InDataAnchorPtr>> target_edges;
  173. if (this->ConvertRelatedInputNameToRank() != SUCCESS) {
  174. GELOGE(FAILED, "AippOp: convert related input name to rank failed.");
  175. return FAILED;
  176. }
  177. GE_CHK_STATUS_RET(this->GetTargetPosition(graph, target_input, target_edges), "Get data nodes position failed");
  178. std::map<OutDataAnchorPtr, NodePtr> out_anchors_to_aipp;
  179. for (auto &out_in_anchors : target_edges) {
  180. auto iter = out_anchors_to_aipp.find(out_in_anchors.first);
  181. if (iter == out_anchors_to_aipp.end()) {
  182. auto aipp = CreateAipp(out_in_anchors.first, aippConfigPath, index);
  183. GE_CHECK_NOTNULL(aipp);
  184. out_anchors_to_aipp[out_in_anchors.first] = aipp;
  185. auto ret = GraphUtils::InsertNodeBetweenDataAnchors(out_in_anchors.first, out_in_anchors.second, aipp);
  186. if (ret != GRAPH_SUCCESS) {
  187. GELOGE(INTERNAL_ERROR, "Failed to link edges for aipp node %s", aipp->GetName().c_str());
  188. return INTERNAL_ERROR;
  189. }
  190. // add aipp data if needed
  191. if (GetAippMode() == domi::AippOpParams::dynamic) {
  192. ret = CreateAippData(aipp);
  193. if (ret != SUCCESS) {
  194. GELOGE(INTERNAL_ERROR, "Failed to create aipp data for aipp %s data %s", aipp->GetName().c_str(),
  195. out_in_anchors.first->GetOwnerNode()->GetName().c_str());
  196. return INTERNAL_ERROR;
  197. }
  198. }
  199. GELOGI("Create aipp %s and insert it to the graph", aipp->GetName().c_str());
  200. } else {
  201. out_in_anchors.second->UnlinkAll();
  202. auto &aipp = iter->second;
  203. auto ret = out_in_anchors.second->LinkFrom(aipp->GetOutDataAnchor(0));
  204. if (ret != GRAPH_SUCCESS) {
  205. GELOGE(INTERNAL_ERROR, "Failed to link aipp %s to the peer node %s", aipp->GetName().c_str(),
  206. out_in_anchors.second->GetOwnerNode()->GetName().c_str());
  207. return INTERNAL_ERROR;
  208. }
  209. }
  210. }
  211. return SUCCESS;
  212. }
  213. NodePtr AippOp::CreateAipp(const OutDataAnchorPtr &out_anchor,
  214. const std::string &aippConfigPath, const uint32_t &index) {
  215. const auto &node = out_anchor->GetOwnerNode();
  216. std::string current_name = node->GetName() + "_" + std::to_string(out_anchor->GetIdx()) + "_huawei_aipp";
  217. auto aipp_opdesc_ptr = MakeShared<OpDesc>(current_name, AIPP);
  218. if (aipp_opdesc_ptr == nullptr) {
  219. GELOGE(OUT_OF_MEMORY, "Failed to alloc aipp desc, name %s", current_name.c_str());
  220. return nullptr;
  221. }
  222. // Update attributes
  223. if (AddAippAttrbutes(aipp_opdesc_ptr, aippConfigPath, index) != SUCCESS) {
  224. return nullptr;
  225. }
  226. // Update input desc, the output desc will be flushed when InferShape
  227. auto node_desc = out_anchor->GetOwnerNode()->GetOpDesc();
  228. if (node_desc == nullptr) {
  229. return nullptr;
  230. }
  231. auto opdesc_src_data = node_desc->GetOutputDesc(out_anchor->GetIdx());
  232. if (opdesc_src_data.GetDataType() != DT_FLOAT) {
  233. GELOGW("The datatype of data node %s is not FP32", node_desc->GetName().c_str());
  234. opdesc_src_data.SetDataType(DT_FLOAT);
  235. }
  236. // We must get the TensorDesc from the output anchor on the Data node,
  237. // and update the TensorDesc to the input anchor on the Aipp node.
  238. // Because the InferShape function for the Aipp node needs the input tensor format,
  239. // but the InferFormat process before InferShape can not infer the format
  240. // if the tensor on the Aipp has an unknown shape
  241. if (aipp_opdesc_ptr->UpdateInputDesc(kAippImageInputIndex, opdesc_src_data) != GRAPH_SUCCESS) {
  242. GELOGE(INTERNAL_ERROR, "Failed to update the output desc from node %s to aipp %s", node_desc->GetName().c_str(),
  243. aipp_opdesc_ptr->GetName().c_str());
  244. return nullptr;
  245. }
  246. return node->GetOwnerComputeGraph()->AddNode(aipp_opdesc_ptr);
  247. }
  248. Status AippOp::AddAippAttrbutes(const OpDescPtr &op_desc, const std::string &aipp_cfg_path, const uint32_t &index) {
  249. GeAttrValue::NAMED_ATTRS aipp_attr;
  250. ConvertParamToAttr(aipp_attr);
  251. GE_CHK_BOOL_RET_STATUS(AttrUtils::SetNamedAttrs(op_desc, ATTR_NAME_AIPP, aipp_attr),
  252. INTERNAL_ERROR, "Set name attrs for aipp node failed");
  253. GE_CHK_BOOL_RET_STATUS(AttrUtils::SetStr(op_desc, kAippConfigPath, aipp_cfg_path),
  254. INTERNAL_ERROR, "Set config file path attr for aipp node failed");
  255. std::vector<std::string> empty_names;
  256. GE_CHK_BOOL_RET_STATUS(AttrUtils::SetListStr(op_desc, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, empty_names),
  257. INTERNAL_ERROR, "Set ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES attr for aipp node failed");
  258. GE_CHK_BOOL_RET_STATUS(AttrUtils::SetInt(op_desc, kCurrentAippIndex, index),
  259. INTERNAL_ERROR, "Set kCurrentAippIndex attr for aipp node failed");
  260. // add input/output desc
  261. GeTensorDesc tensor;
  262. GE_CHK_GRAPH_STATUS_RET(op_desc->AddInputDesc("images", tensor), "Failed to add input images for aipp node");
  263. if (GetAippMode() == domi::AippOpParams::dynamic) {
  264. GE_CHK_GRAPH_STATUS_RET(op_desc->AddOptionalInputDesc("params", tensor), "Failed to add params for aipp node");
  265. }
  266. GE_CHK_GRAPH_STATUS_RET(op_desc->AddOutputDesc("features", tensor), "Failed to add output features for aipp node");
  267. return SUCCESS;
  268. }
  269. domi::AippOpParams::AippMode AippOp::GetAippMode() { return aipp_params_->aipp_mode(); }
  270. NodePtr AippOp::FindDataByIndex(const ComputeGraphPtr &graph, int rank) {
  271. int64_t data_index = 0;
  272. for (auto &node : graph->GetDirectNode()) {
  273. if (node->GetType() != DATA) {
  274. continue;
  275. }
  276. // For functional multi batch, Skip Data for index.
  277. if (node->GetOpDesc()->HasAttr(ATTR_INSERT_BY_MBATCH)) {
  278. continue;
  279. }
  280. // There is no `index` attribute on the `Data` node when compile in inference scene
  281. // so we can only use the order of all `Data` nodes to infer the data index
  282. if (data_index++ != rank) {
  283. continue;
  284. }
  285. return node;
  286. }
  287. GELOGE(PARAM_INVALID, "Can not find the data node by index %d", rank);
  288. string errormsg = "Can not find the data node by aipp parameter related_input_rank " + to_string(rank);
  289. ErrorManager::GetInstance().ATCReportErrMessage("E10043", {"reason"}, {errormsg});
  290. return nullptr;
  291. }
  292. Status AippOp::GetAndCheckTarget(const ComputeGraphPtr &graph, int rank, NodePtr &target,
  293. std::set<uint32_t> &edge_indexes) {
  294. auto data_node = FindDataByIndex(graph, rank);
  295. if (data_node == nullptr) {
  296. GELOGE(PARAM_INVALID, "Get target input node for rank %d failed", rank);
  297. return PARAM_INVALID;
  298. }
  299. data_node_linked_aipp = data_node;
  300. auto data_opdesc = data_node->GetOpDesc();
  301. GE_CHECK_NOTNULL(data_opdesc);
  302. string set_dt_str;
  303. if (ge::AttrUtils::GetStr(data_opdesc, ATTR_ATC_USER_DEFINE_DATATYPE, set_dt_str)) {
  304. ErrorManager::GetInstance().ATCReportErrMessage("E10034", {"opname"}, {data_opdesc->GetName()});
  305. GELOGE(INTERNAL_ERROR,
  306. "This input op [%s] is linked to aipp, can not be set to fp16, "
  307. "please check your atc parameter --insert_op_conf, --input_fp16_nodes.",
  308. data_opdesc->GetName().c_str());
  309. return PARAM_INVALID;
  310. }
  311. // add dynamic or static attr memsage to data
  312. if (GetAippMode() == domi::AippOpParams::static_) {
  313. (void)AttrUtils::SetStr(data_opdesc, ATTR_DATA_RELATED_AIPP_MODE, "static_aipp");
  314. } else if (GetAippMode() == domi::AippOpParams::dynamic) {
  315. (void)AttrUtils::SetStr(data_opdesc, ATTR_DATA_RELATED_AIPP_MODE, "dynamic_aipp");
  316. }
  317. // In scenario AIPP+CONV2D+POOLING, keep the aipp info to Data, since AIPP disappear after subgraph optimize
  318. GeAttrValue::NAMED_ATTRS aipp_attr;
  319. ConvertParamToAttr(aipp_attr);
  320. if (!AttrUtils::SetNamedAttrs(data_opdesc, ATTR_NAME_AIPP, aipp_attr)) {
  321. GELOGE(INTERNAL_ERROR, "Set name attrs for Data node failed. id: %d", rank);
  322. return INTERNAL_ERROR;
  323. }
  324. if (aipp_params_->input_edge_idx_size() > 0) {
  325. for (auto edge_index : aipp_params_->input_edge_idx()) {
  326. edge_indexes.insert(edge_index);
  327. }
  328. }
  329. if (!edge_indexes.empty() && (*edge_indexes.rbegin() >= data_node->GetOutDataNodes().size())) {
  330. GELOGE(PARAM_INVALID, "input_edge_idx %u should smaller than out edge size of target input %zu",
  331. *edge_indexes.rbegin(), data_node->GetOutDataNodes().size());
  332. string errormsg = "The aipp parameter input_edge_idx should be smaller than the target input's outnodes.";
  333. ErrorManager::GetInstance().ATCReportErrMessage("E10043", {"reason"}, {errormsg});
  334. return PARAM_INVALID;
  335. }
  336. target = data_node;
  337. return GetStaticTargetNode(graph, data_node, target);
  338. }
  339. Status AippOp::GetStaticTargetNode(const ComputeGraphPtr &graph, NodePtr &data_node, NodePtr &target) {
  340. if (GetAippMode() != domi::AippOpParams::static_) {
  341. return SUCCESS;
  342. }
  343. std::string related_node_name;
  344. if (AttrUtils::GetStr(data_node->GetOpDesc(), kMbatchSwitchnName, related_node_name)) {
  345. if (related_node_name.empty()) {
  346. GELOGE(INTERNAL_ERROR, "The data node %s has switchn node flag, but the value is empty",
  347. data_node->GetName().c_str());
  348. return INTERNAL_ERROR;
  349. }
  350. auto switchn = graph->FindNode(related_node_name);
  351. if (switchn == nullptr) {
  352. GELOGE(INTERNAL_ERROR, "The data node %s has switchn node %s, but can not find it on the graph",
  353. data_node->GetName().c_str(), related_node_name.c_str());
  354. return INTERNAL_ERROR;
  355. }
  356. target = switchn;
  357. GELOGI("Multi-batch/image size and static aipp for data %s, "
  358. "the aipp node will be insert after %s instead of origin data node",
  359. data_node->GetName().c_str(), switchn->GetName().c_str());
  360. return SUCCESS;
  361. }
  362. const auto out_anchor = data_node->GetOutDataAnchor(0);
  363. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  364. if (in_anchor == nullptr) {
  365. continue;
  366. }
  367. const auto &case_node = in_anchor->GetOwnerNode();
  368. if (case_node->GetType() == CASE) {
  369. target = case_node;
  370. return SUCCESS;
  371. }
  372. }
  373. return SUCCESS;
  374. }
  375. Status AippOp::ConvertRelatedInputNameToRank() {
  376. GE_CHECK_NOTNULL(aipp_params_);
  377. string related_input_name = aipp_params_->related_input_name();
  378. if(related_input_name.empty()) {
  379. return SUCCESS;
  380. }
  381. auto data_top_names = domi::GetContext().data_top_names;
  382. GELOGI("Convert name to rank start: data size[%zu]", data_top_names.size());
  383. uint32_t index = 0;
  384. bool convert_flag = false;
  385. for (const auto &data_top_name : data_top_names) {
  386. if (related_input_name == data_top_name) {
  387. aipp_params_->set_related_input_rank(index);
  388. convert_flag = true;
  389. GELOGI("AippOp: rank: %u, top name: %s.", index, data_top_name.c_str());
  390. break;
  391. }
  392. index++;
  393. }
  394. if (!convert_flag) {
  395. string error_msg = "Top name " + related_input_name + "convert rank failed, Please"
  396. " ensure top name in aipp config is the top name of data node.";
  397. ErrorManager::GetInstance().ATCReportErrMessage("E10043", {"reason"}, {error_msg});
  398. GELOGE(PARAM_INVALID, "Top name[%s] converts rank failed.", related_input_name.c_str());
  399. return PARAM_INVALID;
  400. }
  401. return SUCCESS;
  402. }
  403. Status AippOp::GetTargetPosition(ComputeGraphPtr graph, NodePtr &target_input,
  404. std::vector<std::pair<OutDataAnchorPtr, InDataAnchorPtr>> &target_edges) {
  405. GE_CHECK_NOTNULL(graph);
  406. GE_CHECK_NOTNULL(aipp_params_);
  407. std::set<uint32_t> edge_indexes;
  408. const uint32_t related_input_rank = aipp_params_->related_input_rank();
  409. auto ret = GetAndCheckTarget(graph, related_input_rank, target_input, edge_indexes);
  410. if (ret != SUCCESS) {
  411. GELOGE(ret, "Get target input node for rank %u failed", related_input_rank);
  412. return ret;
  413. }
  414. target_edges.clear();
  415. if (target_input->GetType() != CASE) {
  416. for (OutDataAnchorPtr &src_out : target_input->GetAllOutDataAnchors()) {
  417. auto dst_ins = src_out->GetPeerInDataAnchors();
  418. for (uint32_t i = 0; i < dst_ins.size(); ++i) {
  419. auto dst_in = dst_ins.at(i);
  420. if (edge_indexes.empty() || edge_indexes.count(i) > 0) {
  421. target_edges.emplace_back(src_out, dst_in);
  422. }
  423. }
  424. }
  425. } else {
  426. const auto &func_desc = target_input->GetOpDesc();
  427. for (const auto &name : func_desc->GetSubgraphInstanceNames()) {
  428. const auto &subgraph = graph->GetSubgraph(name);
  429. if (subgraph == nullptr) {
  430. GELOGE(GE_GRAPH_EMPTY_SUBGRAPH, "Subgraph not found, name: %s", name.c_str());
  431. return GE_GRAPH_EMPTY_SUBGRAPH;
  432. }
  433. auto data_node = FindDataByIndex(subgraph, related_input_rank);
  434. if (data_node == nullptr) {
  435. GELOGE(PARAM_INVALID, "Get target input node for rank %d failed", related_input_rank);
  436. return PARAM_INVALID;
  437. }
  438. for (OutDataAnchorPtr &src_out : data_node->GetAllOutDataAnchors()) {
  439. auto dst_ins = src_out->GetPeerInDataAnchors();
  440. for (uint32_t i = 0; i < dst_ins.size(); ++i) {
  441. auto dst_in = dst_ins.at(i);
  442. if (edge_indexes.empty() || edge_indexes.count(i) > 0) {
  443. target_edges.emplace_back(src_out, dst_in);
  444. }
  445. }
  446. }
  447. }
  448. }
  449. return SUCCESS;
  450. }
  451. Status AippOp::SetDefaultParams() {
  452. GE_CHECK_NOTNULL(aipp_params_);
  453. const domi::AippOpParams::AippMode aipp_mode = aipp_params_->aipp_mode();
  454. if (aipp_mode == domi::AippOpParams::static_) {
  455. if (aipp_params_->csc_switch()) {
  456. SetCscDefaultValue();
  457. }
  458. SetDtcDefaultValue();
  459. GELOGI("parse aipp params:input_format:%s, csc_switch:%d.",
  460. domi::AippOpParams::InputFormat_Name(aipp_params_->input_format()).c_str(), aipp_params_->csc_switch());
  461. GELOGI("parse aipp params:mean_chn_0:%d, mean_chn_1:%d, mean_chn_2:%d, mean_chn_3:%d.", aipp_params_->mean_chn_0(),
  462. aipp_params_->mean_chn_1(), aipp_params_->mean_chn_2(), aipp_params_->mean_chn_3());
  463. GELOGI("parse aipp params:min_chn_0:%f, min_chn_1:%f, min_chn_2:%f.", aipp_params_->min_chn_0(),
  464. aipp_params_->min_chn_1(), aipp_params_->min_chn_2());
  465. GE_IF_BOOL_EXEC(!aipp_params_->crop(), aipp_params_->set_load_start_pos_h(0); aipp_params_->set_load_start_pos_w(0);
  466. aipp_params_->set_crop_size_h(0); aipp_params_->set_crop_size_w(0););
  467. GE_IF_BOOL_EXEC(!aipp_params_->resize(), aipp_params_->set_resize_output_h(0);
  468. aipp_params_->set_resize_output_w(0););
  469. GE_IF_BOOL_EXEC(!aipp_params_->padding(), aipp_params_->set_left_padding_size(0);
  470. aipp_params_->set_right_padding_size(0); aipp_params_->set_top_padding_size(0);
  471. aipp_params_->set_bottom_padding_size(0););
  472. }
  473. return SUCCESS;
  474. }
  475. Status AippOp::ValidateParams() {
  476. GE_CHECK_NOTNULL(aipp_params_);
  477. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->aipp_mode() != domi::AippOpParams::undefined, PARAM_INVALID,
  478. "When insert AIPP op, aipp_mode must be configured as static or dynamic ");
  479. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->var_reci_chn_0_size() <= 1, PARAM_INVALID,
  480. "The parameter var_reci_chn_0 can not be configed repeatedly");
  481. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->var_reci_chn_1_size() <= 1, PARAM_INVALID,
  482. "The parameter var_reci_chn_1 can not be configed repeatedly");
  483. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->var_reci_chn_2_size() <= 1, PARAM_INVALID,
  484. "The parameter var_reci_chn_2 can not be configed repeatedly");
  485. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->var_reci_chn_3_size() <= 1, PARAM_INVALID,
  486. "The parameter var_reci_chn_3 can not be configed repeatedly");
  487. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r0c0_size() <= 1, PARAM_INVALID,
  488. "The parameter matrix_r0c0 can not be configed repeatedly");
  489. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r0c1_size() <= 1, PARAM_INVALID,
  490. "The parameter matrix_r0c1 can not be configed repeatedly");
  491. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r0c2_size() <= 1, PARAM_INVALID,
  492. "The parameter matrix_r0c2 can not be configed repeatedly");
  493. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r1c0_size() <= 1, PARAM_INVALID,
  494. "The parameter matrix_r1c0 can not be configed repeatedly");
  495. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r1c1_size() <= 1, PARAM_INVALID,
  496. "The parameter matrix_r1c1 can not be configed repeatedly");
  497. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r1c2_size() <= 1, PARAM_INVALID,
  498. "The parameter matrix_r1c2 can not be configed repeatedly");
  499. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r2c0_size() <= 1, PARAM_INVALID,
  500. "The parameter matrix_r2c0 can not be configed repeatedly");
  501. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r2c1_size() <= 1, PARAM_INVALID,
  502. "The parameter matrix_r2c1 can not be configed repeatedly");
  503. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->matrix_r2c2_size() <= 1, PARAM_INVALID,
  504. "The parameter matrix_r2c2 can not be configed repeatedly");
  505. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->output_bias_0_size() <= 1, PARAM_INVALID,
  506. "The parameter output_bias_0 can not be configed repeatedly");
  507. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->output_bias_1_size() <= 1, PARAM_INVALID,
  508. "The parameter output_bias_1 can not be configed repeatedly");
  509. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->output_bias_2_size() <= 1, PARAM_INVALID,
  510. "The parameter output_bias_2 can not be configed repeatedly");
  511. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->input_bias_0_size() <= 1, PARAM_INVALID,
  512. "The parameter input_bias_0 can not be configed repeatedly");
  513. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->input_bias_1_size() <= 1, PARAM_INVALID,
  514. "The parameter input_bias_1 can not be configed repeatedly");
  515. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->input_bias_2_size() <= 1, PARAM_INVALID,
  516. "The parameter input_bias_2 can not be configed repeatedly");
  517. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->input_edge_idx_size() <= 1, PARAM_INVALID,
  518. "The parameter input_edge_idx can not be configed repeatedly");
  519. const domi::AippOpParams::AippMode aipp_mode = aipp_params_->aipp_mode();
  520. if (aipp_mode == domi::AippOpParams::dynamic) {
  521. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(
  522. aipp_params_->max_src_image_size() > 0, PARAM_INVALID,
  523. "For dynamic AIPP params, max_src_image_size must be set which number should be greater than 0");
  524. } else {
  525. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->input_format() != domi::AippOpParams::UNDEFINED, PARAM_INVALID,
  526. "Input format of AIPP conf is undefined");
  527. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->src_image_size_w() >= 0, PARAM_INVALID,
  528. "Src_image_size_w must not be configed smaller than 0");
  529. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->src_image_size_h() >= 0, PARAM_INVALID,
  530. "Src_image_size_h must not be configed smaller than 0");
  531. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->load_start_pos_w() >= 0, PARAM_INVALID,
  532. "Load_start_pos_w must not be configed smaller than 0");
  533. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->load_start_pos_h() >= 0, PARAM_INVALID,
  534. "Load_start_pos_h must not be configed smaller than 0");
  535. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->crop_size_w() >= 0, PARAM_INVALID,
  536. "Crop_size_w must not be configed smaller than 0");
  537. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->resize_output_w() >= 0, PARAM_INVALID,
  538. "Resize_output_w must not be configed smaller than 0");
  539. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->resize_output_h() >= 0, PARAM_INVALID,
  540. "Resize_output_h must not be configed smaller than 0");
  541. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->left_padding_size() >= 0, PARAM_INVALID,
  542. "Left_padding_size must not be configed smaller than 0");
  543. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->right_padding_size() >= 0, PARAM_INVALID,
  544. "Right_padding_size must not be configed smaller than 0");
  545. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->top_padding_size() >= 0, PARAM_INVALID,
  546. "Top_padding_size must not be configed smaller than 0");
  547. AIPP_RETURN_STATUS_AND_REPROT_ERRORMSG(aipp_params_->bottom_padding_size() >= 0, PARAM_INVALID,
  548. "Bottom_padding_size must not be configed smaller than 0");
  549. }
  550. return SUCCESS;
  551. }
  552. void AippOp::SetCscDefaultValue() {
  553. GE_CHECK_NOTNULL_JUST_RETURN(aipp_params_);
  554. if (aipp_params_->input_format() == domi::AippOpParams::YUV420SP_U8) {
  555. CHECK_FALSE_EXEC(aipp_params_->matrix_r0c0_size() > 0, aipp_params_->add_matrix_r0c0(DEFAULT_MATRIX_R2C0_YUV2RGB));
  556. CHECK_FALSE_EXEC(aipp_params_->matrix_r0c1_size() > 0, aipp_params_->add_matrix_r0c1(DEFAULT_MATRIX_R2C1_YUV2RGB));
  557. CHECK_FALSE_EXEC(aipp_params_->matrix_r0c2_size() > 0, aipp_params_->add_matrix_r0c2(DEFAULT_MATRIX_R2C2_YUV2RGB));
  558. CHECK_FALSE_EXEC(aipp_params_->matrix_r1c0_size() > 0, aipp_params_->add_matrix_r1c0(DEFAULT_MATRIX_R1C0_YUV2RGB));
  559. CHECK_FALSE_EXEC(aipp_params_->matrix_r1c1_size() > 0, aipp_params_->add_matrix_r1c1(DEFAULT_MATRIX_R1C1_YUV2RGB));
  560. CHECK_FALSE_EXEC(aipp_params_->matrix_r1c2_size() > 0, aipp_params_->add_matrix_r1c2(DEFAULT_MATRIX_R1C2_YUV2RGB));
  561. CHECK_FALSE_EXEC(aipp_params_->matrix_r2c0_size() > 0, aipp_params_->add_matrix_r2c0(DEFAULT_MATRIX_R0C0_YUV2RGB));
  562. CHECK_FALSE_EXEC(aipp_params_->matrix_r2c1_size() > 0, aipp_params_->add_matrix_r2c1(DEFAULT_MATRIX_R0C1_YUV2RGB));
  563. CHECK_FALSE_EXEC(aipp_params_->matrix_r2c2_size() > 0, aipp_params_->add_matrix_r2c2(DEFAULT_MATRIX_R0C2_YUV2RGB));
  564. } else {
  565. CHECK_FALSE_EXEC(aipp_params_->matrix_r0c0_size() > 0, aipp_params_->add_matrix_r0c0(DEFAULT_MATRIX_R0C0_RGB2YUV));
  566. CHECK_FALSE_EXEC(aipp_params_->matrix_r0c1_size() > 0, aipp_params_->add_matrix_r0c1(DEFAULT_MATRIX_R0C1_RGB2YUV));
  567. CHECK_FALSE_EXEC(aipp_params_->matrix_r0c2_size() > 0, aipp_params_->add_matrix_r0c2(DEFAULT_MATRIX_R0C2_RGB2YUV));
  568. CHECK_FALSE_EXEC(aipp_params_->matrix_r1c0_size() > 0, aipp_params_->add_matrix_r1c0(DEFAULT_MATRIX_R1C0_RGB2YUV));
  569. CHECK_FALSE_EXEC(aipp_params_->matrix_r1c1_size() > 0, aipp_params_->add_matrix_r1c1(DEFAULT_MATRIX_R1C1_RGB2YUV));
  570. CHECK_FALSE_EXEC(aipp_params_->matrix_r1c2_size() > 0, aipp_params_->add_matrix_r1c2(DEFAULT_MATRIX_R1C2_RGB2YUV));
  571. CHECK_FALSE_EXEC(aipp_params_->matrix_r2c0_size() > 0, aipp_params_->add_matrix_r2c0(DEFAULT_MATRIX_R2C0_RGB2YUV));
  572. CHECK_FALSE_EXEC(aipp_params_->matrix_r2c1_size() > 0, aipp_params_->add_matrix_r2c1(DEFAULT_MATRIX_R2C1_RGB2YUV));
  573. CHECK_FALSE_EXEC(aipp_params_->matrix_r2c2_size() > 0, aipp_params_->add_matrix_r2c2(DEFAULT_MATRIX_R2C2_RGB2YUV));
  574. }
  575. CHECK_FALSE_EXEC(aipp_params_->input_bias_0_size() > 0, aipp_params_->add_input_bias_0(DEFAULT_INPUT_BIAS_0));
  576. CHECK_FALSE_EXEC(aipp_params_->input_bias_1_size() > 0, aipp_params_->add_input_bias_1(DEFAULT_INPUT_BIAS_1));
  577. CHECK_FALSE_EXEC(aipp_params_->input_bias_2_size() > 0, aipp_params_->add_input_bias_2(DEFAULT_INPUT_BIAS_2));
  578. CHECK_FALSE_EXEC(aipp_params_->output_bias_0_size() > 0, aipp_params_->add_output_bias_0(DEFAULT_OUTPUT_BIAS_0));
  579. CHECK_FALSE_EXEC(aipp_params_->output_bias_1_size() > 0, aipp_params_->add_output_bias_1(DEFAULT_OUTPUT_BIAS_1));
  580. CHECK_FALSE_EXEC(aipp_params_->output_bias_2_size() > 0, aipp_params_->add_output_bias_2(DEFAULT_OUTPUT_BIAS_2));
  581. }
  582. void AippOp::SetDtcDefaultValue() {
  583. GE_CHECK_NOTNULL_JUST_RETURN(aipp_params_);
  584. CHECK_FALSE_EXEC(aipp_params_->var_reci_chn_0_size() > 0, aipp_params_->add_var_reci_chn_0(DEFAULT_VAR_RECI_CHN));
  585. GELOGD("var_reci_chn_0 is %f, size is %u.", DEFAULT_VAR_RECI_CHN, aipp_params_->var_reci_chn_0_size());
  586. CHECK_FALSE_EXEC(aipp_params_->var_reci_chn_1_size() > 0, aipp_params_->add_var_reci_chn_1(DEFAULT_VAR_RECI_CHN));
  587. GELOGD("var_reci_chn_1 is %f, size is %u.", DEFAULT_VAR_RECI_CHN, aipp_params_->var_reci_chn_1_size());
  588. CHECK_FALSE_EXEC(aipp_params_->var_reci_chn_2_size() > 0, aipp_params_->add_var_reci_chn_2(DEFAULT_VAR_RECI_CHN));
  589. GELOGD("var_reci_chn_2 is %f, size is %u.", DEFAULT_VAR_RECI_CHN, aipp_params_->var_reci_chn_2_size());
  590. CHECK_FALSE_EXEC(aipp_params_->var_reci_chn_3_size() > 0, aipp_params_->add_var_reci_chn_3(DEFAULT_VAR_RECI_CHN));
  591. GELOGD("var_reci_chn_3 is %f, size is %u.", DEFAULT_VAR_RECI_CHN, aipp_params_->var_reci_chn_3_size());
  592. }
  593. Status AippOp::GenerateOpDesc(OpDescPtr op_desc) {
  594. GE_CHECK_NOTNULL(op_desc);
  595. static std::atomic_long atomic_op_idx(0);
  596. auto op_idx = atomic_op_idx.fetch_add(1);
  597. op_desc->SetName(std::string("aipp_node").append(std::to_string(op_idx)));
  598. op_desc->SetType(AIPP);
  599. // Add two InputDesc, add the second after the first one is added successfully.
  600. if ((op_desc->AddInputDesc(GeTensorDesc()) != GRAPH_SUCCESS) ||
  601. (op_desc->AddInputDesc(GeTensorDesc()) != GRAPH_SUCCESS)) {
  602. GELOGE(FAILED, "failed to add input desc");
  603. return FAILED;
  604. }
  605. if (op_desc->AddOutputDesc(GeTensorDesc()) != GRAPH_SUCCESS) {
  606. GELOGE(FAILED, "add output desc failed.");
  607. return FAILED;
  608. }
  609. GeAttrValue::NAMED_ATTRS aipp_attrs;
  610. ConvertParamToAttr(aipp_attrs);
  611. GE_IF_BOOL_EXEC(!AttrUtils::SetNamedAttrs(op_desc, ATTR_NAME_AIPP, aipp_attrs),
  612. GELOGE(FAILED, "failed to set ATTR_NAME_AIPP");
  613. return FAILED);
  614. return SUCCESS;
  615. }
  616. void AippOp::ConvertParamToAttr(GeAttrValue::NAMED_ATTRS &aipp_attrs) {
  617. GE_CHECK_NOTNULL_JUST_RETURN(aipp_params_);
  618. SAVE_AIPP_ATTR(aipp_mode, GeAttrValue::INT);
  619. SAVE_AIPP_ATTR(related_input_rank, GeAttrValue::INT);
  620. if (aipp_params_->aipp_mode() == domi::AippOpParams::static_) {
  621. SAVE_AIPP_ATTR(input_format, GeAttrValue::INT);
  622. SAVE_AIPP_ATTR(csc_switch, GeAttrValue::BOOL);
  623. SAVE_AIPP_ATTR(crop, GeAttrValue::BOOL);
  624. SAVE_AIPP_ATTR(resize, GeAttrValue::BOOL);
  625. SAVE_AIPP_ATTR(load_start_pos_w, GeAttrValue::INT);
  626. SAVE_AIPP_ATTR(load_start_pos_h, GeAttrValue::INT);
  627. SAVE_AIPP_ATTR(crop_size_w, GeAttrValue::INT);
  628. SAVE_AIPP_ATTR(crop_size_h, GeAttrValue::INT);
  629. SAVE_AIPP_ATTR(resize, GeAttrValue::BOOL);
  630. SAVE_AIPP_ATTR(resize_output_w, GeAttrValue::INT);
  631. SAVE_AIPP_ATTR(resize_output_h, GeAttrValue::INT);
  632. SAVE_AIPP_ATTR(padding, GeAttrValue::BOOL);
  633. SAVE_AIPP_ATTR(left_padding_size, GeAttrValue::INT);
  634. SAVE_AIPP_ATTR(right_padding_size, GeAttrValue::INT);
  635. SAVE_AIPP_ATTR(top_padding_size, GeAttrValue::INT);
  636. SAVE_AIPP_ATTR(bottom_padding_size, GeAttrValue::INT);
  637. SAVE_AIPP_ATTR(src_image_size_w, GeAttrValue::INT);
  638. SAVE_AIPP_ATTR(src_image_size_h, GeAttrValue::INT);
  639. SAVE_AIPP_ATTR(cpadding_value, GeAttrValue::FLOAT);
  640. SAVE_AIPP_ATTR(rbuv_swap_switch, GeAttrValue::BOOL);
  641. SAVE_AIPP_ATTR(ax_swap_switch, GeAttrValue::BOOL);
  642. SAVE_AIPP_ATTR(single_line_mode, GeAttrValue::BOOL);
  643. SAVE_AIPP_ATTR(mean_chn_0, GeAttrValue::INT);
  644. SAVE_AIPP_ATTR(mean_chn_1, GeAttrValue::INT);
  645. SAVE_AIPP_ATTR(mean_chn_2, GeAttrValue::INT);
  646. SAVE_AIPP_ATTR(mean_chn_3, GeAttrValue::INT);
  647. SAVE_AIPP_ATTR(min_chn_0, GeAttrValue::FLOAT);
  648. SAVE_AIPP_ATTR(min_chn_1, GeAttrValue::FLOAT);
  649. SAVE_AIPP_ATTR(min_chn_2, GeAttrValue::FLOAT);
  650. SAVE_AIPP_ATTR(min_chn_3, GeAttrValue::FLOAT);
  651. SAVE_AIPP_ATTR_LIST(var_reci_chn_0, GeAttrValue::FLOAT);
  652. SAVE_AIPP_ATTR_LIST(var_reci_chn_1, GeAttrValue::FLOAT);
  653. SAVE_AIPP_ATTR_LIST(var_reci_chn_2, GeAttrValue::FLOAT);
  654. SAVE_AIPP_ATTR_LIST(var_reci_chn_3, GeAttrValue::FLOAT);
  655. SAVE_AIPP_ATTR_LIST(matrix_r0c0, GeAttrValue::INT);
  656. SAVE_AIPP_ATTR_LIST(matrix_r0c1, GeAttrValue::INT);
  657. SAVE_AIPP_ATTR_LIST(matrix_r0c2, GeAttrValue::INT);
  658. SAVE_AIPP_ATTR_LIST(matrix_r1c0, GeAttrValue::INT);
  659. SAVE_AIPP_ATTR_LIST(matrix_r1c1, GeAttrValue::INT);
  660. SAVE_AIPP_ATTR_LIST(matrix_r1c2, GeAttrValue::INT);
  661. SAVE_AIPP_ATTR_LIST(matrix_r2c0, GeAttrValue::INT);
  662. SAVE_AIPP_ATTR_LIST(matrix_r2c1, GeAttrValue::INT);
  663. SAVE_AIPP_ATTR_LIST(matrix_r2c2, GeAttrValue::INT);
  664. SAVE_AIPP_ATTR_LIST(output_bias_0, GeAttrValue::INT);
  665. SAVE_AIPP_ATTR_LIST(output_bias_1, GeAttrValue::INT);
  666. SAVE_AIPP_ATTR_LIST(output_bias_2, GeAttrValue::INT);
  667. SAVE_AIPP_ATTR_LIST(input_bias_0, GeAttrValue::INT);
  668. SAVE_AIPP_ATTR_LIST(input_bias_1, GeAttrValue::INT);
  669. SAVE_AIPP_ATTR_LIST(input_bias_2, GeAttrValue::INT);
  670. } else {
  671. SAVE_AIPP_ATTR(max_src_image_size, GeAttrValue::INT);
  672. SAVE_AIPP_ATTR(support_rotation, GeAttrValue::BOOL);
  673. }
  674. }
  675. Status AippOp::CreateAippData(const NodePtr &aipp_node) {
  676. GELOGD("Enter add aipp data node process.");
  677. // get previous node, it should be DATA
  678. auto data_node = aipp_node->GetInDataNodes().at(kAippImageInputIndex);
  679. auto data_op_desc = data_node->GetOpDesc();
  680. GE_CHECK_NOTNULL(data_op_desc);
  681. auto ori_data_format = GetAndCheckFormat();
  682. if (ori_data_format != FORMAT_NCHW && ori_data_format != FORMAT_NHWC) {
  683. string format_str = TypeUtils::FormatToSerialString(ori_data_format);
  684. GELOGE(PARAM_INVALID, "when dynamic aipp, input_format must be NCHW or NHWC, but [%s] format is %s",
  685. data_node->GetName().c_str(), format_str.c_str());
  686. string reason = "format must be NCHW or NHWC in dynamic aipp process";
  687. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  688. {data_node->GetName(), "format " + format_str, reason});
  689. return PARAM_INVALID;
  690. }
  691. // dynamic aipp shape HWC is not fixed, need to be set -1
  692. int64_t data_shape_n = 0;
  693. // dynamic batch or HW, need acquire N from ATTR_MBATCH_ORIGIN_INPUT_DIMS
  694. if (data_op_desc->HasAttr(ATTR_MBATCH_ORIGIN_INPUT_DIMS)) {
  695. vector<int64_t> origin_input_dims;
  696. (void)AttrUtils::GetListInt(data_op_desc, ATTR_MBATCH_ORIGIN_INPUT_DIMS, origin_input_dims);
  697. if (!origin_input_dims.empty()) {
  698. data_shape_n = origin_input_dims[0];
  699. }
  700. } else {
  701. data_shape_n = data_op_desc->MutableInputDesc(0)->GetShape().GetDim(0);
  702. }
  703. vector<int64_t> dynamic_aipp_linked_data_shape{data_shape_n, kDynamicDim, kDynamicDim, kDynamicDim};
  704. (void)AttrUtils::SetListInt(data_op_desc, ATTR_DYNAMIC_AIPP_INPUT_DIMS, dynamic_aipp_linked_data_shape);
  705. int64_t batch_count = -1;
  706. if (GetDataDimN(data_node, ori_data_format, batch_count) != ge::SUCCESS) {
  707. GELOGE(PARAM_INVALID, "Get data_node dims and transfer to nchw_dims failed!");
  708. return PARAM_INVALID;
  709. }
  710. if (batch_count <= 0) {
  711. GELOGE(PARAM_INVALID, "Batch count %ld is invalid", batch_count);
  712. return PARAM_INVALID;
  713. }
  714. int64_t max_dynamic_aipp_size = CalcMaxSize(batch_count);
  715. if (max_dynamic_aipp_size < 0) {
  716. GELOGE(PARAM_INVALID, "The dynamic aipp size is not positive.");
  717. return PARAM_INVALID;
  718. }
  719. GELOGI("Add aipp input data, batch count is %ld, max_dynamic_aipp_size is %ld", batch_count, max_dynamic_aipp_size);
  720. return AddNodeToGraph(aipp_node, max_dynamic_aipp_size);
  721. }
  722. Status AippOp::AddAttrToAippData(const OpDescPtr &aipp_data_op_desc) {
  723. // Add dynamic aipp config to aipp_data
  724. GeAttrValue::NAMED_ATTRS aipp_attr;
  725. ConvertParamToAttr(aipp_attr);
  726. (void)AttrUtils::SetNamedAttrs(aipp_data_op_desc, ATTR_NAME_AIPP, aipp_attr);
  727. (void)AttrUtils::SetStr(aipp_data_op_desc, ATTR_DATA_RELATED_AIPP_MODE, "dynamic_aipp_conf");
  728. // add node name attr to data linked aipp_data, it can be queried by acl.
  729. GE_CHECK_NOTNULL(data_node_linked_aipp);
  730. auto data_op_desc = data_node_linked_aipp->GetOpDesc();
  731. GE_CHECK_NOTNULL(data_op_desc);
  732. (void)AttrUtils::SetStr(data_op_desc, ATTR_DATA_AIPP_DATA_NAME_MAP, aipp_data_op_desc->GetName());
  733. (void)AttrUtils::SetStr(aipp_data_op_desc, ATTR_DATA_AIPP_DATA_NAME_MAP, data_op_desc->GetName());
  734. return SUCCESS;
  735. }
  736. Status AippOp::AddNodeToGraph(const NodePtr &aipp_node, int64_t max_dynamic_aipp_size) {
  737. std::vector<int64_t> input_shape_dim(1, max_dynamic_aipp_size);
  738. GeShape input_shape(input_shape_dim);
  739. // construct input tensor
  740. GeTensorDesc input_tensor(input_shape, FORMAT_ND, DT_UINT8);
  741. TensorUtils::SetReuseInput(input_tensor, false);
  742. TensorUtils::SetSize(input_tensor, max_dynamic_aipp_size);
  743. GE_CHECK_NOTNULL(aipp_node);
  744. const ComputeGraphPtr &graph = aipp_node->GetOwnerComputeGraph();
  745. string node_name;
  746. // First aippdata name should be definite.
  747. if (graph->FindFirstNodeMatchType(AIPPDATA) == nullptr) {
  748. GELOGI("Current graph has no aippdata node, so the name of it must be definite.");
  749. node_name = kDynamicAippData;
  750. } else {
  751. node_name = string(kDynamicAippData) + "_" + aipp_node->GetName();
  752. }
  753. GELOGI("Current add aippdata node name is %s", node_name.c_str());
  754. // new add aipp_data ops for dynamic aipp param input
  755. OpDescPtr op_desc_ptr_data = MakeShared<OpDesc>(node_name, AIPPDATA);
  756. GE_CHECK_NOTNULL(op_desc_ptr_data);
  757. if (AddAttrToAippData(op_desc_ptr_data) != SUCCESS) {
  758. return INTERNAL_ERROR;
  759. }
  760. auto stat1 = op_desc_ptr_data->AddInputDesc(input_tensor);
  761. GeShape output_shape(input_shape_dim);
  762. // construct output tensor
  763. GeTensorDesc output_tensor(output_shape, FORMAT_ND, DT_UINT8);
  764. TensorUtils::SetReuseInput(output_tensor, false);
  765. TensorUtils::SetSize(output_tensor, max_dynamic_aipp_size);
  766. auto stat2 = op_desc_ptr_data->AddOutputDesc(output_tensor);
  767. NodePtr aipp_data_node_ptr = graph->AddNode(op_desc_ptr_data);
  768. GE_CHECK_NOTNULL(aipp_data_node_ptr);
  769. // add node desc for aipp node
  770. auto stat3 = aipp_node->GetOpDesc()->UpdateInputDesc(kAippParamsInputIndex, output_tensor);
  771. if (stat1 != GRAPH_SUCCESS || stat2 != GRAPH_SUCCESS || stat3 != GRAPH_SUCCESS) {
  772. GELOGE(INTERNAL_ERROR, "node process desc failed!");
  773. return INTERNAL_ERROR;
  774. }
  775. // aipp_node should have two input data but now tbe only one input
  776. if (GraphUtils::AddEdge(aipp_data_node_ptr->GetOutDataAnchor(kAippDataOutputIndex),
  777. aipp_node->GetInDataAnchor(kAippParamsInputIndex)) != GRAPH_SUCCESS) {
  778. GELOGE(INTERNAL_ERROR, "Add Anchor anchor between aipp data node and aipp failed!");
  779. return INTERNAL_ERROR;
  780. }
  781. return SUCCESS;
  782. }
  783. } // namespace ge

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