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.

multi_batch_options.cc 31 kB

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
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
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
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
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 "multi_batch_options.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include "framework/omg/omg_inner_types.h"
  19. #include "framework/common/util.h"
  20. #include "framework/common/string_util.h"
  21. #include "common/formats/utils/formats_trans_utils.h"
  22. #include "common/util/error_manager/error_manager.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. #include "graph/utils/node_utils.h"
  25. #include "graph/ge_context.h"
  26. #include "graph/common/local_context.h"
  27. #include "framework/common/types.h"
  28. #include "graph/compute_graph.h"
  29. #include "graph/utils/graph_utils.h"
  30. #include "graph/common/omg_util.h"
  31. namespace ge {
  32. namespace multibatch {
  33. constexpr int kDecimal = 10;
  34. constexpr uint8_t kMaxShapesCount = 100;
  35. constexpr uint8_t kMinShapesCount = 2;
  36. const int kDynmaicDims = -1;
  37. const int kDynamicImgSizeDynamciDimsNum = 2;
  38. const size_t kNumOfGetnextNode = 1;
  39. const int kDivisionConst = 2;
  40. const char *const kSubstrOfGetNextNosinkName = "IteratorGetNext";
  41. const char *const kShapeDataName = "ascend_mbatch_shape_data";
  42. const char *const kGetNextName = "IteratorV2";
  43. inline bool IsGetNextType(const NodePtr &node) {
  44. std::string original_type;
  45. GE_IF_BOOL_EXEC(GetOriginalType(node, original_type) != SUCCESS,
  46. GELOGW("Get original type failed."); return false);
  47. return (original_type == kGetNextName);
  48. }
  49. void ParseDynamicSize(string dynamic_size, vector<vector<int64_t>> &shapes) {
  50. std::vector<std::string> shape_strs = ge::StringUtils::Split(dynamic_size, ';');
  51. for (const auto &shape_str : shape_strs) {
  52. if (shape_str.empty()) {
  53. continue;
  54. }
  55. std::vector<int64_t> shape;
  56. std::vector<std::string> dims = ge::StringUtils::Split(shape_str, ',');
  57. for (const auto &dim : dims) {
  58. if (dim.empty()) {
  59. continue;
  60. }
  61. shape.emplace_back(std::strtol(dim.c_str(), nullptr, kDecimal));
  62. }
  63. if (!shape.empty()) {
  64. shapes.emplace_back(shape);
  65. }
  66. }
  67. }
  68. Status DistinguishGetNextAndData(ComputeGraphPtr &graph, vector<NodePtr> &data_nodes,
  69. vector<NodePtr> &getnext_nosink_nodes, vector<NodePtr> &getnext_sink_nodes) {
  70. GELOGD("Start distinguish getnext and data node.");
  71. for (NodePtr &input_node : graph->GetDirectNode()) {
  72. GE_CHECK_NOTNULL(input_node);
  73. OpDescPtr op_desc = input_node->GetOpDesc();
  74. GE_CHECK_NOTNULL(op_desc);
  75. if (op_desc->GetType() == DATA && op_desc->GetName() != kShapeDataName) {
  76. if (op_desc->GetName().find(kSubstrOfGetNextNosinkName) == string::npos) {
  77. data_nodes.emplace_back(input_node);
  78. GELOGD("Name of data node is %s.", op_desc->GetName().c_str());
  79. } else {
  80. getnext_nosink_nodes.emplace_back(input_node);
  81. GELOGD("Name of getnext nosink is %s.", op_desc->GetName().c_str());
  82. }
  83. }
  84. if (IsGetNextType(input_node)) {
  85. GELOGD("Name of getnext sink is %s.", op_desc->GetName().c_str());
  86. getnext_sink_nodes.emplace_back(input_node);
  87. }
  88. }
  89. GELOGI("Data count is %zu, getnext nosink count is %zu, getnext sink count is %zu.", data_nodes.size(),
  90. getnext_nosink_nodes.size(), getnext_sink_nodes.size());
  91. GetLocalOmgContext().data_nodes = data_nodes;
  92. GetLocalOmgContext().getnext_nosink_nodes = getnext_nosink_nodes;
  93. return SUCCESS;
  94. }
  95. Status CheckSequenceOfData(ComputeGraphPtr &graph, const vector<NodePtr> &data_nodes) {
  96. GELOGD("Start check input sequence from data nodes and input shape.");
  97. if (data_nodes.size() != GetLocalOmgContext().user_input_dims.size()) {
  98. REPORT_INNER_ERROR("E19999", "Count:%zu of data_nodes in graph:%s should be equal to "
  99. "input_shape count:%zu from option, check invalid",
  100. data_nodes.size(), graph->GetName().c_str(), GetLocalOmgContext().user_input_dims.size());
  101. GELOGE(PARAM_INVALID, "[Check][Param] Count:%zu of data_nodes in graph:%s should be equal to "
  102. "input_shape count:%zu from option",
  103. data_nodes.size(), graph->GetName().c_str(), GetLocalOmgContext().user_input_dims.size());
  104. return PARAM_INVALID;
  105. }
  106. for (size_t i = 0; i < data_nodes.size(); ++i) {
  107. auto data_node = data_nodes.at(i);
  108. GE_CHECK_NOTNULL(data_node);
  109. GE_CHECK_NOTNULL(data_node->GetOpDesc());
  110. auto output_shape = data_node->GetOpDesc()->GetOutputDesc(0).GetShape().GetDims();
  111. auto dynamic_dims = GetLocalOmgContext().user_input_dims.at(i).second;
  112. GELOGD("The %zu data node is %s, node shape is %s, dynamic dim is %s.", i, data_node->GetName().c_str(),
  113. formats::JoinToString(output_shape).c_str(), formats::JoinToString(dynamic_dims).c_str());
  114. if (output_shape.empty() && dynamic_dims.size() == 1 && dynamic_dims.at(0) == 0) {
  115. GELOGI("No need to check sequence for constant.");
  116. continue;
  117. }
  118. if (dynamic_dims.size() != output_shape.size()) {
  119. REPORT_INNER_ERROR("E19999", "The output shape of %s is %s, the input shape from options of %s is %s, graph:%s,"
  120. "check invalid", data_node->GetName().c_str(),
  121. formats::JoinToString(output_shape).c_str(),
  122. GetLocalOmgContext().user_input_dims.at(i).first.c_str(),
  123. formats::JoinToString(dynamic_dims).c_str(), graph->GetName().c_str());
  124. GELOGE(PARAM_INVALID, "[Check][Param] The output shape of %s is %s, "
  125. "the input shape from options of %s is %s, graph:%s",
  126. data_node->GetName().c_str(), formats::JoinToString(output_shape).c_str(),
  127. GetLocalOmgContext().user_input_dims.at(i).first.c_str(),
  128. formats::JoinToString(dynamic_dims).c_str(), graph->GetName().c_str());
  129. return PARAM_INVALID;
  130. }
  131. for (size_t j = 0; j < dynamic_dims.size(); ++j) {
  132. if (dynamic_dims.at(j) != kDynmaicDims && dynamic_dims.at(j) != output_shape.at(j)) {
  133. REPORT_INNER_ERROR("E19999", "Value of input shape %s from option and output shape %s of data op:%s "
  134. "should be equal to %d, index:%zu, graph:%s, check invalid",
  135. formats::JoinToString(dynamic_dims).c_str(),
  136. formats::JoinToString(output_shape).c_str(), data_node->GetName().c_str(), kDynmaicDims,
  137. j, graph->GetName().c_str());
  138. GELOGE(INTERNAL_ERROR, "[Check][Param] Value of input shape %s from option and output shape %s of data op:%s "
  139. "should be equal to %d, index:%zu, graph:%s",
  140. formats::JoinToString(dynamic_dims).c_str(), formats::JoinToString(output_shape).c_str(),
  141. data_node->GetName().c_str(), kDynmaicDims, j, graph->GetName().c_str());
  142. return INTERNAL_ERROR;
  143. }
  144. }
  145. }
  146. return SUCCESS;
  147. }
  148. Status CheckSequenceOfGetnext(ComputeGraphPtr &graph, const vector<NodePtr> &getnext_sink_node) {
  149. GELOGD("Start check input sequence from getnext sink nodes and input shape.");
  150. if (getnext_sink_node.size() != kNumOfGetnextNode) {
  151. REPORT_INNER_ERROR("E19999", "Not support dynamic dims when a graph with multi getnext nodes, graph:%s, "
  152. "num of getnext node:%zu, check invalid",
  153. graph->GetName().c_str(), getnext_sink_node.size());
  154. GELOGE(PARAM_INVALID, "[Check][Param] Not support dynamic dims when a graph with multi getnext nodes, graph:%s, "
  155. "num of getnext node:%zu", graph->GetName().c_str(), getnext_sink_node.size());
  156. return PARAM_INVALID;
  157. }
  158. auto data_node = getnext_sink_node.at(0);
  159. GE_CHECK_NOTNULL(data_node);
  160. auto op_desc = data_node->GetOpDesc();
  161. GE_CHECK_NOTNULL(op_desc);
  162. size_t data_count = data_node->GetAllOutDataAnchors().size() / kDivisionConst;
  163. if (data_count != GetLocalOmgContext().user_input_dims.size()) {
  164. REPORT_INNER_ERROR("E19999", "Output desc count of %s is %zu, should be equal to count of input shape:%zu, "
  165. "graph:%s, check invalid", op_desc->GetName().c_str(), data_count,
  166. GetLocalOmgContext().user_input_dims.size(), graph->GetName().c_str());
  167. GELOGE(PARAM_INVALID, "[Check][Param] Output desc count of %s is %zu, "
  168. "should be equal to count of input shape:%zu, graph:%s", op_desc->GetName().c_str(),
  169. data_count, GetLocalOmgContext().user_input_dims.size(), graph->GetName().c_str());
  170. return PARAM_INVALID;
  171. }
  172. for (size_t i = 0; i < data_count; ++i) {
  173. auto output_shape = data_node->GetOpDesc()->GetOutputDesc(i).GetShape().GetDims();
  174. auto dynamic_dims = GetLocalOmgContext().user_input_dims.at(i).second;
  175. GELOGD("The %zu getnext node is %s, node shape is %s, dynamic dim is %s.", i, data_node->GetName().c_str(),
  176. formats::JoinToString(output_shape).c_str(), formats::JoinToString(dynamic_dims).c_str());
  177. if (output_shape.empty() && dynamic_dims.size() == 1 && dynamic_dims.at(0) == 0) {
  178. GELOGI("No need to check sequence for constant.");
  179. continue;
  180. }
  181. if (dynamic_dims.size() != output_shape.size()) {
  182. REPORT_INNER_ERROR("E19999", "The %zu output_shape of %s is %s not equal to the input_shape:%s "
  183. "from options of %s, graph:%s, check invalid", i,
  184. data_node->GetName().c_str(), formats::JoinToString(output_shape).c_str(),
  185. formats::JoinToString(dynamic_dims).c_str(),
  186. GetLocalOmgContext().user_input_dims.at(i).first.c_str(),
  187. graph->GetName().c_str());
  188. GELOGE(PARAM_INVALID, "[Check][Param] The %zu output_shape of %s is %s not equal to the input_shape:%s "
  189. "from options of %s, graph:%s", i, data_node->GetName().c_str(),
  190. formats::JoinToString(output_shape).c_str(), formats::JoinToString(dynamic_dims).c_str(),
  191. GetLocalOmgContext().user_input_dims.at(i).first.c_str(), graph->GetName().c_str());
  192. return PARAM_INVALID;
  193. }
  194. for (size_t j = 0; j < dynamic_dims.size(); ++j) {
  195. if (dynamic_dims.at(j) != kDynmaicDims && dynamic_dims.at(j) != output_shape.at(j)) {
  196. REPORT_INNER_ERROR("E19999", "Value of input shape %s from option and output shape %s of data op:%s "
  197. "should be equal to %d, index:%zu, graph:%s, check invalid",
  198. formats::JoinToString(dynamic_dims).c_str(),
  199. formats::JoinToString(output_shape).c_str(), data_node->GetName().c_str(), kDynmaicDims,
  200. j, graph->GetName().c_str());
  201. GELOGE(INTERNAL_ERROR, "[Check][Param] Value of input shape %s from option and output shape %s of data op:%s "
  202. "should be equal to %d, index:%zu, graph:%s", formats::JoinToString(dynamic_dims).c_str(),
  203. formats::JoinToString(output_shape).c_str(), data_node->GetName().c_str(), kDynmaicDims,
  204. j, graph->GetName().c_str());
  205. return INTERNAL_ERROR;
  206. }
  207. }
  208. }
  209. return SUCCESS;
  210. }
  211. Status CheckSequenceOfOptions(ComputeGraphPtr &graph, vector<NodePtr> &data_nodes,
  212. vector<NodePtr> &getnext_nosink_nodes, vector<NodePtr> &getnext_sink_nodes) {
  213. if (GetLocalOmgContext().dynamic_node_type.empty()) {
  214. GELOGI("No need to CheckSequenceOfOptions.");
  215. return SUCCESS;
  216. }
  217. if (DistinguishGetNextAndData(graph, data_nodes, getnext_nosink_nodes, getnext_sink_nodes) != SUCCESS) {
  218. GELOGE(PARAM_INVALID, "[Call][DistinguishGetNextAndData] failed.");
  219. return PARAM_INVALID;
  220. }
  221. if (GetLocalOmgContext().dynamic_node_type == DATA) {
  222. GELOGD("Users want data nodes to be dynamic.");
  223. if (CheckSequenceOfData(graph, data_nodes) != SUCCESS) {
  224. GELOGE(PARAM_INVALID, "[Check][Sequence] Of Data nodes failed.");
  225. return PARAM_INVALID;
  226. }
  227. } else {
  228. GELOGD("Users want getnext nodes to be dynamic.");
  229. if (!getnext_nosink_nodes.empty()) {
  230. if (CheckSequenceOfData(graph, getnext_nosink_nodes) != SUCCESS) {
  231. GELOGE(PARAM_INVALID, "[Check][Sequence] of getnext nosink nodes failed.");
  232. return PARAM_INVALID;
  233. }
  234. } else {
  235. if (CheckSequenceOfGetnext(graph, getnext_sink_nodes) != SUCCESS) {
  236. GELOGE(PARAM_INVALID, "[Check][Sequence] of getnext sink nodes failed.");
  237. return PARAM_INVALID;
  238. }
  239. }
  240. }
  241. return SUCCESS;
  242. }
  243. Status UpdateNameOfData(ComputeGraphPtr &graph, const vector<NodePtr> &data_nodes) {
  244. GELOGD("Update first value of input shape by data nodes.");
  245. if (data_nodes.size() != GetLocalOmgContext().user_input_dims.size()) {
  246. REPORT_INNER_ERROR("E19999", "Count:%zu of data_nodes in graph:%s should be equal to "
  247. "input_shape count:%zu from option, check invalid",
  248. data_nodes.size(), graph->GetName().c_str(), GetLocalOmgContext().user_input_dims.size());
  249. GELOGE(PARAM_INVALID, "[Check][Param] Count:%zu of data_nodes in graph:%s should be equal to "
  250. "input_shape count:%zu from option",
  251. data_nodes.size(), graph->GetName().c_str(), GetLocalOmgContext().user_input_dims.size());
  252. return PARAM_INVALID;
  253. }
  254. for (size_t i = 0; i < data_nodes.size(); ++i) {
  255. GELOGD("The %zu data name is %s.", i, data_nodes.at(i)->GetOpDesc()->GetName().c_str());
  256. GetLocalOmgContext().user_input_dims.at(i).first = data_nodes.at(i)->GetOpDesc()->GetName();
  257. }
  258. return SUCCESS;
  259. }
  260. Status UpdateNameOfGetnext(ComputeGraphPtr &graph, const vector<NodePtr> &getnext_sink_nodes) {
  261. GELOGD("Update first value of input shape by getnext sink nodes.");
  262. if (getnext_sink_nodes.size() != kNumOfGetnextNode) {
  263. REPORT_INNER_ERROR("E19999", "Not support dynamic dims when a graph with multi getnext nodes, graph:%s, "
  264. "num of getnext node:%zu, check invalid",
  265. graph->GetName().c_str(), getnext_sink_nodes.size());
  266. GELOGE(PARAM_INVALID, "[Check][Param] Not support dynamic dims when a graph with multi getnext nodes, graph:%s, "
  267. "num of getnext node:%zu", graph->GetName().c_str(), getnext_sink_nodes.size());
  268. return PARAM_INVALID;
  269. }
  270. auto input_node = getnext_sink_nodes.at(0);
  271. GE_CHECK_NOTNULL(input_node);
  272. auto op_desc = input_node->GetOpDesc();
  273. GE_CHECK_NOTNULL(op_desc);
  274. // user want getnext dynamic, just getnext or data+getnext_sink
  275. size_t data_count = input_node->GetAllOutDataAnchors().size() / kDivisionConst;
  276. if (data_count != GetLocalOmgContext().user_input_dims.size()) {
  277. REPORT_INNER_ERROR("E19999", "Output desc count of %s is %zu, should be equal to count of input shape:%zu, "
  278. "graph:%s, check invalid", op_desc->GetName().c_str(), data_count,
  279. GetLocalOmgContext().user_input_dims.size(), graph->GetName().c_str());
  280. GELOGE(PARAM_INVALID, "[Check][Param]Output desc count of %s is %zu, "
  281. "should be equal to count of input shape:%zu, graph:%s", op_desc->GetName().c_str(), data_count,
  282. GetLocalOmgContext().user_input_dims.size(), graph->GetName().c_str());
  283. return PARAM_INVALID;
  284. }
  285. for (size_t i = 0; i < data_count; ++i) {
  286. string data_name = op_desc->GetName() + "_" + std::to_string(i);
  287. GELOGD("Data just from getnext sink is %s.", data_name.c_str());
  288. GetLocalOmgContext().user_input_dims.at(i).first = data_name;
  289. }
  290. return SUCCESS;
  291. }
  292. // need to distinguish online and offline, offline no need to update the name of input_shape
  293. Status UpdateNameOfInputShape(ComputeGraphPtr &graph, const vector<NodePtr> &data_nodes,
  294. const vector<NodePtr> &getnext_nosink_nodes, const vector<NodePtr> &getnext_sink_nodes) {
  295. if (GetLocalOmgContext().dynamic_node_type.empty()) {
  296. GELOGI("No need to update first value of input shape when offline infer.");
  297. return SUCCESS;
  298. }
  299. if (GetLocalOmgContext().dynamic_node_type == DATA) {
  300. GELOGD("Users want data nodes to be dynamic.");
  301. if (UpdateNameOfData(graph, data_nodes) != SUCCESS) {
  302. GELOGE(PARAM_INVALID, "[Call][UpdateNameOfData] update first value of input shape of data nodes failed.");
  303. return PARAM_INVALID;
  304. }
  305. } else {
  306. GELOGD("Users want getnext nodes to be dynamic.");
  307. if (!getnext_nosink_nodes.empty()) {
  308. if (UpdateNameOfData(graph, getnext_nosink_nodes) != SUCCESS) {
  309. GELOGE(PARAM_INVALID,
  310. "[Call][UpdateNameOfData] update first value of input shape of getnext nosink nodes failed.");
  311. return PARAM_INVALID;
  312. }
  313. } else {
  314. if (UpdateNameOfGetnext(graph, getnext_sink_nodes) != SUCCESS) {
  315. GELOGE(PARAM_INVALID,
  316. "[Call][UpdateNameOfGetnext] update first value of input shape of getnext sink nodes failed.");
  317. return PARAM_INVALID;
  318. }
  319. }
  320. }
  321. return SUCCESS;
  322. }
  323. Status DeleteIdentityInsertByAdapter(ComputeGraphPtr &graph) {
  324. GELOGD("Start delete identity node inserted by adapter.");
  325. for (NodePtr &node : graph->GetDirectNode()) {
  326. GE_CHECK_NOTNULL(node);
  327. OpDescPtr op_desc = node->GetOpDesc();
  328. GE_CHECK_NOTNULL(op_desc);
  329. if (IsGetNextType(node)) {
  330. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  331. GE_IF_BOOL_EXEC(out_data_anchor == nullptr, continue);
  332. for (auto &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  333. GE_IF_BOOL_EXEC(peer_in_anchor == nullptr, continue);
  334. auto dst_node = peer_in_anchor->GetOwnerNode();
  335. GE_IF_BOOL_EXEC(dst_node == nullptr, continue);
  336. if (dst_node->GetType() == IDENTITY && dst_node->GetOutDataNodes().empty()) {
  337. GELOGI("Need to remove %s.", dst_node->GetName().c_str());
  338. if (GraphUtils::RemoveNodeWithoutRelink(graph, dst_node) != GRAPH_SUCCESS) {
  339. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) from graph:%s failed",
  340. dst_node->GetName().c_str(), dst_node->GetType().c_str(), graph->GetName().c_str());
  341. GELOGE(FAILED, "[Remove][Node] %s(%s) from graph:%s failed",
  342. dst_node->GetName().c_str(), dst_node->GetType().c_str(), graph->GetName().c_str());
  343. return FAILED;
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }
  350. return SUCCESS;
  351. }
  352. Status CheckNegativeCountOfOptions(const std::vector<std::vector<int64_t>> &shapes) {
  353. if (!GetLocalOmgContext().dynamic_dims.empty()) {
  354. size_t negative_count = 0;
  355. for (size_t i = 0; i < GetLocalOmgContext().user_input_dims.size(); ++i) {
  356. for (size_t j = 0; j < GetLocalOmgContext().user_input_dims.at(i).second.size(); ++j) {
  357. if (GetLocalOmgContext().user_input_dims.at(i).second.at(j) == kDynmaicDims) {
  358. negative_count++;
  359. }
  360. }
  361. }
  362. for (size_t i = 0; i < shapes.size(); ++i) {
  363. if (shapes.at(i).size() != negative_count) {
  364. REPORT_INNER_ERROR("E19999", "gear num of dynamic_dims is %zu should be equal to num:%zu from option, "
  365. "check invalid", shapes.at(i).size(), negative_count);
  366. GELOGE(PARAM_INVALID, "[Check][Param] gear num of dynamic_dims is %zu should be equal to num:%zu from option",
  367. shapes.at(i).size(), negative_count);
  368. return PARAM_INVALID;
  369. }
  370. }
  371. }
  372. return SUCCESS;
  373. }
  374. ///
  375. /// @ingroup ge
  376. /// @brief Init Dynamic Param from Options.
  377. /// @param [out] std::vector<std::vector<int64_t>> &shapes: Result for Params.
  378. /// @return true: Configed for Multi batch / false: Not configed for Multi batch.
  379. ///
  380. bool InitDynamicParams(vector<vector<int64_t>> &shapes) {
  381. if (!GetLocalOmgContext().dynamic_batch_size.empty()) {
  382. GELOGD("Found dynamic batch option, value %s", GetLocalOmgContext().dynamic_batch_size.c_str());
  383. std::vector<std::string> dims = ge::StringUtils::Split(GetLocalOmgContext().dynamic_batch_size, ',');
  384. for (const auto &dim : dims) {
  385. if (dim.empty()) {
  386. continue;
  387. }
  388. shapes.emplace_back(std::vector<int64_t>({std::strtol(dim.c_str(), nullptr, kDecimal)}));
  389. GELOGI("Found dynamic batch, shape %s", formats::JoinToString(*shapes.rbegin()).c_str());
  390. }
  391. }
  392. if (!GetLocalOmgContext().dynamic_image_size.empty()) {
  393. GELOGD("Found dynamic image size option, value %s", GetLocalOmgContext().dynamic_image_size.c_str());
  394. ParseDynamicSize(GetLocalOmgContext().dynamic_image_size, shapes);
  395. for (const auto &shape : shapes) {
  396. GELOGI("Found dynamic image size, shape %s", formats::JoinToString(shape).c_str());
  397. }
  398. }
  399. if (!GetLocalOmgContext().dynamic_dims.empty()) {
  400. GELOGD("Found dynamic dims option, value %s", GetLocalOmgContext().dynamic_dims.c_str());
  401. ParseDynamicSize(GetLocalOmgContext().dynamic_dims, shapes);
  402. for (const auto &shape : shapes) {
  403. GELOGI("Found dynamic dims, shape %s", formats::JoinToString(shape).c_str());
  404. }
  405. }
  406. return !shapes.empty();
  407. }
  408. ///
  409. /// @ingroup ge
  410. /// @brief parse each data's own dynamic dims.
  411. /// @param [out] map<string, vector<vector<int64_t>>> &data_to_dynamic_info: key:data_name. value:dynamic dims.
  412. /// @return true: Configed for Multi batch / false: Not configed for Multi batch.
  413. ///
  414. Status ParserDataToDynamicInfo(const vector<vector<int64_t>> &shapes,
  415. vector<pair<string, vector<int64_t>>> &data_name_and_shape,
  416. map<string, vector<vector<int64_t>> > &data_to_dynamic_info) {
  417. size_t cur_data_index = 0;
  418. for (size_t index = 0; index < data_name_and_shape.size(); ++index) {
  419. auto &cur_item = data_name_and_shape[index];
  420. auto &data_name = cur_item.first;
  421. auto &data_shape = cur_item.second;
  422. auto dynamic_dims_num = std::count_if(data_shape.begin(), data_shape.end(),
  423. [&data_shape](int64_t dim){ return dim < 0; });
  424. GELOGI("Train_Dynamic dynamic_dims_num of %s is %zu", data_name.c_str(), dynamic_dims_num);
  425. vector<vector<int64_t> > dynamic_info;
  426. for (auto &dynamic_gear_info : shapes) {
  427. GELOGI("Train_Dynamic dynamic_gear_info is %s", formats::JoinToString(dynamic_gear_info).c_str());
  428. vector<int64_t> one_gear;
  429. if (dynamic_gear_info.size() == static_cast<size_t>(dynamic_dims_num)) {
  430. one_gear = dynamic_gear_info;
  431. } else if (dynamic_gear_info.size() > static_cast<size_t>(dynamic_dims_num)) {
  432. auto tmp_index = cur_data_index;
  433. for (size_t i = 0; i < static_cast<size_t>(dynamic_dims_num); ++i) {
  434. if (tmp_index >= dynamic_gear_info.size()) {
  435. ErrorManager::GetInstance().ATCReportErrMessage(
  436. "E10045", {"name", "shape"}, {data_name, formats::JoinToString(data_shape)});
  437. GELOGE(PARAM_INVALID, "[Check][Param] Data:%s shape:%s make dynamic dims overflow", data_name.c_str(),
  438. formats::JoinToString(data_shape).c_str());
  439. return FAILED;
  440. }
  441. one_gear.push_back(dynamic_gear_info[tmp_index++]);
  442. }
  443. } else {
  444. ErrorManager::GetInstance().ATCReportErrMessage(
  445. "E10046", {"name", "shape"}, {data_name, formats::JoinToString(data_shape)});
  446. GELOGE(PARAM_INVALID, "[Check][Param] Dynamic dims num of data: %s shape: %s "
  447. "can not be more than one gear dynamic info size",
  448. data_name.c_str(), formats::JoinToString(data_shape).c_str());
  449. return FAILED;
  450. }
  451. GELOGI("Train_Dynamic one_gear is %s.", formats::JoinToString(one_gear).c_str());
  452. dynamic_info.push_back(one_gear);
  453. }
  454. cur_data_index += dynamic_dims_num;
  455. data_to_dynamic_info[data_name] = dynamic_info;
  456. }
  457. return SUCCESS;
  458. }
  459. ///
  460. /// @ingroup ge
  461. /// @brief Check Dynamic Param is invalid.
  462. /// @param [in] const vector<vector<int64_t>> &shapes: Params for check.
  463. /// @return SUCCESS: valid / PARAM_INVALID: invalid.
  464. ///
  465. Status CheckDynamicParams(const vector<vector<int64_t>> &shapes) {
  466. if (shapes.size() < kMinShapesCount) {
  467. ErrorManager::GetInstance().ATCReportErrMessage(
  468. "E10035", {"shapesize", "minshapesize"}, {std::to_string(shapes.size()), std::to_string(kMinShapesCount - 1)});
  469. GELOGE(PARAM_INVALID,
  470. "[Check][Param] Input parameter[--dynamic_batch_size, --dynamic_image_size or --dynamic_dims]'s "
  471. "value size [%zu] must be greater than [%d].",
  472. shapes.size(), kMinShapesCount - 1);
  473. return PARAM_INVALID;
  474. }
  475. if (shapes.size() > kMaxShapesCount) {
  476. ErrorManager::GetInstance().ATCReportErrMessage(
  477. "E10036", {"shapesize", "maxshapesize"}, {std::to_string(shapes.size()), std::to_string(kMaxShapesCount + 1)});
  478. GELOGE(PARAM_INVALID,
  479. "[Check][Param] Input parameter[--dynamic_batch_size, --dynamic_image_size or --dynamic_dims]'s "
  480. "value size [%zu] must be less than [%d].",
  481. shapes.size(), kMaxShapesCount + 1);
  482. return PARAM_INVALID;
  483. }
  484. std::set<std::vector<int64_t>> shapes_set;
  485. size_t shape_size = shapes.at(0).size();
  486. for (auto &shape : shapes) {
  487. if (shape_size != shape.size()) {
  488. ErrorManager::GetInstance().ATCReportErrMessage("E10037", {"shapesize1", "shapesize2"},
  489. {std::to_string(shape_size), std::to_string(shape.size())});
  490. GELOGE(PARAM_INVALID,
  491. "[Check][Param] Input parameter[--dynamic_batch_size, --dynamic_image_size or --dynamic_dims]'s "
  492. "value size must be same, first group's size is %zu and another's is %zu.",
  493. shape_size, shape.size());
  494. return PARAM_INVALID;
  495. }
  496. for (auto dim : shape) {
  497. if (dim <= 0) {
  498. ErrorManager::GetInstance().ATCReportErrMessage("E10038", {"dim"}, {std::to_string(dim)});
  499. GELOGE(PARAM_INVALID, "[Check][Param] Invalid dim %ld, all dims must be greater than 0", dim);
  500. return PARAM_INVALID;
  501. }
  502. }
  503. shapes_set.insert(shape);
  504. }
  505. if (shapes_set.size() != shapes.size()) {
  506. ErrorManager::GetInstance().ATCReportErrMessage("E10039");
  507. GELOGE(PARAM_INVALID, "[Check][Param] Input parameter[--dynamic_batch_size, "
  508. "--dynamic_image_size or --dynamic_dims] exist duplicate shapes.");
  509. return PARAM_INVALID;
  510. }
  511. return SUCCESS;
  512. }
  513. ///
  514. /// @ingroup ge
  515. /// @brief Get GeShape from configed shape.
  516. /// @param [in] const std::vector<int64_t> &batch_shape: Configed shape.
  517. /// @param [out] GeShape &data_shape: GeShape for configed shape.
  518. /// @return SUCCESS / PARAM_INVALID
  519. ///
  520. Status CalcShape(const std::vector<int64_t> &batch_shape, GeShape &data_shape) {
  521. size_t batch_shape_index = 0;
  522. for (size_t i = 0; i < data_shape.GetDimNum(); ++i) {
  523. if (data_shape.GetDim(i) < 0) {
  524. if (batch_shape_index >= batch_shape.size()) {
  525. REPORT_INNER_ERROR("E19999", "the batch shape count %zu, does not match the data shape %s",
  526. batch_shape.size(), data_shape.ToString().c_str());
  527. GELOGE(PARAM_INVALID, "[Check][Param] Failed to calc tensor shape, the batch shape count %zu, "
  528. "does not match the data shape %s", batch_shape.size(), data_shape.ToString().c_str());
  529. return PARAM_INVALID;
  530. }
  531. data_shape.SetDim(i, batch_shape[batch_shape_index++]);
  532. }
  533. }
  534. GELOGI("CalcShape size of batch_shape is %zu, batch_shape_index is %zu.", batch_shape.size(), batch_shape_index);
  535. if (batch_shape_index != batch_shape.size()) {
  536. REPORT_INNER_ERROR("E19999", "the batch shape count %zu, does not match the data shape %s",
  537. batch_shape.size(), data_shape.ToString().c_str());
  538. GELOGE(PARAM_INVALID, "[Check][Param] Failed to calc tensor shape, the batch shape count %zu, "
  539. "does not match the data shape %s", batch_shape.size(), data_shape.ToString().c_str());
  540. return PARAM_INVALID;
  541. }
  542. return SUCCESS;
  543. }
  544. ///
  545. /// @ingroup ge
  546. /// @brief Set mbatch_dynamic_type on node.
  547. /// @param [in] const OpDescPtr &op_desc: Node for set attribute.
  548. /// @return 0: SUCCESS / others: INTERNAL_ERROR
  549. ///
  550. Status StampDynamicType(const OpDescPtr &op_desc) {
  551. GE_CHECK_NOTNULL(op_desc);
  552. int32_t dynamic_type = static_cast<int32_t>(FIXED);
  553. if (!GetLocalOmgContext().dynamic_batch_size.empty()) {
  554. dynamic_type = static_cast<int32_t>(DYNAMIC_BATCH);
  555. }
  556. if (!GetLocalOmgContext().dynamic_image_size.empty()) {
  557. dynamic_type = static_cast<int32_t>(DYNAMIC_IMAGE);
  558. }
  559. if (!GetLocalOmgContext().dynamic_dims.empty()) {
  560. dynamic_type = static_cast<int32_t>(DYNAMIC_DIMS);
  561. }
  562. if (!AttrUtils::SetInt(op_desc, ATTR_DYNAMIC_TYPE, dynamic_type)) {
  563. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  564. ATTR_DYNAMIC_TYPE.c_str(), op_desc->GetName().c_str(), op_desc->GetType().c_str());
  565. GELOGE(INTERNAL_ERROR, "[Set][Attr] %s to node:%s(%s) failed",
  566. ATTR_DYNAMIC_TYPE.c_str(), op_desc->GetName().c_str(), op_desc->GetType().c_str());
  567. return INTERNAL_ERROR;
  568. }
  569. return SUCCESS;
  570. }
  571. ///
  572. /// @ingroup ge
  573. /// @brief Check dynamic batch Shape.
  574. /// @param [in] const vector<int64_t> &shape: data_shape to be checked.
  575. /// @param [in] const string &data_name: cur data name.
  576. /// @return 0: true/false
  577. ///
  578. bool CheckDynamicBatchShape(const vector<int64_t> &shape, const string &data_name) {
  579. if (shape[0] == kDynmaicDims) {
  580. for (size_t i = 1; i < shape.size(); ++i) {
  581. if (shape[i] < 1) {
  582. ErrorManager::GetInstance().ATCReportErrMessage("E10018", {"index", "shape"},
  583. {std::to_string(i), std::to_string(shape[i])});
  584. GELOGE(ge::PARAM_INVALID, "[Check][Param] Only batch N can be -1 when set --dynamic_batch_size, "
  585. "current data: %s shape[%zu] is %ld", data_name.c_str(), i, shape[i]);
  586. return false;
  587. }
  588. }
  589. return true;
  590. } else {
  591. return false;
  592. }
  593. }
  594. ///
  595. /// @ingroup ge
  596. /// @brief Check Dynamic image size shape.
  597. /// @param [in] unordered_map<string, vector<int64_t>> &shape_map: map of data_name and data_shape.
  598. /// @param [in] const std::string &input_format: format of input.
  599. /// @return 0: true/false
  600. ///
  601. bool CheckDynamicImageSizeShape(const vector<int64_t> &shape, const string &data_name,
  602. const std::string &input_format) {
  603. int64_t height = 0;
  604. int64_t width = 0;
  605. if (input_format == "NCHW") {
  606. height = shape[NCHW_DIM_H];
  607. width = shape[NCHW_DIM_W];
  608. }
  609. if (input_format == "NHWC") {
  610. height = shape[NHWC_DIM_H];
  611. width = shape[NHWC_DIM_W];
  612. }
  613. if (height == kDynmaicDims && width == kDynmaicDims &&
  614. std::count(shape.begin(), shape.end(), kDynmaicDims) == kDynamicImgSizeDynamciDimsNum) {
  615. return true;
  616. } else {
  617. ErrorManager::GetInstance().ATCReportErrMessage("E10019");
  618. GELOGE(ge::PARAM_INVALID, "[Check][Param] --input_shape's shape is invalid, only height and width can be -1 "
  619. "when set --dynamic_image_size.");
  620. return false;
  621. }
  622. }
  623. } // namespace multibatch
  624. } // namespace ge

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