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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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::GetNextRowCharIF(MSTensorMapChar *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. 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 buffer
  46. row->clear();
  47. // create a dataset tensor row and fetch. Then we convert the output to MSTensor
  48. std::vector<std::shared_ptr<dataset::Tensor>> md_row;
  49. Status rc = consumer_->GetNextAsVector(&md_row);
  50. if (rc.IsError()) {
  51. row->clear();
  52. return rc;
  53. }
  54. for (auto de_tensor : md_row) {
  55. CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(), "Apply transform failed, output tensor has no data");
  56. row->push_back(mindspore::MSTensor(std::make_shared<DETensor>(de_tensor)));
  57. }
  58. return Status::OK();
  59. }
  60. // Shut down the data pipeline.
  61. void Iterator::Stop() {
  62. Status rc = runtime_context_->Terminate();
  63. if (rc.IsError()) {
  64. MS_LOG(ERROR) << rc.ToString();
  65. }
  66. }
  67. // Function to build and launch the execution tree.
  68. Status Iterator::BuildAndLaunchTree(std::shared_ptr<Dataset> ds, int32_t num_epochs) {
  69. runtime_context_ = std::make_unique<NativeRuntimeContext>();
  70. RETURN_IF_NOT_OK(runtime_context_->Init());
  71. auto consumer = std::make_unique<IteratorConsumer>(num_epochs);
  72. consumer_ = consumer.get();
  73. RETURN_IF_NOT_OK(consumer->Init(ds->IRNode()));
  74. runtime_context_->AssignConsumer(std::move(consumer));
  75. return Status::OK();
  76. }
  77. } // namespace dataset
  78. } // namespace mindspore