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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor.second->HasData(), "Apply transform failed, output tensor has no data");
  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 buffer
  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. for (auto de_tensor : md_row) {
  56. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(), "Apply transform failed, output tensor has no data");
  57. row->push_back(mindspore::MSTensor(std::make_shared<DETensor>(de_tensor)));
  58. }
  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. RETURN_IF_NOT_OK(runtime_context_->Init());
  74. auto consumer = std::make_unique<IteratorConsumer>(num_epochs);
  75. consumer_ = consumer.get();
  76. RETURN_IF_NOT_OK(consumer->Init(ds->IRNode()));
  77. runtime_context_->AssignConsumer(std::move(consumer));
  78. return Status::OK();
  79. }
  80. PullIterator::PullIterator() : pull_consumer_(nullptr) {}
  81. // Get the next row from the data pipeline.
  82. Status PullIterator::GetRows(int32_t num_rows, std::vector<MSTensorVec> *row) {
  83. for (int i = 0; i < num_rows; i++) {
  84. std::vector<std::shared_ptr<dataset::Tensor>> md_row;
  85. Status rc = pull_consumer_->GetNextAsVector(&md_row);
  86. if (rc.IsError()) {
  87. row->clear();
  88. MS_LOG(ERROR) << "GetNextRow: Failed to get next row. Error status: " << rc;
  89. return rc;
  90. }
  91. MSTensorVec ms_row = {};
  92. for (auto de_tensor : md_row) {
  93. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(), "Apply transform failed, output tensor has no data");
  94. ms_row.push_back(mindspore::MSTensor(std::make_shared<DETensor>(de_tensor)));
  95. }
  96. row->push_back(ms_row);
  97. }
  98. return Status::OK();
  99. }
  100. Status PullIterator::GetNextRow(MSTensorVec *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. } // namespace dataset
  124. } // namespace mindspore