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.

infer_value_range_pass.cc 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "graph/passes/infer_value_range_pass.h"
  17. #include "common/formats/utils/formats_trans_utils.h"
  18. #include "common/util/error_manager/error_manager.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "graph/debug/ge_attr_define.h"
  21. #include "graph/operator_factory_impl.h"
  22. #include "graph/passes/constant_folding_pass.h"
  23. #include "graph/utils/type_utils.h"
  24. #include "common/ge/ge_util.h"
  25. using std::unique_ptr;
  26. namespace ge {
  27. namespace {
  28. #define GET_DATA_BY_DTYPE(DTYPE, TYPE) \
  29. case (DTYPE): \
  30. ConstructValueRange<TYPE>(lower_boundary_tensor, upper_boundary_tensor, output_tensor_value_range); \
  31. break;
  32. void SerialShapeRange(const GeTensorDescPtr &desc, std::string &desc_str) {
  33. std::vector<std::pair<int64_t, int64_t>> shape_range;
  34. (void)desc->GetShapeRange(shape_range);
  35. desc_str += formats::RangeToString(shape_range);
  36. shape_range.clear();
  37. (void)desc->GetOriginShapeRange(shape_range);
  38. desc_str += ",";
  39. desc_str += formats::RangeToString(shape_range);
  40. shape_range.clear();
  41. }
  42. Status RunCpuKernelForValueRange(NodePtr &node, const vector<ConstGeTensorPtr> &inputs,
  43. std::vector<GeTensorPtr> &outputs) {
  44. // RunOpKernelWithCheck, RunOpKernel for test
  45. auto ret = ConstantFoldingPass::RunOpKernel(node, inputs, outputs);
  46. if (ret != SUCCESS) {
  47. auto op_kernel = folding_pass::GetKernelByType(node);
  48. if (op_kernel == nullptr) {
  49. GELOGW("Calculate value range failed, no op kernel for node %s type %s", node->GetName().c_str(),
  50. node->GetType().c_str());
  51. return NOT_CHANGED;
  52. }
  53. ret = op_kernel->Compute(node->GetOpDesc(), inputs, outputs);
  54. if (ret != SUCCESS) {
  55. GELOGW("Calculate value range failed, node %s run cpu kernel failed.", node->GetName().c_str());
  56. return NOT_CHANGED;
  57. }
  58. }
  59. GELOGI("Node %s type %s, run cpu kernel success.", node->GetName().c_str(), node->GetType().c_str());
  60. return SUCCESS;
  61. }
  62. } // namespace
  63. graphStatus InferValueRangePass::Infer(NodePtr &node) {
  64. auto infer_value_range_param = OperatorFactoryImpl::GetInferValueRangePara(node->GetType());
  65. // Use registered func to calculate value range
  66. if (!infer_value_range_param.use_cpu_kernel) {
  67. if (infer_value_range_param.infer_value_func == nullptr) {
  68. GELOGW("The registered func of node %s to infer value range is nullptr.", node->GetName().c_str());
  69. return GRAPH_NOT_CHANGED;
  70. }
  71. Operator op = OpDescUtils::CreateOperatorFromNode(node);
  72. auto ret = node->GetOpDesc()->CallInferValueRangeFunc(op);
  73. if (ret != GRAPH_SUCCESS) {
  74. GELOGW("Node %s call infer value range func failed, ret: %u.", node->GetName().c_str(), ret);
  75. return GRAPH_NOT_CHANGED;
  76. }
  77. GELOGD("Node %s infer value range func succeed by registered func.", node->GetName().c_str());
  78. return GRAPH_SUCCESS;
  79. }
  80. // if input value range has -1, cpu kernel cannot calculate correctly, so set {1:-1}
  81. if (InputHasUnknownValueRange(node)) {
  82. GELOGI("Node %s has unknown value range in input tensors, set value range {1:-1}, and skip cpu kernel.",
  83. node->GetName().c_str());
  84. return GenerateWorstValueRange(node);
  85. }
  86. // Use CPU kernel func to calculate value range
  87. auto ret = ConstructInputAndInferValueRange(node);
  88. if (ret != GRAPH_SUCCESS) {
  89. GELOGW("Use CPU kernel to calculate value range failed. node: %s, ret: %u", node->GetName().c_str(), ret);
  90. return GRAPH_NOT_CHANGED;
  91. }
  92. GELOGD("Node %s infer value range func succeed by running cpu kernel.", node->GetName().c_str());
  93. return GRAPH_SUCCESS;
  94. }
  95. std::string InferValueRangePass::SerialTensorInfo(const GeTensorDescPtr &tensor_desc) const {
  96. std::stringstream ss;
  97. ss << "[";
  98. ss << "(shape:[" << tensor_desc->MutableShape().ToString() << "]),";
  99. string range_str;
  100. SerialShapeRange(tensor_desc, range_str);
  101. ss << "(shape_range:" << range_str << "),";
  102. std::vector<std::pair<int64_t, int64_t>> value_range;
  103. (void)tensor_desc->GetValueRange(value_range);
  104. string value_range_str = formats::RangeToString(value_range);
  105. ss << "(value_range:" << value_range_str << ")]";
  106. return ss.str();
  107. }
  108. bool InferValueRangePass::NeedInfer(const NodePtr &node) const {
  109. auto infer_value_range_param = OperatorFactoryImpl::GetInferValueRangePara(node->GetType());
  110. if (!infer_value_range_param.is_initialized) {
  111. GELOGD("Node %s does not register func to infer value range, skip infer_value_range_pass.",
  112. node->GetName().c_str());
  113. return false;
  114. }
  115. if (infer_value_range_param.when_call == INPUT_IS_DYNAMIC) {
  116. // Only do infer for node that all inputs are dynamic, such as shape
  117. if (InputIsDynamic(node)) {
  118. return true;
  119. }
  120. GELOGD("Node %s register func to infer value range and when_call is INPUT_IS_DYNAMIC, but check input failed.",
  121. node->GetName().c_str());
  122. } else if (infer_value_range_param.when_call == INPUT_HAS_VALUE_RANGE) {
  123. // Only do infer for node that all inputs have value_range or node type of inputs is constant/const
  124. if (InputIsConstOrHasValueRange(node)) {
  125. return true;
  126. }
  127. GELOGD("Node %s register func to infer value range and when_call is INPUT_HAS_VALUE_RANGE, but check input failed.",
  128. node->GetName().c_str());
  129. }
  130. GELOGD("Node %s does not need to infer value range, skip infer_value_range_pass.", node->GetName().c_str());
  131. return false;
  132. }
  133. bool InferValueRangePass::InputIsDynamic(const NodePtr &node) const{
  134. bool input_is_dynamic = false;
  135. auto cur_op_desc = node->GetOpDesc();
  136. for (const auto &input_desc : cur_op_desc->GetAllInputsDescPtr()) {
  137. auto dims = input_desc->GetShape().GetDims();
  138. for (auto dim : dims) {
  139. if (dim == UNKNOWN_DIM || dim == UNKNOWN_DIM_NUM) {
  140. input_is_dynamic = true;
  141. break;
  142. }
  143. }
  144. }
  145. return input_is_dynamic;
  146. }
  147. bool InferValueRangePass::InputIsConstOrHasValueRange(const NodePtr &node) const {
  148. bool input_is_const_or_has_value_range = true;
  149. auto cur_op_desc = node->GetOpDesc();
  150. auto in_data_anchors = node->GetAllInDataAnchors();
  151. for (size_t i = 0; i < in_data_anchors.size(); ++i) {
  152. auto peer_out_anchor = in_data_anchors.at(i)->GetPeerOutAnchor();
  153. if (peer_out_anchor == nullptr) {
  154. continue;
  155. }
  156. auto peer_node = peer_out_anchor->GetOwnerNode();
  157. if (peer_node == nullptr || peer_node->GetOpDesc() == nullptr) {
  158. continue;
  159. }
  160. if ((peer_node->GetType() == CONSTANT) || (peer_node->GetType() == CONSTANTOP)) {
  161. continue;
  162. }
  163. const auto &input_desc = cur_op_desc->GetInputDesc(i);
  164. std::vector<std::pair<int64_t, int64_t>> value_range;
  165. (void)input_desc.GetValueRange(value_range);
  166. if (value_range.empty()) {
  167. GELOGD("Node %s input %zu does not have value range, skip infer_value_range_pass for current node.",
  168. node->GetName().c_str(), i);
  169. input_is_const_or_has_value_range = false;
  170. break;
  171. }
  172. }
  173. return input_is_const_or_has_value_range;
  174. }
  175. bool InferValueRangePass::InputHasUnknownValueRange(const NodePtr &node) const {
  176. bool has_unknown_value_range = false;
  177. auto cur_op_desc = node->GetOpDesc();
  178. for (const auto &input_desc : cur_op_desc->GetAllInputsDescPtr()) {
  179. std::vector<std::pair<int64_t, int64_t>> input_desc_value_range;
  180. input_desc->GetValueRange(input_desc_value_range);
  181. if (!input_desc_value_range.empty()) {
  182. for (const auto &range : input_desc_value_range) {
  183. if (range.first == -1 || range.second == -1) {
  184. GELOGD("Node %s input tensors have unknown value range, value range is %s.", node->GetName().c_str(),
  185. formats::RangeToString(input_desc_value_range).c_str());
  186. has_unknown_value_range = true;
  187. }
  188. }
  189. }
  190. }
  191. return has_unknown_value_range;
  192. }
  193. graphStatus InferValueRangePass::UpdateTensorDesc(const GeTensorDescPtr &src, GeTensorDescPtr &dst, bool &changed) {
  194. if (src == nullptr || dst == nullptr) {
  195. REPORT_CALL_ERROR("E19999", "While updating tensor desc, input desc is null.");
  196. GELOGE(GRAPH_FAILED, "[Param][check] While updating tensor desc, input desc is null.");
  197. return GRAPH_FAILED;
  198. }
  199. changed = false;
  200. std::vector<std::pair<int64_t, int64_t>> src_value_range;
  201. std::vector<std::pair<int64_t, int64_t>> dst_value_range;
  202. (void)src->GetValueRange(src_value_range);
  203. (void)dst->GetValueRange(dst_value_range);
  204. if (src_value_range != dst_value_range) {
  205. GELOGD("While updating tensor desc, value range has been changed, src value range: %s, dst value range: %s.",
  206. formats::RangeToString(src_value_range).c_str(), formats::RangeToString(dst_value_range).c_str());
  207. changed = true;
  208. }
  209. dst->SetValueRange(src_value_range);
  210. return GRAPH_SUCCESS;
  211. }
  212. graphStatus InferValueRangePass::UpdateOutputFromSubgraphs(const std::vector<GeTensorDescPtr> &src,
  213. GeTensorDescPtr &dst) {
  214. std::vector<std::pair<int64_t, int64_t>> ref_out_tensor_value_range;
  215. auto ref_out_tensor = src.at(0);
  216. (void)ref_out_tensor->GetValueRange(ref_out_tensor_value_range);
  217. for (auto &ref_tensor : src) {
  218. std::vector<std::pair<int64_t, int64_t>> ref_tensor_value_range;
  219. (void)ref_tensor->GetValueRange(ref_tensor_value_range);
  220. if (ref_tensor_value_range.size() != ref_out_tensor_value_range.size()) {
  221. GELOGD("Update TensorDesc %s failed, rank of value ranges %s and %s are not the same, skip value range refresh.",
  222. dst->GetName().c_str(), formats::RangeToString(ref_out_tensor_value_range).c_str(),
  223. formats::RangeToString(ref_tensor_value_range).c_str());
  224. return GRAPH_SUCCESS;
  225. }
  226. for (size_t j = 0; j < ref_out_tensor_value_range.size(); j++) {
  227. if ((ref_out_tensor_value_range.at(j).first != ref_tensor_value_range.at(j).first) ||
  228. (ref_out_tensor_value_range.at(j).second != ref_tensor_value_range.at(j).second)) {
  229. ref_out_tensor_value_range[j] = std::make_pair(1, -1);
  230. }
  231. }
  232. }
  233. GELOGD("While updating output desc from subgraphs, set parent node desc value range %s.",
  234. formats::RangeToString(ref_out_tensor_value_range).c_str());
  235. dst->SetValueRange(ref_out_tensor_value_range);
  236. return GRAPH_SUCCESS;
  237. }
  238. graphStatus InferValueRangePass::UpdateOutputFromSubgraphsForMultiDims(const std::vector<GeTensorDescPtr> &src,
  239. GeTensorDescPtr &dst) {
  240. REPORT_INNER_ERROR("E19999",
  241. "Update TensorDesc %s failed. In dynamic multi-dims size scene, there should be no value range.",
  242. dst->GetName().c_str());
  243. GELOGE(GRAPH_FAILED,
  244. "[Update][TensorDesc] %s failed. In dynamic multi-dims size scene, there should be no value range.",
  245. dst->GetName().c_str());
  246. return GRAPH_FAILED;
  247. }
  248. graphStatus InferValueRangePass::GenerateWorstValueRange(NodePtr &node) {
  249. GELOGI("Node %s does not run cpu kernel, because input value range has -1.", node->GetName().c_str());
  250. OpDescPtr op_desc = node->GetOpDesc();
  251. for (size_t i = 0; i < op_desc->GetOutputsSize(); ++i) {
  252. auto output_desc = op_desc->MutableOutputDesc(i);
  253. if (output_desc == nullptr) {
  254. continue;
  255. }
  256. auto output_i_shape = output_desc->GetShape();
  257. auto output_i_shape_size = output_i_shape.GetShapeSize();
  258. if (output_i_shape_size < 0) {
  259. GELOGD("Node %s output shape is unknown, cannot infer value range, shape is %s.", node->GetName().c_str(),
  260. formats::ShapeToString(output_i_shape).c_str());
  261. return GRAPH_NOT_CHANGED;
  262. }
  263. std::vector<std::pair<int64_t, int64_t>> output_i_value_range(output_i_shape_size, {1, -1});
  264. if (output_i_shape.IsScalar()) {
  265. output_i_value_range.emplace_back(1, -1);
  266. }
  267. output_desc->SetValueRange(output_i_value_range);
  268. GELOGD("Node %s output %zu shape is %s, the generated worst value range is %s.", node->GetName().c_str(), i,
  269. formats::ShapeToString(output_i_shape).c_str(), formats::RangeToString(output_i_value_range).c_str());
  270. }
  271. return GRAPH_SUCCESS;
  272. }
  273. template <typename T>
  274. graphStatus InferValueRangePass::ConstructData(const GeTensorDesc &tensor_desc, bool use_floor_value,
  275. GeTensorPtr &output_ptr) {
  276. std::vector<std::pair<int64_t, int64_t>> value_range;
  277. (void)tensor_desc.GetValueRange(value_range);
  278. if (static_cast<int64_t>(value_range.size()) != tensor_desc.GetShape().GetShapeSize()) {
  279. GELOGW("Value range of input %s is invalid.", tensor_desc.GetName().c_str());
  280. return GRAPH_PARAM_INVALID;
  281. }
  282. size_t value_range_data_num = value_range.size();
  283. unique_ptr<T[]> buf(new (std::nothrow) T[value_range_data_num]());
  284. if (buf == nullptr) {
  285. REPORT_INNER_ERROR("E19999", "New buf failed");
  286. GELOGE(MEMALLOC_FAILED, "New buf failed");
  287. return GRAPH_FAILED;
  288. }
  289. for (size_t j = 0; j < value_range_data_num; ++j) {
  290. auto value_range_j = use_floor_value ? value_range[j].first : value_range[j].second;
  291. buf[j] = static_cast<T>(value_range_j);
  292. }
  293. if (output_ptr->SetData(reinterpret_cast<uint8_t *>(buf.get()), value_range_data_num * sizeof(T)) != GRAPH_SUCCESS) {
  294. GELOGW("Set data failed while constructing value range input tensor.");
  295. return GRAPH_NOT_CHANGED;
  296. }
  297. return GRAPH_SUCCESS;
  298. }
  299. graphStatus InferValueRangePass::ConstructDataByType(const GeTensorDesc &tensor_desc, bool use_floor_value,
  300. GeTensorPtr &output_ptr) {
  301. graphStatus ret = GRAPH_SUCCESS;
  302. auto data_type = tensor_desc.GetDataType();
  303. output_ptr->MutableTensorDesc().SetDataType(data_type);
  304. switch (data_type) {
  305. case DT_FLOAT:
  306. ret = ConstructData<float>(tensor_desc, use_floor_value, output_ptr);
  307. break;
  308. case DT_DOUBLE:
  309. ret = ConstructData<double>(tensor_desc, use_floor_value, output_ptr);
  310. break;
  311. case DT_UINT8:
  312. ret = ConstructData<uint8_t>(tensor_desc, use_floor_value, output_ptr);
  313. break;
  314. case DT_INT8:
  315. ret = ConstructData<int8_t>(tensor_desc, use_floor_value, output_ptr);
  316. break;
  317. case DT_UINT16:
  318. ret = ConstructData<uint16_t>(tensor_desc, use_floor_value, output_ptr);
  319. break;
  320. case DT_INT16:
  321. ret = ConstructData<int16_t>(tensor_desc, use_floor_value, output_ptr);
  322. break;
  323. case DT_INT32:
  324. ret = ConstructData<int32_t>(tensor_desc, use_floor_value, output_ptr);
  325. break;
  326. case DT_INT64:
  327. ret = ConstructData<int64_t>(tensor_desc, use_floor_value, output_ptr);
  328. break;
  329. default:
  330. GELOGW("Data type:%s is not supported.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  331. ret = GRAPH_PARAM_INVALID;
  332. }
  333. return ret;
  334. }
  335. vector<ConstGeTensorPtr> InferValueRangePass::ConstructInputTensors(const NodePtr &node, bool use_floor_value) {
  336. vector<ConstGeTensorPtr> input_tensors;
  337. auto cur_op_desc = node->GetOpDesc();
  338. auto in_data_anchors = node->GetAllInDataAnchors();
  339. for (size_t i = 0; i < in_data_anchors.size(); ++i) {
  340. auto peer_out_anchor = in_data_anchors.at(i)->GetPeerOutAnchor();
  341. if (peer_out_anchor == nullptr) {
  342. continue;
  343. }
  344. auto peer_node = peer_out_anchor->GetOwnerNode();
  345. if (peer_node == nullptr) {
  346. continue;
  347. }
  348. // construct input tensor by constant node
  349. if ((peer_node->GetType() == CONSTANT) || (peer_node->GetType() == CONSTANTOP)) {
  350. vector<GeTensorPtr> const_weight = OpDescUtils::MutableWeights(peer_node);
  351. if (const_weight.empty()) {
  352. GELOGW("MutableWeights failed, weight is empty, node: %s(%s)", peer_node->GetName().c_str(),
  353. peer_node->GetType().c_str());
  354. return vector<ConstGeTensorPtr>();
  355. }
  356. // const/constant op has only one weight
  357. if (const_weight.at(0) == nullptr) {
  358. GELOGW("MutableWeights failed, weight of constant is null, node name: %s(%s)",
  359. peer_node->GetName().c_str(), peer_node->GetType().c_str());
  360. return vector<ConstGeTensorPtr>();
  361. }
  362. input_tensors.push_back(const_weight.at(0));
  363. GELOGD("Node %s construct input tensor %zu by constant node.", node->GetName().c_str(), input_tensors.size());
  364. continue;
  365. }
  366. // construct input tensor by boundary of value range
  367. const auto &input_tensor_desc = cur_op_desc->GetInputDesc(i);
  368. GeTensorPtr tmp_tensor_ptr = MakeShared<GeTensor>(input_tensor_desc);
  369. if (tmp_tensor_ptr == nullptr) {
  370. REPORT_INNER_ERROR("E19999", "Make shared failed");
  371. GELOGE(MEMALLOC_FAILED, "Make shared failed");
  372. return vector<ConstGeTensorPtr>();
  373. }
  374. auto ret = ConstructDataByType(input_tensor_desc, use_floor_value, tmp_tensor_ptr);
  375. if (ret != GRAPH_SUCCESS) {
  376. GELOGW("Construct input tensor by boundary of value range failed for input %s.",
  377. input_tensor_desc.GetName().c_str());
  378. return vector<ConstGeTensorPtr>();
  379. }
  380. input_tensors.push_back(tmp_tensor_ptr);
  381. GELOGD("Node %s construct input tensor %zu by input desc value range.", node->GetName().c_str(),
  382. input_tensors.size());
  383. }
  384. return input_tensors;
  385. }
  386. graphStatus InferValueRangePass::ConstructInputAndInferValueRange(NodePtr &node) {
  387. auto inputs = ConstructInputTensors(node, true);
  388. if (inputs.empty()) {
  389. return GRAPH_PARAM_INVALID;
  390. }
  391. vector<GeTensorPtr> lower_boundary_outputs;
  392. auto ret = RunCpuKernelForValueRange(node, inputs, lower_boundary_outputs);
  393. if (ret != SUCCESS) {
  394. GELOGW("Node %s run cpu kernel failed while calculating value range.", node->GetName().c_str());
  395. return GRAPH_PARAM_INVALID;
  396. }
  397. inputs = ConstructInputTensors(node, false);
  398. if (inputs.empty()) {
  399. return GRAPH_PARAM_INVALID;
  400. }
  401. vector<GeTensorPtr> upper_boundary_outputs;
  402. ret = RunCpuKernelForValueRange(node, inputs, upper_boundary_outputs);
  403. if (ret != SUCCESS) {
  404. GELOGW("Node %s run cpu kernel failed while calculating value range.", node->GetName().c_str());
  405. return GRAPH_PARAM_INVALID;
  406. }
  407. // construct value range from output tensor
  408. OpDescPtr node_desc = node->GetOpDesc();
  409. std::vector<std::pair<int64_t, int64_t>> output_tensor_value_range;
  410. size_t node_output_desc_size = node_desc->GetOutputsSize();
  411. for (size_t i = 0; i < node_output_desc_size; ++i) {
  412. output_tensor_value_range.clear();
  413. auto output_tensor_desc = node_desc->MutableOutputDesc(i);
  414. auto output_shape_size = output_tensor_desc->GetShape().GetShapeSize();
  415. auto lower_boundary_tensor = lower_boundary_outputs[i];
  416. auto lower_boundary_shape = lower_boundary_tensor->GetTensorDesc().GetShape();
  417. auto upper_boundary_tensor = upper_boundary_outputs[i];
  418. auto upper_boundary_shape = upper_boundary_tensor->GetTensorDesc().GetShape();
  419. if (lower_boundary_shape.GetShapeSize() != output_shape_size ||
  420. upper_boundary_shape.GetShapeSize() != output_shape_size) {
  421. GELOGD(
  422. "Cpu kernel result shapes %s, %s and output shape %s do not match, can not infer value range for output %s.",
  423. formats::ShapeToString(lower_boundary_shape).c_str(), formats::ShapeToString(upper_boundary_shape).c_str(),
  424. formats::ShapeToString(output_tensor_desc->GetShape()).c_str(), output_tensor_desc->GetName().c_str());
  425. return GRAPH_PARAM_INVALID;
  426. }
  427. auto data_type = output_tensor_desc->GetDataType();
  428. switch (data_type) {
  429. GET_DATA_BY_DTYPE(DT_INT8, int8_t)
  430. GET_DATA_BY_DTYPE(DT_INT16, int16_t)
  431. GET_DATA_BY_DTYPE(DT_INT32, int32_t)
  432. GET_DATA_BY_DTYPE(DT_INT64, int64_t)
  433. GET_DATA_BY_DTYPE(DT_UINT8, uint8_t)
  434. GET_DATA_BY_DTYPE(DT_UINT16, uint16_t)
  435. GET_DATA_BY_DTYPE(DT_UINT32, uint32_t)
  436. GET_DATA_BY_DTYPE(DT_UINT64, uint64_t)
  437. GET_DATA_BY_DTYPE(DT_FLOAT, float)
  438. GET_DATA_BY_DTYPE(DT_DOUBLE, double)
  439. default:
  440. GELOGW("Data type:%s is not supported.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  441. return GRAPH_PARAM_INVALID;
  442. }
  443. output_tensor_desc->SetValueRange(output_tensor_value_range);
  444. GELOGD("Node %s calculates output %zu value range %s by running cpu kernel.", node->GetName().c_str(), i,
  445. formats::RangeToString(output_tensor_value_range).c_str());
  446. }
  447. return GRAPH_SUCCESS;
  448. }
  449. template <typename T>
  450. void InferValueRangePass::ConstructValueRange(const GeTensorPtr &left_tensor, const GeTensorPtr &right_tensor,
  451. std::vector<std::pair<int64_t, int64_t>> &value_range) {
  452. auto x = reinterpret_cast<const T *>(left_tensor->GetData().GetData());
  453. auto y = reinterpret_cast<const T *>(right_tensor->GetData().GetData());
  454. if (x == nullptr || y == nullptr) {
  455. GELOGI("Output tensor of cpu kernel does not have data, no way to set value range.");
  456. return;
  457. }
  458. for (auto j = 0; j < left_tensor->GetTensorDesc().GetShape().GetShapeSize(); ++j) {
  459. auto left = static_cast<int64_t>(*(x + j));
  460. auto right = static_cast<int64_t>(*(y + j));
  461. value_range.emplace_back(std::make_pair(left, right));
  462. }
  463. }
  464. } // namespace ge

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