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.

trans.cc 41 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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 "common/trans.h"
  17. #include <functional>
  18. #include <numeric>
  19. #include <utility>
  20. #include "utils/ms_utils.h"
  21. #include "backend/session/anf_runtime_algorithm.h"
  22. #include "backend/kernel_compiler/kernel.h"
  23. #include "runtime/device/convert_tensor_utils.h"
  24. #include "utils/convert_utils.h"
  25. #include "utils/log_adapter.h"
  26. #include "utils/utils.h"
  27. namespace mindspore {
  28. namespace trans {
  29. enum kAxis : int { kN = 0, kC, kH, kW, kNchwDims, kNdhwc };
  30. const std::map<TypeId, size_t> type_map = {{kNumberTypeBool, 1}, {kNumberTypeInt, 4}, {kNumberTypeInt8, 1},
  31. {kNumberTypeInt16, 2}, {kNumberTypeInt32, 4}, {kNumberTypeInt64, 8},
  32. {kNumberTypeUInt, 4}, {kNumberTypeUInt8, 1}, {kNumberTypeUInt16, 2},
  33. {kNumberTypeUInt32, 4}, {kNumberTypeUInt64, 8}, {kNumberTypeFloat, 4},
  34. {kNumberTypeFloat16, 2}, {kNumberTypeFloat32, 4}, {kNumberTypeFloat64, 8}};
  35. inline void SetData(size_t size, bool pad_zero, size_t src_idx, size_t dst_idx, const FormatArgs &args, void *result) {
  36. switch (size) {
  37. case 1:
  38. static_cast<uint8_t *>(result)[dst_idx] = pad_zero ? 0 : static_cast<const uint8_t *>(args.data)[src_idx];
  39. break;
  40. case 2:
  41. static_cast<uint16_t *>(result)[dst_idx] = pad_zero ? 0 : static_cast<const uint16_t *>(args.data)[src_idx];
  42. break;
  43. case 4:
  44. static_cast<uint32_t *>(result)[dst_idx] = pad_zero ? 0 : static_cast<const uint32_t *>(args.data)[src_idx];
  45. break;
  46. case 8:
  47. static_cast<uint64_t *>(result)[dst_idx] = pad_zero ? 0 : static_cast<const uint64_t *>(args.data)[src_idx];
  48. break;
  49. default:
  50. MS_LOG(EXCEPTION) << "Trans data not support size " << size;
  51. }
  52. }
  53. template <typename T>
  54. T DivCeil(T n1, T n2) {
  55. if (n2 != 0) {
  56. return (n1 + n2 - 1) / n2;
  57. }
  58. return 0;
  59. }
  60. enum DataTypeTransMode {
  61. FROM_FLOAT_TO_FLOAT16,
  62. FROM_FLOAT_TO_INT32,
  63. FROM_FLOAT16_TO_FLOAT,
  64. FROM_FLOAT16_TO_INT32,
  65. FROM_FLOAT16_TO_UINT8,
  66. FROM_INT32_TO_FLOAT,
  67. FROM_INT32_TO_FLOAT16,
  68. FROM_INT32_TO_UINT8,
  69. FROM_INT32_TO_INT8,
  70. FROM_INT32_TO_BOOL,
  71. FROM_UINT8_TO_FLOAT,
  72. FROM_UINT8_TO_INT32,
  73. FROM_UINT8_TO_FLOAT16,
  74. FROM_INT8_TO_FLOAT,
  75. FROM_INT8_TO_FLOAT16,
  76. FROM_INT8_TO_INT32,
  77. FROM_INT64_TO_INT32,
  78. FROM_UINT16_TO_INT32,
  79. FROM_BOOL_TO_FLOAT,
  80. FROM_BOOL_TO_INT32,
  81. FROM_BOOL_TO_UINT8,
  82. FROM_BOOL_TO_FLOAT16,
  83. FROM_FLOAT64_TO_FLOAT32,
  84. FROM_FLOAT32_TO_FLOAT64
  85. };
  86. const std::map<std::pair<TypeId, TypeId>, DataTypeTransMode> mode_map{
  87. {std::pair<TypeId, TypeId>(kNumberTypeFloat64, kNumberTypeFloat32), FROM_FLOAT64_TO_FLOAT32},
  88. {std::pair<TypeId, TypeId>(kNumberTypeFloat32, kNumberTypeFloat64), FROM_FLOAT32_TO_FLOAT64},
  89. {std::pair<TypeId, TypeId>(kNumberTypeFloat32, kNumberTypeFloat16), FROM_FLOAT_TO_FLOAT16},
  90. {std::pair<TypeId, TypeId>(kNumberTypeFloat32, kNumberTypeInt32), FROM_FLOAT_TO_INT32},
  91. {std::pair<TypeId, TypeId>(kNumberTypeFloat16, kNumberTypeFloat32), FROM_FLOAT16_TO_FLOAT},
  92. {std::pair<TypeId, TypeId>(kNumberTypeFloat16, kNumberTypeInt32), FROM_FLOAT16_TO_INT32},
  93. {std::pair<TypeId, TypeId>(kNumberTypeFloat16, kNumberTypeUInt8), FROM_FLOAT16_TO_UINT8},
  94. {std::pair<TypeId, TypeId>(kNumberTypeInt32, kNumberTypeFloat32), FROM_INT32_TO_FLOAT},
  95. {std::pair<TypeId, TypeId>(kNumberTypeInt32, kNumberTypeFloat16), FROM_INT32_TO_FLOAT16},
  96. {std::pair<TypeId, TypeId>(kNumberTypeInt32, kNumberTypeUInt8), FROM_INT32_TO_UINT8},
  97. {std::pair<TypeId, TypeId>(kNumberTypeInt32, kNumberTypeInt8), FROM_INT32_TO_INT8},
  98. {std::pair<TypeId, TypeId>(kNumberTypeInt32, kNumberTypeBool), FROM_INT32_TO_BOOL},
  99. {std::pair<TypeId, TypeId>(kNumberTypeUInt8, kNumberTypeFloat32), FROM_UINT8_TO_FLOAT},
  100. {std::pair<TypeId, TypeId>(kNumberTypeUInt8, kNumberTypeInt32), FROM_UINT8_TO_INT32},
  101. {std::pair<TypeId, TypeId>(kNumberTypeUInt8, kNumberTypeFloat16), FROM_UINT8_TO_FLOAT16},
  102. {std::pair<TypeId, TypeId>(kNumberTypeInt8, kNumberTypeFloat32), FROM_INT8_TO_FLOAT},
  103. {std::pair<TypeId, TypeId>(kNumberTypeInt8, kNumberTypeFloat16), FROM_INT8_TO_FLOAT16},
  104. {std::pair<TypeId, TypeId>(kNumberTypeInt8, kNumberTypeInt32), FROM_INT8_TO_INT32},
  105. {std::pair<TypeId, TypeId>(kNumberTypeInt64, kNumberTypeInt32), FROM_INT64_TO_INT32},
  106. {std::pair<TypeId, TypeId>(kNumberTypeUInt16, kNumberTypeInt32), FROM_UINT16_TO_INT32},
  107. {std::pair<TypeId, TypeId>(kNumberTypeBool, kNumberTypeInt32), FROM_BOOL_TO_INT32},
  108. {std::pair<TypeId, TypeId>(kNumberTypeBool, kNumberTypeFloat), FROM_BOOL_TO_FLOAT},
  109. {std::pair<TypeId, TypeId>(kNumberTypeBool, kNumberTypeUInt8), FROM_BOOL_TO_UINT8},
  110. {std::pair<TypeId, TypeId>(kNumberTypeBool, kNumberTypeFloat16), FROM_BOOL_TO_FLOAT16}};
  111. void CheckMemSize(const TypeIdArgs &args) {
  112. auto src_type_size = TypeIdSize(args.host_data_type);
  113. auto dst_type_size = TypeIdSize(args.device_data_type);
  114. if (src_type_size < 1 || dst_type_size < 1) {
  115. MS_LOG(EXCEPTION) << "Invalid src or dst data type.";
  116. }
  117. if (args.data_size / src_type_size != args.host_shape_size) {
  118. MS_LOG(EXCEPTION) << "Invalid src or dst data size.";
  119. }
  120. }
  121. template <typename SrcT, typename DstT>
  122. void TransDataSrc2Dst(const TypeIdArgs &args, void *dst, const size_t data_size) {
  123. CheckMemSize(args);
  124. for (size_t idx = 0; idx != data_size; idx++) {
  125. SrcT src_data = static_cast<const SrcT *>(args.data)[idx];
  126. static_cast<DstT *>(dst)[idx] = static_cast<DstT>(src_data);
  127. }
  128. }
  129. template <typename SrcT>
  130. void TransDataSrc2Fp16(const TypeIdArgs &args, void *dst, const size_t data_size) {
  131. CheckMemSize(args);
  132. auto src_data = static_cast<const SrcT *>(args.data);
  133. auto half_data = static_cast<float16 *>(dst);
  134. for (size_t i = 0; i < data_size; i++) {
  135. half_data[i] = float16(src_data[i]);
  136. }
  137. }
  138. bool CastKernel(const TypeIdArgs &args, void *dst, const size_t data_size, const DataTypeTransMode mode) {
  139. using DtypeKernel = std::function<void(const TypeIdArgs &, void *, const size_t)>;
  140. const std::map<DataTypeTransMode, DtypeKernel> cast_kernel_map{
  141. {FROM_FLOAT_TO_INT32, TransDataSrc2Dst<float, int32_t>},
  142. {FROM_FLOAT64_TO_FLOAT32, TransDataSrc2Dst<double, float>},
  143. {FROM_FLOAT32_TO_FLOAT64, TransDataSrc2Dst<float, double>},
  144. {FROM_FLOAT16_TO_INT32, TransDataSrc2Dst<float16, int32_t>},
  145. {FROM_FLOAT16_TO_UINT8, TransDataSrc2Dst<float16, uint8_t>},
  146. {FROM_INT32_TO_FLOAT, TransDataSrc2Dst<int32_t, float>},
  147. {FROM_INT32_TO_INT8, TransDataSrc2Dst<int32_t, int8_t>},
  148. {FROM_INT32_TO_UINT8, TransDataSrc2Dst<int32_t, uint8_t>},
  149. {FROM_INT32_TO_BOOL, TransDataSrc2Dst<int32_t, int8_t>},
  150. {FROM_INT32_TO_FLOAT16, TransDataSrc2Fp16<int32_t>},
  151. {FROM_UINT8_TO_FLOAT, TransDataSrc2Dst<uint8_t, float>},
  152. {FROM_UINT8_TO_INT32, TransDataSrc2Dst<uint8_t, int32_t>},
  153. {FROM_UINT8_TO_FLOAT16, TransDataSrc2Fp16<uint8_t>},
  154. {FROM_INT8_TO_FLOAT, TransDataSrc2Dst<int8_t, float>},
  155. {FROM_INT8_TO_FLOAT16, TransDataSrc2Fp16<int8_t>},
  156. {FROM_INT8_TO_INT32, TransDataSrc2Dst<int8_t, int32_t>},
  157. {FROM_INT64_TO_INT32, TransDataSrc2Dst<int64_t, int32_t>},
  158. {FROM_UINT16_TO_INT32, TransDataSrc2Dst<uint16_t, int32_t>},
  159. {FROM_BOOL_TO_INT32, TransDataSrc2Dst<int8_t, int32_t>},
  160. {FROM_BOOL_TO_FLOAT, TransDataSrc2Dst<int8_t, float>},
  161. {FROM_BOOL_TO_UINT8, TransDataSrc2Dst<int8_t, uint8_t>},
  162. {FROM_BOOL_TO_FLOAT16, TransDataSrc2Fp16<int8_t>}};
  163. if (mode == FROM_FLOAT_TO_FLOAT16) {
  164. device::FloatToHalf(dst, args.data, data_size);
  165. return true;
  166. } else if (mode == FROM_FLOAT16_TO_FLOAT) {
  167. device::HalfToFloat(dst, args.data, data_size);
  168. return true;
  169. }
  170. auto iter = cast_kernel_map.find(mode);
  171. if (iter != cast_kernel_map.end()) {
  172. iter->second(args, dst, data_size);
  173. return true;
  174. } else {
  175. MS_LOG(ERROR) << "Unsupported datatype trans";
  176. return false;
  177. }
  178. }
  179. size_t CubeSizeByType(const TypeId data_type) {
  180. const size_t default_error = 0;
  181. auto dt_size = TypeIdSize(data_type);
  182. if (dt_size < 1) {
  183. MS_LOG(ERROR) << "Illegal dtype.";
  184. return default_error;
  185. } else if (dt_size == 1) {
  186. return kCubeSize * 2;
  187. }
  188. return kCubeSize;
  189. }
  190. size_t ShapeSize(const std::vector<size_t> &shape) {
  191. return std::accumulate(shape.begin(), shape.end(), IntToSize(1), std::multiplies<size_t>());
  192. }
  193. size_t TypeIdSize(const TypeId data_type) {
  194. const size_t unsupported_type_error = 0;
  195. auto iter = type_map.find(data_type);
  196. if (iter != type_map.end()) {
  197. return iter->second;
  198. }
  199. return unsupported_type_error;
  200. }
  201. namespace {
  202. bool CheckDims(const std::vector<size_t> &shape) {
  203. if (shape.size() != kNchwDims) {
  204. MS_LOG(ERROR) << "Host shape dims shoud be 4";
  205. return false;
  206. }
  207. return true;
  208. }
  209. std::vector<size_t> NchwDeviceShape(const std::vector<size_t> &shape) {
  210. if (!CheckDims(shape)) {
  211. MS_LOG(EXCEPTION) << "Check dims failed.";
  212. }
  213. return shape;
  214. }
  215. std::vector<size_t> NhwcDeviceShape(const std::vector<size_t> &shape) {
  216. if (!CheckDims(shape)) {
  217. MS_LOG(EXCEPTION) << "Ccheck dims failed.";
  218. }
  219. std::vector<size_t> device_shape;
  220. device_shape.push_back(shape[kN]);
  221. device_shape.push_back(shape[kH]);
  222. device_shape.push_back(shape[kW]);
  223. device_shape.push_back(shape[kC]);
  224. return device_shape;
  225. }
  226. std::vector<size_t> HwchDeviceShape(const std::vector<size_t> &shape) {
  227. if (!CheckDims(shape)) {
  228. MS_LOG(EXCEPTION) << "Check dims failed.";
  229. }
  230. std::vector<size_t> device_shape;
  231. device_shape.push_back(shape[kH]);
  232. device_shape.push_back(shape[kW]);
  233. device_shape.push_back(shape[kC]);
  234. device_shape.push_back(shape[kN]);
  235. return device_shape;
  236. }
  237. std::vector<size_t> FracZDeviceShape(const std::vector<size_t> &shape) {
  238. if (!CheckDims(shape)) {
  239. MS_LOG(EXCEPTION) << "Check dims failed.";
  240. }
  241. std::vector<size_t> device_shape;
  242. const size_t cout16 = ((shape[kN] + kCubeSize - 1) / kCubeSize) * kCubeSize;
  243. const size_t cin16 = ((shape[kC] + kCubeSize - 1) / kCubeSize) * kCubeSize;
  244. device_shape.push_back(shape[kH] * shape[kW] * cin16 / kCubeSize);
  245. device_shape.push_back(cout16 / kCubeSize);
  246. device_shape.push_back(kCubeSize);
  247. device_shape.push_back(kCubeSize);
  248. return device_shape;
  249. }
  250. std::vector<size_t> Nc1hwc0DeviceShape(const std::vector<size_t> &shape) {
  251. if (!CheckDims(shape)) {
  252. MS_LOG(EXCEPTION) << "Check dims failed.";
  253. }
  254. std::vector<size_t> device_shape;
  255. const size_t C1 = (shape[kC] + kCubeSize - 1) / kCubeSize;
  256. const size_t C0 = kCubeSize;
  257. device_shape.push_back(shape[kN]);
  258. device_shape.push_back(C1);
  259. device_shape.push_back(shape[kH]);
  260. device_shape.push_back(shape[kW]);
  261. device_shape.push_back(C0);
  262. return device_shape;
  263. }
  264. std::vector<size_t> C1hwncoc0DeviceShape(const std::vector<size_t> &shape) {
  265. if (!CheckDims(shape)) {
  266. MS_LOG(EXCEPTION) << "Check dims failed.";
  267. }
  268. std::vector<size_t> device_shape;
  269. device_shape.push_back((shape[kC] - 1) / kCubeSize + 1);
  270. device_shape.push_back(shape[kH]);
  271. device_shape.push_back(shape[kW]);
  272. device_shape.push_back(shape[kN]);
  273. device_shape.push_back(kCubeSize);
  274. device_shape.push_back(kCubeSize);
  275. return device_shape;
  276. }
  277. std::vector<size_t> FracZc04DeviceShape(const std::vector<size_t> &shape) {
  278. if (!CheckDims(shape)) {
  279. MS_LOG(EXCEPTION) << "Check dims failed.";
  280. }
  281. std::vector<size_t> device_shape;
  282. const size_t c0 = 4;
  283. auto first_dim = DivCeil(c0 * shape[kH] * shape[kW], kCubeSize);
  284. auto no = DivCeil(shape.at(kN), kCubeSize);
  285. device_shape.push_back(first_dim);
  286. device_shape.push_back(no);
  287. device_shape.push_back(kCubeSize);
  288. device_shape.push_back(kCubeSize);
  289. return device_shape;
  290. }
  291. std::vector<size_t> Nc1hwc04DeviceShape(const std::vector<size_t> &shape) {
  292. if (!CheckDims(shape)) {
  293. MS_LOG(EXCEPTION) << "Check dims failed.";
  294. }
  295. std::vector<size_t> device_shape;
  296. const size_t C1 = 1;
  297. const size_t C0 = 4;
  298. device_shape.push_back(shape[kN]);
  299. device_shape.push_back(C1);
  300. device_shape.push_back(shape[kH]);
  301. device_shape.push_back(shape[kW]);
  302. device_shape.push_back(C0);
  303. return device_shape;
  304. }
  305. std::vector<size_t> NdhwcDeviceShape(const std::vector<size_t> &shape) {
  306. if (shape.size() < kNdhwc) {
  307. MS_LOG(EXCEPTION) << "Shape dims must be 5 when format is ndhwc.";
  308. }
  309. return shape;
  310. }
  311. std::vector<size_t> PaddingShapeTo4dByDefault(const std::vector<size_t> &shape) {
  312. std::vector<size_t> shape_4d(kNchwDims, 1);
  313. switch (shape.size()) {
  314. case 0:
  315. return shape_4d;
  316. case 1:
  317. shape_4d[kC] = shape[kN];
  318. break;
  319. case 2:
  320. shape_4d[kC] = shape[kN];
  321. shape_4d[kH] = shape[kC];
  322. break;
  323. case 3:
  324. shape_4d[kC] = shape[kN];
  325. shape_4d[kH] = shape[kC];
  326. shape_4d[kW] = shape[kH];
  327. break;
  328. case 4:
  329. std::copy(shape.begin(), shape.end(), shape_4d.begin());
  330. break;
  331. default:
  332. MS_LOG(EXCEPTION) << "Unexpect shape size = " << shape.size();
  333. }
  334. return shape_4d;
  335. }
  336. } // namespace
  337. bool IsNeedPadding(const std::string &format, const size_t shape_size) {
  338. if (shape_size == 0) {
  339. return false;
  340. }
  341. if (format == kOpFormat_DEFAULT || format == kOpFormat_FRAC_NZ) {
  342. return false;
  343. } else if (shape_size < kNchwDims) {
  344. return true;
  345. }
  346. return false;
  347. }
  348. ShapeVector GetRuntimePaddingShape(const AnfNodePtr &node, size_t index) {
  349. MS_EXCEPTION_IF_NULL(node);
  350. ShapeVector shape;
  351. std::vector<size_t> host_shape;
  352. if (node->isa<ValueNode>()) {
  353. auto value_node = node->cast<ValueNodePtr>();
  354. MS_EXCEPTION_IF_NULL(value_node);
  355. auto node_value = value_node->value();
  356. MS_EXCEPTION_IF_NULL(node_value);
  357. auto tensor = node_value->cast<tensor::TensorPtr>();
  358. if (tensor == nullptr) {
  359. MS_LOG(EXCEPTION) << " The node[ " << node->DebugString() << "]'s cannot convert ";
  360. }
  361. auto shape_temp = tensor->shape();
  362. (void)std::transform(shape_temp.begin(), shape_temp.end(), std::back_inserter(host_shape), IntToSize);
  363. if (host_shape.empty()) {
  364. host_shape.push_back(1);
  365. }
  366. } else {
  367. host_shape = AnfAlgo::GetOutputInferShape(node, index);
  368. }
  369. if (trans::IsNeedPadding(AnfAlgo::GetOutputFormat(node, index), host_shape.size())) {
  370. host_shape = trans::PaddingShapeTo4d(host_shape, AnfAlgo::GetOutputReshapeType(node, index));
  371. }
  372. std::transform(host_shape.begin(), host_shape.end(), std::back_inserter(shape), SizeToInt);
  373. return shape;
  374. }
  375. std::vector<size_t> PaddingShapeTo4d(const std::vector<size_t> &shape, const std::vector<Axis> &padding_axis) {
  376. if (padding_axis.empty() || shape.size() != padding_axis.size()) {
  377. return PaddingShapeTo4dByDefault(shape);
  378. }
  379. std::vector<size_t> shape_4d(kNchwDims, 1);
  380. for (size_t index = 0; index < padding_axis.size(); index++) {
  381. shape_4d[padding_axis[index]] = shape[index];
  382. }
  383. return shape_4d;
  384. }
  385. std::vector<size_t> TransShapeToDevice(const std::vector<size_t> &shape, const std::string &format) {
  386. using DeviceShapeTransfer = std::function<std::vector<size_t>(const std::vector<size_t> &)>;
  387. const std::map<std::string, DeviceShapeTransfer> device_shape_map{{kOpFormat_NCHW, NchwDeviceShape},
  388. {kOpFormat_NHWC, NhwcDeviceShape},
  389. {kOpFormat_HWCN, HwchDeviceShape},
  390. {kOpFormat_FRAC_Z, FracZDeviceShape},
  391. {kOpFormat_NC1HWC0, Nc1hwc0DeviceShape},
  392. {kOpFormat_C1HWNCoC0, C1hwncoc0DeviceShape},
  393. {kOpFormat_FRACTAL_Z_C04, FracZc04DeviceShape},
  394. {kOpFormat_NC1HWC0_C04, Nc1hwc04DeviceShape},
  395. {kOpFormat_NDHWC, NdhwcDeviceShape}};
  396. if (format == kOpFormat_ND || format == kOpFormat_DEFAULT) {
  397. return shape;
  398. }
  399. auto temp_shape = shape;
  400. std::vector<size_t> device_shape;
  401. if (format == kOpFormat_FRAC_NZ) {
  402. if (shape.size() == 1 && (shape[0] == 1 || shape[0] % kCubeSize == 0)) {
  403. // For [1] and [1024] shape we can trait it as NZ shape
  404. return shape;
  405. }
  406. if (shape.size() < 2) {
  407. MS_LOG(EXCEPTION) << "Format" << format << " is not support shape " << shape.size();
  408. } else {
  409. (void)std::copy(shape.begin(), shape.end() - 2, std::back_inserter(device_shape));
  410. }
  411. auto h1 = (shape[shape.size() - 2] - 1) / kCubeSize + 1;
  412. auto w1 = (shape[shape.size() - 1] - 1) / kCubeSize + 1;
  413. device_shape.push_back(w1);
  414. device_shape.push_back(h1);
  415. device_shape.push_back(kCubeSize);
  416. device_shape.push_back(kCubeSize);
  417. return device_shape;
  418. } else if (format == kOpFormat_FRACTAL_ZN_LSTM) {
  419. const size_t c0 = 4;
  420. const size_t h = shape.at(kN) / c0;
  421. const size_t i = shape.at(kC) - h;
  422. const size_t first = DivCeil(i, kCubeSize) + DivCeil(h, kCubeSize);
  423. const size_t second = c0 * DivCeil(h, kCubeSize);
  424. device_shape.push_back(first);
  425. device_shape.push_back(second);
  426. device_shape.push_back(kCubeSize);
  427. device_shape.push_back(kCubeSize);
  428. return device_shape;
  429. }
  430. if (shape.size() != kNchwDims) {
  431. MS_LOG(WARNING) << "Get Device Shape using a shape size is less than 4 ,should be Padding shape by Default firstly";
  432. temp_shape = PaddingShapeTo4dByDefault(shape);
  433. }
  434. auto iter = device_shape_map.find(format);
  435. if (iter == device_shape_map.end()) {
  436. MS_LOG(EXCEPTION) << "Unexpected format[" << format << "]";
  437. }
  438. return iter->second(temp_shape);
  439. }
  440. bool CheckArgs(const FormatArgs &args, size_t *size, size_t *total_size) {
  441. if (args.host_shape.size() != kNchwDims) {
  442. MS_LOG(ERROR) << "Invalid host shape, host shape dims:" << args.host_shape.size() << ", expect dims:" << kNchwDims;
  443. return false;
  444. }
  445. MS_EXCEPTION_IF_NULL(size);
  446. MS_EXCEPTION_IF_NULL(total_size);
  447. *size = TypeIdSize(args.src_data_type);
  448. if (*size < 1) {
  449. MS_LOG(ERROR) << "Illegal dtype.";
  450. return false;
  451. }
  452. *total_size = ShapeSize(args.device_shape) * (*size);
  453. if (*total_size != args.device_size) {
  454. MS_LOG(ERROR) << "Illegal total data size, total_size:" << *total_size << ", device_size:" << args.device_size;
  455. return false;
  456. }
  457. return true;
  458. }
  459. bool TransDataType(const TypeIdArgs &args, void *result) {
  460. MS_LOG(DEBUG) << "Begin trans datatype from " << TypeIdLabel(args.host_data_type) << " to "
  461. << TypeIdLabel(args.device_data_type);
  462. MS_EXCEPTION_IF_NULL(result);
  463. std::pair<TypeId, TypeId> type_info(args.host_data_type, args.device_data_type);
  464. auto iter = mode_map.find(type_info);
  465. if (iter == mode_map.end()) {
  466. MS_LOG(ERROR) << "Unsupported datatype trans. src_type :" << TypeIdLabel(args.host_data_type)
  467. << ", dst_type:" << TypeIdLabel(args.device_data_type);
  468. return false;
  469. }
  470. auto trans_mode = iter->second;
  471. if (!CastKernel(args, result, args.host_shape_size, trans_mode)) {
  472. MS_LOG(ERROR) << "Failed to trans datatype..";
  473. return false;
  474. }
  475. return true;
  476. }
  477. bool TransFormat(const FormatArgs &args, void *result) {
  478. using FormatTransfer = std::function<bool(const FormatArgs &, void *)>;
  479. const std::map<std::string, FormatTransfer> format_trans_map{
  480. {kOpFormat_FRAC_Z, NchwToFracZ}, {kOpFormat_FRAC_NZ, NchwToFracNz},
  481. {kOpFormat_NC1HWC0, NchwToNc1hwc0}, {kOpFormat_C1HWNCoC0, NchwToC1hwncoc0},
  482. {kOpFormat_FRACTAL_Z_C04, NchwToFracZc04}, {kOpFormat_NC1HWC0_C04, NchwToNc1hwc04}};
  483. MS_LOG(DEBUG) << "Start trans format.";
  484. if (TypeIdSize(args.src_data_type) < 1) {
  485. MS_LOG(ERROR) << "Invalid datatype..";
  486. return false;
  487. }
  488. if (args.device_format == kOpFormat_HWCN || args.device_format == kOpFormat_NHWC) {
  489. return NchwTo4D(args, result);
  490. }
  491. auto iter = format_trans_map.find(args.device_format);
  492. if (iter == format_trans_map.end()) {
  493. MS_LOG(EXCEPTION) << "Unexpected format[" << args.device_format << "]";
  494. }
  495. return iter->second(args, result);
  496. }
  497. bool TransFormatFromDeviceToHost(const FormatArgs &args, void *result) {
  498. using FormatTransfer = std::function<bool(const FormatArgs &, void *)>;
  499. const std::map<std::string, FormatTransfer> format_trans_map{{kOpFormat_FRAC_Z, FracZToNchw},
  500. {kOpFormat_FRAC_NZ, FracNzToNchw},
  501. {kOpFormat_NC1HWC0, Nc1hwc0ToNchw},
  502. {kOpFormat_C1HWNCoC0, C1hwncoc0ToNchw},
  503. {kOpFormat_NC1HWC0_C04, Nc1hwc04ToNchw}};
  504. MS_LOG(DEBUG) << "Start trans format.";
  505. if (TypeIdSize(args.src_data_type) < 1) {
  506. MS_LOG(ERROR) << "Invalid datatype..";
  507. return false;
  508. }
  509. if (args.device_format == kOpFormat_HWCN || args.device_format == kOpFormat_NHWC) {
  510. return ToNchw(args, result);
  511. }
  512. auto iter = format_trans_map.find(args.device_format);
  513. if (iter == format_trans_map.end()) {
  514. MS_LOG(EXCEPTION) << "Unexpected format[" << args.device_format << "]";
  515. }
  516. return iter->second(args, result);
  517. }
  518. bool NchwTo4D(const FormatArgs &args, void *result) {
  519. // trans nchw to 4d
  520. MS_LOG(DEBUG) << "Trans format from nchw to 4d.";
  521. MS_EXCEPTION_IF_NULL(result);
  522. size_t size = 0;
  523. size_t total_size = 0;
  524. if (!CheckArgs(args, &size, &total_size)) {
  525. MS_LOG(ERROR) << "Check args failed.";
  526. return false;
  527. }
  528. auto n = args.host_shape[kN];
  529. auto c = args.host_shape[kC];
  530. auto h = args.host_shape[kH];
  531. auto w = args.host_shape[kW];
  532. for (size_t ni = 0; ni < n; ni++) {
  533. for (size_t ci = 0; ci < c; ci++) {
  534. for (size_t hi = 0; hi < h; hi++) {
  535. for (size_t wi = 0; wi < w; wi++) {
  536. auto src_idx = ni * c * h * w + ci * h * w + hi * w + wi;
  537. auto dst_idx = 0;
  538. if (args.device_format == kOpFormat_NHWC) {
  539. dst_idx = ni * h * w * c + hi * w * c + wi * c + ci;
  540. } else if (args.device_format == kOpFormat_HWCN) {
  541. dst_idx = hi * w * c * n + wi * c * n + ci * n + ni;
  542. }
  543. SetData(size, false, src_idx, dst_idx, args, result);
  544. }
  545. }
  546. }
  547. }
  548. return true;
  549. }
  550. bool ToNchw(const FormatArgs &args, void *result) {
  551. MS_LOG(DEBUG) << "Trans format to nchw from 4d.";
  552. MS_EXCEPTION_IF_NULL(result);
  553. size_t size = 0;
  554. size_t total_size = 0;
  555. if (!CheckArgs(args, &size, &total_size)) {
  556. MS_LOG(ERROR) << "Check args failed.";
  557. return false;
  558. }
  559. auto n = args.host_shape[kN];
  560. auto c = args.host_shape[kC];
  561. auto h = args.host_shape[kH];
  562. auto w = args.host_shape[kW];
  563. for (size_t ni = 0; ni < n; ni++) {
  564. for (size_t ci = 0; ci < c; ci++) {
  565. for (size_t hi = 0; hi < h; hi++) {
  566. for (size_t wi = 0; wi < w; wi++) {
  567. auto dst_idx = ni * c * h * w + ci * h * w + hi * w + wi;
  568. auto src_idx = 0;
  569. if (args.device_format == kOpFormat_NHWC) {
  570. src_idx = ni * h * w * c + hi * w * c + wi * c + ci;
  571. } else if (args.device_format == kOpFormat_HWCN) {
  572. src_idx = hi * w * c * n + wi * c * n + ci * n + ni;
  573. }
  574. SetData(size, false, src_idx, dst_idx, args, result);
  575. }
  576. }
  577. }
  578. }
  579. return true;
  580. }
  581. bool NchwToFracZ(const FormatArgs &args, void *result) {
  582. MS_LOG(DEBUG) << "Trans format from nchw to frac_z";
  583. MS_EXCEPTION_IF_NULL(result);
  584. if (args.host_shape.size() != kNchwDims) {
  585. MS_LOG(ERROR) << "Invalid host shape, host shape dims:" << args.host_shape.size() << ", expect dims:" << kNchwDims;
  586. return false;
  587. }
  588. auto size = TypeIdSize(args.src_data_type);
  589. if (size < 1) {
  590. MS_LOG(ERROR) << "Illegal dtype.";
  591. return false;
  592. }
  593. auto n = args.host_shape[kN];
  594. auto c = args.host_shape[kC];
  595. auto h = args.host_shape[kH];
  596. auto w = args.host_shape[kW];
  597. auto c0 = CubeSizeByType(args.src_data_type);
  598. if (c0 < 1) {
  599. MS_LOG(ERROR) << "Illegal dtype.";
  600. return false;
  601. }
  602. auto c1 = DivCeil(c, c0);
  603. auto hw = h * w;
  604. auto chw = c * hw;
  605. auto hwc0 = hw * c0;
  606. auto nchw = n * chw;
  607. auto hf_cnt = DivCeil(n, kCubeSize);
  608. auto vf_cnt = c1 * hw;
  609. auto fractal_ele_cnt = c0 * kCubeSize;
  610. auto total_ele_cnt = hf_cnt * vf_cnt * fractal_ele_cnt;
  611. auto dst_size = total_ele_cnt * size;
  612. if (dst_size != args.device_size) {
  613. MS_LOG(ERROR) << "Illegal total data size."
  614. << "dst size is :" << dst_size << "device size is :" << args.device_size;
  615. return false;
  616. }
  617. for (size_t vfi = 0; vfi < vf_cnt; vfi++) {
  618. auto vf_base_i = vfi * hf_cnt; // vertical fractal matrix base index
  619. for (size_t hfi = 0; hfi < hf_cnt; hfi++) {
  620. auto gfi = vf_base_i + hfi; // global fractal matrix index
  621. auto src_n_offset = hfi * chw * kCubeSize;
  622. auto src_f_offset = src_n_offset + vfi % hw + vfi / hw * hwc0;
  623. for (size_t row = 0; row < c0; row++) {
  624. auto src_ci = vfi / hw * c0 + row;
  625. auto src_row_offset = src_f_offset + row * hw;
  626. for (size_t col = 0; col < kCubeSize; col++) {
  627. auto src_ni = hfi * kCubeSize + col;
  628. auto src_idx = src_row_offset + chw * col;
  629. auto dst_idx = gfi * fractal_ele_cnt + col * c0 + row;
  630. auto pad_zero = src_ni >= n || src_idx >= nchw || src_ci >= c;
  631. SetData(size, pad_zero, src_idx, dst_idx, args, result);
  632. }
  633. }
  634. }
  635. }
  636. return true;
  637. }
  638. bool FracZToNchw(const FormatArgs &args, void *result) {
  639. MS_LOG(DEBUG) << "Trans format from frac_z to nchw";
  640. MS_EXCEPTION_IF_NULL(result);
  641. if (args.host_shape.size() != kNchwDims) {
  642. MS_LOG(ERROR) << "Invalid host shape, host shape dims:" << args.host_shape.size() << ", expect dims:" << kNchwDims;
  643. return false;
  644. }
  645. auto size = TypeIdSize(args.src_data_type);
  646. if (size < 1) {
  647. MS_LOG(ERROR) << "Illegal dtype.";
  648. return false;
  649. }
  650. auto total_size = ShapeSize(args.device_shape) * size;
  651. if (total_size != args.device_size) {
  652. MS_LOG(ERROR) << "Illegal total data size, total_size:" << total_size << ", device_size:" << args.device_size;
  653. return false;
  654. }
  655. auto n0 = args.device_shape.at(1);
  656. auto ni = args.device_shape.at(2);
  657. auto c0 = args.device_shape.at(3);
  658. auto n = args.host_shape[kN];
  659. auto c = args.host_shape[kC];
  660. auto h = args.host_shape[kH];
  661. auto w = args.host_shape[kW];
  662. auto nc = ni * n0;
  663. auto ncc0 = nc * c0;
  664. auto wncc0 = w * ncc0;
  665. auto hwncc0 = h * wncc0;
  666. auto hw = h * w;
  667. auto chw = c * hw;
  668. for (size_t n_idx = 0; n_idx < n; n_idx++) {
  669. size_t n_head_addr = n_idx * chw;
  670. for (size_t c_idx = 0; c_idx < c; c_idx++) {
  671. size_t c_head_addr = n_head_addr + c_idx * hw;
  672. for (size_t h_idx = 0; h_idx < h; h_idx++) {
  673. size_t h_head_addr = c_head_addr + h_idx * w;
  674. for (size_t w_idx = 0; w_idx < w; w_idx++) {
  675. size_t dst_idx = h_head_addr + w_idx;
  676. size_t c1_idx = c_idx / c0;
  677. size_t c0_idx = c_idx % c0;
  678. size_t nc_idx = n_idx;
  679. size_t src_idx = c1_idx * hwncc0 + h_idx * wncc0 + w_idx * ncc0 + nc_idx * c0 + c0_idx;
  680. SetData(size, false, src_idx, dst_idx, args, result);
  681. }
  682. }
  683. }
  684. }
  685. return true;
  686. }
  687. bool NchwToFracZc04(const FormatArgs &args, void *result) {
  688. // trans nchw to FracZc04
  689. MS_LOG(DEBUG) << "Trans format from nchw to FracZc04.";
  690. MS_EXCEPTION_IF_NULL(result);
  691. size_t size = 0;
  692. size_t total_size = 0;
  693. if (!CheckArgs(args, &size, &total_size)) {
  694. MS_LOG(ERROR) << "Check args failed.";
  695. return false;
  696. }
  697. auto cube = kCubeSize;
  698. auto n = args.host_shape[kN];
  699. auto c = args.host_shape[kC];
  700. auto h = args.host_shape[kH];
  701. auto w = args.host_shape[kW];
  702. const size_t c0 = 4;
  703. auto c1 = DivCeil(c, c0);
  704. auto hwc0 = h * w * c0;
  705. auto hwc = h * w * c;
  706. auto nhwc = n * h * w * c;
  707. auto n_cnt = DivCeil(n, cube);
  708. auto v_cnt = DivCeil(h * w * c0 * c1, cube);
  709. size_t dst_idx = 0;
  710. for (size_t vi = 0; vi < v_cnt; vi++) {
  711. for (size_t ni = 0; ni < n_cnt; ni++) {
  712. for (size_t col = 0; col < cube; col++) {
  713. for (size_t row = 0; row < cube; row++) {
  714. size_t cur_cube_n = cube * ni + col;
  715. size_t cur_cube_c1hwc0 = cube * vi + row;
  716. auto desc_g = cur_cube_n / n;
  717. auto desc_n = cur_cube_n % n;
  718. auto desc_c1 = cur_cube_c1hwc0 / hwc0;
  719. auto desc_c0 = cur_cube_c1hwc0 % c0;
  720. auto desc_h = (cur_cube_c1hwc0 - hwc0 * desc_c1) / (w * c0);
  721. auto desc_w = (cur_cube_c1hwc0 - hwc0 * desc_c1 - w * c0 * desc_h) / c0;
  722. auto c_idx = desc_c1 * c0 + desc_c0;
  723. auto src_idx = desc_g * nhwc + desc_n * hwc + c_idx * h * w + desc_h * w + desc_w;
  724. auto pad_zero = desc_g >= 1 || desc_n >= n || c_idx >= c;
  725. SetData(size, pad_zero, src_idx, dst_idx, args, result);
  726. dst_idx++;
  727. }
  728. }
  729. }
  730. }
  731. return true;
  732. }
  733. bool NchwToNc1hwc04(const FormatArgs &args, void *result) {
  734. MS_LOG(DEBUG) << "Trans format from nchw to Nc1hwc04.";
  735. return NchwToNc1hwc0(args, result);
  736. }
  737. bool Nc1hwc04ToNchw(const FormatArgs &args, void *result) {
  738. MS_LOG(DEBUG) << "Trans format from Nc1hwc04 to nchw.";
  739. return Nc1hwc0ToNchw(args, result);
  740. }
  741. bool TransShapeToNz(const std::vector<size_t> &host_shape, std::vector<size_t> *hw_shape) {
  742. MS_EXCEPTION_IF_NULL(hw_shape);
  743. if (host_shape.empty()) {
  744. MS_LOG(ERROR) << "Size of vector is 0.";
  745. return false;
  746. }
  747. switch (host_shape.size()) {
  748. case 1:
  749. hw_shape->push_back(1);
  750. hw_shape->push_back(1);
  751. hw_shape->push_back(host_shape[0]);
  752. return true;
  753. default:
  754. auto size = host_shape.size();
  755. if (size < 2) {
  756. MS_LOG(ERROR) << "Illegal size.";
  757. return false;
  758. }
  759. size_t times = 1;
  760. for (size_t i = 0; i != size - 2; i++) {
  761. times *= host_shape[i];
  762. }
  763. hw_shape->push_back(times);
  764. hw_shape->push_back(host_shape[size - 2]);
  765. hw_shape->push_back(host_shape[size - 1]);
  766. return true;
  767. }
  768. }
  769. bool NchwToFracNz(const FormatArgs &args, void *result) {
  770. MS_LOG(DEBUG) << "Trans format from nchw to frac_nz.";
  771. MS_EXCEPTION_IF_NULL(result);
  772. std::vector<size_t> hw_shape;
  773. if (!TransShapeToNz(args.host_shape, &hw_shape)) {
  774. MS_LOG(ERROR) << "Trans shape failed..";
  775. return false;
  776. }
  777. if (hw_shape.size() < 3 || args.device_shape.size() < 4) {
  778. MS_LOG(ERROR) << "Invalid shape size.";
  779. return false;
  780. }
  781. auto size = TypeIdSize(args.src_data_type);
  782. if (size < 1) {
  783. MS_LOG(ERROR) << "Illegal dtype";
  784. return false;
  785. }
  786. auto dst_size = ShapeSize(args.device_shape) * size;
  787. if (dst_size != args.device_size) {
  788. MS_LOG(ERROR) << "Illegal total data size, total_size:" << dst_size << ", device_size:" << args.device_size;
  789. return false;
  790. }
  791. auto times = hw_shape.at(0);
  792. auto h = hw_shape.at(1);
  793. auto w = hw_shape.at(2);
  794. auto hw = h * w;
  795. auto shape_size = args.device_shape.size();
  796. auto w1 = args.device_shape[shape_size - 4];
  797. auto h1 = args.device_shape[shape_size - 3];
  798. auto h0 = args.device_shape[shape_size - 2];
  799. auto w0 = args.device_shape[shape_size - 1];
  800. auto h1h0w0 = h1 * h0 * w0;
  801. auto w1h1h0w0 = w1 * h1h0w0;
  802. auto num_w1 = w / w0;
  803. for (size_t times_idx = 0; times_idx < times; times_idx++) {
  804. auto times_head = times_idx * w1h1h0w0;
  805. auto src_times_head = times_idx * hw;
  806. for (size_t h1h0_idx = 0; h1h0_idx < h; h1h0_idx++) {
  807. auto h1h0_head = times_head + h1h0_idx * w0;
  808. auto src_h_head = src_times_head + h1h0_idx * w;
  809. for (size_t w1_idx = 0; w1_idx < num_w1; w1_idx++) {
  810. for (size_t i = 0; i < w0; ++i) {
  811. size_t src_idx = src_h_head + w1_idx * w0 + i;
  812. size_t dst_idx = h1h0_head + w1_idx * h1h0w0 + i;
  813. SetData(size, false, src_idx, dst_idx, args, result);
  814. }
  815. }
  816. auto w1_head = num_w1 * w0;
  817. for (size_t w0_idx = 0; w1_head + w0_idx < w; w0_idx++) {
  818. auto src_w_idx = w1_head + w0_idx;
  819. size_t dst_idx = h1h0_head + num_w1 * h1h0w0 + w0_idx;
  820. size_t src_idx = src_h_head + src_w_idx;
  821. SetData(size, false, src_idx, dst_idx, args, result);
  822. }
  823. }
  824. }
  825. return true;
  826. }
  827. bool FracNzToNchw(const FormatArgs &args, void *result) {
  828. MS_LOG(DEBUG) << "Trans format from frac_nz to nchw";
  829. MS_EXCEPTION_IF_NULL(result);
  830. std::vector<size_t> hw_shape;
  831. if (!TransShapeToNz(args.host_shape, &hw_shape)) {
  832. MS_LOG(ERROR) << "Trans shape failed..";
  833. return false;
  834. }
  835. if (hw_shape.size() < 3 || args.device_shape.size() < 4) {
  836. MS_LOG(ERROR) << "Invalid shape size.";
  837. return false;
  838. }
  839. auto size = TypeIdSize(args.src_data_type);
  840. if (size < 1) {
  841. MS_LOG(ERROR) << "Illegal dtype";
  842. return false;
  843. }
  844. auto dst_size = ShapeSize(args.device_shape) * size;
  845. if (dst_size != args.device_size) {
  846. MS_LOG(ERROR) << "Illegal total data size, total_size:" << dst_size << ", device_size:" << args.device_size;
  847. return false;
  848. }
  849. auto times = hw_shape.at(0);
  850. auto h = hw_shape.at(1);
  851. auto w = hw_shape.at(2);
  852. auto hw = h * w;
  853. auto shape_size = args.device_shape.size();
  854. auto w1 = args.device_shape[shape_size - 4];
  855. auto h1 = args.device_shape[shape_size - 3];
  856. auto h0 = args.device_shape[shape_size - 2];
  857. auto w0 = args.device_shape[shape_size - 1];
  858. auto h1h0w0 = h1 * h0 * w0;
  859. auto w1h1h0w0 = w1 * h1h0w0;
  860. auto num_w1 = w / w0;
  861. for (size_t times_idx = 0; times_idx < times; times_idx++) {
  862. auto times_head = times_idx * w1h1h0w0;
  863. auto src_times_head = times_idx * hw;
  864. for (size_t h1h0_idx = 0; h1h0_idx < h; h1h0_idx++) {
  865. auto h1h0_head = times_head + h1h0_idx * w0;
  866. auto src_h_head = src_times_head + h1h0_idx * w;
  867. for (size_t w1_idx = 0; w1_idx < num_w1; w1_idx++) {
  868. for (size_t i = 0; i < w0; ++i) {
  869. size_t src_idx = h1h0_head + w1_idx * h1h0w0 + i;
  870. size_t dst_idx = src_h_head + w1_idx * w0 + i;
  871. SetData(size, false, src_idx, dst_idx, args, result);
  872. }
  873. }
  874. auto w1_head = num_w1 * w0;
  875. for (size_t w0_idx = 0; w1_head + w0_idx < w; w0_idx++) {
  876. auto src_w_idx = w1_head + w0_idx;
  877. size_t src_idx = h1h0_head + num_w1 * h1h0w0 + w0_idx;
  878. size_t dst_idx = src_h_head + src_w_idx;
  879. SetData(size, false, src_idx, dst_idx, args, result);
  880. }
  881. }
  882. }
  883. return true;
  884. }
  885. bool NchwToNc1hwc0(const FormatArgs &args, void *result) {
  886. MS_LOG(DEBUG) << "Trans format from nchw to Nc1h1wc0";
  887. MS_EXCEPTION_IF_NULL(result);
  888. if (args.host_shape.size() != kNchwDims) {
  889. MS_LOG(ERROR) << "Invalid host shape, host shape dims:" << args.host_shape.size() << ", expect dims:" << kNchwDims;
  890. return false;
  891. }
  892. auto size = TypeIdSize(args.src_data_type);
  893. if (size < 1) {
  894. MS_LOG(ERROR) << "Illegal dtype.";
  895. return false;
  896. }
  897. auto total_size = ShapeSize(args.device_shape) * size;
  898. if (total_size != args.device_size) {
  899. MS_LOG(ERROR) << "Illegal total data size, total_size:" << total_size << ", device_size:" << args.device_size;
  900. return false;
  901. }
  902. auto n = args.host_shape[kN];
  903. auto c = args.host_shape[kC];
  904. auto h = args.host_shape[kH];
  905. auto w = args.host_shape[kW];
  906. auto c0 = CubeSizeByType(args.src_data_type);
  907. if (c0 < 1) {
  908. MS_LOG(ERROR) << "Illegal dtype.";
  909. return false;
  910. }
  911. if (args.device_format == kOpFormat_NC1HWC0_C04) {
  912. c0 = 4;
  913. }
  914. auto c1 = DivCeil(c, c0);
  915. auto hw = h * w;
  916. auto chw = c * hw;
  917. auto c1hwc0 = c1 * hw * c0;
  918. auto wc0 = w * c0;
  919. for (size_t n_idx = 0; n_idx < n; n_idx++) {
  920. size_t n_head_addr = n_idx * c1hwc0;
  921. for (size_t c1_idx = 0; c1_idx < c1; c1_idx++) {
  922. size_t c1_head_addr = n_head_addr + c1_idx * hw * c0;
  923. for (size_t h_idx = 0; h_idx < h; h_idx++) {
  924. size_t h_head_addr = c1_head_addr + h_idx * wc0;
  925. for (size_t w_idx = 0; w_idx < w; w_idx++) {
  926. size_t w_head_addr = h_head_addr + w_idx * c0;
  927. for (size_t c0_idx = 0; c0_idx < c0; c0_idx++) {
  928. size_t dst_idx = c0_idx + w_head_addr;
  929. size_t c_idx = c0_idx + c1_idx * c0;
  930. size_t src_idx = n_idx * chw + c_idx * hw + h_idx * w + w_idx;
  931. auto pad_zero = c_idx >= c;
  932. SetData(size, pad_zero, src_idx, dst_idx, args, result);
  933. }
  934. }
  935. }
  936. }
  937. }
  938. return true;
  939. }
  940. bool Nc1hwc0ToNchw(const FormatArgs &args, void *result) {
  941. MS_LOG(DEBUG) << "Trans format from nc1h1wc0 to nchw";
  942. MS_EXCEPTION_IF_NULL(result);
  943. if (args.host_shape.size() != kNchwDims) {
  944. MS_LOG(ERROR) << "Invalid host shape, host shape dims:" << args.host_shape.size() << ", expect dims:" << kNchwDims;
  945. return false;
  946. }
  947. auto size = TypeIdSize(args.src_data_type);
  948. if (size < 1) {
  949. MS_LOG(ERROR) << "Illegal dtype.";
  950. return false;
  951. }
  952. auto total_size = ShapeSize(args.device_shape) * size;
  953. if (total_size != args.device_size) {
  954. MS_LOG(ERROR) << "Illegal total data size, total_size:" << total_size << ", device_size:" << args.device_size;
  955. return false;
  956. }
  957. auto n = args.host_shape[kN];
  958. auto c = args.host_shape[kC];
  959. auto h = args.host_shape[kH];
  960. auto w = args.host_shape[kW];
  961. auto c1 = args.device_shape[1];
  962. auto c0 = args.device_shape[4];
  963. auto hw = h * w;
  964. auto chw = c * hw;
  965. auto wc0 = w * c0;
  966. auto hwc0 = h * wc0;
  967. auto c1hwc0 = c1 * hwc0;
  968. for (size_t n_idx = 0; n_idx < n; n_idx++) {
  969. size_t n_head_addr = n_idx * chw;
  970. for (size_t c_idx = 0; c_idx < c; c_idx++) {
  971. size_t c_head_addr = n_head_addr + c_idx * hw;
  972. for (size_t h_idx = 0; h_idx < h; h_idx++) {
  973. size_t h_head_addr = c_head_addr + h_idx * w;
  974. for (size_t w_idx = 0; w_idx < w; w_idx++) {
  975. size_t dst_idx = h_head_addr + w_idx;
  976. size_t c1_idx = c_idx / c0;
  977. size_t c0_idx = c_idx % c0;
  978. size_t src_idx = n_idx * c1hwc0 + c1_idx * hwc0 + h_idx * wc0 + w_idx * c0 + c0_idx;
  979. SetData(size, false, src_idx, dst_idx, args, result);
  980. }
  981. }
  982. }
  983. }
  984. return true;
  985. }
  986. bool NchwToC1hwncoc0(const FormatArgs &args, void *result) {
  987. // trans nchw to c1hwncoc0
  988. MS_LOG(DEBUG) << "Trans format from nchw to c1hwncoc0.";
  989. MS_EXCEPTION_IF_NULL(result);
  990. size_t size = 0;
  991. size_t total_size = 0;
  992. if (!CheckArgs(args, &size, &total_size)) {
  993. MS_LOG(ERROR) << "Check args failed.";
  994. return false;
  995. }
  996. auto n = args.host_shape[kN];
  997. auto c = args.host_shape[kC];
  998. auto h = args.host_shape[kH];
  999. auto w = args.host_shape[kW];
  1000. const int co_idx = 4;
  1001. const int c0_idx = 5;
  1002. auto c1 = args.device_shape[0];
  1003. auto co = args.device_shape[co_idx];
  1004. auto c0 = args.device_shape[c0_idx];
  1005. for (size_t c1_i = 0; c1_i < c1; c1_i++) {
  1006. for (size_t h_i = 0; h_i < h; h_i++) {
  1007. for (size_t w_i = 0; w_i < w; w_i++) {
  1008. for (size_t n_i = 0; n_i < n; n_i++) {
  1009. for (size_t co_i = 0; co_i < co; co_i++) {
  1010. for (size_t c0_i = 0; c0_i < c0; c0_i++) {
  1011. size_t dst_idx = c1_i * h * w * n * co * c0 + h_i * w * n * co * c0 + w_i * n * co * c0 + n_i * co * c0 +
  1012. co_i * c0 + c0_i;
  1013. size_t c_i = c0_i + c1_i * c0;
  1014. size_t src_idx = n_i * c * h * w + c_i * h * w + h_i * w + w_i;
  1015. auto pad_zero = !(c_i < c && c0_i == co_i);
  1016. SetData(size, pad_zero, src_idx, dst_idx, args, result);
  1017. }
  1018. }
  1019. }
  1020. }
  1021. }
  1022. }
  1023. return true;
  1024. }
  1025. bool C1hwncoc0ToNchw(const FormatArgs &args, void *result) {
  1026. // trans c1hwncoc0 to nchw
  1027. MS_LOG(DEBUG) << "Trans format from c1hwncoc0 to nchw";
  1028. MS_EXCEPTION_IF_NULL(result);
  1029. size_t size = 0;
  1030. size_t total_size = 0;
  1031. if (!CheckArgs(args, &size, &total_size)) {
  1032. MS_LOG(ERROR) << "Check args failed.";
  1033. return false;
  1034. }
  1035. auto n = args.host_shape[kN];
  1036. auto c = args.host_shape[kC];
  1037. auto h = args.host_shape[kH];
  1038. auto w = args.host_shape[kW];
  1039. const int co_idx = 4;
  1040. const int c0_idx = 5;
  1041. auto co = args.device_shape[co_idx];
  1042. auto c0 = args.device_shape[c0_idx];
  1043. for (size_t n_i = 0; n_i < n; n_i++) {
  1044. for (size_t c_i = 0; c_i < c; c_i++) {
  1045. for (size_t h_i = 0; h_i < h; h_i++) {
  1046. for (size_t w_i = 0; w_i < w; w_i++) {
  1047. size_t dst_idx = n_i * c * h * w + c_i * h * w + h_i * w + w_i;
  1048. size_t c1_i = c_i / kCubeSize;
  1049. size_t c0_i = c_i % kCubeSize;
  1050. size_t co_i = c0_i;
  1051. size_t src_idx =
  1052. c1_i * h * w * n * co * c0 + h_i * w * n * co * c0 + w_i * n * co * c0 + n_i * co * c0 + co_i * c0 + c0_i;
  1053. SetData(size, false, src_idx, dst_idx, args, result);
  1054. }
  1055. }
  1056. }
  1057. }
  1058. return true;
  1059. }
  1060. } // namespace trans
  1061. } // namespace mindspore