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.

zip_op_test.cc 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <chrono>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <functional>
  20. #include <iostream>
  21. #include <memory>
  22. #include <string>
  23. #include <thread>
  24. #include "minddata/dataset/core/client.h"
  25. #include "minddata/dataset/include/constants.h"
  26. #include "minddata/dataset/engine/datasetops/zip_op.h"
  27. #include "minddata/dataset/core/tensor.h"
  28. #include "minddata/dataset/core/config_manager.h"
  29. #include "common/common.h"
  30. #include "utils/ms_utils.h"
  31. #include "minddata/dataset/engine/data_buffer.h"
  32. #include "gtest/gtest.h"
  33. #include "minddata/dataset/core/global_context.h"
  34. #include "utils/log_adapter.h"
  35. namespace common = mindspore::common;
  36. using namespace mindspore::dataset;
  37. using mindspore::LogStream;
  38. using mindspore::ExceptionType::NoExceptionType;
  39. using mindspore::MsLogLevel::INFO;
  40. class MindDataTestZipOp : public UT::DatasetOpTesting {};
  41. TEST_F(MindDataTestZipOp, MindDataTestZipOpDefault) {
  42. /* Tree:
  43. *
  44. *
  45. * OpId(2) ZipOp
  46. * / \
  47. * OpId(0) TFReaderOp OpId(1) TFReaderOp
  48. * Start with an empty execution tree
  49. */
  50. Status rc;
  51. MS_LOG(INFO) << "UT test TestZipBasic.";
  52. auto my_tree = std::make_shared<ExecutionTree>();
  53. // Creating TFReaderOp
  54. std::string dataset_path = datasets_root_path_ + "/test_tf_file_3_images/train-0000-of-0001.data";
  55. std::string dataset_path2 = datasets_root_path_ + "/testBatchDataset/test.data";
  56. std::shared_ptr<TFReaderOp> my_tfreader_op;
  57. rc = TFReaderOp::Builder()
  58. .SetDatasetFilesList({dataset_path})
  59. .SetWorkerConnectorSize(16)
  60. .SetNumWorkers(1)
  61. .Build(&my_tfreader_op);
  62. EXPECT_TRUE(rc.IsOk());
  63. rc = my_tree->AssociateNode(my_tfreader_op);
  64. EXPECT_TRUE(rc.IsOk());
  65. std::shared_ptr<TFReaderOp> my_tfreader_op2;
  66. rc = TFReaderOp::Builder()
  67. .SetDatasetFilesList({dataset_path2})
  68. .SetWorkerConnectorSize(1)
  69. .SetNumWorkers(1)
  70. .Build(&my_tfreader_op2);
  71. EXPECT_TRUE(rc.IsOk());
  72. rc = my_tree->AssociateNode(my_tfreader_op2);
  73. EXPECT_TRUE(rc.IsOk());
  74. // Creating DatasetOp
  75. std::shared_ptr<ZipOp> zip_op;
  76. rc = ZipOp::Builder().Build(&zip_op);
  77. EXPECT_TRUE(rc.IsOk());
  78. rc = my_tree->AssociateNode(zip_op);
  79. EXPECT_TRUE(rc.IsOk());
  80. rc = zip_op->AddChild(std::move(my_tfreader_op));
  81. EXPECT_TRUE(rc.IsOk());
  82. rc = zip_op->AddChild(std::move(my_tfreader_op2));
  83. EXPECT_TRUE(rc.IsOk());
  84. rc = my_tree->AssignRoot(zip_op);
  85. EXPECT_TRUE(rc.IsOk());
  86. rc = my_tree->Prepare();
  87. EXPECT_TRUE(rc.IsOk());
  88. // Launch the tree execution to kick off threads and start running the pipeline
  89. MS_LOG(INFO) << "Launching my tree.";
  90. rc = my_tree->Launch();
  91. EXPECT_TRUE(rc.IsOk());
  92. // Simulate a parse of data from our pipeline.
  93. std::shared_ptr<DatasetOp> rootNode = my_tree->root();
  94. DatasetIterator di(my_tree);
  95. TensorRow tensor_list;
  96. rc = di.FetchNextTensorRow(&tensor_list);
  97. EXPECT_TRUE(rc.IsOk());
  98. int row_count = 0;
  99. while (!tensor_list.empty()) {
  100. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  101. // Display the tensor by calling the printer on it
  102. for (int i = 0; i < tensor_list.size(); i++) {
  103. std::ostringstream ss;
  104. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  105. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  106. }
  107. rc = di.FetchNextTensorRow(&tensor_list);
  108. EXPECT_TRUE(rc.IsOk());
  109. row_count++;
  110. }
  111. ASSERT_EQ(row_count, 3); // Should be 3 rows fetched
  112. }
  113. TEST_F(MindDataTestZipOp, MindDataTestZipOpRepeat) {
  114. /* Tree:
  115. * OpId(3) Repeat(3)
  116. *
  117. * OpId(2) ZipOp
  118. * / \
  119. * OpId(0) TFReaderOp OpId(1) TFReaderOp
  120. *
  121. * Start with an empty execution tree
  122. */
  123. Status rc;
  124. MS_LOG(INFO) << "UT test TestZipRepeat.";
  125. auto my_tree = std::make_shared<ExecutionTree>();
  126. uint32_t num_repeats = 3;
  127. std::string dataset_path = datasets_root_path_ + "/test_tf_file_3_images/train-0000-of-0001.data";
  128. std::string dataset_path2 = datasets_root_path_ + "/testBatchDataset/test.data";
  129. std::shared_ptr<TFReaderOp> my_tfreader_op;
  130. rc = TFReaderOp::Builder()
  131. .SetDatasetFilesList({dataset_path})
  132. .SetWorkerConnectorSize(16)
  133. .SetNumWorkers(1)
  134. .Build(&my_tfreader_op);
  135. EXPECT_TRUE(rc.IsOk());
  136. rc = my_tree->AssociateNode(my_tfreader_op);
  137. EXPECT_TRUE(rc.IsOk());
  138. std::shared_ptr<TFReaderOp> my_tfreader_op2;
  139. rc = TFReaderOp::Builder()
  140. .SetDatasetFilesList({dataset_path2})
  141. .SetWorkerConnectorSize(1)
  142. .SetNumWorkers(1)
  143. .Build(&my_tfreader_op2);
  144. EXPECT_TRUE(rc.IsOk());
  145. rc = my_tree->AssociateNode(my_tfreader_op2);
  146. EXPECT_TRUE(rc.IsOk());
  147. // Creating DatasetOp
  148. std::shared_ptr<ZipOp> zip_op;
  149. rc = ZipOp::Builder().Build(&zip_op);
  150. EXPECT_TRUE(rc.IsOk());
  151. rc = my_tree->AssociateNode(zip_op);
  152. EXPECT_TRUE(rc.IsOk());
  153. my_tfreader_op->set_total_repeats(num_repeats);
  154. my_tfreader_op->set_num_repeats_per_epoch(num_repeats);
  155. rc = zip_op->AddChild(std::move(my_tfreader_op));
  156. EXPECT_TRUE(rc.IsOk());
  157. my_tfreader_op2->set_total_repeats(num_repeats);
  158. my_tfreader_op2->set_num_repeats_per_epoch(num_repeats);
  159. rc = zip_op->AddChild(std::move(my_tfreader_op2));
  160. EXPECT_TRUE(rc.IsOk());
  161. // Builder(num_of_repeats)
  162. std::shared_ptr<RepeatOp> my_repeat_op;
  163. rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
  164. EXPECT_TRUE(rc.IsOk());
  165. rc = my_tree->AssociateNode(my_repeat_op);
  166. EXPECT_TRUE(rc.IsOk());
  167. zip_op->set_total_repeats(num_repeats);
  168. zip_op->set_num_repeats_per_epoch(num_repeats);
  169. rc = my_repeat_op->AddChild(zip_op);
  170. EXPECT_TRUE(rc.IsOk());
  171. rc = my_tree->AssignRoot(my_repeat_op);
  172. EXPECT_TRUE(rc.IsOk());
  173. rc = my_tree->Prepare();
  174. EXPECT_TRUE(rc.IsOk());
  175. // Launch the tree execution to kick off threads and start running the pipeline
  176. MS_LOG(INFO) << "Launching my tree.";
  177. rc = my_tree->Launch();
  178. EXPECT_TRUE(rc.IsOk());
  179. // Simulate a parse of data from our pipeline.
  180. std::shared_ptr<DatasetOp> rootNode = my_tree->root();
  181. DatasetIterator di(my_tree);
  182. TensorRow tensor_list;
  183. rc = di.FetchNextTensorRow(&tensor_list);
  184. EXPECT_TRUE(rc.IsOk());
  185. int row_count = 0;
  186. while (!tensor_list.empty()) {
  187. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  188. // Display the tensor by calling the printer on it
  189. for (int i = 0; i < tensor_list.size(); i++) {
  190. std::ostringstream ss;
  191. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  192. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  193. }
  194. rc = di.FetchNextTensorRow(&tensor_list);
  195. EXPECT_TRUE(rc.IsOk());
  196. row_count++;
  197. }
  198. ASSERT_EQ(row_count, 9); // Should be 9 rows fetched
  199. }