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.

kernel_utils.cc 5.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "host_kernels/kernel_utils.h"
  17. #include <vector>
  18. #include "framework/common/ge_inner_error_codes.h"
  19. #include "framework/common/types.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/utils/op_desc_utils.h"
  22. #include "graph/utils/tensor_utils.h"
  23. #include "graph/utils/type_utils.h"
  24. namespace {
  25. const int kDimensionShapeIndex = 0;
  26. const int kDimensionDimsIndex = 1;
  27. const size_t kDimensionNodeInputSize = 2;
  28. } // namespace
  29. namespace ge {
  30. Status KernelUtils::CheckDimensionNodeInfo(const NodePtr &node_ptr) {
  31. if (node_ptr == nullptr) {
  32. GELOGE(FAILED, "parameter is null.");
  33. return FAILED;
  34. }
  35. auto input_nodes = node_ptr->GetInDataNodes();
  36. if (input_nodes.size() != kDimensionNodeInputSize) {
  37. GELOGW("op:%s type: %s, dimension input size must be %zu, but get %zu inputs", node_ptr->GetName().c_str(),
  38. node_ptr->GetType().c_str(), kDimensionNodeInputSize, input_nodes.size());
  39. return NOT_CHANGED;
  40. }
  41. NodePtr dim_node = input_nodes.at(kDimensionDimsIndex);
  42. if (dim_node == nullptr) {
  43. GELOGE(PARAM_INVALID, "dim node is nullptr");
  44. return PARAM_INVALID;
  45. }
  46. std::vector<ConstGeTensorPtr> const_ge_tensor = OpDescUtils::GetWeights(dim_node);
  47. if (const_ge_tensor.empty()) {
  48. GELOGE(PARAM_INVALID, "dim node must be const op");
  49. return PARAM_INVALID;
  50. }
  51. const ConstGeTensorPtr &input_dim = const_ge_tensor.at(0);
  52. if (input_dim->GetData().size() == 0) {
  53. GELOGE(PARAM_INVALID, "dim data size is 0");
  54. return PARAM_INVALID;
  55. }
  56. return SUCCESS;
  57. }
  58. bool KernelUtils::CheckFormatSupported(const NodePtr &node_ptr) {
  59. if (node_ptr == nullptr) {
  60. GELOGE(FAILED, "parameter is null.");
  61. return false;
  62. }
  63. OpDescPtr op_desc = node_ptr->GetOpDesc();
  64. if (op_desc == nullptr) {
  65. GELOGE(FAILED, "op_desc is null");
  66. return false;
  67. }
  68. const auto &input_desc = op_desc->MutableInputDesc(kDimensionShapeIndex);
  69. GE_CHECK_NOTNULL_EXEC(input_desc, return false);
  70. Format fmt = input_desc->GetFormat();
  71. if (fmt == FORMAT_NC1HWC0 || fmt == FORMAT_FRACTAL_Z) {
  72. GELOGW("invalid format, fmt: %s", TypeUtils::FormatToSerialString(fmt).c_str());
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool KernelUtils::CheckSizeForTransOp(const ge::ConstGeTensorPtr &const_weight_ptr,
  78. const ge::OpDescPtr &op_desc_ptr) {
  79. if (const_weight_ptr == nullptr || op_desc_ptr == nullptr) {
  80. GELOGE(FAILED, "parameter invalid");
  81. return false;
  82. }
  83. auto data_size = const_weight_ptr->GetData().GetSize();
  84. const auto &input_desc = op_desc_ptr->MutableInputDesc(0);
  85. GE_CHECK_NOTNULL_EXEC(input_desc, return false);
  86. DataType data_type = input_desc->GetDataType();
  87. GeShape data_shape = input_desc->GetShape();
  88. Format data_format = input_desc->GetFormat();
  89. auto shape_size = input_desc->GetShape().GetShapeSize();
  90. int64_t cal_size = 0;
  91. auto ret = TensorUtils::CalcTensorMemSize(data_shape, data_format, data_type, cal_size);
  92. if (ret != SUCCESS) {
  93. GELOGE(FAILED, "CalcTensorMemSize failed");
  94. return false;
  95. }
  96. uint32_t length = 1;
  97. if (!TypeUtils::GetDataTypeLength(data_type, length)) {
  98. GELOGE(PARAM_INVALID, "Input datatype %d is not support .", data_type);
  99. return false;
  100. }
  101. GELOGI("Const real value Size:%zu, op_desc Shape Size:%ld, data_type:%s.", data_size, cal_size,
  102. TypeUtils::DataTypeToSerialString(data_type).c_str());
  103. if (shape_size != 0) {
  104. // Standard tensor
  105. if (data_size != static_cast<size_t>(cal_size) || data_size == 0) {
  106. GELOGW("Const input data size is not equal with tensor desc shape");
  107. return false;
  108. }
  109. } else if (data_shape.GetDimNum() != 0) {
  110. // Empty tensor, has zero in shape vector
  111. if (data_size != 0) {
  112. GELOGW("Const input data size is not equal with tensor desc shape");
  113. return false;
  114. }
  115. } else {
  116. // Scalar tensor, has only one element in tensor
  117. if (length != 0 && (data_size / static_cast<size_t>(length) != 1)) {
  118. GELOGW("Const input data size is not equal with tensor desc shape");
  119. return false;
  120. }
  121. }
  122. return true;
  123. }
  124. bool KernelUtils::IsUnknownShape(const ge::GeShape &shape) {
  125. vector<int64_t> dims = shape.GetDims();
  126. for (auto dim : dims) {
  127. if (dim < 0) {
  128. GELOGW("Shape kernel recoginze unknown shape.Ignore shape kernel.");
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. } // namespace ge

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