2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONERT_IR_MODEL_H__
18 #define __ONERT_IR_MODEL_H__
21 #include <unordered_map>
23 #include "ir/IGraph.h"
25 #include "util/ObjectManager.h"
35 } // namespace backend
47 Model(const Model &obj) = default;
48 Model(Model &&) = default;
49 Model &operator=(const Model &) = default;
50 Model &operator=(Model &&) = default;
54 * @brief Put subgraph in the container with a new Index for that
56 * @param[in] subg Subgraph to be pushed
57 * @param[in] index Index of subgraph to be pushed
60 void push(SubgraphIndex index, const std::shared_ptr<IGraph> &subg) { _subgraphs[index] = subg; }
63 * @brief Remove the subgraph that is associated with the given index
65 * @param[in] index Index of the subgraph to be removed
68 void remove(const SubgraphIndex &index) { _subgraphs.erase(index); }
71 * @brief Get the subgraph that is associated with the given index
73 * @param[in] index Index of the subgraph to be returned
76 const std::shared_ptr<IGraph> &at(const SubgraphIndex &index) const
78 return _subgraphs.at(index);
81 * @brief Get the subgraph that is associated with the given index
83 * @param[in] index Index of the subgraph to be returned
86 std::shared_ptr<IGraph> &at(const SubgraphIndex &index) { return _subgraphs.at(index); }
89 * @brief Get the subgraph that is associated with the given index
91 * @param[in] index Index of the subgraph to be returned
92 * @return true if such entry exists otherwise false
94 bool exist(const SubgraphIndex &index) const
96 auto it = _subgraphs.find(index);
97 return it != _subgraphs.end();
101 * @brief Iterate over the container with given function
103 * @param[in] fn Function to be run for every container entry
106 void iterate(const std::function<void(const SubgraphIndex &, const IGraph &)> &fn) const
108 for (const auto &e : _subgraphs)
110 fn(e.first, *e.second);
115 * @brief Iterate over the container with given function
117 * @param[in] fn Function to be run for every container entry
120 void iterate(const std::function<void(const SubgraphIndex &, IGraph &)> &fn)
122 for (const auto &e : _subgraphs)
124 fn(e.first, *e.second);
129 * @brief Get count of Subgraphs
131 * @return count of Subgraphs
133 size_t subgraphs_count() const { return _subgraphs.size(); }
136 * @brief Return the primary subgraph
138 * @return std::shared_ptr<IGraph> Primary subgraph
140 std::shared_ptr<IGraph> primary_subgraph() const { return _subgraphs.at(SubgraphIndex{0}); }
143 * @brief Return whether the model has only typename Graph
145 * @tparam Graph Type that inherits from IGraph
147 * @return true if the model has only typename Graph, otherwise false
149 template <typename Graph, std::enable_if_t<std::is_base_of<IGraph, Graph>::value, bool> = true>
152 for (const auto &e : _subgraphs)
154 if (std::dynamic_pointer_cast<Graph>(e.second) == nullptr)
161 std::unordered_map<SubgraphIndex, std::shared_ptr<IGraph>> _subgraphs;
163 // Custom operations support
166 bindKernelBuilder(const std::shared_ptr<onert::backend::custom::IKernelBuilder> &kernel_builder)
168 _kernel_builder = kernel_builder;
171 const std::shared_ptr<backend::custom::IKernelBuilder> &getKernelBuilder() const
173 return _kernel_builder;
177 std::shared_ptr<backend::custom::IKernelBuilder> _kernel_builder;
183 #endif // __ONERT_IR_MODEL_H__