• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 
17 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_TREE_ADAPTER_LITE_H_
18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_TREE_ADAPTER_LITE_H_
19 
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 #include <vector>
25 
26 #include "minddata/dataset/engine/ir/datasetops/dataset_node.h"
27 
28 namespace mindspore {
29 namespace dataset {
30 
31 class TensorRow;
32 class DatasetNode;
33 
34 class TreeAdapterLite {
35  public:
36   // this flag is used to indicate the purpose of the creation of this tree adapter (type of the tree_consumer).
37   // Currently there are 3 types of consumer, Iterator, Getter and TDT/Vocab/Save ...
38   // To avoid premature optimization, the last type (TDT/Vocab/Save) is regarded as Iterator for now.
39   enum UsageFlag { kDeIterator = 0, kDeGetter = 1, kDeReset = 2 };
40 
41   explicit TreeAdapterLite(UsageFlag usage = kDeGetter);
42 
43   ~TreeAdapterLite() = default;
44 
45   Status BuildTree(std::shared_ptr<DatasetNode> root_ir);
46 
47   // Get rows equal to num_rows
48   Status GetNextRow(TensorRow *const row);
49 
GetColumnNameMap()50   std::unordered_map<std::string, int32_t> GetColumnNameMap() const { return tree_->root()->column_name_id_map(); }
51 
52   // unique_ptr overloads operator bool(), will return false if it doesn't manage an object
GetRoot()53   std::weak_ptr<DatasetOp> GetRoot() const { return tree_ ? tree_->root() : nullptr; }
54 
55   // This function performs syntax checking, semantics checking, and then call BuildTree
56   Status Compile(const std::shared_ptr<DatasetNode> &input_ir, int32_t num_epochs = -1);
57 
58  protected:
59   // Run the mandatory pass checking the syntax and semantics of the IR tree
60   Status PrePass(std::shared_ptr<DatasetNode> ir);
61 
62   // Run the mandatory pass augmenting the IR tree
63   Status PostPass(std::shared_ptr<DatasetNode> ir) const;
64 
65   // Return Offload Json
66   nlohmann::json GetOffloadJson();
67 
68   std::shared_ptr<DatasetNode> input_ir_;
69   std::shared_ptr<DatasetNode> root_ir_;
70 
71  private:
72   // This RECURSIVE function walks the (optimized) IR tree in DFS to build its corresponding Execution tree.
73   Status BuildExecutionTreeRecur(std::shared_ptr<DatasetNode> ir, std::shared_ptr<DatasetOp> *op);
74 
75   std::shared_ptr<DatasetOp> root_;  // current connector capacity of root op, used for profiling
76   std::unique_ptr<ExecutionTree> tree_;
77   UsageFlag usage_;  // usage of this tree adapter (type of consumer)
78   nlohmann::json offload_json_;
79 };
80 
81 }  // namespace dataset
82 }  // namespace mindspore
83 
84 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_TREE_ADAPTER_LITE_H_
85