• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_REPEAT_NODE_H_
18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_REPEAT_NODE_H_
19 
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 #include "minddata/dataset/engine/ir/datasetops/dataset_node.h"
27 
28 namespace mindspore {
29 namespace dataset {
30 
31 class RepeatOp;
32 
33 class RepeatNode : public DatasetNode {
34   // Allow GeneratorNode to access internal members
35   friend class GeneratorNode;
36 
37  public:
38   /// \brief Constructor
RepeatNode()39   RepeatNode() : op_(nullptr), reset_ancestor_(nullptr), repeat_count_(-1) {}
40 
41   /// \brief Constructor
42   RepeatNode(std::shared_ptr<DatasetNode> child, int32_t count);
43 
44   /// \brief Destructor
45   ~RepeatNode() = default;
46 
47   /// \brief Node name getter
48   /// \return Name of the current node
Name()49   std::string Name() const override { return kRepeatNode; }
50 
51   /// \brief Print the description
52   /// \param out - The output stream to write output to
53   void Print(std::ostream &out) const override;
54 
55   /// \brief Copy the node to a new object
56   /// \return A shared pointer to the new copy
57   std::shared_ptr<DatasetNode> Copy() override;
58 
59   /// \brief a base class override function to create the required runtime dataset op objects for this class
60   /// \param node_ops - A vector containing shared pointer to the Dataset Ops that this object will create
61   /// \return Status Status::OK() if build successfully
62   Status Build(std::vector<std::shared_ptr<DatasetOp>> *const node_ops) override;
63 
64   /// \brief Parameters validation
65   /// \return Status Status::OK() if all the parameters are valid
66   Status ValidateParams() override;
67 
68   /// \brief Getter
69   /// \return Number of cycles to repeat the execution
Count()70   const int32_t Count() const { return repeat_count_; }
71 
72   /// \brief Base-class override for GetDatasetSize
73   /// \param[in] size_getter Shared pointer to DatasetSizeGetter
74   /// \param[in] estimate This is only supported by some of the ops and it's used to speed up the process of getting
75   ///     dataset size at the expense of accuracy.
76   /// \param[out] dataset_size the size of the dataset
77   /// \return Status of the function
78   Status GetDatasetSize(const std::shared_ptr<DatasetSizeGetter> &size_getter, bool estimate,
79                         int64_t *dataset_size) override;
80 
81   /// \brief Base-class override for accepting IRNodePass visitor
82   /// \param[in] p The node to visit
83   /// \param[out] modified Indicator if the node was modified
84   /// \return Status of the node visit
85   Status Accept(IRNodePass *const p, bool *const modified) override;
86 
87   /// \brief Base-class override for accepting IRNodePass visitor
88   /// \param[in] p The node to visit
89   /// \param[out] modified Indicator if the node was modified
90   /// \return Status of the node visit
91   Status AcceptAfter(IRNodePass *const p, bool *const modified) override;
92 
93   /// \brief Record the Repeat/EpochCtrl node that is the closest ancestor of this node
94   /// \param[in] the ancestor node
95   /// \return Status of the function
AddResetAncestor(const std::shared_ptr<RepeatNode> & src)96   Status AddResetAncestor(const std::shared_ptr<RepeatNode> &src) {
97     /*
98      * This check is to ensure we don't overwrite an existing value of its ancestor.
99      * It is okay to assign to the same value more than once in RepeatNode (but not in GeneratorNode).
100      * Consider the following scenario
101      *       EpochCtrl(-1)
102      *           |
103      *        Repeat
104      *           |
105      *        Concat
106      *        /    \
107      *  GenData1  GenData2
108      *
109      * We will record the ancestor relationship of (Repeat, EpochCtrl) twice, one at Visit(GenData1), the other at
110      * Vist(GenData2).
111      */
112     CHECK_FAIL_RETURN_UNEXPECTED(reset_ancestor_ == nullptr || reset_ancestor_ == src,
113                                  "Internal error: Overwriting an existing value");
114     reset_ancestor_ = src;
115     return Status::OK();
116   }
117 
118   /// \brief Getter functions
RepeatCount()119   int32_t RepeatCount() const { return repeat_count_; }
120 
121   /// \brief Get the arguments of node
122   /// \param[out] out_json JSON string of all attributes
123   /// \return Status of the function
124   Status to_json(nlohmann::json *out_json) override;
125 
126   /// \brief Function for read dataset operation from json
127   /// \param[in] json_obj The JSON object to be deserialized
128   /// \param[in] ds dataset node constructed
129   /// \param[out] result Deserialized dataset after the operation
130   /// \return Status The status code returned
131   static Status from_json(nlohmann::json json_obj, std::shared_ptr<DatasetNode> ds,
132                           std::shared_ptr<DatasetNode> *result);
133 
134  protected:
135   std::shared_ptr<RepeatOp> op_;                // keep its corresponding run-time op of EpochCtrlNode and RepeatNode
136   std::shared_ptr<RepeatNode> reset_ancestor_;  // updated its immediate Repeat/EpochCtrl ancestor in GeneratorNodePass
137   int32_t repeat_count_;
138 };
139 
140 }  // namespace dataset
141 }  // namespace mindspore
142 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_REPEAT_NODE_H_
143