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

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