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

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