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.

range_kernel.cc 5.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/range_kernel.h"
  17. #include <memory>
  18. #include <set>
  19. #include "common/debug/log.h"
  20. #include "common/fp16_t.h"
  21. #include "common/types.h"
  22. #include "common/util.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "framework/common/ge_inner_error_codes.h"
  25. #include "graph/utils/type_utils.h"
  26. #include "inc/kernel_factory.h"
  27. namespace ge {
  28. namespace {
  29. constexpr size_t kRangeInputNum = 3;
  30. constexpr uint32_t kRangeDimNum = 0;
  31. const std::set<DataType> kRangeSupportedType = {DT_INT32, DT_FLOAT};
  32. } // namespace
  33. Status RangeKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  34. std::vector<GeTensorPtr> &v_output) {
  35. GELOGD("RangeKernel in");
  36. if (op_desc_ptr == nullptr) {
  37. GELOGE(PARAM_INVALID, "Parameter's invalid, input opDescPtr is nullptr.");
  38. return PARAM_INVALID;
  39. }
  40. Status ret = RangeCheck(input);
  41. if (ret != SUCCESS) {
  42. return ret;
  43. }
  44. GeTensorPtr output_ptr = MakeShared<GeTensor>(op_desc_ptr->GetOutputDesc(0));
  45. if (output_ptr == nullptr) {
  46. GELOGE(MEMALLOC_FAILED, "Make shared failed");
  47. return MEMALLOC_FAILED;
  48. }
  49. ConstGeTensorPtr start = input.at(0);
  50. ConstGeTensorPtr limit = input.at(1);
  51. ConstGeTensorPtr delta = input.at(2);
  52. DataType data_type = delta->GetTensorDesc().GetDataType();
  53. if (data_type == DT_FLOAT) {
  54. if (GetRange(*reinterpret_cast<const float *>(start->GetData().data()),
  55. *reinterpret_cast<const float *>(limit->GetData().data()),
  56. *reinterpret_cast<const float *>(delta->GetData().data()), output_ptr) != SUCCESS) {
  57. return PARAM_INVALID;
  58. }
  59. } else if (data_type == DT_INT32) {
  60. if (GetRange(*reinterpret_cast<const int32_t *>(start->GetData().data()),
  61. *reinterpret_cast<const int32_t *>(limit->GetData().data()),
  62. *reinterpret_cast<const int32_t *>(delta->GetData().data()), output_ptr) != SUCCESS) {
  63. return PARAM_INVALID;
  64. }
  65. }
  66. output_ptr->MutableTensorDesc().SetDataType(data_type);
  67. v_output.push_back(output_ptr);
  68. return SUCCESS;
  69. }
  70. Status RangeKernel::RangeCheck(const std::vector<ConstGeTensorPtr> &input) {
  71. // check input number
  72. if (input.size() != kRangeInputNum) {
  73. GELOGI("The number of input for Range must be %zu.", kRangeInputNum);
  74. return NOT_CHANGED;
  75. }
  76. ConstGeTensorPtr start = input.at(0);
  77. ConstGeTensorPtr limit = input.at(1);
  78. ConstGeTensorPtr delta = input.at(2);
  79. GE_CHECK_NOTNULL(start);
  80. GE_CHECK_NOTNULL(limit);
  81. GE_CHECK_NOTNULL(delta);
  82. // check whether there is data in Tensor
  83. if (start->GetData().size() == 0 || limit->GetData().size() == 0 || delta->GetData().size() == 0) {
  84. GELOGI("Check data size fail. start: %zu, limit: %zu, delta: %zu", start->GetData().size(), limit->GetData().size(),
  85. delta->GetData().size());
  86. return NOT_CHANGED;
  87. }
  88. // check whether the data types are the same
  89. DataType type = start->GetTensorDesc().GetDataType();
  90. if ((type != limit->GetTensorDesc().GetDataType()) || (type != delta->GetTensorDesc().GetDataType())) {
  91. GELOGI("Data type of inputs for Range not matched.");
  92. return NOT_CHANGED;
  93. }
  94. // check whether are all scalars
  95. size_t range_dim = static_cast<size_t>(kRangeDimNum);
  96. bool all_scalar = (start->GetTensorDesc().MutableShape().GetDimNum() == range_dim) &&
  97. (limit->GetTensorDesc().MutableShape().GetDimNum() == range_dim) &&
  98. (delta->GetTensorDesc().MutableShape().GetDimNum() == range_dim);
  99. if (!all_scalar) {
  100. GELOGI("Inputs for Range are not all scalars.");
  101. return NOT_CHANGED;
  102. }
  103. // check if input data type is supported
  104. if (kRangeSupportedType.find(type) == kRangeSupportedType.end()) {
  105. GELOGI("Range does not support this Data type: %s", TypeUtils::DataTypeToSerialString(type).c_str());
  106. return NOT_CHANGED;
  107. }
  108. return SUCCESS;
  109. }
  110. template <typename T>
  111. Status RangeKernel::GetRange(const T start, const T limit, const T delta, GeTensorPtr &output) {
  112. // check whether start, limit, delta is valid
  113. if (delta == 0) {
  114. GELOGE(PARAM_INVALID, "Requires delta != 0");
  115. return PARAM_INVALID;
  116. }
  117. if (start > limit && delta > 0) {
  118. GELOGE(PARAM_INVALID, "Requires start <= limit when delta > 0");
  119. return PARAM_INVALID;
  120. }
  121. if (start < limit && delta < 0) {
  122. GELOGE(PARAM_INVALID, "Requires start >= limit when delta < 0");
  123. return PARAM_INVALID;
  124. }
  125. int64_t size = (std::is_integral<T>::value ? ((std::abs(limit - start) + std::abs(delta) - 1) / std::abs(delta))
  126. : std::ceil(std::abs((limit - start) / delta)));
  127. output->MutableTensorDesc().SetShape(GeShape()); // when size is 0
  128. if (size > 0) {
  129. unique_ptr<T[]> buf(new (std::nothrow) T[size]);
  130. if (buf == nullptr) {
  131. GELOGE(MEMALLOC_FAILED, "New buf failed.");
  132. return MEMALLOC_FAILED;
  133. }
  134. T val = start;
  135. for (int64_t i = 0; i < size; ++i) {
  136. buf[i] = val;
  137. val += delta;
  138. }
  139. if (output->SetData(reinterpret_cast<uint8_t *>(buf.get()), size * sizeof(T)) != GRAPH_SUCCESS) {
  140. GELOGW("GetRange: SetData failed");
  141. }
  142. output->MutableTensorDesc().SetShape(GeShape({size}));
  143. }
  144. return SUCCESS;
  145. }
  146. REGISTER_KERNEL(RANGE, RangeKernel);
  147. } // namespace ge

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