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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * Copyright 2019-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. namespace ge {
  28. namespace multibatch {
  29. constexpr int kDecimal = 10;
  30. constexpr uint8_t kMaxShapesCount = 100;
  31. constexpr uint8_t kMinShapesCount = 2;
  32. void ParseDynamicSize(string dynamic_size, vector<vector<int64_t>> &shapes) {
  33. std::vector<std::string> shape_strs = ge::StringUtils::Split(dynamic_size, ';');
  34. for (const auto &shape_str : shape_strs) {
  35. if (shape_str.empty()) {
  36. continue;
  37. }
  38. std::vector<int64_t> shape;
  39. std::vector<std::string> dims = ge::StringUtils::Split(shape_str, ',');
  40. for (const auto &dim : dims) {
  41. if (dim.empty()) {
  42. continue;
  43. }
  44. shape.emplace_back(std::strtol(dim.c_str(), nullptr, kDecimal));
  45. }
  46. if (!shape.empty()) {
  47. shapes.emplace_back(shape);
  48. }
  49. }
  50. }
  51. ///
  52. /// @ingroup ge
  53. /// @brief Init Dynamic Param from Options.
  54. /// @param [out] std::vector<std::vector<int64_t>> &shapes: Result for Params.
  55. /// @return true: Configed for Multi batch / false: Not configed for Multi batch.
  56. ///
  57. bool InitDynamicParams(vector<vector<int64_t>> &shapes) {
  58. if (!GetLocalOmgContext().dynamic_batch_size.empty()) {
  59. GELOGD("Found dynamic batch option, value %s", GetLocalOmgContext().dynamic_batch_size.c_str());
  60. std::vector<std::string> dims = ge::StringUtils::Split(GetLocalOmgContext().dynamic_batch_size, ',');
  61. for (const auto &dim : dims) {
  62. if (dim.empty()) {
  63. continue;
  64. }
  65. shapes.emplace_back(std::vector<int64_t>({std::strtol(dim.c_str(), nullptr, kDecimal)}));
  66. GELOGI("Found dynamic batch, shape %s", formats::JoinToString(*shapes.rbegin()).c_str());
  67. }
  68. }
  69. if (!GetLocalOmgContext().dynamic_image_size.empty()) {
  70. GELOGD("Found dynamic image size option, value %s", GetLocalOmgContext().dynamic_image_size.c_str());
  71. ParseDynamicSize(GetLocalOmgContext().dynamic_image_size, shapes);
  72. for (const auto &shape : shapes) {
  73. GELOGI("Found dynamic image size, shape %s", formats::JoinToString(shape).c_str());
  74. }
  75. }
  76. if (!GetLocalOmgContext().dynamic_dims.empty()) {
  77. GELOGD("Found dynamic dims option, value %s", GetLocalOmgContext().dynamic_dims.c_str());
  78. ParseDynamicSize(GetLocalOmgContext().dynamic_dims, shapes);
  79. for (const auto &shape : shapes) {
  80. GELOGI("Found dynamic dims, shape %s", formats::JoinToString(shape).c_str());
  81. }
  82. }
  83. return !shapes.empty();
  84. }
  85. ///
  86. /// @ingroup ge
  87. /// @brief parse each data's own dynamic dims.
  88. /// @param [out] map<string, vector<vector<int64_t>>> &data_to_dynamic_info: key:data_name. value:dynamic dims.
  89. /// @return true: Configed for Multi batch / false: Not configed for Multi batch.
  90. ///
  91. Status ParserDataToDynmaicInfo(const vector<vector<int64_t>> &shapes,
  92. vector<pair<string, vector<int64_t>>> &data_name_and_shape,
  93. map<string, vector<vector<int64_t>>> &data_to_dynamic_info) {
  94. size_t cur_data_index = 0;
  95. for (size_t index = 0; index < data_name_and_shape.size(); ++index) {
  96. auto &cur_item = data_name_and_shape[index];
  97. auto &data_name = cur_item.first;
  98. auto &data_shape = cur_item.second;
  99. auto dynamic_dims_num =
  100. std::count_if(data_shape.begin(), data_shape.end(), [&data_shape](int64_t dim) { return dim < 0; });
  101. vector<vector<int64_t>> dynamic_info;
  102. for (auto &dynamic_gear_info : shapes) {
  103. vector<int64_t> one_gear;
  104. if (dynamic_gear_info.size() == static_cast<size_t>(dynamic_dims_num)) {
  105. one_gear = dynamic_gear_info;
  106. } else if (dynamic_gear_info.size() > static_cast<size_t>(dynamic_dims_num)) {
  107. auto tmp_index = cur_data_index;
  108. for (size_t i = 0; i < static_cast<size_t>(dynamic_dims_num); ++i) {
  109. if (tmp_index >= dynamic_gear_info.size()) {
  110. GELOGE(PARAM_INVALID, "Data: %s shape: %s make dynamic dims overflow", data_name.c_str(),
  111. formats::JoinToString(data_shape).c_str());
  112. return FAILED;
  113. }
  114. one_gear.push_back(dynamic_gear_info[tmp_index++]);
  115. }
  116. } else {
  117. GELOGE(PARAM_INVALID, "Dynamic dims num of data: %s shape: %s can not be more than one gear dynamic info size",
  118. data_name.c_str(), formats::JoinToString(data_shape).c_str());
  119. return FAILED;
  120. }
  121. dynamic_info.push_back(one_gear);
  122. }
  123. cur_data_index += dynamic_dims_num;
  124. data_to_dynamic_info[data_name] = dynamic_info;
  125. }
  126. return SUCCESS;
  127. }
  128. ///
  129. /// @ingroup ge
  130. /// @brief Check Dynamic Param is invalid.
  131. /// @param [in] const vector<vector<int64_t>> &shapes: Params for check.
  132. /// @return SUCCESS: valid / PARAM_INVALID: invalid.
  133. ///
  134. Status CheckDynamicParams(const vector<vector<int64_t>> &shapes) {
  135. if (shapes.size() < kMinShapesCount) {
  136. ErrorManager::GetInstance().ATCReportErrMessage(
  137. "E10035", {"shapesize", "minshapesize"}, {std::to_string(shapes.size()), std::to_string(kMinShapesCount - 1)});
  138. GELOGE(PARAM_INVALID,
  139. "Input parameter[--dynamic_batch_size, --dynamic_image_size or --dynamic_dims]'s "
  140. "value size [%zu] must be greater than [%zu].",
  141. shapes.size(), kMinShapesCount - 1);
  142. return PARAM_INVALID;
  143. }
  144. if (shapes.size() > kMaxShapesCount) {
  145. ErrorManager::GetInstance().ATCReportErrMessage(
  146. "E10036", {"shapesize", "maxshapesize"}, {std::to_string(shapes.size()), std::to_string(kMaxShapesCount + 1)});
  147. GELOGE(PARAM_INVALID,
  148. "Input parameter[--dynamic_batch_size, --dynamic_image_size or --dynamic_dims]'s "
  149. "value size [%zu] must be less than [%zu].",
  150. shapes.size(), kMaxShapesCount + 1);
  151. return PARAM_INVALID;
  152. }
  153. std::set<std::vector<int64_t>> shapes_set;
  154. size_t shape_size = shapes.at(0).size();
  155. for (auto &shape : shapes) {
  156. if (shape_size != shape.size()) {
  157. ErrorManager::GetInstance().ATCReportErrMessage("E10037", {"shapesize1", "shapesize2"},
  158. {std::to_string(shape_size), std::to_string(shape.size())});
  159. GELOGE(PARAM_INVALID,
  160. "Input parameter[--dynamic_batch_size, --dynamic_image_size or --dynamic_dims]'s "
  161. "value size must be same, first group's size is %zu and another's is %zu.",
  162. shape_size, shape.size());
  163. return PARAM_INVALID;
  164. }
  165. for (auto dim : shape) {
  166. if (dim <= 0) {
  167. ErrorManager::GetInstance().ATCReportErrMessage("E10038", {"dim"}, {std::to_string(dim)});
  168. GELOGE(PARAM_INVALID, "Invalid dim %ld, all dims must be greater than 0", dim);
  169. return PARAM_INVALID;
  170. }
  171. }
  172. shapes_set.insert(shape);
  173. }
  174. if (shapes_set.size() != shapes.size()) {
  175. ErrorManager::GetInstance().ATCReportErrMessage("E10039");
  176. GELOGE(PARAM_INVALID,
  177. "Input parameter[--dynamic_batch_size, --dynamic_image_size or --dynamic_dims] exist duplicate shapes.");
  178. return PARAM_INVALID;
  179. }
  180. return SUCCESS;
  181. }
  182. ///
  183. /// @ingroup ge
  184. /// @brief Get GeShape from configed shape.
  185. /// @param [in] const std::vector<int64_t> &batch_shape: Configed shape.
  186. /// @param [out] GeShape &data_shape: GeShape for configed shape.
  187. /// @return SUCCESS / PARAM_INVALID
  188. ///
  189. Status CalcShape(const std::vector<int64_t> &batch_shape, GeShape &data_shape) {
  190. size_t batch_shape_index = 0;
  191. for (size_t i = 0; i < data_shape.GetDimNum(); ++i) {
  192. if (data_shape.GetDim(i) < 0) {
  193. if (batch_shape_index >= batch_shape.size()) {
  194. ErrorManager::GetInstance().ATCReportErrMessage(
  195. "E19012", {"function", "reason"},
  196. {"CalcShape", "the batch shape count " + std::to_string(batch_shape.size()) +
  197. " does not match the data shape " + data_shape.ToString()});
  198. GELOGE(PARAM_INVALID,
  199. "Failed to calc tensor shape, the batch shape count %zu, does not match the data shape %s",
  200. batch_shape.size(), data_shape.ToString().c_str());
  201. return PARAM_INVALID;
  202. }
  203. data_shape.SetDim(i, batch_shape[batch_shape_index++]);
  204. }
  205. }
  206. if (batch_shape_index != batch_shape.size()) {
  207. ErrorManager::GetInstance().ATCReportErrMessage(
  208. "E19012", {"function", "reason"},
  209. {"CalcShape", "the batch shape count " + std::to_string(batch_shape.size()) + " does not match the data shape " +
  210. data_shape.ToString()});
  211. GELOGE(PARAM_INVALID, "Failed to calc tensor shape, the batch shape count %zu, does not match the data shape %s",
  212. batch_shape.size(), data_shape.ToString().c_str());
  213. return PARAM_INVALID;
  214. }
  215. return SUCCESS;
  216. }
  217. ///
  218. /// @ingroup ge
  219. /// @brief Set mbatch_dynamic_type on node.
  220. /// @param [in] const OpDescPtr &op_desc: Node for set attribute.
  221. /// @return 0: SUCCESS / others: INTERNAL_ERROR
  222. ///
  223. Status StampDynamicType(const OpDescPtr &op_desc) {
  224. GE_CHECK_NOTNULL(op_desc);
  225. int32_t dynamic_type = static_cast<int32_t>(FIXED);
  226. if (!GetLocalOmgContext().dynamic_batch_size.empty()) {
  227. dynamic_type = static_cast<int32_t>(DYNAMIC_BATCH);
  228. }
  229. if (!GetLocalOmgContext().dynamic_image_size.empty()) {
  230. dynamic_type = static_cast<int32_t>(DYNAMIC_IMAGE);
  231. }
  232. if (!GetLocalOmgContext().dynamic_dims.empty()) {
  233. dynamic_type = static_cast<int32_t>(DYNAMIC_DIMS);
  234. }
  235. if (!AttrUtils::SetInt(op_desc, ATTR_DYNAMIC_TYPE, dynamic_type)) {
  236. GELOGE(INTERNAL_ERROR, "Failed to add dynamic type attr for node %s", op_desc->GetName().c_str());
  237. return INTERNAL_ERROR;
  238. }
  239. return SUCCESS;
  240. }
  241. } // namespace multibatch
  242. } // namespace ge

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