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.

floormod_kernel.cc 6.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/floormod_kernel.h"
  17. #include <memory>
  18. #include <set>
  19. #include "common/types.h"
  20. #include "common/util.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "framework/common/ge_inner_error_codes.h"
  23. #include "graph/common/bcast.h"
  24. #include "graph/utils/type_utils.h"
  25. #include "inc/kernel_factory.h"
  26. namespace ge {
  27. namespace {
  28. const size_t kFloorModInputX = 0;
  29. const size_t kFloorModInputY = 1;
  30. const size_t kFloorModFirstOutput = 0;
  31. const size_t kFloorModInputNum = 2;
  32. const std::set<DataType> kFloorModSupportedType = {DT_INT32};
  33. // func FloorDiv is compute floor(x/y); quotient is integer
  34. template <typename T>
  35. T FloorDiv(T const &x, T const &y) {
  36. if ((x < static_cast<T>(0)) != (y < static_cast<T>(0))) {
  37. T abs_x = std::abs(x);
  38. T abs_y = std::abs(y);
  39. return static_cast<int32_t>((1 - abs_x) / abs_y - 1);
  40. } else {
  41. return x / y;
  42. }
  43. }
  44. template <typename T>
  45. Status CheckYIsZero(T const &y, DataType &type) {
  46. switch (type) {
  47. case DT_INT32:
  48. if (y == static_cast<T>(0)) {
  49. GELOGE(INTERNAL_ERROR, "CheckYIsZero failed, y is zero.");
  50. return INTERNAL_ERROR;
  51. }
  52. break;
  53. default:
  54. return INTERNAL_ERROR;
  55. }
  56. return SUCCESS;
  57. }
  58. // mod(x,y) equals to x - y * floor(x/y)
  59. #define DEFINE_FUNC_BY_TYPE(TYPE) \
  60. std::function<TYPE(TYPE const &, TYPE const &, DataType &, Status &)> func_##TYPE = \
  61. [](TYPE const &a, TYPE const &b, DataType &type, Status &ret) -> TYPE { \
  62. ret = CheckYIsZero(b, type); \
  63. if (ret != SUCCESS) { \
  64. return static_cast<TYPE>(0); \
  65. } \
  66. return (a - b * FloorDiv(a, b)); \
  67. };
  68. #define SET_BCAST_COMPUTE_CASE(DTYPE, TYPE) \
  69. case DTYPE: \
  70. ret = bcast.BCastComputeCheck(input, y_data_##TYPE, func_##TYPE); \
  71. break;
  72. #define SET_OUTPUT(DTYPE, TYPE) \
  73. case DTYPE: \
  74. (void)output_ptr->SetData(reinterpret_cast<uint8_t *>(y_data_##TYPE.data()), y_data_##TYPE.size() * length); \
  75. break;
  76. DEFINE_FUNC_BY_TYPE(int32_t)
  77. } // namespace
  78. Status FloorModKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  79. std::vector<GeTensorPtr> &v_output) {
  80. GELOGD("FloorModKernel in");
  81. if (op_desc_ptr == nullptr) {
  82. GELOGE(PARAM_INVALID, "Parameter's invalid, input opDescPtr is nullptr.");
  83. return PARAM_INVALID;
  84. }
  85. Status ret = FloorModCheck(input);
  86. if (ret != SUCCESS) {
  87. return ret;
  88. }
  89. std::vector<int32_t> y_data_int32_t;
  90. DataType data_type = input[kFloorModInputX]->GetTensorDesc().GetDataType();
  91. BCast bcast;
  92. switch (data_type) {
  93. SET_BCAST_COMPUTE_CASE(DT_INT32, int32_t)
  94. default:
  95. ret = NOT_CHANGED;
  96. break;
  97. }
  98. if (ret != SUCCESS) {
  99. GELOGW("BCastCompute fail, data_type: %s, ret: %s", TypeUtils::DataTypeToSerialString(data_type).c_str(),
  100. GET_ERRORNO_STR(ret).c_str());
  101. return NOT_CHANGED;
  102. }
  103. uint32_t length = 1;
  104. if (!TypeUtils::GetDataTypeLength(data_type, length)) {
  105. GELOGW("Can't GetDataTypeLength of data_type: %s", TypeUtils::DataTypeToSerialString(data_type).c_str());
  106. return NOT_CHANGED;
  107. }
  108. GeTensorPtr output_ptr = MakeShared<GeTensor>(op_desc_ptr->GetOutputDesc(kFloorModFirstOutput));
  109. if (output_ptr == nullptr) {
  110. GELOGW("make_shared ge::GeTensor failed, node name %s.", op_desc_ptr->GetName().c_str());
  111. return NOT_CHANGED;
  112. }
  113. output_ptr->MutableTensorDesc().SetShape(GeShape(bcast.GetOutputShape()));
  114. // only return GRAPH_SUCCESS here
  115. switch (data_type) {
  116. SET_OUTPUT(DT_INT32, int32_t)
  117. default:
  118. break;
  119. }
  120. output_ptr->MutableTensorDesc().SetDataType(data_type);
  121. v_output.push_back(output_ptr);
  122. GELOGD("FloorModKernel success");
  123. return SUCCESS;
  124. }
  125. Status FloorModKernel::FloorModCheck(const std::vector<ConstGeTensorPtr> &input) {
  126. // check input number
  127. if (input.size() != kFloorModInputNum) {
  128. GELOGI("The number of input for FloorMod must be %zu.", kFloorModInputNum);
  129. return NOT_CHANGED;
  130. }
  131. ConstGeTensorPtr input_x1 = input.at(kFloorModInputX);
  132. ConstGeTensorPtr input_x2 = input.at(kFloorModInputY);
  133. GE_CHECK_NOTNULL(input_x1);
  134. GE_CHECK_NOTNULL(input_x2);
  135. // check whether there is data in Tensor
  136. if (input_x1->GetData().size() == 0 || input_x2->GetData().size() == 0) {
  137. GELOGI("Check data size fail. x1: %zu, x2: %zu", input_x1->GetData().size(), input_x2->GetData().size());
  138. return NOT_CHANGED;
  139. }
  140. // check whether the data types are the same
  141. DataType type = input_x1->GetTensorDesc().GetDataType();
  142. if (type != input_x2->GetTensorDesc().GetDataType()) {
  143. GELOGI("Data type of inputs for FloorMod not matched.");
  144. return NOT_CHANGED;
  145. }
  146. // check if input data type is supported
  147. if (kFloorModSupportedType.find(type) == kFloorModSupportedType.end()) {
  148. GELOGI("FloorMod does not support this Data type: %s", TypeUtils::DataTypeToSerialString(type).c_str());
  149. return NOT_CHANGED;
  150. }
  151. return SUCCESS;
  152. }
  153. REGISTER_KERNEL(FLOORMOD, FloorModKernel);
  154. } // namespace ge

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