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 16 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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/strided_slice_kernel.h"
  17. #include "common/fp16_t.h"
  18. #include "common/math/math_util.h"
  19. #include "framework/common/types.h"
  20. #include "graph/utils/type_utils.h"
  21. #include "host_kernels/kernel_utils.h"
  22. #include "inc/kernel_factory.h"
  23. namespace ge {
  24. namespace {
  25. const int32_t kNumOne = 1;
  26. const size_t kStridedSliceInputSize = 4;
  27. const size_t kStridedSliceInputIndex = 0;
  28. const size_t kStridedSliceBeginIndex = 1;
  29. const size_t kStridedSliceEndIndex = 2;
  30. const size_t kStridedSliceStrideIndex = 3;
  31. const int32_t kDefaultStrideSize = 1;
  32. const uint32_t kMaskBitLeftUnit = 1;
  33. const std::set<DataType> kIndexNumberType = {DT_INT32, DT_INT64};
  34. bool IsEllipsisMaskValid(const GeTensorDescPtr &input_desc, const uint32_t ellipsis_mask) {
  35. if (ellipsis_mask != 0) {
  36. auto ellipsis_num = 0;
  37. auto input_shape = input_desc->GetShape();
  38. for (size_t i = 0; i < input_shape.GetDimNum(); ++i) {
  39. auto i_temp = static_cast<uint32_t>(i);
  40. bool ellipsis_mask_flag = (ellipsis_mask) & (kMaskBitLeftUnit << i_temp);
  41. if (ellipsis_mask_flag) {
  42. ++ellipsis_num;
  43. }
  44. if (ellipsis_num > 1) {
  45. GELOGW("Only one non-zero bit is allowed in ellipsis_mask.");
  46. return false;
  47. }
  48. }
  49. }
  50. return true;
  51. }
  52. void GetOriginStrideVec(const std::vector<ge::ConstGeTensorPtr> &input, vector<int64_t> &orig_begin_vec,
  53. vector<int64_t> &orig_end_vec, vector<int64_t> &orig_stride_vec) {
  54. ConstGeTensorPtr begin_tensor = input[kStridedSliceBeginIndex];
  55. ConstGeTensorPtr end_tensor = input[kStridedSliceEndIndex];
  56. ConstGeTensorPtr stride_tensor = input[kStridedSliceStrideIndex];
  57. auto data_type = begin_tensor->GetTensorDesc().GetDataType();
  58. size_t vec_size = begin_tensor->GetData().size() / GetSizeByDataType(data_type);
  59. if (data_type == DT_INT32) {
  60. const int32_t *begin = reinterpret_cast<const int32_t *>(begin_tensor->GetData().data());
  61. const int32_t *end = reinterpret_cast<const int32_t *>(end_tensor->GetData().data());
  62. const int32_t *stride = reinterpret_cast<const int32_t *>(stride_tensor->GetData().data());
  63. for (size_t i = 0; i < vec_size; ++i) {
  64. orig_begin_vec.emplace_back(begin[i]);
  65. orig_end_vec.emplace_back(end[i]);
  66. orig_stride_vec.emplace_back(stride[i]);
  67. }
  68. } else {
  69. const int64_t *begin = reinterpret_cast<const int64_t *>(begin_tensor->GetData().data());
  70. const int64_t *end = reinterpret_cast<const int64_t *>(end_tensor->GetData().data());
  71. const int64_t *stride = reinterpret_cast<const int64_t *>(stride_tensor->GetData().data());
  72. for (size_t i = 0; i < vec_size; ++i) {
  73. orig_begin_vec.emplace_back(begin[i]);
  74. orig_end_vec.emplace_back(end[i]);
  75. orig_stride_vec.emplace_back(stride[i]);
  76. }
  77. }
  78. }
  79. } // namespace
  80. Status StridedSliceKernel::Compute(const ge::OpDescPtr attr, const std::vector<ge::ConstGeTensorPtr> &input,
  81. vector<ge::GeTensorPtr> &v_output) {
  82. GELOGD("StridedSliceKernel in.");
  83. // 1.Check input and attrs
  84. if (CheckAndGetAttr(attr) != SUCCESS) {
  85. GELOGW("Check and get attrs failed.Ignore kernel.");
  86. return NOT_CHANGED;
  87. }
  88. if (CheckInputParam(input) != SUCCESS) {
  89. GELOGW("Check input params failed.Ignore kernel.");
  90. return NOT_CHANGED;
  91. }
  92. // 2.Init param with mask attrs.
  93. std::vector<int64_t> input_dims;
  94. std::vector<int64_t> begin_vec;
  95. std::vector<int64_t> output_dims;
  96. std::vector<int64_t> stride_vec;
  97. if (InitParamWithAttrs(input, input_dims, begin_vec, output_dims, stride_vec) != SUCCESS) {
  98. GELOGW("Init param with mask attrs failed.Ignore kernel.");
  99. return NOT_CHANGED;
  100. }
  101. // 3.Set sliced data to output_ptr
  102. ConstGeTensorPtr weight0 = input[kStridedSliceInputIndex];
  103. auto data_type = weight0->GetTensorDesc().GetDataType();
  104. size_t data_size = weight0->GetData().size() / GetSizeByDataType(data_type);
  105. void *data = reinterpret_cast<void *>(const_cast<uint8_t *>(weight0->GetData().data()));
  106. GE_CHECK_NOTNULL(data);
  107. // Index 0 can always gets a GeTensorDesc object from any OpDescPtr.
  108. auto output_tensor_desc = attr->GetOutputDesc(0);
  109. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  110. if (output_ptr == nullptr) {
  111. GELOGE(MEMALLOC_FAILED, "MakeShared GeTensor failed, node name %s.", attr->GetName().c_str());
  112. return NOT_CHANGED;
  113. }
  114. auto ret = OpUtils::SetOutputSliceData(data, static_cast<int64_t>(data_size), data_type, input_dims, begin_vec,
  115. output_dims, output_ptr.get(), stride_vec);
  116. if (ret != SUCCESS) {
  117. GELOGE(INTERNAL_ERROR, "SetOutputSliceData failed.");
  118. return NOT_CHANGED;
  119. }
  120. // 4.Set output data_type and shape
  121. GeTensorDesc &t_d = output_ptr->MutableTensorDesc();
  122. t_d.SetDataType(static_cast<DataType>(data_type));
  123. auto final_dim_size = static_cast<uint32_t>(output_dims.size());
  124. vector<int64_t> v_dims;
  125. GetOutputDims(final_dim_size, output_dims, v_dims);
  126. t_d.SetShape(GeShape(v_dims));
  127. v_output.push_back(output_ptr);
  128. GELOGI("StridedSliceKernel success.");
  129. return SUCCESS;
  130. }
  131. Status StridedSliceKernel::CheckAndGetAttr(const OpDescPtr &attr) {
  132. if (attr == nullptr) {
  133. GELOGE(PARAM_INVALID, "input opdescptr is nullptr.");
  134. return PARAM_INVALID;
  135. }
  136. // Get all op attr value of strided_slice
  137. for (auto &attr_2_value : attr_value_map_) {
  138. if (!AttrUtils::GetInt(attr, attr_2_value.first, attr_2_value.second)) {
  139. GELOGE(PARAM_INVALID, "Get %s attr failed.", attr_2_value.first.c_str());
  140. return PARAM_INVALID;
  141. }
  142. }
  143. // Check ellipsis_mask is valid
  144. const auto &input_desc = attr->MutableInputDesc(kStridedSliceInputIndex);
  145. GE_CHECK_NOTNULL(input_desc);
  146. auto ellipsis_mask = attr_value_map_.at(STRIDE_SLICE_ATTR_ELLIPSIS_MASK);
  147. if (!IsEllipsisMaskValid(input_desc, ellipsis_mask)) {
  148. return PARAM_INVALID;
  149. }
  150. return SUCCESS;
  151. }
  152. Status StridedSliceKernel::CheckInputParam(const std::vector<ConstGeTensorPtr> &input) {
  153. if (input.size() != kStridedSliceInputSize) {
  154. GELOGE(PARAM_INVALID, "The number of input for strided slice must be %zu.", kStridedSliceInputSize);
  155. return PARAM_INVALID;
  156. }
  157. ConstGeTensorPtr weight0 = input[kStridedSliceInputIndex];
  158. ConstGeTensorPtr begin_tensor = input[kStridedSliceBeginIndex];
  159. ConstGeTensorPtr end_tensor = input[kStridedSliceEndIndex];
  160. ConstGeTensorPtr stride_tensor = input[kStridedSliceStrideIndex];
  161. GE_CHECK_NOTNULL(weight0);
  162. GE_CHECK_NOTNULL(begin_tensor);
  163. GE_CHECK_NOTNULL(end_tensor);
  164. GE_CHECK_NOTNULL(stride_tensor);
  165. // check if begin,end,strides data type is supported
  166. auto begin_tensor_desc = begin_tensor->GetTensorDesc();
  167. auto end_tensor_desc = begin_tensor->GetTensorDesc();
  168. auto stride_tensor_desc = begin_tensor->GetTensorDesc();
  169. if (begin_tensor_desc.GetDataType() != end_tensor_desc.GetDataType() ||
  170. end_tensor_desc.GetDataType() != stride_tensor_desc.GetDataType()) {
  171. GELOGW("Data type of StridedSlice OP(begin,end,strides) must be same.");
  172. return PARAM_INVALID;
  173. }
  174. if (kIndexNumberType.find(begin_tensor_desc.GetDataType()) == kIndexNumberType.end()) {
  175. GELOGW("Data type of StridedSlice OP(begin,end,strides) must be int32 or int64.");
  176. return PARAM_INVALID;
  177. }
  178. // check data
  179. auto x_data_type = weight0->GetTensorDesc().GetDataType();
  180. auto x_data_size = GetSizeByDataType(x_data_type);
  181. if (x_data_size < 0) {
  182. GELOGW("Data type of x input %s is not supported.", TypeUtils::DataTypeToSerialString(x_data_type).c_str());
  183. return PARAM_INVALID;
  184. }
  185. size_t weight0_size = weight0->GetData().size() / x_data_size;
  186. size_t begin_data_size = begin_tensor->GetData().size();
  187. size_t end_data_size = end_tensor->GetData().size();
  188. size_t stride_data_size = stride_tensor->GetData().size();
  189. if ((weight0_size == 0) || (begin_data_size == 0) || (end_data_size == 0) || (stride_data_size == 0)) {
  190. GELOGW("Data size of inputs is 0.");
  191. return PARAM_INVALID;
  192. }
  193. // check dim size
  194. if (!((begin_data_size == end_data_size) && (end_data_size == stride_data_size))) {
  195. GELOGW("The sizes of begin, end and stride is not supported.");
  196. return PARAM_INVALID;
  197. }
  198. return SUCCESS;
  199. }
  200. Status StridedSliceKernel::InitParamWithAttrs(const std::vector<ConstGeTensorPtr> &input,
  201. std::vector<int64_t> &input_dims, std::vector<int64_t> &begin_vec,
  202. std::vector<int64_t> &output_dims, std::vector<int64_t> &stride_vec) {
  203. ConstGeTensorPtr weight0 = input[kStridedSliceInputIndex];
  204. ConstGeTensorPtr begin_tensor = input[kStridedSliceBeginIndex];
  205. const GeShape x_shape = weight0->GetTensorDesc().GetShape();
  206. auto x_dims = x_shape.GetDims();
  207. auto x_dims_num = x_shape.GetDimNum();
  208. // handle new_axis_mask
  209. ExpandDimsWithNewAxis(begin_tensor, x_dims_num, x_dims);
  210. vector<int64_t> orig_begin_vec, orig_end_vec, orig_stride_vec;
  211. GetOriginStrideVec(input, orig_begin_vec, orig_end_vec, orig_stride_vec);
  212. // calculate begin_mask & end_mask by ellipsis_mask
  213. ExpandStrideWithEllipsisMask(x_dims_num, x_dims, orig_begin_vec, orig_end_vec, orig_stride_vec);
  214. auto begin_dim_num = orig_begin_vec.size();
  215. auto min_dim = x_dims_num > begin_dim_num ? begin_dim_num : x_dims_num;
  216. for (size_t i = 0; i < x_dims.size(); ++i) {
  217. auto i_temp = static_cast<uint32_t>(i);
  218. bool new_axis_mask_flag = (attr_value_map_.at(STRIDE_SLICE_ATTR_NEW_AXIS_MASK) & (kMaskBitLeftUnit << i_temp));
  219. if (new_axis_mask_flag) {
  220. output_dims.push_back(1);
  221. input_dims.push_back(1);
  222. begin_vec.push_back(0);
  223. stride_vec.push_back(1);
  224. continue;
  225. }
  226. int64_t begin_i = 0;
  227. int64_t end_i = 0;
  228. int64_t stride_i = 1;
  229. if (i < min_dim) {
  230. begin_i = orig_begin_vec[i];
  231. end_i = orig_end_vec[i];
  232. stride_i = orig_stride_vec[i];
  233. } else {
  234. begin_i = 0;
  235. end_i = x_dims.at(i);
  236. stride_i = 1;
  237. }
  238. 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,
  239. stride_i, x_dims.at(i));
  240. auto ret = MaskCal(i, begin_i, end_i, x_dims.at(i));
  241. if (ret != SUCCESS) {
  242. GELOGW("MaskCal failed, because of data overflow.");
  243. return NOT_CHANGED;
  244. }
  245. int64_t dim_final;
  246. 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,
  247. stride_i, x_dims.at(i));
  248. (void) StrideCal(x_dims.at(i), begin_i, end_i, stride_i, dim_final);
  249. output_dims.push_back(dim_final);
  250. input_dims.push_back(x_dims.at(i));
  251. begin_vec.push_back(begin_i);
  252. stride_vec.push_back(stride_i);
  253. }
  254. return SUCCESS;
  255. }
  256. void StridedSliceKernel::ExpandDimsWithNewAxis(const ConstGeTensorPtr &begin_tensor, const size_t x_dims_num,
  257. vector<int64_t> &x_dims) {
  258. auto begin_data_type_size = GetSizeByDataType(begin_tensor->GetTensorDesc().GetDataType());
  259. size_t begin_vec_size = begin_tensor->GetData().size() / begin_data_type_size;
  260. auto final_dim_num = x_dims_num < begin_vec_size ? begin_vec_size : x_dims_num;
  261. for (size_t i = 0; i < final_dim_num; i++) {
  262. auto i_temp = static_cast<uint32_t>(i);
  263. bool new_axis_mask_flag = (attr_value_map_.at(STRIDE_SLICE_ATTR_NEW_AXIS_MASK) & (kMaskBitLeftUnit << i_temp));
  264. if (new_axis_mask_flag) {
  265. x_dims.insert(x_dims.begin() + i, 1);
  266. }
  267. }
  268. }
  269. void StridedSliceKernel::ExpandStrideWithEllipsisMask(const size_t x_dims_num,
  270. const vector<int64_t> &x_dims, vector<int64_t> &orig_begin_vec,
  271. vector<int64_t> &orig_end_vec, vector<int64_t> &orig_stride_vec) {
  272. if (attr_value_map_.at(STRIDE_SLICE_ATTR_ELLIPSIS_MASK) != 0) {
  273. auto end_mask = attr_value_map_.at(STRIDE_SLICE_ATTR_END_MASK);
  274. auto begin_mask = attr_value_map_.at(STRIDE_SLICE_ATTR_BEGIN_MASK);
  275. if (begin_mask != 0 && x_dims_num != orig_begin_vec.size()) {
  276. begin_mask *= begin_mask * (kMaskBitLeftUnit << (x_dims_num - orig_begin_vec.size() - 1));
  277. attr_value_map_.at(STRIDE_SLICE_ATTR_BEGIN_MASK) = begin_mask;
  278. }
  279. if (end_mask != 0 && x_dims_num != orig_end_vec.size()) {
  280. end_mask *= end_mask * (kMaskBitLeftUnit << (x_dims_num - orig_end_vec.size() - 1));
  281. attr_value_map_.at(STRIDE_SLICE_ATTR_END_MASK) = end_mask;
  282. }
  283. for (size_t i = 0; i < x_dims_num; ++i) {
  284. bool ellipsis_mask_flag = attr_value_map_.at(STRIDE_SLICE_ATTR_ELLIPSIS_MASK) & (kMaskBitLeftUnit << i);
  285. if (ellipsis_mask_flag) {
  286. auto ellipsis_dim = i;
  287. orig_begin_vec[i] = 0;
  288. orig_end_vec[i] = x_dims.at(i);
  289. orig_stride_vec[i] = 1;
  290. if (orig_begin_vec.size() < x_dims_num) {
  291. for (size_t j = 1; j < (x_dims_num - orig_begin_vec.size() + 1); ++j) {
  292. orig_begin_vec.insert((orig_begin_vec.begin() + ellipsis_dim + j), 0);
  293. orig_end_vec.insert((orig_end_vec.begin() + ellipsis_dim + j), x_dims.at(ellipsis_dim +j));
  294. orig_stride_vec.insert((orig_stride_vec.begin() + ellipsis_dim + j), 1);
  295. }
  296. }
  297. }
  298. }
  299. }
  300. }
  301. Status StridedSliceKernel::MaskCal(const size_t i, int64_t &begin_i, int64_t &end_i, int64_t &dim_i) const {
  302. auto i_temp = static_cast<uint32_t>(i);
  303. bool begin_mask_flag = (attr_value_map_.at(STRIDE_SLICE_ATTR_BEGIN_MASK) & (kMaskBitLeftUnit << i_temp));
  304. bool end_mask_flag = (attr_value_map_.at(STRIDE_SLICE_ATTR_END_MASK) & (kMaskBitLeftUnit << i_temp));
  305. bool shrink_mask_flag = (attr_value_map_.at(STRIDE_SLICE_ATTR_SHRINK_AXIS_MASK) & (kMaskBitLeftUnit << i_temp));
  306. if (shrink_mask_flag) {
  307. begin_i = (begin_i < 0 ? (dim_i + begin_i) : begin_i);
  308. FMK_INT32_ADDCHECK(begin_i, kNumOne)
  309. end_i = begin_i + kNumOne;
  310. } else {
  311. if (begin_mask_flag) {
  312. begin_i = 0;
  313. } else {
  314. begin_i = (begin_i < 0 ? (dim_i + begin_i) : begin_i);
  315. }
  316. if (end_mask_flag) {
  317. end_i = dim_i;
  318. } else {
  319. end_i = (end_i < 0 ? (dim_i + end_i) : end_i);
  320. }
  321. }
  322. return SUCCESS;
  323. }
  324. Status StridedSliceKernel::StrideCal(const int64_t x_dims_i, int64_t &begin_i, int64_t &end_i, int64_t &stride_i,
  325. int64_t &dim_final) {
  326. if (stride_i == 0) {
  327. stride_i = kDefaultStrideSize;
  328. } else if (stride_i < 0) {
  329. stride_i = -stride_i;
  330. if (begin_i < 0 && end_i < 0) {
  331. begin_i = x_dims_i - begin_i - 1;
  332. end_i = x_dims_i - end_i - 1;
  333. }
  334. }
  335. if (end_i > x_dims_i) {
  336. end_i = x_dims_i;
  337. }
  338. if ((begin_i == 0) && (end_i == 0)) {
  339. dim_final = x_dims_i;
  340. } else {
  341. dim_final = abs(end_i - begin_i) / stride_i;
  342. }
  343. return SUCCESS;
  344. }
  345. void StridedSliceKernel::GetOutputDims(uint32_t dims_size, const std::vector<int64_t> &output_dims,
  346. vector<int64_t> &v_dims) {
  347. for (uint32_t k = 0; k < dims_size; k++) {
  348. bool shrink_mask_i = (attr_value_map_.at(STRIDE_SLICE_ATTR_SHRINK_AXIS_MASK) & (kMaskBitLeftUnit << k));
  349. if (shrink_mask_i) {
  350. continue;
  351. }
  352. v_dims.push_back(output_dims[k]);
  353. }
  354. }
  355. REGISTER_KERNEL(STRIDEDSLICE, StridedSliceKernel);
  356. } // namespace ge

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