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

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