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.

concat_op_test.cc 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright 2020 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 <iostream>
  17. #include <memory>
  18. #include <vector>
  19. #include "common/common.h"
  20. #include "utils/ms_utils.h"
  21. #include "minddata/dataset/core/client.h"
  22. #include "minddata/dataset/engine/jagged_connector.h"
  23. #include "gtest/gtest.h"
  24. #include "utils/log_adapter.h"
  25. namespace common = mindspore::common;
  26. using namespace mindspore::dataset;
  27. using mindspore::LogStream;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::MsLogLevel::INFO;
  30. class MindDataTestConcatOp : public UT::DatasetOpTesting {};
  31. TEST_F(MindDataTestConcatOp, TestConcatProject) {
  32. /* Tree:
  33. *
  34. * OpId(2) ConcatOp
  35. * / \
  36. * OpId(0) TFReaderOp OpId(1) TFReaderOp
  37. *
  38. * Start with an empty execution tree
  39. */
  40. MS_LOG(INFO) << "UT test TestConcatProject.";
  41. auto my_tree = std::make_shared<ExecutionTree>();
  42. std::string dataset_path;
  43. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  44. // TFReaderOp1
  45. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  46. auto op_connector_size = config_manager->op_connector_size();
  47. int32_t num_workers = 1; // only one file -> one worker
  48. int32_t worker_connector_size = 16;
  49. std::vector<std::string> columns_to_load = {};
  50. std::vector<std::string> files = {dataset_path};
  51. std::unique_ptr<DataSchema> schema1 = std::make_unique<DataSchema>();
  52. schema1->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema1Row.json", {});
  53. // 16 is worker connector size
  54. std::shared_ptr<TFReaderOp> my_tfreader_op1 =
  55. std::make_shared<TFReaderOp>(num_workers, worker_connector_size, 0, files, std::move(schema1), op_connector_size,
  56. columns_to_load, false, 1, 0, false);
  57. Status rc = my_tfreader_op1->Init();
  58. ASSERT_OK(rc);
  59. rc = my_tree->AssociateNode(my_tfreader_op1);
  60. ASSERT_OK(rc);
  61. // TFReaderOp2
  62. std::unique_ptr<DataSchema> schema2 = std::make_unique<DataSchema>();
  63. schema2->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema1Row.json", {});
  64. // 16 is worker connector size
  65. std::shared_ptr<TFReaderOp> my_tfreader_op2 =
  66. std::make_shared<TFReaderOp>(num_workers, worker_connector_size, 0, files, std::move(schema2), op_connector_size,
  67. columns_to_load, false, 1, 0, false);
  68. rc = my_tfreader_op2->Init();
  69. ASSERT_OK(rc);
  70. rc = my_tree->AssociateNode(my_tfreader_op2);
  71. ASSERT_OK(rc);
  72. // Creating ConcatOp
  73. std::shared_ptr<SamplerRT> concat_sampler = std::make_shared<DistributedSamplerRT>(1, 0, false, 0);
  74. std::vector<std::pair<int, int>> flag_and_nums = {};
  75. std::vector<std::pair<int, int>> children_start_end_index = {};
  76. std::shared_ptr<ConcatOp> concat_op =
  77. std::make_shared<ConcatOp>(std::move(concat_sampler), flag_and_nums, children_start_end_index);
  78. rc = my_tree->AssociateNode(concat_op);
  79. EXPECT_TRUE(rc.IsOk());
  80. rc = concat_op->AddChild(std::move(my_tfreader_op1));
  81. EXPECT_TRUE(rc.IsOk());
  82. rc = concat_op->AddChild(std::move(my_tfreader_op2));
  83. EXPECT_TRUE(rc.IsOk());
  84. rc = my_tree->AssignRoot(concat_op);
  85. EXPECT_TRUE(rc.IsOk());
  86. rc = my_tree->Prepare();
  87. EXPECT_TRUE(rc.IsOk());
  88. // Launch the tree execution to kick off threads and start running the pipeline
  89. MS_LOG(INFO) << "Launching my tree.";
  90. rc = my_tree->Launch();
  91. EXPECT_TRUE(rc.IsOk());
  92. // Simulate a parse of data from our pipeline.
  93. std::shared_ptr<DatasetOp> rootNode = my_tree->root();
  94. DatasetIterator di(my_tree);
  95. TensorRow tensor_list;
  96. rc = di.FetchNextTensorRow(&tensor_list);
  97. EXPECT_TRUE(rc.IsOk());
  98. int row_count = 0;
  99. while (!tensor_list.empty()) {
  100. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  101. // Display the tensor by calling the printer on it
  102. for (int i = 0; i < tensor_list.size(); i++) {
  103. std::ostringstream ss;
  104. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  105. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  106. }
  107. rc = di.FetchNextTensorRow(&tensor_list);
  108. EXPECT_TRUE(rc.IsOk());
  109. row_count++;
  110. }
  111. ASSERT_EQ(row_count, 2); // Should be 2 rows fetched
  112. }