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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. ss << "(format:" << TypeUtils::FormatToSerialString(tensor_desc->GetFormat()) << "),";
  100. ss << "(dtype:" << TypeUtils::DataTypeToSerialString(tensor_desc->GetDataType()) << "),";
  101. ss << "(origin_shape:" << tensor_desc->GetOriginShape().ToString() << "),";
  102. ss << "(origin_format:" << TypeUtils::FormatToSerialString(tensor_desc->GetOriginFormat()) << "),";
  103. ss << "(origin_dtype:" << TypeUtils::DataTypeToSerialString(tensor_desc->GetOriginDataType()) << "),";
  104. string range_str;
  105. SerialShapeRange(tensor_desc, range_str);
  106. ss << "(shape_range:" << range_str << "),";
  107. std::vector<std::pair<int64_t, int64_t>> value_range;
  108. (void)tensor_desc->GetValueRange(value_range);
  109. string value_range_str = formats::RangeToString(value_range);
  110. ss << "(value_range:" << value_range_str << ")]";
  111. return ss.str();
  112. }
  113. bool InferValueRangePass::NeedInfer(const NodePtr &node) const {
  114. auto infer_value_range_param = OperatorFactoryImpl::GetInferValueRangePara(node->GetType());
  115. if (!infer_value_range_param.is_initialized) {
  116. GELOGD("Node %s does not register func to infer value range, skip infer_value_range_pass.",
  117. node->GetName().c_str());
  118. return false;
  119. }
  120. if (infer_value_range_param.when_call == INPUT_IS_DYNAMIC) {
  121. // Only do infer for node that all inputs are dynamic, such as shape
  122. if (InputIsDynamic(node)) {
  123. return true;
  124. }
  125. GELOGD("Node %s register func to infer value range and when_call is INPUT_IS_DYNAMIC, but check input failed.",
  126. node->GetName().c_str());
  127. } else if (infer_value_range_param.when_call == INPUT_HAS_VALUE_RANGE) {
  128. // Only do infer for node that all inputs have value_range or node type of inputs is constant/const
  129. if (InputIsConstOrHasValueRange(node)) {
  130. return true;
  131. }
  132. GELOGD("Node %s register func to infer value range and when_call is INPUT_HAS_VALUE_RANGE, but check input failed.",
  133. node->GetName().c_str());
  134. }
  135. GELOGD("Node %s does not need to infer value range, skip infer_value_range_pass.", node->GetName().c_str());
  136. return false;
  137. }
  138. bool InferValueRangePass::InputIsDynamic(const NodePtr &node) const{
  139. bool input_is_dynamic = false;
  140. auto cur_op_desc = node->GetOpDesc();
  141. for (const auto &input_desc : cur_op_desc->GetAllInputsDescPtr()) {
  142. auto dims = input_desc->GetShape().GetDims();
  143. for (auto dim : dims) {
  144. if (dim == UNKNOWN_DIM || dim == UNKNOWN_DIM_NUM) {
  145. input_is_dynamic = true;
  146. break;
  147. }
  148. }
  149. }
  150. return input_is_dynamic;
  151. }
  152. bool InferValueRangePass::InputIsConstOrHasValueRange(const NodePtr &node) const {
  153. bool input_is_const_or_has_value_range = true;
  154. auto cur_op_desc = node->GetOpDesc();
  155. auto in_data_anchors = node->GetAllInDataAnchors();
  156. for (size_t i = 0; i < in_data_anchors.size(); ++i) {
  157. auto peer_out_anchor = in_data_anchors.at(i)->GetPeerOutAnchor();
  158. if (peer_out_anchor == nullptr) {
  159. continue;
  160. }
  161. auto peer_node = peer_out_anchor->GetOwnerNode();
  162. if (peer_node == nullptr || peer_node->GetOpDesc() == nullptr) {
  163. continue;
  164. }
  165. if ((peer_node->GetType() == CONSTANT) || (peer_node->GetType() == CONSTANTOP)) {
  166. continue;
  167. }
  168. const auto &input_desc = cur_op_desc->GetInputDesc(i);
  169. std::vector<std::pair<int64_t, int64_t>> value_range;
  170. (void)input_desc.GetValueRange(value_range);
  171. if (value_range.empty()) {
  172. GELOGD("Node %s input %zu does not have value range, skip infer_value_range_pass for current node.",
  173. node->GetName().c_str(), i);
  174. input_is_const_or_has_value_range = false;
  175. break;
  176. }
  177. }
  178. return input_is_const_or_has_value_range;
  179. }
  180. bool InferValueRangePass::InputHasUnknownValueRange(const NodePtr &node) const {
  181. bool has_unknown_value_range = false;
  182. auto cur_op_desc = node->GetOpDesc();
  183. for (const auto &input_desc : cur_op_desc->GetAllInputsDescPtr()) {
  184. std::vector<std::pair<int64_t, int64_t>> input_desc_value_range;
  185. input_desc->GetValueRange(input_desc_value_range);
  186. if (!input_desc_value_range.empty()) {
  187. for (const auto &range : input_desc_value_range) {
  188. if (range.first == -1 || range.second == -1) {
  189. GELOGD("Node %s input tensors have unknown value range, value range is %s.", node->GetName().c_str(),
  190. formats::RangeToString(input_desc_value_range).c_str());
  191. has_unknown_value_range = true;
  192. }
  193. }
  194. }
  195. }
  196. return has_unknown_value_range;
  197. }
  198. graphStatus InferValueRangePass::UpdateTensorDesc(const GeTensorDescPtr &src, GeTensorDescPtr &dst, bool &changed) {
  199. if (src == nullptr || dst == nullptr) {
  200. REPORT_CALL_ERROR("E19999", "While updating tensor desc, input desc is null.");
  201. GELOGE(GRAPH_FAILED, "[Param][check] While updating tensor desc, input desc is null.");
  202. return GRAPH_FAILED;
  203. }
  204. changed = false;
  205. std::vector<std::pair<int64_t, int64_t>> src_value_range;
  206. std::vector<std::pair<int64_t, int64_t>> dst_value_range;
  207. (void)src->GetValueRange(src_value_range);
  208. (void)dst->GetValueRange(dst_value_range);
  209. if (src_value_range != dst_value_range) {
  210. GELOGD("While updating tensor desc, value range has been changed, src value range: %s, dst value range: %s.",
  211. formats::RangeToString(src_value_range).c_str(), formats::RangeToString(dst_value_range).c_str());
  212. changed = true;
  213. }
  214. dst->SetValueRange(src_value_range);
  215. return GRAPH_SUCCESS;
  216. }
  217. graphStatus InferValueRangePass::UpdateOutputFromSubgraphs(const std::vector<GeTensorDescPtr> &src,
  218. GeTensorDescPtr &dst) {
  219. std::vector<std::pair<int64_t, int64_t>> ref_out_tensor_value_range;
  220. auto ref_out_tensor = src.at(0);
  221. (void)ref_out_tensor->GetValueRange(ref_out_tensor_value_range);
  222. for (auto &ref_tensor : src) {
  223. std::vector<std::pair<int64_t, int64_t>> ref_tensor_value_range;
  224. (void)ref_tensor->GetValueRange(ref_tensor_value_range);
  225. if (ref_tensor_value_range.size() != ref_out_tensor_value_range.size()) {
  226. GELOGD("Update TensorDesc %s failed, rank of value ranges %s and %s are not the same, skip value range refresh.",
  227. dst->GetName().c_str(), formats::RangeToString(ref_out_tensor_value_range).c_str(),
  228. formats::RangeToString(ref_tensor_value_range).c_str());
  229. return GRAPH_SUCCESS;
  230. }
  231. for (size_t j = 0; j < ref_out_tensor_value_range.size(); j++) {
  232. if ((ref_out_tensor_value_range.at(j).first != ref_tensor_value_range.at(j).first) ||
  233. (ref_out_tensor_value_range.at(j).second != ref_tensor_value_range.at(j).second)) {
  234. ref_out_tensor_value_range[j] = std::make_pair(1, -1);
  235. }
  236. }
  237. }
  238. GELOGD("While updating output desc from subgraphs, set parent node desc value range %s.",
  239. formats::RangeToString(ref_out_tensor_value_range).c_str());
  240. dst->SetValueRange(ref_out_tensor_value_range);
  241. return GRAPH_SUCCESS;
  242. }
  243. graphStatus InferValueRangePass::UpdateOutputFromSubgraphsForMultiDims(const std::vector<GeTensorDescPtr> &src,
  244. GeTensorDescPtr &dst) {
  245. REPORT_INNER_ERROR("E19999",
  246. "Update TensorDesc %s failed. In dynamic multi-dims size scene, there should be no value range.",
  247. dst->GetName().c_str());
  248. GELOGE(GRAPH_FAILED,
  249. "[Update][TensorDesc] %s failed. In dynamic multi-dims size scene, there should be no value range.",
  250. dst->GetName().c_str());
  251. return GRAPH_FAILED;
  252. }
  253. graphStatus InferValueRangePass::GenerateWorstValueRange(NodePtr &node) {
  254. GELOGI("Node %s does not run cpu kernel, because input value range has -1.", node->GetName().c_str());
  255. OpDescPtr op_desc = node->GetOpDesc();
  256. for (size_t i = 0; i < op_desc->GetOutputsSize(); ++i) {
  257. auto output_desc = op_desc->MutableOutputDesc(i);
  258. if (output_desc == nullptr) {
  259. continue;
  260. }
  261. auto output_i_shape = output_desc->GetShape();
  262. auto output_i_shape_size = output_i_shape.GetShapeSize();
  263. if (output_i_shape_size < 0) {
  264. GELOGD("Node %s output shape is unknown, cannot infer value range, shape is %s.", node->GetName().c_str(),
  265. formats::ShapeToString(output_i_shape).c_str());
  266. return GRAPH_NOT_CHANGED;
  267. }
  268. std::vector<std::pair<int64_t, int64_t>> output_i_value_range(output_i_shape_size, {1, -1});
  269. output_desc->SetValueRange(output_i_value_range);
  270. GELOGD("Node %s output %zu shape is %s, the generated worst value range is %s.", node->GetName().c_str(), i,
  271. formats::ShapeToString(output_i_shape).c_str(), formats::RangeToString(output_i_value_range).c_str());
  272. }
  273. return GRAPH_SUCCESS;
  274. }
  275. template <typename T>
  276. graphStatus InferValueRangePass::ConstructData(const GeTensorDesc &tensor_desc, bool use_floor_value,
  277. GeTensorPtr &output_ptr) {
  278. std::vector<std::pair<int64_t, int64_t>> value_range;
  279. (void)tensor_desc.GetValueRange(value_range);
  280. if (static_cast<int64_t>(value_range.size()) != tensor_desc.GetShape().GetShapeSize()) {
  281. GELOGW("Value range of input %s is invalid.", tensor_desc.GetName().c_str());
  282. return GRAPH_PARAM_INVALID;
  283. }
  284. size_t value_range_data_num = value_range.size();
  285. unique_ptr<T[]> buf(new (std::nothrow) T[value_range_data_num]());
  286. if (buf == nullptr) {
  287. REPORT_INNER_ERROR("E19999", "New buf failed");
  288. GELOGE(MEMALLOC_FAILED, "New buf failed");
  289. return GRAPH_FAILED;
  290. }
  291. for (size_t j = 0; j < value_range_data_num; ++j) {
  292. auto value_range_j = use_floor_value ? value_range[j].first : value_range[j].second;
  293. buf[j] = static_cast<T>(value_range_j);
  294. }
  295. if (output_ptr->SetData(reinterpret_cast<uint8_t *>(buf.get()), value_range_data_num * sizeof(T)) != GRAPH_SUCCESS) {
  296. GELOGW("Set data failed while constructing value range input tensor.");
  297. return GRAPH_NOT_CHANGED;
  298. }
  299. return GRAPH_SUCCESS;
  300. }
  301. graphStatus InferValueRangePass::ConstructDataByType(const GeTensorDesc &tensor_desc, bool use_floor_value,
  302. GeTensorPtr &output_ptr) {
  303. graphStatus ret = GRAPH_SUCCESS;
  304. auto data_type = tensor_desc.GetDataType();
  305. output_ptr->MutableTensorDesc().SetDataType(data_type);
  306. switch (data_type) {
  307. case DT_FLOAT:
  308. ret = ConstructData<float>(tensor_desc, use_floor_value, output_ptr);
  309. break;
  310. case DT_DOUBLE:
  311. ret = ConstructData<double>(tensor_desc, use_floor_value, output_ptr);
  312. break;
  313. case DT_UINT8:
  314. ret = ConstructData<uint8_t>(tensor_desc, use_floor_value, output_ptr);
  315. break;
  316. case DT_INT8:
  317. ret = ConstructData<int8_t>(tensor_desc, use_floor_value, output_ptr);
  318. break;
  319. case DT_UINT16:
  320. ret = ConstructData<uint16_t>(tensor_desc, use_floor_value, output_ptr);
  321. break;
  322. case DT_INT16:
  323. ret = ConstructData<int16_t>(tensor_desc, use_floor_value, output_ptr);
  324. break;
  325. case DT_INT32:
  326. ret = ConstructData<int32_t>(tensor_desc, use_floor_value, output_ptr);
  327. break;
  328. case DT_INT64:
  329. ret = ConstructData<int64_t>(tensor_desc, use_floor_value, output_ptr);
  330. break;
  331. default:
  332. GELOGW("Data type:%s is not supported.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  333. ret = GRAPH_PARAM_INVALID;
  334. }
  335. return ret;
  336. }
  337. vector<ConstGeTensorPtr> InferValueRangePass::ConstructInputTensors(const NodePtr &node, bool use_floor_value) {
  338. vector<ConstGeTensorPtr> input_tensors;
  339. auto cur_op_desc = node->GetOpDesc();
  340. auto in_data_anchors = node->GetAllInDataAnchors();
  341. for (size_t i = 0; i < in_data_anchors.size(); ++i) {
  342. auto peer_out_anchor = in_data_anchors.at(i)->GetPeerOutAnchor();
  343. if (peer_out_anchor == nullptr) {
  344. continue;
  345. }
  346. auto peer_node = peer_out_anchor->GetOwnerNode();
  347. if (peer_node == nullptr) {
  348. continue;
  349. }
  350. // construct input tensor by constant node
  351. if ((peer_node->GetType() == CONSTANT) || (peer_node->GetType() == CONSTANTOP)) {
  352. vector<GeTensorPtr> const_weight = OpDescUtils::MutableWeights(peer_node);
  353. if (const_weight.empty()) {
  354. GELOGW("MutableWeights failed, weight is empty, node: %s(%s)", peer_node->GetName().c_str(),
  355. peer_node->GetType().c_str());
  356. return vector<ConstGeTensorPtr>();
  357. }
  358. // const/constant op has only one weight
  359. if (const_weight.at(0) == nullptr) {
  360. GELOGW("MutableWeights failed, weight of constant is null, node name: %s(%s)",
  361. peer_node->GetName().c_str(), peer_node->GetType().c_str());
  362. return vector<ConstGeTensorPtr>();
  363. }
  364. input_tensors.push_back(const_weight.at(0));
  365. GELOGD("Node %s construct input tensor %zu by constant node.", node->GetName().c_str(), input_tensors.size());
  366. continue;
  367. }
  368. // construct input tensor by boundary of value range
  369. const auto &input_tensor_desc = cur_op_desc->GetInputDesc(i);
  370. GeTensorPtr tmp_tensor_ptr = MakeShared<GeTensor>(input_tensor_desc);
  371. if (tmp_tensor_ptr == nullptr) {
  372. REPORT_INNER_ERROR("E19999", "Make shared failed");
  373. GELOGE(MEMALLOC_FAILED, "Make shared failed");
  374. return vector<ConstGeTensorPtr>();
  375. }
  376. auto ret = ConstructDataByType(input_tensor_desc, use_floor_value, tmp_tensor_ptr);
  377. if (ret != GRAPH_SUCCESS) {
  378. GELOGW("Construct input tensor by boundary of value range failed for input %s.",
  379. input_tensor_desc.GetName().c_str());
  380. return vector<ConstGeTensorPtr>();
  381. }
  382. input_tensors.push_back(tmp_tensor_ptr);
  383. GELOGD("Node %s construct input tensor %zu by input desc value range.", node->GetName().c_str(),
  384. input_tensors.size());
  385. }
  386. return input_tensors;
  387. }
  388. graphStatus InferValueRangePass::ConstructInputAndInferValueRange(NodePtr &node) {
  389. auto inputs = ConstructInputTensors(node, true);
  390. if (inputs.empty()) {
  391. return GRAPH_PARAM_INVALID;
  392. }
  393. vector<GeTensorPtr> lower_boundary_outputs;
  394. auto ret = RunCpuKernelForValueRange(node, inputs, lower_boundary_outputs);
  395. if (ret != SUCCESS) {
  396. GELOGW("Node %s run cpu kernel failed while calculating value range.", node->GetName().c_str());
  397. return GRAPH_PARAM_INVALID;
  398. }
  399. inputs = ConstructInputTensors(node, false);
  400. if (inputs.empty()) {
  401. return GRAPH_PARAM_INVALID;
  402. }
  403. vector<GeTensorPtr> upper_boundary_outputs;
  404. ret = RunCpuKernelForValueRange(node, inputs, upper_boundary_outputs);
  405. if (ret != SUCCESS) {
  406. GELOGW("Node %s run cpu kernel failed while calculating value range.", node->GetName().c_str());
  407. return GRAPH_PARAM_INVALID;
  408. }
  409. // construct value range from output tensor
  410. OpDescPtr node_desc = node->GetOpDesc();
  411. std::vector<std::pair<int64_t, int64_t>> output_tensor_value_range;
  412. size_t node_output_desc_size = node_desc->GetOutputsSize();
  413. for (size_t i = 0; i < node_output_desc_size; ++i) {
  414. output_tensor_value_range.clear();
  415. auto output_tensor_desc = node_desc->MutableOutputDesc(i);
  416. auto output_shape_size = output_tensor_desc->GetShape().GetShapeSize();
  417. auto lower_boundary_tensor = lower_boundary_outputs[i];
  418. auto lower_boundary_shape = lower_boundary_tensor->GetTensorDesc().GetShape();
  419. auto upper_boundary_tensor = upper_boundary_outputs[i];
  420. auto upper_boundary_shape = upper_boundary_tensor->GetTensorDesc().GetShape();
  421. if (lower_boundary_shape.GetShapeSize() != output_shape_size ||
  422. upper_boundary_shape.GetShapeSize() != output_shape_size) {
  423. GELOGD(
  424. "Cpu kernel result shapes %s, %s and output shape %s do not match, can not infer value range for output %s.",
  425. formats::ShapeToString(lower_boundary_shape).c_str(), formats::ShapeToString(upper_boundary_shape).c_str(),
  426. formats::ShapeToString(output_tensor_desc->GetShape()).c_str(), output_tensor_desc->GetName().c_str());
  427. return GRAPH_PARAM_INVALID;
  428. }
  429. auto data_type = output_tensor_desc->GetDataType();
  430. switch (data_type) {
  431. GET_DATA_BY_DTYPE(DT_INT8, int8_t)
  432. GET_DATA_BY_DTYPE(DT_INT16, int16_t)
  433. GET_DATA_BY_DTYPE(DT_INT32, int32_t)
  434. GET_DATA_BY_DTYPE(DT_INT64, int64_t)
  435. GET_DATA_BY_DTYPE(DT_UINT8, uint8_t)
  436. GET_DATA_BY_DTYPE(DT_UINT16, uint16_t)
  437. GET_DATA_BY_DTYPE(DT_UINT32, uint32_t)
  438. GET_DATA_BY_DTYPE(DT_UINT64, uint64_t)
  439. GET_DATA_BY_DTYPE(DT_FLOAT, float)
  440. GET_DATA_BY_DTYPE(DT_DOUBLE, double)
  441. default:
  442. GELOGW("Data type:%s is not supported.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  443. return GRAPH_PARAM_INVALID;
  444. }
  445. output_tensor_desc->SetValueRange(output_tensor_value_range);
  446. GELOGD("Node %s calculates output %zu value range %s by running cpu kernel.", node->GetName().c_str(), i,
  447. formats::RangeToString(output_tensor_value_range).c_str());
  448. }
  449. return GRAPH_SUCCESS;
  450. }
  451. template <typename T>
  452. void InferValueRangePass::ConstructValueRange(const GeTensorPtr &left_tensor, const GeTensorPtr &right_tensor,
  453. std::vector<std::pair<int64_t, int64_t>> &value_range) {
  454. auto x = reinterpret_cast<const T *>(left_tensor->GetData().GetData());
  455. auto y = reinterpret_cast<const T *>(right_tensor->GetData().GetData());
  456. if (x == nullptr || y == nullptr) {
  457. GELOGI("Output tensor of cpu kernel does not have data, no way to set value range.");
  458. return;
  459. }
  460. for (auto j = 0; j < left_tensor->GetTensorDesc().GetShape().GetShapeSize(); ++j) {
  461. auto left = static_cast<int64_t>(*(x + j));
  462. auto right = static_cast<int64_t>(*(y + j));
  463. value_range.emplace_back(std::make_pair(left, right));
  464. }
  465. }
  466. } // namespace ge

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