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

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