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

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