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.

empty_kernel.cc 5.1 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
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/empty_kernel.h"
  17. #include <memory>
  18. #include "common/fp16_t.h"
  19. #include "framework/common/op/ge_op_utils.h"
  20. #include "framework/common/types.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "framework/common/ge_inner_error_codes.h"
  23. #include "host_kernels/kernel_utils.h"
  24. #include "graph/passes/pass_utils.h"
  25. #include "graph/utils/type_utils.h"
  26. #include "inc/kernel_factory.h"
  27. namespace ge {
  28. namespace {
  29. const size_t kEmptyFirstInput = 0;
  30. const size_t kEmptyFirstOutput = 0;
  31. const size_t kEmptyInputsSize = 1;
  32. const size_t kEmptyOutputsSize = 1;
  33. const size_t kShapeMaxDims = 1;
  34. } // namespace
  35. Status EmptyKernel::EmptyCheck(const OpDescPtr &op_desc_ptr, const std::vector<ConstGeTensorPtr> &input) {
  36. if (op_desc_ptr == nullptr) {
  37. GELOGW("Parameter's invalid, Input opDescPtr is nullptr.");
  38. return PARAM_INVALID;
  39. }
  40. // check input size
  41. bool size_check =
  42. ((op_desc_ptr->GetAllInputsDesc().size() != kEmptyInputsSize) || (input.size() != kEmptyInputsSize) ||
  43. (op_desc_ptr->GetAllOutputsDesc().size() != kEmptyOutputsSize));
  44. if (size_check) {
  45. GELOGW("Input/Output size error. InDesc size:%zu, OutDesc size:%zu, in size:%zu ",
  46. op_desc_ptr->GetAllInputsDesc().size(), op_desc_ptr->GetAllOutputsDesc().size(), input.size());
  47. return PARAM_INVALID;
  48. }
  49. if (input.at(kEmptyFirstInput) == nullptr) {
  50. GELOGW("Parameter's invalid, first input is nullptr.");
  51. return PARAM_INVALID;
  52. }
  53. ConstGeTensorPtr shape = input.at(kEmptyFirstInput);
  54. // Check if the dimension is 1-D
  55. if (shape->GetTensorDesc().GetShape().GetDimNum() > kShapeMaxDims) {
  56. GELOGW("Check if the dimension is 1-D failed, dims:%zu",
  57. shape->GetTensorDesc().GetShape().GetDimNum());
  58. return PARAM_INVALID;
  59. }
  60. return SUCCESS;
  61. }
  62. Status EmptyKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  63. std::vector<GeTensorPtr> &v_output) {
  64. GELOGD("Empty kernel in");
  65. Status ret = EmptyCheck(op_desc_ptr, input);
  66. if (ret != SUCCESS) {
  67. return NOT_CHANGED;
  68. }
  69. ConstGeTensorPtr shape = input.at(kEmptyFirstInput);
  70. GE_CHECK_NOTNULL(shape);
  71. int64_t total_data_size = 1;
  72. std::vector<int64_t> shape_vec;
  73. DataType shape_type = shape->GetTensorDesc().GetDataType();
  74. // Calculate user input dim
  75. if (shape_type == DT_INT32) {
  76. ret = KernelUtils::CalcDims<int32_t>(shape, shape_vec, total_data_size);
  77. } else if (shape_type == DT_INT64) {
  78. ret = KernelUtils::CalcDims<int64_t>(shape, shape_vec, total_data_size);
  79. } else {
  80. GELOGW("shape type must be DT_INT32 or DT_INT64.");
  81. return NOT_CHANGED;
  82. }
  83. if (ret != SUCCESS) {
  84. GELOGE(ret, "CalcDims failed, dim_type: %s", TypeUtils::DataTypeToSerialString(shape_type).c_str());
  85. return ret;
  86. }
  87. auto output_tensor_desc = op_desc_ptr->GetOutputDesc(kEmptyFirstOutput);
  88. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  89. if (output_ptr == nullptr) {
  90. GELOGE(MEMALLOC_FAILED, "make_shared ge::GeTensor failed");
  91. return MEMALLOC_FAILED;
  92. }
  93. DataType data_type = op_desc_ptr->GetOutputDesc(kEmptyFirstOutput).GetDataType();
  94. ret = PARAM_INVALID;
  95. uint64_t data = 0;
  96. switch (data_type) {
  97. #define CASE(dtype, type) \
  98. case dtype: \
  99. ret = KernelUtils::GenData(total_data_size, (type)data, output_ptr); \
  100. break;
  101. CASE(DT_FLOAT, float)
  102. CASE(DT_FLOAT16, ge::fp16_t)
  103. CASE(DT_INT8, int8_t)
  104. CASE(DT_INT16, int16_t)
  105. CASE(DT_UINT16, uint16_t)
  106. CASE(DT_UINT8, uint8_t)
  107. CASE(DT_INT32, int32_t)
  108. CASE(DT_INT64, int64_t)
  109. CASE(DT_UINT32, uint32_t)
  110. CASE(DT_UINT64, uint64_t)
  111. CASE(DT_BOOL, bool)
  112. CASE(DT_DOUBLE, double)
  113. #undef CASE
  114. default:
  115. GELOGW("invalid data type: %s", TypeUtils::DataTypeToSerialString(data_type).c_str());
  116. return NOT_CHANGED;
  117. }
  118. if (ret != SUCCESS) {
  119. GELOGE(ret, "GenData failed, data_type: %s", TypeUtils::DataTypeToSerialString(data_type).c_str());
  120. return ret;
  121. }
  122. output_ptr->MutableTensorDesc().SetShape(GeShape(shape_vec));
  123. output_ptr->MutableTensorDesc().SetDataType(DataType(data_type));
  124. Format format = op_desc_ptr->GetOutputDesc(kEmptyFirstOutput).GetFormat();
  125. output_ptr->MutableTensorDesc().SetFormat(format);
  126. v_output.push_back(output_ptr);
  127. GELOGI("Empty kernel success");
  128. return SUCCESS;
  129. }
  130. REGISTER_KERNEL(EMPTY, EmptyKernel);
  131. } // namespace ge

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