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.

strided_slice_kernel.cc 13 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
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
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
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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. #include "host_kernels/strided_slice_kernel.h"
  14. #include <memory>
  15. #include "common/fp16_t.h"
  16. #include "common/ge_inner_error_codes.h"
  17. #include "common/math/math_util.h"
  18. #include "common/op/ge_op_utils.h"
  19. #include "external/graph/types.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "framework/common/types.h"
  22. #include "graph/utils/type_utils.h"
  23. #include "host_kernels/kernel_utils.h"
  24. #include "inc/kernel_factory.h"
  25. namespace ge {
  26. namespace {
  27. const int32_t kNumOne = 1;
  28. const size_t kStridedSliceInputSize = 4;
  29. const size_t kStridedSliceInputIndex = 0;
  30. const size_t kStridedSliceBeginIndex = 1;
  31. const size_t kStridedSliceEndIndex = 2;
  32. const size_t kStridedSliceStrideIndex = 3;
  33. const int32_t kDefaultStrideSize = 1;
  34. const std::set<DataType> kIndexNumberType = {DT_INT32, DT_INT64};
  35. bool IsEllipsisMaskValid(const GeTensorDescPtr &input_desc, const int ellipsis_mask) {
  36. if (ellipsis_mask != 0) {
  37. auto ellipsis_num = 0;
  38. auto input_shape = input_desc->GetShape();
  39. bool ellipsis_mask_flag = false;
  40. for (size_t i = 0; i < input_shape.GetDimNum(); i++) {
  41. uint32_t i_temp = static_cast<uint32_t>(i);
  42. ellipsis_mask_flag = (static_cast<uint32_t>(ellipsis_mask) & (1 << i_temp));
  43. if (ellipsis_mask_flag) {
  44. ++ellipsis_num;
  45. }
  46. if (ellipsis_num > 1) {
  47. GELOGW("Only one non-zero bit is allowed in ellipsis_mask.");
  48. return false;
  49. }
  50. }
  51. }
  52. return true;
  53. }
  54. } // namespace
  55. Status StridedSliceKernel::Compute(const ge::OpDescPtr attr, const std::vector<ge::ConstGeTensorPtr> &input,
  56. vector<ge::GeTensorPtr> &v_output) {
  57. GELOGD("StridedSliceKernel in.");
  58. // 1.Check input and attrs
  59. if (CheckAndGetAttr(attr) != SUCCESS) {
  60. GELOGW("Check and get attrs failed.Ignore kernel.");
  61. return NOT_CHANGED;
  62. }
  63. if (CheckInputParam(input) != SUCCESS) {
  64. GELOGW("Check input params failed.Ignore kernel.");
  65. return NOT_CHANGED;
  66. }
  67. // 2.Init param with mask attrs.
  68. std::vector<int64_t> input_dims;
  69. std::vector<int64_t> begin_vec;
  70. std::vector<int64_t> output_dims;
  71. std::vector<int64_t> stride_vec;
  72. if (InitParamWithAttrs(input, input_dims, begin_vec, output_dims, stride_vec) != SUCCESS) {
  73. GELOGW("Init param with mask attrs failed.Ignore kernel.");
  74. return NOT_CHANGED;
  75. }
  76. // 3.Set sliced data to output_ptr
  77. ConstGeTensorPtr weight0 = input[kStridedSliceInputIndex];
  78. auto data_type = weight0->GetTensorDesc().GetDataType();
  79. size_t data_size = weight0->GetData().size() / GetSizeByDataType(data_type);
  80. void *data = reinterpret_cast<void *>(const_cast<uint8_t *>(weight0->GetData().data()));
  81. GE_CHECK_NOTNULL(data);
  82. // Index 0 can always gets a GeTensorDesc object from any OpDescPtr.
  83. auto output_tensor_desc = attr->GetOutputDesc(0);
  84. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  85. if (output_ptr == nullptr) {
  86. GELOGE(MEMALLOC_FAILED, "MakeShared GeTensor failed, node name %s.", attr->GetName().c_str());
  87. return NOT_CHANGED;
  88. }
  89. auto ret = OpUtils::SetOutputSliceData(data, static_cast<int64_t>(data_size), data_type, input_dims, begin_vec,
  90. output_dims, output_ptr.get(), stride_vec);
  91. if (ret != SUCCESS) {
  92. GELOGE(INTERNAL_ERROR, "SetOutputSliceData failed.");
  93. return NOT_CHANGED;
  94. }
  95. // 4.Set output data_type and shape
  96. GeTensorDesc &t_d = output_ptr->MutableTensorDesc();
  97. t_d.SetDataType(static_cast<DataType>(data_type));
  98. auto final_dim_size = static_cast<uint32_t>(output_dims.size());
  99. vector<int64_t> v_dims;
  100. GetOutputDims(final_dim_size, output_dims, v_dims);
  101. t_d.SetShape(GeShape(v_dims));
  102. v_output.push_back(output_ptr);
  103. GELOGI("StridedSliceKernel success.");
  104. return SUCCESS;
  105. }
  106. Status StridedSliceKernel::CheckAndGetAttr(const OpDescPtr &attr) {
  107. if (attr == nullptr) {
  108. GELOGE(PARAM_INVALID, "input opdescptr is nullptr.");
  109. return PARAM_INVALID;
  110. }
  111. // Get all op attr value of strided_slice
  112. for (auto &attr_2_value : attr_value_map_) {
  113. if (!AttrUtils::GetInt(attr, attr_2_value.first, attr_2_value.second)) {
  114. GELOGE(PARAM_INVALID, "Get %s attr failed.", attr_2_value.first.c_str());
  115. return PARAM_INVALID;
  116. }
  117. }
  118. // Check ellipsis_mask is valid
  119. const auto &input_desc = attr->MutableInputDesc(kStridedSliceInputIndex);
  120. GE_CHECK_NOTNULL(input_desc);
  121. auto ellipsis_mask = attr_value_map_.at(STRIDE_SLICE_ATTR_ELLIPSIS_MASK);
  122. if (!IsEllipsisMaskValid(input_desc, ellipsis_mask)) {
  123. return PARAM_INVALID;
  124. }
  125. return SUCCESS;
  126. }
  127. Status StridedSliceKernel::CheckInputParam(const std::vector<ConstGeTensorPtr> &input) const {
  128. if (input.size() != kStridedSliceInputSize) {
  129. GELOGE(PARAM_INVALID, "The number of input for strided slice must be %zu.", kStridedSliceInputSize);
  130. return PARAM_INVALID;
  131. }
  132. ConstGeTensorPtr weight0 = input[kStridedSliceInputIndex];
  133. ConstGeTensorPtr begin_tensor = input[kStridedSliceBeginIndex];
  134. ConstGeTensorPtr end_tensor = input[kStridedSliceEndIndex];
  135. ConstGeTensorPtr stride_tensor = input[kStridedSliceStrideIndex];
  136. GE_CHECK_NOTNULL(weight0);
  137. GE_CHECK_NOTNULL(begin_tensor);
  138. GE_CHECK_NOTNULL(end_tensor);
  139. GE_CHECK_NOTNULL(stride_tensor);
  140. // check if begin,end,strides data type is supported
  141. auto begin_tensor_desc = begin_tensor->GetTensorDesc();
  142. auto end_tensor_desc = begin_tensor->GetTensorDesc();
  143. auto stride_tensor_desc = begin_tensor->GetTensorDesc();
  144. if (begin_tensor_desc.GetDataType() != end_tensor_desc.GetDataType() ||
  145. end_tensor_desc.GetDataType() != stride_tensor_desc.GetDataType()) {
  146. GELOGW("Data type of StridedSlice OP(begin,end,strides) must be same.");
  147. return PARAM_INVALID;
  148. }
  149. if (kIndexNumberType.find(begin_tensor_desc.GetDataType()) == kIndexNumberType.end()) {
  150. GELOGW("Data type of StridedSlice OP(begin,end,strides) must be int32 or int64.");
  151. return PARAM_INVALID;
  152. }
  153. // check data
  154. auto x_data_type = weight0->GetTensorDesc().GetDataType();
  155. auto x_data_size = GetSizeByDataType(x_data_type);
  156. if (x_data_size < 0) {
  157. GELOGW("Data type of x input %s is not supported.", TypeUtils::DataTypeToSerialString(x_data_type).c_str());
  158. return PARAM_INVALID;
  159. }
  160. size_t weight0_size = weight0->GetData().size() / x_data_size;
  161. size_t begin_data_size = begin_tensor->GetData().size() / sizeof(int32_t);
  162. size_t end_data_size = end_tensor->GetData().size() / sizeof(int32_t);
  163. size_t stride_data_size = stride_tensor->GetData().size() / sizeof(int32_t);
  164. if ((weight0_size == 0) || (begin_data_size == 0) || (end_data_size == 0) || (stride_data_size == 0)) {
  165. GELOGW("Data size of inputs is 0.");
  166. return PARAM_INVALID;
  167. }
  168. // check dim size
  169. if (!((begin_data_size == end_data_size) && (end_data_size == stride_data_size))) {
  170. GELOGW("The sizes of begin, end and stride is not supported.");
  171. return PARAM_INVALID;
  172. }
  173. return SUCCESS;
  174. }
  175. Status StridedSliceKernel::InitParamWithAttrs(const std::vector<ConstGeTensorPtr> &input,
  176. std::vector<int64_t> &input_dims, std::vector<int64_t> &begin_vec,
  177. std::vector<int64_t> &output_dims, std::vector<int64_t> &stride_vec) {
  178. ConstGeTensorPtr weight0 = input[kStridedSliceInputIndex];
  179. ConstGeTensorPtr begin_tensor = input[kStridedSliceBeginIndex];
  180. ConstGeTensorPtr end_tensor = input[kStridedSliceEndIndex];
  181. ConstGeTensorPtr stride_tensor = input[kStridedSliceStrideIndex];
  182. const GeShape x_shape = weight0->GetTensorDesc().GetShape();
  183. auto x_dims = x_shape.GetDims();
  184. auto x_dims_num = x_shape.GetDimNum();
  185. // handle new_axis_mask
  186. ExpandDimsWithNewAxis(begin_tensor, x_dims_num, x_dims);
  187. const int32_t *begin = reinterpret_cast<const int32_t *>(begin_tensor->GetData().data());
  188. const int32_t *end = reinterpret_cast<const int32_t *>(end_tensor->GetData().data());
  189. const int32_t *stride = reinterpret_cast<const int32_t *>(stride_tensor->GetData().data());
  190. auto begin_dim_num = begin_tensor->GetData().size() / sizeof(int32_t);
  191. auto min_dim = x_dims_num > begin_dim_num ? begin_dim_num : x_dims_num;
  192. for (size_t i = 0; i < x_dims.size(); ++i) {
  193. auto i_temp = static_cast<uint64_t>(i);
  194. bool new_axis_mask_flag =
  195. (static_cast<uint64_t>(attr_value_map_.at(STRIDE_SLICE_ATTR_NEW_AXIS_MASK)) & (1 << i_temp));
  196. if (new_axis_mask_flag) {
  197. output_dims.push_back(1);
  198. input_dims.push_back(1);
  199. begin_vec.push_back(0);
  200. stride_vec.push_back(1);
  201. continue;
  202. }
  203. int64_t begin_i = 0;
  204. int64_t end_i = 0;
  205. int64_t stride_i = 1;
  206. if (i < min_dim) {
  207. begin_i = begin[i];
  208. end_i = end[i];
  209. stride_i = stride[i];
  210. } else {
  211. begin_i = 0;
  212. end_i = x_dims.at(i);
  213. stride_i = 1;
  214. }
  215. GELOGD("Before mask calculate. Begin is : %d\t,end is : %d\t stride is : %d\t x_dim_i is : %d.", begin_i, end_i,
  216. stride_i, x_dims.at(i));
  217. auto ret = MaskCal(i, begin_i, end_i, x_dims.at(i));
  218. if (ret != SUCCESS) {
  219. GELOGW("MaskCal failed, because of data overflow.");
  220. return NOT_CHANGED;
  221. }
  222. int64_t dim_final;
  223. GELOGD("Before stride calculate. Begin is : %d\t,end is : %d\t stride is : %d\t x_dim_i is : %d.", begin_i, end_i,
  224. stride_i, x_dims.at(i));
  225. (void)StrideCal(x_dims.at(i), begin_i, end_i, stride_i, dim_final);
  226. output_dims.push_back(dim_final);
  227. input_dims.push_back(x_dims.at(i));
  228. begin_vec.push_back(begin_i);
  229. stride_vec.push_back(stride_i);
  230. }
  231. return SUCCESS;
  232. }
  233. void StridedSliceKernel::ExpandDimsWithNewAxis(const ConstGeTensorPtr &begin_tensor, const size_t x_dims_num,
  234. vector<int64_t> &x_dims) {
  235. auto begin_data_type_size = GetSizeByDataType(begin_tensor->GetTensorDesc().GetDataType());
  236. size_t begin_vec_size = begin_tensor->GetData().size() / begin_data_type_size;
  237. auto final_dim_num = x_dims_num < begin_vec_size ? begin_vec_size : x_dims_num;
  238. for (size_t i = 0; i < final_dim_num; i++) {
  239. auto i_temp = static_cast<uint64_t>(i);
  240. bool new_axis_mask_flag =
  241. (static_cast<uint64_t>(attr_value_map_.at(STRIDE_SLICE_ATTR_NEW_AXIS_MASK)) & (1 << i_temp));
  242. if (new_axis_mask_flag) {
  243. x_dims.insert(x_dims.begin() + i, 1);
  244. }
  245. }
  246. }
  247. Status StridedSliceKernel::MaskCal(const size_t i, int64_t &begin_i, int64_t &end_i, int64_t &dim_i) const {
  248. uint64_t i_temp = static_cast<uint64_t>(i);
  249. bool begin_mask_flag = (static_cast<uint64_t>(attr_value_map_.at(STRIDE_SLICE_ATTR_BEGIN_MASK)) & (1 << i_temp));
  250. bool end_mask_flag = (static_cast<uint64_t>(attr_value_map_.at(STRIDE_SLICE_ATTR_END_MASK)) & (1 << i_temp));
  251. bool ellipsis_mask_flag =
  252. (static_cast<uint64_t>(attr_value_map_.at(STRIDE_SLICE_ATTR_ELLIPSIS_MASK)) & (1 << i_temp));
  253. bool shrink_mask_flag =
  254. (static_cast<uint32_t>(attr_value_map_.at(STRIDE_SLICE_ATTR_SHRINK_AXIS_MASK)) & (1 << i_temp));
  255. if (shrink_mask_flag) {
  256. begin_i = (begin_i < 0 ? (dim_i + begin_i) : begin_i);
  257. FMK_INT32_ADDCHECK(begin_i, kNumOne)
  258. end_i = begin_i + kNumOne;
  259. } else {
  260. if (begin_mask_flag) {
  261. begin_i = 0;
  262. } else {
  263. begin_i = (begin_i < 0 ? (dim_i + begin_i) : begin_i);
  264. }
  265. if (end_mask_flag) {
  266. end_i = dim_i;
  267. } else {
  268. end_i = (end_i < 0 ? (dim_i + end_i) : end_i);
  269. }
  270. if (ellipsis_mask_flag) {
  271. begin_i = 0;
  272. end_i = dim_i;
  273. }
  274. }
  275. return SUCCESS;
  276. }
  277. Status StridedSliceKernel::StrideCal(const int64_t x_dims_i, int64_t &begin_i, int64_t &end_i, int64_t &stride_i,
  278. int64_t &dim_final) const {
  279. if (stride_i == 0) {
  280. stride_i = kDefaultStrideSize;
  281. } else if (stride_i < 0) {
  282. stride_i = -stride_i;
  283. begin_i = x_dims_i - begin_i - 1;
  284. end_i = x_dims_i - end_i - 1;
  285. }
  286. if (end_i > x_dims_i) {
  287. end_i = x_dims_i;
  288. }
  289. if ((begin_i == 0) && (end_i == 0)) {
  290. dim_final = x_dims_i;
  291. } else {
  292. dim_final = abs(end_i - begin_i) / stride_i;
  293. }
  294. return SUCCESS;
  295. }
  296. void StridedSliceKernel::GetOutputDims(uint32_t dims_size, const std::vector<int64_t> &output_dims,
  297. vector<int64_t> &v_dims) {
  298. for (uint32_t k = 0; k < dims_size; k++) {
  299. bool shrink_mask_i = (static_cast<uint32_t>(attr_value_map_.at(STRIDE_SLICE_ATTR_SHRINK_AXIS_MASK)) & (1 << k));
  300. if (shrink_mask_i) {
  301. continue;
  302. }
  303. v_dims.push_back(output_dims[k]);
  304. }
  305. }
  306. REGISTER_KERNEL(STRIDEDSLICE, StridedSliceKernel);
  307. } // namespace ge

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