ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Model.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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 __ONERT_IR_MODEL_H__
18#define __ONERT_IR_MODEL_H__
19
20#include <memory>
21#include <unordered_map>
22
23#include "ir/IGraph.h"
24#include "ir/Index.h"
25#include "util/ObjectManager.h"
26
28{
29class IKernelBuilder;
30} // namespace onert::backend::custom
31
32namespace onert::ir
33{
34
35class Model
36{
37public:
38 Model() = default;
39 Model(const Model &obj) = default;
40 Model(Model &&) = default;
41 Model &operator=(const Model &) = default;
42 Model &operator=(Model &&) = default;
43 ~Model() = default;
44
52 void push(SubgraphIndex index, const std::shared_ptr<IGraph> &subg) { _subgraphs[index] = subg; }
53
60 void remove(const SubgraphIndex &index) { _subgraphs.erase(index); }
61
68 const std::shared_ptr<IGraph> &at(const SubgraphIndex &index) const
69 {
70 return _subgraphs.at(index);
71 }
78 std::shared_ptr<IGraph> &at(const SubgraphIndex &index) { return _subgraphs.at(index); }
79
86 bool exist(const SubgraphIndex &index) const
87 {
88 auto it = _subgraphs.find(index);
89 return it != _subgraphs.end();
90 }
91
98 void iterate(const std::function<void(const SubgraphIndex &, const IGraph &)> &fn) const
99 {
100 for (const auto &[subg_idx, subg] : _subgraphs)
101 {
102 fn(subg_idx, *subg);
103 }
104 }
105
112 void iterate(const std::function<void(const SubgraphIndex &, IGraph &)> &fn)
113 {
114 for (const auto &[subg_idx, subg] : _subgraphs)
115 {
116 fn(subg_idx, *subg);
117 }
118 }
119
125 size_t subgraphs_count() const { return _subgraphs.size(); }
126
132 std::shared_ptr<IGraph> primary_subgraph() const { return _subgraphs.at(SubgraphIndex{0}); }
133
141 template <typename Graph, std::enable_if_t<std::is_base_of_v<IGraph, Graph>, bool> = true>
142 bool hasOnly()
143 {
144 for (const auto &e : _subgraphs)
145 {
146 if (std::dynamic_pointer_cast<Graph>(e.second) == nullptr)
147 return false;
148 }
149 return true;
150 }
151
152private:
153 std::unordered_map<SubgraphIndex, std::shared_ptr<IGraph>> _subgraphs;
154
155 // Custom operations support
156public:
157 void
158 bindKernelBuilder(const std::shared_ptr<onert::backend::custom::IKernelBuilder> &kernel_builder)
159 {
160 _kernel_builder = kernel_builder;
161 }
162
163 const std::shared_ptr<backend::custom::IKernelBuilder> &getKernelBuilder() const
164 {
165 return _kernel_builder;
166 }
167
168private:
169 std::shared_ptr<backend::custom::IKernelBuilder> _kernel_builder;
170
171public:
172 void add_metadata(const std::string &name, std::unique_ptr<const ir::Data> data)
173 {
174 _metadatas.emplace(name, std::move(data));
175 }
176
177 bool exists_metadata(const std::string &name) const
178 {
179 return _metadatas.find(name) != _metadatas.end();
180 }
181
182 // NOTE The corresponding metadata is deleted from the model and returned
183 std::unique_ptr<const ir::Data> extract_metadata(const std::string name)
184 {
185 auto m = _metadatas.find(name);
186
187 if (m == _metadatas.end())
188 throw std::out_of_range{"no meatdata named " + name};
189
190 auto data = std::move(m->second);
191 _metadatas.erase(m);
192 return data;
193 }
194
195private:
196 // TODO: Apply Heterogeneous lookup for unordered containers (transparent hashing) since C++20
197 // to use `std::string_view` with lookup functions in unordered containers
198 std::unordered_map<std::string, std::unique_ptr<const ir::Data>> _metadatas;
199};
200} // namespace onert::ir
201
202#endif // __ONERT_IR_MODEL_H__
Model(const Model &obj)=default
std::unique_ptr< const ir::Data > extract_metadata(const std::string name)
Definition Model.h:183
void iterate(const std::function< void(const SubgraphIndex &, IGraph &)> &fn)
Iterate over the container with given function.
Definition Model.h:112
std::shared_ptr< IGraph > & at(const SubgraphIndex &index)
Get the subgraph that is associated with the given index.
Definition Model.h:78
std::shared_ptr< IGraph > primary_subgraph() const
Return the primary subgraph.
Definition Model.h:132
bool exist(const SubgraphIndex &index) const
Get the subgraph that is associated with the given index.
Definition Model.h:86
Model & operator=(const Model &)=default
Model & operator=(Model &&)=default
void iterate(const std::function< void(const SubgraphIndex &, const IGraph &)> &fn) const
Iterate over the container with given function.
Definition Model.h:98
void remove(const SubgraphIndex &index)
Remove the subgraph that is associated with the given index.
Definition Model.h:60
size_t subgraphs_count() const
Get count of Subgraphs.
Definition Model.h:125
bool hasOnly()
Return whether the model has only typename Graph.
Definition Model.h:142
~Model()=default
Model(Model &&)=default
const std::shared_ptr< backend::custom::IKernelBuilder > & getKernelBuilder() const
Definition Model.h:163
bool exists_metadata(const std::string &name) const
Definition Model.h:177
void add_metadata(const std::string &name, std::unique_ptr< const ir::Data > data)
Definition Model.h:172
void bindKernelBuilder(const std::shared_ptr< onert::backend::custom::IKernelBuilder > &kernel_builder)
Definition Model.h:158
void push(SubgraphIndex index, const std::shared_ptr< IGraph > &subg)
Put subgraph in the container with a new Index for that.
Definition Model.h:52
const std::shared_ptr< IGraph > & at(const SubgraphIndex &index) const
Get the subgraph that is associated with the given index.
Definition Model.h:68