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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Copyright 2019 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/core/constants.h"
  26. #include "minddata/dataset/engine/datasetops/map_op.h"
  27. #include "minddata/dataset/engine/datasetops/zip_op.h"
  28. #include "minddata/dataset/core/tensor.h"
  29. #include "minddata/dataset/core/config_manager.h"
  30. #include "common/common.h"
  31. #include "common/utils.h"
  32. #include "minddata/dataset/engine/data_buffer.h"
  33. #include "gtest/gtest.h"
  34. #include "minddata/dataset/core/global_context.h"
  35. #include "utils/log_adapter.h"
  36. namespace common = mindspore::common;
  37. using namespace mindspore::dataset;
  38. using mindspore::MsLogLevel::INFO;
  39. using mindspore::ExceptionType::NoExceptionType;
  40. using mindspore::LogStream;
  41. class MindDataTestZipOp : public UT::DatasetOpTesting {
  42. };
  43. TEST_F(MindDataTestZipOp, MindDataTestZipOpDefault) {
  44. /* Tree:
  45. *
  46. *
  47. * OpId(2) ZipOp
  48. * / \
  49. * OpId(0) TFReaderOp OpId(1) TFReaderOp
  50. * Start with an empty execution tree
  51. */
  52. Status rc;
  53. MS_LOG(INFO) << "UT test TestZipBasic.";
  54. auto my_tree = std::make_shared<ExecutionTree>();
  55. // Creating TFReaderOp
  56. std::string dataset_path = datasets_root_path_ + "/test_tf_file_3_images/train-0000-of-0001.data";
  57. std::string dataset_path2 = datasets_root_path_ + "/testBatchDataset/test.data";
  58. std::shared_ptr<TFReaderOp> my_tfreader_op;
  59. rc = TFReaderOp::Builder()
  60. .SetDatasetFilesList({dataset_path})
  61. .SetRowsPerBuffer(2)
  62. .SetWorkerConnectorSize(16)
  63. .SetNumWorkers(1)
  64. .Build(&my_tfreader_op);
  65. EXPECT_TRUE(rc.IsOk());
  66. rc = my_tree->AssociateNode(my_tfreader_op);
  67. EXPECT_TRUE(rc.IsOk());
  68. std::shared_ptr<TFReaderOp> my_tfreader_op2;
  69. rc = TFReaderOp::Builder()
  70. .SetDatasetFilesList({dataset_path2})
  71. .SetRowsPerBuffer(2)
  72. .SetWorkerConnectorSize(1)
  73. .SetNumWorkers(1)
  74. .Build(&my_tfreader_op2);
  75. EXPECT_TRUE(rc.IsOk());
  76. rc = my_tree->AssociateNode(my_tfreader_op2);
  77. EXPECT_TRUE(rc.IsOk());
  78. // Creating DatasetOp
  79. std::shared_ptr<ZipOp> zip_op;
  80. rc = ZipOp::Builder().Build(&zip_op);
  81. EXPECT_TRUE(rc.IsOk());
  82. rc = my_tree->AssociateNode(zip_op);
  83. EXPECT_TRUE(rc.IsOk());
  84. rc = zip_op->AddChild(std::move(my_tfreader_op));
  85. EXPECT_TRUE(rc.IsOk());
  86. rc = zip_op->AddChild(std::move(my_tfreader_op2));
  87. EXPECT_TRUE(rc.IsOk());
  88. rc = my_tree->AssignRoot(zip_op);
  89. EXPECT_TRUE(rc.IsOk());
  90. rc = my_tree->Prepare();
  91. EXPECT_TRUE(rc.IsOk());
  92. // Launch the tree execution to kick off threads and start running the pipeline
  93. MS_LOG(INFO) << "Launching my tree.";
  94. rc = my_tree->Launch();
  95. EXPECT_TRUE(rc.IsOk());
  96. // Simulate a parse of data from our pipeline.
  97. std::shared_ptr<DatasetOp> rootNode = my_tree->root();
  98. DatasetIterator di(my_tree);
  99. TensorRow tensor_list;
  100. rc = di.FetchNextTensorRow(&tensor_list);
  101. EXPECT_TRUE(rc.IsOk());
  102. int row_count = 0;
  103. while (!tensor_list.empty()) {
  104. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  105. // Display the tensor by calling the printer on it
  106. for (int i = 0; i < tensor_list.size(); i++) {
  107. std::ostringstream ss;
  108. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  109. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  110. }
  111. rc = di.FetchNextTensorRow(&tensor_list);
  112. EXPECT_TRUE(rc.IsOk());
  113. row_count++;
  114. }
  115. ASSERT_EQ(row_count, 3); // Should be 3 rows fetched
  116. }
  117. TEST_F(MindDataTestZipOp, MindDataTestZipOpRepeat) {
  118. /* Tree:
  119. * OpId(3) Repeat(3)
  120. *
  121. * OpId(2) ZipOp
  122. * / \
  123. * OpId(0) TFReaderOp OpId(1) TFReaderOp
  124. *
  125. * Start with an empty execution tree
  126. */
  127. Status rc;
  128. MS_LOG(INFO) << "UT test TestZipRepeat.";
  129. auto my_tree = std::make_shared<ExecutionTree>();
  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. rc = zip_op->AddChild(std::move(my_tfreader_op));
  159. EXPECT_TRUE(rc.IsOk());
  160. rc = zip_op->AddChild(std::move(my_tfreader_op2));
  161. EXPECT_TRUE(rc.IsOk());
  162. // Builder(num_of_repeats)
  163. std::shared_ptr<RepeatOp> my_repeat_op;
  164. rc = RepeatOp::Builder(3).Build(&my_repeat_op);
  165. EXPECT_TRUE(rc.IsOk());
  166. rc = my_tree->AssociateNode(my_repeat_op);
  167. EXPECT_TRUE(rc.IsOk());
  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. }