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

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