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.

clue_op_test.cc 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "minddata/dataset/core/client.h"
  20. #include "common/common.h"
  21. #include "utils/ms_utils.h"
  22. #include "gtest/gtest.h"
  23. #include "utils/log_adapter.h"
  24. #include "minddata/dataset/engine/datasetops/source/clue_op.h"
  25. #include "minddata/dataset/util/status.h"
  26. namespace common = mindspore::common;
  27. using namespace mindspore::dataset;
  28. using mindspore::LogStream;
  29. using mindspore::ExceptionType::NoExceptionType;
  30. using mindspore::MsLogLevel::INFO;
  31. class MindDataTestCLUEOp : public UT::DatasetOpTesting {};
  32. std::shared_ptr<ClueOp> Clue(std::vector<std::string> file_list, int32_t op_connector_size,
  33. std::map<std::string, std::string> key_map) {
  34. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  35. auto worker_connector_size = config_manager->worker_connector_size();
  36. auto num_workers = config_manager->num_parallel_workers();
  37. int64_t num_samples = 0;
  38. bool shuffle = false;
  39. int32_t num_devices = 1;
  40. int32_t device_id = 0;
  41. ColKeyMap ck_map;
  42. for (auto &p : key_map) {
  43. std::vector<std::string> res = {};
  44. std::stringstream ss(p.second);
  45. std::string item = "";
  46. while (getline(ss, item, '/')) {
  47. res.push_back(item);
  48. }
  49. ck_map.insert({p.first, res});
  50. }
  51. std::shared_ptr<ClueOp> so = std::make_shared<ClueOp>(num_workers, num_samples, worker_connector_size, ck_map,
  52. file_list, op_connector_size, shuffle, num_devices, device_id);
  53. so->Init();
  54. return so;
  55. }
  56. TEST_F(MindDataTestCLUEOp, TestCLUEBasic) {
  57. // Start with an empty execution tree
  58. auto tree = std::make_shared<ExecutionTree>();
  59. Status rc;
  60. std::string dataset_path;
  61. dataset_path = datasets_root_path_ + "/testCLUE/afqmc/train.json";
  62. std::map<std::string, std::string> key_map;
  63. key_map["sentence1"] = "sentence1";
  64. key_map["sentence2"] = "sentence2";
  65. key_map["label"] = "label";
  66. std::shared_ptr<ClueOp> op = Clue({dataset_path}, 2, key_map);
  67. rc = tree->AssociateNode(op);
  68. ASSERT_TRUE(rc.IsOk());
  69. rc = tree->AssignRoot(op);
  70. ASSERT_TRUE(rc.IsOk());
  71. MS_LOG(INFO) << "Launching tree and begin iteration.";
  72. rc = tree->Prepare();
  73. ASSERT_TRUE(rc.IsOk());
  74. rc = tree->Launch();
  75. ASSERT_TRUE(rc.IsOk());
  76. // Start the loop of reading tensors from our pipeline
  77. DatasetIterator di(tree);
  78. TensorRow tensor_list;
  79. rc = di.FetchNextTensorRow(&tensor_list);
  80. ASSERT_TRUE(rc.IsOk());
  81. int row_count = 0;
  82. while (!tensor_list.empty()) {
  83. // Display the tensor by calling the printer on it
  84. for (int i = 0; i < tensor_list.size(); i++) {
  85. std::ostringstream ss;
  86. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  87. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  88. }
  89. rc = di.FetchNextTensorRow(&tensor_list);
  90. ASSERT_TRUE(rc.IsOk());
  91. row_count++;
  92. }
  93. ASSERT_EQ(row_count, 3);
  94. }
  95. TEST_F(MindDataTestCLUEOp, TestTotalRows) {
  96. std::string tf_file1 = datasets_root_path_ + "/testCLUE/afqmc/train.json";
  97. std::string tf_file2 = datasets_root_path_ + "/testCLUE/afqmc/dev.json";
  98. std::vector<std::string> files;
  99. files.push_back(tf_file1);
  100. int64_t total_rows = 0;
  101. ClueOp::CountAllFileRows(files, &total_rows);
  102. ASSERT_EQ(total_rows, 3);
  103. files.clear();
  104. files.push_back(tf_file2);
  105. ClueOp::CountAllFileRows(files, &total_rows);
  106. ASSERT_EQ(total_rows, 3);
  107. files.clear();
  108. files.push_back(tf_file1);
  109. files.push_back(tf_file2);
  110. ClueOp::CountAllFileRows(files, &total_rows);
  111. ASSERT_EQ(total_rows, 6);
  112. files.clear();
  113. }