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.

iterator.cc 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright 2020-2021 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 "minddata/dataset/include/iterator.h"
  17. #include "minddata/dataset/core/client.h"
  18. #include "minddata/dataset/engine/consumers/pull_based_tree_consumer.h"
  19. #include "minddata/dataset/engine/consumers/tree_consumer.h"
  20. #include "minddata/dataset/engine/runtime_context.h"
  21. #include "minddata/dataset/include/datasets.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. Iterator::Iterator() : consumer_(nullptr) {}
  25. Iterator::~Iterator() { Stop(); }
  26. // Get the next row from the data pipeline.
  27. Status Iterator::GetNextRowCharIF(MSTensorMapChar *row) {
  28. // Clean data buffer
  29. row->clear();
  30. std::unordered_map<std::string, std::shared_ptr<dataset::Tensor>> md_map;
  31. Status rc = consumer_->GetNextAsMap(&md_map);
  32. if (rc.IsError()) {
  33. MS_LOG(ERROR) << "GetNextRow: Failed to get next row. Error status: " << rc;
  34. row->clear();
  35. return rc;
  36. }
  37. for (auto de_tensor : md_map) {
  38. std::vector<char> col_name(de_tensor.first.begin(), de_tensor.first.end());
  39. row->insert(std::make_pair(col_name, mindspore::MSTensor(std::make_shared<DETensor>(de_tensor.second))));
  40. }
  41. return Status::OK();
  42. }
  43. // Get the next row from the data pipeline.
  44. Status Iterator::GetNextRow(MSTensorVec *row) {
  45. // Clean data buffer
  46. row->clear();
  47. // create a dataset tensor row and fetch. Then we convert the output to MSTensor
  48. std::vector<std::shared_ptr<dataset::Tensor>> md_row;
  49. Status rc = consumer_->GetNextAsVector(&md_row);
  50. if (rc.IsError()) {
  51. row->clear();
  52. return rc;
  53. }
  54. std::transform(md_row.begin(), md_row.end(), std::back_inserter(*row),
  55. [](auto t) { return mindspore::MSTensor(std::make_shared<DETensor>(t)); });
  56. return Status::OK();
  57. }
  58. // Shut down the data pipeline.
  59. void Iterator::Stop() {
  60. if (runtime_context_ != nullptr) {
  61. Status rc = runtime_context_->Terminate();
  62. if (rc.IsError()) {
  63. MS_LOG(ERROR) << rc.ToString();
  64. }
  65. }
  66. }
  67. // Function to build and launch the execution tree.
  68. Status Iterator::BuildAndLaunchTree(std::shared_ptr<Dataset> ds, int32_t num_epochs) {
  69. runtime_context_ = std::make_unique<NativeRuntimeContext>();
  70. RETURN_IF_NOT_OK(runtime_context_->Init());
  71. auto consumer = std::make_unique<IteratorConsumer>(num_epochs);
  72. consumer_ = consumer.get();
  73. RETURN_IF_NOT_OK(consumer->Init(ds->IRNode()));
  74. runtime_context_->AssignConsumer(std::move(consumer));
  75. return Status::OK();
  76. }
  77. PullIterator::PullIterator() : pull_consumer_(nullptr) {}
  78. // Get the next row from the data pipeline.
  79. Status PullIterator::GetRows(int32_t num_rows, std::vector<MSTensorVec> *row) {
  80. for (int i = 0; i < num_rows; i++) {
  81. std::vector<std::shared_ptr<dataset::Tensor>> md_row;
  82. Status rc = pull_consumer_->GetNextAsVector(&md_row);
  83. if (rc.IsError()) {
  84. row->clear();
  85. MS_LOG(ERROR) << "GetNextRow: Failed to get next row. Error status: " << rc;
  86. return rc;
  87. }
  88. MSTensorVec ms_row = {};
  89. for (auto de_tensor : md_row) {
  90. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(), "Apply transform failed, output tensor has no data");
  91. ms_row.push_back(mindspore::MSTensor(std::make_shared<DETensor>(de_tensor)));
  92. }
  93. row->push_back(ms_row);
  94. }
  95. return Status::OK();
  96. }
  97. Status PullIterator::GetNextRow(MSTensorVec *row) {
  98. CHECK_FAIL_RETURN_UNEXPECTED(pull_consumer_ != nullptr, "Consumer is nullptr.");
  99. std::vector<std::shared_ptr<dataset::Tensor>> md_row;
  100. Status rc = pull_consumer_->GetNextAsVector(&md_row);
  101. if (rc.IsError()) {
  102. row->clear();
  103. MS_LOG(ERROR) << "GetNextRow: Failed to get next row. Error status: " << rc;
  104. return rc;
  105. }
  106. for (auto de_tensor : md_row) {
  107. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(), "Apply transform failed, output tensor has no data");
  108. row->push_back(mindspore::MSTensor(std::make_shared<DETensor>(de_tensor)));
  109. }
  110. return Status::OK();
  111. }
  112. // Function to build and launch the execution tree. This function kicks off a different type of consumer
  113. // for the tree, the reason why this is the case is due to the fact that PullBasedIterator does not need
  114. // to instantiate threads for each op. As such, the call to the consumer will by pass the execution tree.
  115. Status PullIterator::BuildAndLaunchTree(std::shared_ptr<Dataset> ds) {
  116. if (pull_consumer_ == nullptr) pull_consumer_ = std::make_unique<PullBasedIteratorConsumer>();
  117. RETURN_IF_NOT_OK(pull_consumer_->Init(std::move(ds->IRNode())));
  118. return Status::OK();
  119. }
  120. } // namespace dataset
  121. } // namespace mindspore