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.

execute.cc 26 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /**
  2. * Copyright 2020-2021 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 <algorithm>
  17. #include <fstream>
  18. #include "minddata/dataset/include/dataset/execute.h"
  19. #include "minddata/dataset/core/de_tensor.h"
  20. #include "minddata/dataset/core/tensor_row.h"
  21. #include "minddata/dataset/core/tensor.h"
  22. #include "minddata/dataset/core/type_id.h"
  23. #include "minddata/dataset/kernels/ir/tensor_operation.h"
  24. #include "minddata/dataset/kernels/tensor_op.h"
  25. #ifndef ENABLE_ANDROID
  26. #include "utils/log_adapter.h"
  27. #else
  28. #include "mindspore/lite/src/common/log_adapter.h"
  29. #endif
  30. #ifdef ENABLE_ACL
  31. #include "minddata/dataset/core/ascend_resource.h"
  32. #include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h"
  33. #include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h"
  34. #endif
  35. namespace mindspore {
  36. namespace dataset {
  37. using json = nlohmann::json;
  38. struct Execute::ExtraInfo {
  39. std::multimap<std::string, std::vector<uint32_t>> aipp_cfg_;
  40. bool init_with_shared_ptr_ = true; // Initial execute object with shared_ptr as default
  41. #ifdef ENABLE_ACL
  42. std::multimap<std::string, std::string> op2para_map_ = {{vision::kDvppCropJpegOperation, "size"},
  43. {vision::kDvppDecodeResizeOperation, "size"},
  44. {vision::kDvppDecodeResizeCropOperation, "crop_size"},
  45. {vision::kDvppDecodeResizeCropOperation, "resize_size"},
  46. {vision::kDvppNormalizeOperation, "mean"},
  47. {vision::kDvppNormalizeOperation, "std"},
  48. {vision::kDvppResizeJpegOperation, "size"}};
  49. #endif
  50. };
  51. // FIXME - Temporarily overload Execute to support both TensorOperation and TensorTransform
  52. Execute::Execute(std::shared_ptr<TensorOperation> op, MapTargetDevice deviceType, uint32_t device_id) {
  53. ops_.emplace_back(std::move(op));
  54. device_type_ = deviceType;
  55. info_ = std::make_shared<ExtraInfo>();
  56. #ifdef ENABLE_ACL
  57. if (device_type_ == MapTargetDevice::kAscend310) {
  58. device_resource_ = std::make_shared<AscendResource>();
  59. Status rc = device_resource_->InitResource(device_id);
  60. if (!rc.IsOk()) {
  61. device_resource_ = nullptr;
  62. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  63. }
  64. }
  65. #endif
  66. }
  67. Execute::Execute(std::shared_ptr<TensorTransform> op, MapTargetDevice deviceType, uint32_t device_id) {
  68. // Initialize the op and other context
  69. transforms_.emplace_back(op);
  70. info_ = std::make_shared<ExtraInfo>();
  71. device_type_ = deviceType;
  72. #ifdef ENABLE_ACL
  73. if (device_type_ == MapTargetDevice::kAscend310) {
  74. device_resource_ = std::make_shared<AscendResource>();
  75. Status rc = device_resource_->InitResource(device_id);
  76. if (!rc.IsOk()) {
  77. device_resource_ = nullptr;
  78. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  79. }
  80. }
  81. #endif
  82. }
  83. Execute::Execute(std::reference_wrapper<TensorTransform> op, MapTargetDevice deviceType, uint32_t device_id) {
  84. // Initialize the transforms_ and other context
  85. std::shared_ptr<TensorOperation> operation = op.get().Parse();
  86. ops_.emplace_back(std::move(operation));
  87. info_ = std::make_shared<ExtraInfo>();
  88. info_->init_with_shared_ptr_ = false;
  89. device_type_ = deviceType;
  90. #ifdef ENABLE_ACL
  91. if (device_type_ == MapTargetDevice::kAscend310) {
  92. device_resource_ = std::make_shared<AscendResource>();
  93. Status rc = device_resource_->InitResource(device_id);
  94. if (!rc.IsOk()) {
  95. device_resource_ = nullptr;
  96. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  97. }
  98. }
  99. #endif
  100. }
  101. // Execute function for the example case: auto decode(new vision::Decode());
  102. Execute::Execute(TensorTransform *op, MapTargetDevice deviceType, uint32_t device_id) {
  103. // Initialize the transforms_ and other context
  104. std::shared_ptr<TensorTransform> smart_ptr_op(op);
  105. transforms_.emplace_back(smart_ptr_op);
  106. info_ = std::make_shared<ExtraInfo>();
  107. device_type_ = deviceType;
  108. #ifdef ENABLE_ACL
  109. if (device_type_ == MapTargetDevice::kAscend310) {
  110. device_resource_ = std::make_shared<AscendResource>();
  111. Status rc = device_resource_->InitResource(device_id);
  112. if (!rc.IsOk()) {
  113. device_resource_ = nullptr;
  114. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  115. }
  116. }
  117. #endif
  118. }
  119. Execute::Execute(std::vector<std::shared_ptr<TensorOperation>> ops, MapTargetDevice deviceType, uint32_t device_id)
  120. : ops_(std::move(ops)), device_type_(deviceType) {
  121. info_ = std::make_shared<ExtraInfo>();
  122. #ifdef ENABLE_ACL
  123. if (device_type_ == MapTargetDevice::kAscend310) {
  124. device_resource_ = std::make_shared<AscendResource>();
  125. Status rc = device_resource_->InitResource(device_id);
  126. if (!rc.IsOk()) {
  127. device_resource_ = nullptr;
  128. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  129. }
  130. }
  131. #endif
  132. }
  133. Execute::Execute(std::vector<std::shared_ptr<TensorTransform>> ops, MapTargetDevice deviceType, uint32_t device_id) {
  134. // Initialize the transforms_ and other context
  135. transforms_ = ops;
  136. info_ = std::make_shared<ExtraInfo>();
  137. device_type_ = deviceType;
  138. #ifdef ENABLE_ACL
  139. if (device_type_ == MapTargetDevice::kAscend310) {
  140. device_resource_ = std::make_shared<AscendResource>();
  141. Status rc = device_resource_->InitResource(device_id);
  142. if (!rc.IsOk()) {
  143. device_resource_ = nullptr;
  144. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  145. }
  146. }
  147. #endif
  148. }
  149. Execute::Execute(const std::vector<std::reference_wrapper<TensorTransform>> ops, MapTargetDevice deviceType,
  150. uint32_t device_id) {
  151. // Initialize the transforms_ and other context
  152. if (deviceType == MapTargetDevice::kCpu) {
  153. (void)std::transform(
  154. ops.begin(), ops.end(), std::back_inserter(ops_),
  155. [](TensorTransform &operation) -> std::shared_ptr<TensorOperation> { return operation.Parse(); });
  156. } else {
  157. for (auto &op : ops) {
  158. ops_.emplace_back(op.get().Parse(deviceType));
  159. }
  160. }
  161. info_ = std::make_shared<ExtraInfo>();
  162. info_->init_with_shared_ptr_ = false;
  163. device_type_ = deviceType;
  164. #ifdef ENABLE_ACL
  165. if (device_type_ == MapTargetDevice::kAscend310) {
  166. device_resource_ = std::make_shared<AscendResource>();
  167. Status rc = device_resource_->InitResource(device_id);
  168. if (!rc.IsOk()) {
  169. device_resource_ = nullptr;
  170. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  171. }
  172. }
  173. #endif
  174. }
  175. // Execute function for the example vector case: auto decode(new vision::Decode());
  176. Execute::Execute(const std::vector<TensorTransform *> &ops, MapTargetDevice deviceType, uint32_t device_id) {
  177. // Initialize the transforms_ and other context
  178. for (auto &op : ops) {
  179. std::shared_ptr<TensorTransform> smart_ptr_op(op);
  180. transforms_.emplace_back(smart_ptr_op);
  181. }
  182. info_ = std::make_shared<ExtraInfo>();
  183. device_type_ = deviceType;
  184. #ifdef ENABLE_ACL
  185. if (device_type_ == MapTargetDevice::kAscend310) {
  186. device_resource_ = std::make_shared<AscendResource>();
  187. Status rc = device_resource_->InitResource(device_id);
  188. if (!rc.IsOk()) {
  189. device_resource_ = nullptr;
  190. MS_LOG(ERROR) << "Initialize Ascend310 resource fail";
  191. }
  192. }
  193. #endif
  194. }
  195. Execute::~Execute() {
  196. #ifdef ENABLE_ACL
  197. if (device_type_ == MapTargetDevice::kAscend310) {
  198. if (device_resource_) {
  199. device_resource_->FinalizeResource();
  200. } else {
  201. MS_LOG(ERROR) << "Device resource is nullptr which is illegal under case Ascend310";
  202. }
  203. }
  204. #endif
  205. }
  206. Status Execute::operator()(const mindspore::MSTensor &input, mindspore::MSTensor *output) {
  207. // Validate input tensor
  208. CHECK_FAIL_RETURN_UNEXPECTED(input.DataSize() > 0, "Input Tensor has no data");
  209. CHECK_FAIL_RETURN_UNEXPECTED(validate_device_(), "Device Type should be 'Ascend310' or 'CPU'");
  210. // Parse TensorTransform transforms_ into TensorOperation ops_
  211. if (info_->init_with_shared_ptr_) {
  212. RETURN_IF_NOT_OK(ParseTransforms_());
  213. info_->init_with_shared_ptr_ = false;
  214. }
  215. CHECK_FAIL_RETURN_UNEXPECTED(!ops_.empty(), "Input TensorOperation should be provided");
  216. // Validate and build runtime ops
  217. std::vector<std::shared_ptr<TensorOp>> transforms; // record the transformations
  218. std::map<MapTargetDevice, std::string> env_list = {
  219. {MapTargetDevice::kCpu, "kCpu"}, {MapTargetDevice::kGpu, "kGpu"}, {MapTargetDevice::kAscend310, "kAscend310"}};
  220. for (int32_t i = 0; i < ops_.size(); i++) {
  221. if (ops_[i] == nullptr) {
  222. MS_LOG(ERROR) << "Input TensorOperation["
  223. << std::to_string(i) + "] is unsupported on your input device:" << env_list.at(device_type_);
  224. }
  225. CHECK_FAIL_RETURN_UNEXPECTED(ops_[i] != nullptr, "Input TensorOperation[" + std::to_string(i) + "] is null");
  226. RETURN_IF_NOT_OK(ops_[i]->ValidateParams());
  227. transforms.emplace_back(ops_[i]->Build());
  228. }
  229. if (device_type_ == MapTargetDevice::kCpu) {
  230. // Convert mindspore::Tensor to dataset::Tensor
  231. std::shared_ptr<dataset::Tensor> de_tensor;
  232. Status rc = dataset::Tensor::CreateFromMemory(dataset::TensorShape(input.Shape()),
  233. MSTypeToDEType(static_cast<TypeId>(input.DataType())),
  234. (const uchar *)(input.Data().get()), input.DataSize(), &de_tensor);
  235. if (rc.IsError()) {
  236. MS_LOG(ERROR) << rc;
  237. return rc;
  238. }
  239. // Apply transforms on tensor
  240. for (auto &t : transforms) {
  241. std::shared_ptr<dataset::Tensor> de_output;
  242. Status rc_ = t->Compute(de_tensor, &de_output);
  243. if (rc_.IsError()) {
  244. MS_LOG(ERROR) << rc_;
  245. return rc_;
  246. }
  247. // For next transform
  248. de_tensor = std::move(de_output);
  249. }
  250. // Convert dataset::Tensor to mindspore::Tensor
  251. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(), "Apply transform failed, output tensor has no data");
  252. *output = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
  253. } else { // Ascend310 case, where we must set Ascend resource on each operators
  254. #ifdef ENABLE_ACL
  255. CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310");
  256. // Sink data from host into device
  257. std::shared_ptr<mindspore::dataset::DeviceTensor> device_input;
  258. RETURN_IF_NOT_OK(device_resource_->Sink(input, &device_input));
  259. for (auto &t : transforms) {
  260. // Initialize AscendResource for each operators
  261. std::shared_ptr<DeviceTensor> device_output;
  262. RETURN_IF_NOT_OK(t->SetAscendResource(device_resource_));
  263. RETURN_IF_NOT_OK(t->Compute(device_input, &device_output));
  264. // For next transform
  265. device_input = std::move(device_output);
  266. }
  267. CHECK_FAIL_RETURN_UNEXPECTED(device_input->HasDeviceData(), "Apply transform failed, output tensor has no data");
  268. *output = mindspore::MSTensor(std::make_shared<DETensor>(device_input, true));
  269. #endif
  270. }
  271. return Status::OK();
  272. }
  273. Status Execute::operator()(const std::vector<MSTensor> &input_tensor_list, std::vector<MSTensor> *output_tensor_list) {
  274. // Validate input tensor
  275. CHECK_FAIL_RETURN_UNEXPECTED(!input_tensor_list.empty(), "Input Tensor is not valid");
  276. for (auto &tensor : input_tensor_list) {
  277. CHECK_FAIL_RETURN_UNEXPECTED(tensor.DataSize() > 0, "Input Tensor has no data");
  278. }
  279. CHECK_FAIL_RETURN_UNEXPECTED(validate_device_(), "Device Type should be 'Ascend310' or 'CPU'");
  280. // Parse TensorTransform transforms_ into TensorOperation ops_
  281. if (info_->init_with_shared_ptr_) {
  282. RETURN_IF_NOT_OK(ParseTransforms_());
  283. info_->init_with_shared_ptr_ = false;
  284. }
  285. CHECK_FAIL_RETURN_UNEXPECTED(!ops_.empty(), "Input TensorOperation should be provided");
  286. std::map<MapTargetDevice, std::string> env_list = {
  287. {MapTargetDevice::kCpu, "kCpu"}, {MapTargetDevice::kGpu, "kGpu"}, {MapTargetDevice::kAscend310, "kAscend310"}};
  288. // Validate and build runtime ops
  289. std::vector<std::shared_ptr<TensorOp>> transforms;
  290. for (int32_t i = 0; i < ops_.size(); i++) {
  291. if (ops_[i] == nullptr) {
  292. MS_LOG(ERROR) << "Input TensorOperation["
  293. << std::to_string(i) + "] is unsupported on your input device:" << env_list.at(device_type_);
  294. }
  295. CHECK_FAIL_RETURN_UNEXPECTED(ops_[i] != nullptr, "Input TensorOperation[" + std::to_string(i) + "] is null");
  296. RETURN_IF_NOT_OK(ops_[i]->ValidateParams());
  297. transforms.emplace_back(ops_[i]->Build());
  298. }
  299. if (device_type_ == MapTargetDevice::kCpu) { // Case CPU
  300. TensorRow de_tensor_list;
  301. for (auto &tensor : input_tensor_list) {
  302. std::shared_ptr<dataset::Tensor> de_tensor;
  303. Status rc = dataset::Tensor::CreateFromMemory(
  304. dataset::TensorShape(tensor.Shape()), MSTypeToDEType(static_cast<TypeId>(tensor.DataType())),
  305. (const uchar *)(tensor.Data().get()), tensor.DataSize(), &de_tensor);
  306. if (rc.IsError()) {
  307. MS_LOG(ERROR) << rc;
  308. RETURN_IF_NOT_OK(rc);
  309. }
  310. de_tensor_list.emplace_back(std::move(de_tensor));
  311. }
  312. // Apply transforms on tensor
  313. for (auto &t : transforms) {
  314. TensorRow de_output_list;
  315. RETURN_IF_NOT_OK(t->Compute(de_tensor_list, &de_output_list));
  316. // For next transform
  317. de_tensor_list = std::move(de_output_list);
  318. }
  319. for (auto &tensor : de_tensor_list) {
  320. CHECK_FAIL_RETURN_UNEXPECTED(tensor->HasData(), "Apply transform failed, output tensor has no data");
  321. auto ms_tensor = mindspore::MSTensor(std::make_shared<DETensor>(tensor));
  322. output_tensor_list->emplace_back(ms_tensor);
  323. }
  324. CHECK_FAIL_RETURN_UNEXPECTED(!output_tensor_list->empty(), "Output Tensor is not valid");
  325. } else { // Case Ascend310
  326. #ifdef ENABLE_ACL
  327. CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310");
  328. for (auto &input_tensor : input_tensor_list) {
  329. // Sink each data from host into device
  330. std::shared_ptr<dataset::DeviceTensor> device_input;
  331. RETURN_IF_NOT_OK(device_resource_->Sink(input_tensor, &device_input));
  332. for (auto &t : transforms) {
  333. std::shared_ptr<DeviceTensor> device_output;
  334. RETURN_IF_NOT_OK(t->SetAscendResource(device_resource_));
  335. RETURN_IF_NOT_OK(t->Compute(device_input, &device_output));
  336. // For next transform
  337. device_input = std::move(device_output);
  338. }
  339. CHECK_FAIL_RETURN_UNEXPECTED(device_input->HasDeviceData(), "Apply transform failed, output tensor has no data");
  340. // Due to the limitation of Ascend310 memory, we have to pop every data onto host memory
  341. // So the speed of this batch method is slower than solo mode
  342. std::shared_ptr<mindspore::dataset::Tensor> host_output;
  343. RETURN_IF_NOT_OK(device_resource_->Pop(device_input, &host_output));
  344. auto ms_tensor = mindspore::MSTensor(std::make_shared<DETensor>(host_output));
  345. output_tensor_list->emplace_back(ms_tensor);
  346. // Release the data on the device because we have copied one piece onto host
  347. RETURN_IF_NOT_OK(device_resource_->DeviceDataRelease());
  348. }
  349. CHECK_FAIL_RETURN_UNEXPECTED(!output_tensor_list->empty(), "Output Tensor vector is empty");
  350. #endif
  351. }
  352. return Status::OK();
  353. }
  354. std::vector<uint32_t> AippSizeFilter(const std::vector<uint32_t> &resize_para, const std::vector<uint32_t> &crop_para) {
  355. std::vector<uint32_t> aipp_size;
  356. // Special condition where (no Crop and no Resize) or (no Crop and resize with fixed ratio) will lead to dynamic input
  357. if ((resize_para.size() == 0 || resize_para.size() == 1) && crop_para.size() == 0) {
  358. aipp_size = {0, 0};
  359. MS_LOG(WARNING) << "Dynamic input shape is not supported, incomplete aipp config file will be generated. Please "
  360. "checkout your TensorTransform input, both src_image_size_h and src_image_size will be 0";
  361. return aipp_size;
  362. }
  363. if (resize_para.size() == 0) { // If only Crop operator exists
  364. aipp_size = crop_para;
  365. } else if (crop_para.size() == 0) { // If only Resize operator with 2 parameters exists
  366. aipp_size = resize_para;
  367. } else { // If both of them exist
  368. if (resize_para.size() == 1) {
  369. aipp_size = crop_para;
  370. } else {
  371. aipp_size =
  372. *min_element(resize_para.begin(), resize_para.end()) < *min_element(crop_para.begin(), crop_para.end())
  373. ? resize_para
  374. : crop_para;
  375. }
  376. }
  377. #ifdef ENABLE_ACL
  378. aipp_size[0] = DVPP_ALIGN_UP(aipp_size[0], VPC_HEIGHT_ALIGN); // H
  379. aipp_size[1] = DVPP_ALIGN_UP(aipp_size[1], VPC_WIDTH_ALIGN); // W
  380. #endif
  381. return aipp_size;
  382. }
  383. std::vector<uint32_t> AippMeanFilter(const std::vector<uint32_t> &normalize_para) {
  384. std::vector<uint32_t> aipp_mean;
  385. if (normalize_para.size() == 6) { // If Normalize operator exist
  386. std::transform(normalize_para.begin(), normalize_para.begin() + 3, std::back_inserter(aipp_mean),
  387. [](uint32_t i) { return static_cast<uint32_t>(i / 10000); });
  388. } else {
  389. aipp_mean = {0, 0, 0};
  390. }
  391. return aipp_mean;
  392. }
  393. std::vector<float> AippStdFilter(const std::vector<uint32_t> &normalize_para) {
  394. std::vector<float> aipp_std;
  395. if (normalize_para.size() == 6) { // If Normalize operator exist
  396. auto zeros = std::find(std::begin(normalize_para), std::end(normalize_para), 0);
  397. if (zeros == std::end(normalize_para)) {
  398. std::transform(normalize_para.begin() + 3, normalize_para.end(), std::back_inserter(aipp_std),
  399. [](uint32_t i) { return 10000 / static_cast<float>(i); });
  400. } else { // If 0 occurs in std vector
  401. MS_LOG(WARNING) << "Detect 0 in std vector, please verify your input";
  402. aipp_std = {1.0, 1.0, 1.0};
  403. }
  404. } else {
  405. aipp_std = {1.0, 1.0, 1.0};
  406. }
  407. return aipp_std;
  408. }
  409. Status AippInfoCollection(std::map<std::string, std::string> *aipp_options, const std::vector<uint32_t> &aipp_size,
  410. const std::vector<uint32_t> &aipp_mean, const std::vector<float> &aipp_std) {
  411. // Several aipp config parameters
  412. aipp_options->insert(std::make_pair("related_input_rank", "0"));
  413. aipp_options->insert(std::make_pair("src_image_size_w", std::to_string(aipp_size[1])));
  414. aipp_options->insert(std::make_pair("src_image_size_h", std::to_string(aipp_size[0])));
  415. aipp_options->insert(std::make_pair("crop", "false"));
  416. aipp_options->insert(std::make_pair("input_format", "YUV420SP_U8"));
  417. aipp_options->insert(std::make_pair("aipp_mode", "static"));
  418. aipp_options->insert(std::make_pair("csc_switch", "true"));
  419. aipp_options->insert(std::make_pair("rbuv_swap_switch", "false"));
  420. // Y = AX + b, this part is A
  421. std::vector<int32_t> color_space_matrix = {256, 0, 359, 256, -88, -183, 256, 454, 0};
  422. int count = 0;
  423. for (int i = 0; i < 3; i++) {
  424. for (int j = 0; j < 3; j++) {
  425. std::string key_word = "matrix_r" + std::to_string(i) + "c" + std::to_string(j);
  426. aipp_options->insert(std::make_pair(key_word, std::to_string(color_space_matrix[count])));
  427. ++count;
  428. }
  429. }
  430. // This part is b
  431. std::vector<uint32_t> color_space_bias = {0, 128, 128};
  432. for (int i = 0; i < 3; i++) {
  433. std::string key_word = "input_bias_" + std::to_string(i);
  434. aipp_options->insert(std::make_pair(key_word, std::to_string(color_space_bias[i])));
  435. }
  436. // Y = (X - mean - min) * [std^(-1)], this part is mean
  437. for (int i = 0; i < aipp_mean.size(); i++) {
  438. std::string key_word = "mean_chn_" + std::to_string(i);
  439. aipp_options->insert(std::make_pair(key_word, std::to_string(aipp_mean[i])));
  440. }
  441. // This part is min
  442. for (int i = 0; i < aipp_mean.size(); i++) {
  443. std::string key_word = "min_chn_" + std::to_string(i);
  444. aipp_options->insert(std::make_pair(key_word, "0.0"));
  445. }
  446. // This part is std^(-1)
  447. for (int i = 0; i < aipp_std.size(); i++) {
  448. std::string key_word = "var_reci_chn_" + std::to_string(i);
  449. aipp_options->insert(std::make_pair(key_word, std::to_string(aipp_std[i])));
  450. }
  451. return Status::OK();
  452. }
  453. std::string Execute::AippCfgGenerator() {
  454. std::string config_location = "./aipp.cfg";
  455. #ifdef ENABLE_ACL
  456. if (info_->init_with_shared_ptr_) {
  457. ParseTransforms_();
  458. info_->init_with_shared_ptr_ = false;
  459. }
  460. std::vector<uint32_t> paras; // Record the parameters value of each Ascend operators
  461. for (int32_t i = 0; i < ops_.size(); i++) {
  462. // Validate operator ir
  463. json ir_info;
  464. if (ops_[i] == nullptr) {
  465. MS_LOG(ERROR) << "Input TensorOperation[" + std::to_string(i) + "] is null";
  466. return "";
  467. }
  468. // Define map between operator name and parameter name
  469. ops_[i]->to_json(&ir_info);
  470. // Collect the information of operators
  471. for (auto pos = info_->op2para_map_.equal_range(ops_[i]->Name()); pos.first != pos.second; ++pos.first) {
  472. auto paras_key_word = pos.first->second;
  473. paras = ir_info[paras_key_word].get<std::vector<uint32_t>>();
  474. info_->aipp_cfg_.insert(std::make_pair(ops_[i]->Name(), paras));
  475. }
  476. }
  477. std::ofstream outfile;
  478. outfile.open(config_location, std::ofstream::out);
  479. if (!outfile.is_open()) {
  480. MS_LOG(ERROR) << "Fail to open Aipp config file, please verify your system config(including authority)"
  481. << "We will return empty string which represent the location of Aipp config file in this case";
  482. std::string except = "";
  483. return except;
  484. }
  485. if (device_type_ == MapTargetDevice::kAscend310) {
  486. // Process resize parameters and crop parameters to find out the final size of input data
  487. std::vector<uint32_t> resize_paras;
  488. std::vector<uint32_t> crop_paras;
  489. // Find resize parameters
  490. std::map<std::string, std::vector<uint32_t>>::iterator iter;
  491. if (info_->aipp_cfg_.find(vision::kDvppResizeJpegOperation) != info_->aipp_cfg_.end()) {
  492. iter = info_->aipp_cfg_.find(vision::kDvppResizeJpegOperation);
  493. resize_paras = iter->second;
  494. } else if (info_->aipp_cfg_.find(vision::kDvppDecodeResizeOperation) != info_->aipp_cfg_.end()) {
  495. iter = info_->aipp_cfg_.find(vision::kDvppDecodeResizeOperation);
  496. resize_paras = iter->second;
  497. }
  498. // Find crop parameters
  499. if (info_->aipp_cfg_.find(vision::kDvppCropJpegOperation) != info_->aipp_cfg_.end()) {
  500. iter = info_->aipp_cfg_.find(vision::kDvppCropJpegOperation);
  501. crop_paras = iter->second;
  502. } else if (info_->aipp_cfg_.find(vision::kDvppDecodeResizeCropOperation) != info_->aipp_cfg_.end()) {
  503. iter = info_->aipp_cfg_.find(vision::kDvppDecodeResizeCropOperation);
  504. crop_paras = iter->second;
  505. }
  506. if (crop_paras.size() == 1) {
  507. crop_paras.emplace_back(crop_paras[0]);
  508. }
  509. std::vector<uint32_t> aipp_size = AippSizeFilter(resize_paras, crop_paras);
  510. // Process Normalization parameters to find out the final Normalization parameters for Aipp module
  511. std::vector<uint32_t> normalize_paras;
  512. if (info_->aipp_cfg_.find(vision::kDvppNormalizeOperation) != info_->aipp_cfg_.end()) {
  513. for (auto pos = info_->aipp_cfg_.equal_range(vision::kDvppNormalizeOperation); pos.first != pos.second;
  514. ++pos.first) {
  515. auto mean_or_std = pos.first->second;
  516. normalize_paras.insert(normalize_paras.end(), mean_or_std.begin(), mean_or_std.end());
  517. }
  518. }
  519. std::vector<uint32_t> aipp_mean = AippMeanFilter(normalize_paras);
  520. std::vector<float> aipp_std = AippStdFilter(normalize_paras);
  521. std::map<std::string, std::string> aipp_options;
  522. AippInfoCollection(&aipp_options, aipp_size, aipp_mean, aipp_std);
  523. std::string tab_char(4, ' ');
  524. outfile << "aipp_op {" << std::endl;
  525. for (auto &option : aipp_options) {
  526. outfile << tab_char << option.first << " : " << option.second << std::endl;
  527. }
  528. outfile << "}";
  529. outfile.close();
  530. } else { // For case GPU or CPU
  531. outfile << "aipp_op {" << std::endl << "}";
  532. outfile.close();
  533. MS_LOG(WARNING) << "Your runtime environment is not Ascend310, this config file will lead to undefined behavior on "
  534. "computing result. Please check that.";
  535. }
  536. #endif
  537. return config_location;
  538. }
  539. bool IsEmptyPtr(std::shared_ptr<TensorTransform> api_ptr) { return api_ptr == nullptr; }
  540. Status Execute::ParseTransforms_() {
  541. auto iter = std::find_if(transforms_.begin(), transforms_.end(), IsEmptyPtr);
  542. if (iter != transforms_.end()) {
  543. std::string err_msg = "Your input TensorTransforms contain at least one nullptr, please check your input";
  544. MS_LOG(ERROR) << err_msg;
  545. RETURN_STATUS_UNEXPECTED(err_msg);
  546. }
  547. if (device_type_ == MapTargetDevice::kCpu) {
  548. (void)std::transform(transforms_.begin(), transforms_.end(), std::back_inserter(ops_),
  549. [](std::shared_ptr<TensorTransform> operation) -> std::shared_ptr<TensorOperation> {
  550. return operation->Parse();
  551. });
  552. } else {
  553. for (auto &transform_ : transforms_) {
  554. ops_.emplace_back(transform_->Parse(device_type_));
  555. }
  556. }
  557. return Status::OK();
  558. }
  559. Status Execute::validate_device_() {
  560. if (device_type_ != MapTargetDevice::kCpu && device_type_ != MapTargetDevice::kAscend310 &&
  561. device_type_ != MapTargetDevice::kGpu) {
  562. std::string err_msg = "Your input device is not supported. (Option: CPU or GPU or Ascend310)";
  563. MS_LOG(ERROR) << err_msg;
  564. RETURN_STATUS_UNEXPECTED(err_msg);
  565. }
  566. return Status::OK();
  567. }
  568. Status Execute::DeviceMemoryRelease() {
  569. CHECK_FAIL_RETURN_UNEXPECTED(device_resource_, "Device resource is nullptr which is illegal under case Ascend310");
  570. Status rc = device_resource_->DeviceDataRelease();
  571. if (rc.IsError()) {
  572. std::string err_msg = "Error in device data release";
  573. MS_LOG(ERROR) << err_msg;
  574. RETURN_STATUS_UNEXPECTED(err_msg);
  575. }
  576. return Status::OK();
  577. }
  578. } // namespace dataset
  579. } // namespace mindspore