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 28 kB

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