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.

storage_op_test.cc 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "dataset/core/client.h"
  17. #include "common/common.h"
  18. #include "common/utils.h"
  19. #include "gtest/gtest.h"
  20. #include "utils/log_adapter.h"
  21. #include <memory>
  22. #include <vector>
  23. #include <iostream>
  24. namespace common = mindspore::common;
  25. using namespace mindspore::dataset;
  26. using mindspore::MsLogLevel::INFO;
  27. using mindspore::ExceptionType::NoExceptionType;
  28. using mindspore::LogStream;
  29. class MindDataTestStorageOp : public UT::DatasetOpTesting {
  30. };
  31. TEST_F(MindDataTestStorageOp, TestStorageBasic1) {
  32. // single storage op and nothing else
  33. //
  34. // StorageOp
  35. MS_LOG(INFO) << "UT test TestStorageBasic1.";
  36. Status rc;
  37. // Start with an empty execution tree
  38. auto my_tree = std::make_shared<ExecutionTree>();
  39. // Test info:
  40. // Dataset from testDataset1 has 10 rows, 2 columns.
  41. // RowsPerBuffer buffer setting of 2 divides evenly into total rows.
  42. std::string dataset_path;
  43. dataset_path = datasets_root_path_ + "/testDataset1";
  44. std::shared_ptr<StorageOp> my_storage_op;
  45. StorageOp::Builder builder;
  46. builder.SetDatasetFilesDir(dataset_path)
  47. .SetRowsPerBuffer(2)
  48. .SetWorkerConnectorSize(16)
  49. .SetNumWorkers(1);
  50. rc = builder.Build(&my_storage_op);
  51. ASSERT_TRUE(rc.IsOk());
  52. my_tree->AssociateNode(my_storage_op);
  53. // Set children/root layout.
  54. my_tree->AssignRoot(my_storage_op);
  55. MS_LOG(INFO) << "Launching tree and begin iteration.";
  56. my_tree->Prepare();
  57. my_tree->Launch();
  58. // Start the loop of reading tensors from our pipeline
  59. DatasetIterator di(my_tree);
  60. TensorRow tensor_list;
  61. rc = di.FetchNextTensorRow(&tensor_list);
  62. ASSERT_TRUE(rc.IsOk());
  63. int row_count = 0;
  64. while (!tensor_list.empty()) {
  65. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  66. // Display the tensor by calling the printer on it
  67. for (int i = 0; i < tensor_list.size(); i++) {
  68. std::ostringstream ss;
  69. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  70. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  71. }
  72. rc = di.FetchNextTensorRow(&tensor_list);
  73. ASSERT_TRUE(rc.IsOk());
  74. row_count++;
  75. }
  76. ASSERT_EQ(row_count, 10); // Should be 10 rows fetched
  77. // debugging temp. what happens if we keep fetching..
  78. rc = di.FetchNextTensorRow(&tensor_list);
  79. ASSERT_TRUE(rc.IsOk());
  80. rc = di.FetchNextTensorRow(&tensor_list);
  81. ASSERT_TRUE(rc.IsOk());
  82. }
  83. TEST_F(MindDataTestStorageOp, TestStorageBasic2) {
  84. // single storage op and nothing else
  85. //
  86. // StorageOp
  87. MS_LOG(INFO) << "UT test TestStorageBasic1.";
  88. Status rc;
  89. // Start with an empty execution tree
  90. auto my_tree = std::make_shared<ExecutionTree>();
  91. // Test info:
  92. // Dataset from testDataset1 has 10 rows, 2 columns.
  93. // RowsPerBuffer buffer setting of 3 yields 4 buffers with the last buffer having single row
  94. // only. 2 workers.
  95. // Test a column selection instead of all columns as well.
  96. std::string dataset_path;
  97. dataset_path = datasets_root_path_ + "/testDataset1";
  98. std::vector<std::string> column_list;
  99. std::string label_colname("label");
  100. column_list.push_back(label_colname);
  101. std::shared_ptr<StorageOp> my_storage_op;
  102. StorageOp::Builder builder;
  103. builder.SetDatasetFilesDir(dataset_path)
  104. .SetRowsPerBuffer(3)
  105. .SetWorkerConnectorSize(16)
  106. .SetNumWorkers(2)
  107. .SetColumnsToLoad(column_list);
  108. rc = builder.Build(&my_storage_op);
  109. ASSERT_TRUE(rc.IsOk());
  110. my_tree->AssociateNode(my_storage_op);
  111. // Set children/root layout.
  112. my_tree->AssignRoot(my_storage_op);
  113. MS_LOG(INFO) << "Launching tree and begin iteration.";
  114. my_tree->Prepare();
  115. my_tree->Launch();
  116. // Start the loop of reading tensors from our pipeline
  117. DatasetIterator di(my_tree);
  118. TensorRow tensor_list;
  119. rc = di.FetchNextTensorRow(&tensor_list);
  120. ASSERT_TRUE(rc.IsOk());
  121. int row_count = 0;
  122. while (!tensor_list.empty()) {
  123. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  124. // Display the tensor by calling the printer on it
  125. for (int i = 0; i < tensor_list.size(); i++) {
  126. std::ostringstream ss;
  127. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  128. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  129. }
  130. rc = di.FetchNextTensorRow(&tensor_list);
  131. ASSERT_TRUE(rc.IsOk());
  132. row_count++;
  133. }
  134. ASSERT_EQ(row_count, 10); // Should be 10 rows fetched
  135. }