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

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