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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. size_t value_range_data_num = value_range.size();
  279. auto tensor_shape = tensor_desc.GetShape();
  280. bool value_range_and_tensor_shape_matched = true;
  281. if (tensor_shape.IsScalar()){
  282. // scalar tensor has only one value_range pair
  283. if (value_range_data_num != 1) {
  284. value_range_and_tensor_shape_matched = false;
  285. }
  286. } else {
  287. // normal tensor, value_range size is equal to tensor shape size.
  288. if (static_cast<int64_t>(value_range_data_num) != tensor_shape.GetShapeSize()) {
  289. value_range_and_tensor_shape_matched = false;
  290. }
  291. }
  292. if (!value_range_and_tensor_shape_matched) {
  293. GELOGW("Input %s value range and tensor shape do not match. Value range size is %zu, tensor shape is %s.",
  294. tensor_desc.GetName().c_str(), value_range_data_num, formats::ShapeToString(tensor_shape).c_str());
  295. return GRAPH_PARAM_INVALID;
  296. }
  297. unique_ptr<T[]> buf(new (std::nothrow) T[value_range_data_num]());
  298. if (buf == nullptr) {
  299. REPORT_INNER_ERROR("E19999", "New buf failed");
  300. GELOGE(MEMALLOC_FAILED, "New buf failed");
  301. return GRAPH_FAILED;
  302. }
  303. for (size_t j = 0; j < value_range_data_num; ++j) {
  304. auto value_range_j = use_floor_value ? value_range[j].first : value_range[j].second;
  305. buf[j] = static_cast<T>(value_range_j);
  306. }
  307. if (output_ptr->SetData(reinterpret_cast<uint8_t *>(buf.get()), value_range_data_num * sizeof(T)) != GRAPH_SUCCESS) {
  308. GELOGW("Set data failed while constructing value range input tensor.");
  309. return GRAPH_NOT_CHANGED;
  310. }
  311. return GRAPH_SUCCESS;
  312. }
  313. graphStatus InferValueRangePass::ConstructDataByType(const GeTensorDesc &tensor_desc, bool use_floor_value,
  314. GeTensorPtr &output_ptr) {
  315. graphStatus ret = GRAPH_SUCCESS;
  316. auto data_type = tensor_desc.GetDataType();
  317. output_ptr->MutableTensorDesc().SetDataType(data_type);
  318. switch (data_type) {
  319. case DT_FLOAT:
  320. ret = ConstructData<float>(tensor_desc, use_floor_value, output_ptr);
  321. break;
  322. case DT_DOUBLE:
  323. ret = ConstructData<double>(tensor_desc, use_floor_value, output_ptr);
  324. break;
  325. case DT_UINT8:
  326. ret = ConstructData<uint8_t>(tensor_desc, use_floor_value, output_ptr);
  327. break;
  328. case DT_INT8:
  329. ret = ConstructData<int8_t>(tensor_desc, use_floor_value, output_ptr);
  330. break;
  331. case DT_UINT16:
  332. ret = ConstructData<uint16_t>(tensor_desc, use_floor_value, output_ptr);
  333. break;
  334. case DT_INT16:
  335. ret = ConstructData<int16_t>(tensor_desc, use_floor_value, output_ptr);
  336. break;
  337. case DT_INT32:
  338. ret = ConstructData<int32_t>(tensor_desc, use_floor_value, output_ptr);
  339. break;
  340. case DT_INT64:
  341. ret = ConstructData<int64_t>(tensor_desc, use_floor_value, output_ptr);
  342. break;
  343. default:
  344. GELOGW("Data type:%s is not supported.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  345. ret = GRAPH_PARAM_INVALID;
  346. }
  347. return ret;
  348. }
  349. vector<ConstGeTensorPtr> InferValueRangePass::ConstructInputTensors(const NodePtr &node, bool use_floor_value) {
  350. vector<ConstGeTensorPtr> input_tensors;
  351. auto cur_op_desc = node->GetOpDesc();
  352. auto in_data_anchors = node->GetAllInDataAnchors();
  353. for (size_t i = 0; i < in_data_anchors.size(); ++i) {
  354. auto peer_out_anchor = in_data_anchors.at(i)->GetPeerOutAnchor();
  355. if (peer_out_anchor == nullptr) {
  356. continue;
  357. }
  358. auto peer_node = peer_out_anchor->GetOwnerNode();
  359. if (peer_node == nullptr) {
  360. continue;
  361. }
  362. // construct input tensor by constant node
  363. if ((peer_node->GetType() == CONSTANT) || (peer_node->GetType() == CONSTANTOP)) {
  364. vector<GeTensorPtr> const_weight = OpDescUtils::MutableWeights(peer_node);
  365. if (const_weight.empty()) {
  366. GELOGW("MutableWeights failed, weight is empty, node: %s(%s)", peer_node->GetName().c_str(),
  367. peer_node->GetType().c_str());
  368. return vector<ConstGeTensorPtr>();
  369. }
  370. // const/constant op has only one weight
  371. if (const_weight.at(0) == nullptr) {
  372. GELOGW("MutableWeights failed, weight of constant is null, node name: %s(%s)",
  373. peer_node->GetName().c_str(), peer_node->GetType().c_str());
  374. return vector<ConstGeTensorPtr>();
  375. }
  376. input_tensors.push_back(const_weight.at(0));
  377. GELOGD("Node %s construct input tensor %zu by constant node.", node->GetName().c_str(), input_tensors.size());
  378. continue;
  379. }
  380. // construct input tensor by boundary of value range
  381. const auto &input_tensor_desc = cur_op_desc->GetInputDesc(i);
  382. GeTensorPtr tmp_tensor_ptr = MakeShared<GeTensor>(input_tensor_desc);
  383. if (tmp_tensor_ptr == nullptr) {
  384. REPORT_INNER_ERROR("E19999", "Make shared failed");
  385. GELOGE(MEMALLOC_FAILED, "Make shared failed");
  386. return vector<ConstGeTensorPtr>();
  387. }
  388. auto ret = ConstructDataByType(input_tensor_desc, use_floor_value, tmp_tensor_ptr);
  389. if (ret != GRAPH_SUCCESS) {
  390. GELOGW("Construct input tensor by boundary of value range failed for input %s.",
  391. input_tensor_desc.GetName().c_str());
  392. return vector<ConstGeTensorPtr>();
  393. }
  394. input_tensors.push_back(tmp_tensor_ptr);
  395. GELOGD("Node %s construct input tensor %zu by input desc value range.", node->GetName().c_str(),
  396. input_tensors.size());
  397. }
  398. return input_tensors;
  399. }
  400. graphStatus InferValueRangePass::ConstructInputAndInferValueRange(NodePtr &node) {
  401. auto inputs = ConstructInputTensors(node, true);
  402. if (inputs.empty()) {
  403. return GRAPH_PARAM_INVALID;
  404. }
  405. vector<GeTensorPtr> lower_boundary_outputs;
  406. auto ret = RunCpuKernelForValueRange(node, inputs, lower_boundary_outputs);
  407. if (ret != SUCCESS) {
  408. GELOGW("Node %s run cpu kernel failed while calculating value range.", node->GetName().c_str());
  409. return GRAPH_PARAM_INVALID;
  410. }
  411. inputs = ConstructInputTensors(node, false);
  412. if (inputs.empty()) {
  413. return GRAPH_PARAM_INVALID;
  414. }
  415. vector<GeTensorPtr> upper_boundary_outputs;
  416. ret = RunCpuKernelForValueRange(node, inputs, upper_boundary_outputs);
  417. if (ret != SUCCESS) {
  418. GELOGW("Node %s run cpu kernel failed while calculating value range.", node->GetName().c_str());
  419. return GRAPH_PARAM_INVALID;
  420. }
  421. // construct value range from output tensor
  422. OpDescPtr node_desc = node->GetOpDesc();
  423. std::vector<std::pair<int64_t, int64_t>> output_tensor_value_range;
  424. size_t node_output_desc_size = node_desc->GetOutputsSize();
  425. for (size_t i = 0; i < node_output_desc_size; ++i) {
  426. output_tensor_value_range.clear();
  427. auto output_tensor_desc = node_desc->MutableOutputDesc(i);
  428. auto output_shape_size = output_tensor_desc->GetShape().GetShapeSize();
  429. auto lower_boundary_tensor = lower_boundary_outputs[i];
  430. auto lower_boundary_shape = lower_boundary_tensor->GetTensorDesc().GetShape();
  431. auto upper_boundary_tensor = upper_boundary_outputs[i];
  432. auto upper_boundary_shape = upper_boundary_tensor->GetTensorDesc().GetShape();
  433. if (lower_boundary_shape.GetShapeSize() != output_shape_size ||
  434. upper_boundary_shape.GetShapeSize() != output_shape_size) {
  435. GELOGD(
  436. "Cpu kernel result shapes %s, %s and output shape %s do not match, can not infer value range for output %s.",
  437. formats::ShapeToString(lower_boundary_shape).c_str(), formats::ShapeToString(upper_boundary_shape).c_str(),
  438. formats::ShapeToString(output_tensor_desc->GetShape()).c_str(), output_tensor_desc->GetName().c_str());
  439. return GRAPH_PARAM_INVALID;
  440. }
  441. auto data_type = output_tensor_desc->GetDataType();
  442. switch (data_type) {
  443. GET_DATA_BY_DTYPE(DT_INT8, int8_t)
  444. GET_DATA_BY_DTYPE(DT_INT16, int16_t)
  445. GET_DATA_BY_DTYPE(DT_INT32, int32_t)
  446. GET_DATA_BY_DTYPE(DT_INT64, int64_t)
  447. GET_DATA_BY_DTYPE(DT_UINT8, uint8_t)
  448. GET_DATA_BY_DTYPE(DT_UINT16, uint16_t)
  449. GET_DATA_BY_DTYPE(DT_UINT32, uint32_t)
  450. GET_DATA_BY_DTYPE(DT_UINT64, uint64_t)
  451. GET_DATA_BY_DTYPE(DT_FLOAT, float)
  452. GET_DATA_BY_DTYPE(DT_DOUBLE, double)
  453. default:
  454. GELOGW("Data type:%s is not supported.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  455. return GRAPH_PARAM_INVALID;
  456. }
  457. output_tensor_desc->SetValueRange(output_tensor_value_range);
  458. GELOGD("Node %s calculates output %zu value range %s by running cpu kernel.", node->GetName().c_str(), i,
  459. formats::RangeToString(output_tensor_value_range).c_str());
  460. }
  461. return GRAPH_SUCCESS;
  462. }
  463. template <typename T>
  464. void InferValueRangePass::ConstructValueRange(const GeTensorPtr &left_tensor, const GeTensorPtr &right_tensor,
  465. std::vector<std::pair<int64_t, int64_t>> &value_range) {
  466. auto x = reinterpret_cast<const T *>(left_tensor->GetData().GetData());
  467. auto y = reinterpret_cast<const T *>(right_tensor->GetData().GetData());
  468. if (x == nullptr || y == nullptr) {
  469. GELOGI("Output tensor of cpu kernel does not have data, no way to set value range.");
  470. return;
  471. }
  472. auto left_tensor_shape = left_tensor->GetTensorDesc().GetShape();
  473. for (auto j = 0; j < left_tensor_shape.GetShapeSize(); ++j) {
  474. auto left = static_cast<int64_t>(*(x + j));
  475. auto right = static_cast<int64_t>(*(y + j));
  476. value_range.emplace_back(left, right);
  477. }
  478. if (left_tensor_shape.IsScalar()) {
  479. GELOGD("When inferring value range, output tensors of cpu kernel are scalar tensors.");
  480. value_range.emplace_back(static_cast<int64_t>(*x), static_cast<int64_t>(*y));
  481. }
  482. }
  483. } // namespace ge

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