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

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