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

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