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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Copyright 2020 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/tree_consumer.h"
  19. #include "minddata/dataset/engine/runtime_context.h"
  20. #include "minddata/dataset/include/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::GetNextRow(MSTensorMap *row) {
  27. // Clean data buffer
  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. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor.second->HasData(), "Apply transform failed, output tensor has no data");
  38. row->insert(std::make_pair(de_tensor.first, 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 buffer
  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. for (auto de_tensor : md_row) {
  54. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(), "Apply transform failed, output tensor has no data");
  55. row->push_back(mindspore::MSTensor(std::make_shared<DETensor>(de_tensor)));
  56. }
  57. return Status::OK();
  58. }
  59. // Shut down the data pipeline.
  60. void Iterator::Stop() {
  61. Status rc = runtime_context_->Terminate();
  62. if (rc.IsError()) {
  63. MS_LOG(ERROR) << rc.ToString();
  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. } // namespace dataset
  77. } // namespace mindspore