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.

tree_adapter.cc 3.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/engine/tree_adapter.h"
  17. #include "minddata/dataset/core/client.h"
  18. #include "minddata/dataset/include/datasets.h"
  19. namespace mindspore {
  20. namespace dataset {
  21. Status TreeAdapter::BuildAndPrepare(std::shared_ptr<api::Dataset> root_ir, int32_t num_epoch) {
  22. // Check whether this function has been called before. If so, return failure
  23. CHECK_FAIL_RETURN_UNEXPECTED(tree_ == nullptr, "ExecutionTree is already built.");
  24. RETURN_UNEXPECTED_IF_NULL(root_ir);
  25. // This will evolve in the long run
  26. tree_ = std::make_unique<ExecutionTree>();
  27. std::shared_ptr<DatasetOp> root_op;
  28. RETURN_IF_NOT_OK(DFSBuildTree(root_ir, &root_op));
  29. RETURN_IF_NOT_OK(tree_->AssignRoot(root_op));
  30. // Prepare the tree
  31. RETURN_IF_NOT_OK(tree_->Prepare(num_epoch));
  32. // After the tree is prepared, the col_name_id_map can safely be obtained
  33. column_name_map_ = tree_->root()->column_name_id_map();
  34. return Status::OK();
  35. }
  36. Status TreeAdapter::GetNext(TensorRow *row) {
  37. RETURN_UNEXPECTED_IF_NULL(tree_);
  38. RETURN_UNEXPECTED_IF_NULL(row);
  39. row->clear(); // make sure row is empty
  40. // When cur_db_ is a nullptr, it means this is the first call to get_next, launch ExecutionTree
  41. if (cur_db_ == nullptr) {
  42. RETURN_IF_NOT_OK(tree_->Launch());
  43. RETURN_IF_NOT_OK(tree_->root()->GetNextBuffer(&cur_db_)); // first buf can't be eof or empty buf with none flag
  44. RETURN_OK_IF_TRUE(cur_db_->eoe()); // return empty tensor if 1st buf is a ctrl buf (no rows)
  45. }
  46. CHECK_FAIL_RETURN_UNEXPECTED(!cur_db_->eof(), "EOF has already been reached.");
  47. if (cur_db_->NumRows() == 0) { // a new row is fetched if cur buf is empty or a ctrl buf
  48. RETURN_IF_NOT_OK(tree_->root()->GetNextBuffer(&cur_db_));
  49. RETURN_OK_IF_TRUE(cur_db_->eoe() || cur_db_->eof()); // return empty if this new buffer is a ctrl flag
  50. }
  51. RETURN_IF_NOT_OK(cur_db_->PopRow(row));
  52. return Status::OK();
  53. }
  54. Status TreeAdapter::DFSBuildTree(std::shared_ptr<api::Dataset> ir, std::shared_ptr<DatasetOp> *op) {
  55. std::vector<std::shared_ptr<DatasetOp>> ops = ir->Build();
  56. CHECK_FAIL_RETURN_UNEXPECTED(!ops.empty(), "Unable to build node.");
  57. (*op) = ops.front(); // return the first op to be added as child by the caller of this function
  58. RETURN_IF_NOT_OK(tree_->AssociateNode(*op));
  59. for (size_t i = 1; i < ops.size(); i++) {
  60. RETURN_IF_NOT_OK(tree_->AssociateNode(ops[i]));
  61. RETURN_IF_NOT_OK(ops[i - 1]->AddChild(ops[i]));
  62. }
  63. // Build the children of ir, once they return, add the return value to *op
  64. for (std::shared_ptr<api::Dataset> child_ir : ir->children) {
  65. std::shared_ptr<DatasetOp> child_op;
  66. RETURN_IF_NOT_OK(DFSBuildTree(child_ir, &child_op));
  67. RETURN_IF_NOT_OK(ops.back()->AddChild(child_op)); // append children to the last of ops
  68. }
  69. return Status::OK();
  70. }
  71. } // namespace dataset
  72. } // namespace mindspore