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