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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/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. std::string dataset_path = datasets_root_path_ + "/test_tf_file_3_images/train-0000-of-0001.data";
  130. std::string dataset_path2 = datasets_root_path_ + "/testBatchDataset/test.data";
  131. std::shared_ptr<TFReaderOp> my_tfreader_op;
  132. rc = TFReaderOp::Builder()
  133. .SetDatasetFilesList({dataset_path})
  134. .SetRowsPerBuffer(2)
  135. .SetWorkerConnectorSize(16)
  136. .SetNumWorkers(1)
  137. .Build(&my_tfreader_op);
  138. EXPECT_TRUE(rc.IsOk());
  139. rc = my_tree->AssociateNode(my_tfreader_op);
  140. EXPECT_TRUE(rc.IsOk());
  141. std::shared_ptr<TFReaderOp> my_tfreader_op2;
  142. rc = TFReaderOp::Builder()
  143. .SetDatasetFilesList({dataset_path2})
  144. .SetRowsPerBuffer(2)
  145. .SetWorkerConnectorSize(1)
  146. .SetNumWorkers(1)
  147. .Build(&my_tfreader_op2);
  148. EXPECT_TRUE(rc.IsOk());
  149. rc = my_tree->AssociateNode(my_tfreader_op2);
  150. EXPECT_TRUE(rc.IsOk());
  151. // Creating DatasetOp
  152. std::shared_ptr<ZipOp> zip_op;
  153. rc = ZipOp::Builder().Build(&zip_op);
  154. EXPECT_TRUE(rc.IsOk());
  155. rc = my_tree->AssociateNode(zip_op);
  156. EXPECT_TRUE(rc.IsOk());
  157. rc = zip_op->AddChild(std::move(my_tfreader_op));
  158. EXPECT_TRUE(rc.IsOk());
  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(3).Build(&my_repeat_op);
  164. EXPECT_TRUE(rc.IsOk());
  165. rc = my_tree->AssociateNode(my_repeat_op);
  166. EXPECT_TRUE(rc.IsOk());
  167. rc = my_repeat_op->AddChild(zip_op);
  168. EXPECT_TRUE(rc.IsOk());
  169. rc = my_tree->AssignRoot(my_repeat_op);
  170. EXPECT_TRUE(rc.IsOk());
  171. rc = my_tree->Prepare();
  172. EXPECT_TRUE(rc.IsOk());
  173. // Launch the tree execution to kick off threads and start running the pipeline
  174. MS_LOG(INFO) << "Launching my tree.";
  175. rc = my_tree->Launch();
  176. EXPECT_TRUE(rc.IsOk());
  177. // Simulate a parse of data from our pipeline.
  178. std::shared_ptr<DatasetOp> rootNode = my_tree->root();
  179. DatasetIterator di(my_tree);
  180. TensorRow tensor_list;
  181. rc = di.FetchNextTensorRow(&tensor_list);
  182. EXPECT_TRUE(rc.IsOk());
  183. int row_count = 0;
  184. while (!tensor_list.empty()) {
  185. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  186. // Display the tensor by calling the printer on it
  187. for (int i = 0; i < tensor_list.size(); i++) {
  188. std::ostringstream ss;
  189. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  190. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  191. }
  192. rc = di.FetchNextTensorRow(&tensor_list);
  193. EXPECT_TRUE(rc.IsOk());
  194. row_count++;
  195. }
  196. ASSERT_EQ(row_count, 9); // Should be 9 rows fetched
  197. }