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.

tfReader_op_test.cc 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * Copyright 2019-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 <iostream>
  17. #include <memory>
  18. #include <vector>
  19. #include "minddata/dataset/core/client.h"
  20. #include "minddata/dataset/engine/data_schema.h"
  21. #include "minddata/dataset/engine/jagged_connector.h"
  22. #include "common/common.h"
  23. #include "gtest/gtest.h"
  24. #include "utils/log_adapter.h"
  25. namespace common = mindspore::common;
  26. using namespace mindspore::dataset;
  27. using mindspore::LogStream;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::MsLogLevel::INFO;
  30. class MindDataTestTFReaderOp : public UT::DatasetOpTesting {};
  31. TEST_F(MindDataTestTFReaderOp, TestTFReaderLargeRowsPerBuffer) {
  32. // Start with an empty execution tree
  33. auto my_tree = std::make_shared<ExecutionTree>();
  34. Status rc;
  35. std::string dataset_path;
  36. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  37. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  38. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  39. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  40. int32_t op_connector_size = config_manager->op_connector_size();
  41. int32_t num_workers = 1;
  42. int32_t worker_connector_size = config_manager->worker_connector_size();
  43. std::vector<std::string> files = {dataset_path};
  44. std::vector<std::string> columns_to_load = {};
  45. std::shared_ptr<TFReaderOp> my_tfreader_op =
  46. std::make_shared<TFReaderOp>(num_workers, worker_connector_size, 0, files, std::move(schema), op_connector_size,
  47. columns_to_load, false, 1, 0, false);
  48. rc = my_tfreader_op->Init();
  49. ASSERT_TRUE(rc.IsOk());
  50. rc = my_tree->AssociateNode(my_tfreader_op);
  51. ASSERT_TRUE(rc.IsOk());
  52. rc = my_tree->AssignRoot(my_tfreader_op);
  53. ASSERT_TRUE(rc.IsOk());
  54. MS_LOG(INFO) << "Launching tree and begin iteration.";
  55. rc = my_tree->Prepare();
  56. ASSERT_TRUE(rc.IsOk());
  57. rc = my_tree->Launch();
  58. ASSERT_TRUE(rc.IsOk());
  59. // Start the loop of reading tensors from our pipeline
  60. DatasetIterator di(my_tree);
  61. TensorRow tensor_list;
  62. rc = di.FetchNextTensorRow(&tensor_list);
  63. ASSERT_TRUE(rc.IsOk());
  64. int row_count = 0;
  65. while (!tensor_list.empty()) {
  66. // Display the tensor by calling the printer on it
  67. for (int i = 0; i < tensor_list.size(); i++) {
  68. std::ostringstream ss;
  69. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  70. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  71. }
  72. rc = di.FetchNextTensorRow(&tensor_list);
  73. ASSERT_TRUE(rc.IsOk());
  74. row_count++;
  75. }
  76. ASSERT_EQ(row_count, 12);
  77. }
  78. TEST_F(MindDataTestTFReaderOp, TestTFReaderSmallRowsPerBuffer) {
  79. // Start with an empty execution tree
  80. auto my_tree = std::make_shared<ExecutionTree>();
  81. Status rc;
  82. std::string dataset_path;
  83. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  84. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  85. int32_t op_connector_size = config_manager->op_connector_size();
  86. int32_t num_workers = 1;
  87. int32_t worker_connector_size = config_manager->worker_connector_size();
  88. std::vector<std::string> files = {dataset_path};
  89. std::vector<std::string> columns_to_load = {};
  90. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  91. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  92. std::shared_ptr<TFReaderOp> my_tfreader_op =
  93. std::make_shared<TFReaderOp>(num_workers, worker_connector_size, 0, files, std::move(schema), op_connector_size,
  94. columns_to_load, false, 1, 0, false);
  95. rc = my_tfreader_op->Init();
  96. ASSERT_TRUE(rc.IsOk());
  97. rc = my_tree->AssociateNode(my_tfreader_op);
  98. ASSERT_TRUE(rc.IsOk());
  99. rc = my_tree->AssignRoot(my_tfreader_op);
  100. ASSERT_TRUE(rc.IsOk());
  101. MS_LOG(INFO) << "Launching tree and begin iteration.";
  102. rc = my_tree->Prepare();
  103. ASSERT_TRUE(rc.IsOk());
  104. rc = my_tree->Launch();
  105. ASSERT_TRUE(rc.IsOk());
  106. // Start the loop of reading tensors from our pipeline
  107. DatasetIterator di(my_tree);
  108. TensorRow tensor_list;
  109. rc = di.FetchNextTensorRow(&tensor_list);
  110. ASSERT_TRUE(rc.IsOk());
  111. int row_count = 0;
  112. while (!tensor_list.empty()) {
  113. // Display the tensor by calling the printer on it
  114. for (int i = 0; i < tensor_list.size(); i++) {
  115. std::ostringstream ss;
  116. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  117. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  118. }
  119. rc = di.FetchNextTensorRow(&tensor_list);
  120. ASSERT_TRUE(rc.IsOk());
  121. row_count++;
  122. }
  123. ASSERT_EQ(row_count, 12);
  124. }
  125. TEST_F(MindDataTestTFReaderOp, TestTFReaderLargeQueueSize) {
  126. // Start with an empty execution tree
  127. auto my_tree = std::make_shared<ExecutionTree>();
  128. Status rc;
  129. std::string dataset_path;
  130. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  131. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  132. int32_t op_connector_size = config_manager->op_connector_size();
  133. int32_t num_workers = 1;
  134. int32_t worker_connector_size = 1;
  135. std::vector<std::string> files = {dataset_path};
  136. std::vector<std::string> columns_to_load = {};
  137. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  138. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  139. std::shared_ptr<TFReaderOp> my_tfreader_op =
  140. std::make_shared<TFReaderOp>(num_workers, worker_connector_size, 0, files, std::move(schema), op_connector_size,
  141. columns_to_load, false, 1, 0, false);
  142. rc = my_tfreader_op->Init();
  143. ASSERT_TRUE(rc.IsOk());
  144. rc = my_tree->AssociateNode(my_tfreader_op);
  145. ASSERT_TRUE(rc.IsOk());
  146. rc = my_tree->AssignRoot(my_tfreader_op);
  147. ASSERT_TRUE(rc.IsOk());
  148. MS_LOG(INFO) << "Launching tree and begin iteration.";
  149. rc = my_tree->Prepare();
  150. ASSERT_TRUE(rc.IsOk());
  151. rc = my_tree->Launch();
  152. ASSERT_TRUE(rc.IsOk());
  153. // Start the loop of reading tensors from our pipeline
  154. DatasetIterator di(my_tree);
  155. TensorRow tensor_list;
  156. rc = di.FetchNextTensorRow(&tensor_list);
  157. ASSERT_TRUE(rc.IsOk());
  158. int row_count = 0;
  159. while (!tensor_list.empty()) {
  160. // Display the tensor by calling the printer on it
  161. for (int i = 0; i < tensor_list.size(); i++) {
  162. std::ostringstream ss;
  163. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  164. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  165. }
  166. rc = di.FetchNextTensorRow(&tensor_list);
  167. ASSERT_TRUE(rc.IsOk());
  168. row_count++;
  169. }
  170. ASSERT_EQ(row_count, 12);
  171. }
  172. TEST_F(MindDataTestTFReaderOp, TestTFReaderOneThread) {
  173. // Start with an empty execution tree
  174. auto my_tree = std::make_shared<ExecutionTree>();
  175. Status rc;
  176. std::string dataset_path;
  177. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  178. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  179. int32_t op_connector_size = config_manager->op_connector_size();
  180. int32_t num_workers = 1;
  181. int32_t worker_connector_size = config_manager->worker_connector_size();
  182. std::vector<std::string> files = {dataset_path};
  183. std::vector<std::string> columns_to_load = {};
  184. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  185. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  186. std::shared_ptr<TFReaderOp> my_tfreader_op =
  187. std::make_shared<TFReaderOp>(num_workers, worker_connector_size, 0, files, std::move(schema), op_connector_size,
  188. columns_to_load, false, 1, 0, false);
  189. rc = my_tfreader_op->Init();
  190. ASSERT_TRUE(rc.IsOk());
  191. rc = my_tree->AssociateNode(my_tfreader_op);
  192. ASSERT_TRUE(rc.IsOk());
  193. rc = my_tree->AssignRoot(my_tfreader_op);
  194. ASSERT_TRUE(rc.IsOk());
  195. MS_LOG(INFO) << "Launching tree and begin iteration.";
  196. rc = my_tree->Prepare();
  197. ASSERT_TRUE(rc.IsOk());
  198. rc = my_tree->Launch();
  199. ASSERT_TRUE(rc.IsOk());
  200. // Start the loop of reading tensors from our pipeline
  201. DatasetIterator di(my_tree);
  202. TensorRow tensor_list;
  203. rc = di.FetchNextTensorRow(&tensor_list);
  204. ASSERT_TRUE(rc.IsOk());
  205. int row_count = 0;
  206. while (!tensor_list.empty()) {
  207. // Display the tensor by calling the printer on it
  208. for (int i = 0; i < tensor_list.size(); i++) {
  209. std::ostringstream ss;
  210. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  211. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  212. }
  213. rc = di.FetchNextTensorRow(&tensor_list);
  214. ASSERT_TRUE(rc.IsOk());
  215. row_count++;
  216. }
  217. ASSERT_EQ(row_count, 12);
  218. }
  219. TEST_F(MindDataTestTFReaderOp, TestTFReaderTake1Buffer) {
  220. // Start with an empty execution tree
  221. auto my_tree = std::make_shared<ExecutionTree>();
  222. Status rc;
  223. std::string dataset_path;
  224. dataset_path = datasets_root_path_ + "/testTFTestAllTypes";
  225. std::string data_schema_filepath = dataset_path + "/datasetSchema5Rows.json";
  226. // TFReaderOp
  227. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  228. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema5Rows.json", {});
  229. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  230. int32_t op_connector_size = config_manager->op_connector_size();
  231. int32_t num_workers = 1;
  232. int32_t worker_connector_size = config_manager->worker_connector_size();
  233. std::vector<std::string> files = {dataset_path + "/test.data"};
  234. std::vector<std::string> columns_to_load = {};
  235. std::shared_ptr<TFReaderOp> my_tfreader_op =
  236. std::make_shared<TFReaderOp>(num_workers, worker_connector_size, 0, files, std::move(schema), op_connector_size,
  237. columns_to_load, false, 1, 0, false);
  238. rc = my_tfreader_op->Init();
  239. ASSERT_TRUE(rc.IsOk());
  240. rc = my_tree->AssociateNode(my_tfreader_op);
  241. ASSERT_TRUE(rc.IsOk());
  242. rc = my_tree->AssignRoot(my_tfreader_op);
  243. ASSERT_TRUE(rc.IsOk());
  244. MS_LOG(INFO) << "Launching tree and begin iteration.";
  245. rc = my_tree->Prepare();
  246. ASSERT_TRUE(rc.IsOk());
  247. rc = my_tree->Launch();
  248. ASSERT_TRUE(rc.IsOk());
  249. // Start the loop of reading tensors from our pipeline
  250. DatasetIterator di(my_tree);
  251. TensorRow tensor_list;
  252. rc = di.FetchNextTensorRow(&tensor_list);
  253. ASSERT_TRUE(rc.IsOk());
  254. int row_count = 0;
  255. while (!tensor_list.empty()) {
  256. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  257. // Display the tensor by calling the printer on it
  258. for (int i = 0; i < tensor_list.size(); i++) {
  259. std::ostringstream ss;
  260. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  261. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  262. }
  263. rc = di.FetchNextTensorRow(&tensor_list);
  264. ASSERT_TRUE(rc.IsOk());
  265. row_count++;
  266. }
  267. ASSERT_EQ(row_count, 5);
  268. }
  269. TEST_F(MindDataTestTFReaderOp, TestTotalRowsBasic) {
  270. std::string tf_file = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  271. std::vector<std::string> filenames;
  272. for (int i = 0; i < 5; i++) {
  273. filenames.push_back(tf_file);
  274. }
  275. int64_t total_rows = 0;
  276. TFReaderOp::CountTotalRows(&total_rows, filenames, 1);
  277. ASSERT_EQ(total_rows, 60);
  278. TFReaderOp::CountTotalRows(&total_rows, filenames, 2);
  279. ASSERT_EQ(total_rows, 60);
  280. TFReaderOp::CountTotalRows(&total_rows, filenames, 3);
  281. ASSERT_EQ(total_rows, 60);
  282. TFReaderOp::CountTotalRows(&total_rows, filenames, 4);
  283. ASSERT_EQ(total_rows, 60);
  284. TFReaderOp::CountTotalRows(&total_rows, filenames, 5);
  285. ASSERT_EQ(total_rows, 60);
  286. TFReaderOp::CountTotalRows(&total_rows, filenames, 6);
  287. ASSERT_EQ(total_rows, 60);
  288. TFReaderOp::CountTotalRows(&total_rows, filenames, 729);
  289. ASSERT_EQ(total_rows, 60);
  290. TFReaderOp::CountTotalRows(&total_rows, filenames, 1, true);
  291. ASSERT_EQ(total_rows, 60);
  292. TFReaderOp::CountTotalRows(&total_rows, filenames, 2, true);
  293. ASSERT_EQ(total_rows, 60);
  294. TFReaderOp::CountTotalRows(&total_rows, filenames, 3, true);
  295. ASSERT_EQ(total_rows, 60);
  296. TFReaderOp::CountTotalRows(&total_rows, filenames, 4, true);
  297. ASSERT_EQ(total_rows, 60);
  298. TFReaderOp::CountTotalRows(&total_rows, filenames, 5, true);
  299. ASSERT_EQ(total_rows, 60);
  300. TFReaderOp::CountTotalRows(&total_rows, filenames, 6, true);
  301. ASSERT_EQ(total_rows, 60);
  302. TFReaderOp::CountTotalRows(&total_rows, filenames, 729, true);
  303. ASSERT_EQ(total_rows, 60);
  304. }